Files
physics/geometry/bounds.sakura
T

121 lines
4.1 KiB
Plaintext
Raw Normal View History

2026-10-27 22:08:25 -05:00
#! Contains `bounds`, a box along the axes: the least corner and the greatest of
#! what it holds, as of a mesh, a body, or a part of a scene, so whether two
#! meet, or a ray or a camera reaches one, is a few comparisons.
import std::math;
import std::serial;
import linear;
module geometry;
#! A box along the axes, from its least corner to its greatest.
@serial
pub struct bounds {
@serial pub var min: vec3;
@serial pub var max: vec3;
}
#! The box holding nothing, from infinity to minus infinity, which takes the
#! place or box it's first grown by.
pub val bounds::empty := bounds{
vec3{ math::infinity::<f32>(), math::infinity::<f32>(), math::infinity::<f32>() },
vec3{ -math::infinity::<f32>(), -math::infinity::<f32>(), -math::infinity::<f32>() },
};
#! The box of a center, reaching so far from it along each axis.
pub def bounds::around(center: vec3, half_size: vec3): bounds {
return bounds{ center - half_size, center + half_size };
}
#! The least box holding places, or `empty` of none.
pub def bounds::of(points: span<vec3>): bounds {
var made := bounds::empty;
for p in points {
made = made.including(p);
}
return made;
}
#! The least box holding a sphere.
pub def bounds::of(s: sphere): bounds {
return bounds::around(s.center, vec3{ s.radius, s.radius, s.radius });
}
pub val bounds::center: vec3 {
get => (this.min + this.max) * 0.5;
}
pub val bounds::size: vec3 {
get => this.max - this.min;
}
#! How far it reaches from its center along each axis.
pub val bounds::half_size: vec3 {
get => (this.max - this.min) * 0.5;
}
#! Whether it holds nothing, as `empty` does: a least corner past its greatest.
pub val bounds::is_empty: bool {
get => this.min.x > this.max.x || this.min.y > this.max.y || this.min.z > this.max.z;
}
pub def bounds::equals(&this, other: &bounds): bool {
return this.min == other.min && this.max == other.max;
}
#! Whether a place is within it.
pub def bounds::contains(&this, p: vec3): bool {
return p.x >= this.min.x && p.y >= this.min.y && p.z >= this.min.z
&& p.x <= this.max.x && p.y <= this.max.y && p.z <= this.max.z;
}
#! Whether another box is within it whole.
pub def bounds::contains(&this, other: bounds): bool {
return this.contains(other.min) && this.contains(other.max);
}
#! Whether it and another box meet.
pub def bounds::overlaps(&this, other: bounds): bool {
return this.min.x <= other.max.x && this.min.y <= other.max.y && this.min.z <= other.max.z
&& this.max.x >= other.min.x && this.max.y >= other.min.y && this.max.z >= other.min.z;
}
#! Whether it and a sphere meet.
pub def bounds::overlaps(&this, s: sphere): bool {
return s.overlaps(this);
}
#! Its place nearest another: the place itself where it's within.
pub def bounds::closest(&this, p: vec3): vec3 {
return p.max(this.min).min(this.max);
}
#! The box grown to hold a place too.
pub def bounds::including(&this, p: vec3): bounds {
return bounds{ this.min.min(p), this.max.max(p) };
}
#! The least box holding it and another.
pub def bounds::merged(&this, other: bounds): bounds {
return bounds{ this.min.min(other.min), this.max.max(other.max) };
}
#! The least box along the axes holding this one moved, turned, and scaled by a
#! matrix that doesn't project: its center moved, and its reach along each of
#! its own axes turned, each number made positive, and added.
pub def bounds::transformed(&this, by: mat4): bounds {
return bounds::empty if this.is_empty;
val half := this.half_size;
val m := &by.m;
if lanes_held() {
# Each column as lanes, read where it lies, as `mat4`'s products read them.
val reach := lanes_at(&m[0]).abs() * f32x4::splat(half.x) + lanes_at(&m[4]).abs() * f32x4::splat(half.y)
+ lanes_at(&m[8]).abs() * f32x4::splat(half.z);
return bounds::around(by.transform_point(this.center), vec3::of_lanes(reach));
}
val reach := vec3{
math::abs(m[0]) * half.x + math::abs(m[4]) * half.y + math::abs(m[8]) * half.z,
math::abs(m[1]) * half.x + math::abs(m[5]) * half.y + math::abs(m[9]) * half.z,
math::abs(m[2]) * half.x + math::abs(m[6]) * half.y + math::abs(m[10]) * half.z,
};
return bounds::around(by.transform_point(this.center), reach);
}