packages

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.

nabi
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)));
  • pub min: Vec3<T>§

    Source
  • pub max: Vec3<T>§

    Source
  • pub const fn new(min: Vec3<T>, max: Vec3<T>) -> Aabb<T>§

    Source

    The box from min to max.

    nabi
    use spatial::{Aabb, Vec3};
    
    assert(Aabb::new(Vec3::ZERO, Vec3::ONE).size() == Vec3::ONE);
    
  • pub fn from_center_half_extents(center: Vec3<T>, half: Vec3<T>) -> Aabb<T>§

    Source

    The box center - half..center + half.

    nabi
    use spatial::{Aabb, Vec3};
    
    assert(Aabb::from_center_half_extents(Vec3::ZERO, Vec3::ONE).min == -Vec3::ONE);
    
  • pub fn from_points(points: List<Vec3<T>>) -> Aabb<T> | none§

    Source

    The smallest box around points, none for no points.

    nabi
    use 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));
    
  • pub fn center(self) -> Vec3<T>§

    Source

    The middle.

    nabi
    use spatial::{Aabb, Vec3};
    
    assert(Aabb::new(Vec3::ZERO, Vec3::splat(2.0)).center() == Vec3::ONE);
    
  • pub fn size(self) -> Vec3<T>§

    Source

    max - min.

    nabi
    use spatial::{Aabb, Vec3};
    
    assert(Aabb::new(Vec3::ZERO, Vec3::splat(2.0)).size() == Vec3::splat(2.0));
    
  • pub fn half_extents(self) -> Vec3<T>§

    Source

    Half the size, center to face.

    nabi
    use spatial::{Aabb, Vec3};
    
    assert(Aabb::new(Vec3::ZERO, Vec3::splat(2.0)).half_extents() == Vec3::ONE);
    
  • pub fn is_empty(self) -> bool§

    Source

    Whether min <= max fails on some axis.

    nabi
    use spatial::{Aabb, Vec3};
    
    assert(Aabb::new(Vec3::ONE, Vec3::ZERO).is_empty());
    
  • pub fn contains(self, p: Vec3<T>) -> bool§

    Source

    Whether p is inside or on the boundary.

    nabi
    use spatial::{Aabb, Vec3};
    
    assert(Aabb::new(Vec3::ZERO, Vec3::ONE).contains(Vec3::ONE));
    
  • pub fn intersects(self, o: Aabb<T>) -> bool§

    Source

    Whether the boxes overlap; touching counts.

    nabi
    use spatial::{Aabb, Vec3};
    
    let a = Aabb::new(Vec3::ZERO, Vec3::ONE);
    assert(a.intersects(Aabb::new(Vec3::ONE, Vec3::splat(2.0))));
    
  • pub fn union(self, o: Aabb<T>) -> Aabb<T>§

    Source

    The smallest box around both.

    nabi
    use 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));
    
  • pub fn extend(self, p: Vec3<T>) -> Aabb<T>§

    Source

    The smallest box around self and the point p.

    nabi
    use spatial::{Aabb, Vec3};
    
    assert(Aabb::new(Vec3::ZERO, Vec3::ZERO).extend(Vec3::ONE).max == Vec3::ONE);
    
  • pub fn closest_point(self, p: Vec3<T>) -> Vec3<T>§

    Source

    The point of the box nearest p; p itself when inside.

    nabi
    use spatial::{Aabb, Vec3};
    
    assert(Aabb::new(Vec3::ZERO, Vec3::ONE).closest_point(Vec3::splat(3.0)) == Vec3::ONE);
    
  • pub fn distance_squared(self, p: Vec3<T>) -> T§

    Source

    The squared distance from p to the box, 0 inside.

    nabi
    use spatial::{Aabb, Vec3};
    
    assert(Aabb::new(Vec3::ZERO, Vec3::ONE).distance_squared(Vec3::new(3.0, 0.5, 0.5)) == 4.0);
    
  • pub fn transform(self, m: Mat4<T>) -> Aabb<T>§

    Source

    The box around this box transformed by the affine matrix m: not tight for a rotation, but it contains every transformed corner.

    nabi
    use 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.

nabi
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);
  • pub normal: Vec3<T>§

    Source
  • pub d: T§

    Source
  • pub const fn new(normal: Vec3<T>, d: T) -> Plane<T>§

    Source

    The plane normal . p + d = 0, as given.

    nabi
    use spatial::{Plane, Vec3};
    
    assert(Plane::new(Vec3::Y, -1.0).distance(Vec3::ZERO) == -1.0);
    
  • pub fn from_point_normal(p: Vec3<T>, n: Vec3<T>) -> Plane<T>§

    Source

    The plane through p with unit normal n.

    nabi
    use spatial::{Plane, Vec3};
    
    assert(Plane::from_point_normal(Vec3::Y, Vec3::Y).d == -1.0);
    
  • pub fn from_points(a: Vec3<T>, b: Vec3<T>, c: Vec3<T>) -> Plane<T>§

    Source

    The plane through three points, its normal toward the side from which a, b, c run counterclockwise. Points on one line give a NaN normal.

    nabi
    use spatial::{Plane, Vec3};
    
    let p = Plane::from_points(Vec3::ZERO, Vec3::X, Vec3::Y);
    assert(p.normal == Vec3::Z && p.d == 0.0);
    
  • pub fn from_vec4(v: Vec4<T>) -> Plane<T>§

    Source

    The plane of (a, b, c, d), as a frustum row gives it.

    nabi
    use spatial::{Plane, Vec4};
    
    assert(Plane::from_vec4(Vec4::new(0.0, 1.0, 0.0, 2.0)).d == 2.0);
    
  • pub fn to_vec4(self) -> Vec4<T>§

    Source

    (normal, d) as a Vec4.

    nabi
    use spatial::{Plane, Vec3, Vec4};
    
    assert(Plane::new(Vec3::Y, 2.0).to_vec4() == Vec4::new(0.0, 1.0, 0.0, 2.0));
    
  • pub fn normalize(self) -> Plane<T>§

    Source

    The same plane with a unit normal (needed for distance).

    nabi
    use spatial::{Plane, Vec3};
    
    assert(Plane::new(Vec3::new(0.0, 2.0, 0.0), 2.0).normalize() == Plane::new(Vec3::Y, 1.0));
    
  • pub fn distance(self, p: Vec3<T>) -> T§

    Source

    normal . p + d: the signed distance for a unit normal.

    nabi
    use spatial::{Plane, Vec3};
    
    assert(Plane::new(Vec3::Z, 0.0).distance(Vec3::new(0.0, 0.0, -3.0)) == -3.0);
    
  • pub fn project(self, p: Vec3<T>) -> Vec3<T>§

    Source

    The point of the plane nearest p (unit normal).

    nabi
    use 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.

nabi
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);
  • pub origin: Vec3<T>§

    Source
  • pub dir: Vec3<T>§

    Source
  • pub const fn new(origin: Vec3<T>, dir: Vec3<T>) -> Ray<T>§

    Source

    The ray from origin along dir.

    nabi
    use spatial::{Ray, Vec3};
    
    assert(Ray::new(Vec3::ZERO, Vec3::X).dir == Vec3::X);
    
  • pub fn at(self, t: T) -> Vec3<T>§

    Source

    The point at parameter t.

    nabi
    use spatial::{Ray, Vec3};
    
    assert(Ray::new(Vec3::ZERO, Vec3::X).at(2.0) == Vec3::new(2.0, 0.0, 0.0));
    
  • pub fn intersect_aabb(self, b: Aabb<T>) -> T | none§

    Source

    Where the ray first meets the box (slab test); 0 from inside.

    nabi
    use 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);
    
  • pub fn intersect_plane(self, p: Plane<T>) -> T | none§

    Source

    Where the ray meets the plane; none when parallel or behind.

    nabi
    use 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);
    
  • pub fn intersect_sphere(self, s: Sphere<T>) -> T | none§

    Source

    Where the ray first meets the sphere; 0 from inside.

    nabi
    use 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.

nabi
use spatial::{Sphere, Vec3};

let s = Sphere::new(Vec3::ZERO, 2.0);
assert(s.contains(Vec3::ONE) && !s.contains(Vec3::splat(2.0)));
  • pub center: Vec3<T>§

    Source
  • pub radius: T§

    Source
  • pub const fn new(center: Vec3<T>, radius: T) -> Sphere<T>§

    Source

    The sphere around center.

    nabi
    use spatial::{Sphere, Vec3};
    
    assert(Sphere::new(Vec3::ZERO, 1.0).radius == 1.0);
    
  • pub fn contains(self, p: Vec3<T>) -> bool§

    Source

    Whether p is inside or on the surface.

    nabi
    use spatial::{Sphere, Vec3};
    
    assert(Sphere::new(Vec3::ZERO, 1.0).contains(Vec3::X));
    
  • pub fn intersects(self, o: Sphere<T>) -> bool§

    Source

    Whether the spheres overlap; touching counts.

    nabi
    use spatial::{Sphere, Vec3};
    
    assert(Sphere::new(Vec3::ZERO, 1.0).intersects(Sphere::new(Vec3::new(2.0, 0.0, 0.0), 1.0)));
    
  • pub fn intersects_aabb(self, b: Aabb<T>) -> bool§

    Source

    Whether the sphere and the box overlap.

    nabi
    use 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));
    
  • pub fn aabb(self) -> Aabb<T>§

    Source

    The box around the sphere.

    nabi
    use spatial::{Aabb, Sphere, Vec3};
    
    assert(Sphere::new(Vec3::ZERO, 1.0).aabb() == Aabb::new(-Vec3::ONE, Vec3::ONE));