1use std::collections::{btree_set, BTreeSet};
2
3use super::{
4 BehindHandle, Cycle, Face, GlobalEdge, HalfEdge, Object, Surface, Vertex,
5};
6
7pub struct ObjectSet {
9 inner: BTreeSet<Object<BehindHandle>>,
10}
11
12impl From<&Face> for ObjectSet {
13 fn from(face: &Face) -> Self {
14 let mut self_ = Self {
15 inner: BTreeSet::new(),
16 };
17
18 face.insert_into_set(&mut self_);
19
20 self_
21 }
22}
23
24impl From<Face> for ObjectSet {
25 fn from(face: Face) -> Self {
26 Self::from(&face)
27 }
28}
29
30impl<Faces> From<Faces> for ObjectSet
31where
32 Faces: IntoIterator<Item = Face>,
33{
34 fn from(faces: Faces) -> Self {
35 let mut self_ = Self {
36 inner: BTreeSet::new(),
37 };
38
39 for face in faces {
40 face.insert_into_set(&mut self_);
41 }
42
43 self_
44 }
45}
46
47impl IntoIterator for ObjectSet {
48 type Item = Object<BehindHandle>;
49 type IntoIter = btree_set::IntoIter<Self::Item>;
50
51 fn into_iter(self) -> Self::IntoIter {
52 self.inner.into_iter()
53 }
54}
55
56trait InsertIntoSet {
57 fn insert_into_set(&self, objects: &mut ObjectSet);
58}
59
60impl InsertIntoSet for Cycle {
61 fn insert_into_set(&self, objects: &mut ObjectSet) {
62 for half_edge in self.half_edges() {
63 objects.inner.insert(half_edge.clone().into());
64 half_edge.insert_into_set(objects);
65 }
66 }
67}
68
69impl InsertIntoSet for Face {
70 fn insert_into_set(&self, objects: &mut ObjectSet) {
71 objects.inner.insert(self.surface().clone().into());
72 self.surface().insert_into_set(objects);
73
74 objects.inner.insert(self.exterior().clone().into());
75 self.exterior().insert_into_set(objects);
76
77 for interior in self.interiors() {
78 objects.inner.insert(interior.clone().into());
79 }
80 for interior in self.interiors() {
81 interior.insert_into_set(objects);
82 }
83 }
84}
85
86impl InsertIntoSet for GlobalEdge {
87 fn insert_into_set(&self, _: &mut ObjectSet) {}
88}
89
90impl InsertIntoSet for HalfEdge {
91 fn insert_into_set(&self, objects: &mut ObjectSet) {
92 objects.inner.insert(self.start_vertex().clone().into());
93 self.start_vertex().insert_into_set(objects);
94
95 objects.inner.insert(self.global_form().clone().into());
96 self.global_form().insert_into_set(objects);
97 }
98}
99
100impl InsertIntoSet for Surface {
101 fn insert_into_set(&self, _: &mut ObjectSet) {}
102}
103
104impl InsertIntoSet for Vertex {
105 fn insert_into_set(&self, _: &mut ObjectSet) {}
106}