rsspice 0.1.0

Pure Rust port of the SPICE Toolkit for space geometry
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
//
// GENERATED FILE
//

use super::*;
use f2rust_std::*;

const AXTOL: f64 = 0.00000000000001;
const MSGLEN: i32 = 240;
const NEASY: i32 = 3;
const NSIMPL: i32 = 8;
const NRANDM: i32 = 1000;
const NUMSCL: i32 = 5;
const UBEL: i32 = 9;
const UBPL: i32 = 4;

struct SaveVars {
    EASYA: StackArray<f64, 3>,
    EASYB: StackArray<f64, 3>,
    EASYC: StackArray<f64, 3>,
    EASYPT: StackArray2D<f64, 9>,
    SMPC: StackArray<f64, 8>,
    SMPN: StackArray2D<f64, 24>,
    SMPRAD: StackArray2D<f64, 24>,
    XFOUND: StackArray<bool, 8>,
}

impl SaveInit for SaveVars {
    fn new() -> Self {
        let mut EASYA = StackArray::<f64, 3>::new(1..=NEASY);
        let mut EASYB = StackArray::<f64, 3>::new(1..=NEASY);
        let mut EASYC = StackArray::<f64, 3>::new(1..=NEASY);
        let mut EASYPT = StackArray2D::<f64, 9>::new(1..=3, 1..=NEASY);
        let mut SMPC = StackArray::<f64, 8>::new(1..=NSIMPL);
        let mut SMPN = StackArray2D::<f64, 24>::new(1..=3, 1..=NSIMPL);
        let mut SMPRAD = StackArray2D::<f64, 24>::new(1..=3, 1..=NSIMPL);
        let mut XFOUND = StackArray::<bool, 8>::new(1..=NSIMPL);

        {
            use f2rust_std::data::Val;

            let mut clist = [Val::D(100.0), Val::D(200.0), Val::D(4.0)].into_iter();
            EASYA
                .iter_mut()
                .for_each(|n| *n = clist.next().unwrap().into_f64());

            debug_assert!(clist.next().is_none(), "DATA not fully initialised");
        }
        {
            use f2rust_std::data::Val;

            let mut clist = [Val::D(10.0), Val::D(2.0), Val::D(40.0)].into_iter();
            EASYB
                .iter_mut()
                .for_each(|n| *n = clist.next().unwrap().into_f64());

            debug_assert!(clist.next().is_none(), "DATA not fully initialised");
        }
        {
            use f2rust_std::data::Val;

            let mut clist = [Val::D(1.0), Val::D(20.0), Val::D(400.0)].into_iter();
            EASYC
                .iter_mut()
                .for_each(|n| *n = clist.next().unwrap().into_f64());

            debug_assert!(clist.next().is_none(), "DATA not fully initialised");
        }
        {
            use f2rust_std::data::Val;

            let mut clist = [
                Val::D(0.0),
                Val::D(0.0),
                Val::D(2.0),
                Val::D(0.0),
                Val::D(4.0),
                Val::D(0.0),
                Val::D(-8.0),
                Val::D(0.0),
                Val::D(0.0),
            ]
            .into_iter();
            EASYPT
                .iter_mut()
                .for_each(|n| *n = clist.next().unwrap().into_f64());

            debug_assert!(clist.next().is_none(), "DATA not fully initialised");
        }
        {
            use f2rust_std::data::Val;

            let mut clist = [
                Val::D(1.0),
                Val::D(1.0),
                Val::D(1.0),
                Val::D(200000000000000000000.0),
                Val::D(200000000000000000000.0),
                Val::D(200000000000000000000.0),
                Val::D(0.00000000000000000004),
                Val::D(0.00000000000000000004),
                Val::D(0.00000000000000000004),
                Val::D(0.5),
                Val::D(1.0),
                Val::D(4.0),
                Val::D(1000000.0),
                Val::D(2000000.0),
                Val::D(4000000.0),
                Val::D(1000000.0),
                Val::D(1000000.0),
                Val::D(1000000.0),
                Val::D(1.0),
                Val::D(1.0),
                Val::D(1.0),
                Val::D(1.0),
                Val::D(1.0),
                Val::D(1.0),
            ]
            .into_iter();
            SMPRAD
                .iter_mut()
                .for_each(|n| *n = clist.next().unwrap().into_f64());

            debug_assert!(clist.next().is_none(), "DATA not fully initialised");
        }
        {
            use f2rust_std::data::Val;

            let mut clist = [
                Val::D(0.0),
                Val::D(0.0),
                Val::D(1.0),
                Val::D(0.0),
                Val::D(0.0),
                Val::D(1.0),
                Val::D(0.0),
                Val::D(100000000000000000000.0),
                Val::D(-1.0),
                Val::D(1.0),
                Val::D(0.0),
                Val::D(0.00000000000000000002),
                Val::D(0.0),
                Val::D(0.0),
                Val::D(1.0),
                Val::D(2.0),
                Val::D(0.0),
                Val::D(0.0),
                Val::D(1.0),
                Val::D(2000000.0),
                Val::D(1.0),
                Val::D(1.0),
                Val::D(1.0),
                Val::D(500000.0),
                Val::D(0.0),
                Val::D(0.0),
                Val::D(1.0),
                Val::D(1.0),
                Val::D(0.0),
                Val::D(0.0),
                Val::D(1.0),
                Val::D(1.0000000001),
            ]
            .into_iter();
            for I in intrinsics::range(1, NSIMPL, 1) {
                for J in intrinsics::range(1, 3, 1) {
                    SMPN[[J, I]] = clist.next().unwrap().into_f64();
                }
                SMPC[I] = clist.next().unwrap().into_f64();
            }

            debug_assert!(clist.next().is_none(), "DATA not fully initialised");
        }
        {
            use f2rust_std::data::Val;

            let mut clist = []
                .into_iter()
                .chain(std::iter::repeat_n(Val::L(true), 7 as usize))
                .chain([Val::L(false)]);

            XFOUND
                .iter_mut()
                .for_each(|n| *n = clist.next().unwrap().into_bool());

            debug_assert!(clist.next().is_none(), "DATA not fully initialised");
        }

        Self {
            EASYA,
            EASYB,
            EASYC,
            EASYPT,
            SMPC,
            SMPN,
            SMPRAD,
            XFOUND,
        }
    }
}

//$Procedure F_SLICE ( Ellipsoid/plane intersection routine tests )
pub fn F_SLICE(OK: &mut bool, ctx: &mut Context) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut TITLE = [b' '; MSGLEN as usize];
    let mut A: f64 = 0.0;
    let mut ALT: f64 = 0.0;
    let mut B: f64 = 0.0;
    let mut C: f64 = 0.0;
    let mut CENTER = StackArray::<f64, 3>::new(1..=3);
    let mut CONST: f64 = 0.0;
    let mut ELLIPS = StackArray::<f64, 9>::new(1..=UBEL);
    let mut LIMB = StackArray::<f64, 9>::new(1..=UBEL);
    let mut NEAR = StackArray::<f64, 3>::new(1..=3);
    let mut NORMAL = StackArray::<f64, 3>::new(1..=3);
    let mut PFACTR: f64 = 0.0;
    let mut PLANE = StackArray::<f64, 4>::new(1..=UBPL);
    let mut PT = StackArray::<f64, 3>::new(1..=3);
    let mut SFACTR: f64 = 0.0;
    let mut SMAJOR = StackArray::<f64, 3>::new(1..=3);
    let mut SMINOR = StackArray::<f64, 3>::new(1..=3);
    let mut XL = StackArray2D::<f64, 16>::new(1..=2, 1..=NSIMPL);
    let mut SEED: i32 = 0;
    let mut FOUND: bool = false;
    let mut ISELVL: bool = false;
    let mut VALELL: bool = false;

    //
    // SPICELIB functions
    //

    //
    // Other functions
    //

    //
    // Local Parameters
    //

    //
    // Local Variables
    //

    //
    // Saved values
    //

    //
    // Initial values
    //

    //
    // In this test family, we encapsulate some of the geometric
    // tests within functions defined in this file. The function
    //
    //    T_ISPXED
    //
    // test whether an ellipse is the intersection of a specified
    // plane and ellipsoid.  It does this by testing the four
    // endpoints of the ellipse's axes to see whether they are
    //
    //    - On the ellipsoid:  a point P = (x,y,z) is "on" the
    //      ellipsoid with semi-axis lengths a, b, c if
    //      the "level surface parameter"
    //
    //         lambda = x**2/a**2 + y**2/b**2 + z**2/c**2
    //
    //      is sufficiently close to 1.
    //
    //
    //    - On the plane:  a point P = (x,y,z) is "on" the
    //      plane with normal vector N and constant C if
    //      the dot product
    //
    //         < N, P >
    //
    //      is sufficiently close to C.
    //
    //
    // The function
    //
    //    T_ISLIMB
    //
    // test whether an ellipse is the limb defined by a specified
    // viewing point and ellipsoid.  It does this by testing the
    // four endpoints of the ellipse's axes to see whether, for each
    // endpoint, the ray from the viewing point to the endpoint is
    // orthogonal to the ellipsoid's outward normal direction at the
    // endpoint.
    //

    //
    // Open the test family.
    //
    testutil::TOPEN(b"F_SLICE", ctx)?;

    //
    // Perform a number of simple INEDPL tests for which we can compute
    // the intersection ellipse in advance.
    //
    //
    // Set up expected semi-axis lengths for the "simple" tests.
    //
    XL[[1, 1]] = 1.0;
    XL[[2, 1]] = 1.0;

    XL[[1, 2]] = (f64::powf(3.0, 0.5) * 100000000000000000000.0);
    XL[[2, 2]] = (f64::powf(3.0, 0.5) * 100000000000000000000.0);

    XL[[1, 3]] = ((2.0 * f64::powf(3.0, 0.5)) * 0.00000000000000000001);
    XL[[2, 3]] = ((2.0 * f64::powf(3.0, 0.5)) * 0.00000000000000000001);

    XL[[1, 4]] = (0.5 * f64::powf(3.0, 0.5));
    XL[[2, 4]] = f64::powf(3.0, 0.5);

    XL[[1, 5]] = (f64::powf(3.0, 0.5) * 1000000.0);
    XL[[2, 5]] = ((0.5 * f64::powf(3.0, 0.5)) * 1000000.0);

    XL[[1, 6]] = ((0.5 * f64::powf(3.0, 0.5)) * 1000000.0);
    XL[[2, 6]] = ((0.5 * f64::powf(3.0, 0.5)) * 1000000.0);

    XL[[1, 7]] = 0.0;
    XL[[2, 7]] = 0.0;

    for I in 1..=NSIMPL {
        //
        // --- Case: ------------------------------------------------------
        //
        fstr::assign(
            &mut TITLE,
            b"An INEDPL case that can be checked by inspection; case #.",
        );
        spicelib::REPMI(&TITLE.clone(), b"#", I, &mut TITLE, ctx);
        testutil::TCASE(&TITLE, ctx)?;

        spicelib::VHAT(save.SMPN.subarray([1, I]), NORMAL.as_slice_mut());

        CONST = save.SMPC[I];

        A = save.SMPRAD[[1, I]];
        B = save.SMPRAD[[2, I]];
        C = save.SMPRAD[[3, I]];

        spicelib::NVC2PL(NORMAL.as_slice(), CONST, PLANE.as_slice_mut(), ctx)?;
        testutil::CHCKXC(false, b" ", OK, ctx)?;

        spicelib::INEDPL(
            A,
            B,
            C,
            PLANE.as_slice(),
            ELLIPS.as_slice_mut(),
            &mut FOUND,
            ctx,
        )?;
        testutil::CHCKXC(false, b" ", OK, ctx)?;

        if (I != 7) {
            //
            // Check the FOUND flag on all cases but case 7.  Case
            // 7 is a boundary case that may fail on some platforms.
            //
            testutil::CHCKSL(b"FOUND", FOUND, save.XFOUND[I], OK, ctx)?;
        }

        if FOUND {
            //
            // Check the ellipse to make sure it is in the plane
            // and on the surface of the ellipsoid.
            //
            VALELL = T_ISPXED(ELLIPS.as_slice(), A, B, C, PLANE.as_slice(), OK, ctx)?;

            spicelib::EL2CGV(
                ELLIPS.as_slice(),
                CENTER.as_slice_mut(),
                SMAJOR.as_slice_mut(),
                SMINOR.as_slice_mut(),
            );
            testutil::CHCKXC(false, b" ", OK, ctx)?;

            //
            // Check the lengths of the semi-axes.
            //
            testutil::CHCKSD(
                b"||SMAJOR||",
                spicelib::VNORM(SMAJOR.as_slice()),
                b"~/",
                XL[[1, I]],
                AXTOL,
                OK,
                ctx,
            )?;
        }
    }

    //
    // Now for some more difficult cases.  We'll generate the ellipsoids
    // and planes using random numbers.  There are four components to
    // generate:
    //
    //    - random plane normal vectors
    //    - random ellipsoid axes
    //    - random plane constants
    //    - random scale factors for the ellipsoid and plane; these are
    //      used to create a wide range of scales
    //

    SEED = -1;

    for I in 1..=NRANDM {
        //
        // --- Case: ------------------------------------------------------
        //

        //
        // Get a scale factor.
        //
        SFACTR = f64::powf(10.0, testutil::T_RANDD(-290.0, 290.0, &mut SEED, ctx)?);

        //
        // Make up ellipsoid axes and plane constant.
        //
        A = (SFACTR * testutil::T_RANDD(1.0, 100.0, &mut SEED, ctx)?);
        B = (SFACTR * testutil::T_RANDD(1.0, 100.0, &mut SEED, ctx)?);
        C = (SFACTR * testutil::T_RANDD(1.0, 100.0, &mut SEED, ctx)?);

        CONST = (SFACTR * testutil::T_RANDD(1.0, 100.0, &mut SEED, ctx)?);

        //
        // We gotta have a plane normal vector.
        //
        NORMAL[1] = testutil::T_RANDD(-1.0, 1.0, &mut SEED, ctx)?;
        NORMAL[2] = testutil::T_RANDD(-1.0, 1.0, &mut SEED, ctx)?;
        NORMAL[3] = testutil::T_RANDD(-1.0, 1.0, &mut SEED, ctx)?;

        spicelib::VHATIP(NORMAL.as_slice_mut());

        fstr::assign(
            &mut TITLE,
            b"INEDPL Random case #.  A, B, C = # # #; CONST = #; NORMAL = (#, #, #)",
        );
        spicelib::REPMI(&TITLE.clone(), b"#", I, &mut TITLE, ctx);
        spicelib::REPMD(&TITLE.clone(), b"#", A, 14, &mut TITLE, ctx);
        spicelib::REPMD(&TITLE.clone(), b"#", B, 14, &mut TITLE, ctx);
        spicelib::REPMD(&TITLE.clone(), b"#", C, 14, &mut TITLE, ctx);
        spicelib::REPMD(&TITLE.clone(), b"#", CONST, 14, &mut TITLE, ctx);
        spicelib::REPMD(&TITLE.clone(), b"#", NORMAL[1], 14, &mut TITLE, ctx);
        spicelib::REPMD(&TITLE.clone(), b"#", NORMAL[2], 14, &mut TITLE, ctx);
        spicelib::REPMD(&TITLE.clone(), b"#", NORMAL[3], 14, &mut TITLE, ctx);

        testutil::TCASE(&TITLE, ctx)?;

        //
        // Cross our fingers and toes and let 'er rip.
        //
        spicelib::NVC2PL(NORMAL.as_slice(), CONST, PLANE.as_slice_mut(), ctx)?;
        testutil::CHCKXC(false, b" ", OK, ctx)?;

        spicelib::INEDPL(
            A,
            B,
            C,
            PLANE.as_slice(),
            ELLIPS.as_slice_mut(),
            &mut FOUND,
            ctx,
        )?;
        testutil::CHCKXC(false, b" ", OK, ctx)?;

        spicelib::EL2CGV(
            ELLIPS.as_slice(),
            CENTER.as_slice_mut(),
            SMAJOR.as_slice_mut(),
            SMINOR.as_slice_mut(),
        );
        testutil::CHCKXC(false, b" ", OK, ctx)?;

        if FOUND {
            //
            // Make sure the intersection ellipse we found is
            // contained in both the plane and the surface of the
            // ellipsoid.
            //
            VALELL = T_ISPXED(ELLIPS.as_slice(), A, B, C, PLANE.as_slice(), OK, ctx)?;

            testutil::CHCKSL(b"Is ellipse valid?", VALELL, true, OK, ctx)?;
        }
    }

    //
    //     INELPL error cases:
    //
    //
    // --- Case: ------------------------------------------------------
    //
    testutil::TCASE(b"INEDPL: invalid plane", ctx)?;

    spicelib::CLEARD(4, PLANE.as_slice_mut());

    spicelib::INEDPL(
        1.0,
        1.0,
        1.0,
        PLANE.as_slice(),
        ELLIPS.as_slice_mut(),
        &mut FOUND,
        ctx,
    )?;
    testutil::CHCKXC(true, b"SPICE(INVALIDPLANE)", OK, ctx)?;

    //
    // --- Case: ------------------------------------------------------
    //
    testutil::TCASE(b"INEDPL: ellipsoid has one zero-length axis.", ctx)?;

    spicelib::FILLD(1.0, 3, NORMAL.as_slice_mut());

    spicelib::NVC2PL(NORMAL.as_slice(), 0.0, PLANE.as_slice_mut(), ctx)?;
    testutil::CHCKXC(false, b" ", OK, ctx)?;

    spicelib::INEDPL(
        0.0,
        1.0,
        1.0,
        PLANE.as_slice(),
        ELLIPS.as_slice_mut(),
        &mut FOUND,
        ctx,
    )?;
    testutil::CHCKXC(true, b"SPICE(DEGENERATECASE)", OK, ctx)?;

    spicelib::INEDPL(
        1.0,
        0.0,
        1.0,
        PLANE.as_slice(),
        ELLIPS.as_slice_mut(),
        &mut FOUND,
        ctx,
    )?;
    testutil::CHCKXC(true, b"SPICE(DEGENERATECASE)", OK, ctx)?;

    spicelib::INEDPL(
        1.0,
        1.0,
        0.0,
        PLANE.as_slice(),
        ELLIPS.as_slice_mut(),
        &mut FOUND,
        ctx,
    )?;
    testutil::CHCKXC(true, b"SPICE(DEGENERATECASE)", OK, ctx)?;

    //
    // --- Case: ------------------------------------------------------
    //
    testutil::TCASE(b"INEDPL: ellipsoid has one negative-length axis.", ctx)?;

    spicelib::INEDPL(
        -1.0,
        1.0,
        1.0,
        PLANE.as_slice(),
        ELLIPS.as_slice_mut(),
        &mut FOUND,
        ctx,
    )?;
    testutil::CHCKXC(true, b"SPICE(DEGENERATECASE)", OK, ctx)?;

    spicelib::INEDPL(
        1.0,
        -1.0,
        1.0,
        PLANE.as_slice(),
        ELLIPS.as_slice_mut(),
        &mut FOUND,
        ctx,
    )?;
    testutil::CHCKXC(true, b"SPICE(DEGENERATECASE)", OK, ctx)?;

    spicelib::INEDPL(
        1.0,
        1.0,
        -1.0,
        PLANE.as_slice(),
        ELLIPS.as_slice_mut(),
        &mut FOUND,
        ctx,
    )?;
    testutil::CHCKXC(true, b"SPICE(DEGENERATECASE)", OK, ctx)?;

    //
    // Now for the EDLIMB tests.
    //
    //
    // Start out with easy cases for which the answers can be found
    // by inspection.
    //
    for I in 1..=NEASY {
        //
        // --- Case: ------------------------------------------------------
        //
        fstr::assign(
            &mut TITLE,
            b"An EDLIMB case that can be checked by inspection; case #.",
        );
        spicelib::REPMI(&TITLE.clone(), b"#", I, &mut TITLE, ctx);
        testutil::TCASE(&TITLE, ctx)?;

        A = save.EASYA[I];
        B = save.EASYB[I];
        C = save.EASYC[I];

        spicelib::VEQU(save.EASYPT.subarray([1, I]), PT.as_slice_mut());

        spicelib::EDLIMB(A, B, C, PT.as_slice(), LIMB.as_slice_mut(), ctx)?;
        testutil::CHCKXC(false, b" ", OK, ctx)?;

        ISELVL = T_ISLIMB(LIMB.as_slice(), A, B, C, PT.as_slice(), OK, ctx)?;

        if *OK {
            VALELL = (ISELVL && *OK);

            testutil::CHCKSL(b"Is ellipse valid?", VALELL, true, OK, ctx)?;
        }
    }

    //
    // Random cases for EDLIMB follow:
    //
    for I in 1..=NRANDM {
        //
        // --- Case: ------------------------------------------------------
        //
        fstr::assign(&mut TITLE, b"EDLIMB Random case #");
        spicelib::REPMI(&TITLE.clone(), b"#", I, &mut TITLE, ctx);
        testutil::TCASE(&TITLE, ctx)?;

        //
        // Get a scale factor.
        //
        SFACTR = f64::powf(10.0, testutil::T_RANDD(-20.0, 20.0, &mut SEED, ctx)?);

        //
        // Make up ellipsoid axes and viewing point.
        //
        A = (SFACTR * testutil::T_RANDD(1.0, 100.0, &mut SEED, ctx)?);
        B = (SFACTR * testutil::T_RANDD(1.0, 100.0, &mut SEED, ctx)?);
        C = (SFACTR * testutil::T_RANDD(1.0, 100.0, &mut SEED, ctx)?);

        //
        // The viewing point must be outside the ellipsoid.
        //
        PT[1] = (SFACTR * testutil::T_RANDD(-1.0, 1.0, &mut SEED, ctx)?);
        PT[2] = (SFACTR * testutil::T_RANDD(-1.0, 1.0, &mut SEED, ctx)?);
        PT[3] = (SFACTR * testutil::T_RANDD(-1.0, 1.0, &mut SEED, ctx)?);

        //
        // In half of the cases, we'll scale PT by widely varying
        // scale factors.  In the other half, we'll keep PT close to
        // the ellipsoid, since these cases are more demanding.
        //
        if (I <= (NRANDM / 2)) {
            PFACTR = f64::powf(10.0, testutil::T_RANDD(0.1, 10.0, &mut SEED, ctx)?);
        } else {
            PFACTR = 1.0;
        }

        spicelib::VSCLIP(PFACTR, PT.as_slice_mut());

        spicelib::NEARPT(PT.as_slice(), A, B, C, NEAR.as_slice_mut(), &mut ALT, ctx)?;
        testutil::CHCKXC(false, b" ", OK, ctx)?;

        //
        // If ALT is negative, just reflect PT about the tangent
        // plane at NEAR.
        //
        if (ALT < 0 as f64) {
            spicelib::SURFNM(A, B, C, NEAR.as_slice(), NORMAL.as_slice_mut(), ctx)?;
            testutil::CHCKXC(false, b" ", OK, ctx)?;

            spicelib::VHATIP(NORMAL.as_slice_mut());
            testutil::CHCKXC(false, b" ", OK, ctx)?;

            spicelib::VLCOM(
                1.0,
                NEAR.as_slice(),
                -ALT,
                NORMAL.as_slice(),
                PT.as_slice_mut(),
            );
        }

        //
        // Cross our fingers and toes and let 'er rip.
        //
        spicelib::EDLIMB(A, B, C, PT.as_slice(), LIMB.as_slice_mut(), ctx)?;
        testutil::CHCKXC(false, b" ", OK, ctx)?;

        ISELVL = T_ISLIMB(LIMB.as_slice(), A, B, C, PT.as_slice(), OK, ctx)?;

        if *OK {
            VALELL = (ISELVL && *OK);

            testutil::CHCKSL(b"Is ellipse valid?", VALELL, true, OK, ctx)?;
        }
    }

    //
    //     EDLIMB error cases:
    //
    //
    // --- Case: ------------------------------------------------------
    //
    testutil::TCASE(b"EDLIMB: view point inside ellipsoid.", ctx)?;

    spicelib::CLEARD(3, PT.as_slice_mut());

    spicelib::EDLIMB(1.0, 1.0, 1.0, PT.as_slice(), LIMB.as_slice_mut(), ctx)?;
    testutil::CHCKXC(true, b"SPICE(DEGENERATECASE)", OK, ctx)?;

    //
    // --- Case: ------------------------------------------------------
    //
    testutil::TCASE(b"EDLIMB: ellipsoid has one zero-length axis.", ctx)?;

    spicelib::FILLD(10.0, 3, PT.as_slice_mut());

    spicelib::EDLIMB(0.0, 1.0, 1.0, PT.as_slice(), LIMB.as_slice_mut(), ctx)?;
    testutil::CHCKXC(true, b"SPICE(INVALIDAXISLENGTH)", OK, ctx)?;

    spicelib::EDLIMB(1.0, 0.0, 1.0, PT.as_slice(), LIMB.as_slice_mut(), ctx)?;
    testutil::CHCKXC(true, b"SPICE(INVALIDAXISLENGTH)", OK, ctx)?;

    spicelib::EDLIMB(1.0, 1.0, 0.0, PT.as_slice(), LIMB.as_slice_mut(), ctx)?;
    testutil::CHCKXC(true, b"SPICE(INVALIDAXISLENGTH)", OK, ctx)?;

    //
    // --- Case: ------------------------------------------------------
    //
    testutil::TCASE(b"EDLIMB: ellipsoid has one negative-length axis.", ctx)?;

    spicelib::EDLIMB(-1.0, 1.0, 1.0, PT.as_slice(), LIMB.as_slice_mut(), ctx)?;
    testutil::CHCKXC(true, b"SPICE(INVALIDAXISLENGTH)", OK, ctx)?;

    spicelib::EDLIMB(1.0, -1.0, 1.0, PT.as_slice(), LIMB.as_slice_mut(), ctx)?;
    testutil::CHCKXC(true, b"SPICE(INVALIDAXISLENGTH)", OK, ctx)?;

    spicelib::EDLIMB(1.0, 1.0, -1.0, PT.as_slice(), LIMB.as_slice_mut(), ctx)?;
    testutil::CHCKXC(true, b"SPICE(INVALIDAXISLENGTH)", OK, ctx)?;

    //
    // --- Case: ------------------------------------------------------
    //

    //
    //     This error may not be detected on some systems.  We exclude
    //     it for now.
    //
    //CC   CALL TCASE  ( 'EDLIMB: squared, scaled axis length underflows.' )

    //CC      PT(1) = 1.D256
    //CC      PT(2) = 1.D0
    //CC      PT(3) = 1.D0

    //CC      CALL EDLIMB ( 1.D0, 1.D0, 1.D255, PT, LIMB )
    //CC      CALL CHCKXC ( .TRUE., 'SPICE(DEGENERATECASE)', OK )

    //
    // Close out the test family.
    //
    testutil::T_SUCCESS(OK, ctx);
    Ok(())
}