BREP_kernel 0.5.0

A boundary representation (BREP) geometry kernel for building CAD applications.
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
use super::*;

/// Axis/radius description shared by the cylinder and cone wall builders:
/// `station(p)` is the axial coordinate, `radius_at(s)` the wall radius.
struct RevolveInfo {
    origin: Vec3,
    axis: Vec3,
    sense: i8,
    radius_slope: f64,
    radius_offset: f64,
}

impl RevolveInfo {
    fn from_carrier(carrier: &RegionCarrier) -> Option<RevolveInfo> {
        match carrier {
            RegionCarrier::Cylinder {
                axis_point,
                axis_dir,
                radius,
                sense,
            } => Some(RevolveInfo {
                origin: *axis_point,
                axis: *axis_dir,
                sense: *sense,
                radius_slope: 0.0,
                radius_offset: *radius,
            }),
            RegionCarrier::Cone {
                apex,
                axis_dir,
                half_angle_rad,
                sense,
            } => Some(RevolveInfo {
                origin: *apex,
                axis: *axis_dir,
                sense: *sense,
                radius_slope: half_angle_rad.tan(),
                radius_offset: 0.0,
            }),
            _ => None,
        }
    }

    fn station(&self, point: Vec3) -> f64 {
        point.sub(self.origin).dot(self.axis)
    }

    fn radius_at(&self, station: f64) -> f64 {
        self.radius_offset + self.radius_slope * station
    }

    fn axis_point_at(&self, station: f64) -> Vec3 {
        self.origin.add(self.axis.scale(station))
    }

    fn radial(&self, point: Vec3) -> Vec3 {
        let d = point.sub(self.origin);
        d.sub(self.axis.scale(d.dot(self.axis)))
    }
}

/// Statistics of a chain against a revolve carrier: mean axial station and
/// worst deviations from "axis-perpendicular circle at that station".
struct RingFit {
    station: f64,
    station_spread: f64,
    radial_spread: f64,
}

fn ring_fit(builder: &RegionBrepBuilder, info: &RevolveInfo, chain: &Chain) -> RingFit {
    let points = builder.chain_points(chain);
    let mean = points.iter().map(|&p| info.station(p)).sum::<f64>() / points.len() as f64;
    let mut station_spread = 0.0_f64;
    let mut radial_spread = 0.0_f64;
    let radius = info.radius_at(mean);
    for &p in &points {
        station_spread = station_spread.max((info.station(p) - mean).abs());
        radial_spread = radial_spread.max((info.radial(p).length() - radius).abs());
    }
    RingFit {
        station: mean,
        station_spread,
        radial_spread,
    }
}

/// Dispatch a cylinder/cone region to the full-wall or partial-patch
/// builder based on its boundary structure.
pub(super) fn build_revolved_region_face(
    builder: &mut RegionBrepBuilder,
    region: &MeshRegion,
    cycles: &[Vec<usize>],
) -> Result<FaceRecord, String> {
    let info = RevolveInfo::from_carrier(&region.carrier)
        .expect("caller dispatches only revolve carriers");
    let traversals = cycles
        .iter()
        .map(|cycle| builder.cycle_traversals(cycle))
        .collect::<Result<Vec<_>, _>>()?;
    let all_rings = traversals
        .iter()
        .all(|t| t.len() == 1 && builder.chains[t[0].chain].closed);
    if cycles.len() == 2 && all_rings {
        return build_full_wall_face(builder, region, &info, &traversals);
    }
    if matches!(region.carrier, RegionCarrier::Cylinder { .. })
        && cycles.len() == 1
        && traversals[0].len() == 4
    {
        return build_cylinder_patch_face(builder, region, &info, &traversals[0]);
    }
    let mut detail = String::new();
    for (cycle_index, cycle_traversals) in traversals.iter().enumerate() {
        detail.push_str(&format!(" loop{cycle_index}:["));
        for traversal in cycle_traversals {
            let chain = &builder.chains[traversal.chain];
            let fit = ring_fit(builder, &info, chain);
            let points = builder.chain_points(chain);
            detail.push_str(&format!(
                "chain{}(n={},closed={},dz={:.2e},dr={:.2e},line={:.2e})",
                traversal.chain,
                chain.vertices.len(),
                chain.closed,
                fit.station_spread,
                fit.radial_spread,
                max_deviation_from_segment(&points),
            ));
        }
        detail.push(']');
    }
    Err(format!(
        "mesh_regions_to_brep: region {} ({}) has an unsupported boundary layout \
         ({} loops); v1 rebuilds full walls with two perpendicular rings and \
         four-sided cylinder patches;{detail}",
        region.id,
        region.carrier.kind(),
        cycles.len()
    ))
}

/// Full revolve wall bounded by two axis-perpendicular rings — the exact
/// `make_cylinder_brep` layout: seam edge used twice, one circle edge per
/// ring shared with the neighboring face.
fn build_full_wall_face(
    builder: &mut RegionBrepBuilder,
    region: &MeshRegion,
    info: &RevolveInfo,
    traversals: &[Vec<Traversal>],
) -> Result<FaceRecord, String> {
    if info.sense != 1 {
        return Err(format!(
            "mesh_regions_to_brep: region {} is a cavity wall (sense -1); inner revolve \
             walls are not supported in v1",
            region.id
        ));
    }
    let chain_a = traversals[0][0].chain;
    let chain_b = traversals[1][0].chain;
    let fit_a = ring_fit(builder, info, &builder.chains[chain_a]);
    let fit_b = ring_fit(builder, info, &builder.chains[chain_b]);
    for fit in [&fit_a, &fit_b] {
        if fit.station_spread > builder.tol || fit.radial_spread > builder.tol {
            return Err(format!(
                "mesh_regions_to_brep: region {} boundary ring is not an axis-perpendicular \
                 circle (station spread {:.3e}, radial spread {:.3e})",
                region.id, fit.station_spread, fit.radial_spread
            ));
        }
    }
    let (bottom_chain, bottom_fit, top_chain, top_fit) = if fit_a.station <= fit_b.station {
        (chain_a, fit_a, chain_b, fit_b)
    } else {
        (chain_b, fit_b, chain_a, fit_a)
    };
    if top_fit.station - bottom_fit.station <= builder.tol {
        return Err(format!(
            "mesh_regions_to_brep: region {} wall rings coincide axially",
            region.id
        ));
    }
    for chain in [bottom_chain, top_chain] {
        if builder.chain_edges[chain].is_some() {
            return Err(format!(
                "mesh_regions_to_brep: region {} shares a ring with another revolve wall — \
                 not supported in v1",
                region.id
            ));
        }
    }

    let r0 = info.radius_at(bottom_fit.station);
    let r1 = info.radius_at(top_fit.station);
    if !(r0 > builder.tol) || !(r1 > builder.tol) {
        return Err(format!(
            "mesh_regions_to_brep: region {} wall touches its apex/axis",
            region.id
        ));
    }
    let base = info.axis_point_at(bottom_fit.station);
    let top = info.axis_point_at(top_fit.station);
    let x_axis = info.axis.perpendicular()?;
    let generatrix = make_line(base.add(x_axis.scale(r0)), top.add(x_axis.scale(r1)))?;
    let surface = make_revolution(base, info.axis, &generatrix, std::f64::consts::TAU)?;

    let bottom_circle = surface.iso_curve_v(0.0)?;
    let top_circle = surface.iso_curve_v(1.0)?;
    let seam = surface.iso_curve_u(0.0)?;
    let bottom_vertex = builder.allocate_id();
    builder.vertices.push(VertexRecord {
        id: bottom_vertex,
        point: bottom_circle.evaluate(0.0)?,
    });
    let top_vertex = builder.allocate_id();
    builder.vertices.push(VertexRecord {
        id: top_vertex,
        point: top_circle.evaluate(0.0)?,
    });
    let bottom_edge = builder.push_edge(EdgeRecord {
        id: 0,
        curve: bottom_circle,
        t0: 0.0,
        t1: 1.0,
        start_vertex_id: bottom_vertex,
        end_vertex_id: bottom_vertex,
        degenerate: false,
        name: None,
    });
    let top_edge = builder.push_edge(EdgeRecord {
        id: 0,
        curve: top_circle,
        t0: 0.0,
        t1: 1.0,
        start_vertex_id: top_vertex,
        end_vertex_id: top_vertex,
        degenerate: false,
        name: None,
    });
    let seam_edge = builder.push_edge(EdgeRecord {
        id: 0,
        curve: seam,
        t0: 0.0,
        t1: 1.0,
        start_vertex_id: bottom_vertex,
        end_vertex_id: top_vertex,
        degenerate: false,
        name: None,
    });
    builder.chain_edges[bottom_chain] = Some(ChainEdgeInfo {
        edge_id: bottom_edge,
        curve_along_chain: true,
        ring: Some(RingClaim {
            axis_point: base,
            axis: info.axis,
            wall_forward: true,
        }),
    });
    builder.chain_edges[top_chain] = Some(ChainEdgeInfo {
        edge_id: top_edge,
        curve_along_chain: true,
        ring: Some(RingClaim {
            axis_point: top,
            axis: info.axis,
            wall_forward: false,
        }),
    });

    let coedges = vec![
        CoedgeRecord {
            id: builder.allocate_id(),
            edge_id: bottom_edge,
            forward: true,
            pcurve: parameter_segment(0.0, 0.0, 1.0, 0.0)?,
        },
        CoedgeRecord {
            id: builder.allocate_id(),
            edge_id: seam_edge,
            forward: true,
            pcurve: parameter_segment(1.0, 0.0, 1.0, 1.0)?,
        },
        CoedgeRecord {
            id: builder.allocate_id(),
            edge_id: top_edge,
            forward: false,
            pcurve: parameter_segment(1.0, 1.0, 0.0, 1.0)?,
        },
        CoedgeRecord {
            id: builder.allocate_id(),
            edge_id: seam_edge,
            forward: false,
            pcurve: parameter_segment(0.0, 1.0, 0.0, 0.0)?,
        },
    ];
    let loop_id = builder.allocate_id();
    let face_id = builder.allocate_id();
    Ok(FaceRecord {
        id: face_id,
        surface,
        same_sense: true,
        loops: vec![LoopRecord {
            id: loop_id,
            coedges,
        }],
        name: None,
    })
}

enum PatchRole {
    BottomArc,
    TopArc,
    RulingU0,
    RulingU1,
}

/// Partial cylinder patch (fillet blend): one loop of two axis-perpendicular
/// arcs joined by two straight rulings, rebuilt as the exact extrusion of
/// the bottom arc along the axis with iso-parameter edges.
fn build_cylinder_patch_face(
    builder: &mut RegionBrepBuilder,
    region: &MeshRegion,
    info: &RevolveInfo,
    traversals: &[Traversal],
) -> Result<FaceRecord, String> {
    let radius = info.radius_offset;
    // Classify the four chains.  Alternation (arc, ruling, arc, ruling) is
    // implied by the corner checks below: each ruling must land exactly on
    // an arc endpoint.
    let mut arcs: Vec<(usize, f64)> = Vec::new();
    let mut rulings: Vec<usize> = Vec::new();
    for traversal in traversals {
        let chain = &builder.chains[traversal.chain];
        if chain.closed {
            return Err(format!(
                "mesh_regions_to_brep: region {} patch loop contains a closed ring",
                region.id
            ));
        }
        let fit = ring_fit(builder, info, chain);
        let points = builder.chain_points(chain);
        let is_arc = fit.station_spread <= builder.tol && fit.radial_spread <= builder.tol;
        let straight = max_deviation_from_segment(&points) <= builder.tol;
        let axis_parallel = if straight {
            let direction = points
                .last()
                .unwrap()
                .sub(points[0])
                .normalized()
                .unwrap_or_default();
            direction.cross(info.axis).length() <= 1e-3
        } else {
            false
        };
        if is_arc {
            arcs.push((traversal.chain, fit.station));
        } else if straight && axis_parallel {
            rulings.push(traversal.chain);
        } else {
            return Err(format!(
                "mesh_regions_to_brep: region {} patch boundary chain is neither an \
                 axis-perpendicular arc nor an axial ruling (v1)",
                region.id
            ));
        }
    }
    if arcs.len() != 2 || rulings.len() != 2 {
        return Err(format!(
            "mesh_regions_to_brep: region {} patch needs two arcs and two rulings \
             (found {} arcs, {} rulings)",
            region.id,
            arcs.len(),
            rulings.len()
        ));
    }
    let (bottom_chain, s0) = if arcs[0].1 <= arcs[1].1 {
        arcs[0]
    } else {
        arcs[1]
    };
    let (top_chain, s1) = if arcs[0].1 <= arcs[1].1 {
        arcs[1]
    } else {
        arcs[0]
    };
    if s1 - s0 <= builder.tol {
        return Err(format!(
            "mesh_regions_to_brep: region {} patch arcs coincide axially",
            region.id
        ));
    }
    for chain in [bottom_chain, top_chain, rulings[0], rulings[1]] {
        if builder.chain_edges[chain].is_some() {
            return Err(format!(
                "mesh_regions_to_brep: region {} patch chain already claimed by another \
                 curved region — not supported in v1",
                region.id
            ));
        }
    }

    // Exact bottom arc along the chain's stored order.
    let center = info.axis_point_at(s0);
    let bottom_points = builder.chain_points(&builder.chains[bottom_chain]);
    let x_axis = info.radial(bottom_points[0]).normalized()?;
    let y_ccw = info.axis.cross(x_axis);
    let mut swept = 0.0_f64;
    let mut previous = 0.0_f64;
    for &p in &bottom_points[1..] {
        let radial = info.radial(p);
        let angle = radial.dot(y_ccw).atan2(radial.dot(x_axis));
        swept += wrap_to_pi(angle - previous);
        previous = angle;
    }
    if swept.abs() <= 1e-9 || swept.abs() >= std::f64::consts::TAU - 1e-9 {
        return Err(format!(
            "mesh_regions_to_brep: region {} patch arc sweep {swept:.3e} out of range",
            region.id
        ));
    }
    let y_axis = if swept >= 0.0 {
        y_ccw
    } else {
        y_ccw.scale(-1.0)
    };
    let arc_along_chain = make_arc(center, x_axis, y_axis, radius, 0.0, swept.abs())?;

    // Orient the profile so the extrusion normal (tangent × direction)
    // matches the mesh's outward side of the carrier.
    let direction = info.axis.scale(s1 - s0);
    let mid = arc_along_chain.derivatives(0.5, 1)?;
    let outward = info.radial(mid[0]).normalized()?.scale(info.sense as f64);
    let along = mid[1].cross(direction).dot(outward) > 0.0;
    let profile = if along {
        arc_along_chain
    } else {
        arc_along_chain.reversed()?
    };
    let surface = make_extrusion(&profile, direction)?;

    // Edges: bottom/top arcs (iso v) and the two rulings (iso u).
    let bottom_first = builder.chains[bottom_chain].vertices[0];
    let bottom_last = *builder.chains[bottom_chain].vertices.last().unwrap();
    let (bottom_start_welded, bottom_end_welded) = if along {
        (bottom_first, bottom_last)
    } else {
        (bottom_last, bottom_first)
    };
    let geometry_tolerance = builder.tol.max(1e-9 * builder.data.diag);
    let profile_start = profile.evaluate(0.0)?;
    let profile_end = profile.evaluate(1.0)?;
    if profile_start
        .sub(builder.data.verts[bottom_start_welded])
        .length()
        > geometry_tolerance
        || profile_end
            .sub(builder.data.verts[bottom_end_welded])
            .length()
            > geometry_tolerance
    {
        return Err(format!(
            "mesh_regions_to_brep: region {} exact arc misses its mesh corners",
            region.id
        ));
    }
    let top_curve = translate_curve(&profile, direction)?;
    let top_first = builder.chains[top_chain].vertices[0];
    let top_last = *builder.chains[top_chain].vertices.last().unwrap();
    let top_start_point = profile_start.add(direction);
    let top_along =
        if builder.data.verts[top_first].sub(top_start_point).length() <= geometry_tolerance {
            true
        } else if builder.data.verts[top_last].sub(top_start_point).length() <= geometry_tolerance {
            false
        } else {
            return Err(format!(
                "mesh_regions_to_brep: region {} top arc corners do not sit above the bottom arc",
                region.id
            ));
        };
    let (top_start_welded, top_end_welded) = if top_along {
        (top_first, top_last)
    } else {
        (top_last, top_first)
    };

    // Match each ruling chain to u=0 (profile start) or u=1 (profile end).
    let ruling_of = |chain_id: usize| -> Result<(PatchRole, usize, usize), String> {
        let chain = &builder.chains[chain_id];
        let first = chain.vertices[0];
        let last = *chain.vertices.last().unwrap();
        let (bottom_welded, top_welded) =
            if info.station(builder.data.verts[first]) <= info.station(builder.data.verts[last]) {
                (first, last)
            } else {
                (last, first)
            };
        let p_bottom = builder.data.verts[bottom_welded];
        if p_bottom.sub(profile_start).length() <= geometry_tolerance {
            Ok((PatchRole::RulingU0, bottom_welded, top_welded))
        } else if p_bottom.sub(profile_end).length() <= geometry_tolerance {
            Ok((PatchRole::RulingU1, bottom_welded, top_welded))
        } else {
            Err(format!(
                "mesh_regions_to_brep: region {} ruling does not meet the arc corners",
                region.id
            ))
        }
    };
    let (role_a, ruling_a_bottom, ruling_a_top) = ruling_of(rulings[0])?;
    let (role_b, ruling_b_bottom, ruling_b_top) = ruling_of(rulings[1])?;
    if matches!(role_a, PatchRole::RulingU0) == matches!(role_b, PatchRole::RulingU0) {
        return Err(format!(
            "mesh_regions_to_brep: region {} rulings both land on the same arc corner",
            region.id
        ));
    }

    // Create the four shared edges.
    let register = |builder: &mut RegionBrepBuilder,
                    chain_id: usize,
                    curve: NurbsCurve,
                    start_welded: usize,
                    end_welded: usize,
                    curve_along_chain: bool|
     -> u64 {
        let start_vertex_id = builder.welded_vertex_id(start_welded);
        let end_vertex_id = builder.welded_vertex_id(end_welded);
        let edge_id = builder.push_edge(EdgeRecord {
            id: 0,
            curve,
            t0: 0.0,
            t1: 1.0,
            start_vertex_id,
            end_vertex_id,
            degenerate: false,
            name: None,
        });
        builder.chain_edges[chain_id] = Some(ChainEdgeInfo {
            edge_id,
            curve_along_chain,
            ring: None,
        });
        edge_id
    };
    register(
        builder,
        bottom_chain,
        profile.clone(),
        bottom_start_welded,
        bottom_end_welded,
        along,
    );
    register(
        builder,
        top_chain,
        top_curve,
        top_start_welded,
        top_end_welded,
        top_along,
    );
    let ruling_line_u0 = make_line(profile_start, profile_start.add(direction))?;
    let ruling_line_u1 = make_line(profile_end, profile_end.add(direction))?;
    let (u0_chain, u0_bottom, u0_top, u1_chain, u1_bottom, u1_top) =
        if matches!(role_a, PatchRole::RulingU0) {
            (
                rulings[0],
                ruling_a_bottom,
                ruling_a_top,
                rulings[1],
                ruling_b_bottom,
                ruling_b_top,
            )
        } else {
            (
                rulings[1],
                ruling_b_bottom,
                ruling_b_top,
                rulings[0],
                ruling_a_bottom,
                ruling_a_top,
            )
        };
    register(
        builder,
        u0_chain,
        ruling_line_u0,
        u0_bottom,
        u0_top,
        builder.chains[u0_chain].vertices[0] == u0_bottom,
    );
    register(
        builder,
        u1_chain,
        ruling_line_u1,
        u1_bottom,
        u1_top,
        builder.chains[u1_chain].vertices[0] == u1_bottom,
    );

    // Patch loop in the region's own walk order (guarantees opposite edge
    // senses against the planar neighbors' walks).
    let role_of = |chain_id: usize| -> PatchRole {
        if chain_id == bottom_chain {
            PatchRole::BottomArc
        } else if chain_id == top_chain {
            PatchRole::TopArc
        } else if chain_id == u0_chain {
            PatchRole::RulingU0
        } else {
            PatchRole::RulingU1
        }
    };
    let mut coedges = Vec::with_capacity(4);
    for traversal in traversals {
        let infoc = builder.chain_edges[traversal.chain]
            .as_ref()
            .expect("patch chains registered above");
        let forward = traversal.forward_along_chain == infoc.curve_along_chain;
        let edge_id = infoc.edge_id;
        let pcurve = match role_of(traversal.chain) {
            PatchRole::BottomArc => {
                if forward {
                    parameter_segment(0.0, 0.0, 1.0, 0.0)?
                } else {
                    parameter_segment(1.0, 0.0, 0.0, 0.0)?
                }
            }
            PatchRole::TopArc => {
                if forward {
                    parameter_segment(0.0, 1.0, 1.0, 1.0)?
                } else {
                    parameter_segment(1.0, 1.0, 0.0, 1.0)?
                }
            }
            PatchRole::RulingU0 => {
                if forward {
                    parameter_segment(0.0, 0.0, 0.0, 1.0)?
                } else {
                    parameter_segment(0.0, 1.0, 0.0, 0.0)?
                }
            }
            PatchRole::RulingU1 => {
                if forward {
                    parameter_segment(1.0, 0.0, 1.0, 1.0)?
                } else {
                    parameter_segment(1.0, 1.0, 1.0, 0.0)?
                }
            }
        };
        coedges.push(CoedgeRecord {
            id: builder.allocate_id(),
            edge_id,
            forward,
            pcurve,
        });
    }
    let loop_id = builder.allocate_id();
    let face_id = builder.allocate_id();
    Ok(FaceRecord {
        id: face_id,
        surface,
        same_sense: true,
        loops: vec![LoopRecord {
            id: loop_id,
            coedges,
        }],
        name: None,
    })
}

/// Planar region: one trimmed plane face; loops come from the region's
/// ordered boundary walks, edges are shared chain curves, pcurves are exact
/// affine projections.
pub(super) fn build_planar_region_face(
    builder: &mut RegionBrepBuilder,
    region_id: u32,
    origin: Vec3,
    normal: Vec3,
    cycles: &[Vec<usize>],
) -> Result<FaceRecord, String> {
    if cycles.is_empty() {
        return Err(format!(
            "mesh_regions_to_brep: planar region {region_id} has no boundary"
        ));
    }
    let x_axis = normal.perpendicular()?;
    let y_axis = normal.cross(x_axis);

    // Resolve every loop to (edge id, forward) pairs first; the plane's
    // extents must cover all edge control points before pcurves exist.
    let mut loops: Vec<(Vec<(u64, bool)>, f64)> = Vec::new();
    for cycle in cycles {
        let traversals = builder.cycle_traversals(cycle)?;
        let mut entries = Vec::with_capacity(traversals.len());
        for traversal in &traversals {
            if builder.chain_edges[traversal.chain].is_none() {
                builder.ensure_open_chain_edge(traversal.chain)?;
            }
            let chain_info = builder.chain_edges[traversal.chain].as_ref().unwrap();
            let forward = if let Some(claim) = &chain_info.ring {
                let circulation = builder.cycle_circulation(cycle, claim.axis_point, claim.axis)?;
                let forward = circulation > 0.0;
                if forward == claim.wall_forward {
                    return Err(format!(
                        "mesh_regions_to_brep: planar region {region_id} traverses a wall \
                         ring in the wall's own direction (non-manifold orientation)"
                    ));
                }
                forward
            } else {
                traversal.forward_along_chain == chain_info.curve_along_chain
            };
            entries.push((chain_info.edge_id, forward));
        }
        // Signed area of the walk polygon in the outward plane frame:
        // positive = outer loop, negative = hole.
        let mut area = 0.0;
        for index in 0..cycle.len() {
            let a = builder.data.verts[cycle[index]].sub(origin);
            let b = builder.data.verts[cycle[(index + 1) % cycle.len()]].sub(origin);
            let (ax, ay) = (a.dot(x_axis), a.dot(y_axis));
            let (bx, by) = (b.dot(x_axis), b.dot(y_axis));
            area += 0.5 * (ax * by - bx * ay);
        }
        loops.push((entries, area));
    }
    loops.sort_by(|a, b| b.1.total_cmp(&a.1));
    if !(loops[0].1 > 0.0) || loops.iter().skip(1).any(|entry| !(entry.1 < 0.0)) {
        return Err(format!(
            "mesh_regions_to_brep: planar region {region_id} loops do not split into one \
             outer boundary plus holes"
        ));
    }

    // Plane extents over every referenced curve's control hull, with margin
    // so evaluated uv never clamps at the domain boundary.
    let mut min_u = f64::INFINITY;
    let mut max_u = f64::NEG_INFINITY;
    let mut min_v = f64::INFINITY;
    let mut max_v = f64::NEG_INFINITY;
    for (entries, _) in &loops {
        for &(edge_id, _) in entries {
            for control in &builder.edge_curve(edge_id).control_points {
                let delta = control.point()?.sub(origin);
                let u = delta.dot(x_axis);
                let v = delta.dot(y_axis);
                min_u = min_u.min(u);
                max_u = max_u.max(u);
                min_v = min_v.min(v);
                max_v = max_v.max(v);
            }
        }
    }
    let extent = (max_u - min_u).max(max_v - min_v);
    if !(extent > 0.0) || !extent.is_finite() {
        return Err(format!(
            "mesh_regions_to_brep: planar region {region_id} has a degenerate boundary"
        ));
    }
    let margin = (extent * 0.05).max(1e-6 * builder.data.diag);
    let plane_origin = origin
        .add(x_axis.scale(min_u - margin))
        .add(y_axis.scale(min_v - margin));
    let surface = make_plane(
        plane_origin,
        x_axis,
        y_axis,
        (max_u - min_u) + 2.0 * margin,
        (max_v - min_v) + 2.0 * margin,
    )?;

    let mut loop_records = Vec::with_capacity(loops.len());
    for (entries, _) in &loops {
        let mut coedges = Vec::with_capacity(entries.len());
        for &(edge_id, forward) in entries {
            let mapped = affine_pcurve(builder.edge_curve(edge_id), plane_origin, x_axis, y_axis)?;
            let pcurve = if forward { mapped } else { mapped.reversed()? };
            coedges.push(CoedgeRecord {
                id: builder.allocate_id(),
                edge_id,
                forward,
                pcurve,
            });
        }
        let loop_id = builder.allocate_id();
        loop_records.push(LoopRecord {
            id: loop_id,
            coedges,
        });
    }
    let face_id = builder.allocate_id();
    Ok(FaceRecord {
        id: face_id,
        surface,
        same_sense: true,
        loops: loop_records,
        name: None,
    })
}