1pub use crate::one_d::{Line1D, Plane1D, CoordinateVector1D, UnitVector1D};
38pub use crate::two_d::{
40 FreeVector2D, Line2D, Mesh2D, Plane2D, CoordinateVector2D, PolygonFace2D, Triangle2D, UnitVector2D,
41};
42pub use crate::registry::GeometryTableRegistry;
44pub use crate::tables::{HashGeometryTable, SharedGeometryTable};
46pub use crate::three_d::{
48 FreeVector3D, Line3D, Plane3D, CoordinateVector3D, PolygonFace3D, SurfaceMesh3D, Tetrahedron3D,
49 Triangle3D, UnitVector3D, VolumeMesh3D,
50};
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55 use crate::common::{
56 FaceId, HasEdges, IsGeometryTable, IsGeometryTableBase, IsValid, PointId, TetrahedronId,
57 TriangleId,
58 };
59 use crate::coordinate_systems::{CoordinateSystem2D, CoordinateSystem3D};
60 use crate::one_d::IsLine;
61 use crate::three_d::{IsMesh, IsTetrahedron};
62 use crate::two_d::IsPolygon;
63
64 #[test]
65 fn concrete_geometry_smoke_test() {
66 let mut registry = GeometryTableRegistry::new();
67
68 registry
69 .point_table_mut()
70 .insert(PointId(1), CoordinateVector3D::new(0.0, 0.0, 0.0))
71 .unwrap();
72 registry
73 .point_table_mut()
74 .insert(PointId(2), CoordinateVector3D::new(1.0, 0.0, 0.0))
75 .unwrap();
76 registry
77 .point_table_mut()
78 .insert(PointId(3), CoordinateVector3D::new(0.0, 1.0, 0.0))
79 .unwrap();
80
81 let triangle = Triangle3D::new(PointId(1), PointId(2), PointId(3), registry.point_table().clone());
82 registry
83 .triangle_table_mut()
84 .insert(TriangleId(1), triangle.clone())
85 .unwrap();
86
87 let face = PolygonFace3D::new(vec![PointId(1), PointId(2), PointId(3)], registry.point_table().clone());
88 registry.face_table_mut().insert(FaceId(1), face).unwrap();
89
90 let mesh = SurfaceMesh3D::new(vec![FaceId(1)], registry.point_table().clone(), registry.face_table().clone());
91
92 assert!((triangle.area() - 0.5).abs() < 1e-5);
93 assert_eq!(mesh.face_count(), 1);
94 assert!((mesh.surface_area() - 0.5).abs() < 1e-5);
95 }
96
97 #[test]
98 fn concrete_geometry_2d_mesh_smoke_test() {
99 let point_table = std::rc::Rc::new(std::cell::RefCell::new(HashGeometryTable::new()));
100 point_table
101 .borrow_mut()
102 .insert(PointId(1), CoordinateVector2D::new(0.0, 0.0))
103 .unwrap();
104 point_table
105 .borrow_mut()
106 .insert(PointId(2), CoordinateVector2D::new(1.0, 0.0))
107 .unwrap();
108 point_table
109 .borrow_mut()
110 .insert(PointId(3), CoordinateVector2D::new(0.0, 1.0))
111 .unwrap();
112
113 let face_table = std::rc::Rc::new(std::cell::RefCell::new(HashGeometryTable::new()));
114 face_table
115 .borrow_mut()
116 .insert(
117 FaceId(1),
118 PolygonFace2D::new(vec![PointId(1), PointId(2), PointId(3)], point_table.clone()),
119 )
120 .unwrap();
121
122 let mesh = Mesh2D::new(vec![FaceId(1)], point_table, face_table);
123 assert_eq!(mesh.face_count(), 1);
124 assert!((mesh.surface_area() - 0.5).abs() < 1e-5);
125 }
126
127 #[test]
128 fn concrete_tetrahedron3d_smoke_test() {
129 let point_table = std::rc::Rc::new(std::cell::RefCell::new(HashGeometryTable::new()));
130 point_table
131 .borrow_mut()
132 .insert(PointId(1), CoordinateVector3D::new(0.0, 0.0, 0.0))
133 .unwrap();
134 point_table
135 .borrow_mut()
136 .insert(PointId(2), CoordinateVector3D::new(1.0, 0.0, 0.0))
137 .unwrap();
138 point_table
139 .borrow_mut()
140 .insert(PointId(3), CoordinateVector3D::new(0.0, 1.0, 0.0))
141 .unwrap();
142 point_table
143 .borrow_mut()
144 .insert(PointId(4), CoordinateVector3D::new(0.0, 0.0, 1.0))
145 .unwrap();
146
147 let tetrahedron =
148 Tetrahedron3D::new(PointId(1), PointId(2), PointId(3), PointId(4), point_table);
149
150 assert_eq!(tetrahedron.face_count(), 4);
151 assert_eq!(tetrahedron.edge_count(), 6);
152 assert!((tetrahedron.volume() - (1.0 / 6.0)).abs() < 1e-5);
153 assert!(tetrahedron.surface_area() > 2.0);
154 assert!(tetrahedron.is_valid());
155 }
156
157 #[test]
158 fn concrete_volume_mesh3d_smoke_test() {
159 let mut registry = GeometryTableRegistry::new();
160 registry
161 .point_table_mut()
162 .insert(PointId(1), CoordinateVector3D::new(0.0, 0.0, 0.0))
163 .unwrap();
164 registry
165 .point_table_mut()
166 .insert(PointId(2), CoordinateVector3D::new(1.0, 0.0, 0.0))
167 .unwrap();
168 registry
169 .point_table_mut()
170 .insert(PointId(3), CoordinateVector3D::new(0.0, 1.0, 0.0))
171 .unwrap();
172 registry
173 .point_table_mut()
174 .insert(PointId(4), CoordinateVector3D::new(0.0, 0.0, 1.0))
175 .unwrap();
176
177 let tetrahedron = Tetrahedron3D::new(
178 PointId(1),
179 PointId(2),
180 PointId(3),
181 PointId(4),
182 registry.point_table().clone(),
183 );
184 let volume_mesh = VolumeMesh3D::from_tetrahedra(vec![tetrahedron]).unwrap();
185 let surface_mesh = volume_mesh.surface_mesh(registry.face_table().clone()).unwrap();
186
187 assert_eq!(volume_mesh.tetrahedron_count(), 1);
188 assert_eq!(volume_mesh.boundary_triangles().len(), 4);
189 assert_eq!(volume_mesh.boundary_faces().len(), 4);
190 assert_eq!(surface_mesh.face_count(), 4);
191 assert_eq!(registry.face_table().size(), 4);
192 assert!((volume_mesh.volume() - (1.0 / 6.0)).abs() < 1e-5);
193 assert!(volume_mesh.is_valid());
194 }
195
196 #[test]
197 fn tetrahedron_trait_contract_smoke_test() {
198 fn assert_is_tetrahedron<'a, T>(_value: &T)
199 where
200 T: IsTetrahedron<'a, CoordinateVector3D, UnitVector3D>,
201 <T as HasEdges>::Edge: crate::one_d::IsLine<'a, CoordinateVector3D>,
202 {
203 }
204
205 let point_table = std::rc::Rc::new(std::cell::RefCell::new(HashGeometryTable::new()));
206 point_table
207 .borrow_mut()
208 .insert(PointId(1), CoordinateVector3D::new(0.0, 0.0, 0.0))
209 .unwrap();
210 point_table
211 .borrow_mut()
212 .insert(PointId(2), CoordinateVector3D::new(1.0, 0.0, 0.0))
213 .unwrap();
214 point_table
215 .borrow_mut()
216 .insert(PointId(3), CoordinateVector3D::new(0.0, 1.0, 0.0))
217 .unwrap();
218 point_table
219 .borrow_mut()
220 .insert(PointId(4), CoordinateVector3D::new(0.0, 0.0, 1.0))
221 .unwrap();
222
223 let tetrahedron =
224 Tetrahedron3D::new(PointId(1), PointId(2), PointId(3), PointId(4), point_table);
225 assert_is_tetrahedron(&tetrahedron);
226 }
227
228 #[test]
229 fn registry_exposes_tetrahedron_table() {
230 let mut registry = GeometryTableRegistry::new();
231 registry
232 .point_table_mut()
233 .insert(PointId(1), CoordinateVector3D::new(0.0, 0.0, 0.0))
234 .unwrap();
235 registry
236 .point_table_mut()
237 .insert(PointId(2), CoordinateVector3D::new(1.0, 0.0, 0.0))
238 .unwrap();
239 registry
240 .point_table_mut()
241 .insert(PointId(3), CoordinateVector3D::new(0.0, 1.0, 0.0))
242 .unwrap();
243 registry
244 .point_table_mut()
245 .insert(PointId(4), CoordinateVector3D::new(0.0, 0.0, 1.0))
246 .unwrap();
247
248 let tetrahedron = Tetrahedron3D::new(
249 PointId(1),
250 PointId(2),
251 PointId(3),
252 PointId(4),
253 registry.point_table().clone(),
254 );
255 registry
256 .tetrahedron_table_mut()
257 .insert(TetrahedronId(1), tetrahedron.clone())
258 .unwrap();
259
260 assert_eq!(registry.tetrahedron_table().size(), 1);
261 assert_eq!(registry.tetrahedron_table().get(&TetrahedronId(1)), Some(tetrahedron));
262 }
263
264 #[test]
265 fn coordinate_vector2d_coordinate_system_setter_converts_and_preserves_basis_on_math() {
266 let mut coordinate_vector = CoordinateVector2D::new(1.0, 1.0);
267 coordinate_vector.set_coordinate_system(CoordinateSystem2D::Polar);
268
269 let translated = coordinate_vector + FreeVector2D::new(1.0, 0.0);
270
271 assert_eq!(coordinate_vector.coordinate_system(), CoordinateSystem2D::Polar);
272 assert_eq!(translated.coordinate_system(), CoordinateSystem2D::Polar);
273 assert!((coordinate_vector.x() - 1.0).abs() < 1e-5);
274 assert!((coordinate_vector.y() - 1.0).abs() < 1e-5);
275 assert!((translated.x() - 2.0).abs() < 1e-5);
276 assert!((translated.y() - 1.0).abs() < 1e-5);
277 }
278
279 #[test]
280 fn coordinate_vector3d_coordinate_system_setter_converts_and_preserves_basis_on_math() {
281 let mut coordinate_vector = CoordinateVector3D::new(1.0, 0.0, 1.0);
282 coordinate_vector.set_coordinate_system(CoordinateSystem3D::Spherical);
283
284 let translated = coordinate_vector + FreeVector3D::new(0.0, 1.0, 0.0);
285
286 assert_eq!(coordinate_vector.coordinate_system(), CoordinateSystem3D::Spherical);
287 assert_eq!(translated.coordinate_system(), CoordinateSystem3D::Spherical);
288 assert!((coordinate_vector.x() - 1.0).abs() < 1e-5);
289 assert!((coordinate_vector.y() - 0.0).abs() < 1e-5);
290 assert!((coordinate_vector.z() - 1.0).abs() < 1e-5);
291 assert!((translated.x() - 1.0).abs() < 1e-5);
292 assert!((translated.y() - 1.0).abs() < 1e-5);
293 assert!((translated.z() - 1.0).abs() < 1e-5);
294 }
295
296 #[test]
297 fn line_length_uses_cartesian_coordinates_after_basis_change() {
298 let point_table = std::rc::Rc::new(std::cell::RefCell::new(HashGeometryTable::new()));
299 let mut a = CoordinateVector2D::new(0.0, 0.0);
300 let mut b = CoordinateVector2D::new(3.0, 4.0);
301 a.set_coordinate_system(CoordinateSystem2D::Polar);
302 b.set_coordinate_system(CoordinateSystem2D::Polar);
303
304 point_table.borrow_mut().insert(PointId(1), a).unwrap();
305 point_table.borrow_mut().insert(PointId(2), b).unwrap();
306
307 let line = Line2D::new(PointId(1), PointId(2), point_table);
308 assert!((line.length() - 5.0).abs() < 1e-5);
309 }
310
311 #[test]
312 fn polygon_face2d_triangle_round_trip_smoke_test() {
313 let point_table = std::rc::Rc::new(std::cell::RefCell::new(HashGeometryTable::new()));
314 point_table
315 .borrow_mut()
316 .insert(PointId(1), CoordinateVector2D::new(0.0, 0.0))
317 .unwrap();
318 point_table
319 .borrow_mut()
320 .insert(PointId(2), CoordinateVector2D::new(2.0, 0.0))
321 .unwrap();
322 point_table
323 .borrow_mut()
324 .insert(PointId(3), CoordinateVector2D::new(2.0, 1.0))
325 .unwrap();
326 point_table
327 .borrow_mut()
328 .insert(PointId(4), CoordinateVector2D::new(0.0, 1.0))
329 .unwrap();
330
331 let polygon = PolygonFace2D::new(
332 vec![PointId(1), PointId(2), PointId(3), PointId(4)],
333 point_table.clone(),
334 );
335 let triangles = polygon.triangulate();
336 let recomposed = PolygonFace2D::from_triangles(&triangles).unwrap();
337
338 assert_eq!(triangles.len(), 2);
339 assert!((triangles.iter().map(|triangle| triangle.area()).sum::<f32>() - polygon.area()).abs() < 1e-5);
340 assert_eq!(recomposed.vertex_ids().collect::<Vec<_>>(), polygon.vertex_ids().collect::<Vec<_>>());
341 }
342
343 #[test]
344 fn polygon_face3d_triangle_round_trip_smoke_test() {
345 let point_table = std::rc::Rc::new(std::cell::RefCell::new(HashGeometryTable::new()));
346 point_table
347 .borrow_mut()
348 .insert(PointId(1), CoordinateVector3D::new(0.0, 0.0, 0.0))
349 .unwrap();
350 point_table
351 .borrow_mut()
352 .insert(PointId(2), CoordinateVector3D::new(2.0, 0.0, 0.0))
353 .unwrap();
354 point_table
355 .borrow_mut()
356 .insert(PointId(3), CoordinateVector3D::new(2.0, 1.0, 0.0))
357 .unwrap();
358 point_table
359 .borrow_mut()
360 .insert(PointId(4), CoordinateVector3D::new(0.0, 1.0, 0.0))
361 .unwrap();
362
363 let polygon = PolygonFace3D::new(
364 vec![PointId(1), PointId(2), PointId(3), PointId(4)],
365 point_table.clone(),
366 );
367 let triangles = polygon.triangulate();
368 let recomposed = PolygonFace3D::from_triangles(&triangles).unwrap();
369
370 assert_eq!(triangles.len(), 2);
371 assert!((triangles.iter().map(|triangle| triangle.area()).sum::<f32>() - polygon.area()).abs() < 1e-5);
372 assert_eq!(recomposed.vertex_ids().collect::<Vec<_>>(), polygon.vertex_ids().collect::<Vec<_>>());
373 }
374}