Skip to main content

loam_shape/
lib.rs

1//! Pose is extrinsic: shapes are defined in a local frame and positioned by
2//! the caller's transform. [`Shape::Sphere`] and [`Shape::HyperSphere4D`] are
3//! the exceptions, carrying a `center` that physics ignores.
4
5pub mod field;
6pub mod isovolume;
7pub mod polytope;
8pub mod polytope_geom;
9pub mod projected_edges;
10pub mod projection;
11pub mod visualizable;
12
13pub use field::{DistanceField, FieldKind};
14pub use isovolume::Isovolume;
15pub use visualizable::{LineMesh, NotVisualizable, PointMesh, TriangleMesh, Visualizable};
16
17use glam::{Vec2, Vec3, Vec4};
18use serde::{Deserialize, Serialize};
19
20#[derive(Clone, Debug, Serialize, Deserialize)]
21pub enum Shape {
22    Sphere {
23        center: Vec3,
24        radius: f32,
25    },
26
27    HalfSpace {
28        /// Assumed unit: `dot(p, normal) - offset` is read as a signed distance.
29        normal: Vec3,
30        offset: f32,
31    },
32
33    /// Only meaningful on a static body (`inv_mass = 0`).
34    HalfSpace4D {
35        /// Assumed unit, as in [`Shape::HalfSpace`].
36        normal: Vec4,
37        offset: f32,
38    },
39
40    Box3 {
41        /// The box spans `[-half_extents, half_extents]`.
42        half_extents: Vec3,
43    },
44
45    Polygon2D {
46        /// Counter-clockwise boundary loop in the local frame.
47        vertices: Vec<Vec2>,
48    },
49
50    ConvexPolytope3D {
51        /// Unordered point set in the shape frame; the collider is its hull.
52        vertices: Vec<Vec3>,
53    },
54
55    ConvexPolytope4D {
56        /// Unordered local points; the collider is their convex hull.
57        vertices: Vec<Vec4>,
58    },
59
60    HyperSphere4D {
61        center: Vec4,
62        radius: f32,
63    },
64}
65
66impl Shape {
67    pub fn kind(&self) -> ShapeKind {
68        match self {
69            Shape::Sphere { .. } => ShapeKind::Sphere,
70            Shape::HalfSpace { .. } => ShapeKind::HalfSpace,
71            Shape::HalfSpace4D { .. } => ShapeKind::HalfSpace4D,
72            Shape::Box3 { .. } => ShapeKind::Box3,
73            Shape::Polygon2D { .. } => ShapeKind::Polygon2D,
74            Shape::ConvexPolytope3D { .. } => ShapeKind::ConvexPolytope3D,
75            Shape::ConvexPolytope4D { .. } => ShapeKind::ConvexPolytope4D,
76            Shape::HyperSphere4D { .. } => ShapeKind::HyperSphere4D,
77        }
78    }
79
80    pub fn sphere_at_origin(radius: f32) -> Self {
81        Self::Sphere {
82            center: Vec3::ZERO,
83            radius,
84        }
85    }
86
87    pub fn sphere_at(center: Vec3, radius: f32) -> Self {
88        Self::Sphere { center, radius }
89    }
90}
91
92#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
93pub enum ShapeKind {
94    Sphere,
95    HalfSpace,
96    HalfSpace4D,
97    Box3,
98    Polygon2D,
99    ConvexPolytope3D,
100    ConvexPolytope4D,
101    HyperSphere4D,
102}