This repository was archived by the owner on Oct 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgeom.c
More file actions
1511 lines (1365 loc) · 40.2 KB
/
geom.c
File metadata and controls
1511 lines (1365 loc) · 40.2 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license in the file COPYING
* or http://www.opensource.org/licenses/CDDL-1.0.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file COPYING.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2015 Saso Kiselkov. All rights reserved.
*/
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "math.h"
#include "helpers.h"
#include "wmm.h"
#include "geom.h"
#include "perf.h"
/*
* The WGS84 ellipsoid parameters.
*/
const ellip_t wgs84 = {
.a = 6378137.0,
.b = 6356752.314245,
.f = .00335281066474748071,
.ecc = 0.08181919084296430238,
.ecc2 = 0.00669437999019741354,
.r = 6371200.0
};
/*
* Naive implementation of matrix multiplication. We don't use this very
* heavily and can thus afford to rely on compiler auto-vectorization to
* get this optimized.
* Multiplies `x' and `y' and places result in 'z'. `xrows', `ycols' and `sz'
* have meanings as explained below:
* sz ycols ycols
* |<------->| |<------->| |<------->|
* | | | | | |
*
* --- ++= =++ .-++= =++ ++= =++ --
* xrows ^ || x00 x01 || \/ / || y00 y01 || --- || z00 z01 || ^ xrows
* v || x10 x11 || /\ / || y10 y11 || --- || z10 z11 || v
* --- ++= =++ / .-++= =++ ++= =++ --
* --' /
* sz ^ /
* v /
* --'
*/
static void
matrix_mul(const double *x, const double *y, double *z,
size_t xrows, size_t ycols, size_t sz)
{
memset(z, 0, sz * ycols * sizeof (double));
for (size_t row = 0; row < xrows; row++) {
for (size_t col = 0; col < ycols; col++) {
for (size_t i = 0; i < sz; i++) {
z[row * ycols + col] += x[row * sz + i] *
y[i * ycols + col];
}
}
}
}
/*
* Determines whether an angle is part of an arc.
*
* @param angle_x Angle who's membership of the arc to examine (in degrees).
* @param angle1 Start angle of the arc (in degrees).
* @param angle2 End angle of the arc (in degrees).
* @param cw Flag indicating whether the arc progresses clockwise or
* counter clockwise from angle1 to angle2.
*
* @return B_TRUE if angle_x is on the arc, B_FALSE if it is not.
*/
bool_t
is_on_arc(double angle_x, double angle1, double angle2, bool_t cw)
{
if (cw) {
if (angle1 < angle2)
return (angle_x >= angle1 && angle_x <= angle2);
else
return (angle_x >= angle1 || angle_x <= angle2);
} else {
if (angle1 < angle2)
return (angle_x <= angle1 || angle_x >= angle2);
else
return (angle_x <= angle1 && angle_x >= angle2);
}
}
/*
* Returns the absolute value (length) of a 3-space vector:
* r = |a|
*/
double
vect3_abs(vect3_t a)
{
return (sqrt(POW2(a.x) + POW2(a.y) + POW2(a.z)));
}
/*
* Same as vect3_abs, but for 2-space vectors.
*/
double
vect2_abs(vect2_t a)
{
return (sqrt(POW2(a.x) + POW2(a.y)));
}
/*
* Returns the distance between two points defined by vectors `a' and `b'.
*/
double
vect2_dist(vect2_t a, vect2_t b)
{
return (vect2_abs(vect2_sub(a, b)));
}
/*
* Sets the absolute value (length) of a vector without changing
* its orientation.
*/
vect3_t
vect3_set_abs(vect3_t a, double abs)
{
double oldval = vect3_abs(a);
if (oldval != 0.0)
return (vect3_scmul(a, abs / oldval));
else
return (ZERO_VECT3);
}
/*
* Same as vect3_set_abs, but for 2-space vectors.
*/
vect2_t
vect2_set_abs(vect2_t a, double abs)
{
double oldval = vect2_abs(a);
if (oldval != 0.0)
return (vect2_scmul(a, abs / oldval));
else
return (ZERO_VECT2);
}
/*
* Returns a unit vector (vector with identical orientation but a length of 1)
* for a given input vector. The length of the input vector is stored in `l'.
*/
vect3_t
vect3_unit(vect3_t a, double *l)
{
double len;
len = vect3_abs(a);
if (len == 0)
return (NULL_VECT3);
if (l)
*l = len;
return (VECT3(a.x / len, a.y / len, a.z / len));
}
/*
* Same as vect3_unit, but for 2-space vectors.
*/
vect2_t
vect2_unit(vect2_t a, double *l)
{
double len;
len = vect2_abs(a);
if (len == 0)
return (NULL_VECT2);
if (l)
*l = len;
return (VECT2(a.x / len, a.y / len));
}
/*
* Adds 3-space vectors `a' and `b' and returns the result:
* _ _ _
* r = a + b
*/
vect3_t
vect3_add(vect3_t a, vect3_t b)
{
return (VECT3(a.x + b.x, a.y + b.y, a.z + b.z));
}
/*
* Same as vect3_add, but for 2-space vectors.
*/
vect2_t
vect2_add(vect2_t a, vect2_t b)
{
return (VECT2(a.x + b.x, a.y + b.y));
}
/*
* Subtracts 3-space vector `b' from vector `a' and returns the result:
* _ _ _
* r = a - b
*/
vect3_t
vect3_sub(vect3_t a, vect3_t b)
{
return (VECT3(a.x - b.x, a.y - b.y, a.z - b.z));
}
/*
* Same as vect3_sub, but for 2-space vectors.
*/
vect2_t
vect2_sub(vect2_t a, vect2_t b)
{
return (VECT2(a.x - b.x, a.y - b.y));
}
/*
* Performs a scalar multiply of 3-space vector `a' and scalar value `b' and
* returns the result:
* _ _
* r = ab
*/
vect3_t
vect3_scmul(vect3_t a, double b)
{
return (VECT3(a.x * b, a.y * b, a.z * b));
}
/*
* Same as vect3_scmul, but for 2-space vectors.
*/
vect2_t
vect2_scmul(vect2_t a, double b)
{
return (VECT2(a.x * b, a.y * b));
}
/*
* Returns the dot product of 3-space vectors `a' and `b':
* _ _
* r = a . b
*/
double
vect3_dotprod(vect3_t a, vect3_t b)
{
return (a.x * b.x + a.y * b.y + a.z * b.z);
}
/*
* Same as vect3_dotprod, but for 2-space vectors.
*/
double
vect2_dotprod(vect2_t a, vect2_t b)
{
return (a.x * b.x + a.y * b.y);
}
/*
* Returns the cross product of 3-space vectors `a' and `b':
* _ _ _
* r = a x b
*/
vect3_t
vect3_xprod(vect3_t a, vect3_t b)
{
return (VECT3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x));
}
/*
* Returns the mean vector of 3-space vectors `a' and `b'. That is, the
* resulting vector will point exactly in between `a' and `b':
*
* ^.
* | .
* | .
* | x.
* a | / c .
* | / .
* +----------->
* b
*/
vect3_t
vect3_mean(vect3_t a, vect3_t b)
{
return (VECT3((a.x + b.x) / 2, (a.y + b.y) / 2, (a.z + b.z) / 2));
}
/*
* Rotates vector `v' by 90 degrees either to the right or left. This is
* faster than doing full trigonometric calculations in vect2_rot.
*/
vect2_t
vect2_norm(vect2_t v, bool_t right)
{
if (right)
return (VECT2(v.y, -v.x));
else
return (VECT2(-v.y, v.x));
}
/*
* Rotates vector `v' by `a' degrees to the right.
*/
vect2_t
vect2_rot(vect2_t v, double a)
{
double sin_a = sin(DEG2RAD(-a)), cos_a = cos(DEG2RAD(-a));
return (VECT2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a));
}
/*
* Negates vector `v' to point in the opposite direction.
*/
vect2_t
vect2_neg(vect2_t v)
{
return (VECT2(-v.x, -v.y));
}
/*
* Converts surface coordinates on an Earth-sized spheroid into 3-space
* coordinate vector in ECEF space. Please note that this considers the
* Earth to be a perfect sphere and hence cannot be used for very precise
* calculations. For more accurate conversions, use geo2ecef.
*
* @param pos The input position to convert.
*
* In 3-space, axes have their origins at the globe center point, are
* perpendicular to each other and are designated as follows:
* - x: positive & passing through lat=0, lon=0
* - y: positive & passing through lat=0, lon=+90
* - z: positive & passing through lat=90
*/
vect3_t
sph2ecef(geo_pos3_t pos)
{
vect3_t result;
double lat_rad, lon_rad, R, R0;
lat_rad = DEG2RAD(pos.lat);
lon_rad = DEG2RAD(pos.lon);
/* R is total distance from center at alt_msl */
R = pos.elev + EARTH_MSL;
/*
* R0 is the radius of a circular cut parallel to the equator at the
* given latitude of a sphere with radius R.
*/
R0 = R * cos(lat_rad);
/* Given R and R0, we can transform the geo coords into 3-space: */
result.x = R0 * cos(lon_rad);
result.y = R0 * sin(lon_rad);
result.z = R * sin(lat_rad);
return (result);
}
ellip_t
ellip_init(double semi_major, double semi_minor, double flattening)
{
ellip_t ellip;
ellip.a = semi_major;
ellip.b = semi_minor;
ellip.ecc2 = flattening * (2 - flattening);
return (ellip);
}
geo_pos3_t
geo2sph(geo_pos3_t pos, const ellip_t *ellip)
{
double lat_r = DEG2RAD(pos.lat);
double sin_lat = sin(lat_r);
double p, z;
double Rc; /* curvature of the prime vertical */
geo_pos3_t res;
Rc = ellip->a / sqrt(1 - ellip->ecc2 * POW2(sin_lat));
p = (Rc + pos.elev) * cos(lat_r);
z = ((Rc * (1 - ellip->ecc2)) + pos.elev) * sin_lat;
res.elev = sqrt(POW2(p) + POW2(z));
res.lat = RAD2DEG(asin(z / res.elev));
res.lon = pos.lon;
return (res);
}
vect3_t
geo2ecef(geo_pos3_t pos, const ellip_t *ellip)
{
double h = pos.elev / 3.281; /* convert to meters */
double lat_r = DEG2RAD(pos.lat);
double lon_r = DEG2RAD(pos.lon);
double Rc; /* curvature of the prime vertical */
vect3_t res;
double sin_lat = sin(lat_r), cos_lat = cos(lat_r);
double sin_lon = sin(lon_r), cos_lon = cos(lon_r);
Rc = ellip->a / sqrt(1 - ellip->ecc2 * POW2(sin_lat));
res.x = (Rc + h) * cos_lat * cos_lon;
res.y = (Rc + h) * cos_lat * sin_lon;
res.z = (Rc * (1 - ellip->ecc2) + h) * sin_lat;
return (res);
}
geo_pos3_t
ecef2geo(vect3_t pos, const ellip_t *ellip)
{
geo_pos3_t res;
double B;
double d;
double e;
double f;
double g;
double p;
double q;
double r;
double t;
double v;
double zlong;
/*
* 1.0 compute semi-minor axis and set sign to that of z in order
* to get sign of Phi correct.
*/
B = (pos.z >= 0 ? ellip->b : -ellip->b);
/*
* 2.0 compute intermediate values for latitude
*/
r = sqrt(POW2(pos.x) + POW2(pos.y));
e = (B * pos.z - (POW2(ellip->a) - POW2(B))) / (ellip->a * r);
f = (B * pos.z + (POW2(ellip->a) - POW2(B))) / (ellip->a * r);
/*
* 3.0 find solution to:
* t^4 + 2*E*t^3 + 2*F*t - 1 = 0
*/
p = (4.0 / 3.0) * (e * f + 1.0);
q = 2.0 * (POW2(e) - POW2(f));
d = POW3(p) + POW2(q);
if (d >= 0.0) {
v = pow((sqrt(d) - q), (1.0 / 3.0)) - pow((sqrt(d) + q),
(1.0 / 3.0));
} else {
v = 2.0 * sqrt(-p) * cos(acos(q / (p * sqrt(-p))) / 3.0);
}
/*
* 4.0 improve v
* NOTE: not really necessary unless point is near pole
*/
if (POW2(v) < fabs(p))
v = -(POW3(v) + 2.0 * q) / (3.0 * p);
g = (sqrt(POW2(e) + v) + e) / 2.0;
t = sqrt(POW2(g) + (f - v * g) / (2.0 * g - e)) - g;
res.lat = atan((ellip->a * (1.0 - POW2(t))) / (2.0 * B * t));
/*
* 5.0 compute height above ellipsoid
*/
res.elev= (r - ellip->a * t) * cos(res.lat) + (pos.z - B) * sin(res.lat);
/*
* 6.0 compute longitude east of Greenwich
*/
zlong = atan2(pos.y, pos.x);
if (zlong < 0.0)
zlong = zlong + (2 * M_PI);
res.lon = zlong;
/*
* 7.0 convert latitude and longitude to degrees & elev to feet
*/
res.lat = RAD2DEG(res.lat);
res.lon = RAD2DEG(res.lon);
res.elev = MET2FEET(res.elev);
if (res.lon >= 180.0)
res.lon -= 360.0;
return (res);
}
/*
* Converts a 3-space coordinate vector from ECEF coordinate space into
* geocentric coordinates on an EARTH_MSL-radius spheroid.
*/
geo_pos3_t
ecef2sph(vect3_t v)
{
geo_pos3_t pos;
double lat_rad, lon_rad, R, R0;
R0 = sqrt(v.x * v.x + v.y * v.y);
R = vect3_abs(v);
if (R0 == 0) {
/* to prevent a div-by-zero at the poles */
R0 = 0.000000001;
}
lat_rad = atan(v.z / R0);
lon_rad = asin(v.y / R0);
if (v.x < 0.0) {
if (v.y >= 0.0)
lon_rad = M_PI - lon_rad;
else
lon_rad = -M_PI - lon_rad;
}
pos.elev = R - EARTH_MSL;
pos.lat = RAD2DEG(lat_rad);
pos.lon = RAD2DEG(lon_rad);
return (pos);
}
/*
* Determines whether and where a vector intersects the surface of a sphere.
* Returns the number of intersection points (zero, one or two).
*
* @param v Vector for which to determine the intersection.
* @param o Vector pointing from the coordinate origin to the origin
* of vector v (i.e. displacement of `v' from the origin).
* @param c Displacement of sphere center point from the coordinate origin.
* @param r Radius of sphere.
* @param confined If B_TRUE, only intersects which lie between the vector's
* start & ending point (inclusive) are returned. Otherwise any
* intersect along an infinite linear extension of the vector is returned.
* @param i If not NULL, this function stores vectors pointing to the
* intersection points from the coordinate origin in this array:
* - if 0 is returned, two null vectors are stored in the array.
* - if 1 is returned, one null vector and one non-null vector pointing
* to the intersection point are stored in the array (ordering in the
* array is not guarantted as described here).
* - if 2 is returned, two non-null vectors pointing to the
* intersection points are stored in the array.
*/
unsigned
vect2sph_isect(vect3_t v, vect3_t o, vect3_t c, double r, bool_t confined,
vect3_t i[2])
{
vect3_t l, o_min_c;
double d, l_dot_o_min_c, sqrt_tmp, o_min_c_abs;
/* convert v into a unit vector 'l' and scalar distance 'd' */
l = vect3_unit(v, &d);
/* compute (o - c) and the dot product of l.(o - c) */
o_min_c = vect3_sub(o, c);
l_dot_o_min_c = vect3_dotprod(l, o_min_c);
/*
* The full formula for the distance along L for the intersects is:
* -(l.(o - c)) +- sqrt((l.(o - c))^2 - abs(o - c)^2 + r^2)
* The part in the sqrt may be negative, zero or positive, indicating
* respectively no intersection, one touching point or two points, so
* before we start sqrt()ing away, we check which it is. Also, this
* checks for a solution on an infinite line between extending along
* v. Before we declare victory, we check that the computed
* points lie on the vector.
*/
o_min_c_abs = vect3_abs(o_min_c);
sqrt_tmp = POW2(l_dot_o_min_c) - POW2(o_min_c_abs) + POW2(r);
if (sqrt_tmp > 0) {
/* Two solutions */
double i1_d, i2_d;
unsigned intersects = 0;
sqrt_tmp = sqrt(sqrt_tmp);
i1_d = -l_dot_o_min_c - sqrt_tmp;
if ((i1_d >= 0 && i1_d <= d) || !confined) {
/*
* Solution lies on vector, store a vector to it
* if the caller requested it.
*/
if (i != NULL)
i[intersects] = vect3_add(vect3_scmul(l, i1_d),
o);
intersects++;
} else {
/* Solution lies outside of line between o1 & o2 */
i1_d = NAN;
if (i != NULL)
i[intersects] = NULL_VECT3;
}
/* ditto for the second intersect */
i2_d = -l_dot_o_min_c + sqrt_tmp;
if ((i2_d >= 0 && i2_d <= d) || !confined) {
if (i != NULL)
i[intersects] = vect3_add(vect3_scmul(l, i2_d),
o);
intersects++;
} else {
i2_d = NAN;
if (i != NULL)
i[intersects] = NULL_VECT3;
}
return (intersects);
} else if (sqrt_tmp == 0) {
/* One solution */
double i1_d;
if (i != NULL)
i[1] = NULL_VECT3;
i1_d = -l_dot_o_min_c;
if ((i1_d >= 0 && i1_d <= d) || !confined) {
if (i != NULL)
i[0] = vect3_add(vect3_scmul(l, i1_d), o);
return (1);
} else {
if (i != NULL)
i[0] = NULL_VECT3;
return (0);
}
} else {
/* No solution, no intersections, NaN i1 & i2 */
if (i != NULL) {
i[0] = NULL_VECT3;
i[1] = NULL_VECT3;
}
return (0);
}
}
/*
* Determines whether and where a 2D vector intersects a 2D circle. The
* meanings of the arguments and return value are exactly the same as in
* vect2sph_isect.
*/
unsigned
vect2circ_isect(vect2_t v, vect2_t o, vect2_t c, double r, bool_t confined,
vect2_t i[2])
{
/*
* This is basically a simplified case of a vect2sph intersection,
* where both the vector and sphere's center lie on the xy plane.
* So just convert to 3D coordinates with z=0 and run vect2sph_isect.
* This only adds one extra coordinate to the calculation, which is
* generally negligible on performance.
*/
vect3_t v3 = VECT3(v.x, v.y, 0), o3 = VECT3(o.x, o.y, 0);
vect3_t c3 = VECT3(c.x, c.y, 0);
vect3_t i3[2];
int n;
n = vect2sph_isect(v3, o3, c3, r, confined, i3);
if (i != NULL) {
i[0] = VECT2(i3[0].x, i3[0].y);
i[1] = VECT2(i3[1].x, i3[1].y);
}
return (n);
}
/*
* Calculates a 2D vector/vector intersection point and returns it.
*
* @param a First vector.
* @param oa Vector to origin of first vector from the coordinate origin.
* @param b Second vector.
* @param oa Vector to origin of second vector from the coordinate origin.
* @param confined If B_TRUE, only intersects which lie between the vectors'
* start & ending points (inclusive) are considered. Otherwise any
* intersect along an infinite linear extension of the vectors is returned.
*
* @return A vector from the coordinate origin to the intersection point
* or NULL_VECT2 if the vectors are parallel (no intersection or inf
* many intersections if they're directly on top of each other).
*/
vect2_t
vect2vect_isect(vect2_t a, vect2_t oa, vect2_t b, vect2_t ob, bool_t confined)
{
vect2_t p1, p2, p3, p4, r;
double ca, cb, det;
if (VECT2_PARALLEL(a, b))
return (NULL_VECT2);
if (VECT2_EQ(oa, ob))
return (oa);
p1 = oa;
p2 = vect2_add(oa, a);
p3 = ob;
p4 = vect2_add(ob, b);
det = (p1.x - p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x - p4.x);
ASSERT(det != 0.0);
ca = p1.x * p2.y - p1.y * p2.x;
cb = p3.x * p4.y - p3.y * p4.x;
r.x = (ca * (p3.x - p4.x) - cb * (p1.x - p2.x)) / det;
r.y = (ca * (p3.y - p4.y) - cb * (p1.y - p2.y)) / det;
if (confined) {
if (r.x < MIN(p1.x, p2.x) - ROUND_ERROR ||
r.x > MAX(p1.x, p2.x) + ROUND_ERROR ||
r.x < MIN(p3.x, p4.x) - ROUND_ERROR ||
r.x > MAX(p3.x, p4.x) + ROUND_ERROR ||
r.y < MIN(p1.y, p2.y) - ROUND_ERROR ||
r.y > MAX(p1.y, p2.y) + ROUND_ERROR ||
r.y < MIN(p3.y, p4.y) - ROUND_ERROR ||
r.y > MAX(p3.y, p4.y) + ROUND_ERROR)
return (NULL_VECT2);
}
return (r);
}
unsigned
circ2circ_isect(vect2_t ca, double ra, vect2_t cb, double rb, vect2_t i[2])
{
double a, d, h;
vect2_t ca_cb, p2, ca_p2;
ca_cb = vect2_sub(cb, ca);
d = vect2_abs(ca_cb);
if ((d == 0 && ra == rb) || d > ra + rb ||
d + MIN(ra, rb) < MAX(ra, rb))
return (0);
a = (POW2(ra) - POW2(rb) + POW2(d)) / (2 * d);
if (POW2(ra) - POW2(a) < 0)
h = 0;
else
h = sqrt(POW2(ra) - POW2(a));
ca_p2 = vect2_set_abs(ca_cb, a);
p2 = vect2_add(ca, ca_p2);
if (h == 0) {
i[0] = p2;
ASSERT(!IS_NULL_VECT(i[0]));
return (1);
} else {
i[0] = vect2_add(p2, vect2_set_abs(vect2_norm(ca_p2, B_FALSE),
h));
i[1] = vect2_add(p2, vect2_set_abs(vect2_norm(ca_p2, B_TRUE),
h));
ASSERT(!IS_NULL_VECT(i[0]) && !IS_NULL_VECT(i[1]));
return (2);
}
}
/*
* Given a true heading in degrees, constructs a unit vector pointing in that
* direction. 0 degress is parallel with y axis and hdg increases clockwise.
*/
vect2_t
hdg2dir(double truehdg)
{
truehdg = DEG2RAD(truehdg);
return (VECT2(sin(truehdg), cos(truehdg)));
}
/*
* Given a direction vector, returns the true heading that the vector
* is pointing. See hdg2dir for a description of the returned heading value.
*/
double
dir2hdg(vect2_t dir)
{
if (dir.x >= 0 && dir.y >= 0)
return (RAD2DEG(asin(dir.x / vect2_abs(dir))));
if (dir.x < 0 && dir.y >= 0)
return (360 + RAD2DEG(asin(dir.x / vect2_abs(dir))));
if (dir.x >= 0 && dir.y < 0)
return (180 - RAD2DEG(asin(dir.x / vect2_abs(dir))));
return (180 - RAD2DEG(asin(dir.x / vect2_abs(dir))));
}
/*
* Displaces a given geodetic position
*/
geo_pos2_t
geo_displace_mag(const ellip_t *ellip, const wmm_t *wmm, geo_pos2_t pos,
double maghdg, double dist)
{
return (geo_displace(ellip, pos, wmm_mag2true(wmm, maghdg,
GEO2_TO_GEO3(pos, 0)), dist));
}
geo_pos2_t
geo_displace(const ellip_t *ellip, geo_pos2_t pos, double truehdg, double dist)
{
return (geo_displace_dir(ellip, pos, hdg2dir(truehdg), dist));
}
geo_pos2_t
geo_displace_dir(const ellip_t *ellip, geo_pos2_t pos, vect2_t dir, double dist)
{
double dist_r = dist / EARTH_MSL;
fpp_t fpp;
if (dist >= M_PI * EARTH_MSL / 2)
return (NULL_GEO_POS2);
fpp = gnomo_fpp_init(pos, 0, ellip, B_TRUE);
dir = vect2_set_abs(dir, tan(dist_r));
return (fpp2geo(dir, &fpp));
}
geo_pos2_t
geo_mag_radial_isect(const ellip_t *ellip, const wmm_t *wmm, geo_pos2_t pos1,
double rad1, geo_pos2_t pos2, double rad2)
{
geo_pos3_t pos1_3d = GEO2_TO_GEO3(pos1, 0);
geo_pos3_t pos2_3d = GEO2_TO_GEO3(pos2, 0);
vect3_t pos1_v = geo2ecef(pos1_3d, ellip);
vect3_t pos2_v = geo2ecef(pos2_3d, ellip);
vect3_t pos_mean = vect3_mean(pos1_v, pos2_v);
geo_pos3_t fpp_pos = ecef2geo(pos_mean, ellip);
fpp_t fpp;
vect2_t pos1_fpp_v, rad1_dir, pos2_fpp_v, rad2_dir, isect;
fpp = gnomo_fpp_init(GEO3_TO_GEO2(fpp_pos), 0, ellip, B_TRUE);
pos1_fpp_v = geo2fpp(pos1, &fpp);
rad1_dir = hdg2dir(wmm_mag2true(wmm, rad1, pos1_3d));
pos2_fpp_v = geo2fpp(pos2, &fpp);
rad2_dir = hdg2dir(wmm_mag2true(wmm, rad2, pos2_3d));
isect = vect2vect_isect(rad1_dir, pos1_fpp_v, rad2_dir, pos2_fpp_v,
B_FALSE);
if (IS_NULL_VECT(isect))
return (NULL_GEO_POS2);
return (fpp2geo(isect, &fpp));
}
/*
* Computes the number of latitudinal subdivisions used for tiling a spherical
* surface. See world.c for a description of this tiling.
*/
unsigned
sphere_lat_subdiv(double radius, double partition_sz)
{
ASSERT(radius >= partition_sz);
return (ceil((radius * M_PI) / partition_sz) + 1);
}
/*
* Computes the number of longitudinal subdivisions for a given latitude (given
* in degrees, with 0 being the equator) used for tiling a spherical
* surface. See world.c for a description of this tiling.
*/
unsigned
sphere_lon_subdiv(double radius, double lat, double partition_sz)
{
ASSERT(lat >= -90.0 && lat <= 90.0);
ASSERT(radius >= partition_sz);
double r = cos(DEG2RAD(lat)) * radius;
return (ceil((2 * M_PI * r) / partition_sz));
}
bool_t
geo_pos2_from_str(const char *lat, const char *lon, geo_pos2_t *pos)
{
pos->lat = atof(lat);
pos->lon = atof(lon);
return (is_valid_lat(pos->lat) && is_valid_lon(pos->lon));
}
bool_t
geo_pos3_from_str(const char *lat, const char *lon, const char *elev,
geo_pos3_t *pos)
{
pos->lat = atof(lat);
pos->lon = atof(lon);
pos->elev = atof(elev);
return (is_valid_lat(pos->lat) && is_valid_lon(pos->lon) &&
is_valid_elev(pos->elev));
}
/* Cotangent */
static inline double
cot(double x)
{
return (1.0 / tan(x));
}
/* Secant */
static inline double
sec(double x)
{
return (1.0 / cos(x));
}
/*
* Prepares a set of geographical coordinate translation parameters.
*
* @param displac The relative latitude & longitude (in degrees)
* between the origins of the two respective coordinate systems.
* For example, a displacement of +10 degrees of latitude (north)
* and +20 degrees of longitude (east) will result in an input
* coordinate of +5,+5 translating into -5,-15 in the target system
* (assuming `rotation' below is zero).
* Please note that these coordinates as well as all transformations
* are assumed to be in geocentric coordinates on the an EARTH_MSL
* radius spheroid.
* @param rot The relative rotation of the axes of the target
* coordinate system to the source coordinate system in degrees
* counter-clockwise. For example, a rotation of +90 degrees and
* no translation applied to an input coordinate of +5 degrees
* of latitude (north) and +5 degrees of longitude (east) will
* translate into -5,+5.
*/
sph_xlate_t
sph_xlate_init(geo_pos2_t displac, double rot, bool_t inv)
{
/*
* (ECEF axes:)
* lat xlate y axis alpha
* lon xlate z axis bravo
* viewport rotation x axis theta
*/
sph_xlate_t xlate;
double alpha = DEG2RAD(!inv ? displac.lat : -displac.lat);
double bravo = DEG2RAD(!inv ? -displac.lon : displac.lon);
double theta = DEG2RAD(!inv ? rot : -rot);
#define M(m, r, c) ((m)[(r) * 3 + (c)])
double R_a[3 * 3], R_b[3 * 3];
double sin_alpha = sin(alpha), cos_alpha = cos(alpha);
double sin_bravo = sin(bravo), cos_bravo = cos(bravo);
double sin_theta = sin(theta), cos_theta = cos(theta);
/*
* +- -+
* | cos(a) 0 sin(a) |
* | 0 1 0 |
* | -sin(a) 0 cos(a) |
* +- -+
*/
memset(R_a, 0, sizeof (R_a));
M(R_a, 0, 0) = cos_alpha;
M(R_a, 0, 2) = sin_alpha;
M(R_a, 1, 1) = 1;
M(R_a, 2, 0) = -sin_alpha;
M(R_a, 2, 2) = cos_alpha;
/*
* +- -+
* | cos(g) -sin(g) 0 |
* | sin(g) cos(g) 0 |
* | 0 0 1 |
* +- -+
*/
memset(R_b, 0, sizeof (R_b));
M(R_b, 0, 0) = cos_bravo;
M(R_b, 0, 1) = -sin_bravo;
M(R_b, 1, 0) = sin_bravo;
M(R_b, 1, 1) = cos_bravo;
M(R_b, 2, 2) = 1;
if (!inv)
matrix_mul(R_a, R_b, xlate.sph_matrix, 3, 3, 3);
else
matrix_mul(R_b, R_a, xlate.sph_matrix, 3, 3, 3);
xlate.rot_matrix[0] = cos_theta;
xlate.rot_matrix[1] = -sin_theta;
xlate.rot_matrix[2] = sin_theta;
xlate.rot_matrix[3] = cos_theta;
xlate.inv = inv;
return (xlate);
}
/*
* Translates a point at `pos' using the translation specified by `xlate'.
*/
vect3_t
sph_xlate_vect(vect3_t p, const sph_xlate_t *xlate)