cadmpeg-ir 0.6.1

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
// SPDX-License-Identifier: Apache-2.0

#[test]
fn matching_polynomial_seed_avoids_full_control_and_interval_scratch() {
    use crate::geometry::nurbs::NurbsCurve;
    use crate::math::Point3;
    use cadmpeg_core::decode::{DecodeArena, DecodeContext, DecodePolicy};
    let preparation = cadmpeg_test_support::service_decode_context();
    let mut knots = vec![0.0; 4];
    knots.extend((1..1_021).map(|ordinal| f64::from(ordinal) / 1_021.0));
    knots.extend([1.0; 4]);
    let curve = NurbsCurve::from_lanes(
        &preparation,
        3,
        knots,
        (0..1_024)
            .map(|ordinal| Point3::new(f64::from(ordinal), 0.0, 0.0))
            .collect(),
        None,
        false,
    )
    .expect("admitted authored spline")
    .expect("valid authored spline");
    let point = crate::eval::decode::nurbs_curve_point_at(&preparation, &curve, 0.5)
        .expect("seed evaluation")
        .get();
    let mut policy = DecodePolicy::service();
    policy.limits.max_collection_items = 0;
    policy.limits.max_materialized_bytes = 0;
    policy.limits.max_work_units = 1_000;
    let arena = DecodeArena::new();
    let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).expect("empty input");
    assert_eq!(
        super::super::nurbs_curve_parameter_near_point(&ctx, &curve, point, 0.0, 0.5)
            .expect("matching witness needs no population-sized scratch"),
        Some(crate::scalar::FiniteReal::HALF)
    );
    ctx.finish_session().expect("all work remains admitted");
}

#[test]
fn matching_rational_seed_keeps_positive_weight_search_admission() {
    use crate::geometry::nurbs::NurbsCurve;
    use crate::math::Point3;
    let ctx = cadmpeg_test_support::service_decode_context();
    let point = Point3::new(0.0, 0.0, 0.0);
    let curve = NurbsCurve::from_lanes(
        &ctx,
        1,
        vec![0.0, 0.0, 1.0, 1.0],
        vec![point, Point3::new(1.0, 0.0, 0.0)],
        Some(vec![-1.0, 1.0]),
        false,
    )
    .expect("admitted signed-weight fixture")
    .expect("valid signed-weight carrier");
    assert_eq!(
        super::super::nurbs_curve_parameter_near_point(&ctx, &curve, point, 0.0, 0.0)
            .expect("signed weights have no certified search"),
        None
    );
}

#[test]
fn bounded_nurbs_interval_search_keeps_a_fixed_working_set() {
    let boundaries = (0..=10_000)
        .map(|index| crate::scalar::FiniteReal::from_index(index).expect("test index is exact"))
        .collect::<Vec<_>>();
    let seed = crate::scalar::FiniteReal::new(5_000.5).expect("finite seed");
    let ctx = cadmpeg_test_support::service_decode_context();
    let intervals = super::super::bounded_nearest_intervals(&ctx, &boundaries, seed)
        .expect("resource allocation did not fail");

    assert_eq!(intervals.0.len(), 512);
    assert!(intervals
        .0
        .iter()
        .any(|interval| interval.map(crate::scalar::FiniteReal::get) == [5_000.0, 5_001.0]));
}

#[test]
fn bounded_nurbs_containment_search_keeps_the_final_valid_spans() {
    let boundaries = [0.0, 1.0, 1.0, 2.0, 3.0];

    let ctx = cadmpeg_test_support::service_decode_context();
    let search = super::super::bounded_tail_intervals(&ctx, &boundaries)
        .expect("resource allocation did not fail");
    assert_eq!(
        (search.intervals, search.truncated),
        (vec![[0.0, 1.0], [1.0, 2.0], [2.0, 3.0]], false)
    );

    let many_boundaries = (0..=10_000).map(f64::from).collect::<Vec<_>>();
    let search = super::super::bounded_tail_intervals(&ctx, &many_boundaries)
        .expect("resource allocation did not fail");
    assert_eq!(search.intervals.len(), 512);
    assert!(search.truncated);
}

#[test]
fn bounded_nurbs_boundary_witness_preserves_seed_priority() {
    let boundaries = [0, 1, 2]
        .map(|index| crate::scalar::FiniteReal::from_index(index).expect("test index is exact"));
    let seed = crate::scalar::FiniteReal::new(1.4).expect("finite seed");

    assert_eq!(
        super::super::nearest_boundary_witness(
            &cadmpeg_test_support::service_decode_context(),
            &boundaries,
            seed,
            0.0,
            |_| Ok(Some(0.0))
        )
        .expect("resource allocation did not fail"),
        super::super::BoundaryWitness::Found(crate::scalar::FiniteReal::ONE)
    );
}

#[test]
fn boundary_witness_admits_each_visit_before_evaluation() {
    use crate::scalar::FiniteReal;
    use cadmpeg_core::decode::{DecodeArena, DecodeContext, DecodePolicy, ResourceDimension};
    use cadmpeg_core::CodecError;
    use std::cell::Cell;
    let boundaries = [
        FiniteReal::ZERO,
        FiniteReal::ONE,
        FiniteReal::from_index(2).unwrap(),
    ];
    let seed = FiniteReal::new(1.4).unwrap();
    for allowance in 0..=3 {
        let mut policy = DecodePolicy::service();
        policy.limits.max_work_units = allowance;
        policy.limits.max_materialized_bytes = 0;
        policy.limits.max_retained_bytes = 0;
        policy.limits.max_collection_items = 0;
        policy.limits.max_recursion_depth = 0;
        let arena = DecodeArena::new();
        let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).unwrap();
        let calls = Cell::new(0);
        let result = super::super::nearest_boundary_witness(&ctx, &boundaries, seed, 0.0, |_| {
            calls.set(calls.get() + 1);
            Ok(Some(0.0))
        });
        assert_eq!(calls.get(), allowance.min(2));
        if allowance < 3 {
            let original = result.unwrap_err();
            assert_eq!(original.dimension, ResourceDimension::WorkUnits);
            assert_eq!(
                original.operation,
                "IR curve inversion boundary witness scan"
            );
            assert!(
                matches!(ctx.finish_session(), Err(CodecError::ResourceLimit(limit)) if limit == original)
            );
        } else {
            assert_eq!(
                result.unwrap(),
                super::super::BoundaryWitness::Found(FiniteReal::ONE)
            );
            ctx.finish_session().unwrap();
        }
    }
}

#[test]
fn parameter_interval_scan_admits_only_visited_pairs() {
    use crate::scalar::FiniteReal;
    use cadmpeg_core::decode::{DecodeArena, DecodeContext, DecodePolicy, ResourceDimension};
    use cadmpeg_core::CodecError;
    let boundaries = [0, 1, 1, 2, 3].map(|value| FiniteReal::from_index(value).unwrap());
    for allowance in 0..=3 {
        let mut policy = DecodePolicy::service();
        policy.limits.max_work_units = allowance;
        policy.limits.max_materialized_bytes = 0;
        policy.limits.max_retained_bytes = 0;
        policy.limits.max_collection_items = 0;
        policy.limits.max_recursion_depth = 0;
        let arena = DecodeArena::new();
        let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).unwrap();
        let result = super::super::parameter_interval_containing(
            &ctx,
            &boundaries,
            FiniteReal::new(1.5).unwrap(),
        );
        if allowance < 3 {
            let original = result.unwrap_err();
            assert_eq!(original.dimension, ResourceDimension::WorkUnits);
            assert_eq!(
                original.operation,
                "IR curve inversion Newton interval scan"
            );
            assert!(
                matches!(ctx.finish_session(), Err(CodecError::ResourceLimit(limit)) if limit == original)
            );
        } else {
            assert_eq!(result.unwrap().unwrap().endpoints(), [1.0, 2.0]);
            ctx.finish_session().unwrap();
        }
    }
}

#[test]
fn boundary_scans_stop_at_invalid_witness_and_preserve_fused_empty_refusals() {
    use crate::scalar::FiniteReal;
    use cadmpeg_core::decode::{DecodeArena, DecodeContext, DecodePolicy};
    use cadmpeg_core::CodecError;
    use std::cell::Cell;
    let mut policy = DecodePolicy::service();
    policy.limits.max_work_units = 1;
    let arena = DecodeArena::new();
    let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).unwrap();
    let calls = Cell::new(0);
    assert_eq!(
        super::super::nearest_boundary_witness(
            &ctx,
            &[FiniteReal::ZERO, FiniteReal::ONE],
            FiniteReal::HALF,
            0.0,
            |_| {
                calls.set(calls.get() + 1);
                Ok(None)
            }
        )
        .unwrap(),
        super::super::BoundaryWitness::Invalid
    );
    assert_eq!(calls.get(), 1);
    let original = ctx
        .charge_work_limit(1, "original empty boundary refusal")
        .unwrap_err();
    assert_eq!(
        super::super::nearest_boundary_witness(
            &ctx,
            &[],
            FiniteReal::ZERO,
            0.0,
            |_| -> Result<Option<f64>, _> { panic!("empty boundary has no evaluation") }
        )
        .unwrap_err(),
        original
    );
    assert_eq!(
        super::super::parameter_interval_containing(&ctx, &[], FiniteReal::ZERO).unwrap_err(),
        original
    );
    assert!(
        matches!(ctx.finish_session(), Err(CodecError::ResourceLimit(limit)) if limit == original)
    );
}

#[test]
fn bounded_curve_search_preserves_caller_refusals_and_uses_scoped_storage() {
    use crate::geometry::nurbs::NurbsCurve;
    use crate::math::Point3;
    use crate::scalar::FiniteReal;
    use cadmpeg_core::decode::{DecodeArena, DecodeContext, DecodePolicy, ResourceDimension};
    use cadmpeg_core::CodecError;
    let curve = NurbsCurve::from_lanes(
        &cadmpeg_test_support::service_decode_context(),
        1,
        vec![0.0, 0.0, 1.0, 1.0],
        vec![Point3::new(0.0, 0.0, 0.0), Point3::new(1.0, 0.0, 0.0)],
        None,
        false,
    )
    .expect("fixture constructor admission")
    .unwrap();
    let boundaries = [FiniteReal::ZERO, FiniteReal::ONE];
    for dimension in [
        ResourceDimension::MaterializedBytes,
        ResourceDimension::CollectionItems,
        ResourceDimension::WorkUnits,
        ResourceDimension::RecursionDepth,
    ] {
        let arena = DecodeArena::new();
        let mut policy = DecodePolicy::service();
        match dimension {
            ResourceDimension::MaterializedBytes => policy.limits.max_materialized_bytes = 0,
            ResourceDimension::CollectionItems => policy.limits.max_collection_items = 0,
            ResourceDimension::WorkUnits => policy.limits.max_work_units = 0,
            ResourceDimension::RecursionDepth => policy.limits.max_recursion_depth = 0,
            _ => unreachable!(),
        }
        let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).unwrap();
        let error = super::super::nurbs_curve_parameter_near_point(
            &ctx,
            &curve,
            Point3::new(0.25, 0.0, 0.0),
            0.0,
            0.5,
        )
        .unwrap_err();
        assert!(matches!(&error, CodecError::ResourceLimit(limit) if limit.dimension == dimension));
        assert_eq!(
            ctx.finish_session().unwrap_err().to_string(),
            error.to_string()
        );
        if dimension != ResourceDimension::RecursionDepth {
            let arena = DecodeArena::new();
            let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).unwrap();
            let error =
                super::super::bounded_nearest_intervals(&ctx, &boundaries, FiniteReal::HALF)
                    .unwrap_err();
            assert!(
                matches!(&error, CodecError::ResourceLimit(limit) if limit.dimension == dimension)
            );
            assert_eq!(
                ctx.finish_session().unwrap_err().to_string(),
                error.to_string()
            );
        }
    }
    let arena = DecodeArena::new();
    let mut policy = DecodePolicy::service();
    policy.limits.max_retained_bytes = 0;
    let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).unwrap();
    assert_eq!(
        super::super::nurbs_curve_parameter_near_point(
            &ctx,
            &curve,
            Point3::new(0.25, 0.0, 0.0),
            0.0,
            0.5
        )
        .unwrap(),
        Some(FiniteReal::new(0.25).unwrap())
    );
    let intervals =
        super::super::bounded_nearest_intervals(&ctx, &boundaries, FiniteReal::HALF).unwrap();
    assert_eq!(intervals.0, vec![boundaries]);
    drop(intervals);
    ctx.finish_session().unwrap();
}

#[test]
fn tail_interval_search_preserves_refusals_and_releases_its_storage() {
    use cadmpeg_core::decode::{DecodeArena, DecodeContext, DecodePolicy, ResourceDimension};
    use cadmpeg_core::CodecError;
    let boundaries = [0., 1., 1., 2., 3.];
    for cap in 0..8 {
        let arena = DecodeArena::new();
        let mut policy = DecodePolicy::service();
        policy.limits.max_work_units = cap;
        let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).expect("root");
        let limit = super::super::bounded_tail_intervals(&ctx, &boundaries)
            .err()
            .expect("every scan, copy and swap requires work");
        assert_eq!(limit.dimension, ResourceDimension::WorkUnits);
        assert!(
            matches!(ctx.finish_session(), Err(CodecError::ResourceLimit(sticky)) if sticky == limit)
        );
    }
    for dimension in [
        ResourceDimension::MaterializedBytes,
        ResourceDimension::CollectionItems,
    ] {
        let arena = DecodeArena::new();
        let mut policy = DecodePolicy::service();
        if dimension == ResourceDimension::MaterializedBytes {
            policy.limits.max_materialized_bytes = 0;
        } else {
            policy.limits.max_collection_items = 0;
        }
        let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).expect("root");
        let limit = super::super::bounded_tail_intervals(&ctx, &boundaries)
            .err()
            .expect("scratch requires admission");
        assert_eq!(limit.dimension, dimension);
        assert!(
            matches!(ctx.finish_session(), Err(CodecError::ResourceLimit(sticky)) if sticky == limit)
        );
    }
    let arena = DecodeArena::new();
    let mut policy = DecodePolicy::service();
    policy.limits.max_work_units = 8;
    policy.limits.max_materialized_bytes = 4096;
    policy.limits.max_retained_bytes = 0;
    policy.limits.max_collection_items = 5;
    let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).expect("root");
    let search = super::super::bounded_tail_intervals(&ctx, &boundaries).expect("exact visits");
    assert_eq!(search.intervals, vec![[0., 1.], [1., 2.], [2., 3.]]);
    assert!(!search.truncated);
    drop(search);
    let reuse = ctx
        .reserve_scoped(4096, "tail interval scratch reuse")
        .expect("scratch released");
    drop(reuse);
    ctx.finish_session().expect("exact visits and scoped bytes");
}

#[test]
fn pcurve_containment_preserves_caller_refusals_and_releases_scratch() {
    use crate::math::Point2;
    use cadmpeg_core::decode::{DecodeArena, DecodeContext, DecodePolicy, ResourceDimension};
    use cadmpeg_core::CodecError;
    let knots = [0., 0., 1., 1.];
    let controls = [Point2::new(0., 0.), Point2::new(1., 0.)];
    for dimension in [
        ResourceDimension::WorkUnits,
        ResourceDimension::MaterializedBytes,
        ResourceDimension::CollectionItems,
    ] {
        let arena = DecodeArena::new();
        let mut policy = DecodePolicy::service();
        match dimension {
            ResourceDimension::WorkUnits => policy.limits.max_work_units = 0,
            ResourceDimension::MaterializedBytes => policy.limits.max_materialized_bytes = 0,
            ResourceDimension::CollectionItems => policy.limits.max_collection_items = 0,
            _ => panic!("test dimension"),
        }
        let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).expect("root");
        let limit = super::super::nurbs_pcurve_contains_point(
            &ctx,
            1,
            &knots,
            &controls,
            None,
            Point2::new(0.5, 0.),
            0.,
        )
        .expect_err("original caller refusal");
        assert_eq!(limit.dimension, dimension);
        assert!(
            matches!(ctx.finish_session(), Err(CodecError::ResourceLimit(sticky)) if sticky == limit)
        );
    }
    let arena = DecodeArena::new();
    let mut policy = DecodePolicy::service();
    policy.limits.max_retained_bytes = 0;
    policy.limits.max_materialized_bytes = 4096;
    let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).expect("root");
    assert_eq!(
        super::super::nurbs_pcurve_contains_point(
            &ctx,
            1,
            &knots,
            &controls,
            None,
            Point2::new(0.5, 0.),
            0.
        )
        .expect("within limits"),
        Some(true)
    );
    let reuse = ctx
        .reserve_scoped(4096, "pcurve containment scratch reuse")
        .expect("all scratch released");
    drop(reuse);
    ctx.finish_session().expect("no retained temporary storage");
}

#[test]
fn nearest_interval_heap_preserves_descending_order_and_releases_working_storage() {
    use crate::scalar::FiniteReal;
    use cadmpeg_core::decode::{DecodeArena, DecodeContext, DecodePolicy};
    let boundaries =
        [0, 1, 1, 2, 3, 4].map(|value| FiniteReal::from_index(value).expect("exact test index"));
    let mut policy = DecodePolicy::service();
    policy.limits.max_retained_bytes = 0;
    policy.limits.max_materialized_bytes = 4096;
    // Four valid heap slots and four output slots. Repeated intervals add none.
    policy.limits.max_collection_items = 8;
    let arena = DecodeArena::new();
    let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy).expect("root");
    let output = super::super::bounded_nearest_intervals(
        &ctx,
        &boundaries,
        FiniteReal::from_index(1).unwrap(),
    )
    .expect("eight slots");
    assert_eq!(
        output
            .0
            .iter()
            .map(|interval| interval.map(FiniteReal::get))
            .collect::<Vec<_>>(),
        [[3.0, 4.0], [2.0, 3.0], [1.0, 2.0], [0.0, 1.0]]
    );
    let bytes = u64::try_from(output.0.capacity() * std::mem::size_of::<[FiniteReal; 2]>())
        .expect("test bytes fit");
    let spare = ctx
        .reserve_scoped_limit(4096 - bytes, "test nearest interval heap released")
        .expect("only interval output remains");
    drop(spare);
    drop(output);
    let reuse = ctx
        .reserve_scoped_limit(4096, "test nearest intervals released")
        .expect("all bytes reusable");
    drop(reuse);
    ctx.finish_session()
        .expect("scoped intervals retain no bytes");
}