Skip to main content

lib_jsl_geometry/
dim_2.rs

1//! Two-dimensional shape traits and orientation helpers.
2
3use lib_jsl_core::ErrorsJSL;
4/// Concrete two-dimensional line implementation.
5pub mod line2d;
6/// Concrete two-dimensional free-vector implementation.
7pub mod free_vector2d;
8/// Coordinate-system conversion helpers for 2D concrete coordinate vectors.
9pub(crate) mod coordinate_conversions;
10/// Concrete two-dimensional mesh implementation.
11pub mod mesh2d;
12/// Concrete two-dimensional coordinate-vector implementation.
13pub mod coordinate_vector2d;
14/// Concrete two-dimensional plane implementation.
15pub mod plane2d;
16/// Concrete two-dimensional polygon face implementation.
17pub mod polygon_face2d;
18/// Internal helpers shared by 2D concrete implementations.
19pub(crate) mod transform_support;
20/// Concrete two-dimensional triangle implementation.
21pub mod triangle2d;
22/// Concrete two-dimensional unit vector implementation.
23pub mod unit_vector2d;
24
25use crate::common::{
26    GeometricPrimitive, GeometryMeasure, HasCentroid, HasCenter, HasEdges, HasMeasure,
27    HasVertices, IsPlane, PointId,
28};
29use crate::common::IsUnitVector;
30use crate::transformation_traits::{CanMirror, CanRotate, CanShear, CanTranslate};
31use crate::zero_d::IsPoint;
32pub use line2d::Line2D;
33pub use free_vector2d::FreeVector2D;
34pub use mesh2d::Mesh2D;
35pub use plane2d::Plane2D;
36pub use coordinate_vector2d::CoordinateVector2D;
37pub use polygon_face2d::PolygonFace2D;
38pub use triangle2d::Triangle2D;
39pub use unit_vector2d::UnitVector2D;
40
41/// Relative winding/orientation classification for planar geometry.
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43
44pub enum Orientation2D {
45    /// Points or edges are ordered clockwise.
46    Clockwise,
47    /// Points or edges are ordered counter-clockwise.
48    CounterClockwise,
49    /// No orientation can be determined because the data is collinear.
50    Collinear,
51}
52
53/// Exposes the planar orientation of a primitive.
54pub trait HasOrientation {
55    /// Returns the primitive orientation.
56    fn orientation(&self) -> Orientation2D;
57}
58
59/// A triangle primitive backed by a point table and treated as a polygon specialization.
60pub trait IsTriangle<'a, T: IsPoint, N: IsUnitVector>: IsPolygon<'a, T, N> {
61    /// Returns the point-table id of the first vertex.
62    fn a_id(&self) -> PointId;
63    /// Sets the point-table id of the first vertex.
64    fn set_a_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL>;
65    /// Returns the point-table id of the second vertex.
66    fn b_id(&self) -> PointId;
67    /// Sets the point-table id of the second vertex.
68    fn set_b_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL>;
69    /// Returns the point-table id of the third vertex.
70    fn c_id(&self) -> PointId;
71    /// Sets the point-table id of the third vertex.
72    fn set_c_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL>;
73
74    /// Resolves and returns the first vertex from the point table.
75    fn a(&self) -> Option<T> {
76        self.get_vertex(&self.a_id())
77    }
78
79    /// Resolves and returns the second vertex from the point table.
80    fn b(&self) -> Option<T> {
81        self.get_vertex(&self.b_id())
82    }
83
84    /// Resolves and returns the third vertex from the point table.
85    fn c(&self) -> Option<T> {
86        self.get_vertex(&self.c_id())
87    }
88
89    /// Returns the edge from `a` to `b`.
90    fn edge_ab(&self) -> Option<<Self as HasEdges>::Edge> {
91        self.edge(0)
92    }
93    /// Returns the edge from `b` to `c`.
94    fn edge_bc(&self) -> Option<<Self as HasEdges>::Edge> {
95        self.edge(1)
96    }
97    /// Returns the edge from `c` to `a`.
98    fn edge_ca(&self) -> Option<<Self as HasEdges>::Edge> {
99        self.edge(2)
100    }
101}
102
103/// A polygon primitive defined by an ordered point table.
104pub trait IsPolygon<'a, T: IsPoint, N: IsUnitVector>:
105    GeometricPrimitive
106    + HasVertices<'a, Vertex = T>
107    + HasEdges
108    + CanTranslate<Point = T>
109    + CanRotate<Point = T>
110    + CanShear<Point = T>
111    + CanMirror<Point = T, Normal = N>
112    + HasCentroid<Point = T>
113    + HasMeasure
114    + HasOrientation
115{
116    /// Returns the ordered point-table ids that define this polygon.
117    fn vertex_ids(&self) -> Box<dyn Iterator<Item = PointId> + '_>;
118    /// Replaces the point-table id at the given polygon vertex position.
119    fn set_vertex_id(&mut self, index: usize, point_id: PointId) -> Result<(), ErrorsJSL>;
120
121    /// Returns the polygon normal.
122    fn normal(&self) -> N;
123    /// Returns the polygon perimeter.
124    fn perimeter(&self) -> GeometryMeasure;
125    /// Returns the polygon area.
126    fn area(&self) -> GeometryMeasure;
127    /// Returns the plane containing the polygon.
128    fn plane(&self) -> impl IsPlane<Point = T, Normal = N>;
129}
130
131/// A rectangle specialization of `IsPolygon`.
132pub trait IsRectangle<'a, T: IsPoint, N: IsUnitVector>: IsPolygon<'a, T, N> {
133    /// Returns the rectangle width.
134    fn width(&self) -> GeometryMeasure;
135    /// Returns the rectangle height.
136    fn height(&self) -> GeometryMeasure;
137    /// Returns the rectangle diagonal length.
138    fn diagonal(&self) -> GeometryMeasure;
139}
140
141/// A circle primitive with a center and radius-derived measures.
142pub trait IsCircle<T: IsPoint>: GeometricPrimitive + HasCenter<Point = T> + HasMeasure {
143    /// Returns the radius.
144    fn radius(&self) -> GeometryMeasure;
145    /// Returns the diameter.
146    fn diameter(&self) -> GeometryMeasure;
147    /// Returns the circumference.
148    fn circumference(&self) -> GeometryMeasure;
149    /// Returns the enclosed area.
150    fn area(&self) -> GeometryMeasure;
151}