Skip to main content

lib_jsl_geometry/
dim_3.rs

1//! Three-dimensional geometry traits.
2
3use lib_jsl_core::ErrorsJSL;
4/// Concrete three-dimensional line implementation.
5pub mod line3d;
6/// Concrete three-dimensional free-vector implementation.
7pub mod free_vector3d;
8/// Coordinate-system conversion helpers for 3D concrete coordinate vectors.
9pub(crate) mod coordinate_conversions;
10/// Concrete three-dimensional surface mesh implementation.
11pub mod surface_mesh3d;
12/// Concrete three-dimensional plane implementation.
13pub mod plane3d;
14/// Concrete three-dimensional coordinate-vector implementation.
15pub mod coordinate_vector3d;
16/// Concrete three-dimensional polygon face implementation.
17pub mod polygon_face3d;
18/// Concrete three-dimensional triangle implementation.
19pub mod triangle3d;
20/// Concrete three-dimensional tetrahedron implementation.
21pub mod tetrahedron3d;
22/// Concrete three-dimensional volume mesh implementation.
23pub mod volume_mesh3d;
24pub(crate) mod transform_support;
25/// Concrete three-dimensional unit vector implementation.
26pub mod unit_vector3d;
27
28use crate::common::{
29    FaceId, GeometricPrimitive, GeometryMeasure, HasCenter, HasCentroid, HasEdges, HasFaces,
30    HasTetrahedra, HasVertices, IsUnitVector, PointId, TetrahedronId,
31};
32use crate::one_d::IsLine;
33use crate::two_d::IsPolygon;
34use crate::zero_d::IsPoint;
35pub use line3d::Line3D;
36pub use free_vector3d::FreeVector3D;
37pub use surface_mesh3d::SurfaceMesh3D;
38pub use plane3d::Plane3D;
39pub use coordinate_vector3d::CoordinateVector3D;
40pub use polygon_face3d::PolygonFace3D;
41pub use tetrahedron3d::Tetrahedron3D;
42pub use triangle3d::Triangle3D;
43pub use unit_vector3d::UnitVector3D;
44pub use volume_mesh3d::VolumeMesh3D;
45
46/// A sphere primitive with radius-derived surface measures.
47pub trait IsSphere<T: IsPoint>:
48    GeometricPrimitive + HasCenter<Point = T> + crate::common::HasMeasure
49{
50    /// Returns the radius.
51    fn radius(&self) -> GeometryMeasure;
52    /// Returns the diameter.
53    fn diameter(&self) -> GeometryMeasure;
54    /// Returns the surface area.
55    fn surface_area(&self) -> GeometryMeasure;
56    /// Returns the enclosed volume.
57    fn volume(&self) -> GeometryMeasure;
58}
59
60/// A tetrahedron primitive backed by four vertices and derived triangular faces.
61pub trait IsTetrahedron<'a, T: IsPoint, N: IsUnitVector>:
62    GeometricPrimitive + HasVertices<'a, Vertex = T> + HasEdges + HasCentroid<Point = T> + crate::common::HasMeasure
63where
64    <Self as HasEdges>::Edge: IsLine<'a, T>,
65{
66    /// Returns the point-table id of the first vertex.
67    fn a_id(&self) -> PointId;
68    /// Sets the point-table id of the first vertex.
69    fn set_a_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL>;
70    /// Returns the point-table id of the second vertex.
71    fn b_id(&self) -> PointId;
72    /// Sets the point-table id of the second vertex.
73    fn set_b_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL>;
74    /// Returns the point-table id of the third vertex.
75    fn c_id(&self) -> PointId;
76    /// Sets the point-table id of the third vertex.
77    fn set_c_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL>;
78    /// Returns the point-table id of the fourth vertex.
79    fn d_id(&self) -> PointId;
80    /// Sets the point-table id of the fourth vertex.
81    fn set_d_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL>;
82
83    /// Resolves and returns the first vertex from the point table.
84    fn a(&self) -> Option<T> {
85        self.get_vertex(&self.a_id())
86    }
87
88    /// Resolves and returns the second vertex from the point table.
89    fn b(&self) -> Option<T> {
90        self.get_vertex(&self.b_id())
91    }
92
93    /// Resolves and returns the third vertex from the point table.
94    fn c(&self) -> Option<T> {
95        self.get_vertex(&self.c_id())
96    }
97
98    /// Resolves and returns the fourth vertex from the point table.
99    fn d(&self) -> Option<T> {
100        self.get_vertex(&self.d_id())
101    }
102
103    /// Returns the four derived triangular faces of the tetrahedron.
104    fn faces(&self) -> [Triangle3D; 4];
105    /// Returns the number of triangular faces.
106    fn face_count(&self) -> usize;
107    /// Returns the total surface area of the tetrahedron.
108    fn surface_area(&self) -> GeometryMeasure;
109    /// Returns the signed volume of the tetrahedron.
110    fn signed_volume(&self) -> GeometryMeasure;
111    /// Returns the absolute tetrahedron volume.
112    fn volume(&self) -> GeometryMeasure;
113}
114
115/// A surface mesh primitive with stored vertices and polygonal boundary faces.
116///
117/// This trait is explicitly for surface meshes rather than volumetric cell meshes. The current
118/// surface-mesh model assumes vertices are the primary stored table. Faces and edges may be backed
119/// by separate structures internally, but they are exposed through semantic mesh accessors rather
120/// than through the older generic single-table pattern.
121pub trait IsMesh<'a, T: IsPoint, N: IsUnitVector>:
122    GeometricPrimitive
123    + HasVertices<'a, Vertex = T>
124    + HasFaces<'a, Point = T, Normal = N>
125    + HasEdges
126where
127    <Self as HasEdges>::Edge: IsLine<'a, T>,
128{
129    /// Returns the ordered face-table ids that participate in this mesh.
130    fn face_ids(&self) -> Box<dyn Iterator<Item = FaceId> + '_>;
131    /// Replaces the face-table id at the given mesh face position.
132    fn set_face_id(&mut self, index: usize, face_id: FaceId) -> Result<(), ErrorsJSL>;
133    /// Returns the number of faces in the mesh.
134    fn face_count(&self) -> usize {
135        self.face_ids().count()
136    }
137    /// Resolves and returns a face by mesh-local position.
138    fn face(&self, index: usize) -> Option<<Self as HasFaces<'a>>::Face> {
139        self.face_ids().nth(index).and_then(|face_id| self.get_face(&face_id))
140    }
141    /// Returns the total surface area of the mesh.
142    fn surface_area(&self) -> GeometryMeasure;
143    /// Returns the normal associated with a face, if present.
144    fn face_normal(&self, index: usize) -> Option<N> {
145        self.face(index).map(|face| face.normal())
146    }
147}
148
149/// Named alias for the surface-mesh interpretation of [`IsMesh`].
150///
151/// This trait exists to make the intent of the current mesh abstraction obvious at the API level
152/// while preserving compatibility for existing `IsMesh` implementors.
153pub trait IsSurfaceMesh<'a, T: IsPoint, N: IsUnitVector>: IsMesh<'a, T, N>
154where
155    <Self as HasEdges>::Edge: IsLine<'a, T>,
156{
157}
158
159impl<'a, T, N, M> IsSurfaceMesh<'a, T, N> for M
160where
161    T: IsPoint,
162    N: IsUnitVector,
163    M: IsMesh<'a, T, N>,
164    <M as HasEdges>::Edge: IsLine<'a, T>,
165{
166}
167
168/// A volumetric mesh primitive with stored vertices and tetrahedral cells.
169pub trait IsVolumeMesh<'a, T: IsPoint, N: IsUnitVector>:
170    GeometricPrimitive
171    + HasVertices<'a, Vertex = T>
172    + HasTetrahedra<'a, Point = T, Normal = N>
173    + HasEdges
174where
175    <Self as HasEdges>::Edge: IsLine<'a, T>,
176{
177    /// Returns the ordered tetrahedron-table ids that participate in this mesh.
178    fn tetrahedron_ids(&self) -> Box<dyn Iterator<Item = TetrahedronId> + '_>;
179    /// Replaces the tetrahedron-table id at the given mesh cell position.
180    fn set_tetrahedron_id(&mut self, index: usize, tetrahedron_id: TetrahedronId) -> Result<(), ErrorsJSL>;
181    /// Returns the number of tetrahedra in the mesh.
182    fn tetrahedron_count(&self) -> usize {
183        self.tetrahedron_ids().count()
184    }
185    /// Resolves and returns a tetrahedron by mesh-local position.
186    fn tetrahedron(&self, index: usize) -> Option<<Self as HasTetrahedra<'a>>::Tetrahedron> {
187        self.tetrahedron_ids()
188            .nth(index)
189            .and_then(|tetrahedron_id| self.get_tetrahedron(&tetrahedron_id))
190    }
191    /// Returns the total enclosed volume of the mesh.
192    fn volume(&self) -> GeometryMeasure;
193}