Skip to main content

lib_jsl_geometry/dim_2/
plane2d.rs

1//! Concrete planar mirror/reference line abstraction for two-dimensional geometry.
2
3use crate::common::{GeometricPrimitive, GeometricPrimitive2D, IsPlane};
4use crate::two_d::{CoordinateVector2D, UnitVector2D};
5use std::fmt::{self, Display, Formatter};
6
7/// Concrete 2D plane-like primitive represented by a point and an in-plane unit normal.
8///
9/// In the current trait system, [`IsPlane`] serves as the
10/// shared mirror/reference abstraction. For 2D geometry this type corresponds to a line expressed
11/// in point-normal form.
12#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct Plane2D {
15    point: CoordinateVector2D,
16    normal: UnitVector2D,
17}
18
19impl Plane2D {
20    /// Creates a new point-normal mirror/reference line.
21    pub fn new(point: CoordinateVector2D, normal: UnitVector2D) -> Self {
22        Self { point, normal }
23    }
24}
25
26impl Display for Plane2D {
27    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
28        write!(f, "Plane2D(point={}, normal={})", self.point, self.normal)
29    }
30}
31
32impl GeometricPrimitive for Plane2D {}
33impl GeometricPrimitive2D for Plane2D {}
34
35impl IsPlane for Plane2D {
36    type Point = CoordinateVector2D;
37    type Normal = UnitVector2D;
38
39    fn point(&self) -> Self::Point {
40        self.point
41    }
42
43    fn point_mut(&mut self) -> &mut Self::Point {
44        &mut self.point
45    }
46
47    fn normal(&self) -> Self::Normal {
48        self.normal
49    }
50
51    fn normal_mut(&mut self) -> &mut Self::Normal {
52        &mut self.normal
53    }
54}