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
use fj_math::Point;

use crate::{
    objects::{Face, Shell},
    operations::{
        build::{BuildFace, Polygon},
        insert::{Insert, IsInserted, IsInsertedNo, IsInsertedYes},
        join::JoinCycle,
        reverse::ReverseCurveCoordinateSystems,
        update::{UpdateCycle, UpdateFace, UpdateRegion},
    },
    services::Services,
};

/// Build a [`Shell`]
///
/// See [module-level documentation] for context.
///
/// [module-level documentation]: super
pub trait BuildShell {
    /// Build an empty shell
    fn empty() -> Shell {
        Shell::new([])
    }

    /// Build a tetrahedron from the provided points
    ///
    /// Accepts 4 points, naturally. For the purposes of the following
    /// discussion, let's call those `a`, `b`, `c`, and `d`, and assume that the
    /// order they are listed in here matches the order they are provided in
    /// within the array.
    ///
    /// Assumes that `a`, `b`, and `c` form a triangle in counter-clockwise
    /// order, when arranging the viewpoint such that it is on the opposite side
    /// of the triangle from `d`. If this assumption is met, the orientation of
    /// all faces of the tetrahedron will be valid, meaning their
    /// counter-clockwise sides are outside.
    ///
    /// # Implementation Note
    ///
    /// In principle, this method doesn't need to make assumptions about the
    /// order of the points provided. It could, given some extra effort, just
    /// build a correct tetrahedron, regardless of that order.
    fn tetrahedron(
        points: [impl Into<Point<3>>; 4],
        services: &mut Services,
    ) -> TetrahedronShell {
        let [a, b, c, d] = points.map(Into::into);

        let abc = Face::triangle([a, b, c], services);
        let bad = Face::triangle([b, a, d], services).update_region(|region| {
            region
                .update_exterior(|cycle| {
                    cycle
                        .update_half_edge(
                            cycle.half_edges().nth_circular(0),
                            |edge| {
                                edge.reverse_curve_coordinate_systems(services)
                                    .insert(services)
                            },
                        )
                        .join_to(
                            abc.face.region().exterior(),
                            0..=0,
                            0..=0,
                            services,
                        )
                        .insert(services)
                })
                .insert(services)
        });
        let dac = Face::triangle([d, a, c], services).update_region(|region| {
            region
                .update_exterior(|cycle| {
                    cycle
                        .update_half_edge(
                            cycle.half_edges().nth_circular(1),
                            |edge| {
                                edge.reverse_curve_coordinate_systems(services)
                                    .insert(services)
                            },
                        )
                        .join_to(
                            abc.face.region().exterior(),
                            1..=1,
                            2..=2,
                            services,
                        )
                        .update_half_edge(
                            cycle.half_edges().nth_circular(0),
                            |edge| {
                                edge.reverse_curve_coordinate_systems(services)
                                    .insert(services)
                            },
                        )
                        .join_to(
                            bad.face.region().exterior(),
                            0..=0,
                            1..=1,
                            services,
                        )
                        .insert(services)
                })
                .insert(services)
        });
        let cbd = Face::triangle([c, b, d], services).update_region(|region| {
            region
                .update_exterior(|cycle| {
                    cycle
                        .update_half_edge(
                            cycle.half_edges().nth_circular(0),
                            |edge| {
                                edge.reverse_curve_coordinate_systems(services)
                                    .insert(services)
                            },
                        )
                        .update_half_edge(
                            cycle.half_edges().nth_circular(1),
                            |edge| {
                                edge.reverse_curve_coordinate_systems(services)
                                    .insert(services)
                            },
                        )
                        .update_half_edge(
                            cycle.half_edges().nth_circular(2),
                            |edge| {
                                edge.reverse_curve_coordinate_systems(services)
                                    .insert(services)
                            },
                        )
                        .join_to(
                            abc.face.region().exterior(),
                            0..=0,
                            1..=1,
                            services,
                        )
                        .join_to(
                            bad.face.region().exterior(),
                            1..=1,
                            2..=2,
                            services,
                        )
                        .join_to(
                            dac.face.region().exterior(),
                            2..=2,
                            2..=2,
                            services,
                        )
                        .insert(services)
                })
                .insert(services)
        });

        let triangles =
            [abc, bad, dac, cbd].map(|triangle| triangle.insert(services));
        let shell =
            Shell::new(triangles.iter().map(|triangle| triangle.face.clone()));

        let [abc, bad, dac, cbd] = triangles;

        TetrahedronShell {
            shell,
            abc,
            bad,
            dac,
            cbd,
        }
    }
}

impl BuildShell for Shell {}

/// A tetrahedron
///
/// A tetrahedron is constructed from 4 points and has 4 faces. For the purpose
/// of naming the fields of this struct, the points are named `a`, `b`, `c`, and
/// `d`, in the order in which they are passed.
///
/// Returned by [`BuildShell::tetrahedron`].
pub struct TetrahedronShell<I: IsInserted = IsInsertedNo> {
    /// The shell that forms the tetrahedron
    pub shell: I::T<Shell>,

    /// The face formed by the points `a`, `b`, and `c`.
    pub abc: Polygon<3, IsInsertedYes>,

    /// The face formed by the points `b`, `a`, and `d`.
    pub bad: Polygon<3, IsInsertedYes>,

    /// The face formed by the points `d`, `a`, and `c`.
    pub dac: Polygon<3, IsInsertedYes>,

    /// The face formed by the points `c`, `b`, and `d`.
    pub cbd: Polygon<3, IsInsertedYes>,
}