cadmpeg-ir 0.6.2

CAD data model, validation, diffing, and codec API for cadmpeg.
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
// SPDX-License-Identifier: Apache-2.0
//! Curve evaluator arms, and the surface arms that read a model curve, report
//! the point an evaluation that leaves the finite range reached; an arm with
//! no value reports that.

use crate::eval::model_curve_point_by_id;
use crate::eval::model_surface_point;
use crate::eval::model_surface_point_by_id;
use crate::eval::pcurve_tangent;
use crate::eval::EvaluationFailure;
use crate::features::FinitePoint3;
use crate::geometry::analytic::LineCurve;
use crate::geometry::pcurve::{CirclePcurve, LinePcurve, OffsetPcurve, PcurveGeometry};
use crate::geometry::surface_payloads::{
    AxisRevolutionSurfaceConstruction, ExtrusionSurfaceConstruction,
    LinearSweepSurfaceConstruction, RevolutionSurfaceConstruction, SumSurfaceConstruction,
};
use crate::geometry::{
    CacheContract, Curve, CurveGeometry, HelixCurveConstruction, HelixFrame, PlacedCurve,
    ProceduralCurve, ProceduralCurveDefinition, ProceduralSurface, ProceduralSurfaceDefinition,
    SolvedCurveGeometry, Surface, SurfaceGeometry,
};
use crate::ids::{CurveId, ProceduralCurveId, ProceduralSurfaceId, SurfaceId};
use crate::math::{Point2, Point3, Vector3};
use crate::transform::Transform;
use crate::CadIr;

/// The line through `(MAX, 0, 0)` along `x`: its point at `t = MAX` has no
/// finite `x`.
fn edge_line() -> CurveGeometry {
    CurveGeometry::Solved(SolvedCurveGeometry::Line(
        LineCurve::try_new(Point3::new(f64::MAX, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0))
            .expect("line fixture"),
    ))
}

/// A model holding `curves` and the procedural surface `definition` builds
/// from their ids, with the surface's geometry.
fn curve_surface_model(
    curves: Vec<CurveGeometry>,
    definition: impl FnOnce(&[CurveId]) -> ProceduralSurfaceDefinition,
) -> (CadIr, SurfaceId, SurfaceGeometry) {
    let ids = (0..curves.len())
        .map(|index| CurveId::mint(format!("test:model:curve#{index}")).expect("valid identity"))
        .collect::<Vec<_>>();
    let surface_id = SurfaceId::mint("test:model:surface#procedural").expect("valid identity");
    let construction =
        ProceduralSurfaceId::mint("test:model:procedural#surface").expect("valid identity");
    let geometry = SurfaceGeometry::Procedural {
        construction: construction.clone(),
        cache: None,
    };
    let mut ir = CadIr::empty();
    ir.model
        .curves
        .extend(ids.iter().zip(curves).map(|(id, geometry)| Curve {
            parameter_range: None,
            id: id.clone(),
            geometry,
            source_object: None,
        }));
    ir.model.surfaces.push(Surface {
        id: surface_id.clone(),
        geometry: geometry.clone(),
        source_object: None,
    });
    ir.model
        .procedural_surfaces
        .push(ProceduralSurface::new(construction, definition(&ids), None));
    (ir, surface_id, geometry)
}

/// Every surface point route's result at `(u, v)`: the geometry route, and
/// the arena route without and within a work budget.
fn surface_routes(
    ir: &CadIr,
    surface: &SurfaceId,
    geometry: &SurfaceGeometry,
    u: f64,
    v: f64,
) -> [Result<FinitePoint3, EvaluationFailure<Point3>>; 3] {
    let index = crate::index::ModelIndex::build(ir);
    [
        model_surface_point(
            crate::eval::admission::EvaluationAdmission::Standard,
            ir,
            geometry,
            u,
            v,
        ),
        model_surface_point_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            surface,
            u,
            v,
        ),
        crate::eval::admission::EvaluationAdmission::Standard.within_work_slice(
            &cadmpeg_core::decode::WorkBudget::new(1_000_000),
            |admission| model_surface_point_by_id(admission, &index, surface, u, v),
        ),
    ]
}

#[test]
fn an_extrusion_whose_directrix_point_overflows_reports_the_point_its_extrusion_reaches() {
    // The directrix point at u = MAX has no finite x; its extrusion by v = 2
    // along z reaches (NaN, 0, 2).
    let (ir, surface, geometry) = curve_surface_model(vec![edge_line()], |curves| {
        ProceduralSurfaceDefinition::Extrusion(
            ExtrusionSurfaceConstruction::try_new(
                curves[0].clone(),
                None,
                Vector3::new(0.0, 0.0, 1.0),
                None,
                CacheContract::from_form(None),
            )
            .expect("extrusion fixture"),
        )
    });
    for route in surface_routes(&ir, &surface, &geometry, f64::MAX, 2.0) {
        assert!(
            matches!(
                route,
                Err(EvaluationFailure::NonFinite(point))
                    if point.x.is_nan() && point.y == 0.0 && point.z == 2.0
            ),
            "{route:?}"
        );
    }
    for route in surface_routes(&ir, &surface, &geometry, -f64::MAX, 2.0) {
        assert_eq!(route.map(FinitePoint3::get), Ok(Point3::new(0.0, 0.0, 2.0)));
    }
}

/// The line along `y` through the origin, placed by a transform that adds
/// `f64::MAX` to `x` twice: every point it reaches has `x = +inf`.
fn overflowing_placed_line() -> CurveGeometry {
    CurveGeometry::Solved(SolvedCurveGeometry::Transformed(
        PlacedCurve::try_new(
            Box::new(SolvedCurveGeometry::Line(
                LineCurve::try_new(Point3::new(f64::MAX, 0.0, 0.0), Vector3::new(0.0, 1.0, 0.0))
                    .expect("line fixture"),
            )),
            Transform::affine([
                [1.0, 0.0, 0.0, f64::MAX],
                [0.0, 1.0, 0.0, 0.0],
                [0.0, 0.0, 1.0, 0.0],
            ])
            .expect("affine transform"),
        )
        .expect("placed curve fixture"),
    ))
}

#[test]
fn a_nurbs_curve_whose_projection_overflows_reports_the_point_it_reached() {
    // The weights 1 and -1 + 2^-40 nearly cancel at t = 0.5, so the
    // projected x coordinate, about 2e300 over 2^-41, overflows; y and z stay
    // zero.
    let curve = CurveGeometry::Solved(SolvedCurveGeometry::Nurbs(
        crate::geometry::nurbs::NurbsCurve::from_lanes(
            1,
            vec![0.0, 0.0, 1.0, 1.0],
            vec![
                Point3::new(1.0e300, 0.0, 0.0),
                Point3::new(-1.0e300, 0.0, 0.0),
            ],
            Some(vec![1.0, -1.0 + 2.0_f64.powi(-40)]),
            false,
        )
        .expect("NURBS curve fixture"),
    ));
    assert_eq!(
        crate::eval::decode::curve_point(
            crate::eval::admission::EvaluationAdmission::Standard,
            &curve,
            0.5
        ),
        Err(EvaluationFailure::NonFinite(Point3::new(
            f64::INFINITY,
            0.0,
            0.0
        )))
    );
    assert_eq!(
        crate::eval::decode::curve_point(
            crate::eval::admission::EvaluationAdmission::Standard,
            &curve,
            2.0
        ),
        Err(EvaluationFailure::NoValue)
    );
}

/// A model with `curves` and the procedural curve `definition` builds on
/// them, attached to one more curve; the id of that curve.
fn procedural_curve_model(
    curves: Vec<CurveGeometry>,
    definition: impl FnOnce(&[CurveId]) -> ProceduralCurveDefinition,
) -> (CadIr, CurveId) {
    let ids = (0..curves.len())
        .map(|index| CurveId::mint(format!("test:model:curve#{index}")).expect("valid identity"))
        .collect::<Vec<_>>();
    let owner = CurveId::mint("test:model:curve#procedural").expect("valid identity");
    let construction =
        ProceduralCurveId::mint("test:model:procedural#curve").expect("valid identity");
    let mut ir = CadIr::empty();
    ir.model
        .curves
        .extend(ids.iter().zip(curves).map(|(id, geometry)| Curve {
            parameter_range: None,
            id: id.clone(),
            geometry,
            source_object: None,
        }));
    ir.model.curves.push(Curve {
        parameter_range: None,
        id: owner.clone(),
        geometry: CurveGeometry::Procedural {
            construction: construction.clone(),
            cache: None,
        },
        source_object: None,
    });
    ir.model
        .add_procedural_curve(&owner, ProceduralCurve::new(construction, definition(&ids)))
        .expect("procedural curve fixture");
    (ir, owner)
}

#[test]
fn a_replica_curve_whose_placement_overflows_reports_the_point_it_reached() {
    let line = CurveGeometry::Solved(SolvedCurveGeometry::Line(
        LineCurve::try_new(Point3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0))
            .expect("line fixture"),
    ));
    let (ir, replica) =
        procedural_curve_model(vec![line], |curves| ProceduralCurveDefinition::Replica {
            source: curves[0].clone(),
            transform: Transform::affine([
                [1.0, 0.0, 0.0, f64::MAX],
                [0.0, 1.0, 0.0, 2.0],
                [0.0, 0.0, 1.0, 0.0],
            ])
            .expect("affine transform"),
        });
    let index = crate::index::ModelIndex::build(&ir);
    assert_eq!(
        model_curve_point_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &replica,
            f64::MAX
        ),
        Err(EvaluationFailure::NonFinite(Point3::new(
            f64::INFINITY,
            2.0,
            0.0
        )))
    );
    assert_eq!(
        model_curve_point_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &replica,
            -f64::MAX
        )
        .map(FinitePoint3::get),
        Ok(Point3::new(0.0, 2.0, 0.0))
    );
}

#[test]
fn a_subset_curve_whose_span_overflows_keeps_its_finite_local_point() {
    let line = CurveGeometry::Solved(SolvedCurveGeometry::Line(
        LineCurve::try_new(Point3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0))
            .expect("line fixture"),
    ));
    let (ir, subset) = procedural_curve_model(vec![line], |curves| {
        ProceduralCurveDefinition::Subset(
            crate::geometry::curve_payloads::SubsetCurveConstruction::try_new(
                curves[0].clone(),
                [-f64::MAX, f64::MAX],
                true,
                None,
            )
            .expect("subset fixture"),
        )
    });
    let index = crate::index::ModelIndex::build(&ir);
    assert_eq!(
        model_curve_point_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &subset,
            1.0
        )
        .map(FinitePoint3::get),
        Ok(Point3::new(-f64::MAX, 0.0, 0.0))
    );
    assert_eq!(
        model_curve_point_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &subset,
            f64::MAX
        )
        .map(FinitePoint3::get),
        Ok(Point3::new(0.0, 0.0, 0.0))
    );
    assert_eq!(
        model_curve_point_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &subset,
            -1.0
        ),
        Err(EvaluationFailure::NoValue)
    );
}

/// A helix over the angles `[0, 1]` whose major and minor vectors reach
/// `0.9 MAX` and whose `apex_factor` scales its radius: at angle 0 the point
/// `(0.9 MAX, 0, 0)` is finite, the tangent's radial term is
/// `apex_factor / TAU * 0.9 MAX` along `x` and the acceleration's
/// `2 apex_factor / TAU * 0.9 MAX` along `y`.
fn large_helix(apex_factor: f64) -> ProceduralCurveDefinition {
    ProceduralCurveDefinition::Helix(
        HelixCurveConstruction::try_new(
            [0.0, 1.0],
            HelixFrame {
                center: Point3::new(0.0, 0.0, 0.0),
                major: Vector3::new(0.9 * f64::MAX, 0.0, 0.0),
                minor: Vector3::new(0.0, 0.9 * f64::MAX, 0.0),
                pitch: Vector3::new(0.0, 0.0, 1.0),
                axis: Vector3::new(0.0, 0.0, 1.0),
            },
            apex_factor,
            None,
        )
        .expect("helix fixture"),
    )
}

#[test]
fn a_helix_whose_tangent_overflows_has_its_finite_point() {
    // The tangent at angle 0 leaves the finite range; the point reads no
    // tangent.
    let (ir, helix) = procedural_curve_model(Vec::new(), |_| large_helix(10.0));
    let index = crate::index::ModelIndex::build(&ir);
    assert_eq!(
        model_curve_point_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &helix,
            0.0
        )
        .map(FinitePoint3::get),
        Ok(Point3::new(0.9 * f64::MAX, 0.0, 0.0))
    );
    assert_eq!(
        model_curve_point_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &helix,
            2.0
        ),
        Err(EvaluationFailure::NoValue)
    );
}

#[test]
fn an_extrusion_over_a_helix_whose_tangent_overflows_has_its_finite_point() {
    // The helix directrix is finite at angle 0 and its tangent overflows; the
    // extrusion point reads the directrix point only.
    let (mut ir, helix) = procedural_curve_model(Vec::new(), |_| large_helix(10.0));
    let surface = SurfaceId::mint("test:model:surface#extrusion").expect("valid identity");
    let construction =
        ProceduralSurfaceId::mint("test:model:procedural#extrusion").expect("valid identity");
    let geometry = SurfaceGeometry::Procedural {
        construction: construction.clone(),
        cache: None,
    };
    ir.model.surfaces.push(Surface {
        id: surface.clone(),
        geometry: geometry.clone(),
        source_object: None,
    });
    ir.model.procedural_surfaces.push(ProceduralSurface::new(
        construction,
        ProceduralSurfaceDefinition::Extrusion(
            ExtrusionSurfaceConstruction::try_new(
                helix,
                None,
                Vector3::new(0.0, 0.0, 1.0),
                None,
                CacheContract::from_form(None),
            )
            .expect("extrusion fixture"),
        ),
        None,
    ));
    for route in surface_routes(&ir, &surface, &geometry, 0.0, 2.0) {
        assert_eq!(
            route.map(FinitePoint3::get),
            Ok(Point3::new(0.9 * f64::MAX, 0.0, 2.0))
        );
    }
}

#[test]
fn a_ruled_surface_whose_rail_point_overflows_reports_the_point_its_rule_reaches() {
    // The first rail reaches x = +inf; the rule toward the finite second
    // rail at v = 0.5 reaches no finite x.
    let second = CurveGeometry::Solved(SolvedCurveGeometry::Line(
        LineCurve::try_new(Point3::new(0.0, 0.0, 1.0), Vector3::new(0.0, 1.0, 0.0))
            .expect("line fixture"),
    ));
    let (ir, surface, geometry) =
        curve_surface_model(vec![overflowing_placed_line(), second], |curves| {
            ProceduralSurfaceDefinition::Ruled {
                first: curves[0].clone(),
                second: curves[1].clone(),
                cache: None,
            }
        });
    for route in surface_routes(&ir, &surface, &geometry, 3.0, 0.5) {
        assert!(
            matches!(
                route,
                Err(EvaluationFailure::NonFinite(point))
                    if point.x.is_nan() && point.y == 3.0 && point.z == 0.5
            ),
            "{route:?}"
        );
    }
}

#[test]
fn a_sum_surface_whose_curve_point_overflows_reports_the_point_its_sum_reaches() {
    let second = CurveGeometry::Solved(SolvedCurveGeometry::Line(
        LineCurve::try_new(Point3::new(0.0, 0.0, 0.0), Vector3::new(0.0, 0.0, 1.0))
            .expect("line fixture"),
    ));
    let (ir, surface, geometry) =
        curve_surface_model(vec![overflowing_placed_line(), second], |curves| {
            ProceduralSurfaceDefinition::Sum(
                SumSurfaceConstruction::try_new(
                    curves[0].clone(),
                    curves[1].clone(),
                    Vector3::new(0.0, 0.0, 0.0),
                    CacheContract::from_form(None),
                )
                .expect("sum fixture"),
            )
        });
    for route in surface_routes(&ir, &surface, &geometry, 3.0, 2.0) {
        assert_eq!(
            route,
            Err(EvaluationFailure::NonFinite(Point3::new(
                f64::INFINITY,
                3.0,
                2.0
            )))
        );
    }
}

#[test]
fn a_linear_sweep_whose_directrix_point_overflows_reports_the_point_its_sweep_reaches() {
    let (ir, surface, geometry) = curve_surface_model(vec![edge_line()], |curves| {
        ProceduralSurfaceDefinition::LinearSweep(
            LinearSweepSurfaceConstruction::try_new(curves[0].clone(), Vector3::new(0.0, 0.0, 1.0))
                .expect("linear sweep fixture"),
        )
    });
    for route in surface_routes(&ir, &surface, &geometry, f64::MAX, 2.0) {
        assert!(
            matches!(
                route,
                Err(EvaluationFailure::NonFinite(point))
                    if point.x.is_nan() && point.y == 0.0 && point.z == 2.0
            ),
            "{route:?}"
        );
    }
    // A sweep distance that is not finite has no value.
    for route in surface_routes(&ir, &surface, &geometry, 0.0, f64::NAN) {
        assert_eq!(route, Err(EvaluationFailure::NoValue));
    }
}

#[test]
fn a_revolution_whose_directrix_point_overflows_reports_the_point_its_revolution_reaches() {
    // The directrix reaches x = +inf; its revolution by 0 about the z axis
    // keeps no finite x.
    let (ir, surface, geometry) = curve_surface_model(vec![overflowing_placed_line()], |curves| {
        ProceduralSurfaceDefinition::Revolution(
            RevolutionSurfaceConstruction::try_new(
                curves[0].clone(),
                (
                    FinitePoint3::new(Point3::new(0.0, 0.0, 0.0)).expect("finite origin"),
                    crate::units::UnitVector3::new(Vector3::new(0.0, 0.0, 1.0)).expect("unit axis"),
                ),
                [0.0, std::f64::consts::TAU],
                None,
                None,
                false,
                CacheContract::from_form(None),
            )
            .expect("revolution fixture"),
        )
    });
    for route in surface_routes(&ir, &surface, &geometry, 3.0, 0.0) {
        assert!(
            matches!(route, Err(EvaluationFailure::NonFinite(point)) if !point.x.is_finite()),
            "{route:?}"
        );
    }
}

#[test]
fn an_axis_revolution_whose_directrix_point_overflows_reports_the_point_its_revolution_reaches() {
    let (ir, surface, geometry) = curve_surface_model(vec![overflowing_placed_line()], |curves| {
        ProceduralSurfaceDefinition::AxisRevolution(
            AxisRevolutionSurfaceConstruction::try_new(
                curves[0].clone(),
                Point3::new(0.0, 0.0, 0.0),
                Vector3::new(0.0, 0.0, 1.0),
            )
            .expect("axis revolution fixture"),
        )
    });
    for route in surface_routes(&ir, &surface, &geometry, 0.0, 3.0) {
        assert!(
            matches!(route, Err(EvaluationFailure::NonFinite(point)) if !point.x.is_finite()),
            "{route:?}"
        );
    }
}

#[test]
fn an_offset_pcurve_whose_basis_acceleration_overflows_reports_its_tangent_as_left_the_finite_range(
) {
    // The circle of radius MAX about (-MAX, 0) on the axes (1, 0) and (1, 1)
    // has at t = pi / 4 the finite point (0.41 MAX, 0.71 MAX) and tangent
    // (0, 0.71 MAX); its acceleration -0.71 MAX (2, 1) overflows in u.
    let basis = PcurveGeometry::Circle(
        CirclePcurve::try_new(
            Point2::new(-f64::MAX, 0.0),
            Point2::new(1.0, 0.0),
            Point2::new(1.0, 1.0),
            f64::MAX,
        )
        .expect("circle pcurve fixture"),
    );
    let parameter = std::f64::consts::FRAC_PI_4;
    assert!(pcurve_tangent(
        crate::eval::admission::EvaluationAdmission::Standard,
        &basis,
        parameter
    )
    .is_ok());
    let offset = PcurveGeometry::Offset(
        OffsetPcurve::try_new(1.0, Box::new(basis)).expect("offset pcurve fixture"),
    );
    assert!(crate::eval::decode::pcurve_uv(
        crate::eval::admission::EvaluationAdmission::Standard,
        &offset,
        parameter
    )
    .is_ok());
    assert!(matches!(
        pcurve_tangent(crate::eval::admission::EvaluationAdmission::Standard, &offset, parameter),
        Err(EvaluationFailure::NonFinite(tangent)) if tangent.u.is_nan() && tangent.v.is_nan()
    ));
}

#[test]
fn an_offset_pcurve_over_an_offset_basis_has_no_tangent() {
    // An offset states no second derivative, so an offset over it forms no
    // tangent.
    let inner = PcurveGeometry::Offset(
        OffsetPcurve::try_new(
            1.0,
            Box::new(PcurveGeometry::Line(
                LinePcurve::try_new(Point2::new(0.0, 0.0), Point2::new(1.0, 0.0))
                    .expect("line pcurve fixture"),
            )),
        )
        .expect("offset pcurve fixture"),
    );
    let outer =
        PcurveGeometry::Offset(OffsetPcurve::try_new(1.0, Box::new(inner)).expect("offset pcurve"));
    assert_eq!(
        crate::eval::decode::pcurve_uv(
            crate::eval::admission::EvaluationAdmission::Standard,
            &outer,
            0.5
        )
        .map(crate::units::FinitePoint2::get),
        Ok(Point2::new(0.5, 2.0))
    );
    assert_eq!(
        pcurve_tangent(
            crate::eval::admission::EvaluationAdmission::Standard,
            &outer,
            0.5
        ),
        Err(EvaluationFailure::NoValue)
    );
}

#[test]
fn an_extrusion_whose_directrix_tangent_overflows_has_its_finite_point_on_every_route() {
    // The line through the origin along (1, 1, 0) / sqrt(2), placed by rows
    // whose first sums the two lanes at the largest finite scale: its point
    // at t = 0 is the origin, and its tangent reaches x = MAX * sqrt(2),
    // outside the finite range. The extrusion point by v = 2 along z reads no
    // tangent.
    let directrix = CurveGeometry::Solved(SolvedCurveGeometry::Transformed(
        PlacedCurve::try_new(
            Box::new(SolvedCurveGeometry::Line(
                LineCurve::try_new(
                    Point3::new(0.0, 0.0, 0.0),
                    Vector3::new(
                        std::f64::consts::FRAC_1_SQRT_2,
                        std::f64::consts::FRAC_1_SQRT_2,
                        0.0,
                    ),
                )
                .expect("line fixture"),
            )),
            Transform::affine([
                [f64::MAX, f64::MAX, 0.0, 0.0],
                [0.0, 1.0, 0.0, 0.0],
                [0.0, 0.0, 1.0, 0.0],
            ])
            .expect("affine transform"),
        )
        .expect("placed curve fixture"),
    ));
    assert!(crate::eval::decode::curve_tangent(
        crate::eval::admission::EvaluationAdmission::Standard,
        &directrix,
        0.0
    )
    .is_err());
    let (ir, surface, geometry) = curve_surface_model(vec![directrix], |curves| {
        ProceduralSurfaceDefinition::Extrusion(
            ExtrusionSurfaceConstruction::try_new(
                curves[0].clone(),
                None,
                Vector3::new(0.0, 0.0, 1.0),
                None,
                CacheContract::from_form(None),
            )
            .expect("extrusion fixture"),
        )
    });
    for route in surface_routes(&ir, &surface, &geometry, 0.0, 2.0) {
        assert_eq!(route.map(FinitePoint3::get), Ok(Point3::new(0.0, 0.0, 2.0)));
    }
}

/// The circle of radius 2 about `(-2, 0, 0)` in the `xy` plane, placed by a
/// transform that scales `x` by `f64::MAX`: at `t = 0` its point is the
/// origin and its tangent `(0, 2, 0)`, and its acceleration `(-2, 0, 0)`
/// reaches `x = -inf`.
fn overflowing_acceleration_circle() -> CurveGeometry {
    CurveGeometry::Solved(SolvedCurveGeometry::Transformed(
        PlacedCurve::try_new(
            Box::new(SolvedCurveGeometry::Circle(
                crate::geometry::analytic::CircleCurve::try_new(
                    Point3::new(-2.0, 0.0, 0.0),
                    Vector3::new(0.0, 0.0, 1.0),
                    Vector3::new(1.0, 0.0, 0.0),
                    2.0,
                )
                .expect("circle fixture"),
            )),
            Transform::affine([
                [f64::MAX, 0.0, 0.0, 0.0],
                [0.0, 1.0, 0.0, 0.0],
                [0.0, 0.0, 1.0, 0.0],
            ])
            .expect("affine transform"),
        )
        .expect("placed curve fixture"),
    ))
}

/// The line through `origin` along `direction`.
fn line(origin: Point3, direction: Vector3) -> CurveGeometry {
    CurveGeometry::Solved(SolvedCurveGeometry::Line(
        LineCurve::try_new(origin, direction).expect("line fixture"),
    ))
}

/// A surface over [`overflowing_acceleration_circle`] has its point on every
/// route and its first partials `[du, dv]` there; its second partials leave
/// the finite range at the point.
fn assert_second_order_alone_overflows(
    (ir, surface, geometry): (CadIr, SurfaceId, SurfaceGeometry),
    (u, v): (f64, f64),
    point: Point3,
    [du, dv]: [Vector3; 2],
) {
    assert!(crate::eval::curve_second_derivative(
        crate::eval::admission::EvaluationAdmission::Standard,
        &overflowing_acceleration_circle(),
        0.0
    )
    .is_err());
    for route in surface_routes(&ir, &surface, &geometry, u, v) {
        assert_eq!(route.map(FinitePoint3::get), Ok(point));
    }
    let index = crate::index::ModelIndex::build(&ir);
    let budget = cadmpeg_core::decode::WorkBudget::new(1_000_000);
    let expected = Ok(crate::eval::SurfacePartials { point, du, dv });
    assert_eq!(
        crate::eval::model_surface_partials_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &surface,
            u,
            v
        )
        .map(crate::eval::SurfacePartials::into_raw),
        expected
    );
    assert_eq!(
        crate::eval::admission::EvaluationAdmission::Standard
            .within_work_slice(&budget, |admission| {
                crate::eval::model_surface_partials_by_id(admission, &index, &surface, u, v)
            })
            .map(crate::eval::SurfacePartials::into_raw),
        expected
    );
    assert_eq!(
        crate::eval::model_surface_second_partials_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &surface,
            u,
            v
        )
        .map(crate::eval::SurfaceSecondPartials::into_raw),
        Err(EvaluationFailure::NonFinite(point))
    );
}

#[test]
fn an_extrusion_whose_directrix_acceleration_overflows_keeps_its_point_and_first_partials() {
    assert_second_order_alone_overflows(
        curve_surface_model(vec![overflowing_acceleration_circle()], |curves| {
            ProceduralSurfaceDefinition::Extrusion(
                ExtrusionSurfaceConstruction::try_new(
                    curves[0].clone(),
                    None,
                    Vector3::new(0.0, 0.0, 1.0),
                    None,
                    CacheContract::from_form(None),
                )
                .expect("extrusion fixture"),
            )
        }),
        (0.0, 2.0),
        Point3::new(0.0, 0.0, 2.0),
        [Vector3::new(0.0, 2.0, 0.0), Vector3::new(0.0, 0.0, 1.0)],
    );
}

#[test]
fn a_linear_sweep_whose_directrix_acceleration_overflows_keeps_its_point_and_first_partials() {
    assert_second_order_alone_overflows(
        curve_surface_model(vec![overflowing_acceleration_circle()], |curves| {
            ProceduralSurfaceDefinition::LinearSweep(
                LinearSweepSurfaceConstruction::try_new(
                    curves[0].clone(),
                    Vector3::new(0.0, 0.0, 1.0),
                )
                .expect("linear sweep fixture"),
            )
        }),
        (0.0, 2.0),
        Point3::new(0.0, 0.0, 2.0),
        [Vector3::new(0.0, 2.0, 0.0), Vector3::new(0.0, 0.0, 1.0)],
    );
}

#[test]
fn a_ruled_surface_whose_rail_acceleration_overflows_keeps_its_point_and_first_partials() {
    // Halfway from the circle's origin to the line's (0, 0, 2) along y.
    assert_second_order_alone_overflows(
        curve_surface_model(
            vec![
                overflowing_acceleration_circle(),
                line(Point3::new(0.0, 0.0, 2.0), Vector3::new(0.0, 1.0, 0.0)),
            ],
            |curves| ProceduralSurfaceDefinition::Ruled {
                first: curves[0].clone(),
                second: curves[1].clone(),
                cache: None,
            },
        ),
        (0.0, 0.5),
        Point3::new(0.0, 0.0, 1.0),
        [Vector3::new(0.0, 1.5, 0.0), Vector3::new(0.0, 0.0, 2.0)],
    );
}

#[test]
fn a_sum_surface_whose_curve_acceleration_overflows_keeps_its_point_and_first_partials() {
    assert_second_order_alone_overflows(
        curve_surface_model(
            vec![
                overflowing_acceleration_circle(),
                line(Point3::new(0.0, 0.0, 0.0), Vector3::new(0.0, 0.0, 1.0)),
            ],
            |curves| {
                ProceduralSurfaceDefinition::Sum(
                    SumSurfaceConstruction::try_new(
                        curves[0].clone(),
                        curves[1].clone(),
                        Vector3::new(0.0, 0.0, 0.0),
                        CacheContract::from_form(None),
                    )
                    .expect("sum fixture"),
                )
            },
        ),
        (0.0, 2.0),
        Point3::new(0.0, 0.0, 2.0),
        [Vector3::new(0.0, 2.0, 0.0), Vector3::new(0.0, 0.0, 1.0)],
    );
}

#[test]
fn a_revolution_whose_directrix_acceleration_overflows_keeps_its_point_and_first_partials() {
    // The origin revolved by angle 0 about the z axis through (1, 0, 0).
    assert_second_order_alone_overflows(
        curve_surface_model(vec![overflowing_acceleration_circle()], |curves| {
            ProceduralSurfaceDefinition::Revolution(
                RevolutionSurfaceConstruction::try_new(
                    curves[0].clone(),
                    (
                        FinitePoint3::new(Point3::new(1.0, 0.0, 0.0)).expect("finite origin"),
                        crate::units::UnitVector3::new(Vector3::new(0.0, 0.0, 1.0))
                            .expect("unit axis"),
                    ),
                    [0.0, std::f64::consts::TAU],
                    None,
                    None,
                    false,
                    CacheContract::from_form(None),
                )
                .expect("revolution fixture"),
            )
        }),
        (0.0, 0.0),
        Point3::new(0.0, 0.0, 0.0),
        [Vector3::new(0.0, 2.0, 0.0), Vector3::new(0.0, -1.0, 0.0)],
    );
}

#[test]
fn an_axis_revolution_whose_directrix_acceleration_overflows_keeps_its_point_and_first_partials() {
    // The angle is u and the directrix parameter v.
    assert_second_order_alone_overflows(
        curve_surface_model(vec![overflowing_acceleration_circle()], |curves| {
            ProceduralSurfaceDefinition::AxisRevolution(
                AxisRevolutionSurfaceConstruction::try_new(
                    curves[0].clone(),
                    Point3::new(1.0, 0.0, 0.0),
                    Vector3::new(0.0, 0.0, 1.0),
                )
                .expect("axis revolution fixture"),
            )
        }),
        (0.0, 0.0),
        Point3::new(0.0, 0.0, 0.0),
        [Vector3::new(0.0, -1.0, 0.0), Vector3::new(0.0, 2.0, 0.0)],
    );
}

#[test]
fn a_hyperbola_whose_scaled_cosh_alone_overflows_keeps_its_point_and_second_derivative() {
    // With major radius 1 and minor radius MAX, the point and second
    // derivative read cosh(t) and MAX sinh(t), both finite at t = 1e-7; the
    // tangent reads MAX cosh(t), which overflows.
    let hyperbola = CurveGeometry::Solved(SolvedCurveGeometry::Hyperbola(
        crate::geometry::analytic::HyperbolaCurve::try_new(
            Point3::new(0.0, 0.0, 0.0),
            Vector3::new(0.0, 0.0, 1.0),
            Vector3::new(1.0, 0.0, 0.0),
            1.0,
            f64::MAX,
        )
        .expect("hyperbola fixture"),
    ));
    let t = 1.0e-7_f64;
    let lanes = |value: Vector3| {
        assert_eq!(value.x, t.cosh());
        assert!((value.y / (f64::MAX * t.sinh()) - 1.0).abs() <= 4.0 * f64::EPSILON);
        assert_eq!(value.z, 0.0);
    };
    lanes(
        crate::eval::decode::curve_point(
            crate::eval::admission::EvaluationAdmission::Standard,
            &hyperbola,
            t,
        )
        .expect("point")
        .get()
        .vector_from(Point3::new(0.0, 0.0, 0.0)),
    );
    lanes(
        crate::eval::curve_second_derivative(
            crate::eval::admission::EvaluationAdmission::Standard,
            &hyperbola,
            t,
        )
        .expect("second derivative")
        .get(),
    );
    assert_eq!(
        crate::eval::decode::curve_tangent(
            crate::eval::admission::EvaluationAdmission::Standard,
            &hyperbola,
            t
        ),
        Err(EvaluationFailure::NonFinite(()))
    );
}

#[test]
fn an_extrusion_over_a_helix_whose_acceleration_overflows_keeps_its_first_partials() {
    // With apex factor 5 the tangent's radial term 5 / TAU * 0.9 MAX is
    // finite and the acceleration's 10 / TAU * 0.9 MAX overflows.
    let (mut ir, helix) = procedural_curve_model(Vec::new(), |_| large_helix(5.0));
    let surface = SurfaceId::mint("test:model:surface#extrusion").expect("valid identity");
    let construction =
        ProceduralSurfaceId::mint("test:model:procedural#extrusion").expect("valid identity");
    ir.model.surfaces.push(Surface {
        id: surface.clone(),
        geometry: SurfaceGeometry::Procedural {
            construction: construction.clone(),
            cache: None,
        },
        source_object: None,
    });
    ir.model.procedural_surfaces.push(ProceduralSurface::new(
        construction,
        ProceduralSurfaceDefinition::Extrusion(
            ExtrusionSurfaceConstruction::try_new(
                helix,
                None,
                Vector3::new(0.0, 0.0, 1.0),
                None,
                CacheContract::from_form(None),
            )
            .expect("extrusion fixture"),
        ),
        None,
    ));
    let index = crate::index::ModelIndex::build(&ir);
    let point = Point3::new(0.9 * f64::MAX, 0.0, 2.0);
    let partials = crate::eval::model_surface_partials_by_id(
        crate::eval::admission::EvaluationAdmission::Standard,
        &index,
        &surface,
        0.0,
        2.0,
    )
    .expect("first partials")
    .into_raw();
    assert_eq!(partials.point, point);
    let radial = 5.0 / std::f64::consts::TAU * 0.9 * f64::MAX;
    assert!((partials.du.x / radial - 1.0).abs() <= 8.0 * f64::EPSILON);
    assert!((partials.du.y / (0.9 * f64::MAX) - 1.0).abs() <= 8.0 * f64::EPSILON);
    assert!((partials.du.z * std::f64::consts::TAU - 1.0).abs() <= 8.0 * f64::EPSILON);
    assert_eq!(partials.dv, Vector3::new(0.0, 0.0, 1.0));
    assert_eq!(
        crate::eval::model_surface_second_partials_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &surface,
            0.0,
            2.0
        )
        .map(crate::eval::SurfaceSecondPartials::into_raw),
        Err(EvaluationFailure::NonFinite(point))
    );
}

#[test]
fn a_helix_whose_axis_length_overflows_has_its_point() {
    // The helix reads its pitch, not its axis; an axis whose length
    // overflows leaves the point as it is.
    let (ir, helix) = procedural_curve_model(Vec::new(), |_| {
        ProceduralCurveDefinition::Helix(
            HelixCurveConstruction::try_new(
                [0.0, 1.0],
                HelixFrame {
                    center: Point3::new(0.0, 0.0, 0.0),
                    major: Vector3::new(1.0, 0.0, 0.0),
                    minor: Vector3::new(0.0, 1.0, 0.0),
                    pitch: Vector3::new(0.0, 0.0, 1.0),
                    axis: Vector3::new(f64::MAX, f64::MAX, 0.0),
                },
                0.0,
                None,
            )
            .expect("helix fixture"),
        )
    });
    let index = crate::index::ModelIndex::build(&ir);
    assert_eq!(
        model_curve_point_by_id(
            crate::eval::admission::EvaluationAdmission::Standard,
            &index,
            &helix,
            0.0
        )
        .map(FinitePoint3::get),
        Ok(Point3::new(1.0, 0.0, 0.0))
    );
}