egml_core/model/geometry/primitives/
triangle.rs

1use crate::error::Error;
2use nalgebra::Isometry3;
3
4use crate::Error::ContainsEqualElements;
5use crate::model::geometry::DirectPosition;
6use crate::operations::geometry::Geometry;
7use crate::operations::surface::Surface;
8use parry3d_f64::query::PointQuery;
9
10#[derive(Debug, Clone, PartialEq)]
11pub struct Triangle {
12    /// First point of the triangle.
13    pub a: DirectPosition,
14    /// Second point of the triangle.
15    pub b: DirectPosition,
16    /// Third point of the triangle.
17    pub c: DirectPosition,
18}
19
20impl Triangle {
21    pub fn new(a: DirectPosition, b: DirectPosition, c: DirectPosition) -> Result<Self, Error> {
22        if a == b || a == c || b == c {
23            return Err(ContainsEqualElements);
24        }
25        /*let a_b_dist: f64 = distance(&a, &b);
26        let b_c_dist: f64 = distance(&b, &c);
27        let c_a_dist: f64 = distance(&c, &a);
28        if a_b_dist <= f64::EPSILON || b_c_dist <= f64::EPSILON || c_a_dist <= f64::EPSILON {
29            return Err(ContainsEqualElements);
30        }*/
31
32        Ok(Self { a, b, c })
33    }
34
35    pub fn area(&self) -> f64 {
36        let parry_triangle: parry3d_f64::shape::Triangle = self.clone().into();
37        // parry_triangle.distance_to_local_point().
38        parry_triangle.area()
39    }
40
41    pub fn distance_to_local_point(&self, p: &DirectPosition) -> f64 {
42        let parry_triangle: parry3d_f64::shape::Triangle = self.clone().into();
43        let point: parry3d_f64::math::Point<f64> = (*p).into();
44        parry_triangle.distance_to_local_point(&point, false)
45    }
46}
47
48impl Geometry for Triangle {
49    fn points(&self) -> Vec<&DirectPosition> {
50        vec![&self.a, &self.b, &self.c]
51    }
52
53    fn apply_transform(&mut self, m: &Isometry3<f64>) {
54        self.a.apply_transform(m);
55        self.b.apply_transform(m);
56        self.c.apply_transform(m);
57    }
58}
59
60impl Surface for Triangle {
61    fn outer_boundary_points(&self) -> Vec<&DirectPosition> {
62        vec![&self.a, &self.b, &self.c]
63    }
64}
65
66impl From<Triangle> for parry3d_f64::shape::Triangle {
67    fn from(item: Triangle) -> Self {
68        Self::new(item.a.into(), item.b.into(), item.c.into())
69    }
70}