cadmpeg-asm 0.6.5

Parse and write Autodesk Shape Manager record streams.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
// SPDX-License-Identifier: Apache-2.0
//! Walk the reachable topology graph, collect wire and shell chains, and
//! classify edge curve senses.

use super::records::{MeshSurfaceSentinel, WireMembers, WireSide, WireTopology};
use crate::ids::{brep_id, IdFormat};
use crate::nurbs;
use crate::sab::{Record, Token};
use cadmpeg_ir::geometry::{
    pcurve::PcurveGeometry, CurveGeometry, SolvedCurveGeometry, SolvedSurfaceGeometry,
    SurfaceGeometry,
};
use cadmpeg_ir::ids::{
    CoedgeId, EdgeId, FaceId, LoopId, ProceduralCurveId, ProceduralSurfaceId, RegionId, ShellId,
    SurfaceId, VertexId,
};
use cadmpeg_ir::topology::Sense;
use std::collections::{HashMap, HashSet};

use super::attributes::unknown_record_id;
use super::geometry::{
    analytic_procedural_surface, coedge_pcurve_ref, decode_curve, decode_surface,
    is_analytic_curve, is_analytic_surface, is_coedge_record, is_edge_record, is_vertex_record,
    pcurve_ranges_on_domain, procedural_surface_definition_is_exact_carrier, record_reversed,
    reverse_procedural_curve_definition, sense_at, vertex_point_ref,
};
use super::{count_kind, id, AsmBrep, Carriers, DecodePurpose, Reachable, WireShellTopology};
/// Pass 1: classify carriers and decode analytic geometry. Returns the seeded
/// carrier maps and the set of carriers whose native normal is inward.
pub(super) fn decode_analytic_carriers(
    records: &[Record],
) -> Result<(Carriers, HashSet<i64>), cadmpeg_core::CodecError> {
    let mut surface_geo: HashMap<i64, SurfaceGeometry> = HashMap::new();
    let mut inward_normal_surfaces = HashSet::new();
    let mut curve_geo: HashMap<i64, CurveGeometry> = HashMap::new();
    for r in records {
        if is_analytic_surface(r.head()) {
            if let Some((geometry, inward)) = decode_surface(r) {
                if inward {
                    inward_normal_surfaces.insert(i64::try_from(r.index).map_err(|_| {
                        cadmpeg_core::CodecError::malformed(
                            "ASM record index exceeds the i64 range",
                        )
                    })?);
                }
                surface_geo.insert(
                    i64::try_from(r.index).map_err(|_| {
                        cadmpeg_core::CodecError::malformed(
                            "ASM record index exceeds the i64 range",
                        )
                    })?,
                    SurfaceGeometry::Solved(geometry),
                );
            }
        } else if is_analytic_curve(r.head()) {
            if let Some(g) = decode_curve(r) {
                curve_geo.insert(
                    i64::try_from(r.index).map_err(|_| {
                        cadmpeg_core::CodecError::malformed(
                            "ASM record index exceeds the i64 range",
                        )
                    })?,
                    g,
                );
            }
        }
    }
    let carriers = Carriers {
        surface_geo,
        curve_geo,
        ..Carriers::default()
    };
    Ok((carriers, inward_normal_surfaces))
}

/// Inputs shared by the topology passes of one ASM decode.
#[derive(Clone, Copy)]
pub(super) struct TopologyContext<'a, 'ctx, 'record> {
    pub(super) ctx: &'a cadmpeg_core::decode::DecodeContext<'ctx>,
    pub(super) by_index: &'a HashMap<i64, &'record Record>,
    pub(super) token_table: &'a nurbs::toks::SubtypeTable,
    pub(super) purpose: DecodePurpose,
    pub(super) format: IdFormat,
}

/// Pass 2 (faces): keep every face whose surface reference resolves, decoding
/// or classifying its carrier and recording surface reachability.
pub(super) fn keep_faces_and_carriers(
    inputs: TopologyContext<'_, '_, '_>,
    out: &mut AsmBrep,
    records: &[Record],
    carriers: &mut Carriers,
    reach: &mut Reachable,
) -> Result<(), cadmpeg_core::CodecError> {
    let TopologyContext {
        ctx,
        by_index,
        token_table,
        purpose,
        format,
    } = inputs;
    let Carriers {
        surface_geo,
        procedural_surface_defs,
        ..
    } = &mut *carriers;
    let Reachable {
        faces: kept_faces,
        surfaces: kept_surfaces,
        unknown_surface_records,
        cached_unknown_procedural_surfaces,
        undecoded_carriers,
        ..
    } = &mut *reach;
    for r in records {
        if r.head() != "face" {
            continue;
        }
        // A standalone face cannot supply the shell required by the neutral
        // topology. Do not reach its children or carriers through an owner
        // that the emit pass must omit.
        if r.ref_at(5).is_none() {
            continue;
        }
        let Some(surf_ref) = r.ref_at(7) else {
            count_kind(&mut out.stats.missing_face_surface_kinds, "null-reference");
            continue;
        };
        let Some(surf_rec) = by_index.get(&surf_ref) else {
            // Dangling surface reference: a face without a resolvable surface
            // cannot be emitted (the IR requires one), so it is dropped.

            count_kind(
                &mut out.stats.missing_face_surface_kinds,
                "dangling-reference",
            );
            continue;
        };
        kept_faces.insert(i64::try_from(r.index).map_err(|_| {
            cadmpeg_core::CodecError::malformed("ASM record index exceeds the i64 range")
        })?);
        if purpose == DecodePurpose::History {
            let native_kind = (surf_rec.head() == "spline")
                .then(|| nurbs::toks::owned_construction_subtype(&surf_rec.tokens))
                .flatten();
            if native_kind
                .as_deref()
                .is_some_and(|kind| kind.contains("blend"))
            {
                if let Some(procedural) = nurbs::proc_surface::procedural_surface_resolving_refs(
                    ctx,
                    &surf_rec.tokens,
                    token_table,
                ) {
                    procedural_surface_defs.insert(surf_ref, procedural?);
                }
            }
            surface_geo
                .entry(surf_ref)
                .or_insert(SurfaceGeometry::Solved(SolvedSurfaceGeometry::Unknown {
                    record: None,
                }));
            kept_surfaces.insert(surf_ref);
            continue;
        }
        if let Some(procedural) = nurbs::proc_surface::procedural_surface_resolving_refs(
            ctx,
            &surf_rec.tokens,
            token_table,
        ) {
            procedural_surface_defs.insert(surf_ref, procedural?);
        }
        if let Some(procedural) = procedural_surface_defs.get(&surf_ref) {
            if let Some(geometry) = analytic_procedural_surface(procedural.definition()) {
                surface_geo.insert(surf_ref, geometry);
            }
        }
        let exact_cacheless_construction =
            procedural_surface_defs
                .get(&surf_ref)
                .is_some_and(|procedural| {
                    procedural.cache_fit_tolerance().is_none()
                        && procedural_surface_definition_is_exact_carrier(procedural.definition())
                });
        // A non-analytic surface may still carry a decodable B-spline face
        // cache. Exact cacheless constructions own their nested surface blocks
        // as supports, not as evaluated face caches.
        if !exact_cacheless_construction && !surface_geo.contains_key(&surf_ref) {
            if let Some(ns) =
                nurbs::core::surface_cache_resolving_refs(ctx, &surf_rec.tokens, token_table)
            {
                surface_geo.insert(
                    surf_ref,
                    SurfaceGeometry::Solved(SolvedSurfaceGeometry::Nurbs(ns?)),
                );
                if surf_rec.head() == "spline" && !procedural_surface_defs.contains_key(&surf_ref) {
                    cached_unknown_procedural_surfaces.insert(surf_ref);
                    count_kind(
                        &mut out.stats.other_record_kinds,
                        "cached-procedural-surface-untyped",
                    );
                }
                out.stats.nurbs_surfaces += 1;
            }
        }
        if !surface_geo.contains_key(&surf_ref) && procedural_surface_defs.contains_key(&surf_ref) {
            let construction_is_exact_carrier =
                procedural_surface_defs
                    .get(&surf_ref)
                    .is_some_and(|procedural| {
                        procedural_surface_definition_is_exact_carrier(procedural.definition())
                    });
            surface_geo.insert(
                surf_ref,
                if construction_is_exact_carrier {
                    SurfaceGeometry::Procedural {
                        construction: brep_id!(
                            format,
                            ProceduralSurfaceId,
                            "procedural_surface",
                            surf_ref
                        ),
                        cache: None,
                    }
                } else {
                    SurfaceGeometry::Solved(SolvedSurfaceGeometry::Unknown {
                        record: Some(unknown_record_id(surf_rec, format)?),
                    })
                },
            );
            if !construction_is_exact_carrier {
                undecoded_carriers.insert(surf_ref);
            }
        }
        if surface_geo.contains_key(&surf_ref) {
            kept_surfaces.insert(surf_ref);
        } else {
            unknown_surface_records.insert(surf_ref);
            undecoded_carriers.insert(surf_ref);
            if surf_rec.head() == "mesh_surface" && surf_rec.chunks().next().is_none() {
                if !out.mesh_surface_sentinels.iter().any(|sentinel| {
                    usize::try_from(sentinel.record_index).ok() == Some(surf_rec.index)
                }) {
                    out.mesh_surface_sentinels.push(MeshSurfaceSentinel {
                        source_namespace:
                            crate::brep::records::identity::NativeRecordNamespace::new(format),
                        surface: SurfaceId::from(id(format, surf_ref)),
                        record_index: u32::try_from(surf_rec.index).map_err(|_| {
                            cadmpeg_core::CodecError::malformed(
                                "ASM native record index exceeds the u32 range",
                            )
                        })?,
                    });
                }
                out.stats.mesh_surface_faces += 1;
            } else {
                let native_kind = if surf_rec.head() == "spline" {
                    nurbs::toks::owned_construction_subtype(&surf_rec.tokens)
                } else {
                    None
                };
                count_kind(
                    &mut out.stats.unknown_surface_kinds,
                    native_kind.as_deref().unwrap_or_else(|| surf_rec.head()),
                );
            }
        }
    }
    Ok(())
}

/// Pass 2 (topology): walk each kept face's loops and coedge rings, pulling in
/// the supporting edge/vertex/point graph and decoding curve and pcurve carriers.
pub(super) fn walk_reachable_topology(
    inputs: TopologyContext<'_, '_, '_>,
    out: &mut AsmBrep,
    carriers: &mut Carriers,
    reach: &mut Reachable,
) -> Result<(), cadmpeg_core::CodecError> {
    let TopologyContext {
        ctx,
        by_index,
        token_table,
        purpose,
        format,
    } = inputs;
    let Carriers {
        curve_geo,
        procedural_curve_defs,
        pcurve_geo,
        pcurve_parameter_ranges,
        ..
    } = &mut *carriers;
    let Reachable {
        faces: kept_faces,
        loops: kept_loops,
        coedges: kept_coedges,
        edges: kept_edges,
        vertices: kept_vertices,
        points: kept_points,
        curves: kept_curves,
        pcurves: kept_pcurves,
        undecoded_carriers,
        ..
    } = &mut *reach;
    // Walk each kept face's loops and coedge rings, collecting supporting graph.
    let face_indices = kept_faces.iter().copied().collect::<Vec<_>>();
    for face_idx in face_indices {
        let Some(face) = by_index.get(&face_idx) else {
            continue;
        };
        let mut loop_ref = face.ref_at(4);
        while let Some(li) = loop_ref {
            let Some(lp) = by_index.get(&li) else { break };
            if lp.head() != "loop" {
                break;
            }
            // A loop kept from another face was walked with the rest of its
            // chain, so the walk ends here.
            if !kept_loops.insert(li) {
                break;
            }
            // Ring-walk coedges via chunk[3] = next.
            if let Some(first_ce) = lp.ref_at(4) {
                let mut ce_ref = Some(first_ce);
                while let Some(ci) = ce_ref {
                    let Some(ce) = by_index.get(&ci) else { break };
                    if !is_coedge_record(ce) || !kept_coedges.insert(ci) {
                        break;
                    }
                    if let Some(pc) = coedge_pcurve_ref(ce) {
                        if let Some(prec) = by_index.get(&pc) {
                            if purpose == DecodePurpose::History {
                                pcurve_geo.entry(super::PcurveRecordIndex(pc)).or_insert(
                                    PcurveGeometry::Line(
                                        cadmpeg_ir::geometry::pcurve::LinePcurve::U_AXIS,
                                    ),
                                );
                                kept_pcurves.insert(pc);
                            } else {
                                // An inline `exp_par_cur` owns its first BS2 field.
                                // A wrapped subtype ref resolves that same typed
                                // field. A nonzero-discriminator ref form names one
                                // of the target intcurve's two pcurve slots; its
                                // sign composes with the intcurve sense bit.
                                let decoded = match (prec.chunk(3), prec.chunk(4)) {
                                    (Some(Token::Long(0)), Some(Token::True | Token::False)) => {
                                        if let Some(span) = nurbs::toks::payload_subtype_toks(
                                            prec,
                                            5,
                                            "exp_par_cur",
                                        ) {
                                            nurbs::pcurve::explicit_pcurve_cache(span)
                                                .map(|pcurve| Ok((pcurve, true)))
                                        } else if let Some(span) =
                                            nurbs::toks::payload_subtype_toks(prec, 5, "ref")
                                        {
                                            // The interior opens with the `ref`
                                            // identifier the lookup matched; the
                                            // index is the field after it.
                                            match span.interior() {
                                                [_name, Token::Long(index), ..] =>
                                                    nurbs::pcurve::explicit_pcurve_cache_from_subtype_ref(
                                                        *index,
                                                        token_table,
                                                    )
                                                    .map(|pcurve| Ok((pcurve, true))),
                                                _ => None,
                                            }
                                        } else {
                                            None
                                        }
                                    }
                                    (Some(Token::Long(selector)), Some(Token::Ref(reference)))
                                        if matches!(*selector, 1 | 2 | -1 | -2) =>
                                    {
                                        by_index
                                            .get(reference)
                                            .filter(|record| record.head() == "intcurve")
                                            .and_then(|intcurve| {
                                                nurbs::proc_curve::pcurve_for_selector_with_chart(
                                                    ctx,
                                                    &intcurve.tokens,
                                                    *selector,
                                                    token_table,
                                                )
                                                .map(|result| result.map(|(mut curve, native_chart)| {
                                                    if (*selector < 0) ^ record_reversed(intcurve) {
                                                        curve.reverse_parameterization();
                                                    }
                                                    (curve, native_chart)
                                                }))
                                            })
                                    }
                                    _ => None,
                                }.transpose()?;
                                let edge =
                                    ce.ref_at(6).and_then(|edge| by_index.get(&edge)).copied();
                                let decoded = decoded.map(|(mut decoded, native_chart)| -> Result<_, cadmpeg_core::CodecError> {
                                    if native_chart {
                                        if let Some(surface) = face
                                            .ref_at(7)
                                            .and_then(|surface| by_index.get(&surface))
                                        {
                                            let Some(()) = nurbs::support_chart::SupportChart::of_surface_record(surface.head(), &surface.tokens).and_then(|chart| chart.normalize(&mut decoded)) else { return Ok(None); };
                                        }
                                    }
                                    Ok(pcurve_ranges_on_domain(&decoded, edge)
                                        .and_then(|ranges| ranges.into_iter().next())
                                        .map(|range| (decoded, range)))
                                }).transpose()?.flatten();
                                if let Some((decoded, parameter_range)) = decoded {
                                    pcurve_geo.insert(
                                        super::PcurveRecordIndex(pc),
                                        PcurveGeometry::Nurbs { nurbs: decoded },
                                    );
                                    pcurve_parameter_ranges
                                        .insert(super::CoedgeRecordIndex(ci), parameter_range);
                                    kept_pcurves.insert(pc);
                                } else {
                                    count_kind(&mut out.stats.undecoded_pcurve_kinds, prec.head());
                                }
                            }
                        } else {
                            count_kind(&mut out.stats.undecoded_pcurve_kinds, "dangling-reference");
                        }
                    }
                    if let Some(ei) = ce.ref_at(6) {
                        if let Some(edge) = by_index.get(&ei) {
                            // An edge is shared by two coedges; process (and
                            // count its curve loss) only the first time it is
                            // reached so shared edges are not double-counted.
                            if is_edge_record(edge) && kept_edges.insert(ei) {
                                for slot in [3usize, 5] {
                                    if let Some(vi) = edge.ref_at(slot) {
                                        if let Some(v) = by_index.get(&vi) {
                                            if is_vertex_record(v) {
                                                kept_vertices.insert(vi);
                                                if let Some(pi) = vertex_point_ref(v) {
                                                    kept_points.insert(pi);
                                                }
                                            }
                                        }
                                    }
                                }
                                match edge.ref_at(8) {
                                    Some(cv) if curve_geo.contains_key(&cv) => {
                                        kept_curves.insert(cv);
                                    }
                                    Some(cv) => {
                                        if let Some(crec) = by_index.get(&cv) {
                                            if purpose == DecodePurpose::History {
                                                curve_geo.insert(cv, CurveGeometry::Solved(SolvedCurveGeometry::Unknown { record: None }));
                                                kept_curves.insert(cv);
                                            // A procedural curve carries an inline
                                            // 3D B-spline cache in most subtypes.
                                            } else if let Some(decoded) =
                                                nurbs::proc_curve::procedural_curve_resolving_refs(
                                                    ctx,
                                                    &crec.tokens,
                                                    token_table,
                                                ).transpose()?
                                            {
                                                let parsed_domain = nurbs::proc_curve::nurbs_curve_parameter_domain(&decoded.curve);
                                                let mut curve = decoded.curve;
                                                // A reversed intcurve parameterizes
                                                // as the negation of its cache; the
                                                // edge's stored range is on the
                                                // reversed parameterization.
                                                if record_reversed(crec) {
                                                    curve.reverse_parameterization();
                                                }
                                                curve_geo.insert(cv, CurveGeometry::Solved(SolvedCurveGeometry::Nurbs(curve)));
                                                procedural_curve_defs.insert(cv, super::ProceduralCurveSource::Cached {
                                                        construction: Box::new(decoded.construction),
                                                        cache_fit_tolerance: decoded.cache_fit_tolerance,
                                                        parsed_domain,
                                                    });
                                                out.stats.nurbs_curves += 1;
                                                kept_curves.insert(cv);
                                            } else if let Some(definition) =
                                                nurbs::proc_curve::cacheless_procedural_curve_resolving_refs(
                                                    ctx,
                                                    &crec.tokens,
                                                    token_table,
                                                ).transpose()?.and_then(|definition| definition.into_definition().ok())
                                                .and_then(|mut definition| {
                                                    if record_reversed(crec) {
                                                        reverse_procedural_curve_definition(&mut definition).ok()?;
                                                    }
                                                    Some(definition)
                                                })
                                            {
                                                curve_geo.insert(cv, CurveGeometry::Procedural {
                                                    construction: brep_id!(
                                                        format,
                                                        ProceduralCurveId,
                                                        "procedural_curve",
                                                        cv
                                                    ),
                                                        cache: None,
                                                    });
                                                procedural_curve_defs.insert(cv, super::ProceduralCurveSource::Cacheless(Box::new(definition)));
                                                kept_curves.insert(cv);
                                            } else {
                                                undecoded_carriers.insert(cv);

                                                count_kind(&mut out.stats.procedural_curve_kinds, crec.head());
                                            }
                                        } else {
                                            count_kind(
                                                &mut out.stats.procedural_curve_kinds,
                                                "dangling-reference",
                                            );
                                        }
                                    }
                                    _ => {}
                                }
                            }
                        }
                    }
                    ce_ref = ce.ref_at(3);
                    if ce_ref == Some(first_ce) {
                        break;
                    }
                }
            }
            loop_ref = lp.ref_at(3);
        }
    }
    Ok(())
}

/// Pass 2 (wires): collect shell wire edges and free vertices, decoding wire
/// curve carriers and emitting wire topologies.
pub(super) fn collect_wire_topology(
    inputs: TopologyContext<'_, '_, '_>,
    out: &mut AsmBrep,
    records: &[Record],
    saved_entity_limit: Option<i64>,
    carriers: &mut Carriers,
    reach: &mut Reachable,
) -> Result<WireShellTopology, cadmpeg_core::CodecError> {
    let TopologyContext {
        by_index, format, ..
    } = inputs;
    let mut wire_edges_by_shell = HashMap::<i64, Vec<i64>>::new();
    let mut free_vertices_by_shell = HashMap::<i64, Vec<i64>>::new();
    let mut saved_free_edges = Vec::new();
    if let Some(limit) = saved_entity_limit {
        for edge in records {
            let edge_index = i64::try_from(edge.index).map_err(|_| {
                cadmpeg_core::CodecError::malformed("ASM record index exceeds the i64 range")
            })?;
            if !(1..limit).contains(&edge_index) || !is_edge_record(edge) {
                continue;
            }
            let already_owned = reach.edges.contains(&edge_index);
            keep_wire_edge(inputs, out, edge_index, carriers, reach)?;
            if !already_owned && reach.edges.contains(&edge_index) {
                saved_free_edges.push(edge_index);
            }
        }
    }
    let subshell_shells = subshell_ancestor_shells(records, by_index)?;
    // A coedge has one owner, so each is walked by at most one wire.
    let mut walked_coedges = HashSet::new();
    for shell in records.iter().filter(|record| record.head() == "shell") {
        let shell_index = i64::try_from(shell.index).map_err(|_| {
            cadmpeg_core::CodecError::malformed("ASM record index exceeds the i64 range")
        })?;
        let mut wire_guard = HashSet::new();
        let mut shell_edges = HashSet::new();
        let mut shell_vertices = HashSet::new();
        for root in shell_wire_roots(shell, by_index, &subshell_shells) {
            let mut wire_ref = Some(root);
            while let Some(wire_index) = wire_ref {
                if !wire_guard.insert(wire_index) {
                    break;
                }
                let Some(wire) = by_index.get(&wire_index).filter(|record| {
                    record.head() == "wire"
                        && record.ref_at(5).is_some_and(|owner| {
                            resolves_to_shell(owner, shell_index, &subshell_shells)
                        })
                }) else {
                    break;
                };
                let side = match wire.chunk(7) {
                    Some(Token::True) => Some(WireSide::In),
                    Some(Token::False) => Some(WireSide::Out),
                    _ => None,
                };
                let mut wire_edges = Vec::new();
                let mut wire_edge_set = HashSet::new();
                if let Some(first_coedge) = wire.ref_at(4) {
                    let mut coedge_ref = Some(first_coedge);
                    while let Some(coedge_index) = coedge_ref {
                        if !walked_coedges.insert(coedge_index) {
                            break;
                        }
                        let Some(coedge) = by_index
                            .get(&coedge_index)
                            .filter(|record| is_coedge_record(record))
                        else {
                            break;
                        };
                        if let Some(edge_index) = coedge.ref_at(6) {
                            if wire_edge_set.insert(edge_index) {
                                wire_edges.push(edge_index);
                            }
                            if shell_edges.insert(edge_index) {
                                wire_edges_by_shell
                                    .entry(shell_index)
                                    .or_default()
                                    .push(edge_index);
                            }
                            keep_wire_edge(inputs, out, edge_index, carriers, reach)?;
                        }
                        coedge_ref = coedge.ref_at(3);
                        if coedge_ref == Some(first_coedge) {
                            break;
                        }
                    }
                }
                let free_vertex = if wire.ref_at(4).is_none() {
                    wire.ref_at(6).filter(|vertex| {
                        by_index
                            .get(vertex)
                            .is_some_and(|record| is_vertex_record(record))
                    })
                } else {
                    None
                };
                if let Some(vertex) = free_vertex {
                    reach.vertices.insert(vertex);

                    if shell_vertices.insert(vertex) {
                        free_vertices_by_shell
                            .entry(shell_index)
                            .or_default()
                            .push(vertex);
                    }
                    if let Some(point) = by_index
                        .get(&vertex)
                        .and_then(|record| vertex_point_ref(record))
                    {
                        reach.points.insert(point);
                    }
                }
                if let Some(side) = side {
                    out.wire_topologies.push(WireTopology {
                        source_namespace:
                            crate::brep::records::identity::NativeRecordNamespace::new(format),
                        shell: ShellId::from(id(format, shell_index)),
                        record_index: u32::try_from(wire.index).map_err(|_| {
                            cadmpeg_core::CodecError::malformed(
                                "ASM native record index exceeds the u32 range",
                            )
                        })?,
                        members: match free_vertex {
                            Some(vertex) => WireMembers::Vertex(VertexId::from(id(format, vertex))),
                            None => WireMembers::Edges(
                                wire_edges
                                    .into_iter()
                                    .map(|edge| EdgeId::from(id(format, edge)))
                                    .collect::<Vec<_>>(),
                            ),
                        },
                        side,
                    });
                }
                wire_ref = wire.ref_at(3);
            }
        }
    }
    Ok(WireShellTopology {
        wire_edges_by_shell,
        free_vertices_by_shell,
        saved_free_edges,
    })
}

fn keep_wire_edge(
    inputs: TopologyContext<'_, '_, '_>,
    out: &mut AsmBrep,
    edge_index: i64,
    carriers: &mut Carriers,
    reach: &mut Reachable,
) -> Result<(), cadmpeg_core::CodecError> {
    let TopologyContext {
        ctx,
        by_index,
        token_table,
        purpose,
        format,
    } = inputs;
    let Carriers {
        curve_geo,
        procedural_curve_defs,
        ..
    } = carriers;
    let Reachable {
        edges: kept_edges,
        vertices: kept_vertices,
        points: kept_points,
        curves: kept_curves,
        undecoded_carriers,
        ..
    } = reach;
    let Some(edge) = by_index
        .get(&edge_index)
        .filter(|edge| is_edge_record(edge))
    else {
        return Ok(());
    };
    if !kept_edges.insert(edge_index) {
        return Ok(());
    }
    for slot in [3usize, 5] {
        if let Some(vertex_index) = edge.ref_at(slot) {
            if let Some(vertex) = by_index
                .get(&vertex_index)
                .filter(|vertex| is_vertex_record(vertex))
            {
                kept_vertices.insert(vertex_index);
                if let Some(point_index) = vertex_point_ref(vertex) {
                    kept_points.insert(point_index);
                }
            }
        }
    }
    let Some(curve_index) = edge.ref_at(8) else {
        return Ok(());
    };
    if curve_geo.contains_key(&curve_index) {
        kept_curves.insert(curve_index);
    } else {
        let Some(curve_record) = by_index.get(&curve_index) else {
            count_kind(&mut out.stats.procedural_curve_kinds, "dangling-reference");
            return Ok(());
        };
        if purpose == DecodePurpose::History {
            curve_geo.insert(
                curve_index,
                CurveGeometry::Solved(SolvedCurveGeometry::Unknown { record: None }),
            );
            kept_curves.insert(curve_index);
            return Ok(());
        }
        if let Some(decoded) = nurbs::proc_curve::procedural_curve_resolving_refs(
            ctx,
            &curve_record.tokens,
            token_table,
        )
        .transpose()?
        {
            let parsed_domain = nurbs::proc_curve::nurbs_curve_parameter_domain(&decoded.curve);
            let mut curve = decoded.curve;
            if record_reversed(curve_record) {
                curve.reverse_parameterization();
            }
            curve_geo.insert(
                curve_index,
                CurveGeometry::Solved(SolvedCurveGeometry::Nurbs(curve)),
            );
            procedural_curve_defs.insert(
                curve_index,
                super::ProceduralCurveSource::Cached {
                    construction: Box::new(decoded.construction),
                    cache_fit_tolerance: decoded.cache_fit_tolerance,
                    parsed_domain,
                },
            );
            kept_curves.insert(curve_index);
            out.stats.nurbs_curves += 1;
        } else if let Some(definition) =
            nurbs::proc_curve::cacheless_procedural_curve_resolving_refs(
                ctx,
                &curve_record.tokens,
                token_table,
            )
            .transpose()?
            .and_then(|definition| definition.into_definition().ok())
            .and_then(|mut definition| {
                if record_reversed(curve_record) {
                    reverse_procedural_curve_definition(&mut definition).ok()?;
                }
                Some(definition)
            })
        {
            curve_geo.insert(
                curve_index,
                CurveGeometry::Procedural {
                    construction: brep_id!(
                        format,
                        ProceduralCurveId,
                        "procedural_curve",
                        curve_index
                    ),
                    cache: None,
                },
            );
            procedural_curve_defs.insert(
                curve_index,
                super::ProceduralCurveSource::Cacheless(Box::new(definition)),
            );
            kept_curves.insert(curve_index);
        } else {
            undecoded_carriers.insert(curve_index);

            count_kind(&mut out.stats.procedural_curve_kinds, curve_record.head());
        }
    }
    Ok(())
}

/// Partition kept edges' curve references by sense so a carrier shared across
/// both senses can emit a `:reversed` clone beside its forward orientation.
pub(super) fn classify_edge_curve_senses(
    records: &[Record],
    reach: &Reachable,
) -> Result<(HashSet<i64>, HashSet<i64>), cadmpeg_core::CodecError> {
    let Reachable {
        edges: kept_edges,
        curves: kept_curves,
        ..
    } = reach;
    let mut reversed_curve_refs: HashSet<i64> = HashSet::new();
    let mut forward_curve_refs: HashSet<i64> = HashSet::new();
    for r in records {
        if !is_edge_record(r)
            || !kept_edges.contains(
                &(i64::try_from(r.index).map_err(|_| {
                    cadmpeg_core::CodecError::malformed("ASM record index exceeds the i64 range")
                })?),
            )
        {
            continue;
        }
        let Some(curve) = r.ref_at(8).filter(|c| kept_curves.contains(c)) else {
            continue;
        };
        match sense_at(r, 9) {
            Sense::Reversed => reversed_curve_refs.insert(curve),
            Sense::Forward => forward_curve_refs.insert(curve),
        };
    }
    Ok((reversed_curve_refs, forward_curve_refs))
}

pub(super) fn ring_coedges(
    loop_rec: &Record,
    by_index: &HashMap<i64, &Record>,
    kept: &HashSet<i64>,
    format: IdFormat,
) -> Vec<CoedgeId> {
    let id = |i: i64| CoedgeId::from(super::id(format, i));
    let mut out = Vec::new();
    let Some(first) = loop_rec.ref_at(4) else {
        return out;
    };
    let mut cur = Some(first);
    let mut guard = HashSet::new();
    while let Some(ci) = cur {
        if !guard.insert(ci) || !kept.contains(&ci) {
            break;
        }
        let Some(ce) = owned_child(by_index, ci, &["coedge", "tcoedge"], 8, loop_rec) else {
            break;
        };
        out.push(id(ci));
        cur = ce.ref_at(3);
        if cur == Some(first) {
            break;
        }
    }
    out
}

pub(super) fn loop_chain(
    face_rec: &Record,
    by_index: &HashMap<i64, &Record>,
    kept: &HashSet<i64>,
    format: IdFormat,
) -> Vec<LoopId> {
    let id = |i: i64| LoopId::from(super::id(format, i));
    let mut out = Vec::new();
    let mut cur = face_rec.ref_at(4);
    let mut guard = HashSet::new();
    while let Some(li) = cur {
        if !guard.insert(li) {
            break;
        }
        let Some(lp) = owned_child(by_index, li, &["loop"], 5, face_rec) else {
            break;
        };
        if kept.contains(&li) {
            out.push(id(li));
        }
        cur = lp.ref_at(3);
    }
    out
}

/// Append the kept faces of the chain starting at `first` until the chain
/// revisits a face listed anywhere in this shell or reaches a face `owned`
/// rejects.
fn face_chain(
    first: Option<i64>,
    owned: impl Fn(&Record) -> bool,
    by_index: &HashMap<i64, &Record>,
    kept: &HashSet<i64>,
    format: IdFormat,
    guard: &mut HashSet<i64>,
    out: &mut Vec<FaceId>,
) {
    let mut cur = first;
    while let Some(fi) = cur {
        if !guard.insert(fi) {
            break;
        }
        let Some(f) = by_index
            .get(&fi)
            .filter(|record| record.head() == "face" && owned(record))
        else {
            break;
        };
        if kept.contains(&fi) {
            out.push(FaceId::from(super::id(format, fi)));
        }
        cur = f.ref_at(3);
    }
}

pub(super) fn subshell_ancestor_shells(
    records: &[Record],
    by_index: &HashMap<i64, &Record>,
) -> Result<HashMap<i64, i64>, cadmpeg_core::CodecError> {
    let mut out = HashMap::new();
    // A subshell's owner chain ends where its owning subshell's chain ends:
    // at the same shell, or at none when the chain breaks or cycles.
    let mut resolved: HashMap<i64, Option<i64>> = HashMap::new();
    for record in records.iter().filter(|record| record.head() == "subshell") {
        let index = i64::try_from(record.index).map_err(|_| {
            cadmpeg_core::CodecError::malformed("ASM record index exceeds the i64 range")
        })?;
        let mut path = vec![index];
        let mut on_path = HashSet::from([index]);
        let mut current: &Record = record;
        let shell = loop {
            let Some(owner) = current.ref_at(3) else {
                break None;
            };
            if let Some(known) = resolved.get(&owner) {
                break *known;
            }
            let Some(parent) = by_index.get(&owner) else {
                break None;
            };
            if parent.head() == "shell" {
                break Some(owner);
            }
            if parent.head() != "subshell" || !on_path.insert(owner) {
                break None;
            }
            path.push(owner);
            current = *parent;
        };
        resolved.extend(path.into_iter().map(|subshell| (subshell, shell)));
        if let Some(shell) = shell {
            out.insert(index, shell);
        }
    }
    Ok(out)
}

/// Faces of `shell`, then of its subshell tree. Only subshells whose ancestor
/// is `shell` and faces whose owner resolves to `shell` are listed, each once,
/// matching the shell emission assigns each face.
pub(super) fn shell_faces(
    shell: &Record,
    by_index: &HashMap<i64, &Record>,
    subshell_shells: &HashMap<i64, i64>,
    kept: &HashSet<i64>,
    format: IdFormat,
) -> Vec<FaceId> {
    let mut out = Vec::new();
    let Ok(shell_index) = i64::try_from(shell.index) else {
        return out;
    };
    let in_shell = |owner: i64| resolves_to_shell(owner, shell_index, subshell_shells);
    let mut faces = HashSet::new();
    face_chain(
        shell.ref_at(5),
        |face| face.ref_at(5) == Some(shell_index),
        by_index,
        kept,
        format,
        &mut faces,
        &mut out,
    );
    let mut pending = shell.ref_at(4).into_iter().collect::<Vec<_>>();
    let mut guard = HashSet::new();
    while let Some(index) = pending.pop() {
        if !guard.insert(index) {
            break;
        }
        let Some(record) = by_index
            .get(&index)
            .filter(|record| record.head() == "subshell" && in_shell(index))
        else {
            break;
        };
        face_chain(
            record.ref_at(6),
            |face| face.ref_at(5).is_some_and(in_shell),
            by_index,
            kept,
            format,
            &mut faces,
            &mut out,
        );
        if let Some(next) = record.ref_at(4) {
            pending.push(next);
        }
        if let Some(child) = record.ref_at(5) {
            pending.push(child);
        }
    }
    out
}

/// Whether `owner` is `shell` or a subshell whose ancestor is `shell`.
fn resolves_to_shell(owner: i64, shell: i64, subshell_shells: &HashMap<i64, i64>) -> bool {
    owner == shell || subshell_shells.get(&owner) == Some(&shell)
}

/// First wires of `shell` and of the subshells whose ancestor is `shell`.
pub(super) fn shell_wire_roots(
    shell: &Record,
    by_index: &HashMap<i64, &Record>,
    subshell_shells: &HashMap<i64, i64>,
) -> Vec<i64> {
    let mut out = shell.ref_at(6).into_iter().collect::<Vec<_>>();
    let Ok(shell_index) = i64::try_from(shell.index) else {
        return out;
    };
    let mut pending = shell.ref_at(4).into_iter().collect::<Vec<_>>();
    let mut guard = HashSet::new();
    while let Some(index) = pending.pop() {
        if !guard.insert(index) {
            break;
        }
        let Some(record) = by_index.get(&index).filter(|record| {
            record.head() == "subshell" && subshell_shells.get(&index) == Some(&shell_index)
        }) else {
            break;
        };
        if let Some(wire) = record.ref_at(7) {
            out.push(wire);
        }
        if let Some(next) = record.ref_at(4) {
            pending.push(next);
        }
        if let Some(child) = record.ref_at(5) {
            pending.push(child);
        }
    }
    out
}

/// Whether `index` names a record with one of `heads` whose owner slot refers
/// back to `parent`. Emission writes exactly these records, so a parent lists
/// only children that exist in the output.
fn owned_child<'a>(
    by_index: &HashMap<i64, &'a Record>,
    index: i64,
    heads: &[&str],
    owner_slot: usize,
    parent: &Record,
) -> Option<&'a Record> {
    let parent = i64::try_from(parent.index).ok()?;
    by_index.get(&index).copied().filter(|record| {
        heads.contains(&record.head()) && record.ref_at(owner_slot) == Some(parent)
    })
}

pub(super) fn shell_chain(
    region_rec: &Record,
    by_index: &HashMap<i64, &Record>,
    emitted_shells: &HashSet<i64>,
    format: IdFormat,
) -> Vec<ShellId> {
    let id = |i: i64| ShellId::from(super::id(format, i));
    let mut out = Vec::new();
    let mut cur = region_rec.ref_at(4);
    let mut guard = HashSet::new();
    while let Some(si) = cur {
        if !guard.insert(si) {
            break;
        }
        let Some(s) = owned_child(by_index, si, &["shell"], 7, region_rec) else {
            break;
        };
        if emitted_shells.contains(&si) {
            out.push(id(si));
        }
        cur = s.ref_at(3);
    }
    out
}

pub(super) fn region_chain(
    body_rec: &Record,
    by_index: &HashMap<i64, &Record>,
    format: IdFormat,
) -> Vec<RegionId> {
    let id = |i: i64| RegionId::from(super::id(format, i));
    let mut out = Vec::new();
    let mut cur = body_rec.ref_at(3);
    let mut guard = HashSet::new();
    while let Some(li) = cur {
        if !guard.insert(li) {
            break;
        }
        let Some(l) = owned_child(by_index, li, &["region", "lump"], 5, body_rec) else {
            break;
        };
        out.push(id(li));
        cur = l.ref_at(3);
    }
    out
}

#[cfg(test)]
mod tests;