mesh_graph/
selection.rs

1use hashbrown::HashSet;
2
3use super::{FaceId, HalfedgeId, MeshGraph, VertexId};
4
5#[derive(Debug, Clone, Default)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct Selection {
8    pub vertices: HashSet<VertexId>,
9    pub halfedges: HashSet<HalfedgeId>,
10    pub faces: HashSet<FaceId>,
11}
12
13impl Selection {
14    pub fn select_all(mesh_graph: &MeshGraph) -> Self {
15        Self {
16            faces: mesh_graph.faces.keys().collect(),
17            ..Default::default()
18        }
19    }
20
21    pub fn resolve_to_halfedges(&self, mesh_graph: &MeshGraph) -> HashSet<HalfedgeId> {
22        let mut halfedges = self.halfedges.clone();
23
24        for face in &self.faces {
25            halfedges.extend(mesh_graph.faces[*face].halfedges(mesh_graph));
26        }
27
28        for vertex in &self.vertices {
29            halfedges.extend(mesh_graph.vertices[*vertex].outgoing_halfedges(mesh_graph));
30        }
31
32        halfedges
33    }
34
35    pub fn resolve_to_vertices(&self, mesh_graph: &MeshGraph) -> HashSet<VertexId> {
36        let mut vertices = self.vertices.clone();
37
38        for halfedge in &self.halfedges {
39            let he = mesh_graph.halfedges[*halfedge];
40            vertices.insert(he.start_vertex(mesh_graph));
41            vertices.insert(he.end_vertex);
42        }
43
44        for face in &self.faces {
45            vertices.extend(mesh_graph.faces[*face].vertices(mesh_graph));
46        }
47
48        vertices
49    }
50
51    // TODO : also resolve to faces
52}
53
54pub trait SelectionOps<T> {
55    fn insert(&mut self, item: T);
56    fn remove(&mut self, item: T);
57}
58
59impl SelectionOps<VertexId> for Selection {
60    fn insert(&mut self, item: VertexId) {
61        self.vertices.insert(item);
62    }
63
64    fn remove(&mut self, item: VertexId) {
65        self.vertices.remove(&item);
66    }
67}
68
69impl SelectionOps<HalfedgeId> for Selection {
70    fn insert(&mut self, item: HalfedgeId) {
71        self.halfedges.insert(item);
72    }
73
74    fn remove(&mut self, item: HalfedgeId) {
75        self.halfedges.remove(&item);
76    }
77}
78
79impl SelectionOps<FaceId> for Selection {
80    fn insert(&mut self, item: FaceId) {
81        self.faces.insert(item);
82    }
83
84    fn remove(&mut self, item: FaceId) {
85        self.faces.remove(&item);
86    }
87}
88
89impl From<VertexId> for Selection {
90    fn from(value: VertexId) -> Self {
91        Self::from_iter(vec![value])
92    }
93}
94
95impl From<HalfedgeId> for Selection {
96    fn from(value: HalfedgeId) -> Self {
97        Self::from_iter(vec![value])
98    }
99}
100
101impl From<FaceId> for Selection {
102    fn from(value: FaceId) -> Self {
103        Self::from_iter(vec![value])
104    }
105}
106
107impl FromIterator<VertexId> for Selection {
108    fn from_iter<T: IntoIterator<Item = VertexId>>(iter: T) -> Self {
109        Selection {
110            vertices: HashSet::from_iter(iter),
111            ..Default::default()
112        }
113    }
114}
115
116impl FromIterator<HalfedgeId> for Selection {
117    fn from_iter<T: IntoIterator<Item = HalfedgeId>>(iter: T) -> Self {
118        Selection {
119            halfedges: HashSet::from_iter(iter),
120            ..Default::default()
121        }
122    }
123}
124
125impl FromIterator<FaceId> for Selection {
126    fn from_iter<T: IntoIterator<Item = FaceId>>(iter: T) -> Self {
127        Selection {
128            faces: HashSet::from_iter(iter),
129            ..Default::default()
130        }
131    }
132}
133
134macro_rules! impl_from_for_selection {
135    ($type:ident) => {
136        impl From<$type<VertexId>> for Selection {
137            fn from(value: $type<VertexId>) -> Self {
138                Self::from_iter(value)
139            }
140        }
141        impl From<$type<HalfedgeId>> for Selection {
142            fn from(value: $type<HalfedgeId>) -> Self {
143                Self::from_iter(value)
144            }
145        }
146        impl From<$type<FaceId>> for Selection {
147            fn from(value: $type<FaceId>) -> Self {
148                Self::from_iter(value)
149            }
150        }
151    };
152}
153
154impl_from_for_selection!(Vec);
155impl_from_for_selection!(HashSet);