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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use fj_math::Scalar;

use crate::{
    algorithms::transform::TransformObject,
    objects::{
        Curve, Cycle, Face, HalfEdge, Shell, Surface, SurfaceVertex, Vertex,
    },
    partial::HasPartial,
    stores::{Handle, Stores},
};

/// API for building a [`Shell`]
///
/// Also see [`Shell::builder`].
pub struct ShellBuilder<'a> {
    /// The stores that the created objects are put in
    pub stores: &'a Stores,
}

impl<'a> ShellBuilder<'a> {
    /// Create a cube from the length of its edges
    pub fn build_cube_from_edge_length(
        self,
        edge_length: impl Into<Scalar>,
    ) -> Shell {
        let edge_length = edge_length.into();

        // Let's define some short-hands. We're going to need them a lot.
        const Z: Scalar = Scalar::ZERO;
        let h = edge_length / 2.;

        let bottom = {
            let surface = self
                .stores
                .surfaces
                .insert(Surface::xy_plane())
                .translate([Z, Z, -h], self.stores);

            Face::builder(self.stores, surface)
                .with_exterior_polygon_from_points([
                    [-h, -h],
                    [h, -h],
                    [h, h],
                    [-h, h],
                ])
                .build()
        };

        let (sides, top_edges) = {
            let surfaces = bottom
                .exterior()
                .half_edges()
                .map(|half_edge| {
                    let [a, b] = half_edge
                        .vertices()
                        .clone()
                        .map(|vertex| vertex.global_form().position());
                    let c = a + [Z, Z, edge_length];

                    self.stores
                        .surfaces
                        .insert(Surface::plane_from_points([a, b, c]))
                })
                .collect::<Vec<_>>();

            let bottoms = bottom
                .exterior()
                .half_edges()
                .zip(&surfaces)
                .map(|(half_edge, surface)| {
                    HalfEdge::partial()
                        .with_surface(Some(surface.clone()))
                        .with_global_form(Some(half_edge.global_form().clone()))
                        .as_line_segment_from_points([[Z, Z], [edge_length, Z]])
                        .build(self.stores)
                })
                .collect::<Vec<_>>();

            let sides_up = bottoms
                .clone()
                .into_iter()
                .zip(&surfaces)
                .map(|(bottom, surface)| {
                    let [_, from] = bottom.vertices();

                    let from = from.surface_form().clone();
                    let to = SurfaceVertex::partial()
                        .with_position(Some(from.position() + [Z, edge_length]))
                        .with_surface(Some(surface.clone()));

                    HalfEdge::partial()
                        .with_vertices(Some([
                            Vertex::partial().with_surface_form(Some(from)),
                            Vertex::partial().with_surface_form(Some(to)),
                        ]))
                        .as_line_segment()
                        .build(self.stores)
                })
                .collect::<Vec<_>>();

            let sides_down = {
                let mut sides_up_prev = sides_up.clone();
                sides_up_prev.rotate_right(1);

                bottoms
                    .clone()
                    .into_iter()
                    .zip(sides_up_prev)
                    .zip(&surfaces)
                    .map(|((bottom, side_up_prev), surface)| {
                        let [_, from] = side_up_prev.vertices();
                        let [to, _] = bottom.vertices();

                        let to = to.surface_form().clone();
                        let from = SurfaceVertex::partial()
                            .with_position(Some(
                                to.position() + [Z, edge_length],
                            ))
                            .with_surface(Some(surface.clone()))
                            .with_global_form(Some(from.global_form().clone()));

                        let curve = Handle::<Curve>::partial()
                            .with_global_form(Some(
                                side_up_prev.curve().global_form().clone(),
                            ));

                        HalfEdge::partial()
                            .with_curve(Some(curve))
                            .with_vertices(Some([
                                Vertex::partial().with_surface_form(Some(from)),
                                Vertex::partial().with_surface_form(Some(to)),
                            ]))
                            .as_line_segment()
                            .build(self.stores)
                    })
                    .collect::<Vec<_>>()
            };

            let tops = sides_up
                .clone()
                .into_iter()
                .zip(sides_down.clone())
                .zip(&surfaces)
                .map(|((side_up, side_down), surface)| {
                    let [_, from] = side_up.vertices();
                    let [to, _] = side_down.vertices();

                    let from = from.surface_form().clone();
                    let to = SurfaceVertex::partial()
                        .with_position(Some(
                            from.position() + [-edge_length, Z],
                        ))
                        .with_surface(Some(surface.clone()))
                        .with_global_form(Some(to.global_form().clone()));

                    let from = Vertex::partial().with_surface_form(Some(from));
                    let to = Vertex::partial().with_surface_form(Some(to));

                    HalfEdge::partial()
                        .with_vertices(Some([from, to]))
                        .as_line_segment()
                        .build(self.stores)
                })
                .collect::<Vec<_>>();

            let sides = bottoms
                .into_iter()
                .zip(sides_up)
                .zip(tops.clone())
                .zip(sides_down)
                .zip(surfaces)
                .map(|((((bottom, side_up), top), side_down), surface)| {
                    let cycle = Cycle::partial()
                        .with_surface(Some(surface))
                        .with_half_edges([bottom, side_up, top, side_down])
                        .build(self.stores);

                    Face::from_exterior(cycle)
                });

            (sides, tops)
        };

        let top = {
            let surface = self
                .stores
                .surfaces
                .insert(Surface::xy_plane())
                .translate([Z, Z, h], self.stores);

            let points = [[-h, -h], [-h, h], [h, h], [h, -h], [-h, -h]];

            let mut top_edges = top_edges;
            top_edges.reverse();

            let mut edges = Vec::new();
            for (points, edge) in points.windows(2).zip(top_edges) {
                // This can't panic, as we passed `2` to `windows`. Can be
                // cleaned up, once `array_windows` is stable.
                let points = [points[0], points[1]];

                // Can be cleaned up, once `zip` is stable:
                // https://doc.rust-lang.org/std/primitive.array.html#method.zip
                let [point_a, point_b] = points;
                let [vertex_a, vertex_b] = edge.vertices().clone();
                let vertices = [(point_a, vertex_a), (point_b, vertex_b)].map(
                    |(point, vertex)| {
                        let surface_form = SurfaceVertex::partial()
                            .with_position(Some(point))
                            .with_surface(Some(surface.clone()))
                            .with_global_form(Some(
                                vertex.global_form().clone(),
                            ))
                            .build(self.stores);
                        Vertex::partial()
                            .with_position(Some(vertex.position()))
                            .with_surface_form(Some(surface_form))
                    },
                );

                edges.push(
                    HalfEdge::partial()
                        .with_vertices(Some(vertices))
                        .with_global_form(Some(edge.global_form().clone()))
                        .as_line_segment()
                        .build(self.stores),
                );
            }

            Face::from_exterior(Cycle::new(surface, edges))
        };

        let mut faces = Vec::new();
        faces.push(bottom);
        faces.extend(sides);
        faces.push(top);

        Shell::new().with_faces(faces)
    }
}