BREP_kernel 0.15.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
use super::*;

#[derive(Clone)]
struct SectionFrame {
    normal: Vec3,
    centroid: Vec3,
    samples: Vec<Vec3>,
    planar: bool,
}

pub(super) fn closed_points(
    curves: &[NurbsCurve],
    tolerance: f64,
) -> Result<Vec<Vec3>, KernelRefusal> {
    if curves.len() < 2 {
        return Err(KernelRefusal::input(
            KernelStage::Collect,
            "section_curve_count",
            "loftSolid: a section needs at least 2 curves",
        ));
    }
    let mut points = Vec::with_capacity(curves.len());
    for (index, curve) in curves.iter().enumerate() {
        let [start, end] = curve.domain().or_refuse(KernelStage::Collect, "domain")?;
        let next = &curves[(index + 1) % curves.len()];
        let next_start = next.domain().or_refuse(KernelStage::Collect, "domain")?[0];
        if curve
            .evaluate(end)
            .or_refuse(KernelStage::Collect, "evaluate")?
            .sub(
                next.evaluate(next_start)
                    .or_refuse(KernelStage::Collect, "evaluate")?,
            )
            .length()
            > tolerance
        {
            return Err(KernelRefusal::input(
                KernelStage::Collect,
                "open_section",
                format!("loftSolid: section is open at curve {index}"),
            ));
        }
        points.push(
            curve
                .evaluate(start)
                .or_refuse(KernelStage::Collect, "evaluate")?,
        );
    }
    Ok(points)
}

fn section_frame(curves: &[NurbsCurve], tolerance: f64) -> Result<SectionFrame, KernelRefusal> {
    let mut samples = Vec::new();
    for curve in curves {
        let [start, end] = curve.domain().or_refuse(KernelStage::Classify, "domain")?;
        for index in 0..16 {
            samples.push(
                curve
                    .evaluate(start + (end - start) * index as f64 / 16.0)
                    .or_refuse(KernelStage::Classify, "evaluate")?,
            );
        }
    }
    let mut normal = crate::polygon::newell_normal(&samples);
    let mut centroid = samples.iter().fold(Vec3::default(), |sum, &point| sum.add(point));
    // A zero Newell normal is a section without area — the caller's loop, not a
    // solve, so it is an input refusal carrying the vector helper's own text.
    normal = normal
        .normalized()
        .or_input(KernelStage::Classify, "section_normal")?;
    centroid = centroid.scale(1.0 / samples.len() as f64);
    let planar = samples
        .iter()
        .all(|point| point.sub(samples[0]).dot(normal).abs() <= tolerance * 100.0);
    Ok(SectionFrame {
        normal,
        centroid,
        samples,
        planar,
    })
}

/// How far off an end section's plane a step has to lean before it may say which
/// side of that section the loft's material is on. `0.1` — the same band
/// `extrude_profile_brep` refuses a sliver at, and the same one the sweep's own
/// per-station guard uses.
const CAP_ADVANCE_MIN: f64 = 0.1;

/// Find the inward advance direction at an end section, walking nearest first.
/// Skip centroids in the end plane until `|step·normal| / |step|` reaches
/// `CAP_ADVANCE_MIN`. This handles turning lofts and guides that initially
/// travel sideways; an end-to-end chord alone can misorient their caps.
/// Return `None` if the entire run stays in the end plane.
fn advance_from(
    sections: &[Vec<NurbsCurve>],
    anchor: Vec3,
    normal: Vec3,
    order: impl Iterator<Item = usize>,
    tolerance: f64,
) -> Result<Option<Vec3>, KernelRefusal> {
    for index in order {
        let step = section_frame(&sections[index], tolerance)?
            .centroid
            .sub(anchor);
        let length = step.length();
        if length > tolerance && (step.dot(normal) / length).abs() >= CAP_ADVANCE_MIN {
            return Ok(Some(step.scale(1.0 / length)));
        }
    }
    Ok(None)
}

fn reverse_section(curves: &[NurbsCurve]) -> Result<Vec<NurbsCurve>, KernelRefusal> {
    curves
        .iter()
        .rev()
        .map(NurbsCurve::reversed)
        .collect::<Result<Vec<_>, String>>()
        .or_refuse(KernelStage::Classify, "reverse_section")
}

/// One end cap. `advance` is the direction the loft RUNS THROUGH this cap —
/// measured at this end, not end to end (see `advance_from`) — and it orients
/// the cap plane: `outward` then says whether the face's own normal follows it
/// (the finish cap) or opposes it (the start cap).
fn cap_face(
    curves: &[NurbsCurve],
    edge_ids: &[u64],
    frame: &SectionFrame,
    advance: Vec3,
    outward: bool,
    next_id: &mut u64,
) -> Result<FaceRecord, KernelRefusal> {
    let normal = if frame.normal.dot(advance) >= 0.0 {
        frame.normal
    } else {
        frame.normal.scale(-1.0)
    };
    let x_axis = normal
        .perpendicular()
        .or_refuse(KernelStage::Fragment, "cap_frame")?;
    let y_axis = normal
        .cross(x_axis)
        .normalized()
        .or_refuse(KernelStage::Fragment, "cap_frame")?;
    let (mut min_x, mut min_y) = (f64::INFINITY, f64::INFINITY);
    let (mut max_x, mut max_y) = (f64::NEG_INFINITY, f64::NEG_INFINITY);
    for point in &frame.samples {
        let delta = point.sub(frame.centroid);
        min_x = min_x.min(delta.dot(x_axis));
        max_x = max_x.max(delta.dot(x_axis));
        min_y = min_y.min(delta.dot(y_axis));
        max_y = max_y.max(delta.dot(y_axis));
    }
    let padding = (max_x - min_x).max(max_y - min_y) * 0.05 + 1e-6;
    let origin = frame
        .centroid
        .add(x_axis.scale(min_x - padding))
        .add(y_axis.scale(min_y - padding));
    let surface = make_plane(
        origin,
        x_axis,
        y_axis,
        max_x - min_x + 2.0 * padding,
        max_y - min_y + 2.0 * padding,
    )
    .or_refuse(KernelStage::Fragment, "cap_plane")?;
    let mut coedges = Vec::with_capacity(curves.len());
    if outward {
        for (index, curve) in curves.iter().enumerate() {
            coedges.push(CoedgeRecord {
                id: *next_id,
                edge_id: edge_ids[index],
                forward: true,
                pcurve: curve_to_plane_parameters(curve, origin, x_axis, y_axis)
                    .or_refuse(KernelStage::Fragment, "cap_pcurve")?,
            });
            *next_id += 1;
        }
    } else {
        for index in (0..curves.len()).rev() {
            coedges.push(CoedgeRecord {
                id: *next_id,
                edge_id: edge_ids[index],
                forward: false,
                pcurve: curve_to_plane_parameters(&curves[index], origin, x_axis, y_axis)
                    .or_refuse(KernelStage::Fragment, "cap_pcurve")?
                    .reversed()
                    .or_refuse(KernelStage::Fragment, "cap_pcurve")?,
            });
            *next_id += 1;
        }
    }
    let loop_id = *next_id;
    *next_id += 1;
    let face_id = *next_id;
    *next_id += 1;
    Ok(FaceRecord {
        id: face_id,
        surface,
        same_sense: outward,
        loops: vec![LoopRecord {
            id: loop_id,
            coedges,
        }],
        name: None,
    })
}

/// Loft corresponding edges of closed sections. For two sections, exact
/// degree elevation and knot insertion reconcile mixed line/arc representations
/// and the ruled skins retain both boundaries' rational weights. Multi-section
/// interpolation requires matching degrees, knots and weights.
pub fn loft_profile_brep(input_sections: &[Vec<NurbsCurve>]) -> Result<BrepSolid, KernelRefusal> {
    loft_profile_brep_core(input_sections, None)
}

/// §5.8 loft with END TANGENCY: the skin leaves the first section along
/// `start_direction` and arrives at the last along `end_direction` (unit
/// directions; each interpolation column scales them by its own chord length,
/// the standard magnitude that keeps the v-parametrization well conditioned).
/// Exact by construction — the column interpolant reproduces the prescribed
/// end derivatives.
pub fn loft_profile_brep_tangent(
    input_sections: &[Vec<NurbsCurve>],
    start_direction: Vec3,
    end_direction: Vec3,
) -> Result<BrepSolid, KernelRefusal> {
    let start = start_direction.normalized().map_err(|_| {
        KernelRefusal::input(
            KernelStage::Collect,
            "start_tangent",
            "loftSolid: start tangent must be a nonzero direction",
        )
    })?;
    let end = end_direction.normalized().map_err(|_| {
        KernelRefusal::input(
            KernelStage::Collect,
            "end_tangent",
            "loftSolid: end tangent must be a nonzero direction",
        )
    })?;
    loft_profile_brep_core(input_sections, Some((start, end)))
}

fn loft_profile_brep_core(
    input_sections: &[Vec<NurbsCurve>],
    end_tangents: Option<(Vec3, Vec3)>,
) -> Result<BrepSolid, KernelRefusal> {
    let mut profile = super::ConstructionProfile::new("loft");
    let tolerance = 1e-6;
    let section_count = input_sections.len();
    if section_count < 2 {
        return Err(KernelRefusal::input(
            KernelStage::Collect,
            "section_count",
            "loftSolid: need at least 2 sections",
        ));
    }
    let mut sections = input_sections.to_vec();
    // A two-section loft is a ruled rational patch: unlike the interpolated
    // multi-station/tangent paths it can retain different boundary weights.
    let ruled = section_count == 2 && end_tangents.is_none();
    let curve_count = if ruled {
        super::validation::validate_section_loops(&sections, tolerance, "loftSolid")?
    } else {
        validate_sections(&sections, tolerance, "loftSolid", true)?
    };

    let first_frame = section_frame(&sections[0], tolerance)?;
    let last_frame = section_frame(&sections[section_count - 1], tolerance)?;
    if !first_frame.planar || !last_frame.planar {
        return Err(KernelRefusal::unsupported(
            KernelStage::Classify,
            "planar_end_sections",
            "loftSolid: end sections must be planar",
        ));
    }
    // Ends in the same place put the two caps on top of each other whatever the
    // run does in between; say that before blaming a section plane below.
    last_frame
        .centroid
        .sub(first_frame.centroid)
        .normalized()
        .map_err(|_| {
            KernelRefusal::input(
                KernelStage::Classify,
                "coincident_ends",
                "loftSolid: end sections coincide",
            )
        })?;
    // Each cap must face away from its own inward advance direction; a turning
    // loft can leave one end edge-on even when the other end is valid.
    let start_advance = advance_from(
        &sections,
        first_frame.centroid,
        first_frame.normal,
        1..section_count,
        tolerance,
    )?
    .ok_or_else(|| {
        KernelRefusal::input(
            KernelStage::Classify,
            "start_in_plane",
            "loftSolid: the loft runs inside its START section's plane — every later section \
             lies in it, so the start cap would be a sliver rather than a face",
        )
    })?;
    // Walking inward from the FAR end measures backwards, so negate it: both
    // directions point the way the loft runs, start to finish.
    let finish_advance = advance_from(
        &sections,
        last_frame.centroid,
        last_frame.normal,
        (0..section_count - 1).rev(),
        tolerance,
    )?
    .map(|step| step.scale(-1.0))
    .ok_or_else(|| {
        KernelRefusal::input(
            KernelStage::Classify,
            "end_in_plane",
            "loftSolid: the loft runs inside its END section's plane — every earlier section \
             lies in it, so the end cap would be a sliver rather than a face",
        )
    })?;
    let first_normal = if first_frame.normal.dot(start_advance) >= 0.0 {
        first_frame.normal
    } else {
        first_frame.normal.scale(-1.0)
    };
    let x_axis = first_normal
        .perpendicular()
        .or_refuse(KernelStage::Classify, "section_frame")?;
    let y_axis = first_normal
        .cross(x_axis)
        .normalized()
        .or_refuse(KernelStage::Classify, "section_frame")?;
    // Winding is normalized PER SECTION against this one frame, not decided once
    // from section 0 and applied to every section.
    //
    // The skin rails control point `k` of curve `i` of one section to the same
    // `(i, k)` of the next, so two sections traversed OPPOSITE ways rail corner
    // to opposite corner and the skin twists into a bow-tie. Deciding one flip
    // from section 0 and applying it to all of them cannot see that: it reverses
    // them together, which preserves the disagreement exactly.
    //
    // Measured before this changed (`examples/feature_refusal_census_probe.rs`,
    // `GATE loft_profile_brep.opposed_winding`): two 2x2 squares 5 apart, wound
    // opposite ways, lofted to a solid of **volume 0 that passes `validate()`**
    // — a silently-wrong result, not a refusal. The shipping LOFT feature
    // reached it with two ordinary sketches on opposed planes.
    //
    // The comparison is CHAIN-RELATIVE — each section against its PREDECESSOR,
    // not against section 0.
    //
    // Measuring every section against section 0's frame looks equivalent and is
    // not: a frame-guided loft rotates each station's plane with the guide, so
    // on a bend past 90 degrees a station's projected area goes negative purely
    // because its plane has turned (cos > 90 deg < 0), and reversing it un-does
    // the guide's own rotation. Measured on a 150-degree arc: volume 17.5685
    // against a Pappus-exact 104.7198, a 83 % error, where the predecessor rule
    // is exact.
    //
    // Consecutive stations of any sane loft turn far less than 90 degrees, so
    // the predecessor comparison leaves a bend alone, while a genuinely opposed
    // pair still flips. It also removes the knife edge at exactly 90 degrees,
    // where a fixed-frame projection is float noise.
    //
    // `section_frame(..).normal` is the Newell normal, whose ORIENTATION already
    // encodes the traversal direction; only its sign is read. Section 0 keeps
    // the original rule verbatim, so its behaviour — and the side-face
    // permutation below, which keys off section 0 alone — is byte-identical.
    let mut section_reversed = Vec::with_capacity(section_count);
    section_reversed.push(
        profile_area(&sections[0], first_frame.centroid, x_axis, y_axis)
            .or_refuse(KernelStage::Classify, "profile_area")?
            < 0.0,
    );
    let mut previous_normal = if section_reversed[0] {
        first_frame.normal.scale(-1.0)
    } else {
        first_frame.normal
    };
    for section in sections.iter().skip(1) {
        let normal = section_frame(section, tolerance)?.normal;
        let flip = normal.dot(previous_normal) < 0.0;
        section_reversed.push(flip);
        previous_normal = if flip { normal.scale(-1.0) } else { normal };
    }
    // Side faces are named by the FIRST section's input-curve order, so the
    // face permutation below still keys off section 0 alone.
    let reversed_winding = section_reversed[0];
    for (index, flip) in section_reversed.into_iter().enumerate() {
        if flip {
            sections[index] = reverse_section(&sections[index])?;
        }
    }

    if ruled {
        // Match AFTER winding normalization: reversing a loop changes which
        // curves correspond. Degree elevation and knot insertion preserve the
        // exact line/arc geometry; no sampling or refitting is involved.
        for curve_index in 0..curve_count {
            let family = [
                sections[0][curve_index].clone(),
                sections[1][curve_index].clone(),
            ];
            // Preserve existing parameter domains when no conversion is needed.
            if family[0].degree == family[1].degree
                && family[0].knots == family[1].knots
                && family[0].control_points.len() == family[1].control_points.len()
            {
                continue;
            }
            let (compatible, _) = crate::coons::compat::make_family_compatible(&family, 1)
                .or_refuse(KernelStage::Refine, "section_compatibility")?;
            for (section, curve) in sections.iter_mut().zip(compatible) {
                section[curve_index] = curve;
            }
        }
    }

    profile.lap("profile");
    let mut parameters = vec![0.0; section_count];
    let mut accumulated = vec![0.0; section_count];
    let mut columns = 0usize;
    for curve_index in 0..curve_count {
        for control_index in 0..sections[0][curve_index].control_points.len() {
            let mut total = 0.0;
            let mut chords = vec![0.0; section_count];
            for section_index in 1..section_count {
                let previous = sections[section_index - 1][curve_index].control_points
                    [control_index]
                    .point()
                    .or_refuse(KernelStage::Refine, "control_point")?;
                let current = sections[section_index][curve_index].control_points[control_index]
                    .point()
                    .or_refuse(KernelStage::Refine, "control_point")?;
                total += current.sub(previous).length();
                chords[section_index] = total;
            }
            if total <= tolerance {
                continue;
            }
            for section_index in 0..section_count {
                accumulated[section_index] += chords[section_index] / total;
            }
            columns += 1;
        }
    }
    if columns == 0 {
        return Err(KernelRefusal::input(
            KernelStage::Classify,
            "coincident_sections",
            "loftSolid: sections coincide",
        ));
    }
    for index in 0..section_count {
        parameters[index] = accumulated[index] / columns as f64;
    }
    parameters[0] = 0.0;
    parameters[section_count - 1] = 1.0;
    if parameters.windows(2).any(|pair| pair[1] <= pair[0] + 1e-9) {
        return Err(KernelRefusal::input(
            KernelStage::Classify,
            "section_order",
            "loftSolid: sections are not strictly ordered",
        ));
    }
    // Tangent lofts always interpolate cubically — the end-derivative rows
    // need the two extra control points even for a 2-section Hermite loft.
    let degree_v = if end_tangents.is_some() {
        3
    } else {
        3usize.min(section_count - 1)
    };
    let mut skins = Vec::with_capacity(curve_count);
    for curve_index in 0..curve_count {
        let reference = &sections[0][curve_index];
        if ruled {
            // Interpolate HOMOGENEOUS controls, including each end's own
            // weights. Copying the first end's weights onto the second would
            // turn an exact circular arc into a different boundary curve.
            let grid = reference
                .control_points
                .iter()
                .zip(&sections[1][curve_index].control_points)
                .map(|(&start, &end)| vec![start, end])
                .collect();
            skins.push(
                NurbsSurface::new(
                    reference.degree,
                    1,
                    reference.knots.clone(),
                    vec![0.0, 0.0, 1.0, 1.0],
                    grid,
                )
                .or_refuse(KernelStage::Fragment, "skin")?,
            );
            continue;
        }
        let mut grid = Vec::with_capacity(reference.control_points.len());
        let mut knots_v = Vec::new();
        for control_index in 0..reference.control_points.len() {
            let points = sections
                .iter()
                .map(|section| section[curve_index].control_points[control_index].point())
                .collect::<Result<Vec<_>, String>>()
                .or_refuse(KernelStage::Refine, "control_point")?;
            let interpolated = match end_tangents {
                None => interpolate_curve(&points, degree_v, &parameters)
                    .or_refuse(KernelStage::Refine, "interpolate")?,
                Some((start_direction, end_direction)) => {
                    let chord: f64 = points
                        .windows(2)
                        .map(|pair| pair[1].sub(pair[0]).length())
                        .sum();
                    // A column whose stations coincide still needs a usable
                    // tangent magnitude; fall back to the section spacing.
                    let magnitude = if chord > tolerance { chord } else { 1.0 };
                    crate::interpolate_curve_with_end_tangents(
                        &points,
                        &parameters,
                        start_direction.scale(magnitude),
                        end_direction.scale(magnitude),
                    )
                    .or_refuse(KernelStage::Refine, "interpolate_tangent")?
                }
            };
            knots_v = interpolated.knots.clone();
            let weight = reference.control_points[control_index].w;
            grid.push(
                interpolated
                    .control_points
                    .iter()
                    .map(|point| Vec4::from_point(point.point().unwrap(), weight))
                    .collect(),
            );
        }
        skins.push(NurbsSurface::new(
            reference.degree,
            degree_v,
            reference.knots.clone(),
            knots_v,
            grid,
        )
        .or_refuse(KernelStage::Fragment, "skin")?);
    }

    profile.lap("surface");
    let bottom = &sections[0];
    let top = &sections[section_count - 1];
    let bottom_points = closed_points(bottom, tolerance)?;
    let top_points = closed_points(top, tolerance)?;
    let mut vertices = Vec::with_capacity(2 * curve_count);
    for (index, point) in bottom_points.iter().chain(&top_points).enumerate() {
        vertices.push(VertexRecord {
            id: index as u64 + 1,
            point: *point,
        });
    }
    let mut edges = Vec::with_capacity(3 * curve_count);
    let mut bottom_edge_ids = Vec::new();
    let mut top_edge_ids = Vec::new();
    let mut vertical_edge_ids = Vec::new();
    for index in 0..curve_count {
        let [start, end] = bottom[index]
            .domain()
            .or_refuse(KernelStage::Fragment, "domain")?;
        // The TOP curve's own range, which is NOT the bottom's once the winding
        // normalization has reversed one section's curve ORDER: `reverse_section`
        // reverses the order and `reversed()` preserves each curve's domain, so
        // `top[i]` becomes a curve that lived on a different parameter span.
        // Stamping the bottom's span on it made `coedge_sample` evaluate the top
        // curve outside its own domain — pinned at an end while the skin swept
        // the real arc — which on a circular section split into two half-arcs is
        // a deviation of exactly the diameter.
        let [top_start, top_end] = top[index]
            .domain()
            .or_refuse(KernelStage::Fragment, "domain")?;
        let bottom_id = 10 + index as u64;
        let top_id = 10 + curve_count as u64 + index as u64;
        let vertical_id = 10 + 2 * curve_count as u64 + index as u64;
        bottom_edge_ids.push(bottom_id);
        top_edge_ids.push(top_id);
        vertical_edge_ids.push(vertical_id);
        edges.push(EdgeRecord {
            id: bottom_id,
            curve: bottom[index].clone(),
            t0: start,
            t1: end,
            start_vertex_id: index as u64 + 1,
            end_vertex_id: ((index + 1) % curve_count) as u64 + 1,
            degenerate: false,
            name: None,
        });
        edges.push(EdgeRecord {
            id: top_id,
            curve: top[index].clone(),
            t0: top_start,
            t1: top_end,
            start_vertex_id: (curve_count + index) as u64 + 1,
            end_vertex_id: (curve_count + (index + 1) % curve_count) as u64 + 1,
            degenerate: false,
            name: None,
        });
        let vertical = skins[index]
            .iso_curve_u(start)
            .or_refuse(KernelStage::Fragment, "iso_curve")?;
        let [v_start, v_end] = vertical
            .domain()
            .or_refuse(KernelStage::Fragment, "domain")?;
        edges.push(EdgeRecord {
            id: vertical_id,
            curve: vertical,
            t0: v_start,
            t1: v_end,
            start_vertex_id: index as u64 + 1,
            end_vertex_id: (curve_count + index) as u64 + 1,
            degenerate: false,
            name: None,
        });
    }

    profile.lap("edges");
    let mut next_id = 1000u64;
    let mut faces = Vec::with_capacity(curve_count + 2);
    for index in 0..curve_count {
        let [start, end] = bottom[index]
            .domain()
            .or_refuse(KernelStage::Fragment, "domain")?;
        let coedges = vec![
            CoedgeRecord {
                id: next_id,
                edge_id: bottom_edge_ids[index],
                forward: true,
                pcurve: parameter_line(start, 0.0, end, 0.0)
                    .or_refuse(KernelStage::Fragment, "pcurve")?,
            },
            CoedgeRecord {
                id: next_id + 1,
                edge_id: vertical_edge_ids[(index + 1) % curve_count],
                forward: true,
                pcurve: parameter_line(end, 0.0, end, 1.0)
                    .or_refuse(KernelStage::Fragment, "pcurve")?,
            },
            CoedgeRecord {
                id: next_id + 2,
                edge_id: top_edge_ids[index],
                forward: false,
                pcurve: parameter_line(end, 1.0, start, 1.0)
                    .or_refuse(KernelStage::Fragment, "pcurve")?,
            },
            CoedgeRecord {
                id: next_id + 3,
                edge_id: vertical_edge_ids[index],
                forward: false,
                pcurve: parameter_line(start, 1.0, start, 0.0)
                    .or_refuse(KernelStage::Fragment, "pcurve")?,
            },
        ];
        next_id += 4;
        let loop_id = next_id;
        next_id += 1;
        let face_id = next_id;
        next_id += 1;
        faces.push(FaceRecord {
            id: face_id,
            surface: skins[index].clone(),
            same_sense: true,
            loops: vec![LoopRecord {
                id: loop_id,
                coedges,
            }],
            name: None,
        });
    }
    if reversed_winding {
        // The winding normalization reversed the section curve order above.
        // Callers stamp side-face names by INPUT-curve order (the extrude
        // and revolve wall-naming permutation, loft edition) — emit side
        // faces in input order.
        faces.reverse();
    }
    faces.push(cap_face(
        bottom,
        &bottom_edge_ids,
        &first_frame,
        start_advance,
        false,
        &mut next_id,
    )?);
    faces.push(cap_face(
        top,
        &top_edge_ids,
        &last_frame,
        finish_advance,
        true,
        &mut next_id,
    )?);
    let solid = BrepSolid {
        mass_properties_cache: Default::default(),
        cells: Default::default(),
        id: next_id + 1,
        vertices,
        edges,
        shells: vec![ShellRecord { id: next_id, faces }],
        genus: 0,
        kind: crate::BodyKind::Solid,
    };
    profile.lap("topology_pcurves");
    let issues = solid.validate();
    profile.lap("validation");
    if !issues.is_empty() {
        return Err(KernelRefusal::internal(
            KernelStage::Validate,
            "validate",
            format!(
                "Rust loft builder produced invalid topology: {issues:?}"
            ),
        ));
    }
    // `validate()` is an INCIDENCE test: a loft whose walls pass through each
    // other — sections that cross, a run tighter than the profile's own
    // half-width — satisfies every one of its checks. This is the soundness
    // question asked at the lane's single exit, repaired where it can be and
    // refused by name where it cannot. Every path sweep ends here too.
    let accepted = crate::accept_sound(solid, "loftSolid");
    profile.lap("acceptance");
    accepted
}