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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
/// Nearest point on triangular plate
///
/// Find the nearest point on a triangular plate to a given point.
///
/// # Required Reading
///
/// * [DSK](crate::required_reading::dsk)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// POINT I A point in 3-dimensional space.
/// V1,
/// V2,
/// V3 I Vertices of a triangular plate.
/// PNEAR O Nearest point on the plate to POINT.
/// DIST O Distance between PNEAR and POINT.
/// ```
///
/// # Detailed Input
///
/// ```text
/// POINT is an arbitrary point in 3-dimensional space.
///
/// V1,
/// V2,
/// V3 are 3-vectors constituting the vertices of
/// a triangular plate.
///
/// The plate is allowed to be degenerate: it may
/// consist of a line segment or of a single point.
/// ```
///
/// # Detailed Output
///
/// ```text
/// PNEAR is the closest point on the plate to POINT.
/// PNEAR is unique, since the plate is convex.
///
/// DIST is the distance between POINT and PNEAR.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) The input plate is allowed to be degenerate: it may be
/// a line segment or a single point.
/// ```
///
/// # Examples
///
/// ```text
/// The numerical results shown for this example may differ across
/// platforms. The results depend on the SPICE kernels used as input
/// (if any), the compiler and supporting libraries, and the machine
/// specific arithmetic implementation.
///
///
/// 1) Find the nearest point to the point (2,2,2) on a plate having
/// vertices at the unit basis vectors that lie along the positive
/// X, Y, and Z coordinate axes.
///
///
/// Example code begins here.
///
///
/// PROGRAM PLTNP_EX1
/// IMPLICIT NONE
///
/// C
/// C Local parameters
/// C
/// CHARACTER*(*) FMT1
/// PARAMETER ( FMT1 = '(A,3E15.7)' )
/// C
/// C Local variables
/// C
/// DOUBLE PRECISION DIST
/// DOUBLE PRECISION POINT ( 3 )
/// DOUBLE PRECISION PNEAR ( 3 )
/// DOUBLE PRECISION V1 ( 3 )
/// DOUBLE PRECISION V2 ( 3 )
/// DOUBLE PRECISION V3 ( 3 )
///
/// C
/// C POINT is the input point.
/// C
/// CALL VPACK ( 2.D0, 2.D0, 2.D0, POINT )
/// C
/// C V1, V2, V3 are the vertices of a plate.
/// C
/// CALL VPACK ( 1.D0, 0.D0, 0.D0, V1 )
/// CALL VPACK ( 0.D0, 1.D0, 0.D0, V2 )
/// CALL VPACK ( 0.D0, 0.D0, 1.D0, V3 )
/// C
/// C Find the near point on the plate.
/// C
/// CALL PLTNP ( POINT, V1, V2, V3, PNEAR, DIST )
///
/// WRITE (*,*) ' '
/// WRITE (*,FMT1) 'Plate vertex 1 = ', V1
/// WRITE (*,FMT1) 'Plate vertex 2 = ', V2
/// WRITE (*,FMT1) 'Plate vertex 3 = ', V3
/// WRITE (*,FMT1) 'Input point = ', POINT
/// WRITE (*,*) ' '
/// WRITE (*,FMT1) 'Near point = ', PNEAR
/// WRITE (*,FMT1) 'Distance = ', DIST
/// WRITE (*,*) ' '
///
/// END
///
///
/// When this program was executed on a Mac/Intel/gfortran/64-bit
/// platform, the output was:
///
///
/// Plate vertex 1 = 0.1000000E+01 0.0000000E+00 0.0000000E+00
/// Plate vertex 2 = 0.0000000E+00 0.1000000E+01 0.0000000E+00
/// Plate vertex 3 = 0.0000000E+00 0.0000000E+00 0.1000000E+01
/// Input point = 0.2000000E+01 0.2000000E+01 0.2000000E+01
///
/// Near point = 0.3333333E+00 0.3333333E+00 0.3333333E+00
/// Distance = 0.2886751E+01
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.3, 04-JUL-2021 (JDR)
///
/// Edited the header to comply with NAIF standard.
///
/// Edited code example output format for the solution to fit
/// within the $Examples section without modifications. Added
/// DSK to $Required_Readings.
///
/// - SPICELIB Version 1.1.2, 01-FEB-2016 (NJB)
///
/// Added code example to header.
///
/// DSKLIB Version 1.1.1, 19-MAR-2015 (NJB)
///
/// Fixed spelling error in header.
///
/// DSKLIB Version 1.1.0, 31-DEC-2014 (NJB)
///
/// Bug fix: vertex indices for outside case, near
/// point on 3rd edge were corrected.
///
/// DSKLIB Version 1.0.0, 29-SEP-2014 (NJB)
/// ```
pub fn pltnp(
ctx: &mut SpiceContext,
point: &[f64; 3],
v1: &[f64; 3],
v2: &[f64; 3],
v3: &[f64; 3],
pnear: &mut [f64; 3],
dist: &mut f64,
) -> crate::Result<()> {
PLTNP(point, v1, v2, v3, pnear, dist, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure PLTNP ( Nearest point on triangular plate )
pub fn PLTNP(
POINT: &[f64],
V1: &[f64],
V2: &[f64],
V3: &[f64],
PNEAR: &mut [f64],
DIST: &mut f64,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let POINT = DummyArray::new(POINT, 1..=3);
let V1 = DummyArray::new(V1, 1..=3);
let V2 = DummyArray::new(V2, 1..=3);
let V3 = DummyArray::new(V3, 1..=3);
let mut PNEAR = DummyArrayMut::new(PNEAR, 1..=3);
let mut D1: f64 = 0.0;
let mut D2: f64 = 0.0;
let mut D3: f64 = 0.0;
let mut E1 = StackArray::<f64, 3>::new(1..=3);
let mut E2 = StackArray::<f64, 3>::new(1..=3);
let mut E3 = StackArray::<f64, 3>::new(1..=3);
let mut ENORM1 = StackArray::<f64, 3>::new(1..=3);
let mut ENORM2 = StackArray::<f64, 3>::new(1..=3);
let mut ENORM3 = StackArray::<f64, 3>::new(1..=3);
let mut L1: f64 = 0.0;
let mut L2: f64 = 0.0;
let mut L3: f64 = 0.0;
let mut NORMAL = StackArray::<f64, 3>::new(1..=3);
let mut NP1 = StackArray::<f64, 3>::new(1..=3);
let mut NP2 = StackArray::<f64, 3>::new(1..=3);
let mut PDIFF = StackArray::<f64, 3>::new(1..=3);
let mut PERP = StackArray::<f64, 3>::new(1..=3);
let mut DEGEN: bool = false;
let mut IN1: bool = false;
let mut IN2: bool = false;
let mut IN3: bool = false;
//
// SPICELIB functions
//
//
// Local variables
//
if RETURN(ctx) {
return Ok(());
}
//
// Use discovery check-in.
//
//
// Compute the plate's edges.
//
VSUB(V2.as_slice(), V1.as_slice(), E1.as_slice_mut());
VSUB(V3.as_slice(), V2.as_slice(), E2.as_slice_mut());
VSUB(V1.as_slice(), V3.as_slice(), E3.as_slice_mut());
//
// Compute a normal vector for the plate, if possible.
// If the plate is degenerate, we'll find out at this point.
//
VCRSS(E1.as_slice(), E2.as_slice(), NORMAL.as_slice_mut());
//
// Compute the outward normals of the plate's edges in the
// plate containing the plate.
//
VCRSS(E1.as_slice(), NORMAL.as_slice(), ENORM1.as_slice_mut());
VCRSS(E2.as_slice(), NORMAL.as_slice(), ENORM2.as_slice_mut());
VCRSS(E3.as_slice(), NORMAL.as_slice(), ENORM3.as_slice_mut());
DEGEN = (((VZERO(NORMAL.as_slice()) || VZERO(ENORM1.as_slice())) || VZERO(ENORM2.as_slice()))
|| VZERO(ENORM3.as_slice()));
if DEGEN {
//
// The "plate" is a line segment or point. Determine
// which case we have.
//
L1 = VNORM(E1.as_slice());
L2 = VNORM(E2.as_slice());
L3 = VNORM(E3.as_slice());
if ((L1 == 0.0) && (L2 == 0.0)) {
//
// Up to round-off error, the vertices coincide.
// The vertex V1 for practical purposes is the plate.
//
VEQU(V1.as_slice(), PNEAR.as_slice_mut());
*DIST = VDIST(PNEAR.as_slice(), POINT.as_slice());
} else {
//
// The plate is a line segment having positive length.
// One of the edges will coincide with the segment.
// Determine which vertices are the endpoints.
//
if (L1 > intrinsics::DMAX1(&[L2, L3])) {
//
// The segment is bounded by V1 and V2.
//
NPSGPT(
V1.as_slice(),
V2.as_slice(),
POINT.as_slice(),
PNEAR.as_slice_mut(),
DIST,
ctx,
)?;
} else if (L2 > intrinsics::DMAX1(&[L3, L1])) {
//
// The segment is bounded by V2 and V3.
//
NPSGPT(
V2.as_slice(),
V3.as_slice(),
POINT.as_slice(),
PNEAR.as_slice_mut(),
DIST,
ctx,
)?;
} else {
//
// The segment is bounded by V3 and V1.
//
NPSGPT(
V3.as_slice(),
V1.as_slice(),
POINT.as_slice(),
PNEAR.as_slice_mut(),
DIST,
ctx,
)?;
}
}
//
// The outputs are set for the degenerate cases.
//
return Ok(());
}
//
// We have a non-degenerate plate. NORMAL has unit length.
//
// We'll treat V1 as an origin in the plane containing
// the plate. Find the offset of the POINT from V1, and
// find the component of this offset orthogonal to NORMAL.
//
VSUB(POINT.as_slice(), V1.as_slice(), PDIFF.as_slice_mut());
VPERP(PDIFF.as_slice(), NORMAL.as_slice(), PERP.as_slice_mut());
//
// Determine whether V1+PERP is inside the plate.
//
// Note that the "line constants" for edges 1 and 3
// are zero, since these edges contain V1. The line
// constant for edge 2 is that of the offset of V2
// from V1; this offset is edge 1.
//
IN1 = (VDOT(PERP.as_slice(), ENORM1.as_slice()) <= 0.0);
IN2 = (VDOT(PERP.as_slice(), ENORM2.as_slice()) <= VDOT(E1.as_slice(), ENORM2.as_slice()));
IN3 = (VDOT(PERP.as_slice(), ENORM3.as_slice()) <= 0.0);
if ((IN1 && IN2) && IN3) {
//
// V1+PERP is inside the plate. It is the closest
// point on the plate to POINT.
//
VADD(V1.as_slice(), PERP.as_slice(), PNEAR.as_slice_mut());
//
// We have the near point; set the distance.
//
*DIST = VDIST(PNEAR.as_slice(), POINT.as_slice());
} else {
//
// PERP is outside the plate. The nearest point
// on the plate to POINT is on one of the edges.
//
// We'll use the "in" flags to reduce the number
// of point-edge distance computations.
//
if (!IN1 && (IN2 && IN3)) {
//
// The solution must be on the first edge.
//
NPSGPT(
V1.as_slice(),
V2.as_slice(),
POINT.as_slice(),
PNEAR.as_slice_mut(),
DIST,
ctx,
)?;
} else if (!IN2 && (IN3 && IN1)) {
//
// The solution must be on the second edge.
//
NPSGPT(
V2.as_slice(),
V3.as_slice(),
POINT.as_slice(),
PNEAR.as_slice_mut(),
DIST,
ctx,
)?;
} else if (!IN3 && (IN1 && IN2)) {
//
// The solution must be on the third edge.
//
NPSGPT(
V3.as_slice(),
V1.as_slice(),
POINT.as_slice(),
PNEAR.as_slice_mut(),
DIST,
ctx,
)?;
} else {
//
// Compute solutions on all three edges and pick
// the best one.
//
NPSGPT(
V1.as_slice(),
V2.as_slice(),
POINT.as_slice(),
NP1.as_slice_mut(),
&mut D1,
ctx,
)?;
NPSGPT(
V2.as_slice(),
V3.as_slice(),
POINT.as_slice(),
NP2.as_slice_mut(),
&mut D2,
ctx,
)?;
NPSGPT(
V3.as_slice(),
V1.as_slice(),
POINT.as_slice(),
PNEAR.as_slice_mut(),
&mut D3,
ctx,
)?;
if (D1 <= intrinsics::DMIN1(&[D2, D3])) {
VEQU(NP1.as_slice(), PNEAR.as_slice_mut());
*DIST = D1;
} else if (D2 <= intrinsics::DMIN1(&[D3, D1])) {
VEQU(NP2.as_slice(), PNEAR.as_slice_mut());
*DIST = D2;
} else {
//
// PNEAR is already set.
//
*DIST = D3;
}
}
}
Ok(())
}