1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use fj_interop::mesh::Color;
use fj_math::Triangle;

use crate::builder::FaceBuilder;

use super::{Cycle, Surface};

/// A face of a shape
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Face {
    representation: Representation,
}

impl Face {
    /// Build a face using [`FaceBuilder`]
    pub fn build(surface: Surface) -> FaceBuilder {
        FaceBuilder::new(surface)
    }

    /// Construct a new instance of `Face`
    ///
    /// Creates the face with no exteriors, no interiors and the default color.
    /// This can be overridden using the `with_` methods.
    pub fn new(surface: Surface) -> Self {
        Self {
            representation: Representation::BRep(BRep {
                surface,
                exteriors: Vec::new(),
                interiors: Vec::new(),
                color: Color::default(),
            }),
        }
    }

    /// Construct an instance that uses triangle representation
    ///
    /// Triangle representation is obsolete, and only still present because
    /// there is one last place in the kernel code that uses it. Don't add any
    /// more of those places!
    ///
    /// See this issue for more context:
    /// <https://github.com/hannobraun/Fornjot/issues/97>
    pub fn from_triangles(triangles: TriRep) -> Self {
        Self {
            representation: Representation::TriRep(triangles),
        }
    }

    /// Add exterior cycles to the face
    ///
    /// Consumes the face and returns the updated instance.
    ///
    /// # Panics
    ///
    /// Panics, if the added cycles are not defined in the face's surface.
    pub fn with_exteriors(
        mut self,
        exteriors: impl IntoIterator<Item = Cycle>,
    ) -> Self {
        for cycle in exteriors.into_iter() {
            assert_eq!(
                self.surface(),
                cycle.surface(),
                "Cycles that bound a face must be in face's surface"
            );

            self.brep_mut().exteriors.push(cycle);
        }

        self
    }

    /// Add interior cycles to the face
    ///
    /// Consumes the face and returns the updated instance.
    ///
    /// # Panics
    ///
    /// Panics, if the added cycles are not defined in the face's surface.
    pub fn with_interiors(
        mut self,
        interiors: impl IntoIterator<Item = Cycle>,
    ) -> Self {
        for cycle in interiors.into_iter() {
            assert_eq!(
                self.surface(),
                cycle.surface(),
                "Cycles that bound a face must be in face's surface"
            );

            self.brep_mut().interiors.push(cycle);
        }

        self
    }

    /// Update the color of the face
    ///
    /// Consumes the face and returns the updated instance.
    pub fn with_color(mut self, color: Color) -> Self {
        self.brep_mut().color = color;
        self
    }

    /// Access this face's surface
    pub fn surface(&self) -> &Surface {
        &self.brep().surface
    }

    /// Access the cycles that bound the face on the outside
    pub fn exteriors(&self) -> impl Iterator<Item = &Cycle> + '_ {
        self.brep().exteriors.iter()
    }

    /// Access the cycles that bound the face on the inside
    ///
    /// Each of these cycles defines a hole in the face.
    pub fn interiors(&self) -> impl Iterator<Item = &Cycle> + '_ {
        self.brep().interiors.iter()
    }

    /// Access all cycles of this face
    ///
    /// This is equivalent to chaining the iterators returned by
    /// [`Face::exteriors`] and [`Face::interiors`].
    pub fn all_cycles(&self) -> impl Iterator<Item = &Cycle> + '_ {
        self.exteriors().chain(self.interiors())
    }

    /// Access the color of the face
    pub fn color(&self) -> Color {
        self.brep().color
    }

    /// Access triangles, if this face uses triangle representation
    ///
    /// Only some faces still use triangle representation. At some point, none
    /// will. This method exists as a workaround, while the transition is still
    /// in progress.
    pub fn triangles(&self) -> Option<&TriRep> {
        if let Representation::TriRep(triangles) = &self.representation {
            return Some(triangles);
        }

        None
    }

    /// Access the boundary representation of the face
    fn brep(&self) -> &BRep {
        if let Representation::BRep(face) = &self.representation {
            return face;
        }

        // No code that still uses triangle representation is calling this
        // method.
        unreachable!()
    }

    /// Access the boundary representation of the face mutably
    fn brep_mut(&mut self) -> &mut BRep {
        if let Representation::BRep(face) = &mut self.representation {
            return face;
        }

        // No code that still uses triangle representation is calling this
        // method.
        unreachable!()
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
enum Representation {
    BRep(BRep),
    TriRep(TriRep),
}

#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
struct BRep {
    surface: Surface,
    exteriors: Vec<Cycle>,
    interiors: Vec<Cycle>,
    color: Color,
}

type TriRep = Vec<(Triangle<3>, Color)>;