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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use truck_topology::*;
use crate::topo_traits::*;
use crate::topo_impls::*;

impl<P, C, S> Sweep<P, C, S> for Vertex<P> {
    type Swept = Edge<P, C>;
    /// Transforms a vertex and creates an edge by connecting vertices.
    /// # Examples
    /// ```
    /// use truck_topology::*;
    /// use truck_modeling::topo_traits::*;
    /// let v = Vertex::new(1);
    /// let edge = v.sweep(
    ///     &move |i: &usize| *i + 1,
    ///     &usize::clone,
    ///     &<()>::clone,
    ///     &move |i: &usize, j: &usize| *i * 10 + j,
    ///     &move |_, _| (),
    /// );
    /// assert_eq!(*edge.front().lock_point().unwrap(), 1);
    /// assert_eq!(*edge.back().lock_point().unwrap(), 2);
    /// assert_eq!(*edge.lock_curve().unwrap(), 12);
    /// ```
    fn sweep<
        FP: Fn(&P) -> P,
        FC: Fn(&C) -> C,
        FS: Fn(&S) -> S,
        CP: Fn(&P, &P) -> C,
        CC: Fn(&C, &C) -> S,
    >(
        &self,
        point_mapping: &FP,
        curve_mapping: &FC,
        surface_mapping: &FS,
        connect_points: &CP,
        _: &CC,
    ) -> Self::Swept
    {
        let v = self.mapped(point_mapping, curve_mapping, surface_mapping);
        connect_vertices(self, &v, connect_points)
    }
}

impl<P, C, S> Sweep<P, C, S> for Edge<P, C> {
    type Swept = Face<P, C, S>;
    /// Transforms an edge and creates a face by connecting vertices and edges.
    /// # Examples
    /// ```
    /// use truck_topology::*;
    /// use truck_modeling::topo_traits::*;
    /// let edge = Edge::new(
    ///     &Vertex::new(1),
    ///     &Vertex::new(2),
    ///     100,
    /// );
    /// let face = edge.sweep(
    ///     &move |i: &usize| *i + 2,
    ///     &move |j: &usize| *j + 100,
    ///     &usize::clone,
    ///     &move |i: &usize, j: &usize| *i * 10 + j,
    ///     &move |i: &usize, j: &usize| *i + *j,
    /// );
    ///
    /// assert_eq!(*face.lock_surface().unwrap(), 300);
    /// assert_eq!(face.boundaries().len(), 1);
    ///
    /// let boundary: Wire<usize, usize> = face.boundaries()[0].clone();
    /// assert_eq!(boundary.len(), 4);
    ///
    /// assert_eq!(boundary[0], edge);
    ///
    /// assert_eq!(*boundary[1].front().lock_point().unwrap(), 2);
    /// assert_eq!(*boundary[1].back().lock_point().unwrap(), 4);
    /// assert_eq!(*boundary[1].lock_curve().unwrap(), 24);
    ///
    /// assert_eq!(*boundary[2].front().lock_point().unwrap(), 4);
    /// assert_eq!(*boundary[2].back().lock_point().unwrap(), 3);
    /// // the curve of second edge is determined by connect_curves  
    /// assert_eq!(*boundary[2].lock_curve().unwrap(), 200);
    ///
    /// assert_eq!(*boundary[3].front().lock_point().unwrap(), 3);
    /// assert_eq!(*boundary[3].back().lock_point().unwrap(), 1);
    /// assert_eq!(*boundary[3].lock_curve().unwrap(), 13);
    /// ```
    fn sweep<
        FP: Fn(&P) -> P,
        FC: Fn(&C) -> C,
        FS: Fn(&S) -> S,
        CP: Fn(&P, &P) -> C,
        CC: Fn(&C, &C) -> S,
    >(
        &self,
        point_mapping: &FP,
        curve_mapping: &FC,
        surface_mapping: &FS,
        connect_points: &CP,
        connect_curves: &CC,
    ) -> Self::Swept
    {
        let edge = self.mapped(point_mapping, curve_mapping, surface_mapping);
        connect_edges(self, &edge, connect_points, connect_curves)
    }
}

impl<P, C, S> Sweep<P, C, S> for Wire<P, C> {
    type Swept = Shell<P, C, S>;
    /// Transforms a wire and creates a shell by connecting vertices and edges.
    /// # Examples
    /// ```
    /// use truck_topology::*;
    /// use truck_modeling::topo_traits::*;
    /// use shell::ShellCondition;
    /// let v = Vertex::news(&[1, 2, 3, 4]);
    /// let wire = Wire::from(vec![
    ///     Edge::new(&v[0], &v[1], 100),
    ///     Edge::new(&v[1], &v[2], 110),
    ///     Edge::new(&v[3], &v[2], 120).inverse(),
    ///     Edge::new(&v[3], &v[1], 130),
    /// ]);
    /// let shell = wire.sweep(
    ///     &move |i: &usize| *i + 4,
    ///     &move |j: &usize| *j + 100,
    ///     &usize::clone,
    ///     &move |i: &usize, j: &usize| *i * 10 + j,
    ///     &move |i: &usize, j: &usize| *i + *j,
    /// );
    /// assert!(shell.is_connected());
    /// 
    /// let face1 = &shell[1];
    /// assert_eq!(*face1.lock_surface().unwrap(), 320);
    /// let boundary1 = &face1.boundaries()[0];
    /// assert_eq!(*boundary1[0].lock_curve().unwrap(), 110);
    /// assert_eq!(*boundary1[1].lock_curve().unwrap(), 37);
    /// assert_eq!(*boundary1[2].lock_curve().unwrap(), 210);
    /// assert_eq!(*boundary1[3].lock_curve().unwrap(), 26);
    /// assert_eq!(*boundary1[0].front().lock_point().unwrap(), 2);
    /// assert_eq!(*boundary1[1].front().lock_point().unwrap(), 3);
    /// assert_eq!(*boundary1[2].front().lock_point().unwrap(), 7);
    /// assert_eq!(*boundary1[3].front().lock_point().unwrap(), 6);
    /// 
    /// let face2 = &shell[2];
    /// assert_eq!(*face2.lock_surface().unwrap(), 340);
    /// let boundary2 = &face2.boundaries()[0];
    /// assert_eq!(*boundary2[0].lock_curve().unwrap(), 120);
    /// assert_eq!(*boundary2[1].lock_curve().unwrap(), 48);
    /// assert_eq!(*boundary2[2].lock_curve().unwrap(), 220);
    /// assert_eq!(*boundary2[3].lock_curve().unwrap(), 37);
    /// assert_eq!(*boundary2[0].front().lock_point().unwrap(), 3);
    /// assert_eq!(*boundary2[1].front().lock_point().unwrap(), 4);
    /// assert_eq!(*boundary2[2].front().lock_point().unwrap(), 8);
    /// assert_eq!(*boundary2[3].front().lock_point().unwrap(), 7);
    /// 
    /// assert_eq!(boundary1[1].id(), boundary2[3].id());
    /// assert_ne!(boundary1[1], boundary2[3]);
    /// ```
    fn sweep<
        FP: Fn(&P) -> P,
        FC: Fn(&C) -> C,
        FS: Fn(&S) -> S,
        CP: Fn(&P, &P) -> C,
        CC: Fn(&C, &C) -> S,
    >(
        &self,
        point_mapping: &FP,
        curve_mapping: &FC,
        surface_mapping: &FS,
        connect_points: &CP,
        connect_curves: &CC,
    ) -> Self::Swept
    {
        let wire = self.mapped(point_mapping, curve_mapping, surface_mapping);
        connect_wires(self, &wire, connect_points, connect_curves).collect()
    }
}

impl<P, C, S> Sweep<P, C, S> for Face<P, C, S> {
    type Swept = Solid<P, C, S>;
    /// Transforms a face and creates a solid by connecting vertices, edges and faces.
    /// # Examples
    /// ```
    /// use truck_topology::*;
    /// use truck_modeling::topo_traits::*;
    /// let v = Vertex::news(&[1, 2]);
    /// let edge = Edge::new(&v[0], &v[1], 12);
    /// let face = edge.sweep(
    ///     &move |i: &usize| *i + 2,
    ///     &move |i: &usize| *i + 22,
    ///     &usize::clone,
    ///     &move |i: &usize, j: &usize| *i * 10 + *j,
    ///     &move |i: &usize, j: &usize| *i * 100 + *j,
    /// );
    /// let solid = face.sweep(
    ///     &move |i: &usize| *i + 4,
    ///     &move |i: &usize| *i + 44,
    ///     &move |i: &usize| *i + 3333,
    ///     &move |i: &usize, j: &usize| *i * 10 + *j,
    ///     &move |i: &usize, j: &usize| *i * 100 + *j,
    /// );
    /// let shell = &solid.boundaries()[0];
    /// # assert_eq!(shell.shell_condition(), shell::ShellCondition::Closed);
    /// 
    /// // The boundary shell has 6 faces since this solid is a cube.
    /// assert_eq!(shell.len(), 6);
    /// 
    /// // the first face of the boundary shell is the inversed original face.
    /// assert_eq!(shell[0].id(), face.id());
    /// assert_ne!(shell[0].orientation(), face.orientation());
    /// 
    /// // Check the condition of the third face.
    /// assert_eq!(*shell[2].lock_surface().unwrap(), 2468);
    /// let bdry = &shell[2].boundaries()[0];
    /// assert_eq!(*bdry[0].lock_curve().unwrap(), 24);
    /// assert_eq!(*bdry[1].lock_curve().unwrap(), 48);
    /// assert_eq!(*bdry[2].lock_curve().unwrap(), 68);
    /// assert_eq!(*bdry[3].lock_curve().unwrap(), 26);
    /// 
    /// // Check the last face: seiling.
    /// assert_eq!(*shell[5].lock_surface().unwrap(), 4567);
    /// ```
    fn sweep<
        FP: Fn(&P) -> P,
        FC: Fn(&C) -> C,
        FS: Fn(&S) -> S,
        CP: Fn(&P, &P) -> C,
        CC: Fn(&C, &C) -> S,
    >(
        &self,
        point_mapping: &FP,
        curve_mapping: &FC,
        surface_mapping: &FS,
        connect_points: &CP,
        connect_curves: &CC,
    ) -> Self::Swept
    {
        let mut shell = Shell::new();
        shell.push(self.inverse());
        let seiling = self.mapped(point_mapping, curve_mapping, surface_mapping);        
        let biter0 = self.boundary_iters().into_iter().flatten();
        let biter1 = seiling.boundary_iters().into_iter().flatten();
        shell.extend(connect_raw_wires(biter0, biter1, connect_points, connect_curves));
        shell.push(seiling);
        Solid::debug_new(vec![shell])
    }
}

impl<P, C, S> Sweep<P, C, S> for Shell<P, C, S> {
    type Swept = Vec<Result<Solid<P, C, S>>>;
    /// Transforms a shell and tries to create solids by connecting vertices, edges and faces.
    /// 
    /// In this function, the shell is broken down into connected components and each of components
    /// extruded to form a solid.
    /// 
    /// # Remarks
    /// For each component, this method returns `Result` of sweeping,
    /// since there is no clear guarantee that a solid can be formed by the extrusion of the shell.
    /// At least, a component must be oriented and not be closed to be extruded.
    fn sweep<
        FP: Fn(&P) -> P,
        FC: Fn(&C) -> C,
        FS: Fn(&S) -> S,
        CP: Fn(&P, &P) -> C,
        CC: Fn(&C, &C) -> S,
    >(
        &self,
        point_mapping: &FP,
        curve_mapping: &FC,
        surface_mapping: &FS,
        connect_points: &CP,
        connect_curves: &CC,
    ) -> Self::Swept
    {
        self.connected_components().into_iter().map(move|shell| {
            let mut bdry = Shell::new();
            let mut seiling = shell.mapped(point_mapping, curve_mapping, surface_mapping);
            bdry.extend(shell.face_iter().map(|face| face.inverse()));
            let bdries0 = shell.extract_boundaries();
            let bdries1 = seiling.extract_boundaries();
            let biter0 = bdries0.iter().flat_map(Wire::edge_iter);
            let biter1 = bdries1.iter().flat_map(Wire::edge_iter);
            bdry.extend(connect_wires(biter0, biter1, connect_points, connect_curves));
            bdry.append(&mut seiling);
            Solid::try_new(vec![bdry])
        }).collect()
    }
}