Skip to main content

lib_jsl_geometry/dim_3/
line3d.rs

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