lib_jsl_geometry/common.rs
1//! Shared foundational traits used by all geometry primitives.
2
3use lib_jsl_core::ErrorsJSL;
4use crate::one_d::IsLine;
5use crate::two_d::{IsPolygon, IsTriangle};
6use crate::three_d::IsTetrahedron;
7use crate::zero_d::IsPoint;
8use std::fmt::{Debug, Display};
9use std::hash::Hash;
10use std::ops::{Add, Div, Index, IndexMut, Mul, Sub};
11
12/// Canonical scalar type used for geometric measurements.
13pub type GeometryMeasure = f32;
14
15/// Stable identifier for entries in the root point table.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize))]
18pub struct PointId(pub u64);
19
20/// Stable identifier for entries in the root line table.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize))]
23pub struct LineId(pub u64);
24
25/// Stable identifier for entries in the root polygon-face table.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize))]
28pub struct FaceId(pub u64);
29
30/// Stable identifier for entries in the root triangle table.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
32#[cfg_attr(feature = "serde", derive(serde::Serialize))]
33pub struct TriangleId(pub u64);
34
35/// Stable identifier for entries in the root tetrahedron table.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37#[cfg_attr(feature = "serde", derive(serde::Serialize))]
38pub struct TetrahedronId(pub u64);
39
40/// `serde::Serialize` when the `serde` feature is enabled, and no requirement otherwise. Used in
41/// trait bounds so that geometry types are serializable exactly when serialization is available.
42#[cfg(feature = "serde")]
43pub trait MaybeSerialize: serde::Serialize {}
44#[cfg(feature = "serde")]
45impl<T: serde::Serialize + ?Sized> MaybeSerialize for T {}
46
47/// `serde::Serialize` when the `serde` feature is enabled, and no requirement otherwise. Used in
48/// trait bounds so that geometry types are serializable exactly when serialization is available.
49#[cfg(not(feature = "serde"))]
50pub trait MaybeSerialize {}
51#[cfg(not(feature = "serde"))]
52impl<T: ?Sized> MaybeSerialize for T {}
53
54/// Base trait for stable geometry table keys.
55pub trait IsGeometryKey: Debug + Clone + Copy + PartialEq + Eq + Hash + MaybeSerialize + Sized {}
56
57impl IsGeometryKey for PointId {}
58impl IsGeometryKey for LineId {}
59impl IsGeometryKey for FaceId {}
60impl IsGeometryKey for TriangleId {}
61impl IsGeometryKey for TetrahedronId {}
62
63/// Base trait implemented by all geometry primitives in this module tree.
64pub trait GeometricPrimitive:
65 Debug
66 + Clone
67 + Display
68 + PartialEq
69 + MaybeSerialize
70 + Hash
71 + Sized
72{
73}
74
75/// A coordinate-bearing primitive that supports indexed scalar access.
76///
77/// This trait is intended for point- and vector-like values. Composite shapes such as lines,
78/// polygons, planes, and meshes should generally implement [`GeometricPrimitive`] without also
79/// implementing this trait unless they are intentionally represented as flat coordinate vectors.
80pub trait CoordinatePrimitive:
81 GeometricPrimitive
82 + AsRef<GeometryMeasure>
83 + AsMut<GeometryMeasure>
84 + Index<usize, Output = GeometryMeasure>
85 + IndexMut<usize, Output = GeometryMeasure>
86{
87}
88
89/// Marker trait for primitives that live in a 2D space.
90pub trait GeometricPrimitive2D: GeometricPrimitive {}
91
92/// Marker trait for primitives that live in a 3D space.
93pub trait GeometricPrimitive3D: GeometricPrimitive {}
94
95/// Marker trait for primitives that live in a 4D space.
96pub trait GeometricPrimitive4D: GeometricPrimitive {}
97
98/// A plane-like primitive represented by a point and a unit normal.
99///
100/// This abstraction is dimension-neutral: in 3D it corresponds to a geometric plane, while in 2D
101/// and 1D it can serve as the mirror/reference hyperplane for transformation traits.
102pub trait IsPlane: GeometricPrimitive {
103 /// Point type used to anchor the plane.
104 type Point: IsPoint;
105 /// Unit normal type used to orient the plane.
106 type Normal: IsUnitVector;
107
108 /// Returns a point on the plane.
109 fn point(&self) -> Self::Point;
110 /// Returns a mutable reference to a point on the plane.
111 fn point_mut(&mut self) -> &mut Self::Point;
112 /// Returns the plane normal.
113 fn normal(&self) -> Self::Normal;
114 /// Returns a mutable reference to the plane normal.
115 fn normal_mut(&mut self) -> &mut Self::Normal;
116}
117
118/// Exposes the dimensionality of a primitive as a compile-time constant.
119pub trait HasDimension {
120 /// Number of coordinates used to represent the primitive.
121 const DIM: usize;
122}
123
124/// Supports scalar arithmetic against the geometry measurement type.
125pub trait ScalarOperable:
126 CoordinatePrimitive
127 + Add<GeometryMeasure, Output = Self>
128 + Sub<GeometryMeasure, Output = Self>
129 + Mul<GeometryMeasure, Output = Self>
130 + Div<GeometryMeasure, Output = Self>
131{
132}
133
134/// Supports primitive-to-primitive addition and subtraction.
135pub trait SelfAddition:
136 CoordinatePrimitive + Add<Output = Self> + Sub<Output = Self>
137{
138}
139
140/// Supports an inner-product-like multiplication producing a scalar measure.
141pub trait SelfProductInner:
142 CoordinatePrimitive + Mul<Self, Output = GeometryMeasure>
143{
144}
145
146/// Abstraction over a keyed mutable collection of geometry primitives.
147pub trait IsGeometryTable {
148 /// Stable key used to address items in the table.
149 type Key: IsGeometryKey;
150 /// Item stored by the table.
151 type Item: GeometricPrimitive;
152
153 /// Returns the item stored at `key`, if present.
154 fn get(&self, key: &Self::Key) -> Option<Self::Item>;
155 /// Inserts or replaces the item stored at `key`.
156 fn insert(&mut self, key: Self::Key, value: Self::Item) -> Result<(), ErrorsJSL>;
157 /// Removes and returns the item stored at `key`, if present.
158 fn remove(&mut self, key: &Self::Key) -> Option<Self::Item>;
159 /// Returns `true` if `key` exists in the table.
160 fn contains_key(&self, key: &Self::Key) -> bool;
161 /// Returns the number of items held by the table.
162 fn size(&self) -> usize;
163 /// Computes the union of two tables.
164 fn union(&self, other: &Self) -> Self
165 where
166 Self: Sized;
167 /// Computes the intersection of two tables.
168 fn intersection(&self, other: &Self) -> Self
169 where
170 Self: Sized;
171 /// Iterates immutably over key/value pairs in the table.
172 fn iter(&self) -> Box<dyn Iterator<Item = (Self::Key, Self::Item)> + '_>;
173 /// Serializes the table to a file (as JSON for the library's tables; requires the `serde`
174 /// feature, and returns an error without it).
175 fn to_file(&self, path: &str) -> Result<(), ErrorsJSL>;
176 /// Loads a table instance from a file.
177 fn from_file(path: &str) -> Result<Self, ErrorsJSL>
178 where
179 Self: Sized;
180}
181
182/// Root registry that owns or exposes the current core geometry tables.
183pub trait IsGeometryTableBase<'a>
184where
185 <Self::Tetrahedron as HasEdges>::Edge: IsLine<'a, Self::Point>,
186{
187 /// Point primitive stored in the point table.
188 type Point: IsPoint;
189 /// Unit normal type used by polygonal faces and triangles.
190 type Normal: IsUnitVector;
191 /// Line primitive stored in the line table.
192 type Line: IsLine<'a, Self::Point>;
193 /// Polygon face primitive stored in the face table.
194 type Face: IsPolygon<'a, Self::Point, Self::Normal>;
195 /// Triangle primitive stored in the triangle table.
196 type Triangle: IsTriangle<'a, Self::Point, Self::Normal>;
197 /// Tetrahedron primitive stored in the tetrahedron table.
198 type Tetrahedron: IsTetrahedron<'a, Self::Point, Self::Normal>;
199
200 /// Concrete point table type.
201 type PointTable: IsGeometryTable<Key = PointId, Item = Self::Point> + ?Sized + 'a;
202 /// Concrete line table type.
203 type LineTable: IsGeometryTable<Key = LineId, Item = Self::Line> + ?Sized + 'a;
204 /// Concrete face table type.
205 type FaceTable: IsGeometryTable<Key = FaceId, Item = Self::Face> + ?Sized + 'a;
206 /// Concrete triangle table type.
207 type TriangleTable: IsGeometryTable<Key = TriangleId, Item = Self::Triangle> + ?Sized + 'a;
208 /// Concrete tetrahedron table type.
209 type TetrahedronTable: IsGeometryTable<Key = TetrahedronId, Item = Self::Tetrahedron> + ?Sized + 'a;
210
211 /// Returns an immutable reference to the point table.
212 fn point_table(&self) -> &Self::PointTable;
213 /// Returns a mutable reference to the point table.
214 fn point_table_mut(&mut self) -> &mut Self::PointTable;
215 /// Returns an immutable reference to the line table.
216 fn line_table(&self) -> &Self::LineTable;
217 /// Returns a mutable reference to the line table.
218 fn line_table_mut(&mut self) -> &mut Self::LineTable;
219 /// Returns an immutable reference to the face table.
220 fn face_table(&self) -> &Self::FaceTable;
221 /// Returns a mutable reference to the face table.
222 fn face_table_mut(&mut self) -> &mut Self::FaceTable;
223 /// Returns an immutable reference to the triangle table.
224 fn triangle_table(&self) -> &Self::TriangleTable;
225 /// Returns a mutable reference to the triangle table.
226 fn triangle_table_mut(&mut self) -> &mut Self::TriangleTable;
227 /// Returns an immutable reference to the tetrahedron table.
228 fn tetrahedron_table(&self) -> &Self::TetrahedronTable;
229 /// Returns a mutable reference to the tetrahedron table.
230 fn tetrahedron_table_mut(&mut self) -> &mut Self::TetrahedronTable;
231}
232
233/// Provides access to a borrowed point table.
234pub trait HasVertices<'a>: Sized {
235 /// Point primitive stored in the vertex table.
236 type Vertex: IsPoint;
237 /// Borrowed point-table type.
238 type VertexTable: IsGeometryTable<Key = PointId, Item = Self::Vertex> + ?Sized + 'a;
239
240 /// Returns an immutable reference to the vertex table.
241 fn vertex_table(&self) -> &Self::VertexTable;
242 /// Returns a mutable reference to the vertex table.
243 fn vertex_table_mut(&mut self) -> &mut Self::VertexTable;
244 /// Rebinds the implementor to a different borrowed vertex table.
245 fn set_vertex_table(&mut self, table: &'a mut Self::VertexTable);
246
247 /// Returns the number of entries in the vertex table.
248 fn vertex_table_size(&self) -> usize {
249 self.vertex_table().size()
250 }
251
252 /// Returns the vertex stored at `id`.
253 fn get_vertex(&self, id: &PointId) -> Option<Self::Vertex> {
254 self.vertex_table().get(id)
255 }
256
257 /// Returns `true` if the vertex table contains `id`.
258 fn contains_vertex(&self, id: &PointId) -> bool {
259 self.vertex_table().contains_key(id)
260 }
261
262 /// Inserts or replaces the vertex stored at `id`.
263 fn insert_vertex(&mut self, id: PointId, value: Self::Vertex) -> Result<(), ErrorsJSL> {
264 self.vertex_table_mut().insert(id, value)
265 }
266
267 /// Removes and returns the vertex stored at `id`, if present.
268 fn remove_vertex(&mut self, id: &PointId) -> Option<Self::Vertex> {
269 self.vertex_table_mut().remove(id)
270 }
271}
272
273/// Provides access to a borrowed line table.
274pub trait HasLines<'a>: Sized {
275 /// Point primitive referenced by lines in the line table.
276 type Point: IsPoint;
277 /// Line primitive stored in the line table.
278 type Line: IsLine<'a, Self::Point>;
279 /// Borrowed line-table type.
280 type LineTable: IsGeometryTable<Key = LineId, Item = Self::Line> + ?Sized + 'a;
281
282 /// Returns an immutable reference to the line table.
283 fn line_table(&self) -> &Self::LineTable;
284 /// Returns a mutable reference to the line table.
285 fn line_table_mut(&mut self) -> &mut Self::LineTable;
286 /// Rebinds the implementor to a different borrowed line table.
287 fn set_line_table(&mut self, table: &'a mut Self::LineTable);
288
289 /// Returns the number of entries in the line table.
290 fn line_table_size(&self) -> usize {
291 self.line_table().size()
292 }
293
294 /// Returns the line stored at `id`.
295 fn get_line(&self, id: &LineId) -> Option<Self::Line> {
296 self.line_table().get(id)
297 }
298
299 /// Returns `true` if the line table contains `id`.
300 fn contains_line(&self, id: &LineId) -> bool {
301 self.line_table().contains_key(id)
302 }
303
304 /// Inserts or replaces the line stored at `id`.
305 fn insert_line(&mut self, id: LineId, value: Self::Line) -> Result<(), ErrorsJSL> {
306 self.line_table_mut().insert(id, value)
307 }
308
309 /// Removes and returns the line stored at `id`, if present.
310 fn remove_line(&mut self, id: &LineId) -> Option<Self::Line> {
311 self.line_table_mut().remove(id)
312 }
313}
314
315/// Provides access to a borrowed triangle table.
316pub trait HasTriangles<'a>: Sized {
317 /// Point primitive referenced by the stored triangles.
318 type Point: IsPoint;
319 /// Unit normal type used by the stored triangles.
320 type Normal: IsUnitVector;
321 /// Triangle primitive stored in the triangle table.
322 type Triangle: IsTriangle<'a, Self::Point, Self::Normal>;
323 /// Borrowed triangle-table type.
324 type TriangleTable: IsGeometryTable<Key = TriangleId, Item = Self::Triangle> + ?Sized + 'a;
325
326 /// Returns an immutable reference to the triangle table.
327 fn triangle_table(&self) -> &Self::TriangleTable;
328 /// Returns a mutable reference to the triangle table.
329 fn triangle_table_mut(&mut self) -> &mut Self::TriangleTable;
330 /// Rebinds the implementor to a different borrowed triangle table.
331 fn set_triangle_table(&mut self, table: &'a mut Self::TriangleTable);
332
333 /// Returns the number of entries in the triangle table.
334 fn triangle_table_size(&self) -> usize {
335 self.triangle_table().size()
336 }
337
338 /// Returns the triangle stored at `id`.
339 fn get_triangle(&self, id: &TriangleId) -> Option<Self::Triangle> {
340 self.triangle_table().get(id)
341 }
342
343 /// Returns `true` if the triangle table contains `id`.
344 fn contains_triangle(&self, id: &TriangleId) -> bool {
345 self.triangle_table().contains_key(id)
346 }
347
348 /// Inserts or replaces the triangle stored at `id`.
349 fn insert_triangle(&mut self, id: TriangleId, value: Self::Triangle) -> Result<(), ErrorsJSL> {
350 self.triangle_table_mut().insert(id, value)
351 }
352
353 /// Removes and returns the triangle stored at `id`, if present.
354 fn remove_triangle(&mut self, id: &TriangleId) -> Option<Self::Triangle> {
355 self.triangle_table_mut().remove(id)
356 }
357}
358
359/// Provides access to a borrowed tetrahedron table.
360///
361/// ```compile_fail
362/// use lib_jsl::geometry::common::{HasTetrahedra, IsGeometryTable, PointId, TetrahedronId};
363/// use lib_jsl::geometry::two_d::{CoordinateVector2D, UnitVector2D};
364///
365/// #[derive(Debug, Clone, PartialEq, Eq, Hash)]
366/// struct FakeTable;
367///
368/// impl IsGeometryTable for FakeTable {
369/// type Key = TetrahedronId;
370/// type Item = CoordinateVector2D;
371///
372/// fn get(&self, _: &Self::Key) -> Option<Self::Item> { None }
373/// fn insert(&mut self, _: Self::Key, _: Self::Item) -> Result<(), ErrorsJSL> { Ok(()) }
374/// fn remove(&mut self, _: &Self::Key) -> Option<Self::Item> { None }
375/// fn contains_key(&self, _: &Self::Key) -> bool { false }
376/// fn size(&self) -> usize { 0 }
377/// fn union(&self, _: &Self) -> Self where Self: Sized { FakeTable }
378/// fn intersection(&self, _: &Self) -> Self where Self: Sized { FakeTable }
379/// fn iter(&self) -> Box<dyn Iterator<Item = (Self::Key, Self::Item)> + '_> { Box::new(std::iter::empty()) }
380/// fn to_file(&self, _: &str) -> Result<(), ErrorsJSL> { Ok(()) }
381/// fn from_file(_: &str) -> Result<Self, ErrorsJSL> where Self: Sized { Ok(FakeTable) }
382/// }
383///
384/// struct BadCells<'a> {
385/// table: &'a mut FakeTable,
386/// }
387///
388/// impl<'a> HasTetrahedra<'a> for BadCells<'a> {
389/// type Point = CoordinateVector2D;
390/// type Normal = UnitVector2D;
391/// type Tetrahedron = CoordinateVector2D;
392/// type TetrahedronTable = FakeTable;
393///
394/// fn tetrahedron_table(&self) -> &Self::TetrahedronTable { self.table }
395/// fn tetrahedron_table_mut(&mut self) -> &mut Self::TetrahedronTable { self.table }
396/// fn set_tetrahedron_table(&mut self, table: &'a mut Self::TetrahedronTable) { self.table = table; }
397/// }
398/// ```
399pub trait HasTetrahedra<'a>: Sized
400where
401 <Self::Tetrahedron as HasEdges>::Edge: IsLine<'a, Self::Point>,
402{
403 /// Point primitive referenced by the stored tetrahedra.
404 type Point: IsPoint;
405 /// Unit normal type used by the stored tetrahedra.
406 type Normal: IsUnitVector;
407 /// Tetrahedron primitive stored in the tetrahedron table.
408 type Tetrahedron: IsTetrahedron<'a, Self::Point, Self::Normal>;
409 /// Borrowed tetrahedron-table type.
410 type TetrahedronTable: IsGeometryTable<Key = TetrahedronId, Item = Self::Tetrahedron> + ?Sized + 'a;
411
412 /// Returns an immutable reference to the tetrahedron table.
413 fn tetrahedron_table(&self) -> &Self::TetrahedronTable;
414 /// Returns a mutable reference to the tetrahedron table.
415 fn tetrahedron_table_mut(&mut self) -> &mut Self::TetrahedronTable;
416 /// Rebinds the implementor to a different borrowed tetrahedron table.
417 fn set_tetrahedron_table(&mut self, table: &'a mut Self::TetrahedronTable);
418
419 /// Returns the number of entries in the tetrahedron table.
420 fn tetrahedron_table_size(&self) -> usize {
421 self.tetrahedron_table().size()
422 }
423
424 /// Returns the tetrahedron stored at `id`.
425 fn get_tetrahedron(&self, id: &TetrahedronId) -> Option<Self::Tetrahedron> {
426 self.tetrahedron_table().get(id)
427 }
428
429 /// Returns `true` if the tetrahedron table contains `id`.
430 fn contains_tetrahedron(&self, id: &TetrahedronId) -> bool {
431 self.tetrahedron_table().contains_key(id)
432 }
433
434 /// Inserts or replaces the tetrahedron stored at `id`.
435 fn insert_tetrahedron(
436 &mut self,
437 id: TetrahedronId,
438 value: Self::Tetrahedron,
439 ) -> Result<(), ErrorsJSL> {
440 self.tetrahedron_table_mut().insert(id, value)
441 }
442
443 /// Removes and returns the tetrahedron stored at `id`, if present.
444 fn remove_tetrahedron(&mut self, id: &TetrahedronId) -> Option<Self::Tetrahedron> {
445 self.tetrahedron_table_mut().remove(id)
446 }
447}
448
449/// Provides access to a borrowed polygon-face table.
450pub trait HasFaces<'a>: Sized {
451 /// Point primitive referenced by the stored faces.
452 type Point: IsPoint;
453 /// Unit normal type used by the stored faces.
454 type Normal: IsUnitVector;
455 /// Polygon face primitive stored in the face table.
456 type Face: IsPolygon<'a, Self::Point, Self::Normal>;
457 /// Borrowed face-table type.
458 type FaceTable: IsGeometryTable<Key = FaceId, Item = Self::Face> + ?Sized + 'a;
459
460 /// Returns an immutable reference to the face table.
461 fn face_table(&self) -> &Self::FaceTable;
462 /// Returns a mutable reference to the face table.
463 fn face_table_mut(&mut self) -> &mut Self::FaceTable;
464 /// Rebinds the implementor to a different borrowed face table.
465 fn set_face_table(&mut self, table: &'a mut Self::FaceTable);
466
467 /// Returns the number of entries in the face table.
468 fn face_table_size(&self) -> usize {
469 self.face_table().size()
470 }
471
472 /// Returns the face stored at `id`.
473 fn get_face(&self, id: &FaceId) -> Option<Self::Face> {
474 self.face_table().get(id)
475 }
476
477 /// Returns `true` if the face table contains `id`.
478 fn contains_face(&self, id: &FaceId) -> bool {
479 self.face_table().contains_key(id)
480 }
481
482 /// Inserts or replaces the face stored at `id`.
483 fn insert_face(&mut self, id: FaceId, value: Self::Face) -> Result<(), ErrorsJSL> {
484 self.face_table_mut().insert(id, value)
485 }
486
487 /// Removes and returns the face stored at `id`, if present.
488 fn remove_face(&mut self, id: &FaceId) -> Option<Self::Face> {
489 self.face_table_mut().remove(id)
490 }
491}
492
493/// Provides indexed access to a shape's derived geometric edges.
494///
495/// Unlike [`HasLines`], this trait does not imply that edges are stored in a table. Edges may be
496/// computed views over a higher-level primitive boundary.
497pub trait HasEdges {
498 /// Edge type returned by the implementor.
499 type Edge: GeometricPrimitive;
500
501 /// Number of edges available on the shape.
502 fn edge_count(&self) -> usize;
503 /// Returns an edge by index.
504 fn edge(&self, index: usize) -> Option<Self::Edge>;
505}
506
507/// Provides a canonical scalar measure for a primitive.
508pub trait HasMeasure {
509 /// Returns the primitive's primary measure, such as length or area.
510 fn measure(&self) -> GeometryMeasure;
511}
512
513/// Exposes a geometric center point.
514pub trait HasCenter {
515 /// Point type used to represent the center.
516 type Point: IsPoint;
517
518 /// Returns the center point.
519 fn center(&self) -> Self::Point;
520}
521
522/// Exposes a centroid point for the primitive.
523pub trait HasCentroid {
524 /// Point type used to represent the centroid.
525 type Point: IsPoint;
526
527 /// Returns the centroid.
528 fn centroid(&self) -> Self::Point;
529}
530
531/// Provides access to a primitive's bounding box.
532pub trait HasBoundingBox {
533 /// Bounding box type associated with the primitive.
534 type BoundingBox: GeometricPrimitive;
535
536 /// Computes the bounding box.
537 fn bounding_box(&self) -> Self::BoundingBox;
538}
539
540/// Computes a distance from `self` to another value.
541pub trait HasDistanceTo<Rhs = Self> {
542 /// Returns the distance to `other`.
543 fn distance_to(&self, other: &Rhs) -> GeometryMeasure;
544}
545
546/// Tests whether a primitive contains another value.
547pub trait Contains<T> {
548 /// Returns `true` when `other` is contained within `self`.
549 fn contains(&self, other: &T) -> bool;
550}
551
552/// Tests whether two values intersect.
553pub trait Intersects<T> {
554 /// Returns `true` when `self` intersects `other`.
555 fn intersects(&self, other: &T) -> bool;
556}
557
558/// Supplies a default epsilon for approximate comparisons.
559pub trait HasEpsilon {
560 /// Returns the epsilon value to use for fuzzy comparisons.
561 fn epsilon() -> GeometryMeasure;
562}
563
564/// Provides approximate equality comparisons with a caller-supplied epsilon.
565pub trait ApproxEq<Rhs = Self> {
566 /// Returns `true` if `self` and `other` are approximately equal.
567 fn approx_eq(&self, other: &Rhs, epsilon: GeometryMeasure) -> bool;
568}
569
570/// Indicates whether a primitive satisfies its invariants.
571pub trait IsValid {
572 /// Returns `true` when the primitive is valid.
573 fn is_valid(&self) -> bool;
574}
575
576/// Attempts to repair an invalid or non-canonical primitive in place.
577pub trait Repair {
578 /// Repairs the primitive or returns an error.
579 fn repair(&mut self) -> Result<(), ErrorsJSL>;
580}
581
582/// Reorders or normalizes an in-memory representation in place.
583pub trait Canonicalize {
584 /// Converts the primitive to a canonical representation.
585 fn canonicalize(&mut self);
586}
587
588/// Uniformly scales a primitive in place.
589pub trait CanScale: GeometricPrimitive + Sized {
590 /// Applies a uniform scaling factor.
591 fn scale(&mut self, factor: GeometryMeasure);
592}
593
594/// Applies non-uniform scaling using a separate scale vector.
595pub trait CanScaleNonUniform: GeometricPrimitive + Sized {
596 /// Primitive used to represent non-uniform scale factors.
597 type ScaleVector: CoordinatePrimitive;
598
599 /// Applies non-uniform scaling in place.
600 fn scale_non_uniform(&mut self, factors: &Self::ScaleVector);
601}
602
603/// Projects a primitive onto another target type.
604pub trait CanProject: GeometricPrimitive + Sized {
605 /// Result type produced by the projection.
606 type Projection;
607
608 /// Projects `self` onto `target`.
609 fn project_onto<T>(&self, target: &T) -> Self::Projection;
610}
611
612/// Normalizes a primitive in place.
613pub trait CanNormalize: CoordinatePrimitive + Sized {
614 /// Mutates the primitive into a normalized form.
615 fn normalize(&mut self);
616}
617
618/// Marker trait for vectors guaranteed to have unit magnitude.
619pub trait IsUnitVector: CoordinatePrimitive {}
620
621/// Computes a dot product between two values.
622pub trait DotProduct<Rhs = Self>: CoordinatePrimitive {
623 /// Output type produced by the dot product.
624 type Output;
625
626 /// Computes the dot product.
627 fn dot(&self, rhs: &Rhs) -> <Self as DotProduct<Rhs>>::Output;
628}
629
630/// Computes a cross product between two values.
631pub trait CrossProduct<Rhs = Self>: CoordinatePrimitive {
632 /// Output type produced by the cross product.
633 type Output;
634
635 /// Computes the cross product.
636 fn cross(&self, rhs: &Rhs) -> <Self as CrossProduct<Rhs>>::Output;
637}
638
639/// Computes the norm or magnitude of a value.
640pub trait HasNorm: CoordinatePrimitive {
641 /// Returns the norm.
642 fn norm(&self) -> GeometryMeasure;
643}
644
645/// Produces a normalized copy without mutating the original value.
646pub trait Normalize: CoordinatePrimitive + Sized {
647 /// Returns a normalized copy of `self`.
648 fn normalized(&self) -> Self;
649}