mesh-graph 0.6.0

Fast halfedge triangle mesh graph in pure Rust
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
use glam::Vec3;
use tracing::{error, instrument};

use crate::{Face, FaceId, HalfedgeId, MeshGraph, VertexId, error_none};

impl MeshGraph {
    #[instrument(skip(self))]
    pub fn add_face_from_positions(&mut self, a: Vec3, b: Vec3, c: Vec3) -> AddFace {
        let a_id = self.add_vertex(a);
        let b_id = self.add_vertex(b);
        let c_id = self.add_vertex(c);

        let inserted_a = self.add_or_get_edge(a_id, b_id);
        let inserted_b = self.add_or_get_edge(b_id, c_id);
        let inserted_c = self.add_or_get_edge(c_id, a_id);

        let face_id = self.add_face(
            inserted_a.start_to_end_he_id,
            inserted_b.start_to_end_he_id,
            inserted_c.start_to_end_he_id,
        );

        let mut halfedge_ids = inserted_a.created_he_ids();
        halfedge_ids.extend(inserted_b.created_he_ids());
        halfedge_ids.extend(inserted_c.created_he_ids());

        AddFace {
            face_id,
            halfedge_ids,
            vertex_ids: vec![a_id, b_id, c_id],
        }
    }

    /// Returns `None` when an edge already has two faces
    #[instrument(skip(self))]
    pub fn add_face_from_halfedge_and_position(
        &mut self,
        he_id: HalfedgeId,
        opposite_vertex_pos: Vec3,
    ) -> Option<AddFace> {
        let vertex_id = self.add_vertex(opposite_vertex_pos);
        self.add_face_from_halfedge_and_vertex(he_id, vertex_id)
    }

    /// Returns `None` when the edge described by `he_id` already has two faces
    #[instrument(skip(self))]
    pub fn add_face_from_halfedge_and_vertex(
        &mut self,
        he_id: HalfedgeId,
        vertex_id: VertexId,
    ) -> Option<AddFace> {
        let he_a_id = self.boundary_he(he_id)?;

        let he_a = self.halfedges[he_a_id];
        let start_vertex_id_he_a = he_a.start_vertex(self)?;
        let end_vertex_id_he_a = he_a.end_vertex;

        let inserted_b = self.add_or_get_edge(end_vertex_id_he_a, vertex_id);
        let inserted_c = self.add_or_get_edge(vertex_id, start_vertex_id_he_a);

        let face_id = self.add_face(
            he_a_id,
            inserted_b.start_to_end_he_id,
            inserted_c.start_to_end_he_id,
        );

        let mut halfedge_ids = inserted_b.created_he_ids();
        halfedge_ids.extend(inserted_c.created_he_ids());

        Some(AddFace {
            face_id,
            halfedge_ids,
            vertex_ids: vec![],
        })
    }

    /// Creates a face from three vertices.
    #[instrument(skip(self))]
    pub fn add_face_from_vertices(
        &mut self,
        v_id1: VertexId,
        v_id2: VertexId,
        v_id3: VertexId,
    ) -> AddFace {
        let inserted_a = self.add_or_get_edge(v_id1, v_id2);
        let inserted_b = self.add_or_get_edge(v_id2, v_id3);
        let inserted_c = self.add_or_get_edge(v_id3, v_id1);

        let face_id = self.add_face(
            inserted_a.start_to_end_he_id,
            inserted_b.start_to_end_he_id,
            inserted_c.start_to_end_he_id,
        );

        let mut halfedge_ids = inserted_a.created_he_ids();
        halfedge_ids.extend(inserted_b.created_he_ids());
        halfedge_ids.extend(inserted_c.created_he_ids());

        AddFace {
            face_id,
            halfedge_ids,
            vertex_ids: vec![],
        }
    }

    /// Creates a face from two halfedges.
    #[instrument(skip(self))]
    pub fn add_face_from_halfedges(
        &mut self,
        he_id1: HalfedgeId,
        he_id2: HalfedgeId,
    ) -> Option<AddFace> {
        let he_id1 = self.boundary_he(he_id1)?;
        let he_id2 = self.boundary_he(he_id2)?;

        let he1 = *self
            .halfedges
            .get(he_id1)
            .or_else(error_none!("Halfedge 1 not found"))?;
        let he2 = *self
            .halfedges
            .get(he_id2)
            .or_else(error_none!("Halfedge 2 not found"))?;

        let he1_start_vertex = he1
            .start_vertex(self)
            .or_else(error_none!("Start vertex should be available"))?;

        let he2_start_vertex = he2
            .start_vertex(self)
            .or_else(error_none!("Start vertex should be available"))?;

        if he1_start_vertex == he2.end_vertex {
            let inserted = self.add_or_get_edge(he1.end_vertex, he2_start_vertex);

            let face_id = self.add_face(inserted.start_to_end_he_id, he_id2, he_id1);

            Some(AddFace {
                face_id,
                halfedge_ids: inserted.created_he_ids(),
                vertex_ids: vec![],
            })
        } else if he1_start_vertex == he2_start_vertex {
            let inserted = self.add_or_get_edge(he1.end_vertex, he2.end_vertex);

            let face_id = self.add_face(
                inserted.start_to_end_he_id,
                he2.twin.or_else(error_none!("Twin not set"))?,
                he_id1,
            );

            Some(AddFace {
                face_id,
                halfedge_ids: inserted.created_he_ids(),
                vertex_ids: vec![],
            })
        } else if he1.end_vertex == he2.end_vertex {
            let inserted = self.add_or_get_edge(he2_start_vertex, he1_start_vertex);

            let face_id = self.add_face(
                inserted.start_to_end_he_id,
                he_id1,
                he2.twin.or_else(error_none!("Twin not set"))?,
            );

            Some(AddFace {
                face_id,
                halfedge_ids: inserted.created_he_ids(),
                vertex_ids: vec![],
            })
        } else {
            let inserted = self.add_or_get_edge(he2.end_vertex, he1_start_vertex);

            let face_id = self.add_face(inserted.start_to_end_he_id, he_id1, he_id2);

            Some(AddFace {
                face_id,
                halfedge_ids: inserted.created_he_ids(),
                vertex_ids: vec![],
            })
        }
    }

    /// Inserts a face into the mesh graph. It connects the halfedges to the face and the face to the first halfedge.
    /// Additionally it connects the halfedges' `next` loop around the face.
    #[instrument(skip(self))]
    pub fn add_face(
        &mut self,
        he1_id: HalfedgeId,
        he2_id: HalfedgeId,
        he3_id: HalfedgeId,
    ) -> FaceId {
        let face_id = self.faces.insert_with_key(|id| Face {
            halfedge: he1_id,
            index: self.next_index,
            id,
        });

        self.index_to_face_id.insert(self.next_index, face_id);

        self.next_index += 1;

        for (he_id, next_he_id) in [(he1_id, he2_id), (he2_id, he3_id), (he3_id, he1_id)] {
            if let Some(halfedge) = self.halfedges.get_mut(he_id) {
                halfedge.face = Some(face_id);
                halfedge.next = Some(next_he_id);
            } else {
                error!("Halfedge not found");
            }
        }

        let face = self.faces[face_id]; // just inserted above
        self.bvh
            .insert_or_update_partially(face.aabb(self), face.index, 0.0);

        face_id
    }
}

/// Return value of several `add_face...` methods
pub struct AddFace {
    /// Id of the created face
    pub face_id: FaceId,
    /// During the process of creating the face all new created halfedges
    pub halfedge_ids: Vec<HalfedgeId>,
    /// During the process of creating the face all new created vertices
    pub vertex_ids: Vec<VertexId>,
}

#[cfg(test)]
mod test {
    use super::*;

    fn add_face(mesh_graph: &mut MeshGraph) -> FaceId {
        mesh_graph
            .add_face_from_positions(
                Vec3::new(0.0, 0.0, 0.0),
                Vec3::new(1.0, 0.0, 0.0),
                Vec3::new(0.0, 1.0, 0.0),
            )
            .face_id
    }

    fn add_face_to_edge(
        mesh_graph: &mut MeshGraph,
        face_id: FaceId,
        pos: Vec3,
        use_twin: bool,
    ) -> Option<FaceId> {
        let mut associated_he_id = mesh_graph.faces[face_id]
            .halfedges(mesh_graph)
            .nth(1)
            .unwrap();

        if use_twin {
            associated_he_id = mesh_graph.halfedges[associated_he_id].twin.unwrap();
        }

        mesh_graph
            .add_face_from_halfedge_and_position(associated_he_id, pos)
            .map(|f| f.face_id)
    }

    fn fill_face(
        mesh_graph: &mut MeshGraph,
        face_id_1: FaceId,
        face_id_2: FaceId,
        use_twin: bool,
    ) -> Option<FaceId> {
        let mut he_id_1 = mesh_graph.faces[face_id_1]
            .halfedges(mesh_graph)
            .next()
            .unwrap();

        let mut he_id_2 = mesh_graph.faces[face_id_2]
            .halfedges(mesh_graph)
            .nth(1)
            .unwrap();

        if use_twin {
            he_id_1 = mesh_graph.halfedges[he_id_1].twin.unwrap();
            he_id_2 = mesh_graph.halfedges[he_id_2].twin.unwrap();
        }

        mesh_graph
            .add_face_from_halfedges(he_id_1, he_id_2)
            .map(|f| f.face_id)
    }

    macro_rules! init_fill_face {
        ($f1:ident, $f2:ident, $f3:ident, $mg:ident) => {
            let $f1 = add_face(&mut $mg);

            let $f2 = add_face_to_edge(&mut $mg, $f1, Vec3::new(1.0, 1.0, 0.0), false);
            let $f3 = add_face_to_edge(&mut $mg, $f2.unwrap(), Vec3::new(0.5, 0.5, 1.0), false);
        };
    }

    macro_rules! log_faces_rerun {
        ($mg:ident, $($face:expr),*) => {
            #[cfg(feature = "rerun")]
            {
                $(
                    $mg.log_face_rerun(&format!("{:?}", $face), $face);
                )*
            }
        };
    }

    fn test_vertices(mesh_graph: &MeshGraph, face: FaceId) {
        let mut vertices = mesh_graph.faces[face].vertices(mesh_graph);

        let vertex = vertices.next();
        assert!(vertex.is_some());
        assert!(
            mesh_graph.vertices[vertex.unwrap()]
                .outgoing_halfedge
                .is_some()
        );

        let vertex = vertices.next();
        assert!(vertex.is_some());
        assert!(
            mesh_graph.vertices[vertex.unwrap()]
                .outgoing_halfedge
                .is_some()
        );

        let vertex = vertices.next();
        assert!(vertex.is_some());
        assert!(
            mesh_graph.vertices[vertex.unwrap()]
                .outgoing_halfedge
                .is_some()
        );

        assert!(vertices.next().is_none());
    }

    fn test_halfedges(mesh_graph: &MeshGraph, face: FaceId) {
        let mut halfedges = mesh_graph.faces[face].halfedges(mesh_graph);

        assert!(halfedges.next().is_some());
        assert!(halfedges.next().is_some());
        assert!(halfedges.next().is_some());
        assert!(halfedges.next().is_none());
    }

    #[test]
    fn test_create_face() {
        let mut mesh_graph = MeshGraph::new();
        let face1 = add_face(&mut mesh_graph);

        assert_eq!(mesh_graph.vertices.len(), 3);
        assert_eq!(mesh_graph.positions.len(), 3);
        assert_eq!(mesh_graph.halfedges.len(), 6);
        assert_eq!(mesh_graph.faces.len(), 1);

        test_vertices(&mesh_graph, face1);
        test_halfedges(&mesh_graph, face1);

        log_faces_rerun!(mesh_graph, face1);
    }

    #[test]
    fn test_add_face_to_edge() {
        let mut mesh_graph = MeshGraph::new();
        let face1 = add_face(&mut mesh_graph);

        let face2 = add_face_to_edge(&mut mesh_graph, face1, Vec3::new(1.0, 1.0, 0.0), false);

        assert!(face2.is_some());
        assert_eq!(mesh_graph.vertices.len(), 4);
        assert_eq!(mesh_graph.positions.len(), 4);
        assert_eq!(mesh_graph.halfedges.len(), 10);
        assert_eq!(mesh_graph.faces.len(), 2);

        test_vertices(&mesh_graph, face2.unwrap());
        test_halfedges(&mesh_graph, face2.unwrap());

        log_faces_rerun!(mesh_graph, face1, face2.unwrap());
    }

    #[test]
    fn test_add_face_to_twin_edge() {
        let mut mesh_graph = MeshGraph::new();
        let face1 = add_face(&mut mesh_graph);

        let face2 = add_face_to_edge(&mut mesh_graph, face1, Vec3::new(1.0, 1.0, 0.0), true);

        assert!(face2.is_some());
        assert_eq!(mesh_graph.vertices.len(), 4);
        assert_eq!(mesh_graph.positions.len(), 4);
        assert_eq!(mesh_graph.halfedges.len(), 10);
        assert_eq!(mesh_graph.faces.len(), 2);

        test_vertices(&mesh_graph, face2.unwrap());
        test_halfedges(&mesh_graph, face2.unwrap());

        log_faces_rerun!(mesh_graph, face1, face2.unwrap());
    }

    #[test]
    fn test_fill_face() {
        let mut mesh_graph = MeshGraph::new();
        init_fill_face!(face1, face2, face3, mesh_graph);

        let face4 = fill_face(&mut mesh_graph, face1, face3.unwrap(), false);

        assert!(face4.is_some());
        assert_eq!(mesh_graph.vertices.len(), 5);
        assert_eq!(mesh_graph.positions.len(), 5);
        assert_eq!(mesh_graph.halfedges.len(), 16);
        assert_eq!(mesh_graph.faces.len(), 4);

        test_vertices(&mesh_graph, face4.unwrap());
        test_halfedges(&mesh_graph, face4.unwrap());

        log_faces_rerun!(
            mesh_graph,
            face1,
            face2.unwrap(),
            face3.unwrap(),
            face4.unwrap()
        );
    }

    #[test]
    fn test_fill_face_from_twin_edges() {
        let mut mesh_graph = MeshGraph::new();
        init_fill_face!(face1, face2, face3, mesh_graph);

        let face4 = fill_face(&mut mesh_graph, face1, face3.unwrap(), true);

        assert!(face4.is_some());
        assert_eq!(mesh_graph.vertices.len(), 5);
        assert_eq!(mesh_graph.positions.len(), 5);
        assert_eq!(mesh_graph.halfedges.len(), 16);
        assert_eq!(mesh_graph.faces.len(), 4);

        test_vertices(&mesh_graph, face4.unwrap());
        test_halfedges(&mesh_graph, face4.unwrap());

        log_faces_rerun!(
            mesh_graph,
            face1,
            face2.unwrap(),
            face3.unwrap(),
            face4.unwrap()
        );
    }
}