Skip to main content

lib_jsl_geometry/dim_2/
line2d.rs

1//! Concrete two-dimensional line type.
2
3use lib_jsl_core::ErrorsJSL;
4use crate::common::{
5    GeometricPrimitive, GeometricPrimitive2D, GeometryMeasure, HasVertices, PointId,
6};
7use crate::one_d::IsLine;
8use crate::tables::SharedGeometryTable;
9use crate::two_d::{CoordinateVector2D, UnitVector2D};
10use std::fmt::{self, Display, Formatter};
11use std::hash::{Hash, Hasher};
12
13/// Concrete 2D line implementation backed by point ids and a shared point table.
14#[derive(Debug, Clone)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize))]
16pub struct Line2D {
17    head_id: PointId,
18    tail_id: PointId,
19    #[cfg_attr(feature = "serde", serde(skip_serializing))]
20    vertex_table: SharedGeometryTable<PointId, CoordinateVector2D>,
21}
22
23impl PartialEq for Line2D {
24    fn eq(&self, other: &Self) -> bool {
25        self.head_id == other.head_id && self.tail_id == other.tail_id
26    }
27}
28
29impl Eq for Line2D {}
30
31impl Hash for Line2D {
32    fn hash<H: Hasher>(&self, state: &mut H) {
33        self.head_id.hash(state);
34        self.tail_id.hash(state);
35    }
36}
37
38impl Line2D {
39    /// Creates a line from two point ids and a shared point table.
40    pub fn new(
41        head_id: PointId,
42        tail_id: PointId,
43        vertex_table: SharedGeometryTable<PointId, CoordinateVector2D>,
44    ) -> Self {
45        Self {
46            head_id,
47            tail_id,
48            vertex_table,
49        }
50    }
51}
52
53impl Display for Line2D {
54    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
55        write!(f, "Line2D({:?} -> {:?})", self.head_id, self.tail_id)
56    }
57}
58
59impl GeometricPrimitive for Line2D {}
60impl GeometricPrimitive2D for Line2D {}
61
62impl<'a> HasVertices<'a> for Line2D {
63    type Vertex = CoordinateVector2D;
64    type VertexTable = SharedGeometryTable<PointId, CoordinateVector2D>;
65
66    fn vertex_table(&self) -> &Self::VertexTable {
67        &self.vertex_table
68    }
69
70    fn vertex_table_mut(&mut self) -> &mut Self::VertexTable {
71        &mut self.vertex_table
72    }
73
74    fn set_vertex_table(&mut self, table: &'a mut Self::VertexTable) {
75        self.vertex_table = table.clone();
76    }
77}
78
79impl<'a> IsLine<'a, CoordinateVector2D> for Line2D {
80    fn head_id(&self) -> PointId {
81        self.head_id
82    }
83
84    fn set_head_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL> {
85        self.head_id = point_id;
86        Ok(())
87    }
88
89    fn tail_id(&self) -> PointId {
90        self.tail_id
91    }
92
93    fn set_tail_id(&mut self, point_id: PointId) -> Result<(), ErrorsJSL> {
94        self.tail_id = point_id;
95        Ok(())
96    }
97
98    fn length(&self) -> GeometryMeasure {
99        match (self.head(), self.tail()) {
100            (Some(head), Some(tail)) => ((tail.x() - head.x()).powi(2) + (tail.y() - head.y()).powi(2)).sqrt(),
101            _ => 0.0,
102        }
103    }
104
105    fn midpoint(&self) -> Option<CoordinateVector2D> {
106        match (self.head(), self.tail()) {
107            (Some(head), Some(tail)) => Some(CoordinateVector2D::new(
108                (head.x() + tail.x()) / 2.0,
109                (head.y() + tail.y()) / 2.0,
110            )),
111            _ => None,
112        }
113    }
114
115    fn direction(&self) -> impl crate::common::IsUnitVector {
116        match (self.head(), self.tail()) {
117            (Some(head), Some(tail)) => UnitVector2D::new(tail.x() - head.x(), tail.y() - head.y()),
118            _ => UnitVector2D::new(1.0, 0.0),
119        }
120    }
121}