|
5 | 5 | PG_FUNCTION_INFO_V1(spherepoint_in);
|
6 | 6 | PG_FUNCTION_INFO_V1(spherepoint_from_long_lat);
|
7 | 7 | PG_FUNCTION_INFO_V1(spherepoint_distance);
|
| 8 | +PG_FUNCTION_INFO_V1(spherepoint_center); |
8 | 9 | PG_FUNCTION_INFO_V1(spherepoint_long);
|
9 | 10 | PG_FUNCTION_INFO_V1(spherepoint_lat);
|
10 | 11 | PG_FUNCTION_INFO_V1(spherepoint_x);
|
11 | 12 | PG_FUNCTION_INFO_V1(spherepoint_y);
|
12 | 13 | PG_FUNCTION_INFO_V1(spherepoint_z);
|
13 | 14 | PG_FUNCTION_INFO_V1(spherepoint_xyz);
|
14 | 15 | PG_FUNCTION_INFO_V1(spherepoint_equal);
|
| 16 | +PG_FUNCTION_INFO_V1(spoint_from_xyz); |
| 17 | +PG_FUNCTION_INFO_V1(center); |
| 18 | + |
15 | 19 |
|
16 | 20 | bool
|
17 | 21 | spoint_eq(const SPoint *p1, const SPoint *p2)
|
@@ -179,6 +183,13 @@ spoint_dist(const SPoint *p1, const SPoint *p2)
|
179 | 183 | }
|
180 | 184 | }
|
181 | 185 |
|
| 186 | +Datum |
| 187 | +spherepoint_center(PG_FUNCTION_ARGS) |
| 188 | +{ |
| 189 | + SPoint *p = (SPoint *) PG_GETARG_POINTER(0); |
| 190 | + PG_RETURN_POINTER(p); |
| 191 | +} |
| 192 | + |
182 | 193 | Datum
|
183 | 194 | spherepoint_distance(PG_FUNCTION_ARGS)
|
184 | 195 | {
|
@@ -263,3 +274,59 @@ spherepoint_equal(PG_FUNCTION_ARGS)
|
263 | 274 |
|
264 | 275 | PG_RETURN_BOOL(spoint_eq(p1, p2));
|
265 | 276 | }
|
| 277 | + |
| 278 | +Datum spoint_from_xyz(PG_FUNCTION_ARGS) |
| 279 | +{ |
| 280 | + Vector3D point_coords; |
| 281 | + SPoint *p = (SPoint *) palloc(sizeof(SPoint)); |
| 282 | + |
| 283 | + point_coords.x = PG_GETARG_FLOAT8(0); |
| 284 | + point_coords.y = PG_GETARG_FLOAT8(1); |
| 285 | + point_coords.z = PG_GETARG_FLOAT8(2); |
| 286 | + vector3d_spoint(p, &point_coords); |
| 287 | + |
| 288 | + if (p == NULL) |
| 289 | + { |
| 290 | + PG_RETURN_NULL(); |
| 291 | + } |
| 292 | + |
| 293 | + spoint_check(p); |
| 294 | + PG_RETURN_POINTER(p); |
| 295 | +} |
| 296 | + |
| 297 | +Datum center(PG_FUNCTION_ARGS) |
| 298 | +{ |
| 299 | + int num_elements, i; |
| 300 | + SPoint * p = (SPoint *) palloc(sizeof(SPoint)); |
| 301 | + SPoint * array_data; |
| 302 | + Vector3D v; |
| 303 | + Vector3D point_coords = {0,0,0}; |
| 304 | + ArrayType *dots_vector; |
| 305 | + |
| 306 | + dots_vector = PG_GETARG_ARRAYTYPE_P(0); |
| 307 | + num_elements = ArrayGetNItems(ARR_NDIM(dots_vector), ARR_DIMS(dots_vector)); |
| 308 | + if (num_elements <= 0) |
| 309 | + { |
| 310 | + PG_RETURN_NULL(); |
| 311 | + } |
| 312 | + |
| 313 | + array_data = (SPoint *) ARR_DATA_PTR(dots_vector); |
| 314 | + |
| 315 | + for (i = 0; i < num_elements; i++) |
| 316 | + { |
| 317 | + spoint_vector3d(&v, &array_data[i]); |
| 318 | + point_coords.x += v.x; |
| 319 | + point_coords.y += v.y; |
| 320 | + point_coords.z += v.z; |
| 321 | + } |
| 322 | + |
| 323 | + point_coords.x /= num_elements; |
| 324 | + point_coords.y /= num_elements; |
| 325 | + point_coords.z /= num_elements; |
| 326 | + |
| 327 | + vector3d_spoint(p, &point_coords); |
| 328 | + |
| 329 | + spoint_check(p); |
| 330 | + |
| 331 | + PG_RETURN_POINTER(p); |
| 332 | +} |
0 commit comments