-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathHorizontal.java
More file actions
501 lines (443 loc) · 13.9 KB
/
Horizontal.java
File metadata and controls
501 lines (443 loc) · 13.9 KB
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
/*
* Copyright (c) 2011-2021 United States Government as represented by
* the National Aeronautics and Space Administration. No copyright
* is claimed in the United States under Title 17, U.S.Code. All Other
* Rights Reserved.
*/
package gov.nasa.larcfm.ACCoRD;
import static gov.nasa.larcfm.ACCoRD.Consts.*;
import gov.nasa.larcfm.Util.Util;
import gov.nasa.larcfm.Util.Vect2;
import gov.nasa.larcfm.Util.Vect3;
/** Horizontal solution */
public class Horizontal extends Vect2 {
/** parameter */
private double k;
/** Construct a horizontal solution */
Horizontal() {
super(0.0, 0.0);
k = 0;
}
/** Construct a horizontal solution */
Horizontal(Vect2 v) {
super(v.x, v.y);
k = 1;
}
/** Construct a horizontal solution */
private Horizontal(double kk, Vect2 v) {
super(v.x, v.y);
k = kk;
}
/** Is this solution undefined? */
public boolean undef() {
return isZero();
}
/** "Solution" indicating no solution: Warning do not test with == use undef instead! */
static final Horizontal NoHorizontalSolution = new Horizontal();
/** */
static Horizontal best_horizontal(Vect2 vo,Horizontal v1,Horizontal v2) {
if (v1.undef())
return v2;
else if (v2.undef() || v1.leq(v2,vo))
return v1;
return v2;
}
/**
* Time of closest point of approach.
*
* @param v Vector
*
* @return the time of horizontal closest point of approach between <code>this</code> point and the line defined
* by the vector <code>v</code>.
*/
public static double tcpa(Vect2 s, Vect2 v) {
if (!v.isZero())
return -s.dot(v)/v.sqv();
return 0;
}
/**
* Distance closest point of approach.
*
* @param v Vector
*
* @return the horizontal distance at closest point of approach between <code>this</code> point and the line defined
* by the vector <code>v</code>.
*/
public static double dcpa(Vect2 s, Vect2 v) {
return v.ScalAdd(tcpa(s,v),s).norm();
}
/* Horizontal miss distance within lookahead time */
public static double hmd(Vect2 s, Vect2 v, double T) {
double t = 0;
if (s.dot(v) < 0) {
// aircraft are horizontally converging
t = Util.min(tcpa(s,v),T);
}
return v.ScalAdd(t,s).norm();
}
/**
* Compute nv2 such that relative position s (= s1-s2) is the horizontal closest point of approach for s, v1, nv2
* (where nv2 has the same direction as v2).
* If nv2 is zero, either v1 or v2 are zero or no such velocity exists.
*/
public static Vect2 vel_of_hcpa(Vect2 s, Vect2 v1, Vect2 v2) {
Vect2 v = v1.Sub(v2);
if (Util.almost_equals(s.dot(v),0.0)) {
return v2;
} else if (v1.isZero() || v2.isZero()) {
return Vect2.ZERO;
} else {
double sdotv2 = s.dot(v2);
if (Util.almost_equals(sdotv2, 0.0)) {
return Vect2.ZERO;
}
double r = s.dot(v1)/sdotv2;
if (r >= 0) {
return v2.Scal(r);
}
return Vect2.ZERO;
}
}
/**
* Intersection time between line and circle.
*
* @param v Vector
* @param D Diameter of of circle
* @param eps +-1
*
* @return the time <i>t</i> such that the parametric line <code>s</code>+<i>t</i><code>v</code>
* intersects the circle of radius <code>D</code>. If <code>eps == -1</code> the returned time is
* the entry time; if <code>eps == 1</code> the returned time is the exit time.
*/
public static double Theta_D(Vect2 s, Vect2 v, int eps, double D) {
double a = v.sqv();
double b = s.dot(v);
double c = s.sqv()-Util.sq(D);
return Util.root2b(a,b,c,eps);
}
/**
* Discriminant of intersection between line and circle.
*
* @param v Vector
* @param D Diameter of of circle
*
* @return the discriminant of the intersection between the parametric line
* <code>s</code>+<i>t</i><code>v</code> and the circle of radius <code>D</code>.
* If the discriminant is less than <code>0</code> the intersection doesn't exist;
* if it is <code>0</code> the line is tangent to the circle; otherwise, the line
* intersects the circle in two different points.
*/
public static double Delta(Vect2 s, Vect2 v, double D) {
return Util.sq(D)*v.sqv() - Util.sq(s.det(v));
}
/** */
public static boolean almost_horizontal_los(Vect2 s, double D) {
double sqs = s.sqv();
double sqD = Util.sq(D);
return !Util.almost_equals(sqs,sqD) && sqs < sqD;
}
/** */
public static boolean horizontal_sep(Vect2 s, double D) {
return s.sqv() >= Util.sq(D);
}
/** */
public static boolean horizontal_dir(Vect2 s, Vect2 v, int dir) {
return dir*s.dot(v) >= 0;
}
/** */
public static boolean horizontal_dir_at(Vect2 s, Vect2 v,double t,int dir) {
Vect2 sp = v.ScalAdd(t,s);
return horizontal_dir(sp,v,dir);
}
static boolean horizontal_entry(Vect2 s,Vect2 v) {
return horizontal_dir(s,v,-1);
}
/** */
static Vect2 Vdir(Vect2 s,Vect2 v) {
Vect2 ps = s.PerpR();
return ps.Scal(Util.sign(ps.dot(v)));
}
/** */
static Vect2 W0(Vect2 s,double j) {
if (!s.isZero())
return s.Scal(j / s.sqv());
return Vect2.ZERO;
}
/** Solve the following equation on k and l:
* k*nv = l*vo-vi.
*/
public static Horizontal gs_only_line(Vect2 nv,Vect2 vo,Vect2 vi) {
double det_vo_v = vo.det(nv);
if (det_vo_v != 0) {
double l = Util.max(0,vi.det(nv) / det_vo_v);
double k = vi.det(vo) / det_vo_v;
Horizontal gso = new Horizontal(vo.Scal(l));
gso.k = k;
return gso;
}
return NoHorizontalSolution;
}
/** */
public static Horizontal gs_line(Vect2 nv,Vect2 vo,Vect2 vi) {
Horizontal gso = gs_only_line(nv,vo,vi);
if (gso.k < 0) {
return NoHorizontalSolution;
}
return gso;
}
/** */
public static Horizontal gs_only_dot(Vect2 u,Vect2 vo,Vect2 vi,double j) {
return gs_only_line(Vdir(u,vo.Sub(vi)),vo,vi.Add(W0(u,j)));
}
/** */
public static Horizontal gs_only_vertical(Vect2 s, Vect2 vo, Vect2 vi,
double th, int dir, double D) {
Vect2 v = vo.Sub(vi);
if (Delta(s,v,D) > 0) {
double td = Theta_D(s,v,dir,D);
if (td > 0) {
Vect2 p = v.ScalAdd(td,s);
return gs_only_dot(p.Scal(th),vo,vi,Util.sq(D)-s.dot(p));
}
}
return NoHorizontalSolution;
}
/** */
public static Horizontal gs_vertical(Vect3 s, Vect3 vo, Vect3 vi, TangentLine l, int epsv,
double D, double H) {
if (!Util.almost_equals(vo.z,vi.z)) {
Vect3 v = vo.Sub(vi);
int dir = Math.abs(s.z) >= H ? epsv*Util.sign(s.z) : Entry;
double t = Vertical.Theta_H(s.z,vo.z-vi.z,-dir,H);
if (t > 0 && epsv == Util.sign(s.z + t*v.z)) {
Horizontal nvo2 = gs_only_vertical(s.vect2(),vo.vect2(),vi.vect2(),t,dir,D);
if (almost_horizontal_los(s.vect2(),D) ||
l.horizontal_criterion(nvo2.Sub(vo.vect2())))
return nvo2;
}
}
return NoHorizontalSolution;
}
/** */
public static Horizontal gs_only(TangentLine nv, Vect3 s, Vect3 vo, Vect3 vi,int epsv,
double D, double H) {
return best_horizontal(vo.vect2(),gs_line(nv,vo.vect2(),vi.vect2()),
gs_vertical(s,vo,vi,nv,epsv,D,H));
}
/** */
public static Horizontal gs_only_circle(Vect2 s,Vect2 vo,Vect2 vi,
double t,int dir,int irt,double D) {
Vect2 w = s.Sub(vi.Scal(t));
double a = Util.sq(t)*vo.sqv();
double b = t*(w.dot(vo));
double c = w.sqv()-Util.sq(D);
double l = Util.root2b(a,b,c,irt);
if (!Double.isNaN(l)) {
Vect2 nvo = vo.Scal(Util.max(l,0));
if (horizontal_dir_at(s,nvo.Sub(vi),t,dir))
return new Horizontal(nvo);
}
return NoHorizontalSolution;
}
/** */
public static Horizontal gs_circle(Vect3 s,Vect3 vo,Vect3 vi,
int dir,int irt,double D,double H) {
if (!Util.almost_equals(vo.z,vi.z)) {
double t = Vertical.Theta_H(s.z,vo.z-vi.z,-dir,H);
return gs_only_circle(s.vect2(),vo.vect2(),vi.vect2(),t,dir,irt,D);
}
return NoHorizontalSolution;
}
/* Solve the following equation on k:
* || k*nv + vi || = || vo ||.
*/
/** Solve the following equation on k:
* || k*nv + vi || = || vo ||.
*/
public static Horizontal trk_only_line_irt(Vect2 nv,Vect2 vo,Vect2 vi,int irt) {
double a = nv.sqv();
double b = nv.dot(vi);
double c = vi.sqv() - vo.sqv();
double k = Util.root2b(a,b,c,irt);
if (!Double.isNaN(k)) {
return new Horizontal(k, nv.ScalAdd(k,vi));
}
return NoHorizontalSolution;
}
/** */
public static Horizontal trk_only_line(Vect2 nv,Vect2 vo,Vect2 vi) {
return best_horizontal(vo,trk_only_line_irt(nv,vo,vi,1),
trk_only_line_irt(nv,vo,vi,-1));
}
/** */
public static Horizontal trk_line_irt(Vect2 nv,Vect2 vo,Vect2 vi,int irt) {
Horizontal trko = trk_only_line_irt(nv,vo,vi,irt);
if (trko.k < 0) {
return NoHorizontalSolution;
}
return trko;
}
/** */
public static Horizontal trk_line(Vect2 nv,Vect2 vo,Vect2 vi) {
return best_horizontal(vo,trk_line_irt(nv,vo,vi,1),
trk_line_irt(nv,vo,vi,-1));
}
/** */
public static Horizontal trk_only_dot(Vect2 u,Vect2 vo,Vect2 vi,double j,int irt) {
return trk_only_line_irt(Vdir(u,vo.Sub(vi)),vo,vi.Add(W0(u,j)),irt);
}
/** */
public static Horizontal trk_only_vertical(Vect2 s, Vect2 vo, Vect2 vi,
double th, int dir, int irt, double D) {
Vect2 v = vo.Sub(vi);
if (Delta(s,v,D) > 0) {
double td = Theta_D(s,v,dir,D);
if (td > 0) {
Vect2 p = v.ScalAdd(td,s);
return trk_only_dot(p.Scal(th),vo,vi,Util.sq(D)-s.dot(p),irt);
}
}
return NoHorizontalSolution;
}
/** */
public static Horizontal trk_vertical_irt(Vect3 s, Vect3 vo, Vect3 vi, TangentLine l, int epsv,
int irt, double D, double H) {
if (!Util.almost_equals(vo.z,vi.z)) {
Vect3 v = vo.Sub(vi);
int dir = Math.abs(s.z) >= H ? epsv*Util.sign(s.z) : Entry;
double t = Vertical.Theta_H(s.z,vo.z-vi.z,-dir,H);
if (t > 0 && epsv == Util.sign(s.z + t*v.z)) {
Horizontal nvo2 = trk_only_vertical(s.vect2(),vo.vect2(),vi.vect2(),t,dir,irt,D);
if (almost_horizontal_los(s.vect2(),D) ||
l.horizontal_criterion(nvo2.Sub(vo.vect2())))
return nvo2;
}
}
return NoHorizontalSolution;
}
/** */
public static Horizontal trk_vertical(Vect3 s, Vect3 vo, Vect3 vi, TangentLine l, int epsv,
double D, double H) {
return best_horizontal(vo.vect2(),trk_vertical_irt(s,vo,vi,l,epsv,1,D,H),
trk_vertical_irt(s,vo,vi,l,epsv,-1,D,H));
}
/** */
public static Horizontal trk_only(TangentLine nv, Vect3 s, Vect3 vo, Vect3 vi,int epsv,
double D, double H) {
return best_horizontal(vo.vect2(),trk_line(nv,vo.vect2(),vi.vect2()),
trk_vertical(s,vo,vi,nv,epsv,D,H));
}
/** */
public static Horizontal trk_only_circle(Vect2 s,Vect2 vo,Vect2 vi,
double t,int dir,int irt,double D) {
if (t > 0) {
Vect2 w = s.AddScal(-t, vi); // s.Sub(vi.Scal(t));
double e = (Util.sq(D) - s.sqv() - Util.sq(t)*(vo.sqv()-vi.sqv())) / (2*t);
if (!s.almostEquals(vi.Scal(t))) {
Horizontal nvo = Horizontal.trk_only_dot(w,vo,vi,e,irt);
if (horizontal_dir_at(s,nvo.Sub(vi),t,dir))
return nvo;
}
}
return NoHorizontalSolution;
}
/** */
public static Horizontal trk_circle(Vect3 s,Vect3 vo,Vect3 vi,
int dir,int irt,double D,double H) {
if (!Util.almost_equals(vo.z, vi.z)) {
double t = Vertical.Theta_H(s.z,vo.z-vi.z,-dir,H);
return trk_only_circle(s.vect2(),vo.vect2(),vi.vect2(),t,dir,irt,D);
}
return NoHorizontalSolution;
}
/**
* Solve the following equation on k and l:
* nv * (k*nv-v) = 0, where v = vo-vi.
*/
public static Horizontal opt_trk_gs_line(Vect2 nv,Vect2 vo,Vect2 vi) {
if (!nv.isZero()) {
Vect2 v = vo.Sub(vi);
double k = nv.dot(v) / nv.sqv();
return new Horizontal(k, nv.ScalAdd(k,vi));
}
return NoHorizontalSolution;
}
/** */
public static Horizontal opt_line(Vect2 nv,Vect2 vo,Vect2 vi) {
Horizontal opt = opt_trk_gs_line(nv,vo,vi);
if (opt.k < 0) {
return NoHorizontalSolution;
}
return opt;
}
/** */
public static Horizontal opt_trk_gs(TangentLine nv, Vect3 s, Vect3 vo, Vect3 vi,int epsv,
double D, double H) {
return best_horizontal(vo.vect2(),opt_trk_gs_line(nv,vo.vect2(),vi.vect2()),
opt_vertical(s,vo,vi,nv,epsv,D,H));
}
/** */
public static Horizontal opt_trk_gs_dot(Vect2 u,Vect2 vo,Vect2 vi,double j) {
return opt_trk_gs_line(Vdir(u,vo.Sub(vi)),vo,vi.Add(W0(u,j)));
}
/** */
public static Horizontal opt_trk_gs_vertical(Vect2 s, Vect2 vo, Vect2 vi,
double th, int dir, double D) {
Vect2 v = vo.Sub(vi);
if (Delta(s,v,D) > 0) {
double td = Theta_D(s,v,dir,D);
if (td > 0) {
Vect2 p = v.ScalAdd(td,s);
return opt_trk_gs_dot(p.Scal(th),vo,vi,Util.sq(D)-s.dot(p));
}
}
return NoHorizontalSolution;
}
/** */
public static Horizontal opt_vertical(Vect3 s, Vect3 vo, Vect3 vi, TangentLine l, int epsv,
double D, double H) {
if (!Util.almost_equals(vo.z,vi.z)) {
Vect3 v = vo.Sub(vi);
int dir = Math.abs(s.z) >= H ? epsv*Util.sign(s.z) : Entry;
double t = Vertical.Theta_H(s.z,vo.z-vi.z,-dir,H);
if (t > 0 && epsv == Util.sign(s.z + t*v.z)) {
Horizontal nvo2 = opt_trk_gs_vertical(s.vect2(),vo.vect2(),vi.vect2(),t,dir,D);
if (almost_horizontal_los(s.vect2(),D) ||
l.horizontal_criterion(nvo2.Sub(vo.vect2())))
return nvo2;
}
}
return NoHorizontalSolution;
}
public static boolean hasEpsilonCriticalPointTrack(Vect2 s, Vect2 vo, Vect2 vi, int irt) {
Horizontal nvo = trk_only_dot(s.PerpR(), vo, vi, 0, irt);
return nvo != NoHorizontalSolution;
}
// if nvo has NoHorizontalSolution, the return is meaningless
public static double epsilonCriticalPointTrack(Vect2 s, Vect2 vo, Vect2 vi, int irt) {
Horizontal nvo = trk_only_dot(s.PerpR(), vo, vi, 0, irt);
if (nvo == NoHorizontalSolution) return -3*Math.PI;
return nvo.trk();
}
public static boolean epsilonCriticalPointGSIsValid(Vect2 s, Vect2 vo, Vect2 vi) {
Horizontal nvo = gs_only_dot(s.PerpR(), vo, vi, 0);
return nvo != NoHorizontalSolution;
}
// if nvo has NoHorizontalSolution, the return is meaningless
public static double epsilonCriticalPointGS(Vect2 s, Vect2 vo, Vect2 vi) {
Horizontal nvo = gs_only_dot(s.PerpR(), vo, vi, 0);
if (nvo == NoHorizontalSolution) return -1.0;
return nvo.norm();
}
/*
* Unit left perpendicular vector to v
*/
public static Vect3 unit_perpL(Vect3 v) {
return v.Hat2D().PerpL();
}
public String toString() {
if (undef())
return "Undef";
return super.toString();
}
}