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
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
pub const MARGIN: f64 = 0.000000000001;
//$Procedure ZZHULLAX ( Pyramidal FOV convex hull to FOV axis )
pub fn ZZHULLAX(
INST: &[u8],
N: i32,
BOUNDS: &[f64],
AXIS: &mut [f64],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let BOUNDS = DummyArray2D::new(BOUNDS, 1..=3, 1..=N);
let mut AXIS = DummyArrayMut::new(AXIS, 1..=3);
let mut CP = StackArray::<f64, 3>::new(1..=3);
let mut DELTA: f64 = 0.0;
let mut LAT: f64 = 0.0;
let mut LON: f64 = 0.0;
let mut MAXLON: f64 = 0.0;
let mut MINLON: f64 = 0.0;
let mut R: f64 = 0.0;
let mut RAY1 = StackArray::<f64, 3>::new(1..=3);
let mut RAY2 = StackArray::<f64, 3>::new(1..=3);
let mut SEP: f64 = 0.0;
let mut TRANS = StackArray2D::<f64, 9>::new(1..=3, 1..=3);
let mut V = StackArray::<f64, 3>::new(1..=3);
let mut XVEC = StackArray::<f64, 3>::new(1..=3);
let mut YVEC = StackArray::<f64, 3>::new(1..=3);
let mut ZVEC = StackArray::<f64, 3>::new(1..=3);
let mut I: i32 = 0;
let mut M: i32 = 0;
let mut MAXIX: i32 = 0;
let mut MINIX: i32 = 0;
let mut NEXT: i32 = 0;
let mut XIDX: i32 = 0;
let mut FOUND: bool = false;
let mut OK: bool = false;
let mut PASS1: bool = false;
//
// SPICELIB functions
//
//
// Local parameters
//
//
// Local variables
//
if RETURN(ctx) {
return Ok(());
}
CHKIN(b"ZZHULLAX", ctx)?;
//
// Nothing found yet.
//
FOUND = false;
XIDX = 0;
//
// We must have at least 3 boundary vectors.
//
if (N < 3) {
SETMSG(
b"Polygonal FOV requires at least 3 boundary vectors but number supplied for # was #.",
ctx,
);
ERRCH(b"#", INST, ctx);
ERRINT(b"#", N, ctx);
SIGERR(b"SPICE(INVALIDCOUNT)", ctx)?;
CHKOUT(b"ZZHULLAX", ctx)?;
return Ok(());
}
//
// Find an exterior face of the pyramid defined by the
// input boundary vectors. Since most polygonal FOVs will have
// an exterior face bounded by two consecutive rays, we'll
// try pairs of consecutive rays first. If this fails, we'll
// try each pair of rays.
//
I = 1;
while ((I <= N) && !FOUND) {
//
// Set the index of the next ray. When we get to the
// last boundary vector, the next ray is the first.
//
if (I == N) {
NEXT = 1;
} else {
NEXT = (I + 1);
}
//
// Find the cross product of the first ray with the
// second. Depending on the ordering of the boundary
// vectors, this could be an inward or outward normal,
// in the case the current face is exterior.
//
VCRSS(
BOUNDS.subarray([1, I]),
BOUNDS.subarray([1, NEXT]),
CP.as_slice_mut(),
);
//
// We insist on consecutive boundary vectors being
// linearly independent.
//
if VZERO(CP.as_slice()) {
SETMSG(b"Polygonal FOV must have linearly independent consecutive boundary but vectors at indices # and # have cross product equal to the zero vector. Instrument is #.", ctx);
ERRINT(b"#", I, ctx);
ERRINT(b"#", NEXT, ctx);
ERRCH(b"#", INST, ctx);
SIGERR(b"SPICE(DEGENERATECASE)", ctx)?;
CHKOUT(b"ZZHULLAX", ctx)?;
return Ok(());
}
//
// See whether the other boundary vectors have angular
// separation of at least MARGIN from the plane containing
// the current face.
//
PASS1 = true;
OK = true;
M = 1;
while ((M <= N) && OK) {
//
// Find the angular separation of CP and the Mth vector if the
// latter is not an edge of the current face.
//
if ((M != I) && (M != NEXT)) {
SEP = VSEP(CP.as_slice(), BOUNDS.subarray([1, M]), ctx);
if PASS1 {
//
// Adjust CP if necessary so that it points
// toward the interior of the pyramid.
//
if (SEP > HALFPI(ctx)) {
//
// Invert the cross product vector and adjust SEP
// accordingly. Within this "M" loop, all other
// angular separations will be computed using the new
// value of CP.
//
VSCLIP(-1.0, CP.as_slice_mut());
SEP = (PI(ctx) - SEP);
}
PASS1 = false;
}
OK = (SEP < (HALFPI(ctx) - MARGIN));
}
if OK {
//
// Consider the next boundary vector.
//
M = (M + 1);
}
}
//
// We've tested each boundary vector against the current face, or
// else the loop terminated early because a vector with
// insufficient angular separation from the plane containing the
// face was found.
//
if OK {
//
// The current face is exterior. It's bounded by rays I and
// NEXT.
//
XIDX = I;
FOUND = true;
} else {
//
// Look at the next face of the pyramid.
//
I = (I + 1);
}
}
//
// If we didn't find an exterior face, we'll have to look at each
// face bounded by a pair of rays, even if those rays are not
// adjacent. (This can be a very slow process is N is large.)
//
if !FOUND {
I = 1;
while ((I <= N) && !FOUND) {
//
// Consider all ray pairs (I,NEXT) where NEXT > I.
//
NEXT = (I + 1);
while ((NEXT <= N) && !FOUND) {
//
// Find the cross product of the first ray with the second.
// If the current face is exterior, CP could be an inward
// or outward normal, depending on the ordering of the
// boundary vectors.
//
VCRSS(
BOUNDS.subarray([1, I]),
BOUNDS.subarray([1, NEXT]),
CP.as_slice_mut(),
);
//
// It's allowable for non-consecutive boundary vectors to
// be linearly dependent, but if we have such a pair,
// it doesn't define an exterior face.
//
if !VZERO(CP.as_slice()) {
//
// The rays having direction vectors indexed I and NEXT
// define a semi-infinite sector of a plane that might
// be of interest.
//
// Check whether all of the boundary vectors that are
// not edges of the current face have angular separation
// of at least MARGIN from the plane containing the
// current face.
//
PASS1 = true;
OK = true;
M = 1;
while ((M <= N) && OK) {
//
// Find the angular separation of CP and the Mth
// vector if the latter is not an edge of the current
// face.
//
if ((M != I) && (M != NEXT)) {
SEP = VSEP(CP.as_slice(), BOUNDS.subarray([1, M]), ctx);
if PASS1 {
//
// Adjust CP if necessary so that it points
// toward the interior of the pyramid.
//
if (SEP > HALFPI(ctx)) {
//
// Invert the cross product vector and
// adjust SEP accordingly. Within this "M"
// loop, all other angular separations will
// be computed using the new value of CP.
//
VSCLIP(-1.0, CP.as_slice_mut());
SEP = (PI(ctx) - SEP);
}
PASS1 = false;
}
OK = (SEP < (HALFPI(ctx) - MARGIN));
}
if OK {
//
// Consider the next boundary vector.
//
M = (M + 1);
}
}
//
// We've tested each boundary vector against the current
// face, or else the loop terminated early because a
// vector with insufficient angular separation from the
// plane containing the face was found.
//
if OK {
//
// The current face is exterior. It's bounded by rays
// I and NEXT.
XIDX = I;
FOUND = true;
}
//
// End of angular separation test block.
//
}
//
// End of non-zero cross product block.
//
if !FOUND {
//
// Look at the face bounded by the rays
// at indices I and NEXT+1.
//
NEXT = (NEXT + 1);
}
}
//
// End of NEXT loop.
//
if !FOUND {
//
// Look at the face bounded by the pairs of rays
// including the ray at index I+1.
//
I = (I + 1);
}
}
//
// End of I loop.
//
}
//
// End of search for exterior face using each pair of rays.
//
// If we still haven't found an exterior face, we can't continue.
//
if !FOUND {
SETMSG(
b"Unable to find face of convex hull of FOV of instrument #.",
ctx,
);
ERRCH(b"#", INST, ctx);
SIGERR(b"SPICE(FACENOTFOUND)", ctx)?;
CHKOUT(b"ZZHULLAX", ctx)?;
return Ok(());
}
//
// Arrival at this point means that the rays at indices
// XIDX and NEXT define a plane such that all boundary
// vectors lie in a half-space bounded by that plane.
//
// We're now going to define a set of orthonormal basis vectors:
//
// +X points along the angle bisector of the bounding vectors
// of the exterior face.
//
// +Y points along CP.
//
// +Z is the cross product of +X and +Y.
//
// We'll call the reference frame having these basis vectors
// the "face frame."
//
//
VHAT(BOUNDS.subarray([1, I]), RAY1.as_slice_mut());
VHAT(BOUNDS.subarray([1, NEXT]), RAY2.as_slice_mut());
VLCOM(
0.5,
RAY1.as_slice(),
0.5,
RAY2.as_slice(),
XVEC.as_slice_mut(),
);
VHATIP(XVEC.as_slice_mut());
VHAT(CP.as_slice(), YVEC.as_slice_mut());
UCRSS(XVEC.as_slice(), YVEC.as_slice(), ZVEC.as_slice_mut());
//
// Create a transformation matrix to map the input boundary
// vectors into the face frame.
//
{
let m1__: i32 = 1;
let m2__: i32 = 3;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
TRANS[[1, I]] = XVEC[I];
TRANS[[2, I]] = YVEC[I];
TRANS[[3, I]] = ZVEC[I];
I += m3__;
}
}
//
// Now we're going to compute the longitude of each boundary in the
// face frame. The vectors with indices XIDX and NEXT are excluded.
// We expect all longitudes to be between MARGIN and pi - MARGIN.
//
MINLON = PI(ctx);
MAXLON = 0.0;
MINIX = 1;
MAXIX = 1;
{
let m1__: i32 = 1;
let m2__: i32 = N;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
if ((I != XIDX) && (I != NEXT)) {
//
// The current vector is not a boundary of our edge,
// so find its longitude.
//
MXV(TRANS.as_slice(), BOUNDS.subarray([1, I]), V.as_slice_mut());
RECLAT(V.as_slice(), &mut R, &mut LON, &mut LAT);
//
// Update the longitude bounds.
//
if (LON < MINLON) {
MINIX = I;
MINLON = LON;
}
if (LON > MAXLON) {
MAXIX = I;
MAXLON = LON;
}
}
I += m3__;
}
}
//
// If the longitude bounds are not as expected, don't try
// to continue.
//
if (MINLON < ((2 as f64) * MARGIN)) {
SETMSG(b"Minimum boundary vector longitude in exterior face frame is # radians. Minimum occurs at index #. This FOV does not conform to the requirements of this routine. Instrument is #.", ctx);
ERRDP(b"#", MINLON, ctx);
ERRINT(b"#", MINIX, ctx);
ERRCH(b"#", INST, ctx);
SIGERR(b"SPICE(NOTSUPPORTED)", ctx)?;
CHKOUT(b"ZZHULLAX", ctx)?;
return Ok(());
} else if (MAXLON > (PI(ctx) - ((2 as f64) * MARGIN))) {
SETMSG(b"Maximum boundary vector longitude in exterior face frame is # radians. Maximum occurs at index #. This FOV does not conform to the requirements of this routine. Instrument is #.", ctx);
ERRDP(b"#", MAXLON, ctx);
ERRINT(b"#", MAXIX, ctx);
ERRCH(b"#", INST, ctx);
SIGERR(b"SPICE(FOVTOOWIDE)", ctx)?;
CHKOUT(b"ZZHULLAX", ctx)?;
return Ok(());
}
//
// Let delta represent the amount we can rotate the exterior
// face clockwise about +Z without contacting another boundary
// vector.
//
DELTA = (PI(ctx) - MAXLON);
//
// Rotate +Y by -DELTA/2 about +Z. The result is our candidate
// FOV axis. Make the axis vector unit length.
//
VROTV(
YVEC.as_slice(),
ZVEC.as_slice(),
-(DELTA / 2 as f64),
AXIS.as_slice_mut(),
);
VHATIP(AXIS.as_slice_mut());
//
// If we have a viable result, ALL boundary vectors have
// angular separation less than HALFPI-MARGIN from AXIS.
//
{
let m1__: i32 = 1;
let m2__: i32 = N;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
SEP = VSEP(BOUNDS.subarray([1, I]), AXIS.as_slice(), ctx);
if (SEP > (HALFPI(ctx) - MARGIN)) {
SETMSG(b"Boundary vector at index # has angular separation of # radians from candidate FOV axis. This FOV does not conform to the requirements of this routine. Instrument is #.", ctx);
ERRINT(b"#", I, ctx);
ERRDP(b"#", SEP, ctx);
ERRCH(b"#", INST, ctx);
SIGERR(b"SPICE(FOVTOOWIDE)", ctx)?;
CHKOUT(b"ZZHULLAX", ctx)?;
return Ok(());
}
I += m3__;
}
}
CHKOUT(b"ZZHULLAX", ctx)?;
Ok(())
}