ifc-geometry 0.4.4

IFC semantic views lowered into the format-neutral geometry DAG.
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
//! Exact curve lowering.
//!
//! # Scope
//!
//! Covers `IfcPolyline`, `IfcLine`, `IfcCircle`, `IfcTrimmedCurve`,
//! `IfcCompositeCurve`, and the explicit-knot
//! `IfcBSplineCurveWithKnots` / `IfcRationalBSplineCurveWithKnots`
//! subtypes. Convention-only `IfcBSplineCurve` and other families report a
//! typed `Unsupported` naming the entity, so a gap is a diagnostic rather
//! than a wrong shape.
//!
//! # Trim parameters are not all lengths
//!
//! `IfcTrimmedCurve` carries `IfcParameterValue`s in the *basis curve's own*
//! parameterisation. For an `IfcLine` that parameter is a length along the
//! direction vector and scales with the length unit. For an `IfcCircle` it is
//! an **angle** in the model's plane-angle unit. Applying the length factor to
//! a conic parameter silently rescales every arc: on a millimetre file the
//! 0.082 rad arc in `swept_disk_composite_arc_crankbar.ifc` would become 82
//! radians and wrap the circle thirteen times. The basis curve therefore
//! decides which unit conversion applies.
//!
//! # Why the frame is applied here and not deferred
//!
//! Curves are lowered as world-space geometry, matching `lower::brep`: the
//! caller's frame is applied to points and to conic frames as they are built.
//! Deferring would require every consumer to carry a parallel transform stack.

use axiolid_core::{Frame3, Point3, Vec3};
use axiolid_curve::{BSplineCurve3, Circle3, Curve3, Ellipse3, KnotSpec, Line3, Polyline3};
use axiolid_model::{
    CurveRelation, CurveSegment, GeometryNode, MasterRepresentation, NodeId, SurfaceSides,
    Transition, TrimSelector, TrimmingPreference as KernelPreference,
};
use ifc_model::EntityId;

use crate::curve::bspline::{BSplineCurve, KnotType};
use crate::curve::composite::{CompositeCurve, CompositeCurveSegment, TransitionCode};
use crate::curve::conic::{Circle, Ellipse};
use crate::curve::line::Line;
use crate::curve::offset::{
    OffsetCurve2D, OffsetCurve3D, PCurve, PreferredSurfaceCurveRepresentation, SurfaceCurve,
};
use crate::curve::polyline::{IndexedPolyCurve, PolySegment, Polyline};
use crate::curve::trimmed::{TrimmedCurve, TrimmingPreference};
use crate::error::GeometryResult;
use crate::lower::session::LoweringSession;
use crate::lower::surface::lower_surface_node;
use crate::resource::direction::resolve_unit;
use crate::resource::placement::axis_placement_transform;
use crate::resource::point::{CartesianPoint, CartesianPointList2D, CartesianPointList3D};
use crate::transform::Transform;

mod composite_range;
mod parameter_space;

use parameter_space::parameter_reference_curve;

/// Family label used for curve memoization.
const KIND: &str = "curve";

/// Lower a sweep's directrix together with its `StartParam`/`EndParam`.
///
/// Returns the directrix node and the `parameter_range` the kernel should
/// apply to it. The range is in the directrix's own parameterisation, which
/// is not always a length:
///
/// - a conic parameter is an angle, a line or polyline parameter is
///   dimensionless: converted by [`scale_parameter`] and handed on;
/// - an `IfcCompositeCurve` accumulates each segment's PARAMETRIC length
///   (1 per polyline edge, an arc's angle), which no kernel arc length
///   matches. The range is applied here, structurally, and the returned range
///   is `None`.
///
/// One end without the other is refused: a half-open sweep is undefined.
pub(crate) fn lower_sweep_directrix(
    session: &mut LoweringSession<'_>,
    sweep: EntityId,
    sweep_type: &str,
    directrix: EntityId,
    frame: Transform,
    start: Option<f64>,
    end: Option<f64>,
) -> GeometryResult<(NodeId, Option<(f64, f64)>)> {
    let range = match (start, end) {
        (Some(start), Some(end)) => Some((start, end)),
        (None, None) => None,
        _ => {
            return Err(session.degenerate(
                sweep,
                sweep_type,
                "only one of StartParam/EndParam is present; a half-open sweep is undefined",
            ))
        }
    };
    let kind = session.type_name(directrix)?;
    if composite_range::is_composite(&kind) {
        let node = composite_range::lower_composite_directrix(
            session, sweep, sweep_type, directrix, frame, range,
        )?;
        return Ok((node, None));
    }
    let node = lower_curve_node(session, directrix, frame)?;
    // A trimmed curve is parameterised by its BASIS (ISO 10303-42): a trimmed
    // circle's range is an angle, a trimmed line's a vector multiple.
    let parameter_kind = if kind == "IFCTRIMMEDCURVE" {
        let entity = session.entity(sweep, directrix)?;
        let basis = TrimmedCurve::new(directrix, entity).basis_curve_ref()?;
        session.type_name(basis)?
    } else {
        kind
    };
    let converted = range.map(|(start, end)| {
        (
            scale_parameter(session, &parameter_kind, start),
            scale_parameter(session, &parameter_kind, end),
        )
    });
    Ok((node, converted))
}

/// Lower any supported curve, returning its node.
pub fn lower_curve_node(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    if let Some(node) = session.memoized(id, KIND, frame) {
        return Ok(node);
    }
    session.enter(id, KIND)?;
    let result = build(session, id, frame);
    session.exit(id);
    let node = result?;
    session.memoize(id, KIND, frame, node);
    Ok(node)
}

fn build(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let type_name = session.type_name(id)?;
    match type_name.as_str() {
        "IFCPOLYLINE" => polyline(session, id, frame),
        "IFCINDEXEDPOLYCURVE" => indexed_polycurve(session, id, frame),
        "IFCLINE" => line(session, id, frame),
        "IFCCIRCLE" => circle(session, id, frame),
        "IFCELLIPSE" => ellipse(session, id, frame),
        "IFCOFFSETCURVE2D" | "IFCOFFSETCURVE3D" => offset(session, id, frame),
        "IFCPCURVE" => parameter_curve(session, id, frame),
        "IFCSURFACECURVE" | "IFCINTERSECTIONCURVE" | "IFCSEAMCURVE" => {
            surface_curve(session, id, frame)
        }
        "IFCTRIMMEDCURVE" => trimmed(session, id, frame),
        "IFCCOMPOSITECURVE"
        | "IFCCOMPOSITECURVEONSURFACE"
        | "IFCBOUNDARYCURVE"
        | "IFCOUTERBOUNDARYCURVE" => composite(session, id, frame),
        "IFCBSPLINECURVEWITHKNOTS" | "IFCRATIONALBSPLINECURVEWITHKNOTS" => {
            bspline(session, id, frame)
        }
        other => Err(session.unsupported(id, other, "curve family")),
    }
}

/// `IfcPolyline`: ordered points, closed when the last repeats the first.
fn polyline(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let entity = session.entity(id, id)?;
    let view = Polyline::new(id, entity);
    let closed = view.closes_by_repeating_first_point()?;
    let refs = view.point_refs()?;

    let mut points = Vec::with_capacity(refs.len());
    for point_ref in &refs {
        points.push(Point3::from_array(world_point(
            session, id, *point_ref, frame,
        )?));
    }
    // A closing duplicate is represented by the `closed` flag, not by a
    // repeated vertex: leaving both produces a zero-length final segment.
    if closed && points.len() > 1 {
        points.pop();
    }
    session.node_for(
        id,
        GeometryNode::Curve3(Curve3::Polyline(Polyline3 { points, closed })),
    )
}

/// Lower indexed line and arc segments against one shared point list.
fn indexed_polycurve(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let entity = session.entity(id, id)?;
    let view = IndexedPolyCurve::new(id, entity);
    let point_list_ref = view.points_ref()?;
    let points = indexed_points(session, id, point_list_ref, frame)?;
    let explicit = view.has_explicit_segments();
    let segments = view.segments(points.len())?;

    if !explicit {
        return session.node_for(
            id,
            GeometryNode::Curve3(Curve3::Polyline(Polyline3 {
                points,
                closed: false,
            })),
        );
    }

    let mut children = Vec::with_capacity(segments.len());
    for segment in segments {
        let curve = match segment {
            PolySegment::Line(indices) => {
                let closed = indices.first() == indices.last();
                let mut selected: Vec<_> = indices.into_iter().map(|i| points[i]).collect();
                if closed && selected.len() > 1 {
                    selected.pop();
                }
                session.node_for(
                    id,
                    GeometryNode::Curve3(Curve3::Polyline(Polyline3 {
                        points: selected,
                        closed,
                    })),
                )?
            }
            PolySegment::Arc { start, mid, end } => {
                indexed_arc(session, id, points[start], points[mid], points[end])?
            }
        };
        children.push(CurveSegment {
            curve,
            same_sense: true,
            transition: Transition::Continuous,
        });
    }
    session.node_for(
        id,
        GeometryNode::CurveRelation(CurveRelation::Composite { segments: children }),
    )
}

fn indexed_points(
    session: &LoweringSession<'_>,
    owner: EntityId,
    list_id: EntityId,
    frame: Transform,
) -> GeometryResult<Vec<Point3>> {
    let entity = session.entity(owner, list_id)?;
    let raw: Vec<[f64; 3]> = match entity.type_name.to_ascii_uppercase().as_str() {
        "IFCCARTESIANPOINTLIST2D" => CartesianPointList2D::new(list_id, entity)
            .coordinates()?
            .into_iter()
            .map(|p| [p[0], p[1], 0.0])
            .collect(),
        "IFCCARTESIANPOINTLIST3D" => CartesianPointList3D::new(list_id, entity).coordinates()?,
        other => {
            return Err(session.unsupported(
                list_id,
                other,
                "indexed curve requires IfcCartesianPointList2D or IfcCartesianPointList3D",
            ));
        }
    };
    if raw.len() < 2 {
        return Err(session.degenerate(
            list_id,
            &entity.type_name,
            "point list needs at least two points",
        ));
    }
    raw.into_iter()
        .map(|p| {
            let metres = p.map(|value| session.units().length(value));
            if !metres.iter().all(|value| value.is_finite()) {
                return Err(session.degenerate(
                    list_id,
                    &entity.type_name,
                    "coordinates must be finite",
                ));
            }
            Ok(Point3::from_array(frame.apply(metres)))
        })
        .collect()
}

mod indexed;

use indexed::indexed_arc;

/// `IfcLine`: origin point plus an `IfcVector` direction.
///
/// The vector's magnitude is the parameterisation scale and is preserved:
/// normalizing it would silently reparameterise every trim taken on the line.
fn line(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let entity = session.entity(id, id)?;
    let view = Line::new(id, entity);
    let origin = world_point(session, id, view.point_ref()?, frame)?;
    let vector_ref = view.direction_vector_ref()?;
    let direction = world_vector(session, id, vector_ref, frame)?;
    session.node_for(
        id,
        GeometryNode::Curve3(Curve3::Line(Line3 {
            origin: Point3::from_array(origin),
            direction: Vec3::from_array(direction),
        })),
    )
}

/// `IfcCircle`: a radius in the XY plane of its own placement.
fn circle(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let entity = session.entity(id, id)?;
    let view = Circle::new(id, entity);
    let radius = session.units().length(view.radius()?);
    let position_ref = view.position_ref()?;
    let position = session.entity(id, position_ref)?;
    let local = axis_placement_transform(session.model(), position_ref, position)?
        .to_metres(session.units());
    let placed = frame.compose(&local);
    session.node_for(
        id,
        GeometryNode::Curve3(Curve3::Circle(Circle3 {
            frame: frame3(&placed),
            radius,
        })),
    )
}

/// `IfcEllipse`: exact semi-axes in the placed conic frame.
fn ellipse(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let entity = session.entity(id, id)?;
    let view = Ellipse::new(id, entity);
    let semi_axis_x = session.units().length(view.semi_axis_1()?);
    let semi_axis_y = session.units().length(view.semi_axis_2()?);
    if !semi_axis_x.is_finite()
        || !semi_axis_y.is_finite()
        || semi_axis_x <= 0.0
        || semi_axis_y <= 0.0
    {
        return Err(session.degenerate(id, "IFCELLIPSE", "semi-axes must be finite and positive"));
    }
    let position_ref = view.position_ref()?;
    let position = session.entity(id, position_ref)?;
    let local = axis_placement_transform(session.model(), position_ref, position)?
        .to_metres(session.units());
    let placed = frame.compose(&local);
    session.node_for(
        id,
        GeometryNode::Curve3(Curve3::Ellipse(Ellipse3 {
            frame: frame3(&placed),
            semi_axis_x,
            semi_axis_y,
        })),
    )
}

/// IFC offset curves remain graph relations over the exact basis curve.
fn offset(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let entity = session.entity(id, id)?;
    let type_name = session.type_name(id)?;
    let (basis_ref, raw_distance, reference_direction) = match type_name.as_str() {
        "IFCOFFSETCURVE2D" => {
            let view = OffsetCurve2D::new(id, entity);
            (view.basis_curve_ref()?, view.distance()?, None)
        }
        "IFCOFFSETCURVE3D" => {
            let view = OffsetCurve3D::new(id, entity);
            let direction_ref = view.ref_direction_ref()?;
            let direction = resolve_unit(session.model(), id, direction_ref)?;
            (
                view.basis_curve_ref()?,
                view.distance()?,
                Some(Vec3::from_array(frame.apply_direction(direction))),
            )
        }
        _ => unreachable!("offset called only for offset curve families"),
    };
    let distance = session.units().length(raw_distance);
    if !distance.is_finite() {
        return Err(session.degenerate(id, &type_name, "Distance must be finite"));
    }
    let basis = lower_curve_node(session, basis_ref, frame)?;
    session.node_for(
        id,
        GeometryNode::CurveRelation(CurveRelation::Offset {
            basis,
            distance,
            reference_direction,
        }),
    )
}

/// Lower an IFC p-curve without applying project length units to `(u, v)`.
fn parameter_curve(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let entity = session.entity(id, id)?;
    let view = PCurve::new(id, entity);
    let basis_ref = view.basis_surface_ref()?;
    let reference_ref = view.reference_curve_ref()?;
    let basis_surface = lower_surface_node(session, basis_ref, frame)?;
    let reference_curve = parameter_reference_curve(session, id, reference_ref)?;
    session.node_for(
        id,
        GeometryNode::CurveRelation(CurveRelation::ParameterCurve {
            basis_surface,
            reference_curve,
        }),
    )
}

/// Preserve redundant 3D/p-curve geometry and its authoritative selection.
fn surface_curve(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let entity = session.entity(id, id)?;
    let view = SurfaceCurve::new(id, entity);
    let curve_3d_ref = view.curve_3d_ref()?;
    let pcurve_refs = view.associated_pcurve_refs()?;
    let preferred = view.master_representation();

    // The neutral master names a SIDE, and a side is a (surface, p-curve)
    // pair, so PCURVE_S2 is only meaningful when a second side exists. A file
    // naming it with one p-curve is inconsistent per the schema rather than
    // merely unsupported, so it is refused as degenerate.
    let master = match preferred {
        PreferredSurfaceCurveRepresentation::Curve3D => MasterRepresentation::Curve3d,
        PreferredSurfaceCurveRepresentation::PCurveS1 => MasterRepresentation::ParameterCurveS1,
        PreferredSurfaceCurveRepresentation::PCurveS2 if pcurve_refs.len() == 2 => {
            MasterRepresentation::ParameterCurveS2
        }
        PreferredSurfaceCurveRepresentation::PCurveS2 => {
            return Err(session.degenerate(
                id,
                &session.type_name(id)?,
                "MasterRepresentation names PCURVE_S2 but AssociatedGeometry holds \
                 one p-curve, so the named side does not exist",
            ));
        }
    };

    let curve_3d = lower_curve_node(session, curve_3d_ref, frame)?;
    // Each side pairs the surface with this curve's image in that surface's
    // own parameter domain. The pairing comes from the IfcPcurve itself, so
    // it is read rather than inferred from list order alone.
    let mut sides = Vec::with_capacity(pcurve_refs.len());
    for pcurve_ref in pcurve_refs {
        let basis_ref =
            PCurve::new(pcurve_ref, session.entity(id, pcurve_ref)?).basis_surface_ref()?;
        let surface = lower_surface_node(session, basis_ref, frame)?;
        let pcurve = parameter_curve(session, pcurve_ref, frame)?;
        sides.push((surface, pcurve));
    }
    let sides = match sides.as_slice() {
        [(s1, p1)] => SurfaceSides::one(*s1, *p1),
        [(s1, p1), (s2, p2)] => SurfaceSides::two(*s1, *p1, *s2, *p2),
        _ => unreachable!("associated_pcurve_refs already bounds the list to 1 or 2"),
    };
    session.node_for(
        id,
        GeometryNode::CurveRelation(CurveRelation::SurfaceCurve {
            curve_3d,
            sides,
            master,
        }),
    )
}

/// `IfcTrimmedCurve`: a basis curve plus two trim selectors.
///
/// The basis curve decides how a parameter selector is scaled: length for a
/// line, plane angle for a conic. See the module note.
fn trimmed(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let entity = session.entity(id, id)?;
    let view = TrimmedCurve::new(id, entity);
    let basis_ref = view.basis_curve_ref()?;
    let basis = lower_curve_node(session, basis_ref, frame)?;

    let basis_kind = session.type_name(basis_ref)?;
    let sense_agreement = view.sense_agreement()?;
    let preference = view.master_representation();
    let spec = view.spec()?;
    let (t1, t2) = spec.endpoints();

    let start = selectors(session, id, t1, &basis_kind, frame)?;
    let end = selectors(session, id, t2, &basis_kind, frame)?;
    if start.is_empty() || end.is_empty() {
        return Err(session.degenerate(
            id,
            "IFCTRIMMEDCURVE",
            "a trim end carries neither a parameter nor a point",
        ));
    }

    session.node_for(
        id,
        GeometryNode::CurveRelation(CurveRelation::Trimmed {
            basis,
            start,
            end,
            sense_agreement,
            preference: match preference {
                TrimmingPreference::Cartesian => KernelPreference::Cartesian,
                TrimmingPreference::Parameter => KernelPreference::Parameter,
                TrimmingPreference::Unspecified => KernelPreference::Unspecified,
            },
        }),
    )
}

/// Convert one trim end into kernel selectors, scaling by basis curve kind.
fn selectors(
    session: &mut LoweringSession<'_>,
    owner: EntityId,
    trim: crate::curve::trimmed::Trim,
    basis_kind: &str,
    frame: Transform,
) -> GeometryResult<Vec<TrimSelector>> {
    let mut out = Vec::new();
    if let Some(raw) = trim.parameter {
        out.push(TrimSelector::Parameter(scale_parameter(
            session, basis_kind, raw,
        )));
    }
    if let Some(point_ref) = trim.cartesian {
        let placed = world_point(session, owner, point_ref, frame)?;
        out.push(TrimSelector::Point3(Point3::from_array(placed)));
    }
    Ok(out)
}

/// Scale one trim parameter into kernel units, by basis curve kind.
///
/// Three parameterisations, not two:
///
/// - A conic (`IfcCircle`, `IfcEllipse`) parameterises by ANGLE.
/// - A line parameter multiplies its already-scaled `IfcVector`, so it is
///   dimensionless. A polyline parameter is a point/segment ordinal.
/// - A composite curve parameter is cumulative path LENGTH and converts.
/// - Other non-conic families parameterise by length and convert.
pub(crate) fn scale_parameter(session: &LoweringSession<'_>, basis_kind: &str, raw: f64) -> f64 {
    match basis_kind {
        "IFCCIRCLE" | "IFCELLIPSE" => session.units().angle(raw),
        "IFCLINE" | "IFCPOLYLINE" => raw,
        _ => session.units().length(raw),
    }
}

/// `IfcCompositeCurve`: ordered segments, each wrapping a parent curve.
fn composite(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let entity = session.entity(id, id)?;
    let view = CompositeCurve::new(id, entity);
    let segment_refs = view.segment_refs()?;
    if segment_refs.is_empty() {
        return Err(session.degenerate(id, "IFCCOMPOSITECURVE", "no segments"));
    }

    let mut segments = Vec::with_capacity(segment_refs.len());
    for segment_ref in &segment_refs {
        let segment_entity = session.entity(id, *segment_ref)?;
        let segment = CompositeCurveSegment::new(*segment_ref, segment_entity);
        let parent = segment.parent_curve_ref()?;
        // Segment order is the curve's own traversal order and must be
        // preserved: sorting or deduplicating segments reorders the path.
        segments.push(CurveSegment {
            curve: lower_curve_node(session, parent, frame)?,
            same_sense: segment.same_sense()?,
            transition: transition(segment.transition()?),
        });
    }
    session.node_for(
        id,
        GeometryNode::CurveRelation(CurveRelation::Composite { segments }),
    )
}

/// Exact explicit-knot polynomial/rational B-spline curve.
fn bspline(
    session: &mut LoweringSession<'_>,
    id: EntityId,
    frame: Transform,
) -> GeometryResult<NodeId> {
    let type_name = session.type_name(id)?;
    let entity = session.entity(id, id)?;
    let view = BSplineCurve::new(id, entity);
    let degree = u16::try_from(view.degree()?)
        .map_err(|_| session.degenerate(id, &type_name, "Degree exceeds u16"))?;
    let knots = view.knots()?.ok_or_else(|| {
        session.unsupported(
            id,
            &type_name,
            "explicit knots required for lossless lowering",
        )
    })?;
    finite_values(session, id, &type_name, "Knots", &knots.values)?;
    // The expanded knot vector is what a kernel materializes, and its length is
    // the SUM of file-declared multiplicities: a handful of integers can name
    // billions of elements. Check the declared total before anything reserves.
    let declared: u128 = knots.multiplicities.iter().map(|m| *m as u128).sum();
    session.check_aggregate(id, &type_name, "knot multiplicities", declared)?;
    let multiplicities = knots
        .multiplicities
        .into_iter()
        .map(|value| {
            u32::try_from(value)
                .map_err(|_| session.degenerate(id, &type_name, "multiplicity exceeds u32"))
        })
        .collect::<GeometryResult<Vec<_>>>()?;

    let refs = view.control_point_refs()?;
    let mut control_points = Vec::with_capacity(refs.len());
    for point_ref in refs {
        let placed = world_point(session, id, point_ref, frame)?;
        finite_values(
            session,
            id,
            &type_name,
            "transformed control point",
            &placed,
        )?;
        control_points.push(Point3::from_array(placed));
    }
    let weights = view.weights()?;
    if let Some(values) = weights.as_deref() {
        finite_values(session, id, &type_name, "WeightsData", values)?;
    }
    let closed = view.closed_curve().ok_or_else(|| {
        session.unsupported(
            id,
            &type_name,
            "unknown ClosedCurve is not lossless in bool",
        )
    })?;
    session.node_for(
        id,
        GeometryNode::Curve3(Curve3::BSpline(BSplineCurve3 {
            degree,
            control_points,
            knots: knots.values,
            multiplicities,
            weights,
            closed,
            self_intersect: view.self_intersect(),
            knot_spec: bspline_knot_spec(view.knot_spec()),
        })),
    )
}

fn bspline_knot_spec(source: KnotType) -> KnotSpec {
    match source {
        KnotType::Uniform => KnotSpec::Uniform,
        KnotType::QuasiUniform => KnotSpec::QuasiUniform,
        KnotType::PiecewiseBezier => KnotSpec::PiecewiseBezier,
        KnotType::Unspecified => KnotSpec::Unspecified,
    }
}

pub(super) fn finite_values(
    session: &LoweringSession<'_>,
    id: EntityId,
    type_name: &str,
    field: &'static str,
    values: &[f64],
) -> GeometryResult<()> {
    if values.iter().all(|value| value.is_finite()) {
        Ok(())
    } else {
        Err(session.degenerate(id, type_name, field))
    }
}

fn transition(code: TransitionCode) -> Transition {
    match code {
        TransitionCode::Discontinuous => Transition::Discontinuous,
        TransitionCode::Continuous => Transition::Continuous,
        TransitionCode::ContSameGradient => Transition::ContinuousSameGradient,
        TransitionCode::ContSameGradientSameCurvature => {
            Transition::ContinuousSameGradientSameCurvature
        }
    }
}

/// Resolve an `IfcCartesianPoint`, scale to metres, then place it.
fn world_point(
    session: &mut LoweringSession<'_>,
    owner: EntityId,
    point_ref: EntityId,
    frame: Transform,
) -> GeometryResult<[f64; 3]> {
    let entity = session.entity(owner, point_ref)?;
    let raw = CartesianPoint::new(point_ref, entity).coordinates_3d()?;
    let scaled = raw.map(|value| session.units().length(value));
    Ok(frame.apply(scaled))
}

/// Resolve an `IfcVector`: unit direction times magnitude, then rotate.
///
/// The magnitude is a length and scales; the direction is rotated only.
fn world_vector(
    session: &mut LoweringSession<'_>,
    owner: EntityId,
    vector_ref: EntityId,
    frame: Transform,
) -> GeometryResult<[f64; 3]> {
    let entity = session.entity(owner, vector_ref)?;
    let slots = crate::slots::Slots::new(vector_ref, entity);
    let direction_ref = slots.req_ref(0, "Orientation")?;
    let magnitude = session.units().length(slots.req_f64(1, "Magnitude")?);
    let unit = resolve_unit(session.model(), owner, direction_ref)?;
    let scaled = unit.map(|component| component * magnitude);
    Ok(frame.apply_direction(scaled))
}

/// Build a kernel frame from a placed transform.
fn frame3(t: &Transform) -> Frame3 {
    Frame3 {
        origin: Point3::from_array(t.origin),
        x: Vec3::from_array(t.basis[0]),
        y: Vec3::from_array(t.basis[1]),
        z: Vec3::from_array(t.basis[2]),
    }
}

#[cfg(test)]
mod tests;