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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
pub const LBCELL: i32 = -5;
/// Difference two DP windows
///
/// Place the difference of two double precision windows into
/// a third window.
///
/// # Required Reading
///
/// * [WINDOWS](crate::required_reading::windows)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// A,
/// B I Input windows.
/// C O Difference of A and B.
/// ```
///
/// # Detailed Input
///
/// ```text
/// A,
/// B are SPICE windows, each of which contains zero or more
/// intervals.
/// ```
///
/// # Detailed Output
///
/// ```text
/// C is the output SPICE window, containing the difference
/// of A and B --- every point contained in A, but not
/// contained in B.
///
/// C must be distinct from both A and B.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the difference of the two windows results in an excess of
/// elements, the error SPICE(WINDOWEXCESS) is signaled.
///
/// 2) The cardinality of the input windows 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
/// Mathematically, the difference of two windows contains every
/// point contained in the first window but not contained in the
/// second window.
///
/// Fortran offers no satisfactory floating point representation
/// of open intervals. Thus, for floating point windows we must
/// return the closure of the set theoretical difference: that is,
/// the difference plus the endpoints of the first window that are
/// contained in the second window.
/// ```
///
/// # Examples
///
/// ```text
/// Let A contain the intervals
///
/// [ 1, 3 ] [ 7, 11 ] [ 23, 27 ]
///
/// and B contain the intervals
///
/// [ 2, 4 ] [ 8, 10 ] [ 16, 18 ]
///
/// Then the difference of A and B contains the intervals
///
/// [ 1, 2 ] [ 7, 8 ] [ 10, 11 ] [ 23, 27 ]
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// H.A. Neilan (JPL)
/// W.L. Taber (JPL)
/// I.M. Underwood (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 2.1.0, 24-AUG-2021 (JDR) (NJB)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard. Fixed I/O type
/// of argument C in $Brief_I/O table.
///
/// Added entry #2 in $Exceptions section.
///
/// - SPICELIB Version 2.0.0, 16-SEP-1998 (WLT)
///
/// The previous version did not work when removing
/// singletons. This has been corrected.
///
/// - 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) (HAN)
/// ```
///
/// # Revisions
///
/// ```text
/// - Beta Version 1.1.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.
/// ```
pub fn wndifd(ctx: &mut SpiceContext, a: &[f64], b: &[f64], c: &mut [f64]) -> crate::Result<()> {
WNDIFD(a, b, c, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure WNDIFD ( Difference two DP windows )
pub fn WNDIFD(A: &[f64], B: &[f64], C: &mut [f64], ctx: &mut Context) -> f2rust_std::Result<()> {
let A = DummyArray::new(A, LBCELL..);
let B = DummyArray::new(B, LBCELL..);
let mut C = DummyArrayMut::new(C, LBCELL..);
let mut ACARD: i32 = 0;
let mut BCARD: i32 = 0;
let mut CSIZE: i32 = 0;
let mut APB: i32 = 0;
let mut APE: i32 = 0;
let mut BPB: i32 = 0;
let mut BPE: i32 = 0;
let mut PUT: i32 = 0;
let mut F: f64 = 0.0;
let mut L: f64 = 0.0;
let mut NEEDED: i32 = 0;
let mut OVER: i32 = 0;
let mut KEEP: bool = false;
let mut UNRSLV: bool = false;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
}
CHKIN(b"WNDIFD", ctx)?;
//
// Find the cardinality of the input windows, and the allowed size
// of the output window. Also, save the size of the second window.
//
ACARD = CARDD(A.as_slice(), ctx)?;
BCARD = CARDD(B.as_slice(), ctx)?;
CSIZE = SIZED(C.as_slice(), ctx)?;
OVER = 0;
//
// Empty out the output window.
//
SSIZED(CSIZE, C.as_slice_mut(), ctx)?;
//
// Let's handle the pathological cases first.
//
if (BCARD == 0) {
COPYD(A.as_slice(), C.as_slice_mut(), ctx)?;
CHKOUT(b"WNDIFD", ctx)?;
return Ok(());
} else if (ACARD == 0) {
CHKOUT(b"WNDIFD", ctx)?;
return Ok(());
}
//
// Now get pointers to the first intervals of A and B.
//
APB = 1;
APE = 2;
BPB = 1;
BPE = 2;
PUT = 1;
//
// As long as the end pointer for A is less than the cardinality
// of A we need to examine intervals and decide how much of
// them to keep in C.
//
while (APE <= ACARD) {
//
// We will work with the interval [F,L] which starts out
// as the next interval of A. We modify it below as required
// when subtracting out intervals of B.
//
F = A[APB];
L = A[APE];
//
// Right now we have not resolved whether to keep the interval
// [F,L], but until we know better we assume it is a keeper.
//
UNRSLV = (BPE <= BCARD);
KEEP = true;
while UNRSLV {
if (L < B[BPB]) {
//
// The interval [F,L] is before the next interval of B, we
// have resolved what to do with this one. It is a
// keeper.
//
UNRSLV = false;
} else if (F > B[BPE]) {
//
// [F,L] is after the end of the current interval in B,
// we need to look at the next interval of B
//
BPB = (BPB + 2);
BPE = (BPE + 2);
UNRSLV = (BPE <= BCARD);
} else {
//
// There is some overlap between the current interval
// of B and the current interval of A. There are
// several possibilities
//
// 1) The current interval of A is contained in the
// current interval of B (This includes singleton
// intervals in A). We just mark [F,L] so that it
// won't be kept. We have fully resolved what to
// do with [F,L].
//
// 2) The interval from B overlaps at the beginning
// of the interval of A
//
// B interval [......]
// A interval [............]
// result of A-B [.........]
//
// In this case we need to shrink the interval [F,L]
// but we have not resolved how much of the result
// to keep.
//
// 3) The interval from B falls inside the current
// interval [F,L]
//
// B interval [......]
// A interval [............]
// result of A-B [..] [..]
//
// If the interval from B is not a singleton, we store
// the first part of [F,L] in C and then set [F,L] to
// be the right interval which is still not resolved.
//
// If the B interval is a singleton we can ignore ignore
// it. But we have not resolved what to do about
// [F,L], we need to look at the next interval of B.
//
//
// 4) The interval from B overlaps at the ending
// of the interval of A
//
// B interval [......]
// A interval [......]
// result of A-B [....]
//
// We need to shrink [F,L]. In this case we know we can
// keep all of what's left because all other intervals
// of B are to the right of [F,L]
//
if ((B[BPB] <= F) && (L <= B[BPE])) {
//
// Case 1 above
//
KEEP = false;
UNRSLV = false;
} else if (B[BPB] <= F) {
//
// Case 2 above
//
F = B[BPE];
BPB = (BPB + 2);
BPE = (BPE + 2);
UNRSLV = (BPE <= BCARD);
} else if (((F <= B[BPB]) && (L >= B[BPE])) && (B[BPB] < B[BPE])) {
//
// Case 3 above (non-singleton interval of B).
//
if (PUT < CSIZE) {
C[PUT] = F;
C[(PUT + 1)] = B[BPB];
SCARDD((PUT + 1), C.as_slice_mut(), ctx)?;
PUT = (PUT + 2);
} else {
OVER = (OVER + 2);
}
F = B[BPE];
//
// If the interval from B contained L, we will not
// want to be keeping the singleton [F,L].
//
if (F == L) {
KEEP = false;
UNRSLV = false;
}
BPB = (BPB + 2);
BPE = (BPE + 2);
UNRSLV = (UNRSLV && (BPE <= BCARD));
} else if (((F <= B[BPB]) && (L >= B[BPE])) && (B[BPB] == B[BPE])) {
//
// Case 3 above (singleton interval of B).
//
BPB = (BPB + 2);
BPE = (BPE + 2);
UNRSLV = (BPE <= BCARD);
} else {
//
// Case 4 above
//
L = B[BPB];
UNRSLV = false;
}
}
}
//
// If there is anything to keep in C, put it there.
//
if KEEP {
//
// Make sure there is sufficient room to do the putting.
//
if (PUT < CSIZE) {
C[PUT] = F;
C[(PUT + 1)] = L;
SCARDD((PUT + 1), C.as_slice_mut(), ctx)?;
PUT = (PUT + 2);
} else {
OVER = (OVER + 2);
}
}
//
// Move the pointers in A to the next interval.
//
APB = (APB + 2);
APE = (APE + 2);
}
//
// We've examined all of the intervals of A and B, but if we
// didn't actually store all of the difference, signal an error.
//
if (OVER > 0) {
NEEDED = (OVER + CSIZE);
SETMSG(b"The output window did not have sufficient room to contain the result of the window difference. It has room for # endpoints, but # were needed to describe the difference. ", ctx);
ERRINT(b"#", CSIZE, ctx);
ERRINT(b"#", NEEDED, ctx);
SIGERR(b"SPICE(WINDOWEXCESS)", ctx)?;
}
CHKOUT(b"WNDIFD", ctx)?;
Ok(())
}