Skip to main content

lib_jsl_geometry/
registry.rs

1//! Concrete root registry for the public geometry table graph.
2//!
3//! [`GeometryTableRegistry`](crate::registry::GeometryTableRegistry) is the main starting point for users of the concrete API. It owns the
4//! current core keyed tables for points, lines, polygon faces, triangles, and tetrahedra, and
5//! exposes them through the [`IsGeometryTableBase`](crate::common::IsGeometryTableBase)
6//! contract.
7//!
8//! Each concrete primitive borrows one or more of these tables through shared handles. This lets
9//! higher-level objects such as lines, polygon faces, triangles, and meshes resolve their child
10//! geometry by stable IDs while still sharing a single point table.
11//!
12//! See the book entry [Geometric types](https://jwkunz.github.io/lib_jsl/book/geometry/types.html#keyed-tables-and-the-registry).
13
14use crate::common::{
15    FaceId, IsGeometryTableBase, LineId, PointId, TetrahedronId, TriangleId,
16};
17use crate::tables::{HashGeometryTable, SharedGeometryTable};
18use crate::three_d::{
19    Line3D, CoordinateVector3D, PolygonFace3D, Tetrahedron3D, Triangle3D, UnitVector3D,
20};
21use std::cell::RefCell;
22use std::rc::Rc;
23
24/// Root registry holding the current concrete point, line, face, triangle, and tetrahedron tables.
25///
26/// This type is intentionally small and explicit so it can serve as the top-level integration
27/// object for applications building a geometry graph on top of the trait system.
28#[derive(Debug, Clone)]
29
30pub struct GeometryTableRegistry {
31    point_table: SharedGeometryTable<PointId, CoordinateVector3D>,
32    line_table: SharedGeometryTable<LineId, Line3D>,
33    face_table: SharedGeometryTable<FaceId, PolygonFace3D>,
34    triangle_table: SharedGeometryTable<TriangleId, Triangle3D>,
35    tetrahedron_table: SharedGeometryTable<TetrahedronId, Tetrahedron3D>,
36}
37
38impl GeometryTableRegistry {
39    /// Creates an empty registry with all currently supported core tables initialized.
40    ///
41    /// The returned registry is ready to hand out shared table handles to concrete primitives.
42    pub fn new() -> Self {
43        Self {
44            point_table: Rc::new(RefCell::new(HashGeometryTable::new())),
45            line_table: Rc::new(RefCell::new(HashGeometryTable::new())),
46            face_table: Rc::new(RefCell::new(HashGeometryTable::new())),
47            triangle_table: Rc::new(RefCell::new(HashGeometryTable::new())),
48            tetrahedron_table: Rc::new(RefCell::new(HashGeometryTable::new())),
49        }
50    }
51}
52
53impl Default for GeometryTableRegistry {
54    fn default() -> Self {
55        Self::new()
56    }
57}
58
59impl<'a> IsGeometryTableBase<'a> for GeometryTableRegistry {
60    type Point = CoordinateVector3D;
61    type Normal = UnitVector3D;
62    type Line = Line3D;
63    type Face = PolygonFace3D;
64    type Triangle = Triangle3D;
65    type Tetrahedron = Tetrahedron3D;
66    type PointTable = SharedGeometryTable<PointId, CoordinateVector3D>;
67    type LineTable = SharedGeometryTable<LineId, Line3D>;
68    type FaceTable = SharedGeometryTable<FaceId, PolygonFace3D>;
69    type TriangleTable = SharedGeometryTable<TriangleId, Triangle3D>;
70    type TetrahedronTable = SharedGeometryTable<TetrahedronId, Tetrahedron3D>;
71
72    fn point_table(&self) -> &Self::PointTable {
73        &self.point_table
74    }
75
76    fn point_table_mut(&mut self) -> &mut Self::PointTable {
77        &mut self.point_table
78    }
79
80    fn line_table(&self) -> &Self::LineTable {
81        &self.line_table
82    }
83
84    fn line_table_mut(&mut self) -> &mut Self::LineTable {
85        &mut self.line_table
86    }
87
88    fn face_table(&self) -> &Self::FaceTable {
89        &self.face_table
90    }
91
92    fn face_table_mut(&mut self) -> &mut Self::FaceTable {
93        &mut self.face_table
94    }
95
96    fn triangle_table(&self) -> &Self::TriangleTable {
97        &self.triangle_table
98    }
99
100    fn triangle_table_mut(&mut self) -> &mut Self::TriangleTable {
101        &mut self.triangle_table
102    }
103
104    fn tetrahedron_table(&self) -> &Self::TetrahedronTable {
105        &self.tetrahedron_table
106    }
107
108    fn tetrahedron_table_mut(&mut self) -> &mut Self::TetrahedronTable {
109        &mut self.tetrahedron_table
110    }
111}