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
//
// GENERATED FILE
//

use super::*;
use crate::SpiceContext;
use f2rust_std::*;

pub const LBCELL: i32 = -5;

/// Insert an interval into a DP window
///
/// Insert an interval into a double precision window.
///
/// # Required Reading
///
/// * [WINDOWS](crate::required_reading::windows)
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  LEFT,
///  RIGHT      I   Left, right endpoints of new interval.
///  WINDOW    I-O  Input, output window.
/// ```
///
/// # Detailed Input
///
/// ```text
///  LEFT,
///  RIGHT    are the left and right endpoints of the interval to be
///           inserted.
///
///  WINDOW   on input, is a SPICE window containing zero or more
///           intervals.
/// ```
///
/// # Detailed Output
///
/// ```text
///  WINDOW   on output, is the original window following the insertion
///           of the interval from LEFT to RIGHT.
/// ```
///
/// # Exceptions
///
/// ```text
///  1)  If LEFT is greater than RIGHT, the error SPICE(BADENDPOINTS)
///      is signaled.
///
///  2)  If the insertion of the interval causes an excess of elements,
///      the error SPICE(WINDOWEXCESS) is signaled.
///
///  3)  The cardinality of the input WINDOW must be even. Left
///      endpoints of stored intervals must be strictly greater than
///      preceding right endpoints. Right endpoints must be greater
///      than or equal to corresponding left endpoints. Invalid window
///      data are not diagnosed by this routine and may lead to
///      unpredictable results.
/// ```
///
/// # Particulars
///
/// ```text
///  This routine inserts the interval from LEFT to RIGHT into the
///  input window. If the new interval overlaps any of the intervals
///  in the window, the intervals are merged. Thus, the cardinality
///  of the input window can actually decrease as the result of an
///  insertion. However, because inserting an interval that is
///  disjoint from the other intervals in the window can increase the
///  cardinality of the window, the routine signals an error.
/// ```
///
/// # Examples
///
/// ```text
///  The numerical results shown for this example may differ across
///  platforms. The results depend on the SPICE kernels used as input,
///  the compiler and supporting libraries, and the machine specific
///  arithmetic implementation.
///
///  1) The following code example demonstrates how to insert an
///     interval into an existing double precision SPICE window, and
///     how to loop over all its intervals to extract their left and
///     right points.
///
///
///     Example code begins here.
///
///
///           PROGRAM WNINSD_EX1
///           IMPLICIT NONE
///
///     C
///     C     SPICELIB functions.
///     C
///           INTEGER               WNCARD
///
///     C
///     C     Local parameters.
///     C
///           INTEGER               LBCELL
///           PARAMETER           ( LBCELL = -5 )
///
///           INTEGER               WNSIZE
///           PARAMETER           ( WNSIZE = 10 )
///
///     C
///     C     Local variables.
///     C
///           DOUBLE PRECISION      WINDOW      ( LBCELL:WNSIZE )
///           DOUBLE PRECISION      LEFT
///           DOUBLE PRECISION      RIGHT
///
///           INTEGER               I
///
///     C
///     C     Validate the window with size WNSIZE and zero elements.
///     C
///           CALL WNVALD( WNSIZE, 0, WINDOW )
///
///     C
///     C     Insert the intervals
///     C
///     C        [ 1, 3 ]  [ 7, 11 ]  [ 23, 27 ]
///     C
///     C     into WINDOW.
///     C
///           CALL WNINSD(  1.D0,  3.D0, WINDOW )
///           CALL WNINSD(  7.D0, 11.D0, WINDOW )
///           CALL WNINSD( 23.D0, 27.D0, WINDOW )
///
///     C
///     C     Loop over the number of intervals in WINDOW, output
///     C     the LEFT and RIGHT endpoints for each interval.
///     C
///           DO I=1, WNCARD(WINDOW)
///
///              CALL WNFETD( WINDOW, I, LEFT, RIGHT )
///
///              WRITE(*,'(A,I2,2(A,F8.3),A)') 'Interval', I,
///          .                  ' [', LEFT,',',  RIGHT, ']'
///
///           END DO
///
///           END
///
///
///     When this program was executed on a Mac/Intel/gfortran/64-bit
///     platform, the output was:
///
///
///     Interval 1 [   1.000,   3.000]
///     Interval 2 [   7.000,  11.000]
///     Interval 3 [  23.000,  27.000]
/// ```
///
/// # Author and Institution
///
/// ```text
///  N.J. Bachman       (JPL)
///  J. Diaz del Rio    (ODC Space)
///  K.R. Gehringer     (JPL)
///  W.L. Taber         (JPL)
///  I.M. Underwood     (JPL)
/// ```
///
/// # Version
///
/// ```text
/// -    SPICELIB Version 1.4.0, 25-AUG-2021 (JDR) (NJB)
///
///         Added IMPLICIT NONE statement.
///
///         Updated to remove unnecessary lines of code in the
///         Standard SPICE error handling CHKIN statements.
///
///         Edited the header to comply to NAIF standard. Added complete
///         code example, problem statement and solution. Added entry #3 in
///         $Exceptions section.
///
///         Removed irrelevant information related to other unary window
///         routines from $Particulars section.
///
/// -    SPICELIB Version 1.3.0, 04-MAR-1993 (KRG)
///
///         There was a bug when moving the intervals in the cell
///         to the right when inserting a new interval to the left
///         of the left most interval. The incrementing in the DO
///         loop was incorrect.
///
///         The loop used to read:
///
///            DO J = I-1, CARD
///               WINDOW(J+2) = WINDOW(J)
///            END DO
///
///         which squashed everything to the right of the first interval
///         with the values of the first interval.
///
///         The loop now reads:
///
///            DO J = CARD, I-1, -1
///               WINDOW(J+2) = WINDOW(J)
///            END DO
///
///         which correctly scoots the elements in reverse order,
///         preserving their values.
///
/// -    SPICELIB Version 1.0.1, 10-MAR-1992 (WLT)
///
///         Comment section for permuted index source lines was added
///         following the header.
///
/// -    SPICELIB Version 1.0.0, 31-JAN-1990 (WLT) (IMU)
/// ```
///
/// # Revisions
///
/// ```text
/// -    SPICELIB Version 1.3.0, 04-MAR-1993 (KRG)
///
///         There was a bug when moving the intervals in the cell
///         to the right when inserting a new interval to the left
///         of the left most interval. the incrementing in the DO
///         loop was incorrect.
///
///         The loop used to read:
///
///            DO J = I-1, CARD
///               WINDOW(J+2) = WINDOW(J)
///            END DO
///
///         which squashed everything to the right of the first interval
///         with the values of the first interval.
///
///         The loop now reads:
///
///            DO J = CARD, I-1, -1
///               WINDOW(J+2) = WINDOW(J)
///            END DO
///
///         which correctly scoots the elements in reverse order,
///         preserving their values.
///
/// -    Beta Version 1.2.0, 27-FEB-1989 (HAN)
///
///         Due to the calling sequence and functionality changes
///         in the routine EXCESS, the method of signaling an
///         excess of elements needed to be changed.
///
/// -    Beta Version 1.1.0, 17-FEB-1989 (HAN) (NJB)
///
///         Contents of the $Required_Reading section was
///         changed from "None." to "WINDOWS".  Also, the
///         declaration of the unused variable K was removed.
/// ```
pub fn wninsd(
    ctx: &mut SpiceContext,
    left: f64,
    right: f64,
    window: &mut [f64],
) -> crate::Result<()> {
    WNINSD(left, right, window, ctx.raw_context())?;
    ctx.handle_errors()?;
    Ok(())
}

//$Procedure WNINSD ( Insert an interval into a DP window )
pub fn WNINSD(
    LEFT: f64,
    RIGHT: f64,
    WINDOW: &mut [f64],
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let mut WINDOW = DummyArrayMut::new(WINDOW, LBCELL..);
    let mut SIZE: i32 = 0;
    let mut CARD: i32 = 0;
    let mut I: i32 = 0;
    let mut J: i32 = 0;

    //
    // SPICELIB functions
    //

    //
    // Local Variables
    //

    //
    // Standard SPICE error handling.
    //
    if RETURN(ctx) {
        return Ok(());
    }

    CHKIN(b"WNINSD", ctx)?;

    //
    // Get the size and cardinality of the window.
    //
    SIZE = SIZED(WINDOW.as_slice(), ctx)?;
    CARD = CARDD(WINDOW.as_slice(), ctx)?;

    //
    // Let's try the easy cases first. No input interval? No change.
    // Signal that an error has occurred and set the error message.
    //
    if (LEFT > RIGHT) {
        SETMSG(b"Left endpoint was *. Right endpoint was *.", ctx);
        ERRDP(b"*", LEFT, ctx);
        ERRDP(b"*", RIGHT, ctx);
        SIGERR(b"SPICE(BADENDPOINTS)", ctx)?;
        CHKOUT(b"WNINSD", ctx)?;
        return Ok(());

    //
    // Empty window? Input interval later than the end of the window?
    // Just insert the interval, if there's room.
    //
    } else if ((CARD == 0) || (LEFT > WINDOW[CARD])) {
        if (SIZE >= (CARD + 2)) {
            SCARDD((CARD + 2), WINDOW.as_slice_mut(), ctx)?;
            WINDOW[(CARD + 1)] = LEFT;
            WINDOW[(CARD + 2)] = RIGHT;
        } else {
            EXCESS(2, b"window", ctx)?;
            SIGERR(b"SPICE(WINDOWEXCESS)", ctx)?;
        }

        CHKOUT(b"WNINSD", ctx)?;
        return Ok(());
    }

    //
    // Now on to the tougher cases.
    //
    // Skip intervals which lie completely to the left of the input
    // interval. (The index I will always point to the right endpoint
    // of an interval).
    //
    I = 2;

    while ((I <= CARD) && (WINDOW[I] < LEFT)) {
        I = (I + 2);
    }

    //
    // There are three ways this can go. The new interval can:
    //
    //    1) lie entirely between the previous interval and the next.
    //
    //    2) overlap the next interval, but no others.
    //
    //    3) overlap more than one interval.
    //
    // Only the first case can possibly cause an overflow, since the
    // other two cases require existing intervals to be merged.
    //

    //
    // Case (1). If there's room, move succeeding intervals back and
    // insert the new one. If there isn't room, signal an error.
    //
    if (RIGHT < WINDOW[(I - 1)]) {
        if (SIZE >= (CARD + 2)) {
            {
                let m1__: i32 = CARD;
                let m2__: i32 = (I - 1);
                let m3__: i32 = -1;
                J = m1__;
                for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
                    WINDOW[(J + 2)] = WINDOW[J];
                    J += m3__;
                }
            }

            SCARDD((CARD + 2), WINDOW.as_slice_mut(), ctx)?;
            WINDOW[(I - 1)] = LEFT;
            WINDOW[I] = RIGHT;
        } else {
            EXCESS(2, b"window", ctx)?;
            SIGERR(b"SPICE(WINDOWEXCESS)", ctx)?;
            CHKOUT(b"WNINSD", ctx)?;
            return Ok(());
        }

    //
    // Cases (2) and (3).
    //
    } else {
        //
        // The left and right endpoints of the new interval may or
        // may not replace the left and right endpoints of the existing
        // interval.
        //
        WINDOW[(I - 1)] = intrinsics::DMIN1(&[LEFT, WINDOW[(I - 1)]]);
        WINDOW[I] = intrinsics::DMAX1(&[RIGHT, WINDOW[I]]);

        //
        // Skip any intervals contained in the one we modified.
        // (Like I, J always points to the right endpoint of an
        // interval.)
        //
        J = (I + 2);

        while ((J <= CARD) && (WINDOW[J] <= WINDOW[I])) {
            J = (J + 2);
        }

        //
        // If the modified interval extends into the next interval,
        // merge the two. (The modified interval grows to the right.)
        //
        if ((J <= CARD) && (WINDOW[I] >= WINDOW[(J - 1)])) {
            WINDOW[I] = WINDOW[J];
            J = (J + 2);
        }

        //
        // Move the rest of the intervals forward to take up the
        // spaces left by the absorbed intervals.
        //
        while (J <= CARD) {
            I = (I + 2);
            WINDOW[(I - 1)] = WINDOW[(J - 1)];
            WINDOW[I] = WINDOW[J];
            J = (J + 2);
        }

        SCARDD(I, WINDOW.as_slice_mut(), ctx)?;
    }

    CHKOUT(b"WNINSD", ctx)?;
    Ok(())
}