axiolid-reference 0.3.11

Portable scalar reference implementation and certified predicates
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Assembly of cut segments into plane-local polylines.
//!
//! `assemble_contours` is the legacy path: every node has degree two and every
//! polyline is a closed contour. `assemble_chains` serves requests (ADR 0091):
//! it also yields open chains that end on an in-plane region, and refuses a
//! vertex where more than two cut segments meet.

use std::collections::{BTreeMap, BTreeSet};

use axiolid_contracts::{ExecutionOptions, GeomError, GeomResult, Operation, Sign};
use axiolid_core::{Frame3, Point2};
use axiolid_mesh::TriMesh;
use axiolid_mesh_section_contract::{
    ChainEnd, SectionChain, SectionContour, SectionLimits, REFUSED_SADDLE_VERTEX,
};

use super::regions::{canonical_rotation, corners, strictly_between, Projection};
use super::{
    internal_topology_error, node_point, point_order, project_local, sequence_order, signed_area,
    simplify_collinear, Classification, EdgeKey, NodeKey, ScalarSection, Segment,
};

pub(super) fn assemble_contours(
    mesh: &TriMesh,
    frame: Frame3,
    classifications: &[Classification],
    segments: &BTreeSet<Segment>,
    limits: SectionLimits,
    options: &ExecutionOptions,
) -> GeomResult<Vec<SectionContour>> {
    let mut adjacency = BTreeMap::<NodeKey, Vec<NodeKey>>::new();
    for &Segment(left, right) in segments {
        adjacency.entry(left).or_default().push(right);
        adjacency.entry(right).or_default().push(left);
    }
    for neighbours in adjacency.values_mut() {
        neighbours.sort_unstable();
        if neighbours.len() != 2 {
            return Err(GeomError::NotManifold(format!(
                "section graph has degree {}, expected 2",
                neighbours.len()
            )));
        }
    }

    let mut visited = BTreeSet::<Segment>::new();
    let mut contours = Vec::new();
    for &start in adjacency.keys() {
        let start_is_complete = adjacency[&start].iter().try_fold(true, |complete, &next| {
            let segment = Segment::new(start, next)?;
            Ok::<bool, GeomError>(complete && visited.contains(&segment))
        })?;
        if start_is_complete {
            continue;
        }
        options.check_cancelled()?;
        if contours.len() >= limits.max_contours {
            return Err(GeomError::BudgetExceeded {
                resource: "section contours",
            });
        }
        let mut nodes = Vec::new();
        let mut previous = None;
        let mut current = start;
        loop {
            if nodes.len() >= limits.max_output_vertices {
                return Err(GeomError::BudgetExceeded {
                    resource: "section output vertices",
                });
            }
            nodes.push(current);
            let neighbours = adjacency
                .get(&current)
                .ok_or_else(internal_topology_error)?;
            let next = match previous {
                None => neighbours[0],
                Some(previous) if neighbours[0] == previous => neighbours[1],
                Some(_) => neighbours[0],
            };
            let edge = Segment::new(current, next)?;
            if !visited.insert(edge) && next != start {
                return Err(GeomError::NotManifold(
                    "section graph revisited an edge before closing a contour".into(),
                ));
            }
            previous = Some(current);
            current = next;
            if current == start {
                break;
            }
            if nodes.len() > segments.len() {
                return Err(internal_topology_error());
            }
        }
        if nodes.len() < 3 {
            return Err(GeomError::Degenerate(
                "section contour has fewer than three source-topology nodes".into(),
            ));
        }
        let mut points = Vec::new();
        points
            .try_reserve_exact(nodes.len())
            .map_err(|_| GeomError::BudgetExceeded { resource: "memory" })?;
        for node in nodes {
            let world = node_point(mesh, classifications, node)?;
            let local = world - frame.origin;
            let point = Point2::new(local.dot(frame.x), local.dot(frame.y));
            if !point.is_finite() {
                return Err(GeomError::Degenerate(
                    "section projection exceeded finite arithmetic".into(),
                ));
            }
            points.push(point);
        }
        simplify_collinear(&mut points, options.tolerance().linear());
        if points.len() < 3 {
            return Err(GeomError::Degenerate(
                "section contour collapsed below three vertices".into(),
            ));
        }
        let area = signed_area(&points);
        if !area.is_finite() || area == 0.0 {
            return Err(GeomError::Degenerate(
                "section contour has no finite signed area".into(),
            ));
        }
        if area < 0.0 {
            points[1..].reverse();
        }
        contours.push(SectionContour::new(points));
    }
    contours.sort_by(|left, right| point_order(&left.points[0], &right.points[0]));
    Ok(contours)
}

/// What chain assembly needs besides the segments.
pub(super) struct ChainContext<'a> {
    pub(super) mesh: &'a TriMesh,
    pub(super) frame: Frame3,
    pub(super) classifications: &'a [Classification],
    pub(super) projection: Projection,
    /// Per segment: whether the cut runs from its first node to its second,
    /// with the solid on the left (counter-clockwise in the frame). Decided
    /// from the exact vertex signs of the segment's triangle.
    pub(super) directions: &'a BTreeMap<Segment, bool>,
    /// Crossing edges whose two triangles are exactly coplanar: the cut points
    /// on either side of such an edge's node are collinear with it.
    pub(super) coplanar_edges: &'a BTreeSet<EdgeKey>,
    /// Vertices of in-plane triangles.
    pub(super) region_vertices: &'a BTreeSet<u32>,
}

/// Open and closed chains of the cut segments of a request (ADR 0091).
///
/// A vertex node of degree one ends an open chain; it must be a vertex of an
/// in-plane triangle, where the chain meets a region boundary. A vertex node
/// of degree above two is a saddle and is refused by name.
///
/// Simplification and orientation are exact, decided on source data. A closed
/// chain is made counter-clockwise by the segment directions. A node is
/// dropped only when its two cut segments are exactly collinear: a vertex node
/// between two vertex nodes with a zero `orient2d`, or an edge node whose two
/// triangles are exactly coplanar (`orient3d`). Vertices of in-plane triangles
/// are never dropped, so a chain end stays bit-identical to the region vertex
/// it meets, and the ends of an open chain are never dropped.
pub(super) fn assemble_chains(
    ctx: &ChainContext<'_>,
    segments: &BTreeSet<Segment>,
    limits: SectionLimits,
    options: &ExecutionOptions,
) -> GeomResult<Vec<SectionChain>> {
    let (mesh, frame, classifications) = (ctx.mesh, ctx.frame, ctx.classifications);
    let region_vertices = ctx.region_vertices;
    let mut adjacency = BTreeMap::<NodeKey, Vec<NodeKey>>::new();
    for &Segment(left, right) in segments {
        adjacency.entry(left).or_default().push(right);
        adjacency.entry(right).or_default().push(left);
    }
    for (node, neighbours) in &mut adjacency {
        neighbours.sort_unstable();
        match (node, neighbours.len()) {
            (_, 1) | (_, 2) => {}
            (NodeKey::Vertex(_), _) => {
                return Err(GeomError::UnsupportedInput {
                    backend: ScalarSection::ID,
                    operation: Operation::MeshPlaneSection,
                    input: REFUSED_SADDLE_VERTEX,
                })
            }
            (NodeKey::Edge(_), _) => return Err(internal_topology_error()),
        }
    }

    let is_region_vertex = |node: NodeKey| matches!(node, NodeKey::Vertex(vertex) if region_vertices.contains(&vertex));
    let mut visited = BTreeSet::<Segment>::new();
    let mut chains = Vec::new();
    let mut chain_vertices = 0usize;

    // Open chains first, from their degree-one ends in sorted order.
    for (&start, neighbours) in &adjacency {
        if neighbours.len() != 1 || visited.contains(&Segment::new(start, neighbours[0])?) {
            continue;
        }
        options.check_cancelled()?;
        let mut nodes = vec![start];
        let mut previous = start;
        let mut current = neighbours[0];
        loop {
            visited.insert(Segment::new(previous, current)?);
            nodes.push(current);
            let onward = &adjacency[&current];
            if onward.len() == 1 {
                break;
            }
            let next = if onward[0] == previous {
                onward[1]
            } else {
                onward[0]
            };
            previous = current;
            current = next;
            if nodes.len() > segments.len() + 1 {
                return Err(internal_topology_error());
            }
        }
        // Landing 1: an open chain can only end on an in-plane region.
        if !(is_region_vertex(start) && is_region_vertex(current)) {
            return Err(internal_topology_error());
        }
        let nodes = simplify_nodes(ctx, nodes, false)?;
        let mut points = chain_points(mesh, frame, classifications, &nodes)?;
        if points.len() < 2 || has_adjacent_duplicate(&points, false) {
            return Err(GeomError::Degenerate(
                "section chain collapsed under projection".into(),
            ));
        }
        match point_order(&points[0], &points[points.len() - 1]) {
            std::cmp::Ordering::Equal => {
                return Err(GeomError::Degenerate(
                    "open section chain starts and ends at one point".into(),
                ))
            }
            std::cmp::Ordering::Greater => points.reverse(),
            std::cmp::Ordering::Less => {}
        }
        push_chain(
            &mut chains,
            &mut chain_vertices,
            SectionChain::open(points, ChainEnd::InPlaneRegion, ChainEnd::InPlaneRegion),
            limits,
        )?;
    }

    // What remains is closed: every node has degree two.
    for &start in adjacency.keys() {
        let complete = adjacency[&start].iter().try_fold(true, |complete, &next| {
            Ok::<bool, GeomError>(complete && visited.contains(&Segment::new(start, next)?))
        })?;
        if complete {
            continue;
        }
        options.check_cancelled()?;
        let mut nodes = Vec::new();
        let mut previous = None;
        let mut current = start;
        loop {
            nodes.push(current);
            let neighbours = &adjacency[&current];
            let next = match previous {
                None => neighbours[0],
                Some(previous) if neighbours[0] == previous => neighbours[1],
                Some(_) => neighbours[0],
            };
            visited.insert(Segment::new(current, next)?);
            previous = Some(current);
            current = next;
            if current == start {
                break;
            }
            if nodes.len() > segments.len() {
                return Err(internal_topology_error());
            }
        }
        if nodes.len() < 3 {
            return Err(GeomError::Degenerate(
                "section contour has fewer than three source-topology nodes".into(),
            ));
        }
        orient_nodes(ctx, &mut nodes)?;
        let nodes = simplify_nodes(ctx, nodes, true)?;
        if nodes.len() < 3 {
            return Err(GeomError::Degenerate(
                "section contour collapsed below three vertices".into(),
            ));
        }
        let points = chain_points(mesh, frame, classifications, &nodes)?;
        if has_adjacent_duplicate(&points, true) {
            return Err(GeomError::Degenerate(
                "section chain collapsed under projection".into(),
            ));
        }
        push_chain(
            &mut chains,
            &mut chain_vertices,
            SectionChain::closed(canonical_rotation(points)),
            limits,
        )?;
    }

    chains.sort_by(|left, right| {
        sequence_order(&left.points, &right.points)
            .then_with(|| right.is_closed().cmp(&left.is_closed()))
    });
    Ok(chains)
}

fn push_chain(
    chains: &mut Vec<SectionChain>,
    chain_vertices: &mut usize,
    chain: SectionChain,
    limits: SectionLimits,
) -> GeomResult<()> {
    if chains.len() >= limits.max_contours {
        return Err(GeomError::BudgetExceeded {
            resource: "section contours",
        });
    }
    *chain_vertices += chain.points.len();
    if *chain_vertices > limits.max_output_vertices {
        return Err(GeomError::BudgetExceeded {
            resource: "section output vertices",
        });
    }
    chains.push(chain);
    Ok(())
}

fn chain_points(
    mesh: &TriMesh,
    frame: Frame3,
    classifications: &[Classification],
    nodes: &[NodeKey],
) -> GeomResult<Vec<Point2>> {
    nodes
        .iter()
        .map(|&node| project_local(frame, node_point(mesh, classifications, node)?))
        .collect()
}

fn has_adjacent_duplicate(points: &[Point2], ring: bool) -> bool {
    let pairs = if ring {
        points.len()
    } else {
        points.len().saturating_sub(1)
    };
    (0..pairs).any(|index| points[index] == points[(index + 1) % points.len()])
}

/// Make a closed chain run counter-clockwise by its segment directions; the
/// directions of all its segments must agree.
fn orient_nodes(ctx: &ChainContext<'_>, nodes: &mut [NodeKey]) -> GeomResult<()> {
    let mut forward = None;
    for index in 0..nodes.len() {
        let (from, to) = (nodes[index], nodes[(index + 1) % nodes.len()]);
        let segment = Segment::new(from, to)?;
        let runs_forward = ctx
            .directions
            .get(&segment)
            .map(|&first_to_second| first_to_second == (from == segment.0))
            .ok_or_else(internal_topology_error)?;
        match forward {
            None => forward = Some(runs_forward),
            Some(known) if known != runs_forward => {
                return Err(GeomError::NotManifold(
                    "section chain segments disagree on their direction".into(),
                ))
            }
            Some(_) => {}
        }
    }
    if forward == Some(false) {
        nodes.reverse();
    }
    Ok(())
}

/// Whether the cut segments on both sides of a node are exactly collinear,
/// so the node can go without moving the polyline.
fn is_droppable(
    ctx: &ChainContext<'_>,
    previous: NodeKey,
    node: NodeKey,
    next: NodeKey,
) -> GeomResult<bool> {
    match node {
        NodeKey::Edge(edge) => Ok(ctx.coplanar_edges.contains(&edge)),
        NodeKey::Vertex(vertex) => {
            if ctx.region_vertices.contains(&vertex) {
                return Ok(false);
            }
            let (NodeKey::Vertex(before), NodeKey::Vertex(after)) = (previous, next) else {
                return Ok(false);
            };
            let [a, b, c] = corners(ctx.mesh, [before, vertex, after])?;
            Ok(ctx.projection.orient(a, b, c)? == Sign::Zero && strictly_between(a, b, c))
        }
    }
}

/// Drop every node of a chain whose two cut segments are exactly collinear.
/// Collinearity is judged on the original neighbours: a dropped neighbour lies
/// on the same line, so one pass suffices. An open chain keeps its ends.
fn simplify_nodes(
    ctx: &ChainContext<'_>,
    nodes: Vec<NodeKey>,
    ring: bool,
) -> GeomResult<Vec<NodeKey>> {
    let last = nodes.len() - 1;
    let mut kept = Vec::with_capacity(nodes.len());
    for (index, &node) in nodes.iter().enumerate() {
        if !ring && (index == 0 || index == last) {
            kept.push(node);
            continue;
        }
        let previous = nodes[(index + nodes.len() - 1) % nodes.len()];
        let next = nodes[(index + 1) % nodes.len()];
        if !is_droppable(ctx, previous, node, next)? {
            kept.push(node);
        }
    }
    Ok(kept)
}

#[cfg(test)]
mod tests {
    use axiolid_core::{Point3, Tolerance, Vec3};

    use super::super::ExactSectionPlane;
    use super::*;

    /// Source vertices on the plane z = 0, all cut-segment nodes:
    /// 0 = (0,0), 1 = (1,0), 2 = (2,0), 3 = (2,1), 4 = (0,1), 5 = (-1,0).
    /// Vertex 1 lies exactly between 0 and 2, on the line y = 0.
    struct Fixture {
        mesh: TriMesh,
        frame: Frame3,
        projection: Projection,
    }

    fn fixture() -> Fixture {
        let positions = [
            (0.0, 0.0),
            (1.0, 0.0),
            (2.0, 0.0),
            (2.0, 1.0),
            (0.0, 1.0),
            (-1.0, 0.0),
        ]
        .map(|(x, y)| Point3::new(x, y, 0.0))
        .to_vec();
        let frame = Frame3 {
            origin: Point3::ZERO,
            x: Vec3::X,
            y: Vec3::Y,
            z: Vec3::Z,
        };
        let plane = ExactSectionPlane::new(frame).expect("plane");
        Fixture {
            mesh: TriMesh::new(positions, vec![]),
            frame,
            projection: Projection::new(&plane).expect("projection"),
        }
    }

    fn vertex(index: u32) -> NodeKey {
        NodeKey::Vertex(index)
    }

    /// Chains of the given vertex path, run in the given direction.
    fn chains(path: &[u32], closed: bool, region_vertices: &[u32]) -> Vec<SectionChain> {
        let fixture = fixture();
        let region_vertices: BTreeSet<u32> = region_vertices.iter().copied().collect();
        let mut segments = BTreeSet::new();
        let mut directions = BTreeMap::new();
        let count = if closed { path.len() } else { path.len() - 1 };
        for index in 0..count {
            let (from, to) = (vertex(path[index]), vertex(path[(index + 1) % path.len()]));
            let segment = Segment::new(from, to).expect("segment");
            directions.insert(segment, from == segment.0);
            segments.insert(segment);
        }
        let coplanar_edges = BTreeSet::new();
        let ctx = ChainContext {
            mesh: &fixture.mesh,
            frame: fixture.frame,
            classifications: &[],
            projection: fixture.projection,
            directions: &directions,
            coplanar_edges: &coplanar_edges,
            region_vertices: &region_vertices,
        };
        let options = ExecutionOptions::new(Tolerance::METRE);
        assemble_chains(
            &ctx,
            &segments,
            SectionLimits::new(100, 100, 100, 10),
            &options,
        )
        .expect("chains")
    }

    fn points(chain: &SectionChain) -> Vec<(f64, f64)> {
        chain.points.iter().map(|p| (p.x, p.y)).collect()
    }

    #[test]
    fn a_closed_chain_drops_an_exactly_collinear_vertex_unless_it_is_protected() {
        // 0 -> 1 -> 2 -> 3 -> 4 is counter-clockwise; vertex 1 is collinear.
        let path = [0, 1, 2, 3, 4];
        let plain = chains(&path, true, &[]);
        assert_eq!(
            points(&plain[0]),
            [(0.0, 0.0), (2.0, 0.0), (2.0, 1.0), (0.0, 1.0)]
        );
        let kept = chains(&path, true, &[1]);
        assert_eq!(
            points(&kept[0]),
            [(0.0, 0.0), (1.0, 0.0), (2.0, 0.0), (2.0, 1.0), (0.0, 1.0)]
        );
    }

    #[test]
    fn an_open_chain_keeps_its_ends_and_a_protected_vertex_it_passes_through() {
        // The ends are region vertices; the middle one is collinear.
        let plain = chains(&[0, 1, 2], false, &[0, 2]);
        assert_eq!(points(&plain[0]), [(0.0, 0.0), (2.0, 0.0)]);
        let through = chains(&[0, 1, 2], false, &[0, 1, 2]);
        assert_eq!(points(&through[0]), [(0.0, 0.0), (1.0, 0.0), (2.0, 0.0)]);
    }

    #[test]
    fn the_ends_of_an_open_chain_are_never_candidates() {
        // 0 lies between 5 and 1, so a ring would drop it; an open chain's
        // end must stay.
        let fixture = fixture();
        let region_vertices = BTreeSet::new();
        let coplanar_edges = BTreeSet::new();
        let directions = BTreeMap::new();
        let ctx = ChainContext {
            mesh: &fixture.mesh,
            frame: fixture.frame,
            classifications: &[],
            projection: fixture.projection,
            directions: &directions,
            coplanar_edges: &coplanar_edges,
            region_vertices: &region_vertices,
        };
        let nodes = vec![vertex(0), vertex(1), vertex(5)];
        assert_eq!(
            simplify_nodes(&ctx, nodes.clone(), false).expect("nodes"),
            nodes
        );
    }
}