-
Notifications
You must be signed in to change notification settings - Fork 16
/
line.c
1053 lines (896 loc) · 19.9 KB
/
line.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
#include "line.h"
/* Line functions */
PG_FUNCTION_INFO_V1(sphereline_in);
PG_FUNCTION_INFO_V1(sphereline_from_point);
PG_FUNCTION_INFO_V1(sphereline_from_points);
PG_FUNCTION_INFO_V1(sphereline_from_trans);
PG_FUNCTION_INFO_V1(sphereline_meridian);
PG_FUNCTION_INFO_V1(sphereline_swap_beg_end);
PG_FUNCTION_INFO_V1(sphereline_turn);
PG_FUNCTION_INFO_V1(sphereline_begin);
PG_FUNCTION_INFO_V1(sphereline_end);
PG_FUNCTION_INFO_V1(sphereline_length);
PG_FUNCTION_INFO_V1(sphereline_cont_point);
PG_FUNCTION_INFO_V1(sphereline_cont_point_neg);
PG_FUNCTION_INFO_V1(sphereline_cont_point_com);
PG_FUNCTION_INFO_V1(sphereline_cont_point_com_neg);
PG_FUNCTION_INFO_V1(spherecircle_cont_line);
PG_FUNCTION_INFO_V1(spherecircle_cont_line_neg);
PG_FUNCTION_INFO_V1(spherecircle_cont_line_com);
PG_FUNCTION_INFO_V1(spherecircle_cont_line_com_neg);
PG_FUNCTION_INFO_V1(sphereline_overlap_circle);
PG_FUNCTION_INFO_V1(sphereline_overlap_circle_neg);
PG_FUNCTION_INFO_V1(sphereline_overlap_circle_com);
PG_FUNCTION_INFO_V1(sphereline_overlap_circle_com_neg);
PG_FUNCTION_INFO_V1(sphereline_equal);
PG_FUNCTION_INFO_V1(sphereline_equal_neg);
PG_FUNCTION_INFO_V1(sphereline_crosses);
PG_FUNCTION_INFO_V1(sphereline_crosses_neg);
PG_FUNCTION_INFO_V1(sphereline_overlap);
PG_FUNCTION_INFO_V1(sphereline_overlap_neg);
PG_FUNCTION_INFO_V1(spheretrans_from_line);
PG_FUNCTION_INFO_V1(spheretrans_line);
PG_FUNCTION_INFO_V1(spheretrans_line_inverse);
/*
* Swaps the beginning and ending of the line.
*/
static void
sline_swap_beg_end(SLine *out, const SLine *in)
{
SLine l;
SEuler se;
l.length = in->length;
l.phi = -in->length;
l.theta = PI;
l.psi = 0.0;
seuler_set_zxz(&se);
se.phi = in->phi;
se.theta = in->theta;
se.psi = in->psi;
euler_sline_trans(out, &l, &se);
}
bool
sline_eq(const SLine *l1, const SLine *l2)
{
if (FPne(l1->length, l2->length))
{
return false;
}
else
{
SEuler e1, e2;
seuler_set_zxz(&e1);
seuler_set_zxz(&e2);
e1.phi = l1->phi;
e1.theta = l1->theta;
e1.psi = l1->psi;
e2.phi = (FPeq(l2->length, PID)) ? (l1->phi) : (l2->phi);
e2.theta = l2->theta;
e2.psi = l2->psi;
return (strans_eq(&e1, &e2));
}
return false;
}
bool
sline_from_points(SLine *sl, const SPoint *pbeg, const SPoint *pend)
{
SEuler se;
float8 l;
l = spoint_dist(pbeg, pend);
if (FPeq(l, PI))
{
if (FPeq(pbeg->lng, pend->lng))
{
sline_meridian(sl, pbeg->lng);
return true;
}
return false;
}
if (spherevector_to_euler(&se, pbeg, pend))
{
sl->phi = se.phi;
sl->theta = se.theta;
sl->psi = se.psi;
sl->length = l;
}
else
{
sl->phi = PIH;
sl->theta = pbeg->lat;
sl->psi = pbeg->lng - PIH;
sl->length = 0.0;
}
return (true);
}
void
sline_meridian(SLine *sl, float8 lng)
{
sl->phi = -PIH;
sl->theta = PIH;
sl->psi = (lng < 0.0) ? lng + PID : lng;
sl->length = PI;
}
void
sline_begin(SPoint *p, const SLine *l)
{
const SPoint tmp = {0.0, 0.0};
SEuler se;
sphereline_to_euler(&se, l);
euler_spoint_trans(p, &tmp, &se);
}
void
sline_end(SPoint *p, const SLine *l)
{
SPoint tmp = {0.0, 0.0};
SEuler se;
tmp.lng = l->length;
sphereline_to_euler(&se, l);
euler_spoint_trans(p, &tmp, &se);
}
/*
* Place begin of a line "l" as vector "v".
*/
static void
sline_vector_begin(Vector3D *v, const SLine *l)
{
const Vector3D tmp = {1.0, 0.0, 0.0};
SEuler se;
sphereline_to_euler(&se, l);
euler_vector_trans(v, &tmp, &se);
}
/*
* Place end of a line "l" as vector "v".
*/
static void
sline_vector_end(Vector3D *v, const SLine *l)
{
Vector3D tmp = {0.0, 0.0, 0.0};
SEuler se;
tmp.x = cos(l->length);
tmp.y = sin(l->length);
sphereline_to_euler(&se, l);
euler_vector_trans(v, &tmp, &se);
}
void
sline_min_max_lat(const SLine *sl, float8 *minlat, float8 *maxlat)
{
float8 inc = sl->theta - floor(sl->theta / PID) * PID;
inc = (inc > PI) ? (PID - inc) : (inc);
if (FPzero(inc) || FPeq(inc, PI))
{
*minlat = *maxlat = 0.0;
return;
}
else
{
SEuler se;
SLine nl;
SPoint tp;
float8 lng;
seuler_set_zxz(&se);
se.phi = -sl->psi;
se.theta = (inc > PIH) ? (PI - 2 * inc) : (0.0);
se.psi = 0.0;
euler_sline_trans(&nl, sl, &se);
/* Now ascending node at (0,0), line ascending */
sline_begin(&tp, &nl);
*minlat = *maxlat = tp.lat;
sline_end(&tp, &nl);
*minlat = Min(tp.lat, *minlat);
*maxlat = Max(tp.lat, *maxlat);
for (lng = PIH; lng < PID; lng += PI)
{
tp.lng = lng;
tp.lat = (lng < PI) ? (inc) : (-inc);
if (spoint_at_sline(&tp, &nl))
{
*minlat = Min(tp.lat, *minlat);
*maxlat = Max(tp.lat, *maxlat);
}
}
}
}
int32
sphereline_latitude_points(const SLine *sl, float8 lat, SPoint *p1, SPoint *p2)
{
float8 inc = sl->theta - floor(sl->theta / PID) * PID;
int32 ret = 0;
if (FPgt(lat, PIH))
return 0;
if (FPlt(lat, -PIH))
return 0;
inc = (inc > PI) ? (PID - inc) : (inc);
if (FPzero(inc) || FPeq(inc, PI))
{
if (FPzero(lat))
return -1;
else
return 0;
}
else
{
SLine nl;
float8 rot = (inc > PIH) ? (sl->psi - PIH) : (sl->psi + PIH);
bool p1b,
p2b;
/* Transform maximum latitude of full line to longitude 0.0 */
memcpy((void *) &nl, (void *) sl, sizeof(SLine));
nl.psi = (inc > PIH) ? (PIH) : (-PIH);
p1->lat = p2->lat = lat;
p1->lng = p2->lng = 0.0;
if (FPeq(inc, PIH))
{
p1->lng = PIH;
p2->lng = -PIH;
ret = 2;
}
else
{
float8 ainc;
ainc = fabs(inc - ((inc > PIH) ? (PI) : (0.0)));
if (FPgt(lat, ainc))
return 0;
else if (FPlt(lat, -ainc))
return 0;
else if (FPeq(lat, ainc))
{
p1->lng = 0.0;
ret = 1;
}
else if (FPeq(lat, -ainc))
{
p1->lng = PI;
ret = 1;
}
else
{
p1->lng = acos(sin(lat) * cos(ainc) / (sin(ainc) * cos(lat)));
p2->lng = PID - p1->lng;
ret = 2;
}
}
if (ret == 1)
{
p1b = spoint_at_sline(p1, &nl);
if (!p1b)
{
ret = 0;
}
}
else if (ret == 2)
{
p1b = spoint_at_sline(p1, &nl);
p2b = spoint_at_sline(p2, &nl);
if (p1b && p2b)
{
ret = 2;
}
else if (!p1b && p2b)
{
ret = 1;
memcpy((void *) p1, (void *) p2, sizeof(SPoint));
}
else if (p1b && !p2b)
{
ret = 1;
}
else
{
ret = 0;
}
}
if (ret > 0)
{
p1->lng += rot;
p2->lng += rot;
spoint_check(p1);
spoint_check(p2);
}
}
return ret;
}
int8
sphereline_circle_pos(const SLine *sl, const SCIRCLE *sc)
{
float8 i, mi;
const float8 step = (PI - 0.01);
SPoint p[2] = {{0.0, 0.0}, {0.0, 0.0}};
SCIRCLE c;
bool bbeg, bend;
SEuler se;
int contain;
if (FPzero(sl->length))
{
/* line is point */
sline_begin(&p[0], sl);
if (spoint_in_circle(&p[0], &c))
{
return PGS_CIRCLE_CONT_LINE;
}
else
{
return PGS_CIRCLE_LINE_AVOID;
}
}
contain = 0;
sphereline_to_euler_inv(&se, sl);
euler_scircle_trans(&c, sc, &se);
mi = sl->length / step;
/* split line in segments and check for each of it */
for (i = 0.0; i < mi; i += 1.0)
{
p[0].lng = i * step;
p[1].lng = (((i + 1.0) > mi) ? (sl->length) : ((i + 1.0) * step));
bbeg = spoint_in_circle(&p[0], &c);
bend = spoint_in_circle(&p[1], &c);
if (bbeg && bend)
{
contain++;
}
else if (bbeg || bend)
{
return PGS_CIRCLE_LINE_OVER;
}
else if (FPle(((c.center.lat < 0) ? (-c.center.lat) : (c.center.lat)),
c.radius) &&
FPge(c.center.lng, p[0].lng) &&
FPle(c.center.lng, p[1].lng))
{
return PGS_CIRCLE_LINE_OVER;
}
else if (contain > 0)
{
return PGS_CIRCLE_LINE_OVER;
}
}
if (contain > 0 && contain == (floor(mi) + 1))
{
return PGS_CIRCLE_CONT_LINE;
}
return PGS_CIRCLE_LINE_AVOID;
}
bool
sline_circle_touch(const SLine *sl, const SCIRCLE *sc)
{
/* we assume here, line and circle overlap */
SEuler se;
SCIRCLE tc;
sphereline_to_euler_inv(&se, sl);
euler_scircle_trans(&tc, sc, &se);
if (FPge(tc.center.lng, 0.0) && FPle(tc.center.lng, sl->length))
{
if (FPeq(fabs(tc.center.lat), sc->radius))
{
return true;
}
return false;
}
else
{
SPoint p;
p.lng = p.lat = 0.0;
if (FPeq(spoint_dist(&p, &tc.center), sc->radius))
{
return true;
}
p.lng = sl->length;
if (FPeq(spoint_dist(&p, &tc.center), sc->radius))
{
return true;
}
return false;
}
}
int8
sline_sline_pos(const SLine *l1, const SLine *l2)
{
const SLine *il1, *il2;
Vector3D v[2][2],
vtmp;
SEuler se;
SLine sl1, sl2, lseg;
SPoint p[4];
const float8 seg_length = (PI - 0.1);
float8 seg_begin;
if (sline_eq(l1, l2))
{
return PGS_LINE_EQUAL;
}
sline_swap_beg_end(&sl1, l1);
if (sline_eq(&sl1, l2))
{
return PGS_LINE_CONT_LINE;
}
/* transform the larger line into equator ( begin at (0,0) ) */
if (FPge(l1->length, l2->length))
{
il1 = l1;
il2 = l2;
}
else
{
il1 = l2;
il2 = l1;
}
if (FPzero(il1->length))
{
/* both are points */
return PGS_LINE_AVOID;
}
sl1.phi = sl1.theta = sl1.psi = 0.0;
p[0].lat = p[0].lng = p[1].lat = 0.0;
sl1.length = p[1].lng = il1->length;
v[0][0].x = 1.0;
v[0][0].y = 0.0;
v[0][0].z = 0.0;
v[0][1].x = cos(il1->length);
v[0][1].y = sin(il1->length);
v[0][1].z = 0.0;
sphereline_to_euler_inv(&se, il1);
sline_vector_begin(&vtmp, il2);
euler_vector_trans(&v[1][0], &vtmp, &se);
sline_vector_end(&vtmp, il2);
euler_vector_trans(&v[1][1], &vtmp, &se);
vector3d_spoint(&p[2], &v[1][0]);
vector3d_spoint(&p[3], &v[1][1]);
/* check connected lines */
if (FPgt(il2->length, 0.0) && (vector3d_eq(&v[0][0], &v[1][0]) ||
vector3d_eq(&v[0][0], &v[1][1]) ||
vector3d_eq(&v[0][1], &v[1][0]) ||
vector3d_eq(&v[0][1], &v[1][1])))
{
return PGS_LINE_CONNECT;
}
/* Check, sl2 is at equator */
if (FPzero(p[2].lat) && FPzero(p[3].lat))
{
bool a1 = spoint_at_sline(&p[2], &sl1);
bool a2 = spoint_at_sline(&p[3], &sl1);
if (a1 && a2)
{
if (il1 == l2)
{
return PGS_LINE_OVER;
}
else
{
return PGS_LINE_CONT_LINE;
}
}
else if (a1 || a2)
{
return PGS_LINE_OVER;
}
return PGS_LINE_AVOID;
}
/* Now sl2 is not at equator */
if (FPle(il2->length, seg_length))
{
bool a1 = (FPge(p[2].lat, 0.0) && FPle(p[3].lat, 0.0));
/* sl2 crosses equator desc. */
bool a2 = (FPle(p[2].lat, 0.0) && FPge(p[3].lat, 0.0));
/* sl2 crosses equator asc. */
if (a1 || a2)
{
SPoint sp;
euler_sline_trans(&sl2, il2, &se);
sphereline_to_euler_inv(&se, &sl2);
sp.lng = ((a1) ? (PI) : (0.0)) - se.phi;
/* node */
sp.lat = 0;
spoint_check(&sp);
if (FPge(sp.lng, 0.0) && FPle(sp.lng, p[1].lng))
{
return PGS_LINE_CROSS;
}
}
return PGS_LINE_AVOID;
}
/*
* We split the smaller line in segments with length less than 180 deg
*/
euler_sline_trans(&sl2, il2, &se);
for (seg_begin = 0.0; seg_begin < il2->length; seg_begin += seg_length)
{
lseg.length = ((seg_begin + seg_length) > il2->length) ?
(il2->length - seg_begin) : seg_length;
lseg.phi = sl2.phi + seg_begin;
lseg.theta = sl2.theta;
lseg.psi = sl2.psi;
if (sline_sline_pos(&sl1, &lseg) != PGS_LINE_AVOID)
{
return PGS_LINE_CROSS;
}
}
return PGS_LINE_AVOID;
}
void
sphereline_to_euler_inv(SEuler *se, const SLine *sl)
{
sphereline_to_euler(se, sl);
spheretrans_inv(se);
}
void
sphereline_to_euler(SEuler *se, const SLine *sl)
{
seuler_set_zxz(se);
se->phi = sl->phi;
se->theta = sl->theta;
se->psi = sl->psi;
}
void
euler_sline_trans(SLine *out, const SLine *in, const SEuler *se)
{
SEuler stmp[2];
sphereline_to_euler(&stmp[0], in);
seuler_trans_zxz(&stmp[1], &stmp[0], se);
out->phi = stmp[1].phi;
out->theta = stmp[1].theta;
out->psi = stmp[1].psi;
out->length = in->length;
}
bool
spoint_at_sline(const SPoint *p, const SLine *sl)
{
SEuler se;
SPoint sp;
sphereline_to_euler_inv(&se, sl);
euler_spoint_trans(&sp, p, &se);
if (FPzero(sp.lat))
{
if (FPge(sp.lng, 0.0) && FPle(sp.lng, sl->length))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
void
sline_center(SPoint *c, const SLine *sl)
{
SEuler se;
SPoint p;
p.lng = sl->length / 2.0;
p.lat = 0.0;
sphereline_to_euler(&se, sl);
euler_spoint_trans(c, &p, &se);
}
Datum
sphereline_in(PG_FUNCTION_ARGS)
{
SLine *sl = (SLine *) palloc(sizeof(SLine));
char *c = PG_GETARG_CSTRING(0);
unsigned char etype[3];
float8 eang[3],
length;
SEuler se,
stmp,
so;
int i;
void sphere_yyparse(void);
init_buffer(c);
sphere_yyparse();
if (get_line(&eang[0], &eang[1], &eang[2], etype, &length))
{
for (i = 0; i < 3; i++)
{
switch (i)
{
case 0:
se.phi_a = etype[i];
break;
case 1:
se.theta_a = etype[i];
break;
case 2:
se.psi_a = etype[i];
break;
}
}
se.phi = eang[0];
se.theta = eang[1];
se.psi = eang[2];
stmp.phi = stmp.theta = stmp.psi = 0.0;
stmp.phi_a = stmp.theta_a = stmp.psi_a = EULER_AXIS_Z;
seuler_trans_zxz(&so, &se, &stmp);
sl->phi = so.phi;
sl->theta = so.theta;
sl->psi = so.psi;
if (FPge(length, PID))
{
length = PID;
}
sl->length = length;
}
else
{
reset_buffer();
pfree(sl);
sl = NULL;
elog(ERROR, "sphereline_in: parse error");
}
reset_buffer();
PG_RETURN_POINTER(sl);
}
Datum
sphereline_equal(PG_FUNCTION_ARGS)
{
SLine *l1 = (SLine *) PG_GETARG_POINTER(0);
SLine *l2 = (SLine *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(sline_eq(l1, l2));
}
Datum
sphereline_equal_neg(PG_FUNCTION_ARGS)
{
SLine *l1 = (SLine *) PG_GETARG_POINTER(0);
SLine *l2 = (SLine *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(!sline_eq(l1, l2));
}
Datum
sphereline_from_point(PG_FUNCTION_ARGS)
{
SLine *sl = (SLine *) palloc(sizeof(SLine));
SPoint *p = (SPoint *) PG_GETARG_POINTER(0);
sline_from_points(sl, p, p);
PG_RETURN_POINTER(sl);
}
Datum
sphereline_from_points(PG_FUNCTION_ARGS)
{
SLine *sl = (SLine *) palloc(sizeof(SLine));
SPoint *beg = (SPoint *) PG_GETARG_POINTER(0);
SPoint *end = (SPoint *) PG_GETARG_POINTER(1);
if (!sline_from_points(sl, beg, end))
{
pfree(sl);
sl = NULL;
elog(ERROR, "sphereline_from_points: length of line must be not equal 180 degrees");
}
PG_RETURN_POINTER(sl);
}
Datum
sphereline_from_trans(PG_FUNCTION_ARGS)
{
SLine *sl = (SLine *) palloc(sizeof(SLine));
SEuler *se = (SEuler *) PG_GETARG_POINTER(0);
float8 l = PG_GETARG_FLOAT8(1);
if (FPlt(l, 0.0))
{
pfree(sl);
elog(ERROR, "sphereline_from_trans: length of line must be >= 0");
PG_RETURN_NULL();
}
else
{
SEuler tmp;
if (FPgt(l, PID))
{
l = PID;
}
strans_zxz(&tmp, se);
sl->phi = tmp.phi;
sl->theta = tmp.theta;
sl->psi = tmp.psi;
sl->length = l;
}
PG_RETURN_POINTER(sl);
}
Datum
sphereline_meridian(PG_FUNCTION_ARGS)
{
SLine *out = (SLine *) palloc(sizeof(SLine));
float8 lng = PG_GETARG_FLOAT8(0);
sline_meridian(out, lng);
PG_RETURN_POINTER(out);
}
Datum
sphereline_swap_beg_end(PG_FUNCTION_ARGS)
{
SLine *in = (SLine *) PG_GETARG_POINTER(0);
SLine *out = (SLine *) palloc(sizeof(SLine));
sline_swap_beg_end(out, in);
PG_RETURN_POINTER(out);
}
Datum
sphereline_turn(PG_FUNCTION_ARGS)
{
SLine *in = (SLine *) PG_GETARG_POINTER(0);
if (FPzero(in->length))
{
PG_RETURN_NULL();
}
else
{
SLine *out = (SLine *) palloc(sizeof(SLine));
SEuler se;
SLine tmp;
tmp.phi = 0.0;
tmp.theta = PI;
tmp.psi = 0.0;
tmp.length = PID - in->length;
sphereline_to_euler(&se, in);
euler_sline_trans(out, &tmp, &se);
PG_RETURN_POINTER(out);
}
PG_RETURN_NULL();
}
Datum
sphereline_begin(PG_FUNCTION_ARGS)
{
SLine *sl = (SLine *) PG_GETARG_POINTER(0);
SPoint *sp = (SPoint *) palloc(sizeof(SPoint));
sline_begin(sp, sl);
PG_RETURN_POINTER(sp);
}
Datum
sphereline_end(PG_FUNCTION_ARGS)
{
SLine *sl = (SLine *) PG_GETARG_POINTER(0);
SPoint *sp = (SPoint *) palloc(sizeof(SPoint));
sline_end(sp, sl);
PG_RETURN_POINTER(sp);
}
Datum
sphereline_length(PG_FUNCTION_ARGS)
{
SLine *sl = (SLine *) PG_GETARG_POINTER(0);
PG_RETURN_FLOAT8(sl->length);
}
Datum
spherecircle_cont_line(PG_FUNCTION_ARGS)
{
SCIRCLE *c = (SCIRCLE *) PG_GETARG_POINTER(0);
SLine *l = (SLine *) PG_GETARG_POINTER(1);
int8 b = sphereline_circle_pos(l, c);
PG_RETURN_BOOL(b == PGS_CIRCLE_CONT_LINE);
}
Datum
spherecircle_cont_line_neg(PG_FUNCTION_ARGS)
{
SCIRCLE *c = (SCIRCLE *) PG_GETARG_POINTER(0);
SLine *l = (SLine *) PG_GETARG_POINTER(1);
int8 b = sphereline_circle_pos(l, c);
PG_RETURN_BOOL(b != PGS_CIRCLE_CONT_LINE);
}
Datum
spherecircle_cont_line_com(PG_FUNCTION_ARGS)
{
SCIRCLE *c = (SCIRCLE *) PG_GETARG_POINTER(1);
SLine *l = (SLine *) PG_GETARG_POINTER(0);
int8 b = sphereline_circle_pos(l, c);
PG_RETURN_BOOL(b == PGS_CIRCLE_CONT_LINE);
}
Datum
spherecircle_cont_line_com_neg(PG_FUNCTION_ARGS)
{
SCIRCLE *c = (SCIRCLE *) PG_GETARG_POINTER(1);
SLine *l = (SLine *) PG_GETARG_POINTER(0);
int8 b = sphereline_circle_pos(l, c);
PG_RETURN_BOOL(b != PGS_CIRCLE_CONT_LINE);
}
Datum
sphereline_overlap_circle(PG_FUNCTION_ARGS)
{
SLine *l = (SLine *) PG_GETARG_POINTER(0);
SCIRCLE *c = (SCIRCLE *) PG_GETARG_POINTER(1);
int8 b = sphereline_circle_pos(l, c);
PG_RETURN_BOOL(b > PGS_CIRCLE_LINE_AVOID);
}
Datum
sphereline_overlap_circle_neg(PG_FUNCTION_ARGS)
{
SLine *l = (SLine *) PG_GETARG_POINTER(0);
SCIRCLE *c = (SCIRCLE *) PG_GETARG_POINTER(1);
int8 b = sphereline_circle_pos(l, c);
PG_RETURN_BOOL(b == PGS_CIRCLE_LINE_AVOID);
}
Datum
sphereline_overlap_circle_com(PG_FUNCTION_ARGS)
{
SLine *l = (SLine *) PG_GETARG_POINTER(1);
SCIRCLE *c = (SCIRCLE *) PG_GETARG_POINTER(0);
int8 b = sphereline_circle_pos(l, c);
PG_RETURN_BOOL(b > PGS_CIRCLE_LINE_AVOID);
}
Datum
sphereline_overlap_circle_com_neg(PG_FUNCTION_ARGS)
{
SLine *l = (SLine *) PG_GETARG_POINTER(1);
SCIRCLE *c = (SCIRCLE *) PG_GETARG_POINTER(0);
int8 b = sphereline_circle_pos(l, c);
PG_RETURN_BOOL(b == PGS_CIRCLE_LINE_AVOID);
}
Datum
sphereline_crosses(PG_FUNCTION_ARGS)
{
SLine *l1 = (SLine *) PG_GETARG_POINTER(0);
SLine *l2 = (SLine *) PG_GETARG_POINTER(1);
int8 r = sline_sline_pos(l1, l2);
PG_RETURN_BOOL(r == PGS_LINE_CROSS);
}
Datum
sphereline_crosses_neg(PG_FUNCTION_ARGS)
{
SLine *l1 = (SLine *) PG_GETARG_POINTER(0);
SLine *l2 = (SLine *) PG_GETARG_POINTER(1);
int8 r = sline_sline_pos(l1, l2);
PG_RETURN_BOOL(r != PGS_LINE_CROSS);
}
Datum
sphereline_overlap(PG_FUNCTION_ARGS)
{
SLine *l1 = (SLine *) PG_GETARG_POINTER(0);
SLine *l2 = (SLine *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(sline_sline_pos(l1, l2) > PGS_LINE_AVOID);
}
Datum
sphereline_overlap_neg(PG_FUNCTION_ARGS)
{
SLine *l1 = (SLine *) PG_GETARG_POINTER(0);
SLine *l2 = (SLine *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(sline_sline_pos(l1, l2) == PGS_LINE_AVOID);
}
Datum
sphereline_cont_point(PG_FUNCTION_ARGS)
{
SLine *l = (SLine *) PG_GETARG_POINTER(0);
SPoint *p = (SPoint *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(spoint_at_sline(p, l));
}
Datum
sphereline_cont_point_neg(PG_FUNCTION_ARGS)
{
SLine *l = (SLine *) PG_GETARG_POINTER(0);
SPoint *p = (SPoint *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(!spoint_at_sline(p, l));
}