/
lwgeodetic.c
3293 lines (2830 loc) · 79.8 KB
/
lwgeodetic.c
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
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca>
* Copyright 2009 David Skea <David.Skea@gov.bc.ca>
*
**********************************************************************/
#include "liblwgeom_internal.h"
#include "lwgeodetic.h"
#include "lwgeom_log.h"
/**
* For testing geodetic bounding box, we have a magic global variable.
* When this is true (when the cunit tests set it), use the slow, but
* guaranteed correct, algorithm. Otherwise use the regular one.
*/
int gbox_geocentric_slow = LW_FALSE;
/**
* Convert a longitude to the range of -PI,PI
*/
double longitude_radians_normalize(double lon)
{
if ( lon == -1.0 * M_PI )
return M_PI;
if ( lon == -2.0 * M_PI )
return 0.0;
if ( lon > 2.0 * M_PI )
lon = remainder(lon, 2.0 * M_PI);
if ( lon < -2.0 * M_PI )
lon = remainder(lon, -2.0 * M_PI);
if ( lon > M_PI )
lon = -2.0 * M_PI + lon;
if ( lon < -1.0 * M_PI )
lon = 2.0 * M_PI + lon;
if ( lon == -2.0 * M_PI )
lon *= -1.0;
return lon;
}
/**
* Convert a latitude to the range of -PI/2,PI/2
*/
double latitude_radians_normalize(double lat)
{
if ( lat > 2.0 * M_PI )
lat = remainder(lat, 2.0 * M_PI);
if ( lat < -2.0 * M_PI )
lat = remainder(lat, -2.0 * M_PI);
if ( lat > M_PI )
lat = M_PI - lat;
if ( lat < -1.0 * M_PI )
lat = -1.0 * M_PI - lat;
if ( lat > M_PI_2 )
lat = M_PI - lat;
if ( lat < -1.0 * M_PI_2 )
lat = -1.0 * M_PI - lat;
return lat;
}
/**
* Convert a longitude to the range of -180,180
* @param lon longitude in degrees
*/
double longitude_degrees_normalize(double lon)
{
if ( lon > 360.0 )
lon = remainder(lon, 360.0);
if ( lon < -360.0 )
lon = remainder(lon, -360.0);
if ( lon > 180.0 )
lon = -360.0 + lon;
if ( lon < -180.0 )
lon = 360 + lon;
if ( lon == -180.0 )
return 180.0;
if ( lon == -360.0 )
return 0.0;
return lon;
}
/**
* Convert a latitude to the range of -90,90
* @param lat latitude in degrees
*/
double latitude_degrees_normalize(double lat)
{
if ( lat > 360.0 )
lat = remainder(lat, 360.0);
if ( lat < -360.0 )
lat = remainder(lat, -360.0);
if ( lat > 180.0 )
lat = 180.0 - lat;
if ( lat < -180.0 )
lat = -180.0 - lat;
if ( lat > 90.0 )
lat = 180.0 - lat;
if ( lat < -90.0 )
lat = -180.0 - lat;
return lat;
}
/**
* Shift a point around by a number of radians
*/
void point_shift(GEOGRAPHIC_POINT *p, double shift)
{
double lon = p->lon + shift;
if ( lon > M_PI )
p->lon = -1.0 * M_PI + (lon - M_PI);
else
p->lon = lon;
return;
}
int geographic_point_equals(const GEOGRAPHIC_POINT *g1, const GEOGRAPHIC_POINT *g2)
{
return FP_EQUALS(g1->lat, g2->lat) && FP_EQUALS(g1->lon, g2->lon);
}
/**
* Initialize a geographic point
* @param lon longitude in degrees
* @param lat latitude in degrees
*/
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
{
g->lat = latitude_radians_normalize(deg2rad(lat));
g->lon = longitude_radians_normalize(deg2rad(lon));
}
/** Returns the angular height (latitudinal span) of the box in radians */
double
gbox_angular_height(const GBOX* gbox)
{
double d[6];
int i;
double zmin = FLT_MAX;
double zmax = -1 * FLT_MAX;
POINT3D pt;
/* Take a copy of the box corners so we can treat them as a list */
/* Elements are xmin, xmax, ymin, ymax, zmin, zmax */
memcpy(d, &(gbox->xmin), 6*sizeof(double));
/* Generate all 8 corner vectors of the box */
for ( i = 0; i < 8; i++ )
{
pt.x = d[i / 4];
pt.y = d[2 + (i % 4) / 2];
pt.z = d[4 + (i % 2)];
normalize(&pt);
if ( pt.z < zmin ) zmin = pt.z;
if ( pt.z > zmax ) zmax = pt.z;
}
return asin(zmax) - asin(zmin);
}
/** Returns the angular width (longitudinal span) of the box in radians */
double
gbox_angular_width(const GBOX* gbox)
{
double d[6];
int i, j;
POINT3D pt[3];
double maxangle;
double magnitude;
/* Take a copy of the box corners so we can treat them as a list */
/* Elements are xmin, xmax, ymin, ymax, zmin, zmax */
memcpy(d, &(gbox->xmin), 6*sizeof(double));
/* Start with the bottom corner */
pt[0].x = gbox->xmin;
pt[0].y = gbox->ymin;
magnitude = sqrt(pt[0].x*pt[0].x + pt[0].y*pt[0].y);
pt[0].x /= magnitude;
pt[0].y /= magnitude;
/* Generate all 8 corner vectors of the box */
/* Find the vector furthest from our seed vector */
for ( j = 0; j < 2; j++ )
{
maxangle = -1 * FLT_MAX;
for ( i = 0; i < 4; i++ )
{
double angle, dotprod;
POINT3D pt_n;
pt_n.x = d[i / 2];
pt_n.y = d[2 + (i % 2)];
magnitude = sqrt(pt_n.x*pt_n.x + pt_n.y*pt_n.y);
pt_n.x /= magnitude;
pt_n.y /= magnitude;
pt_n.z = 0.0;
dotprod = pt_n.x*pt[j].x + pt_n.y*pt[j].y;
angle = acos(dotprod > 1.0 ? 1.0 : dotprod);
if ( angle > maxangle )
{
pt[j+1] = pt_n;
maxangle = angle;
}
}
}
/* Return the distance between the two furthest vectors */
return maxangle;
}
/** Computes the average(ish) center of the box and returns success. */
int
gbox_centroid(const GBOX* gbox, POINT2D* out)
{
double d[6];
GEOGRAPHIC_POINT g;
POINT3D pt;
int i;
/* Take a copy of the box corners so we can treat them as a list */
/* Elements are xmin, xmax, ymin, ymax, zmin, zmax */
memcpy(d, &(gbox->xmin), 6*sizeof(double));
/* Zero out our return vector */
pt.x = pt.y = pt.z = 0.0;
for ( i = 0; i < 8; i++ )
{
POINT3D pt_n;
pt_n.x = d[i / 4];
pt_n.y = d[2 + ((i % 4) / 2)];
pt_n.z = d[4 + (i % 2)];
normalize(&pt_n);
pt.x += pt_n.x;
pt.y += pt_n.y;
pt.z += pt_n.z;
}
pt.x /= 8.0;
pt.y /= 8.0;
pt.z /= 8.0;
normalize(&pt);
cart2geog(&pt, &g);
out->x = longitude_degrees_normalize(rad2deg(g.lon));
out->y = latitude_degrees_normalize(rad2deg(g.lat));
return LW_SUCCESS;
}
/**
* Check to see if this geocentric gbox is wrapped around a pole.
* Only makes sense if this gbox originated from a polygon, as it's assuming
* the box is generated from external edges and there's an "interior" which
* contains the pole.
*
* This function is overdetermined, for very large polygons it might add an
* unwarranted pole. STILL NEEDS WORK!
*/
static int gbox_check_poles(GBOX *gbox)
{
int rv = LW_FALSE;
LWDEBUG(4, "checking poles");
LWDEBUGF(4, "gbox %s", gbox_to_string(gbox));
/* Z axis */
if ( gbox->xmin < 0.0 && gbox->xmax > 0.0 &&
gbox->ymin < 0.0 && gbox->ymax > 0.0 )
{
if ( (gbox->zmin + gbox->zmax) > 0.0 )
{
LWDEBUG(4, "enclosed positive z axis");
gbox->zmax = 1.0;
}
else
{
LWDEBUG(4, "enclosed negative z axis");
gbox->zmin = -1.0;
}
rv = LW_TRUE;
}
/* Y axis */
if ( gbox->xmin < 0.0 && gbox->xmax > 0.0 &&
gbox->zmin < 0.0 && gbox->zmax > 0.0 )
{
if ( gbox->ymin + gbox->ymax > 0.0 )
{
LWDEBUG(4, "enclosed positive y axis");
gbox->ymax = 1.0;
}
else
{
LWDEBUG(4, "enclosed negative y axis");
gbox->ymin = -1.0;
}
rv = LW_TRUE;
}
/* X axis */
if ( gbox->ymin < 0.0 && gbox->ymax > 0.0 &&
gbox->zmin < 0.0 && gbox->zmax > 0.0 )
{
if ( gbox->xmin + gbox->xmax > 0.0 )
{
LWDEBUG(4, "enclosed positive x axis");
gbox->xmax = 1.0;
}
else
{
LWDEBUG(4, "enclosed negative x axis");
gbox->xmin = -1.0;
}
rv = LW_TRUE;
}
return rv;
}
/**
* Convert spherical coordinates to cartesion coordinates on unit sphere
*/
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
{
p->x = cos(g->lat) * cos(g->lon);
p->y = cos(g->lat) * sin(g->lon);
p->z = sin(g->lat);
}
/**
* Convert cartesion coordinates on unit sphere to spherical coordinates
*/
void cart2geog(const POINT3D *p, GEOGRAPHIC_POINT *g)
{
g->lon = atan2(p->y, p->x);
g->lat = asin(p->z);
}
/**
* Convert lon/lat coordinates to cartesion coordinates on unit sphere
*/
void ll2cart(const POINT2D *g, POINT3D *p)
{
double x_rad = M_PI * g->x / 180.0;
double y_rad = M_PI * g->y / 180.0;
double cos_y_rad = cos(y_rad);
p->x = cos_y_rad * cos(x_rad);
p->y = cos_y_rad * sin(x_rad);
p->z = sin(y_rad);
}
/**
* Convert cartesion coordinates on unit sphere to lon/lat coordinates
static void cart2ll(const POINT3D *p, POINT2D *g)
{
g->x = longitude_degrees_normalize(180.0 * atan2(p->y, p->x) / M_PI);
g->y = latitude_degrees_normalize(180.0 * asin(p->z) / M_PI);
}
*/
/**
* Calculate the dot product of two unit vectors
* (-1 == opposite, 0 == orthogonal, 1 == identical)
*/
static double dot_product(const POINT3D *p1, const POINT3D *p2)
{
return (p1->x*p2->x) + (p1->y*p2->y) + (p1->z*p2->z);
}
/**
* Calculate the cross product of two vectors
*/
static void cross_product(const POINT3D *a, const POINT3D *b, POINT3D *n)
{
n->x = a->y * b->z - a->z * b->y;
n->y = a->z * b->x - a->x * b->z;
n->z = a->x * b->y - a->y * b->x;
return;
}
/**
* Calculate the sum of two vectors
*/
void vector_sum(const POINT3D *a, const POINT3D *b, POINT3D *n)
{
n->x = a->x + b->x;
n->y = a->y + b->y;
n->z = a->z + b->z;
return;
}
/**
* Calculate the difference of two vectors
*/
static void vector_difference(const POINT3D *a, const POINT3D *b, POINT3D *n)
{
n->x = a->x - b->x;
n->y = a->y - b->y;
n->z = a->z - b->z;
return;
}
/**
* Scale a vector out by a factor
*/
static void vector_scale(POINT3D *n, double scale)
{
n->x *= scale;
n->y *= scale;
n->z *= scale;
return;
}
/*
* static inline double vector_magnitude(const POINT3D* v)
* {
* return sqrt(v->x*v->x + v->y*v->y + v->z*v->z);
* }
*/
/**
* Angle between two unit vectors
*/
double vector_angle(const POINT3D* v1, const POINT3D* v2)
{
POINT3D v3, normal;
double angle, x, y;
cross_product(v1, v2, &normal);
normalize(&normal);
cross_product(&normal, v1, &v3);
x = dot_product(v1, v2);
y = dot_product(v2, &v3);
angle = atan2(y, x);
return angle;
}
/**
* Normalize to a unit vector.
*/
static void normalize2d(POINT2D *p)
{
double d = sqrt(p->x*p->x + p->y*p->y);
if (FP_IS_ZERO(d))
{
p->x = p->y = 0.0;
return;
}
p->x = p->x / d;
p->y = p->y / d;
return;
}
/**
* Calculates the unit normal to two vectors, trying to avoid
* problems with over-narrow or over-wide cases.
*/
void unit_normal(const POINT3D *P1, const POINT3D *P2, POINT3D *normal)
{
double p_dot = dot_product(P1, P2);
POINT3D P3;
/* If edge is really large, calculate a narrower equivalent angle A1/A3. */
if ( p_dot < 0 )
{
vector_sum(P1, P2, &P3);
normalize(&P3);
}
/* If edge is narrow, calculate a wider equivalent angle A1/A3. */
else if ( p_dot > 0.95 )
{
vector_difference(P2, P1, &P3);
normalize(&P3);
}
/* Just keep the current angle in A1/A3. */
else
{
P3 = *P2;
}
/* Normals to the A-plane and B-plane */
cross_product(P1, &P3, normal);
normalize(normal);
}
/**
* Rotates v1 through an angle (in radians) within the plane defined by v1/v2, returns
* the rotated vector in n.
*/
void vector_rotate(const POINT3D* v1, const POINT3D* v2, double angle, POINT3D* n)
{
POINT3D u;
double cos_a = cos(angle);
double sin_a = sin(angle);
double uxuy, uyuz, uxuz;
double ux2, uy2, uz2;
double rxx, rxy, rxz, ryx, ryy, ryz, rzx, rzy, rzz;
/* Need a unit vector normal to rotate around */
unit_normal(v1, v2, &u);
uxuy = u.x * u.y;
uxuz = u.x * u.z;
uyuz = u.y * u.z;
ux2 = u.x * u.x;
uy2 = u.y * u.y;
uz2 = u.z * u.z;
rxx = cos_a + ux2 * (1 - cos_a);
rxy = uxuy * (1 - cos_a) - u.z * sin_a;
rxz = uxuz * (1 - cos_a) + u.y * sin_a;
ryx = uxuy * (1 - cos_a) + u.z * sin_a;
ryy = cos_a + uy2 * (1 - cos_a);
ryz = uyuz * (1 - cos_a) - u.x * sin_a;
rzx = uxuz * (1 - cos_a) - u.y * sin_a;
rzy = uyuz * (1 - cos_a) + u.x * sin_a;
rzz = cos_a + uz2 * (1 - cos_a);
n->x = rxx * v1->x + rxy * v1->y + rxz * v1->z;
n->y = ryx * v1->x + ryy * v1->y + ryz * v1->z;
n->z = rzx * v1->x + rzy * v1->y + rzz * v1->z;
normalize(n);
}
/**
* Normalize to a unit vector.
*/
void normalize(POINT3D *p)
{
double d = sqrt(p->x*p->x + p->y*p->y + p->z*p->z);
if (FP_IS_ZERO(d))
{
p->x = p->y = p->z = 0.0;
return;
}
p->x = p->x / d;
p->y = p->y / d;
p->z = p->z / d;
return;
}
/**
* Computes the cross product of two vectors using their lat, lng representations.
* Good even for small distances between p and q.
*/
void robust_cross_product(const GEOGRAPHIC_POINT *p, const GEOGRAPHIC_POINT *q, POINT3D *a)
{
double lon_qpp = (q->lon + p->lon) / -2.0;
double lon_qmp = (q->lon - p->lon) / 2.0;
double sin_p_lat_minus_q_lat = sin(p->lat-q->lat);
double sin_p_lat_plus_q_lat = sin(p->lat+q->lat);
double sin_lon_qpp = sin(lon_qpp);
double sin_lon_qmp = sin(lon_qmp);
double cos_lon_qpp = cos(lon_qpp);
double cos_lon_qmp = cos(lon_qmp);
a->x = sin_p_lat_minus_q_lat * sin_lon_qpp * cos_lon_qmp -
sin_p_lat_plus_q_lat * cos_lon_qpp * sin_lon_qmp;
a->y = sin_p_lat_minus_q_lat * cos_lon_qpp * cos_lon_qmp +
sin_p_lat_plus_q_lat * sin_lon_qpp * sin_lon_qmp;
a->z = cos(p->lat) * cos(q->lat) * sin(q->lon-p->lon);
}
void x_to_z(POINT3D *p)
{
double tmp = p->z;
p->z = p->x;
p->x = tmp;
}
void y_to_z(POINT3D *p)
{
double tmp = p->z;
p->z = p->y;
p->y = tmp;
}
int crosses_dateline(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e)
{
double sign_s = signum(s->lon);
double sign_e = signum(e->lon);
double ss = fabs(s->lon);
double ee = fabs(e->lon);
if ( sign_s == sign_e )
{
return LW_FALSE;
}
else
{
double dl = ss + ee;
if ( dl < M_PI )
return LW_FALSE;
else if ( FP_EQUALS(dl, M_PI) )
return LW_FALSE;
else
return LW_TRUE;
}
}
/**
* Returns -1 if the point is to the left of the plane formed
* by the edge, 1 if the point is to the right, and 0 if the
* point is on the plane.
*/
static int
edge_point_side(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
{
POINT3D normal, pt;
double w;
/* Normal to the plane defined by e */
robust_cross_product(&(e->start), &(e->end), &normal);
normalize(&normal);
geog2cart(p, &pt);
/* We expect the dot product of with normal with any vector in the plane to be zero */
w = dot_product(&normal, &pt);
LWDEBUGF(4,"dot product %.9g",w);
if ( FP_IS_ZERO(w) )
{
LWDEBUG(4, "point is on plane (dot product is zero)");
return 0;
}
if ( w < 0 )
return -1;
else
return 1;
}
/**
* Returns the angle in radians at point B of the triangle formed by A-B-C
*/
static double
sphere_angle(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const GEOGRAPHIC_POINT *c)
{
POINT3D normal1, normal2;
robust_cross_product(b, a, &normal1);
robust_cross_product(b, c, &normal2);
normalize(&normal1);
normalize(&normal2);
return sphere_distance_cartesian(&normal1, &normal2);
}
/**
* Computes the spherical area of a triangle. If C is to the left of A/B,
* the area is negative. If C is to the right of A/B, the area is positive.
*
* @param a The first triangle vertex.
* @param b The second triangle vertex.
* @param c The last triangle vertex.
* @return the signed area in radians.
*/
static double
sphere_signed_area(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const GEOGRAPHIC_POINT *c)
{
double angle_a, angle_b, angle_c;
double area_radians = 0.0;
int side;
GEOGRAPHIC_EDGE e;
angle_a = sphere_angle(b,a,c);
angle_b = sphere_angle(a,b,c);
angle_c = sphere_angle(b,c,a);
area_radians = angle_a + angle_b + angle_c - M_PI;
/* What's the direction of the B/C edge? */
e.start = *a;
e.end = *b;
side = edge_point_side(&e, c);
/* Co-linear points implies no area */
if ( side == 0 )
return 0.0;
/* Add the sign to the area */
return side * area_radians;
}
/**
* Returns true if the point p is on the great circle plane.
* Forms the scalar triple product of A,B,p and if the volume of the
* resulting parallelepiped is near zero the point p is on the
* great circle plane.
*/
int edge_point_on_plane(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
{
int side = edge_point_side(e, p);
if ( side == 0 )
return LW_TRUE;
return LW_FALSE;
}
/**
* Returns true if the point p is inside the cone defined by the
* two ends of the edge e.
*/
int edge_point_in_cone(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
{
POINT3D vcp, vs, ve, vp;
double vs_dot_vcp, vp_dot_vcp;
geog2cart(&(e->start), &vs);
geog2cart(&(e->end), &ve);
/* Antipodal case, everything is inside. */
if ( vs.x == -1.0 * ve.x && vs.y == -1.0 * ve.y && vs.z == -1.0 * ve.z )
return LW_TRUE;
geog2cart(p, &vp);
/* The normalized sum bisects the angle between start and end. */
vector_sum(&vs, &ve, &vcp);
normalize(&vcp);
/* The projection of start onto the center defines the minimum similarity */
vs_dot_vcp = dot_product(&vs, &vcp);
LWDEBUGF(4,"vs_dot_vcp %.19g",vs_dot_vcp);
/* The projection of candidate p onto the center */
vp_dot_vcp = dot_product(&vp, &vcp);
LWDEBUGF(4,"vp_dot_vcp %.19g",vp_dot_vcp);
/* If p is more similar than start then p is inside the cone */
LWDEBUGF(4,"fabs(vp_dot_vcp - vs_dot_vcp) %.39g",fabs(vp_dot_vcp - vs_dot_vcp));
/*
** We want to test that vp_dot_vcp is >= vs_dot_vcp but there are
** numerical stability issues for values that are very very nearly
** equal. Unfortunately there are also values of vp_dot_vcp that are legitimately
** very close to but still less than vs_dot_vcp which we also need to catch.
** The tolerance of 10-17 seems to do the trick on 32-bit and 64-bit architectures,
** for the test cases here.
** However, tuning the tolerance value feels like a dangerous hack.
** Fundamentally, the problem is that this test is so sensitive.
*/
/* 1.1102230246251565404236316680908203125e-16 */
if ( vp_dot_vcp > vs_dot_vcp || fabs(vp_dot_vcp - vs_dot_vcp) < 2e-16 )
{
LWDEBUG(4, "point is in cone");
return LW_TRUE;
}
LWDEBUG(4, "point is not in cone");
return LW_FALSE;
}
/**
* True if the longitude of p is within the range of the longitude of the ends of e
*/
int edge_contains_coplanar_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
{
GEOGRAPHIC_EDGE g;
GEOGRAPHIC_POINT q;
double slon = fabs((e->start).lon) + fabs((e->end).lon);
double dlon = fabs(fabs((e->start).lon) - fabs((e->end).lon));
double slat = (e->start).lat + (e->end).lat;
LWDEBUGF(4, "e.start == GPOINT(%.6g %.6g) ", (e->start).lat, (e->start).lon);
LWDEBUGF(4, "e.end == GPOINT(%.6g %.6g) ", (e->end).lat, (e->end).lon);
LWDEBUGF(4, "p == GPOINT(%.6g %.6g) ", p->lat, p->lon);
/* Copy values into working registers */
g = *e;
q = *p;
/* Vertical plane, we need to do this calculation in latitude */
if ( FP_EQUALS( g.start.lon, g.end.lon ) )
{
LWDEBUG(4, "vertical plane, we need to do this calculation in latitude");
/* Supposed to be co-planar... */
if ( ! FP_EQUALS( q.lon, g.start.lon ) )
return LW_FALSE;
if ( ( g.start.lat <= q.lat && q.lat <= g.end.lat ) ||
( g.end.lat <= q.lat && q.lat <= g.start.lat ) )
{
return LW_TRUE;
}
else
{
return LW_FALSE;
}
}
/* Over the pole, we need normalize latitude and do this calculation in latitude */
if ( FP_EQUALS( slon, M_PI ) && ( signum(g.start.lon) != signum(g.end.lon) || FP_EQUALS(dlon, M_PI) ) )
{
LWDEBUG(4, "over the pole...");
/* Antipodal, everything (or nothing?) is inside */
if ( FP_EQUALS( slat, 0.0 ) )
return LW_TRUE;
/* Point *is* the north pole */
if ( slat > 0.0 && FP_EQUALS(q.lat, M_PI_2 ) )
return LW_TRUE;
/* Point *is* the south pole */
if ( slat < 0.0 && FP_EQUALS(q.lat, -1.0 * M_PI_2) )
return LW_TRUE;
LWDEBUG(4, "coplanar?...");
/* Supposed to be co-planar... */
if ( ! FP_EQUALS( q.lon, g.start.lon ) )
return LW_FALSE;
LWDEBUG(4, "north or south?...");
/* Over north pole, test based on south pole */
if ( slat > 0.0 )
{
LWDEBUG(4, "over the north pole...");
if ( q.lat > FP_MIN(g.start.lat, g.end.lat) )
return LW_TRUE;
else
return LW_FALSE;
}
else
/* Over south pole, test based on north pole */
{
LWDEBUG(4, "over the south pole...");
if ( q.lat < FP_MAX(g.start.lat, g.end.lat) )
return LW_TRUE;
else
return LW_FALSE;
}
}
/* Dateline crossing, flip everything to the opposite hemisphere */
else if ( slon > M_PI && ( signum(g.start.lon) != signum(g.end.lon) ) )
{
LWDEBUG(4, "crosses dateline, flip longitudes...");
if ( g.start.lon > 0.0 )
g.start.lon -= M_PI;
else
g.start.lon += M_PI;
if ( g.end.lon > 0.0 )
g.end.lon -= M_PI;
else
g.end.lon += M_PI;
if ( q.lon > 0.0 )
q.lon -= M_PI;
else
q.lon += M_PI;
}
if ( ( g.start.lon <= q.lon && q.lon <= g.end.lon ) ||
( g.end.lon <= q.lon && q.lon <= g.start.lon ) )
{
LWDEBUG(4, "true, this edge contains point");
return LW_TRUE;
}
LWDEBUG(4, "false, this edge does not contain point");
return LW_FALSE;
}
/**
* Given two points on a unit sphere, calculate their distance apart in radians.
*/
double sphere_distance(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e)
{
double d_lon = e->lon - s->lon;
double cos_d_lon = cos(d_lon);
double cos_lat_e = cos(e->lat);
double sin_lat_e = sin(e->lat);
double cos_lat_s = cos(s->lat);
double sin_lat_s = sin(s->lat);
double a1 = POW2(cos_lat_e * sin(d_lon));
double a2 = POW2(cos_lat_s * sin_lat_e - sin_lat_s * cos_lat_e * cos_d_lon);
double a = sqrt(a1 + a2);
double b = sin_lat_s * sin_lat_e + cos_lat_s * cos_lat_e * cos_d_lon;
return atan2(a, b);
}
/**
* Given two unit vectors, calculate their distance apart in radians.
*/
double sphere_distance_cartesian(const POINT3D *s, const POINT3D *e)
{
return acos(FP_MIN(1.0, dot_product(s, e)));
}
/**
* Given two points on a unit sphere, calculate the direction from s to e.
*/
double sphere_direction(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e, double d)
{
double heading = 0.0;
double f;
/* Starting from the poles? Special case. */
if ( FP_IS_ZERO(cos(s->lat)) )
return (s->lat > 0.0) ? M_PI : 0.0;
f = (sin(e->lat) - sin(s->lat) * cos(d)) / (sin(d) * cos(s->lat));
if ( FP_EQUALS(f, 1.0) )
heading = 0.0;
else if ( fabs(f) > 1.0 )
{
LWDEBUGF(4, "f = %g", f);
heading = acos(f);
}
else
heading = acos(f);
if ( sin(e->lon - s->lon) < 0.0 )
heading = -1 * heading;
return heading;
}
#if 0 /* unused */
/**
* Computes the spherical excess of a spherical triangle defined by
* the three vectices A, B, C. Computes on the unit sphere (i.e., divides
* edge lengths by the radius, even if the radius is 1.0). The excess is
* signed based on the sign of the delta longitude of A and B.
*
* @param a The first triangle vertex.
* @param b The second triangle vertex.
* @param c The last triangle vertex.
* @return the signed spherical excess.
*/
static double sphere_excess(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const GEOGRAPHIC_POINT *c)
{
double a_dist = sphere_distance(b, c);
double b_dist = sphere_distance(c, a);
double c_dist = sphere_distance(a, b);
double hca = sphere_direction(c, a, b_dist);
double hcb = sphere_direction(c, b, a_dist);
double sign = signum(hcb-hca);
double ss = (a_dist + b_dist + c_dist) / 2.0;
double E = tan(ss/2.0)*tan((ss-a_dist)/2.0)*tan((ss-b_dist)/2.0)*tan((ss-c_dist)/2.0);
return 4.0 * atan(sqrt(fabs(E))) * sign;
}
#endif
/**
* Returns true if the point p is on the minor edge defined by the
* end points of e.
*/
int edge_contains_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
{
if ( edge_point_in_cone(e, p) && edge_point_on_plane(e, p) )
/* if ( edge_contains_coplanar_point(e, p) && edge_point_on_plane(e, p) ) */