cadmpeg-ir 0.5.5

CAD data model, validation, diffing, and codec API for cadmpeg.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// SPDX-License-Identifier: Apache-2.0

use super::*;

fn variable_blend_eval_fixture(
    second_origin: Point3,
    pcurves: [(Point2, Point2); 2],
    radii: [f64; 2],
    cross_section: Option<VariableBlendCrossSection>,
) -> (CadIr, SurfaceId) {
    let first_surface = SurfaceId("first-support".into());
    let second_surface = SurfaceId("second-support".into());
    let blend_surface = SurfaceId("cacheless-variable-blend".into());
    let slice = CurveId("blend-slice".into());
    let mut ir = CadIr::empty(crate::units::Units::default());
    ir.model.curves.push(Curve {
        id: slice.clone(),
        geometry: CurveGeometry::Line {
            origin: Point3::new(0.0, 0.0, 0.0),
            direction: Vector3::new(0.0, 1.0, 0.0),
        },
        source_object: None,
    });
    ir.model.surfaces.extend([
        Surface {
            id: first_surface.clone(),
            geometry: SurfaceGeometry::Plane {
                origin: Point3::new(0.0, 0.0, 0.0),
                normal: Vector3::new(0.0, 0.0, 1.0),
                u_axis: Vector3::new(1.0, 0.0, 0.0),
            },
            source_object: None,
        },
        Surface {
            id: second_surface.clone(),
            geometry: SurfaceGeometry::Plane {
                origin: second_origin,
                normal: Vector3::new(1.0, 0.0, 0.0),
                u_axis: Vector3::new(0.0, 1.0, 0.0),
            },
            source_object: None,
        },
        Surface {
            id: blend_surface.clone(),
            geometry: SurfaceGeometry::Procedural {
                construction: ProceduralSurfaceId("variable-blend-construction".into()),
            },
            source_object: None,
        },
    ]);
    let side = |surface, origin: Point2, direction: Point2| RollingBallSide {
        support_kind: VariableBlendSupportKind::Surface,
        surface: Some(surface),
        surface_ranges: [[None, None], [None, None]],
        curve: None,
        curve_range: [None, None],
        pcurve: Some(PcurveGeometry::Line { origin, direction }),
        location: Point3::new(0.0, 0.0, 0.0),
        secondary_pcurve: None,
        extension: None,
        tertiary_pcurve: None,
    };
    let radius = VariableBlendValue {
        name: "two_ends".into(),
        modern_flag: false,
        discriminator: 0,
        calibrated: 0,
        payload: VariableBlendValuePayload::TwoEnds {
            parameters: [0.0, 1.0],
            radii,
        },
    };
    ir.model.procedural_surfaces.push(ProceduralSurface {
        id: ProceduralSurfaceId("variable-blend-construction".into()),
        surface: blend_surface.clone(),
        definition: ProceduralSurfaceDefinition::VariableBlend {
            construction: Box::new(VariableBlendConstruction {
                subtype: VariableBlendSurfaceSubtype::VariableBlend,
                revision: 23100,
                sides: Box::new([
                    side(first_surface, pcurves[0].0, pcurves[0].1),
                    side(second_surface, pcurves[1].0, pcurves[1].1),
                ]),
                slice,
                slice_range: [Some(0.0), Some(1.0)],
                offsets: [0.0, 0.0],
                radius_kind: VariableBlendRadiusKind::SingleRadius,
                first_value: radius,
                second_value: None,
                cross_section,
                u_range: [Some(0.0), Some(1.0)],
                v_range: [Some(0.0), Some(1.0)],
                shape_prefix: 1,
                shape_parameter: 0.0,
                shape_length: 0.0,
                shape_tail: 0,
                tail_enum: 2,
                tail_parameterization: Some(RevisionSurfaceParameterization {
                    u_interval: [Some(0.0), Some(1.0)],
                    v_interval: [Some(0.0), Some(1.0)],
                    ..Default::default()
                }),
                discontinuities: std::array::from_fn(|_| Vec::new()),
                tail_flag: false,
                tail_extensions: [0; 3],
                secondary_curve: None,
                secondary_range: [None, None],
                convexity: VariableBlendConvexity::Convex,
                render_mode: VariableBlendRenderMode::RollingBallEnvelope,
                post_range: [None, None],
                post_curve: None,
                post_pcurve: None,
            }),
        },
        cache_fit_tolerance: None,
        record_bounds: None,
    });
    (ir, blend_surface)
}

#[test]
fn cacheless_zero_radius_rounded_chamfer_is_ruled_between_contact_tracks() {
    let (mut ir, blend_surface) = variable_blend_eval_fixture(
        Point3::new(10.0, 0.0, 0.0),
        [
            (Point2::new(1.0, 2.0), Point2::new(2.0, 3.0)),
            (Point2::new(4.0, 5.0), Point2::new(6.0, 7.0)),
        ],
        [2.0, 2.0],
        Some(VariableBlendCrossSection::RoundedChamfer { radius: None }),
    );

    let index = crate::index::ModelIndex::new(&ir);
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 0.0, 0.5),
        Some(Point3::new(2.0, 3.5, 0.0))
    );
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 1.0, 0.5),
        Some(Point3::new(10.0, 7.0, 8.5))
    );
    assert_eq!(
        model_surface_point(&ir, &ir.model.surfaces[2].geometry, 0.25, 0.5),
        Some(Point3::new(4.0, 4.375, 2.125))
    );
    let partials = model_surface_partials_by_id(&index, &blend_surface, 0.25, 0.5)
        .expect("cacheless ruled variable blend");
    assert_eq!(partials.point, Point3::new(4.0, 4.375, 2.125));
    assert_eq!(partials.du, Vector3::new(8.0, 3.5, 8.5));
    assert_eq!(partials.dv, Vector3::new(1.5, 3.75, 1.75));

    {
        let ProceduralSurfaceDefinition::VariableBlend { construction } =
            &mut ir.model.procedural_surfaces[0].definition
        else {
            unreachable!()
        };
        construction.radius_kind = VariableBlendRadiusKind::TwoRadii;
        construction.second_value = Some(construction.first_value.clone());
    }
    let index = crate::index::ModelIndex::new(&ir);
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 0.25, 0.5),
        Some(Point3::new(4.0, 4.375, 2.125))
    );

    let zero_radius = VariableBlendValue {
        name: "two_ends".into(),
        modern_flag: false,
        discriminator: 0,
        calibrated: 0,
        payload: VariableBlendValuePayload::TwoEnds {
            parameters: [0.0, 1.0],
            radii: [0.0, 0.0],
        },
    };
    if let ProceduralSurfaceDefinition::VariableBlend { construction } =
        &mut ir.model.procedural_surfaces[0].definition
    {
        construction.cross_section = Some(VariableBlendCrossSection::RoundedChamfer {
            radius: Some(Box::new(zero_radius.clone())),
        });
    } else {
        unreachable!()
    }
    let index = crate::index::ModelIndex::new(&ir);
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 0.25, 0.5),
        Some(Point3::new(4.0, 4.375, 2.125))
    );
    assert!(variable_blend_is_zero_radius(&VariableBlendValue {
        name: "constant".into(),
        modern_flag: false,
        discriminator: 0,
        calibrated: 0,
        payload: VariableBlendValuePayload::Constant {
            parameters: [0.0, 0.0],
            radius: 0.0,
            variable_chamfer: 0,
            chamfer_type: 0,
            nested: Box::new(zero_radius),
        },
    }));
}

#[test]
fn current_variable_blend_uses_the_solved_cache_for_points_and_partials() {
    let (mut ir, blend_surface) = variable_blend_eval_fixture(
        Point3::new(10.0, 0.0, 0.0),
        [
            (Point2::new(1.0, 2.0), Point2::new(2.0, 3.0)),
            (Point2::new(4.0, 5.0), Point2::new(6.0, 7.0)),
        ],
        [2.0, 2.0],
        Some(VariableBlendCrossSection::G2Round {
            parameters: [1.0, 1.0],
        }),
    );
    ir.model.surfaces[2].geometry = SurfaceGeometry::Nurbs(bilinear_surface());
    {
        let ProceduralSurfaceDefinition::VariableBlend { construction } =
            &mut ir.model.procedural_surfaces[0].definition
        else {
            unreachable!()
        };
        construction.shape_prefix = 1;
        construction.tail_enum = 0;
        construction.tail_parameterization = None;
    }

    let index = crate::index::ModelIndex::new(&ir);
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 0.25, 0.5),
        Some(Point3::new(0.25, 0.5, 0.0))
    );
    let partials = model_surface_partials_by_id(&index, &blend_surface, 0.25, 0.5)
        .expect("current variable blend cache partials");
    assert_eq!(partials.point, Point3::new(0.25, 0.5, 0.0));
    assert_eq!(partials.du, Vector3::new(1.0, 0.0, 0.0));
    assert_eq!(partials.dv, Vector3::new(0.0, 1.0, 0.0));

    if let ProceduralSurfaceDefinition::VariableBlend { construction } =
        &mut ir.model.procedural_surfaces[0].definition
    {
        construction.shape_prefix = 0;
    } else {
        unreachable!()
    }
    let index = crate::index::ModelIndex::new(&ir);
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 0.25, 0.5),
        None
    );
    assert!(model_surface_partials_by_id(&index, &blend_surface, 0.25, 0.5).is_none());
}

#[test]
fn cacheless_circular_variable_blend_uses_the_common_contact_center() {
    let (ir, blend_surface) = variable_blend_eval_fixture(
        Point3::new(0.0, 0.0, 0.0),
        [
            (Point2::new(2.0, 0.0), Point2::new(2.0, 1.0)),
            (Point2::new(0.0, 2.0), Point2::new(1.0, 2.0)),
        ],
        [2.0, 4.0],
        Some(VariableBlendCrossSection::Circular),
    );
    let index = crate::index::ModelIndex::new(&ir);
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 0.0, 0.5),
        Some(Point3::new(3.0, 0.5, 0.0))
    );
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 1.0, 0.5),
        Some(Point3::new(0.0, 0.5, 3.0))
    );
    let point = model_surface_point_by_id(&index, &blend_surface, 0.5, 0.5)
        .expect("cacheless circular variable blend");
    let expected = 3.0 - 3.0 / 2.0_f64.sqrt();
    let tolerance = 64.0 * f64::EPSILON;
    assert!((point.x - expected).abs() <= tolerance);
    assert!((point.y - 0.5).abs() <= tolerance);
    assert!((point.z - expected).abs() <= tolerance);

    let partials = model_surface_partials_by_id(&index, &blend_surface, 0.5, 0.5)
        .expect("cacheless circular variable-blend partials");
    let derivative = 3.0 * std::f64::consts::FRAC_PI_2 / 2.0_f64.sqrt();
    let derivative_tolerance = 128.0 * f64::EPSILON;
    assert!((partials.point.x - expected).abs() <= derivative_tolerance);
    assert!((partials.point.y - 0.5).abs() <= derivative_tolerance);
    assert!((partials.point.z - expected).abs() <= derivative_tolerance);
    assert!((partials.du.x + derivative).abs() <= derivative_tolerance);
    assert!(partials.du.y.abs() <= derivative_tolerance);
    assert!((partials.du.z - derivative).abs() <= derivative_tolerance);
    let transverse = 2.0_f64.sqrt();
    assert!((partials.dv.x - (2.0 - transverse)).abs() <= derivative_tolerance);
    assert!((partials.dv.y - 1.0).abs() <= derivative_tolerance);
    assert!((partials.dv.z - (2.0 - transverse)).abs() <= derivative_tolerance);
}

#[test]
fn cacheless_circular_variable_blend_rejects_an_undetermined_center_tangent() {
    let (mut ir, blend_surface) = variable_blend_eval_fixture(
        Point3::new(0.0, 0.0, 0.0),
        [
            (Point2::new(2.0, 0.0), Point2::new(2.0, 1.0)),
            (Point2::new(0.0, 2.0), Point2::new(1.0, 2.0)),
        ],
        [2.0, 4.0],
        Some(VariableBlendCrossSection::Circular),
    );
    let ProceduralSurfaceDefinition::VariableBlend { construction } =
        &mut ir.model.procedural_surfaces[0].definition
    else {
        unreachable!()
    };
    construction.sides[0].pcurve = Some(crate::geometry::PcurveGeometry::Line {
        origin: Point2::new(3.0, 0.0),
        direction: Point2::new(0.0, 1.0),
    });
    construction.sides[1].pcurve = Some(crate::geometry::PcurveGeometry::Line {
        origin: Point2::new(0.5, 2.0),
        direction: Point2::new(0.0, 2.0),
    });

    let index = crate::index::ModelIndex::new(&ir);
    let point = model_surface_point_by_id(&index, &blend_surface, 0.5, 0.5)
        .expect("contact centers still coincide at the sample");
    let expected = 3.0 - 3.0 / 2.0_f64.sqrt();
    let tolerance = 64.0 * f64::EPSILON;
    assert!((point.x - expected).abs() <= tolerance);
    assert!((point.y - 0.5).abs() <= tolerance);
    assert!((point.z - expected).abs() <= tolerance);
    assert!(model_surface_partials_by_id(&index, &blend_surface, 0.5, 0.5).is_none());
}

#[test]
fn cacheless_constant_rolling_ball_uses_its_spine_as_section_center() {
    let (mut ir, blend_surface) = variable_blend_eval_fixture(
        Point3::new(0.0, 0.0, 0.0),
        [
            (Point2::new(3.0, 0.0), Point2::new(0.0, 1.0)),
            (Point2::new(0.0, 3.0), Point2::new(1.0, 0.0)),
        ],
        [3.0, 3.0],
        Some(VariableBlendCrossSection::Circular),
    );
    ir.model.curves[0].geometry = CurveGeometry::Line {
        origin: Point3::new(3.0, 0.0, 3.0),
        direction: Vector3::new(0.0, 1.0, 0.0),
    };
    let ProceduralSurfaceDefinition::VariableBlend { construction } =
        &ir.model.procedural_surfaces[0].definition
    else {
        unreachable!()
    };
    let sides = construction.sides.clone();
    let slice = construction.slice.clone();
    let supports = sides.each_ref().map(|side| {
        side.surface.as_ref().map(|surface| BlendSupport {
            surface: surface.clone(),
            reversed: false,
        })
    });
    ir.model.procedural_surfaces[0].definition = ProceduralSurfaceDefinition::Blend {
        supports,
        spine: Some(slice.clone()),
        radius: BlendRadiusLaw::Constant { signed_radius: 3.0 },
        cross_section: BlendCrossSection::Circular,
        native: Some(Box::new(RollingBallConstruction {
            definition_index: 0,
            sides,
            slice,
            slice_range: [Some(0.0), Some(1.0)],
            offsets: [3.0, 3.0],
            radius_selector: RollingBallRadiusSelector::None,
            u_range: [Some(0.0), Some(1.0)],
            v_range: [Some(0.0), Some(1.0)],
            shape_prefix: 0,
            parameters: [0.0, 0.0],
            tail: 0,
            tail_enum: 2,
            tail_parameterization: Some(RevisionSurfaceParameterization {
                u_interval: [Some(0.0), Some(1.0)],
                v_interval: [Some(0.0), Some(1.0)],
                ..Default::default()
            }),
            discontinuities: std::array::from_fn(|_| Vec::new()),
            tail_flag: false,
            third: None,
            tail_extensions: [0; 3],
        })),
    };

    let index = crate::index::ModelIndex::new(&ir);
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 0.0, 0.5),
        Some(Point3::new(3.0, 0.5, 0.0))
    );
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 1.0, 0.5),
        Some(Point3::new(0.0, 0.5, 3.0))
    );
    let point = model_surface_point_by_id(&index, &blend_surface, 0.5, 0.5)
        .expect("cacheless constant rolling-ball blend");
    let expected = 3.0 - 3.0 / 2.0_f64.sqrt();
    let tolerance = 64.0 * f64::EPSILON;
    assert!((point.x - expected).abs() <= tolerance);
    assert!((point.y - 0.5).abs() <= tolerance);
    assert!((point.z - expected).abs() <= tolerance);

    let partials = model_surface_partials_by_id(&index, &blend_surface, 0.5, 0.5)
        .expect("cacheless rolling-ball partials");
    let derivative = 3.0 * std::f64::consts::FRAC_PI_2 / 2.0_f64.sqrt();
    assert!((partials.point.x - expected).abs() <= tolerance);
    assert!((partials.point.y - 0.5).abs() <= tolerance);
    assert!((partials.point.z - expected).abs() <= tolerance);
    assert!((partials.du.x + derivative).abs() <= tolerance);
    assert!(partials.du.y.abs() <= tolerance);
    assert!((partials.du.z - derivative).abs() <= tolerance);
    assert!(partials.dv.x.abs() <= tolerance);
    assert!((partials.dv.y - 1.0).abs() <= tolerance);
    assert!(partials.dv.z.abs() <= tolerance);

    let replica_surface = SurfaceId("cacheless-rolling-ball-replica".into());
    let replica_construction = ProceduralSurfaceId("cacheless-rolling-ball-replica-def".into());
    ir.model.surfaces.push(Surface {
        id: replica_surface.clone(),
        geometry: SurfaceGeometry::Procedural {
            construction: replica_construction.clone(),
        },
        source_object: None,
    });
    ir.model.procedural_surfaces.push(ProceduralSurface {
        id: replica_construction,
        surface: replica_surface.clone(),
        definition: ProceduralSurfaceDefinition::Replica {
            source: blend_surface.clone(),
            transform: Transform {
                rows: [
                    [1.0, 0.0, 0.0, 10.0],
                    [0.0, 1.0, 0.0, 20.0],
                    [0.0, 0.0, 1.0, 30.0],
                    [0.0, 0.0, 0.0, 1.0],
                ],
            },
        },
        cache_fit_tolerance: None,
        record_bounds: None,
    });
    let index = crate::index::ModelIndex::new(&ir);
    let replica = model_surface_point_by_id(&index, &replica_surface, 0.5, 0.5)
        .expect("rolling-ball replica");
    assert!((replica.x - (expected + 10.0)).abs() <= tolerance);
    assert!((replica.y - 20.5).abs() <= tolerance);
    assert!((replica.z - (expected + 30.0)).abs() <= tolerance);

    ir.model.curves[0].geometry = CurveGeometry::Polyline {
        points: vec![
            Point3::new(2.0, 0.0, 3.0),
            Point3::new(3.0, 0.5, 3.0),
            Point3::new(3.0, 1.0, 3.0),
        ],
        parameters: Some(vec![0.0, 0.5, 1.0]),
        chordal_deflection: 0.0,
    };
    let index = crate::index::ModelIndex::new(&ir);
    assert!(model_surface_point_by_id(&index, &blend_surface, 0.5, 0.5).is_some());
    assert!(model_surface_partials_by_id(&index, &blend_surface, 0.5, 0.5).is_none());

    let ProceduralSurfaceDefinition::Blend {
        native: Some(native),
        ..
    } = &mut ir.model.procedural_surfaces[0].definition
    else {
        unreachable!()
    };
    native.offsets = [4.0, 4.0];
    let index = crate::index::ModelIndex::new(&ir);
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 0.5, 0.5),
        None
    );

    ir.model.surfaces[2].geometry = SurfaceGeometry::Nurbs(bilinear_surface());
    {
        let ProceduralSurfaceDefinition::Blend {
            native: Some(native),
            ..
        } = &mut ir.model.procedural_surfaces[0].definition
        else {
            unreachable!()
        };
        native.offsets = [3.0, 3.0];
        native.tail_enum = 0;
        native.tail_parameterization = None;
    }
    let index = crate::index::ModelIndex::new(&ir);
    assert_eq!(
        model_surface_point_by_id(&index, &blend_surface, 0.25, 0.5),
        Some(Point3::new(0.25, 0.5, 0.0))
    );
    let cached_partials = model_surface_partials_by_id(&index, &blend_surface, 0.25, 0.5)
        .expect("current rolling-ball cache partials");
    assert_eq!(cached_partials.point, Point3::new(0.25, 0.5, 0.0));
    assert_eq!(cached_partials.du, Vector3::new(1.0, 0.0, 0.0));
    assert_eq!(cached_partials.dv, Vector3::new(0.0, 1.0, 0.0));

    {
        let ProceduralSurfaceDefinition::Blend {
            native: Some(native),
            ..
        } = &mut ir.model.procedural_surfaces[0].definition
        else {
            unreachable!()
        };
        native.offsets = [4.0, 4.0];
        native.tail_enum = 2;
        native.tail_parameterization = Some(RevisionSurfaceParameterization::default());
    }
    let index = crate::index::ModelIndex::new(&ir);
    assert!(model_surface_point_by_id(&index, &blend_surface, 0.25, 0.5).is_none());
    assert!(model_surface_partials_by_id(&index, &blend_surface, 0.25, 0.5).is_none());
}

#[test]
fn rolling_ball_partials_follow_a_changing_section_angle() {
    let section_angle = std::f64::consts::FRAC_PI_3;
    let radius = 3.0;
    let zero = Vector3::new(0.0, 0.0, 0.0);
    let section = ConstantRollingBallSection {
        center: Point3::new(0.0, 0.0, 0.0),
        center_tangent: Some(zero),
        first: ContactTrackDifferential {
            point: Point3::new(radius, 0.0, 0.0),
            tangent: zero,
            normal: zero,
            normal_derivative: None,
        },
        second: ContactTrackDifferential {
            point: Point3::new(
                radius * section_angle.cos(),
                0.0,
                radius * section_angle.sin(),
            ),
            tangent: Vector3::new(
                -radius * section_angle.sin(),
                0.0,
                radius * section_angle.cos(),
            ),
            normal: zero,
            normal_derivative: None,
        },
        radius,
    };
    let u = 0.4;
    let angle = u * section_angle;
    let partials = constant_rolling_ball_partials(&section, u).expect("rolling-ball partials");
    let tolerance = 128.0 * f64::EPSILON;
    assert!((partials.point.x - radius * angle.cos()).abs() <= tolerance);
    assert!(partials.point.y.abs() <= tolerance);
    assert!((partials.point.z - radius * angle.sin()).abs() <= tolerance);
    assert!((partials.du.x + radius * section_angle * angle.sin()).abs() <= tolerance);
    assert!(partials.du.y.abs() <= tolerance);
    assert!((partials.du.z - radius * section_angle * angle.cos()).abs() <= tolerance);
    assert!((partials.dv.x + radius * u * angle.sin()).abs() <= tolerance);
    assert!(partials.dv.y.abs() <= tolerance);
    assert!((partials.dv.z - radius * u * angle.cos()).abs() <= tolerance);
}

#[test]
fn variable_blend_two_ends_radius_extrapolates_its_calibration_line() {
    let value = VariableBlendValue {
        name: "two_ends".into(),
        modern_flag: false,
        discriminator: 0,
        calibrated: 0,
        payload: VariableBlendValuePayload::TwoEnds {
            parameters: [2.0, 4.0],
            radii: [5.0, 9.0],
        },
    };
    assert_eq!(variable_blend_radius(&value, 2.0), Some(5.0));
    assert_eq!(variable_blend_radius(&value, 3.0), Some(7.0));
    assert_eq!(variable_blend_radius(&value, 5.0), Some(11.0));
}

#[test]
fn variable_blend_function_uses_its_first_coordinate_as_radius() {
    let value = VariableBlendValue {
        name: "functional".into(),
        modern_flag: false,
        discriminator: 0,
        calibrated: 0,
        payload: VariableBlendValuePayload::Functional {
            parameter: 0.0,
            radius: 0.0,
            function: PcurveGeometry::Line {
                origin: Point2::new(2.0, 100.0),
                direction: Point2::new(3.0, 200.0),
            },
            terminal: crate::geometry::LoftBridgeToken::Double(0.0),
        },
    };
    assert_eq!(variable_blend_radius(&value, 0.5), Some(3.5));
}