halfedge 0.2.0

A half-edge mesh data structure library for Rust: traversal, topology operations, geometry, subdivision, decimation, parameterization, geodesics, deformation, boolean operations, and more.
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Half-edge mesh building from vertex/face index arrays + shared helpers.

use std::collections::HashMap;

use crate::Scalar;
use crate::ids::{HalfEdgeId, VertexId};
use crate::storage::{Face, HalfEdge, MeshStorage, Vertex};

use super::MeshBuildError;

// ============================================================
// Shared mesh-building helpers
// ============================================================

/// Establish twin relationships and set boundary half-edge next/prev links.
///
/// Shared logic for [`build_mesh_from_vertices_and_faces`] and [`build_mesh_from_polygons`]:
///
/// 1. Iterate all directed edges, look up reverse edge:
///    - Found: set twins mutually (interior edge);
///    - Not found: create a boundary half-edge (`face = None`), set as twin;
/// 2. Set `next/prev` for each boundary half-edge to form closed boundary loops.
fn link_twins_and_boundary_loops(
    mesh: &mut MeshStorage,
    edge_map: &HashMap<(u32, u32), HalfEdgeId>,
    v_ids: &[VertexId],
) {
    // --- Step 3: establish twin relationships ---
    let directed_edges: Vec<(u32, u32, HalfEdgeId)> =
        edge_map.iter().map(|((a, b), h)| (*a, *b, *h)).collect();

    let mut boundary_twins: Vec<HalfEdgeId> = Vec::new();
    for (a, b, h) in &directed_edges {
        if mesh
            .get_halfedge(*h)
            .expect("halfedge exists in mesh")
            .twin
            .is_some()
        {
            continue;
        }
        if let Some(reverse_h) = edge_map.get(&(*b, *a)) {
            mesh.get_halfedge_mut(*h)
                .expect("halfedge just created or known to exist")
                .twin = Some(*reverse_h);
            mesh.get_halfedge_mut(*reverse_h)
                .expect("halfedge just created or known to exist")
                .twin = Some(*h);
        } else {
            let origin_v = v_ids[*a as usize];
            let twin_id = mesh.add_halfedge(HalfEdge::new(origin_v));
            mesh.get_halfedge_mut(*h)
                .expect("halfedge just created or known to exist")
                .twin = Some(twin_id);
            mesh.get_halfedge_mut(twin_id)
                .expect("halfedge just created or known to exist")
                .twin = Some(*h);
            boundary_twins.push(twin_id);
        }
    }

    // --- Step 4: set boundary half-edge next/prev ---
    set_boundary_next_prev(mesh, &boundary_twins);
}

/// Set boundary half-edge next/prev links to form closed boundary loops.
///
/// Algorithm:
/// - `bh.next`: rotate CCW around `bh.tip` from `bh.twin`, find boundary outgoing;
/// - `bh.prev`: rotate CW around `bh.origin` from `bh`, find twin that is boundary outgoing.
fn set_boundary_next_prev(mesh: &mut MeshStorage, boundary_twins: &[HalfEdgeId]) {
    for bh in boundary_twins {
        // bh.next: rotate CCW around bh.tip, find boundary outgoing
        let mut cur = mesh
            .get_halfedge(*bh)
            .expect("halfedge exists in mesh")
            .twin
            .expect("twin must be set at this point");
        let max_iter = mesh.halfedge_count() + 1;
        let mut next_bh = None;
        for _ in 0..max_iter {
            let prev = match mesh.get_halfedge(cur).and_then(|h| h.prev) {
                Some(p) => p,
                None => break,
            };
            let prev_twin = match mesh.get_halfedge(prev).and_then(|h| h.twin) {
                Some(t) => t,
                None => break,
            };
            if mesh
                .get_halfedge(prev_twin)
                .map(|h| h.face.is_none())
                .unwrap_or(false)
            {
                next_bh = Some(prev_twin);
                break;
            }
            cur = prev_twin;
        }
        if let Some(n) = next_bh {
            mesh.get_halfedge_mut(*bh)
                .expect("halfedge exists in mesh")
                .next = Some(n);
        }

        // bh.prev: rotate CW around bh.origin, find twin that is boundary outgoing
        let mut cur = *bh;
        let mut prev_bh = None;
        for _ in 0..max_iter {
            let twin = match mesh.get_halfedge(cur).and_then(|h| h.twin) {
                Some(t) => t,
                None => break,
            };
            let twin_next = match mesh.get_halfedge(twin).and_then(|h| h.next) {
                Some(n) => n,
                None => break,
            };
            let twin_next_twin = match mesh.get_halfedge(twin_next).and_then(|h| h.twin) {
                Some(t) => t,
                None => break,
            };
            if mesh
                .get_halfedge(twin_next_twin)
                .map(|h| h.face.is_none())
                .unwrap_or(false)
            {
                prev_bh = Some(twin_next_twin);
                break;
            }
            cur = twin_next;
        }
        if let Some(p) = prev_bh {
            mesh.get_halfedge_mut(*bh)
                .expect("halfedge exists in mesh")
                .prev = Some(p);
        }
    }
}

// ============================================================
// Build half-edge mesh from vertices + face indices
// ============================================================

/// Build a complete half-edge mesh from vertex positions and triangle face indices.
///
/// - `vertices`: vertex positions `[[x, y, z], ...]`;
/// - `faces`: triangle indices `[[v0, v1, v2], ...]`, 0-based, CCW winding.
///
/// Automatically builds twin / next / prev / boundary loops / vertex entry / face entry.
///
/// # Errors
/// Returns [`MeshBuildError::IndexOutOfRange`] when a face index is out of bounds.
///
/// ```
/// use halfedge::build_mesh_from_vertices_and_faces;
///
/// let verts = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0]];
/// let faces = vec![[0u32, 1, 2], [0, 2, 3]];
/// let mesh = build_mesh_from_vertices_and_faces(&verts, &faces).unwrap();
/// assert_eq!(mesh.vertex_count(), 4);
/// assert_eq!(mesh.face_count(), 2);
/// ```
pub fn build_mesh_from_vertices_and_faces(
    vertices: &[[Scalar; 3]],
    faces: &[[u32; 3]],
) -> Result<MeshStorage, MeshBuildError> {
    let mut mesh = MeshStorage::new();
    // Pre-allocate: max half-edges = 3 * F (interior) + 3 * F (boundary twins) = 6 * F
    mesh.reserve(vertices.len(), faces.len() * 6, faces.len());

    // 1. Create all vertices
    let v_ids: Vec<VertexId> = vertices
        .iter()
        .map(|p| mesh.add_vertex(Vertex::new(*p)))
        .collect();

    // 2. Create 3 interior half-edges per face
    let mut edge_map: HashMap<(u32, u32), HalfEdgeId> = HashMap::new();
    let n_verts = v_ids.len();
    for face_idx in faces {
        let [i0, i1, i2] = *face_idx;
        // Bounds check: public builder returns Err rather than panic
        if (i0 as usize) >= n_verts || (i1 as usize) >= n_verts || (i2 as usize) >= n_verts {
            let bad_idx = if (i0 as usize) >= n_verts {
                i0
            } else if (i1 as usize) >= n_verts {
                i1
            } else {
                i2
            };
            return Err(MeshBuildError::IndexOutOfRange {
                idx: bad_idx,
                vertex_count: n_verts,
            });
        }
        let v0 = v_ids[i0 as usize];
        let v1 = v_ids[i1 as usize];
        let v2 = v_ids[i2 as usize];

        let h0 = mesh.add_halfedge(HalfEdge::new(v1)); // v0 -> v1
        let h1 = mesh.add_halfedge(HalfEdge::new(v2)); // v1 -> v2
        let h2 = mesh.add_halfedge(HalfEdge::new(v0)); // v2 -> v0

        let f_id = mesh.add_face(Face::new());
        for (he, next, prev) in [(h0, h1, h2), (h1, h2, h0), (h2, h0, h1)] {
            let h = mesh
                .get_halfedge_mut(he)
                .expect("halfedge just created or known to exist");
            h.next = Some(next);
            h.prev = Some(prev);
            h.face = Some(f_id);
        }
        mesh.get_face_mut(f_id).expect("face just created").halfedge = Some(h0);

        edge_map.insert((i0, i1), h0);
        edge_map.insert((i1, i2), h1);
        edge_map.insert((i2, i0), h2);

        // Vertex outgoing entry (if not yet set)
        if mesh
            .get_vertex(v0)
            .expect("vertex exists in mesh")
            .halfedge
            .is_none()
        {
            mesh.get_vertex_mut(v0)
                .expect("vertex exists in mesh")
                .halfedge = Some(h0);
        }
        if mesh
            .get_vertex(v1)
            .expect("vertex exists in mesh")
            .halfedge
            .is_none()
        {
            mesh.get_vertex_mut(v1)
                .expect("vertex exists in mesh")
                .halfedge = Some(h1);
        }
        if mesh
            .get_vertex(v2)
            .expect("vertex exists in mesh")
            .halfedge
            .is_none()
        {
            mesh.get_vertex_mut(v2)
                .expect("vertex exists in mesh")
                .halfedge = Some(h2);
        }
    }

    // 3-4. Establish twins + set boundary half-edge next/prev (shared logic)
    link_twins_and_boundary_loops(&mut mesh, &edge_map, &v_ids);

    Ok(mesh)
}

/// Build a complete half-edge mesh from vertex positions and arbitrary polygon face indices.
///
/// Unlike [`build_mesh_from_vertices_and_faces`], this function accepts polygons of any
/// vertex count (triangles, quads, pentagons, etc.), suitable for Catmull-Clark subdivision
/// and other polygon-input scenarios.
///
/// # Conventions
/// - Each face's vertex indices are arranged CCW (counter-clockwise from outside);
/// - Twins / next / prev / boundary loops are automatically established;
/// - **Note**: output polygon faces do not pass the triangular face check
///   (`FaceNotTriangular`) in [`crate::validate::validate_topology`], but other checks still pass.
///
/// # Errors
/// Returns [`MeshBuildError::IndexOutOfRange`] when a face index is out of bounds.
pub fn build_mesh_from_polygons(
    vertices: &[[Scalar; 3]],
    faces: &[Vec<u32>],
) -> Result<MeshStorage, MeshBuildError> {
    let mut mesh = MeshStorage::new();
    // Pre-allocate: max half-edges = sum(k_i) (interior) + sum(k_i) (boundary twins) = 2 * sum(k_i)
    let total_he: usize = faces
        .iter()
        .map(|f| f.len())
        .sum::<usize>()
        .saturating_mul(2);
    mesh.reserve(vertices.len(), total_he, faces.len());

    // 1. Create all vertices
    let v_ids: Vec<VertexId> = vertices
        .iter()
        .map(|p| mesh.add_vertex(Vertex::new(*p)))
        .collect();

    // 2. Create k interior half-edges per face
    let mut edge_map: HashMap<(u32, u32), HalfEdgeId> = HashMap::new();
    let n_verts = v_ids.len();
    let mut skipped_degenerate: u32 = 0;
    for face_idx in faces {
        let k = face_idx.len();
        if k < 3 {
            skipped_degenerate += 1;
            continue; // degenerate face, skip
        }
        // Bounds check: public builder returns Err rather than panic
        for idx in face_idx {
            if (*idx as usize) >= n_verts {
                return Err(MeshBuildError::IndexOutOfRange {
                    idx: *idx,
                    vertex_count: n_verts,
                });
            }
        }
        // Create k half-edges
        let mut he_ids: Vec<HalfEdgeId> = Vec::with_capacity(k);
        for i in 0..k {
            let v_from = v_ids[face_idx[i] as usize];
            let v_to = v_ids[face_idx[(i + 1) % k] as usize];
            let h = mesh.add_halfedge(HalfEdge::new(v_to)); // v_from -> v_to
            he_ids.push(h);
            // Vertex outgoing entry (if not yet set)
            if mesh
                .get_vertex(v_from)
                .expect("vertex exists in mesh")
                .halfedge
                .is_none()
            {
                mesh.get_vertex_mut(v_from)
                    .expect("vertex exists in mesh")
                    .halfedge = Some(h);
            }
        }
        // Create face and set next/prev/face
        let f_id = mesh.add_face(Face::new());
        for i in 0..k {
            let next = he_ids[(i + 1) % k];
            let prev = he_ids[(i + k - 1) % k];
            let h = mesh
                .get_halfedge_mut(he_ids[i])
                .expect("halfedge just created or known to exist");
            h.next = Some(next);
            h.prev = Some(prev);
            h.face = Some(f_id);
        }
        mesh.get_face_mut(f_id).expect("face just created").halfedge = Some(he_ids[0]);
        // Register directed edges
        for i in 0..k {
            let a = face_idx[i];
            let b = face_idx[(i + 1) % k];
            edge_map.insert((a, b), he_ids[i]);
        }
    }

    // 3-4. Establish twins + set boundary half-edge next/prev (shared logic)
    link_twins_and_boundary_loops(&mut mesh, &edge_map, &v_ids);

    if skipped_degenerate > 0 {
        log::warn!(
            "[halfedge::build_mesh_from_polygons] warning: skipped {skipped_degenerate} degenerate face(s) (vertex count < 3)"
        );
    }

    Ok(mesh)
}

// ============================================================
// Tests
// ============================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::validate::check_topology;

    /// Two triangles forming a quad:
    /// v0-v1-v2 triangle + v0-v2-v3 triangle (CCW, facing +z)
    fn make_quad_data() -> (Vec<[f64; 3]>, Vec<[u32; 3]>) {
        let vertices = vec![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [1.0, 1.0, 0.0],
            [0.0, 1.0, 0.0],
        ];
        let faces = vec![[0, 1, 2], [0, 2, 3]];
        (vertices, faces)
    }

    #[test]
    fn build_mesh_basic_quad() {
        let (verts, faces) = make_quad_data();
        let mesh = build_mesh_from_vertices_and_faces(&verts, &faces).unwrap();
        assert_eq!(mesh.vertex_count(), 4);
        assert_eq!(mesh.face_count(), 2);
        assert_eq!(mesh.halfedge_count(), 10);
    }

    #[test]
    fn build_mesh_passes_full_validation() {
        let (verts, faces) = make_quad_data();
        let mesh = build_mesh_from_vertices_and_faces(&verts, &faces).unwrap();
        assert!(
            check_topology(&mesh).is_ok(),
            "built mesh should pass full validation: {:?}",
            check_topology(&mesh)
        );
    }

    #[test]
    fn build_mesh_closed_tetrahedron() {
        let vertices = vec![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            [0.0, 0.0, 1.0],
        ];
        let faces = vec![[0, 1, 2], [0, 2, 3], [0, 3, 1], [1, 3, 2]];
        let mesh = build_mesh_from_vertices_and_faces(&vertices, &faces).unwrap();
        assert_eq!(mesh.vertex_count(), 4);
        assert_eq!(mesh.face_count(), 4);
        assert_eq!(mesh.halfedge_count(), 12);
        assert!(check_topology(&mesh).is_ok());
    }

    #[test]
    fn build_mesh_empty_inputs_returns_empty_mesh() {
        let mesh = build_mesh_from_vertices_and_faces(&[], &[]).unwrap();
        assert_eq!(mesh.vertex_count(), 0);
        assert_eq!(mesh.face_count(), 0);
    }

    #[test]
    fn build_mesh_vertices_no_faces() {
        let vertices = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let mesh = build_mesh_from_vertices_and_faces(&vertices, &[]).unwrap();
        assert_eq!(mesh.vertex_count(), 3);
        assert_eq!(mesh.face_count(), 0);
    }

    #[test]
    fn build_polygons_empty_inputs_returns_empty_mesh() {
        let mesh = build_mesh_from_polygons(&[], &[]).unwrap();
        assert_eq!(mesh.vertex_count(), 0);
        assert_eq!(mesh.face_count(), 0);
    }

    #[test]
    fn build_mesh_face_index_out_of_range_returns_err() {
        let vertices = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let faces = [[0u32, 1, 5]];
        let result = build_mesh_from_vertices_and_faces(&vertices, &faces);
        assert!(result.is_err());
        match result {
            Err(MeshBuildError::IndexOutOfRange { idx, vertex_count }) => {
                assert_eq!(idx, 5);
                assert_eq!(vertex_count, 3);
            }
            _ => panic!("expected IndexOutOfRange error"),
        }
    }

    #[test]
    fn build_polygons_face_index_out_of_range_returns_err() {
        let vertices = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let faces = [vec![0u32, 1, 5]];
        let result = build_mesh_from_polygons(&vertices, &faces);
        assert!(result.is_err());
        match result {
            Err(MeshBuildError::IndexOutOfRange { idx, vertex_count }) => {
                assert_eq!(idx, 5);
                assert_eq!(vertex_count, 3);
            }
            _ => panic!("expected IndexOutOfRange error"),
        }
    }

    #[test]
    fn build_polygons_skips_degenerate_face_2_verts() {
        let vertices = [
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            [0.0, 0.0, 1.0],
        ];
        let faces = [vec![0u32, 1]];
        let mesh = build_mesh_from_polygons(&vertices, &faces).unwrap();
        assert_eq!(mesh.vertex_count(), 4);
        assert_eq!(mesh.face_count(), 0);
    }

    #[test]
    fn build_polygons_mixed_degenerate_and_valid() {
        let vertices = [
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            [0.0, 0.0, 1.0],
        ];
        let faces = [vec![], vec![0u32, 1, 2]];
        let mesh = build_mesh_from_polygons(&vertices, &faces).unwrap();
        assert_eq!(mesh.vertex_count(), 4);
        assert_eq!(mesh.face_count(), 1);
    }
}