plexus 0.0.9

3D mesh generation and manipulation.
Documentation
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use failure::{Error, Fail};
use std::collections::{HashMap, HashSet};
use std::ops::{Deref, DerefMut};

use geometry::Geometry;
use graph::{GraphError, Mesh, Perimeter};
use graph::mesh::{Connectivity, Edge, Face, Vertex};
use graph::storage::{EdgeKey, FaceKey, VertexKey};

enum Mode<I = (), B = ()> {
    Immediate(I),
    Batch(B),
}

type Mutant<'a, G> = Mode<&'a mut Mesh<G>, Mesh<G>>;

impl<'a, G> Mutant<'a, G>
where
    G: 'a + Geometry,
{
    pub fn get(&self) -> &Mesh<G> {
        match *self {
            Mode::Batch(ref mesh) => mesh,
            Mode::Immediate(ref mesh) => mesh,
        }
    }

    pub fn get_mut(&mut self) -> &mut Mesh<G> {
        match *self {
            Mode::Batch(ref mut mesh) => mesh,
            Mode::Immediate(ref mut mesh) => mesh,
        }
    }

    pub fn take(self) -> Option<Mesh<G>> {
        match self {
            Mode::Batch(mesh) => Some(mesh),
            _ => None,
        }
    }
}

impl<'a, G> Deref for Mutant<'a, G>
where
    G: 'a + Geometry,
{
    type Target = Mesh<G>;

    fn deref(&self) -> &Self::Target {
        self.get()
    }
}

impl<'a, G> DerefMut for Mutant<'a, G>
where
    G: 'a + Geometry,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.get_mut()
    }
}

/// Mesh mutation.
///
/// Mutates a `Mesh`. This type provides general operations that are supported
/// by both immediate and batch mutations.
pub struct Mutation<'a, G>
where
    G: 'a + Geometry,
{
    mesh: Mutant<'a, G>,
}

impl<'a, G> Mutation<'a, G>
where
    G: 'a + Geometry,
{
    pub fn immediate(mesh: &'a mut Mesh<G>) -> ImmediateMutation<'a, G> {
        ImmediateMutation::new(Mutation {
            mesh: Mode::Immediate(mesh),
        })
    }

    pub fn batch(mesh: Mesh<G>) -> BatchMutation<'a, G> {
        BatchMutation::new(Mutation {
            mesh: Mode::Batch(mesh),
        })
    }
}

/// Vertex mutations.
impl<'a, G> Mutation<'a, G>
where
    G: 'a + Geometry,
{
    pub fn insert_vertex(&mut self, geometry: G::Vertex) -> VertexKey {
        self.mesh
            .vertices
            .insert_with_generator(Vertex::new(geometry))
    }
}

/// Edge mutations.
impl<'a, G> Mutation<'a, G>
where
    G: 'a + Geometry,
{
    pub fn insert_edge(
        &mut self,
        vertices: (VertexKey, VertexKey),
        geometry: G::Edge,
    ) -> Result<EdgeKey, Error> {
        let mesh = self.mesh.get_mut();
        let (a, b) = vertices;
        let ab = (a, b).into();
        let ba = (b, a).into();
        // If the edge already exists, then fail. This ensures an important
        // invariant: edges may only have two adjacent faces. That is, a
        // half-edge may only have one associated face, at most one preceding
        // half-edge, at most one following half-edge, and may form at most one
        // closed loop.
        if mesh.edges.contains_key(&ab) {
            return Err(GraphError::TopologyConflict.into());
        }
        let vertex = {
            if !mesh.vertices.contains_key(&b) {
                return Err(GraphError::TopologyNotFound.into());
            }
            match mesh.vertices.get_mut(&a) {
                Some(vertex) => vertex,
                _ => {
                    return Err(GraphError::TopologyNotFound.into());
                }
            }
        };
        let mut edge = Edge::new(b, geometry);
        // This is the point of no return. The mesh has been mutated. Unwrap
        // results.
        if let Some(opposite) = mesh.edges.get_mut(&ba) {
            edge.opposite = Some(ba);
            opposite.opposite = Some(ab);
        }
        mesh.edges.insert_with_key(&ab, edge);
        vertex.edge = Some(ab);
        Ok(ab)
    }

    fn get_or_insert_edge(
        &mut self,
        vertices: (VertexKey, VertexKey),
        geometry: G::Edge,
    ) -> Result<EdgeKey, Error> {
        self.insert_edge(vertices, geometry).or_else(|error| {
            match error.downcast::<GraphError>().unwrap() {
                GraphError::TopologyConflict => Ok(vertices.into()),
                error => Err(error.into()),
            }
        })
    }

    fn get_or_insert_composite_edge(
        &mut self,
        vertices: (VertexKey, VertexKey),
        geometry: G::Edge,
    ) -> Result<(EdgeKey, EdgeKey), Error> {
        let (a, b) = vertices;
        let ab = self.get_or_insert_edge((a, b), geometry.clone())?;
        let ba = self.get_or_insert_edge((b, a), geometry)?;
        Ok((ab, ba))
    }

    pub fn remove_edge(&mut self, edge: EdgeKey) -> Result<Edge<G>, Error> {
        if let Some(mut edge) = self.mesh.edge_mut(edge) {
            if let Some(mut next) = edge.raw_next_edge_mut() {
                next.previous = None;
            }
            if let Some(mut previous) = edge.raw_previous_edge_mut() {
                previous.next = None;
            }
            edge.source_vertex_mut().edge = None;
        }
        else {
            return Err(Error::from(GraphError::TopologyNotFound));
        }
        Ok(self.mesh.edges.remove(&edge).unwrap())
    }
}

/// Face mutations.
impl<'a, G> Mutation<'a, G>
where
    G: 'a + Geometry,
{
    fn connect_face_interior(&mut self, edges: &[EdgeKey], face: FaceKey) -> Result<(), Error> {
        for (ab, bc) in edges.perimeter() {
            {
                let mut edge = self.mesh.edge_mut(ab).unwrap();
                edge.next = Some(bc);
                edge.face = Some(face);
            }
            {
                let mut edge = self.mesh.edge_mut(bc).unwrap();
                edge.previous = Some(ab);
            }
        }
        Ok(())
    }

    fn connect_face_exterior(
        &mut self,
        edges: &[EdgeKey],
        connectivity: (Connectivity, Connectivity),
    ) -> Result<(), Error> {
        let (incoming, outgoing) = connectivity;
        for (a, b) in edges.iter().map(|edge| edge.to_vertex_keys()) {
            // Only boundary edges must be connected.
            if self.mesh.edge((b, a).into()).unwrap().face().is_none() {
                // The next edge of b-a is the outgoing edge of the destination
                // vertex A that is also a boundary edge or, if there is no
                // such outgoing edge, the next exterior edge of the face. The
                // previous edge is similar.
                let ax = outgoing[&a]
                    .iter()
                    .map(|ax| self.mesh.edge(*ax).unwrap())
                    .find(|edge| edge.face().is_none())
                    .or_else(|| {
                        self.mesh
                            .edge((a, b).into())
                            .unwrap()
                            .into_previous_edge()
                            .into_raw_opposite_edge()
                    })
                    .unwrap()
                    .key();
                {
                    self.mesh.edge_mut((b, a).into()).unwrap().next = Some(ax);
                    self.mesh.edge_mut(ax).unwrap().previous = Some((b, a).into());
                }
                let xb = incoming[&b]
                    .iter()
                    .map(|xb| self.mesh.edge(*xb).unwrap())
                    .find(|edge| edge.face().is_none())
                    .or_else(|| {
                        self.mesh
                            .edge((a, b).into())
                            .unwrap()
                            .into_next_edge()
                            .into_raw_opposite_edge()
                    })
                    .unwrap()
                    .key();
                {
                    self.mesh.edge_mut((b, a).into()).unwrap().previous = Some(xb);
                    self.mesh.edge_mut(xb).unwrap().next = Some((b, a).into());
                }
            }
        }
        Ok(())
    }
}

/// Immediate mesh mutations.
///
/// This type provides mutations that are only available in immediate mode.
/// Immediate mutations are atomic, and fail immediately if the integrity of
/// the mesh is compromised.
pub struct ImmediateMutation<'a, G>
where
    G: 'a + Geometry,
{
    mutation: Mutation<'a, G>,
}

impl<'a, G> ImmediateMutation<'a, G>
where
    G: 'a + Geometry,
{
    fn new(mutation: Mutation<'a, G>) -> Self {
        ImmediateMutation { mutation }
    }

    // TODO: Is there some way to end the mutable borrow early?
    //
    //   pub fn commit(self) {}
}

/// Face mutations.
impl<'a, G> ImmediateMutation<'a, G>
where
    G: 'a + Geometry,
{
    pub fn insert_face(
        &mut self,
        vertices: &[VertexKey],
        geometry: (G::Edge, G::Face),
    ) -> Result<FaceKey, Error> {
        // Before mutating the mesh, collect the incoming and outgoing edges
        // for each vertex.
        let ((incoming, outgoing), singularity) =
            self.mesh.region_connectivity(self.mesh.region(vertices)?);
        if singularity.is_some() {
            return Err(GraphError::TopologyMalformed
                .context("non-manifold connectivity")
                .into());
        }
        // Insert composite edges and collect the interior edges. This is the
        // point of no return; the mesh has been mutated. Unwrap results.
        let edges = vertices
            .perimeter()
            .map(|ab| {
                self.get_or_insert_composite_edge(ab, geometry.0.clone())
                    .unwrap()
                    .0
            })
            .collect::<Vec<_>>();
        let face = self.mesh
            .faces
            .insert_with_generator(Face::new(edges[0], geometry.1));
        self.connect_face_interior(&edges, face).unwrap();
        self.connect_face_exterior(&edges, (incoming, outgoing))
            .unwrap();
        Ok(face)
    }

    // TODO: This may need to interact with batch mutations. If a non-manifold
    //       heals because of a face insertion, then a face removal could undo
    //       that.  It may be necessary to update the errors tracked by
    //       `BatchMutation` in this case. For now, restrict face removal to
    //       immediate mutations.
    // TODO: This should check to see if neighbors become non-manifold. For
    //       example, imagine three triangles that share a vertex, with one
    //       triangle sharing one side with each of its neighbors. Removing the
    //       center triangle would result in two triangles connected by a
    //       single point.
    //
    //       Technically, this is supported (exactly two faces connected by a
    //       single vertex), but perhaps it should be rejected anyway.
    pub fn remove_face(&mut self, face: FaceKey) -> Result<Face<G>, Error> {
        for mut edge in self.mesh
            .face_mut(face)
            .ok_or_else(|| Error::from(GraphError::TopologyNotFound))?
            .edges_mut()
        {
            edge.face = None;
        }
        Ok(self.mesh.faces.remove(&face).unwrap())
    }
}

impl<'a, G> Deref for ImmediateMutation<'a, G>
where
    G: 'a + Geometry,
{
    type Target = Mutation<'a, G>;

    fn deref(&self) -> &Self::Target {
        &self.mutation
    }
}

impl<'a, G> DerefMut for ImmediateMutation<'a, G>
where
    G: 'a + Geometry,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.mutation
    }
}

/// Batch mesh mutations.
///
/// This type provides mutations that are only available in batch mode. Batch
/// mutations defer certain types of integrity errors until a series of
/// mutations are complete. When `commit` is called, the integrity of the mesh
/// is verified before yielding it to the caller.
pub struct BatchMutation<'a, G>
where
    G: 'a + Geometry,
{
    mutation: Mutation<'a, G>,
    non_manifold_vertices: HashMap<VertexKey, HashSet<FaceKey>>,
}

impl<'a, G> BatchMutation<'a, G>
where
    G: 'a + Geometry,
{
    fn new(mutation: Mutation<'a, G>) -> Self {
        BatchMutation {
            mutation: mutation,
            non_manifold_vertices: HashMap::new(),
        }
    }

    pub fn commit(self) -> Result<Mesh<G>, Error> {
        let mesh = self.mutation.mesh.take().unwrap();
        for (vertex, faces) in self.non_manifold_vertices {
            // TODO: This will not detect exactly two faces joined by a single
            //       vertex. This is technically supported, but perhaps should
            //       be rejected.
            // Determine if any unreachable faces exist in the mesh. This
            // cannot happen if the mesh is ultimately a manifold and edge
            // connectivity heals.
            if let Some(vertex) = mesh.vertex(vertex) {
                for unreachable in
                    faces.difference(&vertex.faces().map(|face| face.key()).collect())
                {
                    if mesh.face(*unreachable).is_some() {
                        return Err(GraphError::TopologyMalformed
                            .context("non-manifold connectivity")
                            .into());
                    }
                }
            }
        }
        Ok(mesh)
    }
}

/// Face mutations.
impl<'a, G> BatchMutation<'a, G>
where
    G: 'a + Geometry,
{
    pub fn insert_face(
        &mut self,
        vertices: &[VertexKey],
        geometry: (G::Edge, G::Face),
    ) -> Result<FaceKey, Error> {
        // Before mutating the mesh, collect the incoming and outgoing edges
        // for each vertex.
        let ((incoming, outgoing), singularity) =
            self.mesh.region_connectivity(self.mesh.region(vertices)?);
        // Insert composite edges and collect the interior edges. This is the
        // point of no return; the mesh has been mutated. Unwrap results.
        let edges = vertices
            .perimeter()
            .map(|ab| {
                self.get_or_insert_composite_edge(ab, geometry.0.clone())
                    .unwrap()
                    .0
            })
            .collect::<Vec<_>>();
        let face = self.mesh
            .faces
            .insert_with_generator(Face::new(edges[0], geometry.1));
        if let Some(singularity) = singularity {
            let faces = self.non_manifold_vertices
                .entry(singularity.0)
                .or_insert_with(Default::default);
            for face in singularity.1 {
                faces.insert(face);
            }
            faces.insert(face);
        }
        self.connect_face_interior(&edges, face).unwrap();
        self.connect_face_exterior(&edges, (incoming, outgoing))
            .unwrap();
        Ok(face)
    }
}

impl<'a, G> Deref for BatchMutation<'a, G>
where
    G: 'a + Geometry,
{
    type Target = Mutation<'a, G>;

    fn deref(&self) -> &Self::Target {
        &self.mutation
    }
}

impl<'a, G> DerefMut for BatchMutation<'a, G>
where
    G: 'a + Geometry,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.mutation
    }
}