rust_3d/
is_searchable_mesh.rs

1/*
2Copyright 2017 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! IsSearchableMesh trait used for meshes which have extended search methods
24
25use crate::*;
26
27//------------------------------------------------------------------------------
28
29/// IsSearchableMesh trait used for meshes which have extended search methods
30pub trait IsSearchableMesh<V, TU>: IsMesh<V, Face3> {
31    /// Should return the edge ids of the given face. Error if id invalid
32    fn edges_of_face(&self, faceid: FId) -> Result<(EId, EId, EId)>;
33    /// Should append the edges originating at the given vertex (pointing away / having the vertex as tail). Error if id invalid
34    fn edges_originating_from_vertex(&self, vertexid: VId, result: &mut Vec<EId>) -> Result<()>;
35    /// Should append the edges ending at the given vertex (pointing to / having the vertex as head). Error if id invalid
36    /// cache can be any Vec and can be used to store intermediate results avoiding allocations
37    fn edges_ending_at_vertex(
38        &self,
39        vertexid: VId,
40        cache: &mut Vec<EId>,
41        result: &mut Vec<EId>,
42    ) -> Result<()>;
43    /// Should append the edges connecting with the vertex. Error if id invalid
44    /// cache can be any Vec and can be used to store intermediate results avoiding allocations
45    fn edges_of_vertex(
46        &self,
47        vertexid: VId,
48        cache: &mut Vec<EId>,
49        result: &mut Vec<EId>,
50    ) -> Result<()>;
51    /// Should return the vertex id of the edge's tail. Error if id invalid
52    fn edge_tail(&self, edgeid: EId) -> Result<VId>;
53    /// Should return the vertex id of the edge's head. Error if id invalid
54    fn edge_head(&self, edgeid: EId) -> Result<VId>;
55    /// Should return the edge id of the next edge. Error if id invalid
56    fn edge_next(&self, edgeid: EId) -> Result<EId>;
57    /// Should return the edge id of the previous edge. Error if id invalid
58    fn edge_prev(&self, edgeid: EId) -> Result<EId>;
59    /// Should return the edge id of the twin edge. Error if id invalid, None if there is none
60    fn edge_twin(&self, edgeid: EId) -> Result<Option<EId>>;
61    /// Should return the face id of the edges face. Error if id invalid
62    fn edge_face(&self, edgeid: EId) -> Result<FId>;
63
64    /// Returns the number of edges within the mesh
65    fn num_edges(&self) -> usize {
66        self.num_faces() * 3
67    }
68    /// Appends faces a vertex is part of. Error if id invalid
69    /// cache can be any Vec
70    fn faces_of_vertex(
71        &self,
72        vertexid: VId,
73        cache: &mut Vec<EId>,
74        result: &mut Vec<FId>,
75    ) -> Result<()> {
76        cache.clear();
77        self.edges_originating_from_vertex(vertexid, cache)?;
78
79        for edgeid in cache {
80            self.edge_face(*edgeid).map(|faceid| result.push(faceid))?;
81        }
82        Ok(())
83    }
84    /// Appends the neighbouring faces of the given face which share the same edges. Error if id invalid
85    fn face_edge_neighbours(&self, faceid: FId, result: &mut Vec<FId>) -> Result<()> {
86        let (e1, e2, e3) = self.edges_of_face(faceid)?;
87
88        {
89            let mut add_twin_face = |edgeid| {
90                self.edge_twin(edgeid).map(|option| match option {
91                    None => {}
92                    Some(twin) => {
93                        let _ = self.edge_face(twin).map(|x| result.push(x));
94                    }
95                })
96            };
97
98            add_twin_face(e1)?;
99            add_twin_face(e2)?;
100            add_twin_face(e3)?;
101        }
102        Ok(())
103    }
104    /// Appends the neighbouring faces of the given face which share the same vertices. Sorts and dedups the result. Error if id invalid
105    /// cache can be any Vec
106    fn face_vertex_neighbours(
107        &self,
108        faceid: FId,
109        cache: &mut Vec<EId>,
110        result: &mut Vec<FId>,
111    ) -> Result<()> {
112        cache.clear();
113        let vids = self.face_vertex_ids(faceid)?;
114
115        {
116            let mut add_vertex_faces = |vertexid| self.faces_of_vertex(vertexid, cache, result);
117
118            add_vertex_faces(vids.a)?;
119            add_vertex_faces(vids.b)?;
120            add_vertex_faces(vids.c)?;
121        }
122        result.sort();
123        result.dedup();
124        Ok(())
125    }
126}