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
//! Assembly selection resolution (build-spec §5) — a selection reference
//! resolved to an ANALYTIC frame read from the exact BREP surfaces/curves,
//! never from tessellation (the retired app's polyline-PCA approximation is
//! the wart this module kills):
//!
//! - planar face → plane (origin + OUTWARD unit normal, plane-z convention)
//! - cylindrical/conical face → axis (origin + direction), radius when the
//!   radius is constant (a genuine cylinder)
//! - spherical face → center + radius
//! - circular/arc edge → center + axis + radius
//! - straight edge → the INFINITE carrier line
//! - vertex → point
//! - whole component → representative point (aggregate bbox center)
//!
//! Anything without an analytic frame is a typed [`ResolveError`] mapping onto
//! the constraint status vocabulary (`unsupported-selection` /
//! `invalid-selection`), never a panic.
//!
//! # Coordinate space — read this before marshaling mates
//!
//! Every resolver reads the geometry EXACTLY as stored on the given
//! [`BrepSolid`], so the resolved frame is in the SOLID'S OWN space. A
//! component instance whose resident solids are world-posed (the ACOMP feature
//! bakes its placement into the geometry) must pass the component's
//! world→local INVERSE transform to [`SelectionGeometry::transformed`] to
//! obtain the COMPONENT-LOCAL frame the assembly solver's mate inputs require
//! ([`MateKind`](crate::MateKind): `*_a`/`*_b` geometry is local to the owning
//! body, which the solver poses — requirements §4.1).
//!
//! # Lane seam — component lookup
//!
//! [`split_component_namespace`] only PARSES the `ACOMP…:` prefix chain off a
//! namespaced topology name. Mapping that chain to the owning component's
//! resident solids and its world→local inverse transform is the scene
//! component registry's job (build-spec §10 item 2, built in parallel); the
//! resolvers here deliberately take `(solid, entity)` plus a transform
//! argument instead of a registry. Vertices carry no kernel names (the
//! renderer's `VertexRef` convention is owning solid + position), so vertex
//! selections arrive as a position and snap to the nearest topology vertex.

use crate::topology::{BrepSolid, EdgeRecord, FaceRecord};
use crate::{AffineTransform, AnalyticSurface, MateAxis, MatePlane, NurbsCurve, Vec3};
use serde::Serialize;

/// Scale-relative acceptance for the straight/circular edge verification —
/// the analytic-surface recognition tolerance, for the same reason: exact
/// kernel-built geometry verifies at machine precision, anything else fails
/// by orders of magnitude.
const RESOLVE_TOLERANCE: f64 = 1e-9;
/// Scale-relative snap distance for vertex-by-position resolution (positions
/// round-trip through the app as f64 but may be re-serialized).
const VERTEX_SNAP_TOLERANCE: f64 = 1e-6;
/// Circle acceptance samples across the edge's trimmed span.
const CIRCLE_VERIFY_SAMPLES: usize = 16;

// ---------------------------------------------------------------------------
// Result / error types
// ---------------------------------------------------------------------------

/// A resolved analytic selection frame, in the coordinate space of the solid
/// it was resolved from (module docs: pass the component's world→local inverse
/// to [`Self::transformed`] for the solver's component-local mate inputs).
///
/// Cross-lane invariant: [`SelectionGeometry::Axis`]`::radius` is `Some` IFF
/// the face is a genuine constant-radius cylinder — the gate for the
/// `tangent_cylinder_plane` mate. Cones, tori, and general revolution faces
/// resolve with `radius: None`.
#[derive(Clone, Copy, Debug, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum SelectionGeometry {
    /// Planar face: origin on the plane (face boundary AABB center projected
    /// onto it — the `face_frame` convention) + OUTWARD unit normal (face
    /// sense respected, per the one plane-z direction convention).
    Plane { origin: Vec3, normal: Vec3 },
    /// Axis-bearing face (cylinder / cone / torus / general revolution):
    /// a point on the axis + unit direction.
    Axis {
        origin: Vec3,
        direction: Vec3,
        radius: Option<f64>,
    },
    /// Spherical face.
    Sphere { center: Vec3, radius: f64 },
    /// Circular or arc edge; `axis` follows the right-hand rule along the
    /// edge's increasing-parameter direction.
    Circle { center: Vec3, axis: Vec3, radius: f64 },
    /// Straight edge: the INFINITE carrier line (origin = chord midpoint,
    /// unit direction start→end). Distance mates measure against the carrier,
    /// never the clamped finite segment.
    Line { origin: Vec3, direction: Vec3 },
    /// Vertex (or whole-component representative point).
    Point { position: Vec3 },
}

/// Typed resolution failure. [`Self::status`] maps each variant onto the
/// constraint status vocabulary (requirements §5) so the constraint lifecycle
/// reports it without string-matching messages.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub enum ResolveError {
    /// No such entity on the solid (or no vertex within snap distance).
    NotFound { name: String },
    /// The entity exists but carries no analytic frame (freeform surface,
    /// spline edge, degenerate edge).
    Unsupported { name: String, detail: String },
    /// The entity's geometry failed to evaluate.
    Geometry { name: String, detail: String },
}

impl ResolveError {
    /// The constraint-status word this failure maps to.
    pub fn status(&self) -> &'static str {
        match self {
            ResolveError::NotFound { .. } | ResolveError::Geometry { .. } => "invalid-selection",
            ResolveError::Unsupported { .. } => "unsupported-selection",
        }
    }
}

impl std::fmt::Display for ResolveError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ResolveError::NotFound { name } => write!(formatter, "selection '{name}' not found"),
            ResolveError::Unsupported { name, detail } => {
                write!(formatter, "selection '{name}' has no analytic frame: {detail}")
            }
            ResolveError::Geometry { name, detail } => {
                write!(formatter, "selection '{name}' failed to resolve: {detail}")
            }
        }
    }
}

impl SelectionGeometry {
    /// The solver's plane input, for planar-face selections.
    pub fn mate_plane(&self) -> Option<MatePlane> {
        match *self {
            SelectionGeometry::Plane { origin, normal } => Some(MatePlane {
                origin: triple(origin),
                normal: triple(normal),
            }),
            _ => None,
        }
    }

    /// The solver's axis input, for every axis-bearing selection: an axis
    /// face, a circular edge (center + circle axis), or a straight edge's
    /// carrier line.
    pub fn mate_axis(&self) -> Option<MateAxis> {
        let (origin, direction) = match *self {
            SelectionGeometry::Axis {
                origin, direction, ..
            }
            | SelectionGeometry::Line { origin, direction } => (origin, direction),
            SelectionGeometry::Circle { center, axis, .. } => (center, axis),
            _ => return None,
        };
        Some(MateAxis {
            origin: triple(origin),
            direction: triple(direction),
        })
    }

    /// The per-kind anchor point (requirements §4.1 point marshaling): plane /
    /// axis / line origin, sphere / circle center, the point itself.
    pub fn representative_point(&self) -> Vec3 {
        match *self {
            SelectionGeometry::Plane { origin, .. }
            | SelectionGeometry::Axis { origin, .. }
            | SelectionGeometry::Line { origin, .. } => origin,
            SelectionGeometry::Sphere { center, .. }
            | SelectionGeometry::Circle { center, .. } => center,
            SelectionGeometry::Point { position } => position,
        }
    }

    /// Map the frame through `transform` — pass the owning component's
    /// world→local inverse to express a world-resolved frame in the
    /// component-local coordinates the solver's mate inputs require. Rigid /
    /// uniform-similarity matrices only (component poses are rigid by spec):
    /// radii scale by the uniform factor; a shearing or non-uniformly scaling
    /// matrix is refused (it has no analytic image for circles/cylinders).
    pub fn transformed(&self, transform: &AffineTransform) -> Result<Self, ResolveError> {
        let scale = uniform_scale(transform).ok_or_else(|| ResolveError::Unsupported {
            name: "transform".into(),
            detail: "selection frames transform only by rigid/uniform-scale matrices".into(),
        })?;
        let direction = |vector: Vec3| {
            linear(transform, vector)
                .normalized()
                .map_err(|error| ResolveError::Geometry {
                    name: "transform".into(),
                    detail: error,
                })
        };
        Ok(match *self {
            SelectionGeometry::Plane { origin, normal } => SelectionGeometry::Plane {
                origin: transform.point(origin),
                normal: direction(normal)?,
            },
            SelectionGeometry::Axis {
                origin,
                direction: axis,
                radius,
            } => SelectionGeometry::Axis {
                origin: transform.point(origin),
                direction: direction(axis)?,
                radius: radius.map(|radius| radius * scale),
            },
            SelectionGeometry::Sphere { center, radius } => SelectionGeometry::Sphere {
                center: transform.point(center),
                radius: radius * scale,
            },
            SelectionGeometry::Circle {
                center,
                axis,
                radius,
            } => SelectionGeometry::Circle {
                center: transform.point(center),
                axis: direction(axis)?,
                radius: radius * scale,
            },
            SelectionGeometry::Line {
                origin,
                direction: axis,
            } => SelectionGeometry::Line {
                origin: transform.point(origin),
                direction: direction(axis)?,
            },
            SelectionGeometry::Point { position } => SelectionGeometry::Point {
                position: transform.point(position),
            },
        })
    }
}

fn triple(vector: Vec3) -> [f64; 3] {
    [vector.x, vector.y, vector.z]
}

/// The linear (rotation/scale) part of the transform applied to a vector.
fn linear(transform: &AffineTransform, vector: Vec3) -> Vec3 {
    let m = transform.elements;
    Vec3::new(
        m[0] * vector.x + m[1] * vector.y + m[2] * vector.z,
        m[4] * vector.x + m[5] * vector.y + m[6] * vector.z,
        m[8] * vector.x + m[9] * vector.y + m[10] * vector.z,
    )
}

/// The uniform scale factor of a rigid/similarity matrix: columns of equal
/// length AND pairwise orthogonal (equal lengths alone admit shear).
fn uniform_scale(transform: &AffineTransform) -> Option<f64> {
    let m = transform.elements;
    let columns = [
        Vec3::new(m[0], m[4], m[8]),
        Vec3::new(m[1], m[5], m[9]),
        Vec3::new(m[2], m[6], m[10]),
    ];
    let lengths = [
        columns[0].length(),
        columns[1].length(),
        columns[2].length(),
    ];
    let scale = (lengths[0] + lengths[1] + lengths[2]) / 3.0;
    if !(scale > 0.0) {
        return None;
    }
    if lengths
        .iter()
        .any(|length| (length - scale).abs() > 1e-9 * scale)
    {
        return None;
    }
    let orthogonality = 1e-9 * scale * scale;
    if columns[0].dot(columns[1]).abs() > orthogonality
        || columns[1].dot(columns[2]).abs() > orthogonality
        || columns[0].dot(columns[2]).abs() > orthogonality
    {
        return None;
    }
    Some(scale)
}

// ---------------------------------------------------------------------------
// Namespace parsing
// ---------------------------------------------------------------------------

/// Split a possibly-namespaced topology name into its `ACOMP…:` component-id
/// chain (outermost first) and the remaining component-LOCAL name. A segment
/// is peeled only when it matches `ACOMP<digits>` AND is followed by `:`, so a
/// bare whole-component reference stays in the LOCAL position — callers detect
/// it with [`is_component_reference`]:
///
/// - `"ACOMP2:Extrude1|Extrude1_top[0]"` → `(["ACOMP2"], "Extrude1|…")`
/// - `"ACOMP3:ACOMP1:S1:G20"` → `(["ACOMP3", "ACOMP1"], "S1:G20")`
/// - `"ACOMP2"` → `([], "ACOMP2")` (whole-component selection)
/// - `"ACOMP3:ACOMP1"` → `(["ACOMP3"], "ACOMP1")` (nested whole-component)
/// - `"S1:G20"` → `([], "S1:G20")` (sketch-child names never namespace)
pub fn split_component_namespace(name: &str) -> (Vec<&str>, &str) {
    let mut chain = Vec::new();
    let mut local = name;
    while let Some((head, tail)) = local.split_once(':') {
        if !is_component_reference(head) {
            break;
        }
        chain.push(head);
        local = tail;
    }
    (chain, local)
}

/// `ACOMP<digits>` — an ACOMP feature id, i.e. a whole-component selection or
/// one namespace segment of a nested chain.
pub fn is_component_reference(name: &str) -> bool {
    name.strip_prefix("ACOMP")
        .map(|digits| !digits.is_empty() && digits.bytes().all(|byte| byte.is_ascii_digit()))
        .unwrap_or(false)
}

// ---------------------------------------------------------------------------
// Entity resolution
// ---------------------------------------------------------------------------

/// Resolve a component-LOCAL topology name (already namespace-stripped)
/// against one solid: faces first, then edges. Names never collide across the
/// two kinds under the deterministic-naming scheme; the order only settles
/// pathological ties.
pub fn resolve_named_selection(
    solid: &BrepSolid,
    name: &str,
) -> Result<SelectionGeometry, ResolveError> {
    if let Some(record) = solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .find(|face| face.name.as_deref() == Some(name))
    {
        return resolve_face_record(solid, record);
    }
    if let Some(record) = solid
        .edges
        .iter()
        .find(|edge| edge.name.as_deref() == Some(name))
    {
        return resolve_edge_record(record);
    }
    Err(ResolveError::NotFound { name: name.into() })
}

/// Resolve a face by topology id (the scene-map `FaceRef` lane: the caller
/// already resolved the selection name to `(handle, face_id)`).
pub fn resolve_face_selection(
    solid: &BrepSolid,
    face_id: u64,
) -> Result<SelectionGeometry, ResolveError> {
    let record = solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .find(|face| face.id == face_id)
        .ok_or_else(|| ResolveError::NotFound {
            name: format!("face {face_id}"),
        })?;
    resolve_face_record(solid, record)
}

/// Resolve an edge by topology id (the `EdgeRef` lane).
pub fn resolve_edge_selection(
    solid: &BrepSolid,
    edge_id: u64,
) -> Result<SelectionGeometry, ResolveError> {
    let record = solid
        .edges
        .iter()
        .find(|edge| edge.id == edge_id)
        .ok_or_else(|| ResolveError::NotFound {
            name: format!("edge {edge_id}"),
        })?;
    resolve_edge_record(record)
}

/// Resolve a vertex selection by position (vertices have no kernel names; the
/// renderer references them by owning solid + position). Nearest topology
/// vertex wins — deterministic when two vertices are close — then the
/// scale-relative snap tolerance gates acceptance; the returned point is the
/// EXACT vertex position, not the query.
pub fn resolve_vertex_selection(
    solid: &BrepSolid,
    position: Vec3,
) -> Result<SelectionGeometry, ResolveError> {
    let mut best: Option<(f64, Vec3)> = None;
    for vertex in &solid.vertices {
        let distance = vertex.point.sub(position).length();
        if best
            .map(|(best_distance, _)| distance < best_distance)
            .unwrap_or(true)
        {
            best = Some((distance, vertex.point));
        }
    }
    match best {
        Some((distance, point))
            if distance <= VERTEX_SNAP_TOLERANCE * crate::solid_scale(solid) =>
        {
            Ok(SelectionGeometry::Point { position: point })
        }
        _ => Err(ResolveError::NotFound {
            name: format!(
                "vertex near ({}, {}, {})",
                position.x, position.y, position.z
            ),
        }),
    }
}

/// Whole-component representative point: the center of the aggregate AABB of
/// every face surface's control-point hull (plus topology vertices) across the
/// component's solids. The hull BOUNDS the exact surfaces (convex-hull
/// property) without touching tessellation; it overshoots curved faces, so
/// this is a representative anchor for coincident/parallel-with-component
/// semantics, not an exact bounding box.
pub fn resolve_component_point(solids: &[&BrepSolid]) -> Result<SelectionGeometry, ResolveError> {
    let mut low = Vec3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY);
    let mut high = Vec3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
    let mut any = false;
    let mut include = |point: Vec3| {
        low = Vec3::new(low.x.min(point.x), low.y.min(point.y), low.z.min(point.z));
        high = Vec3::new(high.x.max(point.x), high.y.max(point.y), high.z.max(point.z));
        any = true;
    };
    for solid in solids {
        for vertex in &solid.vertices {
            include(vertex.point);
        }
        for shell in &solid.shells {
            for face in &shell.faces {
                for row in &face.surface.control_points {
                    for control in row {
                        let point = control.point().map_err(|error| ResolveError::Geometry {
                            name: "component".into(),
                            detail: error,
                        })?;
                        include(point);
                    }
                }
            }
        }
    }
    if !any {
        return Err(ResolveError::Geometry {
            name: "component".into(),
            detail: "component has no geometry to anchor".into(),
        });
    }
    Ok(SelectionGeometry::Point {
        position: low.add(high).scale(0.5),
    })
}

// ---------------------------------------------------------------------------
// Face resolution
// ---------------------------------------------------------------------------

fn face_label(record: &FaceRecord) -> String {
    record
        .name
        .clone()
        .unwrap_or_else(|| format!("face {}", record.id))
}

fn resolve_face_record(
    solid: &BrepSolid,
    record: &FaceRecord,
) -> Result<SelectionGeometry, ResolveError> {
    match record.surface.analytic() {
        Some(AnalyticSurface::RuledRevolution {
            frame, rho0, rho1, ..
        }) => {
            // Meridional generatrix (reconstruction-verified), so equal end
            // radii already mean a constant radius: a genuine cylinder.
            let span = rho0.abs().max(rho1.abs());
            let radius = ((rho0 - rho1).abs() <= RESOLVE_TOLERANCE * (1.0 + span))
                .then_some(0.5 * (rho0 + rho1));
            Ok(SelectionGeometry::Axis {
                origin: frame.origin,
                direction: frame.axis,
                radius,
            })
        }
        Some(AnalyticSurface::Sphere { frame, radius }) => Ok(SelectionGeometry::Sphere {
            center: frame.origin,
            radius: *radius,
        }),
        Some(AnalyticSurface::Torus { frame, .. }) => Ok(SelectionGeometry::Axis {
            origin: frame.origin,
            direction: frame.axis,
            radius: None,
        }),
        Some(AnalyticSurface::Revolution {
            frame, generatrix, ..
        }) => Ok(SelectionGeometry::Axis {
            origin: frame.origin,
            direction: frame.axis,
            radius: revolution_cylinder_radius(frame, generatrix),
        }),
        // Exact plane, or unrecognized (imported) geometry that may still be
        // planar — one shared sampled-planarity lane, `face_frame` parity.
        Some(AnalyticSurface::Plane { .. }) | None => planar_face(solid, record),
    }
}

/// `Some(radius)` when a general-revolution generatrix is a straight line at
/// CONSTANT radial distance from the axis — a partial cylinder (e.g. a 180°
/// revolve wall). Equal END radii alone are not enough: a line skew to the
/// axis revolves into a hyperboloid whose radius dips between equal ends. But
/// squared radial distance along a straight line is a QUADRATIC function of
/// arc length, so three equal samples (ends + midpoint) force it constant.
fn revolution_cylinder_radius(
    frame: &crate::RevolutionFrame,
    generatrix: &NurbsCurve,
) -> Option<f64> {
    if !control_net_is_colinear(generatrix) {
        return None;
    }
    let [t0, t1] = generatrix.domain().ok()?;
    let radial = |t: f64| -> Option<f64> {
        let point = generatrix.evaluate(t).ok()?;
        let delta = point.sub(frame.origin);
        Some(delta.sub(frame.axis.scale(delta.dot(frame.axis))).length())
    };
    let rho0 = radial(t0)?;
    let rho_mid = radial(0.5 * (t0 + t1))?;
    let rho1 = radial(t1)?;
    let span = rho0.abs().max(rho1.abs());
    let tolerance = RESOLVE_TOLERANCE * (1.0 + span);
    ((rho0 - rho1).abs() <= tolerance && (rho_mid - rho0).abs() <= tolerance)
        .then_some((rho0 + rho_mid + rho1) / 3.0)
}

/// Planar-face resolution: sampled-constant-normal acceptance (the established
/// `face_frame` planarity test, densified to a 3×3 grid), OUTWARD normal via
/// the face sense, origin = boundary AABB center projected onto the plane.
fn planar_face(solid: &BrepSolid, record: &FaceRecord) -> Result<SelectionGeometry, ResolveError> {
    let geometry = (|| -> Result<Option<SelectionGeometry>, String> {
        let [u0, u1] = record.surface.domain_u()?;
        let [v0, v1] = record.surface.domain_v()?;
        let (um, vm) = ((u0 + u1) * 0.5, (v0 + v1) * 0.5);
        let center_normal = record.surface.normal(um, vm)?;
        for u in [u0, um, u1] {
            for v in [v0, vm, v1] {
                if record.surface.normal(u, v)?.dot(center_normal) < 1.0 - 1e-6 {
                    return Ok(None);
                }
            }
        }
        let normal = if record.same_sense {
            center_normal
        } else {
            center_normal.scale(-1.0)
        }
        .normalized()?;

        // Boundary AABB center from the face loops' non-degenerate edges,
        // falling back to the patch midpoint for loop-less faces.
        let mut low = Vec3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY);
        let mut high = Vec3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
        let mut any = false;
        for loop_record in &record.loops {
            for coedge in &loop_record.coedges {
                let Some(edge) = solid.edges.iter().find(|edge| edge.id == coedge.edge_id) else {
                    continue;
                };
                if edge.degenerate {
                    continue;
                }
                for step in 0..=4 {
                    let t = edge.t0 + (edge.t1 - edge.t0) * (step as f64 / 4.0);
                    let point = edge.curve.evaluate(t)?;
                    low = Vec3::new(low.x.min(point.x), low.y.min(point.y), low.z.min(point.z));
                    high = Vec3::new(high.x.max(point.x), high.y.max(point.y), high.z.max(point.z));
                    any = true;
                }
            }
        }
        let plane_point = record.surface.evaluate(um, vm)?;
        let center = if any {
            low.add(high).scale(0.5)
        } else {
            plane_point
        };
        // Project onto the plane so the origin lies exactly on it.
        let signed = center.sub(plane_point).dot(normal);
        let origin = center.sub(normal.scale(signed));
        Ok(Some(SelectionGeometry::Plane { origin, normal }))
    })();
    match geometry {
        Ok(Some(frame)) => Ok(frame),
        Ok(None) => Err(ResolveError::Unsupported {
            name: face_label(record),
            detail: "face carries no analytic frame (freeform surface)".into(),
        }),
        Err(error) => Err(ResolveError::Geometry {
            name: face_label(record),
            detail: error,
        }),
    }
}

// ---------------------------------------------------------------------------
// Edge resolution
// ---------------------------------------------------------------------------

fn edge_label(record: &EdgeRecord) -> String {
    record
        .name
        .clone()
        .unwrap_or_else(|| format!("edge {}", record.id))
}

fn resolve_edge_record(record: &EdgeRecord) -> Result<SelectionGeometry, ResolveError> {
    if record.degenerate {
        return Err(ResolveError::Unsupported {
            name: edge_label(record),
            detail: "degenerate edge".into(),
        });
    }
    match edge_geometry(record) {
        Ok(Some(frame)) => Ok(frame),
        Ok(None) => Err(ResolveError::Unsupported {
            name: edge_label(record),
            detail: "edge is neither straight nor circular".into(),
        }),
        Err(error) => Err(ResolveError::Geometry {
            name: edge_label(record),
            detail: error,
        }),
    }
}

fn edge_geometry(record: &EdgeRecord) -> Result<Option<SelectionGeometry>, String> {
    if let Some(line) = straight_edge_line(record)? {
        return Ok(Some(line));
    }
    circular_edge_circle(record)
}

/// The whole curve's extent, for scale-relative acceptance.
fn control_net_extent(curve: &NurbsCurve) -> f64 {
    let mut extent = 0.0_f64;
    for control in &curve.control_points {
        if let Ok(point) = control.point() {
            extent = extent
                .max(point.x.abs())
                .max(point.y.abs())
                .max(point.z.abs());
        }
    }
    extent
}

/// A colinear control net is EXACT proof of a straight curve (convex-hull
/// property), and every straight curve this kernel builds — `make_line`
/// products through any split/degree change — keeps its net colinear.
fn control_net_is_colinear(curve: &NurbsCurve) -> bool {
    let points: Vec<Vec3> = curve
        .control_points
        .iter()
        .filter_map(|control| control.point().ok())
        .collect();
    let Some((&first, rest)) = points.split_first() else {
        return false;
    };
    let tolerance = RESOLVE_TOLERANCE * (1.0 + control_net_extent(curve));
    let Some(direction) = rest
        .iter()
        .map(|point| point.sub(first))
        .max_by(|a, b| a.length().total_cmp(&b.length()))
        .and_then(|chord| chord.normalized().ok())
    else {
        return false;
    };
    points.iter().all(|point| {
        let offset = point.sub(first);
        offset.sub(direction.scale(offset.dot(direction))).length() <= tolerance
    })
}

/// Straight edge → its infinite carrier line, oriented start→end over the
/// trimmed span, origin at the chord midpoint.
fn straight_edge_line(record: &EdgeRecord) -> Result<Option<SelectionGeometry>, String> {
    if !control_net_is_colinear(&record.curve) {
        return Ok(None);
    }
    let start = record.curve.evaluate(record.t0)?;
    let end = record.curve.evaluate(record.t1)?;
    let chord = end.sub(start);
    if chord.length() <= RESOLVE_TOLERANCE * (1.0 + control_net_extent(&record.curve)) {
        // Zero-length trim of a straight curve — nothing to orient by.
        return Ok(None);
    }
    Ok(Some(SelectionGeometry::Line {
        origin: start.add(end).scale(0.5),
        direction: chord.normalized()?,
    }))
}

/// Circular/arc edge → center + axis + radius: a candidate circle from three
/// interior samples (distinct even on a closed full circle), then VERIFIED at
/// [`CIRCLE_VERIFY_SAMPLES`] points — equidistant from the center and coplanar
/// — so ellipses, splines, and helices fail cleanly while exact kernel arcs
/// pass at machine precision.
fn circular_edge_circle(record: &EdgeRecord) -> Result<Option<SelectionGeometry>, String> {
    let span = record.t1 - record.t0;
    if !(span > 0.0) {
        return Ok(None);
    }
    let at = |fraction: f64| record.curve.evaluate(record.t0 + span * fraction);
    let (a, b, c) = (at(1.0 / 6.0)?, at(0.5)?, at(5.0 / 6.0)?);
    let Some(center) = crate::analytic_surface::circumcenter(a, b, c) else {
        return Ok(None);
    };
    // Right-hand rule along the increasing-parameter direction.
    let Ok(axis) = b.sub(a).cross(c.sub(b)).normalized() else {
        return Ok(None);
    };
    let radius = a.sub(center).length();
    let tolerance = RESOLVE_TOLERANCE * (1.0 + control_net_extent(&record.curve));
    for step in 0..=CIRCLE_VERIFY_SAMPLES {
        let point = at(step as f64 / CIRCLE_VERIFY_SAMPLES as f64)?;
        let delta = point.sub(center);
        if (delta.length() - radius).abs() > tolerance || delta.dot(axis).abs() > tolerance {
            return Ok(None);
        }
    }
    Ok(Some(SelectionGeometry::Circle {
        center,
        axis,
        radius,
    }))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

// BREP private tests: 813a3962021020ed