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
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
const ANGTOL: f64 = 0.001;
//$Procedure CK3SDN ( Down sample type 3 CK data prepared for writing )
pub fn CK3SDN(
SDNTOL: f64,
AVFLAG: bool,
NREC: &mut i32,
SCLKDP: &mut [f64],
QUATS: &mut [f64],
AVVS: &mut [f64],
NINTS: i32,
STARTS: &[f64],
DPARR: &mut [f64],
INTARR: &mut [i32],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let mut SCLKDP = DummyArrayMut::new(SCLKDP, 1..);
let mut QUATS = DummyArrayMut2D::new(QUATS, 0..=3, 1..);
let mut AVVS = DummyArrayMut2D::new(AVVS, 1..=3, 1..);
let STARTS = DummyArray::new(STARTS, 1..);
let mut DPARR = DummyArrayMut::new(DPARR, 1..);
let mut INTARR = DummyArrayMut::new(INTARR, 1..);
let mut FRAC: f64 = 0.0;
let mut QINTRP = StackArray::<f64, 4>::new(0..=3);
let mut QKEEPF = StackArray::<f64, 4>::new(0..=3);
let mut QKEEPL = StackArray::<f64, 4>::new(0..=3);
let mut QLNEG = StackArray::<f64, 4>::new(0..=3);
let mut QLINPT = StackArray::<f64, 4>::new(0..=3);
let mut ANGLE: f64 = 0.0;
let mut DIST2: f64 = 0.0;
let mut DIST2A: f64 = 0.0;
let mut DIST2B: f64 = 0.0;
let mut DPOS: f64 = 0.0;
let mut DNEG: f64 = 0.0;
let mut COSVAL: f64 = 0.0;
let mut J: i32 = 0;
let mut INTNRF: i32 = 0;
let mut INTCRF: i32 = 0;
let mut INTCRL: i32 = 0;
let mut KEEPF: i32 = 0;
let mut KEEPL: i32 = 0;
let mut NDROPD: i32 = 0;
let mut LEFT: i32 = 0;
let mut RIGHT: i32 = 0;
let mut SKIPIT: bool = false;
let mut FITOK: bool = false;
//
// SPICELIB functions.
//
//
// Local parameters.
//
// Tolerance for 180 separation check, in radians.
//
//
// Local variables.
//
//
// SPICELIB functions.
//
//
// Standard SPICE error handling.
//
if spicelib::RETURN(ctx) {
return Ok(());
} else {
spicelib::CHKIN(b"CK3SDN", ctx)?;
}
//
// Let's do some sanity checks that needed to make sure that future
// loops and comparisons don't blow up. First, verify that the
// number pointing records is greater that zero.
//
if (*NREC <= 0) {
spicelib::SETMSG(
b"The number of pointing records must be greater than zero. It was #.",
ctx,
);
spicelib::ERRINT(b"#", *NREC, ctx);
spicelib::SIGERR(b"SPICE(INVALIDNUMBEROFRECORDS)", ctx)?;
spicelib::CHKOUT(b"CK3SDN", ctx)?;
return Ok(());
}
//
// Then, verify that the number intervals is greater that zero.
//
if (NINTS <= 0) {
spicelib::SETMSG(
b"The number of interval starts must be greater than zero. It was #.",
ctx,
);
spicelib::ERRINT(b"#", NINTS, ctx);
spicelib::SIGERR(b"SPICE(INVALIDNUMBEROFINTERVALS)", ctx)?;
spicelib::CHKOUT(b"CK3SDN", ctx)?;
return Ok(());
}
//
// Then, verify that the number intervals is less than or equal to
// the number of records.
//
if (NINTS > *NREC) {
spicelib::SETMSG(b"The number of interval starts, #, is not less than or equal to the number of records, #.", ctx);
spicelib::ERRINT(b"#", NINTS, ctx);
spicelib::ERRINT(b"#", *NREC, ctx);
spicelib::SIGERR(b"SPICE(BUFFERSIZESMISMATCH)", ctx)?;
spicelib::CHKOUT(b"CK3SDN", ctx)?;
return Ok(());
}
//
// Then verify that the first time in the intervals array is the same
// as the first time in the records array.
//
if (SCLKDP[1] != STARTS[1]) {
spicelib::SETMSG(
b"The first interval start time, #, is not the same as the first record time, #.",
ctx,
);
spicelib::ERRDP(b"#", SCLKDP[1], ctx);
spicelib::ERRDP(b"#", STARTS[1], ctx);
spicelib::SIGERR(b"SPICE(FIRSTRECORDMISMATCH)", ctx)?;
spicelib::CHKOUT(b"CK3SDN", ctx)?;
return Ok(());
}
//
// Finally verify that input down sampling tolerance is not positive
// number.
//
if (SDNTOL < 0.0) {
spicelib::SETMSG(
b"The down sampling tolerance must be a non-negative number. It was #.",
ctx,
);
spicelib::ERRDP(b"#", SDNTOL, ctx);
spicelib::SIGERR(b"SPICE(BADDOWNSAMPLINGTOL)", ctx)?;
spicelib::CHKOUT(b"CK3SDN", ctx)?;
return Ok(());
}
//
// This variable will hold to the index of the pointing record that
// matches the start of the next interval. For the first interval
// it is set to one.
//
INTNRF = 1;
//
// We will count the number of points that were dropped.
//
NDROPD = 0;
//
// Loop through interpolation intervals.
//
for I in 1..=NINTS {
//
// Assign the index of the pointing record that matches the
// begin time of this interval.
//
INTCRF = INTNRF;
//
// Find the index of the pointing record that ends this interval.
// If this the last interval, it is the last pointing record in
// pointing buffer.
//
if (I == NINTS) {
INTCRL = *NREC;
} else {
//
// This is not the last interval. To get its end time we need
// to find the pointing record that matches the start of the
// next interval and pick the record before it.
//
// First we find index of the pointing record that corresponds
// to the start of the next interval.
//
INTNRF = spicelib::BSRCHD(
STARTS[(I + 1)],
((*NREC - INTCRF) + 1),
SCLKDP.subarray(INTCRF),
);
if (INTNRF != 0) {
//
// Found index must be adjusted to be relative to the
// beginning of the buffer. Currently it is relative to the
// start of the current interval.
//
INTNRF = ((INTNRF + INTCRF) - 1);
//
// The index of the last record belonging to this interval
// in the found index minus 1.
//
INTCRL = (INTNRF - 1);
} else {
//
// We did not find such record. The input buffer must have
// been formed improperly for this to happen. Signal an
// error.
//
spicelib::SETMSG(b"Cannot find pointing record with time that matches the start time # (encoded SCLK ticks) of the interpolation interval number #.", ctx);
spicelib::ERRDP(b"#", STARTS[(I + 1)], ctx);
spicelib::ERRINT(b"#", (I + 1), ctx);
spicelib::SIGERR(b"SPICE(INTERVALSTARTNOTFOUND)", ctx)?;
spicelib::CHKOUT(b"CK3SDN", ctx)?;
return Ok(());
}
}
//
// Let's look at the indexes of the pointing records
// corresponding to the begin and end of this interval. If they
// are the same (meaning it's a singleton interval) or if they
// are next to each other (meaning that the whole set of
// interval's pointing data is comprised of only its begin
// and end points) there is no down sampling to do.
//
SKIPIT = ((INTCRF == INTCRL) || (INTCRF == (INTCRL - 1)));
//
// Set initial values for a binary search.
//
KEEPF = INTCRF;
LEFT = INTCRF;
RIGHT = INTCRL;
while (!SKIPIT && (KEEPF < INTCRL)) {
//
// Set the right endpoint of the interval by dividing the
// binary search region in half.
//
KEEPL = ((LEFT + RIGHT) / 2);
//
// Unitize bracketing quaternions as QMINI seems to be
// very sensitive to that. :)
//
spicelib::VHATG(QUATS.subarray([0, KEEPF]), 4, QKEEPF.as_slice_mut());
spicelib::VHATG(QUATS.subarray([0, KEEPL]), 4, QKEEPL.as_slice_mut());
//
// Pick the closer of the right quaternion or its negative to
// QKEEPF for input into QMINI to ensure that QMINI does
// interpolation in the "shortest arc" direction.
//
spicelib::VMINUG(QKEEPL.as_slice(), 4, QLNEG.as_slice_mut());
DPOS = spicelib::VDISTG(QKEEPL.as_slice(), QKEEPF.as_slice(), 4);
DNEG = spicelib::VDISTG(QLNEG.as_slice(), QKEEPF.as_slice(), 4);
if (DNEG < DPOS) {
spicelib::MOVED(QLNEG.as_slice(), 4, QLINPT.as_slice_mut());
} else {
spicelib::MOVED(QKEEPL.as_slice(), 4, QLINPT.as_slice_mut());
}
//
// If the currently picked window ends are not 180 degrees
// apart, check all records between them to see if
// interpolated pointing is within tolerance of the actual
// pointing. If the currently picked window ends are close to
// 180 degrees apart, don't consider them as a possibility.
//
COSVAL =
((QKEEPF[0] * QLINPT[0]) + spicelib::VDOT(QKEEPF.subarray(1), QLINPT.subarray(1)));
ANGLE = (f64::acos(spicelib::BRCKTD(COSVAL, -1.0, 1.0)) * 2.0);
FITOK = (f64::abs((spicelib::PI(ctx) - ANGLE)) > ANGTOL);
//
// If KEEPF and KEEPL points are next to each other, we will
// declare the fit to be OK even if it does not pass the ``too
// close to 180 degrees apart'' test and set LEFT equal to
// RIGHT to move KEEPF forward. If we don't do this, the
// algorithm goes into indefinite loop.
//
if (((KEEPF + 1) == KEEPL) && !FITOK) {
FITOK = true;
LEFT = RIGHT;
}
J = (KEEPF + 1);
while ((J <= (KEEPL - 1)) && FITOK) {
//
// Compute interpolation fraction for this pointing record.
//
if ((SCLKDP[KEEPL] - SCLKDP[KEEPF]) != 0.0) {
FRAC = ((SCLKDP[J] - SCLKDP[KEEPF]) / (SCLKDP[KEEPL] - SCLKDP[KEEPF]));
} else {
spicelib::SIGERR(b"SPICE(CK3SDNBUG)", ctx)?;
spicelib::CHKOUT(b"CK3SDN", ctx)?;
return Ok(());
}
//
// Call Nat's fast quaternion interpolation routine to
// compute interpolated rotation for this point.
//
QMINI(
QKEEPF.as_slice(),
QLINPT.as_slice(),
FRAC,
QINTRP.as_slice_mut(),
);
//
// Find the squared distance between the interpolated
// and input quaternions.
//
DIST2A = (((((QUATS[[0, J]] - QINTRP[0]) * (QUATS[[0, J]] - QINTRP[0]))
+ ((QUATS[[1, J]] - QINTRP[1]) * (QUATS[[1, J]] - QINTRP[1])))
+ ((QUATS[[2, J]] - QINTRP[2]) * (QUATS[[2, J]] - QINTRP[2])))
+ ((QUATS[[3, J]] - QINTRP[3]) * (QUATS[[3, J]] - QINTRP[3])));
DIST2B = (((((QUATS[[0, J]] + QINTRP[0]) * (QUATS[[0, J]] + QINTRP[0]))
+ ((QUATS[[1, J]] + QINTRP[1]) * (QUATS[[1, J]] + QINTRP[1])))
+ ((QUATS[[2, J]] + QINTRP[2]) * (QUATS[[2, J]] + QINTRP[2])))
+ ((QUATS[[3, J]] + QINTRP[3]) * (QUATS[[3, J]] + QINTRP[3])));
DIST2 = intrinsics::DMIN1(&[DIST2A, DIST2B]);
//
// The rotation angle theta is related to the distance by
// the formula
//
// || Q1 - Q2 || = 2 * | sin(theta/4) |
//
ANGLE = (4.0 * f64::asin(intrinsics::DMIN1(&[(f64::sqrt(DIST2) / 2.0), 1.0])));
//
// Compare the angle with specified threshold.
//
FITOK = (FITOK && (f64::abs(ANGLE) <= SDNTOL));
//
// Increment index to move to the next record.
//
J = (J + 1);
}
//
// Was the fit OK?
//
if FITOK {
//
// Fit was OK. Check if left and right are equal; if so we
// found the point that were were looking for.
//
if (LEFT == RIGHT) {
//
// Mark all records between fist and last with DPMAX.
//
{
let m1__: i32 = (KEEPF + 1);
let m2__: i32 = (KEEPL - 1);
let m3__: i32 = 1;
J = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
SCLKDP[J] = spicelib::DPMAX();
NDROPD = (NDROPD + 1);
J += m3__;
}
}
//
// Set first point for the next search to be equal to
// the to the found point.
//
KEEPF = KEEPL;
//
// Reset window boundaries for binary search.
//
LEFT = KEEPL;
RIGHT = INTCRL;
} else {
//
// Left and right sides haven't converged yet; shift
// left side of the binary search window forward.
//
LEFT = (KEEPL + 1);
}
} else {
//
// No fit; shift right side of the binary search window
// backwards.
//
RIGHT = (KEEPL - 1);
//
// If right side went "over" the left side, set left side
// to be equal to the right side.
//
if (RIGHT < LEFT) {
LEFT = RIGHT;
}
}
}
}
//
// At this point all records that are to be removed, if any, have
// been "tagged" with DPMAX in the times buffer. We need to re-sort
// the buffers to push these records to the bottom and re-set the
// number of records to indicate that only the top portion should be
// used.
//
if (NDROPD != 0) {
//
// Since SCLKs were the ones "marked" by DPMAX, we will use them
// to get the order vector.
//
spicelib::ORDERD(SCLKDP.as_slice(), *NREC, INTARR.as_slice_mut());
//
// Now, with the order vector in hand, sort the SCLKs ...
//
spicelib::REORDD(INTARR.as_slice_mut(), *NREC, SCLKDP.as_slice_mut());
//
// ... then sort quaternions (element by element) ...
//
for I in 0..=3 {
{
let m1__: i32 = 1;
let m2__: i32 = *NREC;
let m3__: i32 = 1;
J = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
DPARR[J] = QUATS[[I, J]];
J += m3__;
}
}
spicelib::REORDD(INTARR.as_slice_mut(), *NREC, DPARR.as_slice_mut());
{
let m1__: i32 = 1;
let m2__: i32 = *NREC;
let m3__: i32 = 1;
J = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
QUATS[[I, J]] = DPARR[J];
J += m3__;
}
}
}
//
// ... and, finally, if requested, sort AVs (also element by
// element) ...
//
if AVFLAG {
for I in 1..=3 {
{
let m1__: i32 = 1;
let m2__: i32 = *NREC;
let m3__: i32 = 1;
J = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
DPARR[J] = AVVS[[I, J]];
J += m3__;
}
}
spicelib::REORDD(INTARR.as_slice_mut(), *NREC, DPARR.as_slice_mut());
{
let m1__: i32 = 1;
let m2__: i32 = *NREC;
let m3__: i32 = 1;
J = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
AVVS[[I, J]] = DPARR[J];
J += m3__;
}
}
}
}
//
// Reset the number of points.
//
*NREC = (*NREC - NDROPD);
}
//
// All done. Check out.
//
spicelib::CHKOUT(b"CK3SDN", ctx)?;
Ok(())
}