Skip to main content

lib_jsl_geometry/dim_1/
line1d.rs

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