fj_core/geometry/
geometry.rs

1use std::collections::BTreeMap;
2
3use fj_math::Vector;
4
5use crate::{
6    objects::{HalfEdge, Objects, Surface},
7    storage::{Handle, HandleWrapper},
8};
9
10use super::{GlobalPath, HalfEdgeGeometry, SurfaceGeometry};
11
12/// Geometric data that is associated with topological objects
13pub struct Geometry {
14    half_edge: BTreeMap<Handle<HalfEdge>, HalfEdgeGeometry>,
15    surface: BTreeMap<HandleWrapper<Surface>, SurfaceGeometry>,
16
17    xy_plane: Handle<Surface>,
18    xz_plane: Handle<Surface>,
19    yz_plane: Handle<Surface>,
20}
21
22impl Geometry {
23    /// Create a new instance of `Geometry`
24    pub fn new(objects: &Objects) -> Self {
25        let mut self_ = Self {
26            half_edge: BTreeMap::new(),
27            surface: BTreeMap::new(),
28
29            xy_plane: objects.surfaces.xy_plane(),
30            xz_plane: objects.surfaces.xz_plane(),
31            yz_plane: objects.surfaces.yz_plane(),
32        };
33
34        self_.define_surface_inner(
35            self_.xy_plane.clone(),
36            SurfaceGeometry {
37                u: GlobalPath::x_axis(),
38                v: Vector::unit_y(),
39            },
40        );
41        self_.define_surface_inner(
42            self_.xz_plane.clone(),
43            SurfaceGeometry {
44                u: GlobalPath::x_axis(),
45                v: Vector::unit_z(),
46            },
47        );
48        self_.define_surface_inner(
49            self_.yz_plane.clone(),
50            SurfaceGeometry {
51                u: GlobalPath::y_axis(),
52                v: Vector::unit_z(),
53            },
54        );
55
56        self_
57    }
58
59    pub(crate) fn define_half_edge_inner(
60        &mut self,
61        half_edge: Handle<HalfEdge>,
62        geometry: HalfEdgeGeometry,
63    ) {
64        self.half_edge.insert(half_edge, geometry);
65    }
66
67    pub(crate) fn define_surface_inner(
68        &mut self,
69        surface: Handle<Surface>,
70        geometry: SurfaceGeometry,
71    ) {
72        self.surface.insert(surface.into(), geometry);
73    }
74
75    /// # Access the geometry of the provided half-edge
76    ///
77    /// ## Panics
78    ///
79    /// Panics, if the geometry of the half-edge is not defined.
80    pub fn of_half_edge(
81        &self,
82        half_edge: &Handle<HalfEdge>,
83    ) -> HalfEdgeGeometry {
84        self.half_edge
85            .get(half_edge)
86            .copied()
87            .expect("Expected geometry of half-edge to be defined")
88    }
89
90    /// # Access the geometry of the provided surface
91    ///
92    /// ## Panics
93    ///
94    /// Panics, if the geometry of surface is not defined.
95    pub fn of_surface(&self, surface: &Handle<Surface>) -> SurfaceGeometry {
96        self.surface
97            .get(&surface.clone().into())
98            .copied()
99            .expect("Expected geometry of surface to be defined")
100    }
101
102    /// Access the geometry of the xy-plane
103    pub fn xy_plane(&self) -> SurfaceGeometry {
104        self.of_surface(&self.xy_plane)
105    }
106
107    /// Access the geometry of the xz-plane
108    pub fn xz_plane(&self) -> SurfaceGeometry {
109        self.of_surface(&self.xz_plane)
110    }
111
112    /// Access the geometry of the yz-plane
113    pub fn yz_plane(&self) -> SurfaceGeometry {
114        self.of_surface(&self.yz_plane)
115    }
116}