-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpoint.c
265 lines (229 loc) · 4.76 KB
/
point.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
#include "point.h"
/* This file contains definitions for spherical point functions. */
PG_FUNCTION_INFO_V1(spherepoint_in);
PG_FUNCTION_INFO_V1(spherepoint_from_long_lat);
PG_FUNCTION_INFO_V1(spherepoint_distance);
PG_FUNCTION_INFO_V1(spherepoint_long);
PG_FUNCTION_INFO_V1(spherepoint_lat);
PG_FUNCTION_INFO_V1(spherepoint_x);
PG_FUNCTION_INFO_V1(spherepoint_y);
PG_FUNCTION_INFO_V1(spherepoint_z);
PG_FUNCTION_INFO_V1(spherepoint_xyz);
PG_FUNCTION_INFO_V1(spherepoint_equal);
bool
spoint_eq(const SPoint *p1, const SPoint *p2)
{
Vector3D a,
b;
spoint_vector3d(&a, p1);
spoint_vector3d(&b, p2);
return (vector3d_eq(&a, &b));
}
void
spoint_check(SPoint *spoint)
{
bool lat_is_neg = (spoint->lat < 0) ? true : false;
if (spoint->lng < 0 || spoint->lng > PID)
spoint->lng = spoint->lng - floor(spoint->lng / (PID)) * PID;
if (spoint->lat < -PIH || spoint->lat > PIH)
spoint->lat = spoint->lat - floor(spoint->lat / (PID)) * PID;
if (spoint->lng < 0.0)
{
spoint->lng += (PID);
}
if (spoint->lat > PI)
{
spoint->lat -= (2 * PI);
}
if (spoint->lat > PIH)
{
spoint->lat = (PI - spoint->lat);
spoint->lng += ((spoint->lng < PI) ? (PI) : (-PI));
}
if (spoint->lat < -PIH)
{
spoint->lat = (-PI - spoint->lat);
spoint->lng += ((spoint->lng < PI) ? (PI) : (-PI));
}
if (FPeq(spoint->lat, PIH) && lat_is_neg)
spoint->lat = -PIH;
if (FPeq(spoint->lng, PID))
{
spoint->lng = 0.0;
}
if (FPzero(spoint->lng))
{
spoint->lng = 0.0;
}
if (FPzero(spoint->lat))
{
spoint->lat = 0.0;
}
}
void
vector3d_spoint(SPoint *p, const Vector3D *v)
{
double rho = sqrt((v->x) * (v->x) + (v->y) * (v->y));
if (0.0 == rho)
{
if (FPzero(v->z))
{
p->lat = 0.0;
}
else if (v->z > 0)
{
p->lat = PIH;
}
else if (v->z < 0)
{
p->lat = -PIH;
}
}
else
{
p->lat = atan(v->z / rho);
}
p->lng = atan2(v->y, v->x);
if (FPzero(p->lng))
{
p->lng = 0.0;
}
else if (p->lng < 0.0)
{
p->lng += PID;
}
}
void
spoint_vector3d(Vector3D *v, const SPoint *p)
{
v->x = cos(p->lng) * cos(p->lat);
v->y = sin(p->lng) * cos(p->lat);
v->z = sin(p->lat);
}
Datum
spherepoint_in(PG_FUNCTION_ARGS)
{
SPoint *sp = (SPoint *) palloc(sizeof(SPoint));
char *c = PG_GETARG_CSTRING(0);
double lng,
lat;
void sphere_yyparse(void);
init_buffer(c);
sphere_yyparse();
if (get_point(&lng, &lat))
{
sp->lng = lng;
sp->lat = lat;
spoint_check(sp);
}
else
{
reset_buffer();
pfree(sp);
sp = NULL;
elog(ERROR, "spherepoint_in: parse error");
}
reset_buffer();
PG_RETURN_POINTER(sp);
}
Datum
spherepoint_from_long_lat(PG_FUNCTION_ARGS)
{
SPoint *p = (SPoint *) palloc(sizeof(SPoint));
p->lng = PG_GETARG_FLOAT8(0);
p->lat = PG_GETARG_FLOAT8(1);
spoint_check(p);
PG_RETURN_POINTER(p);
}
static double
norm2(double a, double b)
{
return sqrt(a * a + b * b);
}
float8
spoint_dist(const SPoint *p1, const SPoint *p2)
{
float8 dl = p1->lng - p2->lng;
/* use Vincenty's formula for the inverse geodesic problem on the sphere */
float8 f = atan2(norm2(cos(p2->lat) * sin(dl),
cos(p1->lat) * sin(p2->lat)
- sin(p1->lat) * cos(p2->lat) * cos(dl)),
sin(p1->lat) * sin(p2->lat)
+ cos(p1->lat) * cos(p2->lat) * cos(dl));
if (FPzero(f))
{
return 0.0;
}
else
{
return f;
}
}
Datum
spherepoint_distance(PG_FUNCTION_ARGS)
{
SPoint *p1 = (SPoint *) PG_GETARG_POINTER(0);
SPoint *p2 = (SPoint *) PG_GETARG_POINTER(1);
PG_RETURN_FLOAT8(spoint_dist(p1, p2));
}
Datum
spherepoint_long(PG_FUNCTION_ARGS)
{
SPoint *p = (SPoint *) PG_GETARG_POINTER(0);
PG_RETURN_FLOAT8(p->lng);
}
Datum
spherepoint_lat(PG_FUNCTION_ARGS)
{
SPoint *p = (SPoint *) PG_GETARG_POINTER(0);
PG_RETURN_FLOAT8(p->lat);
}
Datum
spherepoint_x(PG_FUNCTION_ARGS)
{
SPoint *p = (SPoint *) PG_GETARG_POINTER(0);
Vector3D v;
spoint_vector3d(&v, p);
PG_RETURN_FLOAT8(v.x);
}
Datum
spherepoint_y(PG_FUNCTION_ARGS)
{
SPoint *p = (SPoint *) PG_GETARG_POINTER(0);
Vector3D v;
spoint_vector3d(&v, p);
PG_RETURN_FLOAT8(v.y);
}
Datum
spherepoint_z(PG_FUNCTION_ARGS)
{
SPoint *p = (SPoint *) PG_GETARG_POINTER(0);
Vector3D v;
spoint_vector3d(&v, p);
PG_RETURN_FLOAT8(v.z);
}
Datum
spherepoint_xyz(PG_FUNCTION_ARGS)
{
SPoint *p = (SPoint *) PG_GETARG_POINTER(0);
Datum dret[3];
ArrayType *result;
Vector3D v;
spoint_vector3d(&v, p);
dret[0] = Float8GetDatumFast(v.x);
dret[1] = Float8GetDatumFast(v.y);
dret[2] = Float8GetDatumFast(v.z);
#ifdef USE_FLOAT8_BYVAL
result = construct_array(dret, 3, FLOAT8OID, sizeof(float8), true, 'd');
#else
result = construct_array(dret, 3, FLOAT8OID, sizeof(float8), false, 'd');
#endif
PG_RETURN_ARRAYTYPE_P(result);
}
Datum
spherepoint_equal(PG_FUNCTION_ARGS)
{
SPoint *p1 = (SPoint *) PG_GETARG_POINTER(0);
SPoint *p2 = (SPoint *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(spoint_eq(p1, p2));
}