1use lib_jsl_core::ErrorsJSL;
8use crate::common::{
9 GeometricPrimitive, GeometricPrimitive3D, GeometryMeasure, HasCentroid, HasEdges, HasMeasure,
10 HasVertices, IsPlane, PointId,
11};
12use crate::tables::SharedGeometryTable;
13use crate::three_d::{Line3D, Plane3D, CoordinateVector3D, PolygonFace3D, UnitVector3D};
14use crate::transformation_traits::{CanMirror, CanRotate, CanShear, CanTranslate};
15use crate::two_d::{HasOrientation, IsPolygon, IsTriangle, Orientation2D};
16use std::fmt::{self, Display, Formatter};
17use std::hash::{Hash, Hasher};
18
19#[derive(Debug, Clone)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize))]
22pub struct Triangle3D {
23 a_id: PointId,
24 b_id: PointId,
25 c_id: PointId,
26 #[cfg_attr(feature = "serde", serde(skip_serializing))]
27 vertex_table: SharedGeometryTable<PointId, CoordinateVector3D>,
28}
29
30impl PartialEq for Triangle3D {
31 fn eq(&self, other: &Self) -> bool {
32 self.a_id == other.a_id && self.b_id == other.b_id && self.c_id == other.c_id
33 }
34}
35
36impl Eq for Triangle3D {}
37
38impl Hash for Triangle3D {
39 fn hash<H: Hasher>(&self, state: &mut H) {
40 self.a_id.hash(state);
41 self.b_id.hash(state);
42 self.c_id.hash(state);
43 }
44}
45
46impl Triangle3D {
47 pub fn new(
51 a_id: PointId,
52 b_id: PointId,
53 c_id: PointId,
54 vertex_table: SharedGeometryTable<PointId, CoordinateVector3D>,
55 ) -> Self {
56 Self {
57 a_id,
58 b_id,
59 c_id,
60 vertex_table,
61 }
62 }
63}
64
65impl Display for Triangle3D {
66 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
67 write!(f, "Triangle3D({:?}, {:?}, {:?})", self.a_id, self.b_id, self.c_id)
68 }
69}
70
71impl GeometricPrimitive for Triangle3D {}
72impl GeometricPrimitive3D for Triangle3D {}
73
74impl<'a> HasVertices<'a> for Triangle3D {
75 type Vertex = CoordinateVector3D;
76 type VertexTable = SharedGeometryTable<PointId, CoordinateVector3D>;
77
78 fn vertex_table(&self) -> &Self::VertexTable {
79 &self.vertex_table
80 }
81
82 fn vertex_table_mut(&mut self) -> &mut Self::VertexTable {
83 &mut self.vertex_table
84 }
85
86 fn set_vertex_table(&mut self, table: &'a mut Self::VertexTable) {
87 self.vertex_table = table.clone();
88 }
89}
90
91impl HasEdges for Triangle3D {
92 type Edge = Line3D;
93
94 fn edge_count(&self) -> usize {
95 3
96 }
97
98 fn edge(&self, index: usize) -> Option<Self::Edge> {
99 let (head, tail) = match index {
100 0 => (self.a_id, self.b_id),
101 1 => (self.b_id, self.c_id),
102 2 => (self.c_id, self.a_id),
103 _ => return None,
104 };
105 Some(Line3D::new(head, tail, self.vertex_table.clone()))
106 }
107}
108
109impl HasCentroid for Triangle3D {
110 type Point = CoordinateVector3D;
111
112 fn centroid(&self) -> Self::Point {
113 let a = self.a().unwrap_or(CoordinateVector3D::new(0.0, 0.0, 0.0));
114 let b = self.b().unwrap_or(CoordinateVector3D::new(0.0, 0.0, 0.0));
115 let c = self.c().unwrap_or(CoordinateVector3D::new(0.0, 0.0, 0.0));
116 CoordinateVector3D::new(
117 (a.x() + b.x() + c.x()) / 3.0,
118 (a.y() + b.y() + c.y()) / 3.0,
119 (a.z() + b.z() + c.z()) / 3.0,
120 )
121 }
122}
123
124impl HasMeasure for Triangle3D {
125 fn measure(&self) -> GeometryMeasure {
126 self.area()
127 }
128}
129
130impl HasOrientation for Triangle3D {
131 fn orientation(&self) -> Orientation2D {
132 match (self.a(), self.b(), self.c()) {
133 (Some(a), Some(b), Some(c)) => {
134 let cross = (b.x() - a.x()) * (c.y() - a.y()) - (b.y() - a.y()) * (c.x() - a.x());
135 if cross > 0.0 {
136 Orientation2D::CounterClockwise
137 } else if cross < 0.0 {
138 Orientation2D::Clockwise
139 } else {
140 Orientation2D::Collinear
141 }
142 }
143 _ => Orientation2D::Collinear,
144 }
145 }
146}
147
148impl CanTranslate for Triangle3D {
149 type Point = CoordinateVector3D;
150
151 fn translate<'a, L>(&mut self, translation_vector: &L)
152 where
153 L: crate::one_d::IsLine<'a, Self::Point>,
154 {
155 let mut polygon = PolygonFace3D::new(vec![self.a_id, self.b_id, self.c_id], self.vertex_table.clone());
156 polygon.translate(translation_vector);
157 }
158}
159
160impl CanRotate for Triangle3D {
161 type Point = CoordinateVector3D;
162
163 fn rotate<'a, L>(&mut self, axis: &L, angle_radians: GeometryMeasure)
164 where
165 L: crate::one_d::IsLine<'a, Self::Point>,
166 {
167 let mut polygon = PolygonFace3D::new(vec![self.a_id, self.b_id, self.c_id], self.vertex_table.clone());
168 polygon.rotate(axis, angle_radians);
169 }
170}
171
172impl CanShear for Triangle3D {
173 type Point = CoordinateVector3D;
174
175 fn shear<'a, L>(&mut self, shear_line: &L)
176 where
177 L: crate::one_d::IsLine<'a, Self::Point>,
178 {
179 let mut polygon = PolygonFace3D::new(vec![self.a_id, self.b_id, self.c_id], self.vertex_table.clone());
180 polygon.shear(shear_line);
181 }
182}
183
184impl CanMirror for Triangle3D {
185 type Point = CoordinateVector3D;
186 type Normal = UnitVector3D;
187
188 fn mirror<P>(&mut self, mirror_plane: &P)
189 where
190 P: IsPlane<Point = Self::Point, Normal = Self::Normal>,
191 {
192 let mut polygon = PolygonFace3D::new(vec![self.a_id, self.b_id, self.c_id], self.vertex_table.clone());
193 polygon.mirror(mirror_plane);
194 }
195}
196
197impl<'a> IsPolygon<'a, CoordinateVector3D, UnitVector3D> for Triangle3D {
198 fn vertex_ids(&self) -> Box<dyn Iterator<Item = PointId> + '_> {
199 Box::new([self.a_id, self.b_id, self.c_id].into_iter())
200 }
201
202 fn set_vertex_id(&mut self, index: usize, point_id: PointId) -> Result<(), ErrorsJSL> {
203 match index {
204 0 => self.a_id = point_id,
205 1 => self.b_id = point_id,
206 2 => self.c_id = point_id,
207 _ => return Err(ErrorsJSL::Message(format!("vertex index {} is out of bounds", index))),
208 }
209 Ok(())
210 }
211
212 fn normal(&self) -> UnitVector3D {
213 PolygonFace3D::new(vec![self.a_id, self.b_id, self.c_id], self.vertex_table.clone()).normal()
214 }
215
216 fn perimeter(&self) -> GeometryMeasure {
217 PolygonFace3D::new(vec![self.a_id, self.b_id, self.c_id], self.vertex_table.clone()).perimeter()
218 }
219
220 fn area(&self) -> GeometryMeasure {
221 PolygonFace3D::new(vec![self.a_id, self.b_id, self.c_id], self.vertex_table.clone()).area()
222 }
223
224 fn plane(&self) -> impl IsPlane<Point = CoordinateVector3D, Normal = UnitVector3D> {
225 Plane3D::new(self.centroid(), self.normal())
226 }
227}
228
229impl<'a> IsTriangle<'a, CoordinateVector3D, UnitVector3D> for Triangle3D {
230 fn a_id(&self) -> PointId {
231 self.a_id
232 }
233
234 fn set_a_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL> {
235 self.a_id = point_id;
236 Ok(())
237 }
238
239 fn b_id(&self) -> PointId {
240 self.b_id
241 }
242
243 fn set_b_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL> {
244 self.b_id = point_id;
245 Ok(())
246 }
247
248 fn c_id(&self) -> PointId {
249 self.c_id
250 }
251
252 fn set_c_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL> {
253 self.c_id = point_id;
254 Ok(())
255 }
256}