Skip to main content

geop_core_topology/model/
get.rs

1use crate::{
2    Coedge, CoedgeGeometry, CoedgeId, Edge, EdgeId, Face, FaceId, Sense, Shell, ShellId, Solid,
3    SolidId, Vertex, VertexId,
4};
5use geop_core_math::{
6    geop_error::{GeopError, GeopResult},
7    scalars::Scalar,
8};
9
10use super::Model;
11
12impl<S: Scalar> Model<S> {
13    pub fn get_vertex(&self, id: VertexId) -> GeopResult<&Vertex<S>> {
14        self.vertices
15            .get(&id)
16            .ok_or_else(|| GeopError::new(format!("vertex with id {} does not exist", id.0)))
17    }
18
19    pub fn get_vertex_mut(&mut self, id: VertexId) -> GeopResult<&mut Vertex<S>> {
20        self.vertices
21            .get_mut(&id)
22            .ok_or_else(|| GeopError::new(format!("vertex with id {} does not exist", id.0)))
23    }
24
25    pub fn get_edge(&self, id: EdgeId) -> GeopResult<&Edge<S>> {
26        self.edges
27            .get(&id)
28            .ok_or_else(|| GeopError::new(format!("edge with id {} does not exist", id.0)))
29    }
30
31    pub fn get_edge_mut(&mut self, id: EdgeId) -> GeopResult<&mut Edge<S>> {
32        self.edges
33            .get_mut(&id)
34            .ok_or_else(|| GeopError::new(format!("edge with id {} does not exist", id.0)))
35    }
36
37    pub fn get_coedge(&self, id: CoedgeId) -> GeopResult<&Coedge<S>> {
38        self.coedges
39            .get(&id)
40            .ok_or_else(|| GeopError::new(format!("coedge with id {} does not exist", id.0)))
41    }
42
43    pub fn get_coedge_mut(&mut self, id: CoedgeId) -> GeopResult<&mut Coedge<S>> {
44        self.coedges
45            .get_mut(&id)
46            .ok_or_else(|| GeopError::new(format!("coedge with id {} does not exist", id.0)))
47    }
48
49    pub fn get_face(&self, id: FaceId) -> GeopResult<&Face<S>> {
50        self.faces
51            .get(&id)
52            .ok_or_else(|| GeopError::new(format!("face with id {} does not exist", id.0)))
53    }
54
55    pub fn get_face_mut(&mut self, id: FaceId) -> GeopResult<&mut Face<S>> {
56        self.faces
57            .get_mut(&id)
58            .ok_or_else(|| GeopError::new(format!("face with id {} does not exist", id.0)))
59    }
60
61    pub fn get_shell(&self, id: ShellId) -> GeopResult<&Shell> {
62        self.shells
63            .get(&id)
64            .ok_or_else(|| GeopError::new(format!("shell with id {} does not exist", id.0)))
65    }
66
67    pub fn get_shell_mut(&mut self, id: ShellId) -> GeopResult<&mut Shell> {
68        self.shells
69            .get_mut(&id)
70            .ok_or_else(|| GeopError::new(format!("shell with id {} does not exist", id.0)))
71    }
72
73    pub fn get_solid(&self, id: SolidId) -> GeopResult<&Solid> {
74        self.solids
75            .get(&id)
76            .ok_or_else(|| GeopError::new(format!("solid with id {} does not exist", id.0)))
77    }
78
79    pub fn get_solid_mut(&mut self, id: SolidId) -> GeopResult<&mut Solid> {
80        self.solids
81            .get_mut(&id)
82            .ok_or_else(|| GeopError::new(format!("solid with id {} does not exist", id.0)))
83    }
84
85    pub fn coedge_start_vertex_id(&self, coedge: CoedgeId) -> GeopResult<VertexId> {
86        let coedge = self.get_coedge(coedge)?;
87        match coedge.geometry {
88            CoedgeGeometry::Edge(edge_id) => {
89                let edge = self.get_edge(edge_id)?;
90                Ok(match coedge.sense {
91                    Sense::Forward => edge.start_vertex,
92                    Sense::Reversed => edge.end_vertex,
93                })
94            }
95            // Degenerate: sits at one vertex the whole way, so "start" and
96            // "end" are the same.
97            CoedgeGeometry::Vertex(vertex_id) => Ok(vertex_id),
98        }
99    }
100
101    pub fn coedge_start_vertex(&self, coedge: CoedgeId) -> GeopResult<&Vertex<S>> {
102        let vertex_id = self.coedge_start_vertex_id(coedge)?;
103        self.get_vertex(vertex_id)
104    }
105
106    pub fn coedge_end_vertex_id(&self, coedge: CoedgeId) -> GeopResult<VertexId> {
107        let coedge = self.get_coedge(coedge)?;
108        match coedge.geometry {
109            CoedgeGeometry::Edge(edge_id) => {
110                let edge = self.get_edge(edge_id)?;
111                Ok(match coedge.sense {
112                    Sense::Forward => edge.end_vertex,
113                    Sense::Reversed => edge.start_vertex,
114                })
115            }
116            CoedgeGeometry::Vertex(vertex_id) => Ok(vertex_id),
117        }
118    }
119
120    pub fn coedge_end_vertex(&self, coedge: CoedgeId) -> GeopResult<&Vertex<S>> {
121        let vertex_id = self.coedge_end_vertex_id(coedge)?;
122        self.get_vertex(vertex_id)
123    }
124}