spatial::shape
On this page
Shapes for culling, picking and collision tests: boxes, rays, planes and spheres.
Structs
AabbSource
pub struct Aabb<T = f32>The box of points between min and max on every axis. Aabb
alone is Aabb<f32>. A box with min > max on some axis is empty.
#[repr(C)]: min then max.
use spatial::{Aabb, Vec3};
let b = Aabb::new(Vec3::ZERO, Vec3::ONE);
assert(b.contains(Vec3::splat(0.5)) && !b.contains(Vec3::splat(2.0)));
The box from
mintomax.nabiuse spatial::{Aabb, Vec3}; assert(Aabb::new(Vec3::ZERO, Vec3::ONE).size() == Vec3::ONE);The box
center - half..center + half.nabiuse spatial::{Aabb, Vec3}; assert(Aabb::from_center_half_extents(Vec3::ZERO, Vec3::ONE).min == -Vec3::ONE);The smallest box around
points,nonefor no points.nabiuse spatial::{Aabb, Vec3}; let b = Aabb::from_points([Vec3::X, Vec3::Y, -Vec3::Z]) ?? panic("no points"); assert(b.min == Vec3::new(0.0, 0.0, -1.0) && b.max == Vec3::new(1.0, 1.0, 0.0));The middle.
nabiuse spatial::{Aabb, Vec3}; assert(Aabb::new(Vec3::ZERO, Vec3::splat(2.0)).center() == Vec3::ONE);max - min.nabiuse spatial::{Aabb, Vec3}; assert(Aabb::new(Vec3::ZERO, Vec3::splat(2.0)).size() == Vec3::splat(2.0));Half the size, center to face.
nabiuse spatial::{Aabb, Vec3}; assert(Aabb::new(Vec3::ZERO, Vec3::splat(2.0)).half_extents() == Vec3::ONE);Whether
min <= maxfails on some axis.nabiuse spatial::{Aabb, Vec3}; assert(Aabb::new(Vec3::ONE, Vec3::ZERO).is_empty());Whether
pis inside or on the boundary.nabiuse spatial::{Aabb, Vec3}; assert(Aabb::new(Vec3::ZERO, Vec3::ONE).contains(Vec3::ONE));Whether the boxes overlap; touching counts.
nabiuse spatial::{Aabb, Vec3}; let a = Aabb::new(Vec3::ZERO, Vec3::ONE); assert(a.intersects(Aabb::new(Vec3::ONE, Vec3::splat(2.0))));The smallest box around both.
nabiuse spatial::{Aabb, Vec3}; let u = Aabb::new(Vec3::ZERO, Vec3::ONE).union(Aabb::new(-Vec3::ONE, Vec3::ZERO)); assert(u == Aabb::new(-Vec3::ONE, Vec3::ONE));The smallest box around
selfand the pointp.nabiuse spatial::{Aabb, Vec3}; assert(Aabb::new(Vec3::ZERO, Vec3::ZERO).extend(Vec3::ONE).max == Vec3::ONE);The point of the box nearest
p;pitself when inside.nabiuse spatial::{Aabb, Vec3}; assert(Aabb::new(Vec3::ZERO, Vec3::ONE).closest_point(Vec3::splat(3.0)) == Vec3::ONE);The squared distance from
pto the box, 0 inside.nabiuse spatial::{Aabb, Vec3}; assert(Aabb::new(Vec3::ZERO, Vec3::ONE).distance_squared(Vec3::new(3.0, 0.5, 0.5)) == 4.0);The box around this box transformed by the affine matrix
m: not tight for a rotation, but it contains every transformed corner.nabiuse spatial::{Aabb, Mat4, Vec3}; let b = Aabb::new(Vec3::ZERO, Vec3::ONE).transform(Mat4::from_translation(Vec3::X)); assert(b == Aabb::new(Vec3::X, Vec3::new(2.0, 1.0, 1.0)));
PlaneSource
pub struct Plane<T = f32>The points p with normal.dot(p) + d == 0. With a unit normal,
distance is the signed distance, positive on the side the normal
points to. #[repr(C)]: normal then d, the (a, b, c, d) of a
shader's plane equation.
use spatial::{Plane, Vec3};
let ground = Plane::from_point_normal(Vec3::ZERO, Vec3::Y);
assert(ground.distance(Vec3::new(5.0, 2.0, 1.0)) == 2.0);
The plane
normal . p + d = 0, as given.nabiuse spatial::{Plane, Vec3}; assert(Plane::new(Vec3::Y, -1.0).distance(Vec3::ZERO) == -1.0);The plane through
pwith unit normaln.nabiuse spatial::{Plane, Vec3}; assert(Plane::from_point_normal(Vec3::Y, Vec3::Y).d == -1.0);The plane through three points, its normal toward the side from which
a,b,crun counterclockwise. Points on one line give a NaN normal.nabiuse spatial::{Plane, Vec3}; let p = Plane::from_points(Vec3::ZERO, Vec3::X, Vec3::Y); assert(p.normal == Vec3::Z && p.d == 0.0);The plane of
(a, b, c, d), as a frustum row gives it.nabiuse spatial::{Plane, Vec4}; assert(Plane::from_vec4(Vec4::new(0.0, 1.0, 0.0, 2.0)).d == 2.0);(normal, d)as aVec4.nabiuse spatial::{Plane, Vec3, Vec4}; assert(Plane::new(Vec3::Y, 2.0).to_vec4() == Vec4::new(0.0, 1.0, 0.0, 2.0));The same plane with a unit normal (needed for
distance).nabiuse spatial::{Plane, Vec3}; assert(Plane::new(Vec3::new(0.0, 2.0, 0.0), 2.0).normalize() == Plane::new(Vec3::Y, 1.0));normal . p + d: the signed distance for a unit normal.nabiuse spatial::{Plane, Vec3}; assert(Plane::new(Vec3::Z, 0.0).distance(Vec3::new(0.0, 0.0, -3.0)) == -3.0);The point of the plane nearest
p(unit normal).nabiuse spatial::{Plane, Vec3}; assert(Plane::new(Vec3::Y, 0.0).project(Vec3::ONE) == Vec3::new(1.0, 0.0, 1.0));
RaySource
pub struct Ray<T = f32>The points origin + dir * t for t >= 0. The intersect_*
methods give the smallest such t that hits, or none; with a unit
dir, t is the distance. #[repr(C)]: origin then dir.
use spatial::{Ray, Sphere, Vec3};
let r = Ray::new(Vec3::new(0.0, 0.0, 5.0), -Vec3::Z);
assert(r.intersect_sphere(Sphere::new(Vec3::ZERO, 1.0)) == 4.0);
The ray from
originalongdir.nabiuse spatial::{Ray, Vec3}; assert(Ray::new(Vec3::ZERO, Vec3::X).dir == Vec3::X);The point at parameter
t.nabiuse spatial::{Ray, Vec3}; assert(Ray::new(Vec3::ZERO, Vec3::X).at(2.0) == Vec3::new(2.0, 0.0, 0.0));Where the ray first meets the box (slab test); 0 from inside.
nabiuse spatial::{Aabb, Ray, Vec3}; let b = Aabb::new(Vec3::splat(-1.0), Vec3::ONE); assert(Ray::new(Vec3::new(-5.0, 0.0, 0.0), Vec3::X).intersect_aabb(b) == 4.0); assert(Ray::new(Vec3::new(-5.0, 3.0, 0.0), Vec3::X).intersect_aabb(b) == none);Where the ray meets the plane;
nonewhen parallel or behind.nabiuse spatial::{Plane, Ray, Vec3}; let ground = Plane::new(Vec3::Y, 0.0); assert(Ray::new(Vec3::new(0.0, 2.0, 0.0), -Vec3::Y).intersect_plane(ground) == 2.0);Where the ray first meets the sphere; 0 from inside.
nabiuse spatial::{Ray, Sphere, Vec3}; let s = Sphere::new(Vec3::ZERO, 1.0); assert(Ray::new(Vec3::new(0.0, 2.0, 5.0), -Vec3::Z).intersect_sphere(s) == none);
SphereSource
pub struct Sphere<T = f32>The points within radius of center. #[repr(C)]: center then
radius, the vec4 a shader's bounding sphere often is.
use spatial::{Sphere, Vec3};
let s = Sphere::new(Vec3::ZERO, 2.0);
assert(s.contains(Vec3::ONE) && !s.contains(Vec3::splat(2.0)));
The sphere around
center.nabiuse spatial::{Sphere, Vec3}; assert(Sphere::new(Vec3::ZERO, 1.0).radius == 1.0);Whether
pis inside or on the surface.nabiuse spatial::{Sphere, Vec3}; assert(Sphere::new(Vec3::ZERO, 1.0).contains(Vec3::X));Whether the spheres overlap; touching counts.
nabiuse spatial::{Sphere, Vec3}; assert(Sphere::new(Vec3::ZERO, 1.0).intersects(Sphere::new(Vec3::new(2.0, 0.0, 0.0), 1.0)));Whether the sphere and the box overlap.
nabiuse spatial::{Aabb, Sphere, Vec3}; let b = Aabb::new(Vec3::ZERO, Vec3::ONE); assert(Sphere::new(Vec3::new(2.0, 0.5, 0.5), 1.0).intersects_aabb(b));The box around the sphere.
nabiuse spatial::{Aabb, Sphere, Vec3}; assert(Sphere::new(Vec3::ZERO, 1.0).aabb() == Aabb::new(-Vec3::ONE, Vec3::ONE));