/src/postgis/liblwgeom/measures3d.c
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * PostGIS - Spatial Types for PostgreSQL |
4 | | * http://postgis.net |
5 | | * |
6 | | * PostGIS is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation, either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * PostGIS is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with PostGIS. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | ********************************************************************** |
20 | | * |
21 | | * Copyright 2011 Nicklas Avén |
22 | | * Copyright 2019, 2026 Darafei Praliaskouski <me@komzpa.net> |
23 | | * |
24 | | **********************************************************************/ |
25 | | |
26 | | #include <string.h> |
27 | | #include <stdlib.h> |
28 | | |
29 | | #include "measures3d.h" |
30 | | #include "lwrandom.h" |
31 | | #include "lwgeom_log.h" |
32 | | |
33 | | static inline int |
34 | | get_3dvector_from_points(const POINT3DZ *p1, const POINT3DZ *p2, VECTOR3D *v) |
35 | 0 | { |
36 | 0 | v->x = p2->x - p1->x; |
37 | 0 | v->y = p2->y - p1->y; |
38 | 0 | v->z = p2->z - p1->z; |
39 | |
|
40 | 0 | return (!FP_IS_ZERO(v->x) || !FP_IS_ZERO(v->y) || !FP_IS_ZERO(v->z)); |
41 | 0 | } |
42 | | |
43 | | static inline int |
44 | | get_3dcross_product(const VECTOR3D *v1, const VECTOR3D *v2, VECTOR3D *v) |
45 | 0 | { |
46 | 0 | v->x = (v1->y * v2->z) - (v1->z * v2->y); |
47 | 0 | v->y = (v1->z * v2->x) - (v1->x * v2->z); |
48 | 0 | v->z = (v1->x * v2->y) - (v1->y * v2->x); |
49 | |
|
50 | 0 | return (!FP_IS_ZERO(v->x) || !FP_IS_ZERO(v->y) || !FP_IS_ZERO(v->z)); |
51 | 0 | } |
52 | | |
53 | | /** |
54 | | Finds a point on a plane from where the original point is perpendicular to the plane |
55 | | */ |
56 | | static double |
57 | | project_point_on_plane(const POINT3DZ *p, PLANE3D *pl, POINT3DZ *p0) |
58 | 0 | { |
59 | | /*In our plane definition we have a point on the plane and a normal vector (pl.pv), perpendicular to the plane |
60 | | this vector will be parallel to the line between our inputted point above the plane and the point we are |
61 | | searching for on the plane. So, we already have a direction from p to find p0, but we don't know the distance. |
62 | | */ |
63 | |
|
64 | 0 | VECTOR3D v1; |
65 | 0 | double f; |
66 | |
|
67 | 0 | if (!get_3dvector_from_points(&(pl->pop), p, &v1)) |
68 | 0 | return 0.0; |
69 | | |
70 | 0 | f = DOT(pl->pv, v1); |
71 | 0 | if (FP_IS_ZERO(f)) |
72 | 0 | { |
73 | | /* Point is in the plane */ |
74 | 0 | *p0 = *p; |
75 | 0 | return 0; |
76 | 0 | } |
77 | | |
78 | 0 | f = -f / DOT(pl->pv, pl->pv); |
79 | |
|
80 | 0 | p0->x = p->x + pl->pv.x * f; |
81 | 0 | p0->y = p->y + pl->pv.y * f; |
82 | 0 | p0->z = p->z + pl->pv.z * f; |
83 | |
|
84 | 0 | return f; |
85 | 0 | } |
86 | | |
87 | | /** |
88 | | This function is used to create a vertical line used for cases where one if the |
89 | | geometries lacks z-values. The vertical line crosses the 2d point that is closest |
90 | | and the z-range is from maxz to minz in the geometry that has z values. |
91 | | */ |
92 | | static LWGEOM * |
93 | | create_v_line(const LWGEOM *lwgeom, double x, double y, int32_t srid) |
94 | 0 | { |
95 | |
|
96 | 0 | LWPOINT *lwpoints[2]; |
97 | 0 | GBOX gbox; |
98 | 0 | int rv = lwgeom_calculate_gbox(lwgeom, &gbox); |
99 | |
|
100 | 0 | if (rv == LW_FAILURE) |
101 | 0 | return NULL; |
102 | | |
103 | 0 | lwpoints[0] = lwpoint_make3dz(srid, x, y, gbox.zmin); |
104 | 0 | lwpoints[1] = lwpoint_make3dz(srid, x, y, gbox.zmax); |
105 | |
|
106 | 0 | return (LWGEOM *)lwline_from_ptarray(srid, 2, lwpoints); |
107 | 0 | } |
108 | | |
109 | | LWGEOM * |
110 | | lwgeom_closest_line_3d(const LWGEOM *lw1, const LWGEOM *lw2) |
111 | 0 | { |
112 | 0 | return lw_dist3d_distanceline(lw1, lw2, lw1->srid, DIST_MIN); |
113 | 0 | } |
114 | | |
115 | | LWGEOM * |
116 | | lwgeom_furthest_line_3d(LWGEOM *lw1, LWGEOM *lw2) |
117 | 0 | { |
118 | 0 | return lw_dist3d_distanceline(lw1, lw2, lw1->srid, DIST_MAX); |
119 | 0 | } |
120 | | |
121 | | LWGEOM * |
122 | | lwgeom_closest_point_3d(const LWGEOM *lw1, const LWGEOM *lw2) |
123 | 0 | { |
124 | 0 | return lw_dist3d_distancepoint(lw1, lw2, lw1->srid, DIST_MIN); |
125 | 0 | } |
126 | | |
127 | | /** |
128 | | Function initializing 3dshortestline and 3dlongestline calculations. |
129 | | */ |
130 | | LWGEOM * |
131 | | lw_dist3d_distanceline(const LWGEOM *lw1, const LWGEOM *lw2, int32_t srid, int mode) |
132 | 0 | { |
133 | 0 | LWDEBUG(2, "lw_dist3d_distanceline is called"); |
134 | 0 | double x1, x2, y1, y2, z1, z2, x, y; |
135 | 0 | double initdistance = (mode == DIST_MIN ? DBL_MAX : -1.0); |
136 | 0 | DISTPTS3D thedl; |
137 | 0 | LWPOINT *lwpoints[2]; |
138 | 0 | LWGEOM *result; |
139 | |
|
140 | 0 | thedl.mode = mode; |
141 | 0 | thedl.distance = initdistance; |
142 | 0 | thedl.tolerance = 0.0; |
143 | | |
144 | | /* Empty operands have no closest line to measure. */ |
145 | 0 | if (lwgeom_is_empty(lw1) || lwgeom_is_empty(lw2)) |
146 | 0 | return (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
147 | | |
148 | | /* Check if we really have 3D geometries |
149 | | * If not, send it to 2D-calculations which will give the same result |
150 | | * as an infinite z-value at one or two of the geometries */ |
151 | 0 | if (!lwgeom_has_z(lw1) || !lwgeom_has_z(lw2)) |
152 | 0 | { |
153 | |
|
154 | 0 | lwnotice( |
155 | 0 | "One or both of the geometries is missing z-value. The unknown z-value will be regarded as \"any value\""); |
156 | |
|
157 | 0 | if (!lwgeom_has_z(lw1) && !lwgeom_has_z(lw2)) |
158 | 0 | return lw_dist2d_distanceline(lw1, lw2, srid, mode); |
159 | | |
160 | 0 | DISTPTS thedl2d; |
161 | 0 | thedl2d.mode = mode; |
162 | 0 | thedl2d.distance = initdistance; |
163 | 0 | thedl2d.tolerance = 0.0; |
164 | 0 | if (!lw_dist2d_comp(lw1, lw2, &thedl2d)) |
165 | 0 | { |
166 | | /* should never get here. all cases ought to be error handled earlier */ |
167 | 0 | lwerror("Some unspecified error."); |
168 | 0 | result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
169 | 0 | } |
170 | 0 | LWGEOM *vertical_line; |
171 | 0 | if (!lwgeom_has_z(lw1)) |
172 | 0 | { |
173 | 0 | x = thedl2d.p1.x; |
174 | 0 | y = thedl2d.p1.y; |
175 | |
|
176 | 0 | vertical_line = create_v_line(lw2, x, y, srid); |
177 | 0 | if (!lw_dist3d_recursive(vertical_line, lw2, &thedl)) |
178 | 0 | { |
179 | | /* should never get here. all cases ought to be error handled earlier */ |
180 | 0 | lwfree(vertical_line); |
181 | 0 | lwerror("Some unspecified error."); |
182 | 0 | result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
183 | 0 | } |
184 | 0 | lwfree(vertical_line); |
185 | 0 | } |
186 | 0 | if (!lwgeom_has_z(lw2)) |
187 | 0 | { |
188 | 0 | x = thedl2d.p2.x; |
189 | 0 | y = thedl2d.p2.y; |
190 | |
|
191 | 0 | vertical_line = create_v_line(lw1, x, y, srid); |
192 | 0 | if (!lw_dist3d_recursive(lw1, vertical_line, &thedl)) |
193 | 0 | { |
194 | | /* should never get here. all cases ought to be error handled earlier */ |
195 | 0 | lwfree(vertical_line); |
196 | 0 | lwerror("Some unspecified error."); |
197 | 0 | return (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
198 | 0 | } |
199 | 0 | lwfree(vertical_line); |
200 | 0 | } |
201 | 0 | } |
202 | 0 | else |
203 | 0 | { |
204 | 0 | if (!lw_dist3d_recursive(lw1, lw2, &thedl)) |
205 | 0 | { |
206 | | /* should never get here. all cases ought to be error handled earlier */ |
207 | 0 | lwerror("Some unspecified error."); |
208 | 0 | result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
209 | 0 | } |
210 | 0 | } |
211 | | /*if thedl.distance is unchanged there where only empty geometries input*/ |
212 | 0 | if (thedl.distance == initdistance) |
213 | 0 | { |
214 | 0 | LWDEBUG(3, "didn't find geometries to measure between, returning null"); |
215 | 0 | result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
216 | 0 | } |
217 | 0 | else |
218 | 0 | { |
219 | 0 | x1 = thedl.p1.x; |
220 | 0 | y1 = thedl.p1.y; |
221 | 0 | z1 = thedl.p1.z; |
222 | 0 | x2 = thedl.p2.x; |
223 | 0 | y2 = thedl.p2.y; |
224 | 0 | z2 = thedl.p2.z; |
225 | |
|
226 | 0 | lwpoints[0] = lwpoint_make3dz(srid, x1, y1, z1); |
227 | 0 | lwpoints[1] = lwpoint_make3dz(srid, x2, y2, z2); |
228 | |
|
229 | 0 | result = (LWGEOM *)lwline_from_ptarray(srid, 2, lwpoints); |
230 | 0 | } |
231 | |
|
232 | 0 | return result; |
233 | 0 | } |
234 | | |
235 | | /** |
236 | | Function initializing 3dclosestpoint calculations. |
237 | | */ |
238 | | LWGEOM * |
239 | | lw_dist3d_distancepoint(const LWGEOM *lw1, const LWGEOM *lw2, int32_t srid, int mode) |
240 | 0 | { |
241 | |
|
242 | 0 | double x, y, z; |
243 | 0 | DISTPTS3D thedl; |
244 | 0 | double initdistance = DBL_MAX; |
245 | 0 | LWGEOM *result; |
246 | |
|
247 | 0 | thedl.mode = mode; |
248 | 0 | thedl.distance = initdistance; |
249 | 0 | thedl.tolerance = 0; |
250 | |
|
251 | 0 | LWDEBUG(2, "lw_dist3d_distancepoint is called"); |
252 | | |
253 | | /* Empty operands have no closest point to return. */ |
254 | 0 | if (lwgeom_is_empty(lw1) || lwgeom_is_empty(lw2)) |
255 | 0 | return (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
256 | | |
257 | | /* Check if we really have 3D geometries |
258 | | * If not, send it to 2D-calculations which will give the same result |
259 | | * as an infinite z-value at one or two of the geometries |
260 | | */ |
261 | 0 | if (!lwgeom_has_z(lw1) || !lwgeom_has_z(lw2)) |
262 | 0 | { |
263 | 0 | lwnotice( |
264 | 0 | "One or both of the geometries is missing z-value. The unknown z-value will be regarded as \"any value\""); |
265 | |
|
266 | 0 | if (!lwgeom_has_z(lw1) && !lwgeom_has_z(lw2)) |
267 | 0 | return lw_dist2d_distancepoint(lw1, lw2, srid, mode); |
268 | | |
269 | 0 | DISTPTS thedl2d; |
270 | 0 | thedl2d.mode = mode; |
271 | 0 | thedl2d.distance = initdistance; |
272 | 0 | thedl2d.tolerance = 0.0; |
273 | 0 | if (!lw_dist2d_comp(lw1, lw2, &thedl2d)) |
274 | 0 | { |
275 | | /* should never get here. all cases ought to be error handled earlier */ |
276 | 0 | lwerror("Some unspecified error."); |
277 | 0 | return (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
278 | 0 | } |
279 | | |
280 | 0 | LWGEOM *vertical_line; |
281 | 0 | if (!lwgeom_has_z(lw1)) |
282 | 0 | { |
283 | 0 | x = thedl2d.p1.x; |
284 | 0 | y = thedl2d.p1.y; |
285 | |
|
286 | 0 | vertical_line = create_v_line(lw2, x, y, srid); |
287 | 0 | if (!lw_dist3d_recursive(vertical_line, lw2, &thedl)) |
288 | 0 | { |
289 | | /* should never get here. all cases ought to be error handled earlier */ |
290 | 0 | lwfree(vertical_line); |
291 | 0 | lwerror("Some unspecified error."); |
292 | 0 | return (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
293 | 0 | } |
294 | 0 | lwfree(vertical_line); |
295 | 0 | } |
296 | | |
297 | 0 | if (!lwgeom_has_z(lw2)) |
298 | 0 | { |
299 | 0 | x = thedl2d.p2.x; |
300 | 0 | y = thedl2d.p2.y; |
301 | |
|
302 | 0 | vertical_line = create_v_line(lw1, x, y, srid); |
303 | 0 | if (!lw_dist3d_recursive(lw1, vertical_line, &thedl)) |
304 | 0 | { |
305 | | /* should never get here. all cases ought to be error handled earlier */ |
306 | 0 | lwfree(vertical_line); |
307 | 0 | lwerror("Some unspecified error."); |
308 | 0 | result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
309 | 0 | } |
310 | 0 | lwfree(vertical_line); |
311 | 0 | } |
312 | 0 | } |
313 | 0 | else |
314 | 0 | { |
315 | 0 | if (!lw_dist3d_recursive(lw1, lw2, &thedl)) |
316 | 0 | { |
317 | | /* should never get here. all cases ought to be error handled earlier */ |
318 | 0 | lwerror("Some unspecified error."); |
319 | 0 | result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
320 | 0 | } |
321 | 0 | } |
322 | 0 | if (thedl.distance == initdistance) |
323 | 0 | { |
324 | 0 | LWDEBUG(3, "didn't find geometries to measure between, returning null"); |
325 | 0 | result = (LWGEOM *)lwcollection_construct_empty(COLLECTIONTYPE, srid, 0, 0); |
326 | 0 | } |
327 | 0 | else |
328 | 0 | { |
329 | 0 | x = thedl.p1.x; |
330 | 0 | y = thedl.p1.y; |
331 | 0 | z = thedl.p1.z; |
332 | 0 | result = (LWGEOM *)lwpoint_make3dz(srid, x, y, z); |
333 | 0 | } |
334 | |
|
335 | 0 | return result; |
336 | 0 | } |
337 | | |
338 | | /** |
339 | | Function initializing 3d max distance calculation |
340 | | */ |
341 | | double |
342 | | lwgeom_maxdistance3d(const LWGEOM *lw1, const LWGEOM *lw2) |
343 | 0 | { |
344 | 0 | return lwgeom_maxdistance3d_tolerance(lw1, lw2, 0.0); |
345 | 0 | } |
346 | | |
347 | | /** |
348 | | Function handling 3d max distance calculations and dfullywithin calculations. |
349 | | The difference is just the tolerance. |
350 | | */ |
351 | | double |
352 | | lwgeom_maxdistance3d_tolerance(const LWGEOM *lw1, const LWGEOM *lw2, double tolerance) |
353 | 0 | { |
354 | 0 | if (!lwgeom_has_z(lw1) || !lwgeom_has_z(lw2)) |
355 | 0 | { |
356 | 0 | lwnotice( |
357 | 0 | "One or both of the geometries is missing z-value. The unknown z-value will be regarded as \"any value\""); |
358 | 0 | return lwgeom_maxdistance2d_tolerance(lw1, lw2, tolerance); |
359 | 0 | } |
360 | 0 | DISTPTS3D thedl; |
361 | 0 | LWDEBUG(2, "lwgeom_maxdistance3d_tolerance is called"); |
362 | 0 | thedl.mode = DIST_MAX; |
363 | 0 | thedl.distance = -1; |
364 | 0 | thedl.tolerance = tolerance; |
365 | 0 | if (lw_dist3d_recursive(lw1, lw2, &thedl)) |
366 | 0 | return thedl.distance; |
367 | | |
368 | | /* should never get here. all cases ought to be error handled earlier */ |
369 | 0 | lwerror("Some unspecified error."); |
370 | 0 | return -1; |
371 | 0 | } |
372 | | |
373 | | /** |
374 | | Function initializing 3d min distance calculation |
375 | | */ |
376 | | double |
377 | | lwgeom_mindistance3d(const LWGEOM *lw1, const LWGEOM *lw2) |
378 | 0 | { |
379 | 0 | return lwgeom_mindistance3d_tolerance(lw1, lw2, 0.0); |
380 | 0 | } |
381 | | |
382 | | static inline int |
383 | | gbox_contains_3d(const GBOX *g1, const GBOX *g2) |
384 | 0 | { |
385 | 0 | return (g2->xmin >= g1->xmin) && (g2->xmax <= g1->xmax) && (g2->ymin >= g1->ymin) && (g2->ymax <= g1->ymax) && |
386 | 0 | (g2->zmin >= g1->zmin) && (g2->zmax <= g1->zmax); |
387 | 0 | } |
388 | | |
389 | | static inline int |
390 | | lwgeom_solid_contains_lwgeom(const LWGEOM *solid, const LWGEOM *g) |
391 | 0 | { |
392 | 0 | const GBOX *b1; |
393 | 0 | const GBOX *b2; |
394 | | |
395 | | /* If solid isn't solid it can't contain anything */ |
396 | 0 | if (!FLAGS_GET_SOLID(solid->flags)) |
397 | 0 | return LW_FALSE; |
398 | | |
399 | 0 | b1 = lwgeom_get_bbox(solid); |
400 | 0 | b2 = lwgeom_get_bbox(g); |
401 | | |
402 | | /* If box won't contain box, shape won't contain shape */ |
403 | 0 | if (!gbox_contains_3d(b1, b2)) |
404 | 0 | return LW_FALSE; |
405 | 0 | else /* Raycast upwards in Z */ |
406 | 0 | { |
407 | 0 | POINT4D pt; |
408 | | /* We'll skew copies if we're not lucky */ |
409 | 0 | LWGEOM *solid_copy = lwgeom_clone_deep(solid); |
410 | 0 | LWGEOM *g_copy = lwgeom_clone_deep(g); |
411 | |
|
412 | 0 | while (LW_TRUE) |
413 | 0 | { |
414 | 0 | uint8_t is_boundary = LW_FALSE; |
415 | 0 | uint8_t is_inside = LW_FALSE; |
416 | | |
417 | | /* take first point */ |
418 | 0 | if (!lwgeom_startpoint(g_copy, &pt)) |
419 | 0 | return LW_FALSE; |
420 | | |
421 | | /* get part of solid that is upwards */ |
422 | 0 | LWCOLLECTION *c = lwgeom_clip_to_ordinate_range(solid_copy, 'Z', pt.z, DBL_MAX, 0); |
423 | |
|
424 | 0 | for (uint32_t i = 0; i < c->ngeoms; i++) |
425 | 0 | { |
426 | 0 | if (c->geoms[i]->type == POLYGONTYPE) |
427 | 0 | { |
428 | | /* 3D raycast along Z is 2D point in polygon */ |
429 | 0 | int t = lwpoly_contains_point((LWPOLY *)c->geoms[i], (POINT2D *)&pt); |
430 | |
|
431 | 0 | if (t == LW_INSIDE) |
432 | 0 | is_inside = !is_inside; |
433 | 0 | else if (t == LW_BOUNDARY) |
434 | 0 | { |
435 | 0 | is_boundary = LW_TRUE; |
436 | 0 | break; |
437 | 0 | } |
438 | 0 | } |
439 | 0 | else if (c->geoms[i]->type == TRIANGLETYPE) |
440 | 0 | { |
441 | | /* 3D raycast along Z is 2D point in polygon */ |
442 | 0 | LWTRIANGLE *tri = (LWTRIANGLE *)c->geoms[i]; |
443 | 0 | int t = ptarray_contains_point(tri->points, (POINT2D *)&pt); |
444 | |
|
445 | 0 | if (t == LW_INSIDE) |
446 | 0 | is_inside = !is_inside; |
447 | 0 | else if (t == LW_BOUNDARY) |
448 | 0 | { |
449 | 0 | is_boundary = LW_TRUE; |
450 | 0 | break; |
451 | 0 | } |
452 | 0 | } |
453 | 0 | } |
454 | |
|
455 | 0 | lwcollection_free(c); |
456 | 0 | lwgeom_free(solid_copy); |
457 | 0 | lwgeom_free(g_copy); |
458 | |
|
459 | 0 | if (is_boundary) |
460 | 0 | { |
461 | | /* randomly skew a bit and restart*/ |
462 | 0 | double cx = lwrandom_uniform() - 0.5; |
463 | 0 | double cy = lwrandom_uniform() - 0.5; |
464 | 0 | AFFINE aff = {1, 0, cx, 0, 1, cy, 0, 0, 1, 0, 0, 0}; |
465 | |
|
466 | 0 | solid_copy = lwgeom_clone_deep(solid); |
467 | 0 | lwgeom_affine(solid_copy, &aff); |
468 | |
|
469 | 0 | g_copy = lwgeom_clone_deep(g); |
470 | 0 | lwgeom_affine(g_copy, &aff); |
471 | |
|
472 | 0 | continue; |
473 | 0 | } |
474 | 0 | return is_inside; |
475 | 0 | } |
476 | 0 | } |
477 | 0 | } |
478 | | |
479 | | /** |
480 | | Function handling 3d min distance calculations and dwithin calculations. |
481 | | The difference is just the tolerance. |
482 | | */ |
483 | | double |
484 | | lwgeom_mindistance3d_tolerance(const LWGEOM *lw1, const LWGEOM *lw2, double tolerance) |
485 | 0 | { |
486 | 0 | assert(tolerance >= 0); |
487 | 0 | if (!lwgeom_has_z(lw1) || !lwgeom_has_z(lw2)) |
488 | 0 | { |
489 | 0 | lwnotice( |
490 | 0 | "One or both of the geometries is missing z-value. The unknown z-value will be regarded as \"any value\""); |
491 | |
|
492 | 0 | return lwgeom_mindistance2d_tolerance(lw1, lw2, tolerance); |
493 | 0 | } |
494 | 0 | DISTPTS3D thedl; |
495 | 0 | thedl.mode = DIST_MIN; |
496 | 0 | thedl.distance = DBL_MAX; |
497 | 0 | thedl.tolerance = tolerance; |
498 | |
|
499 | 0 | if (lw_dist3d_recursive(lw1, lw2, &thedl)) |
500 | 0 | { |
501 | 0 | if (thedl.distance <= tolerance) |
502 | 0 | return thedl.distance; |
503 | 0 | if (lwgeom_solid_contains_lwgeom(lw1, lw2) || lwgeom_solid_contains_lwgeom(lw2, lw1)) |
504 | 0 | return 0; |
505 | | |
506 | 0 | return thedl.distance; |
507 | 0 | } |
508 | | |
509 | | /* should never get here. all cases ought to be error handled earlier */ |
510 | 0 | lwerror("Some unspecified error."); |
511 | 0 | return DBL_MAX; |
512 | 0 | } |
513 | | |
514 | | /*------------------------------------------------------------------------------------------------------------ |
515 | | End of Initializing functions |
516 | | --------------------------------------------------------------------------------------------------------------*/ |
517 | | |
518 | | /*------------------------------------------------------------------------------------------------------------ |
519 | | Preprocessing functions |
520 | | Functions preparing geometries for distance-calculations |
521 | | --------------------------------------------------------------------------------------------------------------*/ |
522 | | |
523 | | /** |
524 | | This is a recursive function delivering every possible combination of subgeometries |
525 | | */ |
526 | | int |
527 | | lw_dist3d_recursive(const LWGEOM *lwg1, const LWGEOM *lwg2, DISTPTS3D *dl) |
528 | 0 | { |
529 | 0 | int i, j; |
530 | 0 | int n1 = 1; |
531 | 0 | int n2 = 1; |
532 | 0 | LWGEOM *g1 = NULL; |
533 | 0 | LWGEOM *g2 = NULL; |
534 | 0 | LWCOLLECTION *c1 = NULL; |
535 | 0 | LWCOLLECTION *c2 = NULL; |
536 | |
|
537 | 0 | LWDEBUGF(2, "lw_dist3d_recursive is called with type1=%d, type2=%d", lwg1->type, lwg2->type); |
538 | |
|
539 | 0 | if (lwgeom_is_collection(lwg1)) |
540 | 0 | { |
541 | 0 | LWDEBUG(3, "First geometry is collection"); |
542 | 0 | c1 = lwgeom_as_lwcollection(lwg1); |
543 | 0 | n1 = c1->ngeoms; |
544 | 0 | } |
545 | 0 | if (lwgeom_is_collection(lwg2)) |
546 | 0 | { |
547 | 0 | LWDEBUG(3, "Second geometry is collection"); |
548 | 0 | c2 = lwgeom_as_lwcollection(lwg2); |
549 | 0 | n2 = c2->ngeoms; |
550 | 0 | } |
551 | |
|
552 | 0 | for (i = 0; i < n1; i++) |
553 | 0 | { |
554 | 0 | if (lwgeom_is_collection(lwg1)) |
555 | 0 | g1 = c1->geoms[i]; |
556 | 0 | else |
557 | 0 | g1 = (LWGEOM *)lwg1; |
558 | |
|
559 | 0 | if (lwgeom_is_empty(g1)) |
560 | 0 | continue; |
561 | | |
562 | 0 | if (lwgeom_is_collection(g1)) |
563 | 0 | { |
564 | 0 | LWDEBUG(3, "Found collection inside first geometry collection, recursing"); |
565 | 0 | if (!lw_dist3d_recursive(g1, lwg2, dl)) |
566 | 0 | return LW_FALSE; |
567 | 0 | continue; |
568 | 0 | } |
569 | 0 | for (j = 0; j < n2; j++) |
570 | 0 | { |
571 | 0 | if (lwgeom_is_collection(lwg2)) |
572 | 0 | g2 = c2->geoms[j]; |
573 | 0 | else |
574 | 0 | g2 = (LWGEOM *)lwg2; |
575 | |
|
576 | 0 | if (lwgeom_is_empty(g2)) |
577 | 0 | continue; |
578 | | |
579 | 0 | if (lwgeom_is_collection(g2)) |
580 | 0 | { |
581 | 0 | LWDEBUG(3, "Found collection inside second geometry collection, recursing"); |
582 | 0 | if (!lw_dist3d_recursive(g1, g2, dl)) |
583 | 0 | return LW_FALSE; |
584 | 0 | continue; |
585 | 0 | } |
586 | | |
587 | | /*If one of geometries is empty, return. True here only means continue searching. False would |
588 | | * have stopped the process*/ |
589 | 0 | if (lwgeom_is_empty(g1) || lwgeom_is_empty(g2)) |
590 | 0 | return LW_TRUE; |
591 | | |
592 | 0 | if (!lw_dist3d_distribute_bruteforce(g1, g2, dl)) |
593 | 0 | return LW_FALSE; |
594 | 0 | if (dl->distance <= dl->tolerance && dl->mode == DIST_MIN) |
595 | 0 | return LW_TRUE; /*just a check if the answer is already given*/ |
596 | 0 | } |
597 | 0 | } |
598 | 0 | return LW_TRUE; |
599 | 0 | } |
600 | | |
601 | | /** |
602 | | |
603 | | This function distributes the brute-force for 3D so far the only type, tasks depending on type |
604 | | */ |
605 | | int |
606 | | lw_dist3d_distribute_bruteforce(const LWGEOM *lwg1, const LWGEOM *lwg2, DISTPTS3D *dl) |
607 | 0 | { |
608 | 0 | int t1 = lwg1->type; |
609 | 0 | int t2 = lwg2->type; |
610 | |
|
611 | 0 | LWDEBUGF(2, "lw_dist3d_distribute_bruteforce is called with typ1=%d, type2=%d", lwg1->type, lwg2->type); |
612 | |
|
613 | 0 | if (t1 == POINTTYPE) |
614 | 0 | { |
615 | 0 | if (t2 == POINTTYPE) |
616 | 0 | { |
617 | 0 | dl->twisted = 1; |
618 | 0 | return lw_dist3d_point_point((LWPOINT *)lwg1, (LWPOINT *)lwg2, dl); |
619 | 0 | } |
620 | 0 | else if (t2 == LINETYPE) |
621 | 0 | { |
622 | 0 | dl->twisted = 1; |
623 | 0 | return lw_dist3d_point_line((LWPOINT *)lwg1, (LWLINE *)lwg2, dl); |
624 | 0 | } |
625 | 0 | else if (t2 == POLYGONTYPE) |
626 | 0 | { |
627 | 0 | dl->twisted = 1; |
628 | 0 | return lw_dist3d_point_poly((LWPOINT *)lwg1, (LWPOLY *)lwg2, dl); |
629 | 0 | } |
630 | 0 | else if (t2 == TRIANGLETYPE) |
631 | 0 | { |
632 | 0 | dl->twisted = 1; |
633 | 0 | return lw_dist3d_point_tri((LWPOINT *)lwg1, (LWTRIANGLE *)lwg2, dl); |
634 | 0 | } |
635 | 0 | else |
636 | 0 | { |
637 | 0 | lwerror("%s: Unsupported geometry type: %s", __func__, lwtype_name(t2)); |
638 | 0 | return LW_FALSE; |
639 | 0 | } |
640 | 0 | } |
641 | 0 | else if (t1 == LINETYPE) |
642 | 0 | { |
643 | 0 | if (t2 == POINTTYPE) |
644 | 0 | { |
645 | 0 | dl->twisted = -1; |
646 | 0 | return lw_dist3d_point_line((LWPOINT *)lwg2, (LWLINE *)lwg1, dl); |
647 | 0 | } |
648 | 0 | else if (t2 == LINETYPE) |
649 | 0 | { |
650 | 0 | dl->twisted = 1; |
651 | 0 | return lw_dist3d_line_line((LWLINE *)lwg1, (LWLINE *)lwg2, dl); |
652 | 0 | } |
653 | 0 | else if (t2 == POLYGONTYPE) |
654 | 0 | { |
655 | 0 | dl->twisted = 1; |
656 | 0 | return lw_dist3d_line_poly((LWLINE *)lwg1, (LWPOLY *)lwg2, dl); |
657 | 0 | } |
658 | 0 | else if (t2 == TRIANGLETYPE) |
659 | 0 | { |
660 | 0 | dl->twisted = 1; |
661 | 0 | return lw_dist3d_line_tri((LWLINE *)lwg1, (LWTRIANGLE *)lwg2, dl); |
662 | 0 | } |
663 | 0 | else |
664 | 0 | { |
665 | 0 | lwerror("%s: Unsupported geometry type: %s", __func__, lwtype_name(t2)); |
666 | 0 | return LW_FALSE; |
667 | 0 | } |
668 | 0 | } |
669 | 0 | else if (t1 == POLYGONTYPE) |
670 | 0 | { |
671 | 0 | if (t2 == POLYGONTYPE) |
672 | 0 | { |
673 | 0 | dl->twisted = 1; |
674 | 0 | return lw_dist3d_poly_poly((LWPOLY *)lwg1, (LWPOLY *)lwg2, dl); |
675 | 0 | } |
676 | 0 | else if (t2 == POINTTYPE) |
677 | 0 | { |
678 | 0 | dl->twisted = -1; |
679 | 0 | return lw_dist3d_point_poly((LWPOINT *)lwg2, (LWPOLY *)lwg1, dl); |
680 | 0 | } |
681 | 0 | else if (t2 == LINETYPE) |
682 | 0 | { |
683 | 0 | dl->twisted = -1; |
684 | 0 | return lw_dist3d_line_poly((LWLINE *)lwg2, (LWPOLY *)lwg1, dl); |
685 | 0 | } |
686 | 0 | else if (t2 == TRIANGLETYPE) |
687 | 0 | { |
688 | 0 | dl->twisted = 1; |
689 | 0 | return lw_dist3d_poly_tri((LWPOLY *)lwg1, (LWTRIANGLE *)lwg2, dl); |
690 | 0 | } |
691 | 0 | else |
692 | 0 | { |
693 | 0 | lwerror("%s: Unsupported geometry type: %s", __func__, lwtype_name(t2)); |
694 | 0 | return LW_FALSE; |
695 | 0 | } |
696 | 0 | } |
697 | 0 | else if (t1 == TRIANGLETYPE) |
698 | 0 | { |
699 | 0 | if (t2 == POLYGONTYPE) |
700 | 0 | { |
701 | 0 | dl->twisted = -1; |
702 | 0 | return lw_dist3d_poly_tri((LWPOLY *)lwg2, (LWTRIANGLE *)lwg1, dl); |
703 | 0 | } |
704 | 0 | else if (t2 == POINTTYPE) |
705 | 0 | { |
706 | 0 | dl->twisted = -1; |
707 | 0 | return lw_dist3d_point_tri((LWPOINT *)lwg2, (LWTRIANGLE *)lwg1, dl); |
708 | 0 | } |
709 | 0 | else if (t2 == LINETYPE) |
710 | 0 | { |
711 | 0 | dl->twisted = -1; |
712 | 0 | return lw_dist3d_line_tri((LWLINE *)lwg2, (LWTRIANGLE *)lwg1, dl); |
713 | 0 | } |
714 | 0 | else if (t2 == TRIANGLETYPE) |
715 | 0 | { |
716 | 0 | dl->twisted = 1; |
717 | 0 | return lw_dist3d_tri_tri((LWTRIANGLE *)lwg1, (LWTRIANGLE *)lwg2, dl); |
718 | 0 | } |
719 | 0 | else |
720 | 0 | { |
721 | 0 | lwerror("%s: Unsupported geometry type: %s", __func__, lwtype_name(t2)); |
722 | 0 | return LW_FALSE; |
723 | 0 | } |
724 | 0 | } |
725 | | |
726 | 0 | else |
727 | 0 | { |
728 | 0 | lwerror("%s: Unsupported geometry type: %s", __func__, lwtype_name(t1)); |
729 | 0 | return LW_FALSE; |
730 | 0 | } |
731 | 0 | } |
732 | | |
733 | | /*------------------------------------------------------------------------------------------------------------ |
734 | | End of Preprocessing functions |
735 | | --------------------------------------------------------------------------------------------------------------*/ |
736 | | |
737 | | /*------------------------------------------------------------------------------------------------------------ |
738 | | Brute force functions |
739 | | So far the only way to do 3D-calculations |
740 | | --------------------------------------------------------------------------------------------------------------*/ |
741 | | |
742 | | /** |
743 | | |
744 | | point to point calculation |
745 | | */ |
746 | | int |
747 | | lw_dist3d_point_point(const LWPOINT *point1, const LWPOINT *point2, DISTPTS3D *dl) |
748 | 0 | { |
749 | 0 | POINT3DZ p1; |
750 | 0 | POINT3DZ p2; |
751 | 0 | LWDEBUG(2, "lw_dist3d_point_point is called"); |
752 | |
|
753 | 0 | getPoint3dz_p(point1->point, 0, &p1); |
754 | 0 | getPoint3dz_p(point2->point, 0, &p2); |
755 | |
|
756 | 0 | return lw_dist3d_pt_pt(&p1, &p2, dl); |
757 | 0 | } |
758 | | /** |
759 | | |
760 | | point to line calculation |
761 | | */ |
762 | | int |
763 | | lw_dist3d_point_line(const LWPOINT *point, const LWLINE *line, DISTPTS3D *dl) |
764 | 0 | { |
765 | 0 | POINT3DZ p; |
766 | 0 | POINTARRAY *pa = line->points; |
767 | 0 | LWDEBUG(2, "lw_dist3d_point_line is called"); |
768 | |
|
769 | 0 | getPoint3dz_p(point->point, 0, &p); |
770 | 0 | return lw_dist3d_pt_ptarray(&p, pa, dl); |
771 | 0 | } |
772 | | |
773 | | /** |
774 | | |
775 | | Computes point to polygon distance |
776 | | For mindistance that means: |
777 | | 1) find the plane of the polygon |
778 | | 2) projecting the point to the plane of the polygon |
779 | | 3) finding if that projected point is inside the polygon, if so the distance is measured to that projected point |
780 | | 4) if not in polygon above, check the distance against the boundary of the polygon |
781 | | for max distance it is always point against boundary |
782 | | |
783 | | */ |
784 | | |
785 | | int |
786 | | lw_dist3d_point_poly(const LWPOINT *point, const LWPOLY *poly, DISTPTS3D *dl) |
787 | 0 | { |
788 | 0 | POINT3DZ p, projp; /*projp is "point projected on plane"*/ |
789 | 0 | PLANE3D plane; |
790 | 0 | LWDEBUG(2, "lw_dist3d_point_poly is called"); |
791 | 0 | getPoint3dz_p(point->point, 0, &p); |
792 | | |
793 | | /* If we are looking for max distance, longestline or dfullywithin */ |
794 | 0 | if (dl->mode == DIST_MAX) |
795 | 0 | return lw_dist3d_pt_ptarray(&p, poly->rings[0], dl); |
796 | | |
797 | | /* Find the plane of the polygon, the "holes" have to be on the same plane. so we only care about the boundary */ |
798 | 0 | if (!define_plane(poly->rings[0], &plane)) |
799 | 0 | { |
800 | | /* Polygon does not define a plane: Return distance point -> line */ |
801 | 0 | return lw_dist3d_pt_ptarray(&p, poly->rings[0], dl); |
802 | 0 | } |
803 | | |
804 | | /* Get our point projected on the plane of the polygon */ |
805 | 0 | project_point_on_plane(&p, &plane, &projp); |
806 | |
|
807 | 0 | return lw_dist3d_pt_poly(&p, poly, &plane, &projp, dl); |
808 | 0 | } |
809 | | |
810 | | /* point to triangle calculation */ |
811 | | int |
812 | | lw_dist3d_point_tri(const LWPOINT *point, const LWTRIANGLE *tri, DISTPTS3D *dl) |
813 | 0 | { |
814 | 0 | POINT3DZ p, projp; /*projp is "point projected on plane"*/ |
815 | 0 | PLANE3D plane; |
816 | 0 | getPoint3dz_p(point->point, 0, &p); |
817 | | |
818 | | /* If we are looking for max distance, longestline or dfullywithin */ |
819 | 0 | if (dl->mode == DIST_MAX) |
820 | 0 | return lw_dist3d_pt_ptarray(&p, tri->points, dl); |
821 | | |
822 | | /* If triangle does not define a plane, treat it as a line */ |
823 | 0 | if (!define_plane(tri->points, &plane)) |
824 | 0 | return lw_dist3d_pt_ptarray(&p, tri->points, dl); |
825 | | |
826 | | /* Get our point projected on the plane of triangle */ |
827 | 0 | project_point_on_plane(&p, &plane, &projp); |
828 | |
|
829 | 0 | return lw_dist3d_pt_tri(&p, tri, &plane, &projp, dl); |
830 | 0 | } |
831 | | |
832 | | /** line to line calculation */ |
833 | | int |
834 | | lw_dist3d_line_line(const LWLINE *line1, const LWLINE *line2, DISTPTS3D *dl) |
835 | 0 | { |
836 | 0 | POINTARRAY *pa1 = line1->points; |
837 | 0 | POINTARRAY *pa2 = line2->points; |
838 | 0 | LWDEBUG(2, "lw_dist3d_line_line is called"); |
839 | |
|
840 | 0 | return lw_dist3d_ptarray_ptarray(pa1, pa2, dl); |
841 | 0 | } |
842 | | |
843 | | /** line to polygon calculation */ |
844 | | int |
845 | | lw_dist3d_line_poly(const LWLINE *line, const LWPOLY *poly, DISTPTS3D *dl) |
846 | 0 | { |
847 | 0 | PLANE3D plane; |
848 | 0 | LWDEBUG(2, "lw_dist3d_line_poly is called"); |
849 | |
|
850 | 0 | if (dl->mode == DIST_MAX) |
851 | 0 | return lw_dist3d_ptarray_ptarray(line->points, poly->rings[0], dl); |
852 | | |
853 | | /* if polygon does not define a plane: Return distance line to line */ |
854 | 0 | if (!define_plane(poly->rings[0], &plane)) |
855 | 0 | return lw_dist3d_ptarray_ptarray(line->points, poly->rings[0], dl); |
856 | | |
857 | 0 | return lw_dist3d_ptarray_poly(line->points, poly, &plane, dl); |
858 | 0 | } |
859 | | |
860 | | /** line to triangle calculation */ |
861 | | int |
862 | | lw_dist3d_line_tri(const LWLINE *line, const LWTRIANGLE *tri, DISTPTS3D *dl) |
863 | 0 | { |
864 | 0 | PLANE3D plane; |
865 | |
|
866 | 0 | if (dl->mode == DIST_MAX) |
867 | 0 | return lw_dist3d_ptarray_ptarray(line->points, tri->points, dl); |
868 | | |
869 | | /* if triangle does not define a plane: Return distance line to line */ |
870 | 0 | if (!define_plane(tri->points, &plane)) |
871 | 0 | return lw_dist3d_ptarray_ptarray(line->points, tri->points, dl); |
872 | | |
873 | 0 | return lw_dist3d_ptarray_tri(line->points, tri, &plane, dl); |
874 | 0 | } |
875 | | |
876 | | /** polygon to polygon calculation */ |
877 | | int |
878 | | lw_dist3d_poly_poly(const LWPOLY *poly1, const LWPOLY *poly2, DISTPTS3D *dl) |
879 | 0 | { |
880 | 0 | PLANE3D plane1, plane2; |
881 | 0 | int planedef1, planedef2; |
882 | 0 | LWDEBUG(2, "lw_dist3d_poly_poly is called"); |
883 | 0 | if (dl->mode == DIST_MAX) |
884 | 0 | return lw_dist3d_ptarray_ptarray(poly1->rings[0], poly2->rings[0], dl); |
885 | | |
886 | 0 | planedef1 = define_plane(poly1->rings[0], &plane1); |
887 | 0 | planedef2 = define_plane(poly2->rings[0], &plane2); |
888 | |
|
889 | 0 | if (!planedef1 || !planedef2) |
890 | 0 | { |
891 | | /* Neither polygon define a plane: Return distance line to line */ |
892 | 0 | if (!planedef1 && !planedef2) |
893 | 0 | return lw_dist3d_ptarray_ptarray(poly1->rings[0], poly2->rings[0], dl); |
894 | | |
895 | | /* Only poly2 defines a plane: Return distance from line (poly1) to poly2 */ |
896 | 0 | else if (!planedef1) |
897 | 0 | return lw_dist3d_ptarray_poly(poly1->rings[0], poly2, &plane2, dl); |
898 | | |
899 | | /* Only poly1 defines a plane: Return distance from line (poly2) to poly1 */ |
900 | 0 | else |
901 | 0 | return lw_dist3d_ptarray_poly(poly2->rings[0], poly1, &plane1, dl); |
902 | 0 | } |
903 | | |
904 | | /* What we do here is to compare the boundary of one polygon with the other polygon |
905 | | and then take the second boundary comparing with the first polygon */ |
906 | 0 | dl->twisted = 1; |
907 | 0 | if (!lw_dist3d_ptarray_poly(poly1->rings[0], poly2, &plane2, dl)) |
908 | 0 | return LW_FALSE; |
909 | 0 | if (dl->distance < dl->tolerance) /* Just check if the answer already is given*/ |
910 | 0 | return LW_TRUE; |
911 | | |
912 | 0 | dl->twisted = -1; /* because we switch the order of geometries we switch "twisted" to -1 which will give the |
913 | | right order of points in shortest line. */ |
914 | 0 | return lw_dist3d_ptarray_poly(poly2->rings[0], poly1, &plane1, dl); |
915 | 0 | } |
916 | | |
917 | | /** polygon to triangle calculation */ |
918 | | int |
919 | | lw_dist3d_poly_tri(const LWPOLY *poly, const LWTRIANGLE *tri, DISTPTS3D *dl) |
920 | 0 | { |
921 | 0 | PLANE3D plane1, plane2; |
922 | 0 | int planedef1, planedef2; |
923 | |
|
924 | 0 | if (dl->mode == DIST_MAX) |
925 | 0 | return lw_dist3d_ptarray_ptarray(poly->rings[0], tri->points, dl); |
926 | | |
927 | 0 | planedef1 = define_plane(poly->rings[0], &plane1); |
928 | 0 | planedef2 = define_plane(tri->points, &plane2); |
929 | |
|
930 | 0 | if (!planedef1 || !planedef2) |
931 | 0 | { |
932 | | /* Neither define a plane: Return distance line to line */ |
933 | 0 | if (!planedef1 && !planedef2) |
934 | 0 | return lw_dist3d_ptarray_ptarray(poly->rings[0], tri->points, dl); |
935 | | |
936 | | /* Only tri defines a plane: Return distance from line (poly) to tri */ |
937 | 0 | else if (!planedef1) |
938 | 0 | return lw_dist3d_ptarray_tri(poly->rings[0], tri, &plane2, dl); |
939 | | |
940 | | /* Only poly defines a plane: Return distance from line (tri) to poly */ |
941 | 0 | else |
942 | 0 | return lw_dist3d_ptarray_poly(tri->points, poly, &plane1, dl); |
943 | 0 | } |
944 | | |
945 | | /* What we do here is to compare the boundary of one polygon with the other polygon |
946 | | and then take the second boundary comparing with the first polygon */ |
947 | 0 | dl->twisted = 1; |
948 | 0 | if (!lw_dist3d_ptarray_tri(poly->rings[0], tri, &plane2, dl)) |
949 | 0 | return LW_FALSE; |
950 | 0 | if (dl->distance < dl->tolerance) /* Just check if the answer already is given*/ |
951 | 0 | return LW_TRUE; |
952 | | |
953 | 0 | dl->twisted = -1; /* because we switch the order of geometries we switch "twisted" to -1 which will give the |
954 | | right order of points in shortest line. */ |
955 | 0 | return lw_dist3d_ptarray_poly(tri->points, poly, &plane1, dl); |
956 | 0 | } |
957 | | |
958 | | /** triangle to triangle calculation */ |
959 | | int |
960 | | lw_dist3d_tri_tri(const LWTRIANGLE *tri1, const LWTRIANGLE *tri2, DISTPTS3D *dl) |
961 | 0 | { |
962 | 0 | PLANE3D plane1, plane2; |
963 | 0 | int planedef1, planedef2; |
964 | |
|
965 | 0 | if (dl->mode == DIST_MAX) |
966 | 0 | return lw_dist3d_ptarray_ptarray(tri1->points, tri2->points, dl); |
967 | | |
968 | 0 | planedef1 = define_plane(tri1->points, &plane1); |
969 | 0 | planedef2 = define_plane(tri2->points, &plane2); |
970 | |
|
971 | 0 | if (!planedef1 || !planedef2) |
972 | 0 | { |
973 | | /* Neither define a plane: Return distance line to line */ |
974 | 0 | if (!planedef1 && !planedef2) |
975 | 0 | return lw_dist3d_ptarray_ptarray(tri1->points, tri2->points, dl); |
976 | | |
977 | | /* Only tri defines a plane: Return distance from line (tri1) to tri2 */ |
978 | 0 | else if (!planedef1) |
979 | 0 | return lw_dist3d_ptarray_tri(tri1->points, tri2, &plane2, dl); |
980 | | |
981 | | /* Only poly defines a plane: Return distance from line (tri2) to tri1 */ |
982 | 0 | else |
983 | 0 | return lw_dist3d_ptarray_tri(tri2->points, tri1, &plane1, dl); |
984 | 0 | } |
985 | | |
986 | | /* What we do here is to compare the boundary of one polygon with the other polygon |
987 | | and then take the second boundary comparing with the first polygon */ |
988 | 0 | dl->twisted = 1; |
989 | 0 | if (!lw_dist3d_ptarray_tri(tri1->points, tri2, &plane2, dl)) |
990 | 0 | return LW_FALSE; |
991 | 0 | if (dl->distance < dl->tolerance) /* Just check if the answer already is given*/ |
992 | 0 | return LW_TRUE; |
993 | | |
994 | 0 | dl->twisted = -1; /* because we switch the order of geometries we switch "twisted" to -1 which will give the |
995 | | right order of points in shortest line. */ |
996 | 0 | return lw_dist3d_ptarray_tri(tri2->points, tri1, &plane1, dl); |
997 | 0 | } |
998 | | |
999 | | /** |
1000 | | * search all the segments of pointarray to see which one is closest to p |
1001 | | * Returns distance between point and pointarray |
1002 | | */ |
1003 | | int |
1004 | | lw_dist3d_pt_ptarray(const POINT3DZ *p, const POINTARRAY *pa, DISTPTS3D *dl) |
1005 | 0 | { |
1006 | 0 | uint32_t t; |
1007 | 0 | POINT3DZ start, end; |
1008 | 0 | int twist = dl->twisted; |
1009 | 0 | if (!pa) |
1010 | 0 | return LW_FALSE; |
1011 | | |
1012 | 0 | getPoint3dz_p(pa, 0, &start); |
1013 | |
|
1014 | 0 | for (t = 1; t < pa->npoints; t++) |
1015 | 0 | { |
1016 | 0 | dl->twisted = twist; |
1017 | 0 | getPoint3dz_p(pa, t, &end); |
1018 | 0 | if (!lw_dist3d_pt_seg(p, &start, &end, dl)) |
1019 | 0 | return LW_FALSE; |
1020 | | |
1021 | 0 | if (dl->distance <= dl->tolerance && dl->mode == DIST_MIN) |
1022 | 0 | return LW_TRUE; /*just a check if the answer is already given*/ |
1023 | 0 | start = end; |
1024 | 0 | } |
1025 | | |
1026 | 0 | return LW_TRUE; |
1027 | 0 | } |
1028 | | |
1029 | | /** |
1030 | | If searching for min distance, this one finds the closest point on segment A-B from p. |
1031 | | if searching for max distance it just sends p-A and p-B to pt-pt calculation |
1032 | | */ |
1033 | | int |
1034 | | lw_dist3d_pt_seg(const POINT3DZ *p, const POINT3DZ *A, const POINT3DZ *B, DISTPTS3D *dl) |
1035 | 0 | { |
1036 | 0 | POINT3DZ c; |
1037 | 0 | double r; |
1038 | | /*if start==end, then use pt distance */ |
1039 | 0 | if ((A->x == B->x) && (A->y == B->y) && (A->z == B->z)) |
1040 | 0 | { |
1041 | 0 | return lw_dist3d_pt_pt(p, A, dl); |
1042 | 0 | } |
1043 | | |
1044 | 0 | r = ((p->x - A->x) * (B->x - A->x) + (p->y - A->y) * (B->y - A->y) + (p->z - A->z) * (B->z - A->z)) / |
1045 | 0 | ((B->x - A->x) * (B->x - A->x) + (B->y - A->y) * (B->y - A->y) + (B->z - A->z) * (B->z - A->z)); |
1046 | | |
1047 | | /*This is for finding the 3Dmaxdistance. |
1048 | | the maxdistance have to be between two vertices, |
1049 | | compared to mindistance which can be between |
1050 | | two vertices vertex.*/ |
1051 | 0 | if (dl->mode == DIST_MAX) |
1052 | 0 | { |
1053 | 0 | if (r >= 0.5) |
1054 | 0 | return lw_dist3d_pt_pt(p, A, dl); |
1055 | 0 | if (r < 0.5) |
1056 | 0 | return lw_dist3d_pt_pt(p, B, dl); |
1057 | 0 | } |
1058 | | |
1059 | 0 | if (r <= 0) /*If the first vertex A is closest to the point p*/ |
1060 | 0 | return lw_dist3d_pt_pt(p, A, dl); |
1061 | 0 | if (r >= 1) /*If the second vertex B is closest to the point p*/ |
1062 | 0 | return lw_dist3d_pt_pt(p, B, dl); |
1063 | | |
1064 | | /*else if the point p is closer to some point between a and b |
1065 | | then we find that point and send it to lw_dist3d_pt_pt*/ |
1066 | 0 | c.x = A->x + r * (B->x - A->x); |
1067 | 0 | c.y = A->y + r * (B->y - A->y); |
1068 | 0 | c.z = A->z + r * (B->z - A->z); |
1069 | |
|
1070 | 0 | return lw_dist3d_pt_pt(p, &c, dl); |
1071 | 0 | } |
1072 | | |
1073 | | double |
1074 | | distance3d_pt_pt(const POINT3D *p1, const POINT3D *p2) |
1075 | 0 | { |
1076 | 0 | double dx = p2->x - p1->x; |
1077 | 0 | double dy = p2->y - p1->y; |
1078 | 0 | double dz = p2->z - p1->z; |
1079 | 0 | return sqrt(dx * dx + dy * dy + dz * dz); |
1080 | 0 | } |
1081 | | |
1082 | | /** |
1083 | | |
1084 | | Compares incoming points and |
1085 | | stores the points closest to each other |
1086 | | or most far away from each other |
1087 | | depending on dl->mode (max or min) |
1088 | | */ |
1089 | | int |
1090 | | lw_dist3d_pt_pt(const POINT3DZ *thep1, const POINT3DZ *thep2, DISTPTS3D *dl) |
1091 | 0 | { |
1092 | 0 | double dx = thep2->x - thep1->x; |
1093 | 0 | double dy = thep2->y - thep1->y; |
1094 | 0 | double dz = thep2->z - thep1->z; |
1095 | 0 | double dist = sqrt(dx * dx + dy * dy + dz * dz); |
1096 | 0 | LWDEBUGF(2, |
1097 | 0 | "lw_dist3d_pt_pt called (with points: p1.x=%f, p1.y=%f,p1.z=%f,p2.x=%f, p2.y=%f,p2.z=%f)", |
1098 | 0 | thep1->x, |
1099 | 0 | thep1->y, |
1100 | 0 | thep1->z, |
1101 | 0 | thep2->x, |
1102 | 0 | thep2->y, |
1103 | 0 | thep2->z); |
1104 | |
|
1105 | 0 | if (((dl->distance - dist) * (dl->mode)) > |
1106 | 0 | 0) /*multiplication with mode to handle mindistance (mode=1) and maxdistance (mode = (-1)*/ |
1107 | 0 | { |
1108 | 0 | dl->distance = dist; |
1109 | |
|
1110 | 0 | if (dl->twisted > 0) /*To get the points in right order. twisted is updated between 1 and (-1) every |
1111 | | time the order is changed earlier in the chain*/ |
1112 | 0 | { |
1113 | 0 | dl->p1 = *thep1; |
1114 | 0 | dl->p2 = *thep2; |
1115 | 0 | } |
1116 | 0 | else |
1117 | 0 | { |
1118 | 0 | dl->p1 = *thep2; |
1119 | 0 | dl->p2 = *thep1; |
1120 | 0 | } |
1121 | 0 | } |
1122 | 0 | return LW_TRUE; |
1123 | 0 | } |
1124 | | |
1125 | | /** |
1126 | | |
1127 | | Finds all combinations of segments between two pointarrays |
1128 | | */ |
1129 | | int |
1130 | | lw_dist3d_ptarray_ptarray(const POINTARRAY *l1, const POINTARRAY *l2, DISTPTS3D *dl) |
1131 | 0 | { |
1132 | 0 | uint32_t t, u; |
1133 | 0 | POINT3DZ start, end; |
1134 | 0 | POINT3DZ start2, end2; |
1135 | 0 | int twist = dl->twisted; |
1136 | 0 | LWDEBUGF(2, "lw_dist3d_ptarray_ptarray called (points: %d-%d)", l1->npoints, l2->npoints); |
1137 | |
|
1138 | 0 | if (dl->mode == DIST_MAX) /*If we are searching for maxdistance we go straight to point-point calculation since |
1139 | | the maxdistance have to be between two vertices*/ |
1140 | 0 | { |
1141 | 0 | for (t = 0; t < l1->npoints; t++) /*for each segment in L1 */ |
1142 | 0 | { |
1143 | 0 | getPoint3dz_p(l1, t, &start); |
1144 | 0 | for (u = 0; u < l2->npoints; u++) /*for each segment in L2 */ |
1145 | 0 | { |
1146 | 0 | getPoint3dz_p(l2, u, &start2); |
1147 | 0 | lw_dist3d_pt_pt(&start, &start2, dl); |
1148 | 0 | } |
1149 | 0 | } |
1150 | 0 | } |
1151 | 0 | else |
1152 | 0 | { |
1153 | 0 | getPoint3dz_p(l1, 0, &start); |
1154 | 0 | for (t = 1; t < l1->npoints; t++) /*for each segment in L1 */ |
1155 | 0 | { |
1156 | 0 | getPoint3dz_p(l1, t, &end); |
1157 | 0 | getPoint3dz_p(l2, 0, &start2); |
1158 | 0 | for (u = 1; u < l2->npoints; u++) /*for each segment in L2 */ |
1159 | 0 | { |
1160 | 0 | getPoint3dz_p(l2, u, &end2); |
1161 | 0 | dl->twisted = twist; |
1162 | 0 | lw_dist3d_seg_seg(&start, &end, &start2, &end2, dl); |
1163 | 0 | LWDEBUGF( |
1164 | 0 | 4, "mindist_ptarray_ptarray; seg %i * seg %i, dist = %g\n", t, u, dl->distance); |
1165 | 0 | LWDEBUGF(3, " seg%d-seg%d dist: %f, mindist: %f", t, u, dl->distance, dl->tolerance); |
1166 | 0 | if (dl->distance <= dl->tolerance && dl->mode == DIST_MIN) |
1167 | 0 | return LW_TRUE; /*just a check if the answer is already given*/ |
1168 | 0 | start2 = end2; |
1169 | 0 | } |
1170 | 0 | start = end; |
1171 | 0 | } |
1172 | 0 | } |
1173 | 0 | return LW_TRUE; |
1174 | 0 | } |
1175 | | |
1176 | | /** |
1177 | | |
1178 | | Finds the two closest points on two linesegments |
1179 | | */ |
1180 | | int |
1181 | | lw_dist3d_seg_seg(const POINT3DZ *s1p1, const POINT3DZ *s1p2, const POINT3DZ *s2p1, const POINT3DZ *s2p2, DISTPTS3D *dl) |
1182 | 0 | { |
1183 | 0 | VECTOR3D v1, v2, vl; |
1184 | 0 | double s1k, s2k; /*two variables representing where on Line 1 (s1k) and where on Line 2 (s2k) a connecting line |
1185 | | between the two lines is perpendicular to both lines*/ |
1186 | 0 | POINT3DZ p1, p2; |
1187 | 0 | double a, b, c, d, e, D; |
1188 | | |
1189 | | /*s1p1 and s1p2 are the same point */ |
1190 | 0 | if (p3dz_same(s1p1, s1p2)) |
1191 | 0 | { |
1192 | 0 | return lw_dist3d_pt_seg(s1p1, s2p1, s2p2, dl); |
1193 | 0 | } |
1194 | | /*s2p1 and s2p2 are the same point */ |
1195 | 0 | if (p3dz_same(s2p1, s2p2)) |
1196 | 0 | { |
1197 | 0 | dl->twisted = ((dl->twisted) * (-1)); |
1198 | 0 | return lw_dist3d_pt_seg(s2p1, s1p1, s1p2, dl); |
1199 | 0 | } |
1200 | | /*s2p1 and s1p1 are the same point */ |
1201 | 0 | if (p3dz_same(s2p1, s1p1)) |
1202 | 0 | { |
1203 | 0 | dl->distance = 0.0; |
1204 | 0 | dl->p1 = dl->p2 = *s2p1; |
1205 | 0 | return LW_TRUE; |
1206 | 0 | } |
1207 | | /* |
1208 | | Here we use algorithm from softsurfer.com |
1209 | | that can be found here |
1210 | | http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm |
1211 | | */ |
1212 | | |
1213 | 0 | if (!get_3dvector_from_points(s1p1, s1p2, &v1)) |
1214 | 0 | return LW_FALSE; |
1215 | | |
1216 | 0 | if (!get_3dvector_from_points(s2p1, s2p2, &v2)) |
1217 | 0 | return LW_FALSE; |
1218 | | |
1219 | 0 | if (!get_3dvector_from_points(s2p1, s1p1, &vl)) |
1220 | 0 | return LW_FALSE; |
1221 | | |
1222 | 0 | a = DOT(v1, v1); |
1223 | 0 | b = DOT(v1, v2); |
1224 | 0 | c = DOT(v2, v2); |
1225 | 0 | d = DOT(v1, vl); |
1226 | 0 | e = DOT(v2, vl); |
1227 | 0 | D = a * c - b * b; |
1228 | |
|
1229 | 0 | if (D < 0.000000001) |
1230 | 0 | { /* the lines are almost parallel*/ |
1231 | 0 | s1k = |
1232 | 0 | 0.0; /*If the lines are parallel we try by using the startpoint of first segment. If that gives a |
1233 | | projected point on the second line outside segment 2 it will be found that s2k is >1 or <0.*/ |
1234 | 0 | if (b > c) /* use the largest denominator*/ |
1235 | 0 | s2k = d / b; |
1236 | 0 | else |
1237 | 0 | s2k = e / c; |
1238 | 0 | } |
1239 | 0 | else |
1240 | 0 | { |
1241 | 0 | s1k = (b * e - c * d) / D; |
1242 | 0 | s2k = (a * e - b * d) / D; |
1243 | 0 | } |
1244 | | |
1245 | | /* Now we check if the projected closest point on the infinite lines is outside our segments. If so the |
1246 | | * combinations with start and end points will be tested*/ |
1247 | |
|
1248 | 0 | if (s1k <= 0.0 || s1k >= 1.0 || s2k <= 0.0 || s2k >= 1.0) |
1249 | 0 | { |
1250 | 0 | if (s1k <= 0.0) |
1251 | 0 | { |
1252 | 0 | if (!lw_dist3d_pt_seg(s1p1, s2p1, s2p2, dl)) |
1253 | 0 | return LW_FALSE; |
1254 | 0 | } |
1255 | 0 | if (s1k >= 1.0) |
1256 | 0 | { |
1257 | 0 | if (!lw_dist3d_pt_seg(s1p2, s2p1, s2p2, dl)) |
1258 | 0 | return LW_FALSE; |
1259 | 0 | } |
1260 | 0 | if (s2k <= 0.0) |
1261 | 0 | { |
1262 | 0 | dl->twisted = ((dl->twisted) * (-1)); |
1263 | 0 | if (!lw_dist3d_pt_seg(s2p1, s1p1, s1p2, dl)) |
1264 | 0 | return LW_FALSE; |
1265 | 0 | } |
1266 | 0 | if (s2k >= 1.0) |
1267 | 0 | { |
1268 | 0 | dl->twisted = ((dl->twisted) * (-1)); |
1269 | 0 | if (!lw_dist3d_pt_seg(s2p2, s1p1, s1p2, dl)) |
1270 | 0 | return LW_FALSE; |
1271 | 0 | } |
1272 | 0 | } |
1273 | 0 | else |
1274 | 0 | { /*Find the closest point on the edges of both segments*/ |
1275 | 0 | p1.x = s1p1->x + s1k * (s1p2->x - s1p1->x); |
1276 | 0 | p1.y = s1p1->y + s1k * (s1p2->y - s1p1->y); |
1277 | 0 | p1.z = s1p1->z + s1k * (s1p2->z - s1p1->z); |
1278 | |
|
1279 | 0 | p2.x = s2p1->x + s2k * (s2p2->x - s2p1->x); |
1280 | 0 | p2.y = s2p1->y + s2k * (s2p2->y - s2p1->y); |
1281 | 0 | p2.z = s2p1->z + s2k * (s2p2->z - s2p1->z); |
1282 | |
|
1283 | 0 | if (!lw_dist3d_pt_pt(&p1, &p2, dl)) /* Send the closest points to point-point calculation*/ |
1284 | 0 | { |
1285 | 0 | return LW_FALSE; |
1286 | 0 | } |
1287 | 0 | } |
1288 | 0 | return LW_TRUE; |
1289 | 0 | } |
1290 | | |
1291 | | /** |
1292 | | |
1293 | | Checking if the point projected on the plane of the polygon actually is inside that polygon. |
1294 | | If so the mindistance is between that projected point and our original point. |
1295 | | If not we check from original point to the boundary. |
1296 | | If the projected point is inside a hole of the polygon we check the distance to the boundary of that hole. |
1297 | | */ |
1298 | | int |
1299 | | lw_dist3d_pt_poly(const POINT3DZ *p, const LWPOLY *poly, PLANE3D *plane, POINT3DZ *projp, DISTPTS3D *dl) |
1300 | 0 | { |
1301 | 0 | uint32_t i; |
1302 | |
|
1303 | 0 | if (pt_in_ring_3d(projp, poly->rings[0], plane)) |
1304 | 0 | { |
1305 | 0 | for (i = 1; i < poly->nrings; i++) |
1306 | 0 | { |
1307 | | /* Inside a hole. Distance = pt -> ring */ |
1308 | 0 | if (pt_in_ring_3d(projp, poly->rings[i], plane)) |
1309 | 0 | return lw_dist3d_pt_ptarray(p, poly->rings[i], dl); |
1310 | 0 | } |
1311 | | |
1312 | | /* if the projected point is inside the polygon the shortest distance is between that point and the |
1313 | | * input point */ |
1314 | 0 | return lw_dist3d_pt_pt(p, projp, dl); |
1315 | 0 | } |
1316 | 0 | else |
1317 | | /* if the projected point is outside the polygon we search for the closest distance against the boundary |
1318 | | * instead */ |
1319 | 0 | return lw_dist3d_pt_ptarray(p, poly->rings[0], dl); |
1320 | | |
1321 | 0 | return LW_TRUE; |
1322 | 0 | } |
1323 | | |
1324 | | int |
1325 | | lw_dist3d_pt_tri(const POINT3DZ *p, const LWTRIANGLE *tri, PLANE3D *plane, POINT3DZ *projp, DISTPTS3D *dl) |
1326 | 0 | { |
1327 | 0 | if (pt_in_ring_3d(projp, tri->points, plane)) |
1328 | | /* if the projected point is inside the polygon the shortest distance is between that point and the |
1329 | | * input point */ |
1330 | 0 | return lw_dist3d_pt_pt(p, projp, dl); |
1331 | 0 | else |
1332 | | /* if the projected point is outside the polygon we search for the closest distance against the boundary |
1333 | | * instead */ |
1334 | 0 | return lw_dist3d_pt_ptarray(p, tri->points, dl); |
1335 | | |
1336 | 0 | return LW_TRUE; |
1337 | 0 | } |
1338 | | |
1339 | | /** Computes pointarray to polygon distance */ |
1340 | | int |
1341 | | lw_dist3d_ptarray_poly(const POINTARRAY *pa, const LWPOLY *poly, PLANE3D *plane, DISTPTS3D *dl) |
1342 | 0 | { |
1343 | 0 | uint32_t i, j, k; |
1344 | 0 | double f, s1, s2; |
1345 | 0 | VECTOR3D projp1_projp2; |
1346 | 0 | POINT3DZ p1, p2, projp1, projp2, intersectionp; |
1347 | |
|
1348 | 0 | getPoint3dz_p(pa, 0, &p1); |
1349 | | |
1350 | | /* the sign of s1 tells us on which side of the plane the point is. */ |
1351 | 0 | s1 = project_point_on_plane(&p1, plane, &projp1); |
1352 | 0 | lw_dist3d_pt_poly(&p1, poly, plane, &projp1, dl); |
1353 | 0 | if ((s1 == 0.0) && (dl->distance < dl->tolerance)) |
1354 | 0 | return LW_TRUE; |
1355 | | |
1356 | 0 | for (i = 1; i < pa->npoints; i++) |
1357 | 0 | { |
1358 | 0 | int intersects; |
1359 | 0 | getPoint3dz_p(pa, i, &p2); |
1360 | 0 | s2 = project_point_on_plane(&p2, plane, &projp2); |
1361 | 0 | lw_dist3d_pt_poly(&p2, poly, plane, &projp2, dl); |
1362 | 0 | if ((s2 == 0.0) && (dl->distance < dl->tolerance)) |
1363 | 0 | return LW_TRUE; |
1364 | | |
1365 | | /* If s1 and s2 has different signs that means they are on different sides of the plane of the polygon. |
1366 | | * That means that the edge between the points crosses the plane and might intersect with the polygon */ |
1367 | 0 | if ((s1 * s2) < 0) |
1368 | 0 | { |
1369 | | /* The size of s1 and s2 is the distance from the point to the plane. */ |
1370 | 0 | f = fabs(s1) / (fabs(s1) + fabs(s2)); |
1371 | 0 | get_3dvector_from_points(&projp1, &projp2, &projp1_projp2); |
1372 | | |
1373 | | /* Get the point where the line segment crosses the plane */ |
1374 | 0 | intersectionp.x = projp1.x + f * projp1_projp2.x; |
1375 | 0 | intersectionp.y = projp1.y + f * projp1_projp2.y; |
1376 | 0 | intersectionp.z = projp1.z + f * projp1_projp2.z; |
1377 | | |
1378 | | /* We set intersects to true until the opposite is proved */ |
1379 | 0 | intersects = LW_TRUE; |
1380 | |
|
1381 | 0 | if (pt_in_ring_3d(&intersectionp, poly->rings[0], plane)) /*Inside outer ring*/ |
1382 | 0 | { |
1383 | 0 | for (k = 1; k < poly->nrings; k++) |
1384 | 0 | { |
1385 | | /* Inside a hole, so no intersection with the polygon*/ |
1386 | 0 | if (pt_in_ring_3d(&intersectionp, poly->rings[k], plane)) |
1387 | 0 | { |
1388 | 0 | intersects = LW_FALSE; |
1389 | 0 | break; |
1390 | 0 | } |
1391 | 0 | } |
1392 | 0 | if (intersects) |
1393 | 0 | { |
1394 | 0 | dl->distance = 0.0; |
1395 | 0 | dl->p1.x = intersectionp.x; |
1396 | 0 | dl->p1.y = intersectionp.y; |
1397 | 0 | dl->p1.z = intersectionp.z; |
1398 | |
|
1399 | 0 | dl->p2.x = intersectionp.x; |
1400 | 0 | dl->p2.y = intersectionp.y; |
1401 | 0 | dl->p2.z = intersectionp.z; |
1402 | 0 | return LW_TRUE; |
1403 | 0 | } |
1404 | 0 | } |
1405 | 0 | } |
1406 | | |
1407 | 0 | projp1 = projp2; |
1408 | 0 | s1 = s2; |
1409 | 0 | p1 = p2; |
1410 | 0 | } |
1411 | | |
1412 | | /* check our pointarray against boundary and inner boundaries of the polygon */ |
1413 | 0 | for (j = 0; j < poly->nrings; j++) |
1414 | 0 | lw_dist3d_ptarray_ptarray(pa, poly->rings[j], dl); |
1415 | |
|
1416 | 0 | return LW_TRUE; |
1417 | 0 | } |
1418 | | |
1419 | | /** Computes pointarray to triangle distance */ |
1420 | | int |
1421 | | lw_dist3d_ptarray_tri(const POINTARRAY *pa, const LWTRIANGLE *tri, PLANE3D *plane, DISTPTS3D *dl) |
1422 | 0 | { |
1423 | 0 | uint32_t i; |
1424 | 0 | double f, s1, s2; |
1425 | 0 | VECTOR3D projp1_projp2; |
1426 | 0 | POINT3DZ p1, p2, projp1, projp2, intersectionp; |
1427 | |
|
1428 | 0 | getPoint3dz_p(pa, 0, &p1); |
1429 | | |
1430 | | /* the sign of s1 tells us on which side of the plane the point is. */ |
1431 | 0 | s1 = project_point_on_plane(&p1, plane, &projp1); |
1432 | 0 | lw_dist3d_pt_tri(&p1, tri, plane, &projp1, dl); |
1433 | 0 | if ((s1 == 0.0) && (dl->distance < dl->tolerance)) |
1434 | 0 | return LW_TRUE; |
1435 | | |
1436 | 0 | for (i = 1; i < pa->npoints; i++) |
1437 | 0 | { |
1438 | 0 | int intersects; |
1439 | 0 | getPoint3dz_p(pa, i, &p2); |
1440 | 0 | s2 = project_point_on_plane(&p2, plane, &projp2); |
1441 | 0 | lw_dist3d_pt_tri(&p2, tri, plane, &projp2, dl); |
1442 | 0 | if ((s2 == 0.0) && (dl->distance < dl->tolerance)) |
1443 | 0 | return LW_TRUE; |
1444 | | |
1445 | | /* If s1 and s2 has different signs that means they are on different sides of the plane of the triangle. |
1446 | | * That means that the edge between the points crosses the plane and might intersect with the triangle |
1447 | | */ |
1448 | 0 | if ((s1 * s2) < 0) |
1449 | 0 | { |
1450 | | /* The size of s1 and s2 is the distance from the point to the plane. */ |
1451 | 0 | f = fabs(s1) / (fabs(s1) + fabs(s2)); |
1452 | 0 | get_3dvector_from_points(&projp1, &projp2, &projp1_projp2); |
1453 | | |
1454 | | /* Get the point where the line segment crosses the plane */ |
1455 | 0 | intersectionp.x = projp1.x + f * projp1_projp2.x; |
1456 | 0 | intersectionp.y = projp1.y + f * projp1_projp2.y; |
1457 | 0 | intersectionp.z = projp1.z + f * projp1_projp2.z; |
1458 | | |
1459 | | /* We set intersects to true until the opposite is proved */ |
1460 | 0 | intersects = LW_TRUE; |
1461 | |
|
1462 | 0 | if (pt_in_ring_3d(&intersectionp, tri->points, plane)) /*Inside outer ring*/ |
1463 | 0 | { |
1464 | 0 | if (intersects) |
1465 | 0 | { |
1466 | 0 | dl->distance = 0.0; |
1467 | 0 | dl->p1.x = intersectionp.x; |
1468 | 0 | dl->p1.y = intersectionp.y; |
1469 | 0 | dl->p1.z = intersectionp.z; |
1470 | |
|
1471 | 0 | dl->p2.x = intersectionp.x; |
1472 | 0 | dl->p2.y = intersectionp.y; |
1473 | 0 | dl->p2.z = intersectionp.z; |
1474 | 0 | return LW_TRUE; |
1475 | 0 | } |
1476 | 0 | } |
1477 | 0 | } |
1478 | | |
1479 | 0 | projp1 = projp2; |
1480 | 0 | s1 = s2; |
1481 | 0 | p1 = p2; |
1482 | 0 | } |
1483 | | |
1484 | | /* check our pointarray against triangle contour */ |
1485 | 0 | lw_dist3d_ptarray_ptarray(pa, tri->points, dl); |
1486 | 0 | return LW_TRUE; |
1487 | 0 | } |
1488 | | |
1489 | | /* Here we define the plane of a polygon (boundary pointarray of a polygon) |
1490 | | * the plane is stored as a point in plane (plane.pop) and a normal vector (plane.pv) |
1491 | | * |
1492 | | * We are calculating the average point and using it to break the polygon into |
1493 | | * POL_BREAKS (3) parts. Then we calculate the normal of those 3 vectors and |
1494 | | * use its average to define the normal of the plane.This is done to minimize |
1495 | | * the error introduced by FP precision |
1496 | | * We use [3] breaks to contemplate the special case of 3d triangles |
1497 | | */ |
1498 | | int |
1499 | | define_plane(const POINTARRAY *pa, PLANE3D *pl) |
1500 | 0 | { |
1501 | 0 | const uint32_t POL_BREAKS = 3; |
1502 | |
|
1503 | 0 | assert(pa); |
1504 | 0 | assert(pa->npoints > 3); |
1505 | 0 | if (!pa) |
1506 | 0 | return LW_FALSE; |
1507 | | |
1508 | 0 | uint32_t unique_points = pa->npoints - 1; |
1509 | 0 | uint32_t i; |
1510 | |
|
1511 | 0 | if (pa->npoints < 3) |
1512 | 0 | return LW_FALSE; |
1513 | | |
1514 | | /* Calculate the average point */ |
1515 | 0 | pl->pop.x = 0.0; |
1516 | 0 | pl->pop.y = 0.0; |
1517 | 0 | pl->pop.z = 0.0; |
1518 | 0 | for (i = 0; i < unique_points; i++) |
1519 | 0 | { |
1520 | 0 | POINT3DZ p; |
1521 | 0 | getPoint3dz_p(pa, i, &p); |
1522 | 0 | pl->pop.x += p.x; |
1523 | 0 | pl->pop.y += p.y; |
1524 | 0 | pl->pop.z += p.z; |
1525 | 0 | } |
1526 | |
|
1527 | 0 | pl->pop.x /= unique_points; |
1528 | 0 | pl->pop.y /= unique_points; |
1529 | 0 | pl->pop.z /= unique_points; |
1530 | |
|
1531 | 0 | pl->pv.x = 0.0; |
1532 | 0 | pl->pv.y = 0.0; |
1533 | 0 | pl->pv.z = 0.0; |
1534 | 0 | for (i = 0; i < POL_BREAKS; i++) |
1535 | 0 | { |
1536 | 0 | POINT3DZ point1, point2; |
1537 | 0 | VECTOR3D v1, v2, vp; |
1538 | 0 | uint32_t n1, n2; |
1539 | 0 | n1 = i * unique_points / POL_BREAKS; |
1540 | 0 | n2 = n1 + unique_points / POL_BREAKS; /* May overflow back to the first / last point */ |
1541 | |
|
1542 | 0 | if (n1 == n2) |
1543 | 0 | continue; |
1544 | | |
1545 | 0 | getPoint3dz_p(pa, n1, &point1); |
1546 | 0 | if (!get_3dvector_from_points(&pl->pop, &point1, &v1)) |
1547 | 0 | continue; |
1548 | | |
1549 | 0 | getPoint3dz_p(pa, n2, &point2); |
1550 | 0 | if (!get_3dvector_from_points(&pl->pop, &point2, &v2)) |
1551 | 0 | continue; |
1552 | | |
1553 | 0 | if (get_3dcross_product(&v1, &v2, &vp)) |
1554 | 0 | { |
1555 | | /* If the cross product isn't zero these 3 points aren't collinear |
1556 | | * We divide by its lengthsq to normalize the additions */ |
1557 | 0 | double vl = vp.x * vp.x + vp.y * vp.y + vp.z * vp.z; |
1558 | 0 | pl->pv.x += vp.x / vl; |
1559 | 0 | pl->pv.y += vp.y / vl; |
1560 | 0 | pl->pv.z += vp.z / vl; |
1561 | 0 | } |
1562 | 0 | } |
1563 | |
|
1564 | 0 | return (!FP_IS_ZERO(pl->pv.x) || !FP_IS_ZERO(pl->pv.y) || !FP_IS_ZERO(pl->pv.z)); |
1565 | 0 | } |
1566 | | |
1567 | | |
1568 | | /** |
1569 | | * pt_in_ring_3d(): crossing number test for a point in a polygon |
1570 | | * input: p = a point, |
1571 | | * pa = vertex points of a ring V[n+1] with V[n]=V[0] |
1572 | | * plane=the plane that the vertex points are lying on |
1573 | | * returns: 0 = outside, 1 = inside |
1574 | | * |
1575 | | * Our polygons have first and last point the same, |
1576 | | * |
1577 | | * The difference in 3D variant is that we exclude the dimension that faces the plane least. |
1578 | | * That is the dimension with the highest number in pv |
1579 | | */ |
1580 | | int |
1581 | | pt_in_ring_3d(const POINT3DZ *p, const POINTARRAY *ring, PLANE3D *plane) |
1582 | 0 | { |
1583 | |
|
1584 | 0 | uint32_t cn = 0; /* the crossing number counter */ |
1585 | 0 | uint32_t i; |
1586 | 0 | POINT3DZ v1, v2; |
1587 | |
|
1588 | 0 | POINT3DZ first, last; |
1589 | |
|
1590 | 0 | if ( !ring || ring->npoints == 0 ) |
1591 | 0 | lwerror("%s called on empty pointarray", __func__); |
1592 | |
|
1593 | 0 | getPoint3dz_p(ring, 0, &first); |
1594 | 0 | getPoint3dz_p(ring, ring->npoints - 1, &last); |
1595 | 0 | if (memcmp(&first, &last, sizeof(POINT3DZ))) |
1596 | 0 | { |
1597 | 0 | lwerror("pt_in_ring_3d: V[n] != V[0] (%g %g %g!= %g %g %g)", |
1598 | 0 | first.x, |
1599 | 0 | first.y, |
1600 | 0 | first.z, |
1601 | 0 | last.x, |
1602 | 0 | last.y, |
1603 | 0 | last.z); |
1604 | 0 | return LW_FALSE; |
1605 | 0 | } |
1606 | | |
1607 | 0 | LWDEBUGF(2, "pt_in_ring_3d called with point: %g %g %g", p->x, p->y, p->z); |
1608 | | /* printPA(ring); */ |
1609 | | |
1610 | | /* loop through all edges of the polygon */ |
1611 | 0 | getPoint3dz_p(ring, 0, &v1); |
1612 | |
|
1613 | 0 | if (fabs(plane->pv.z) >= fabs(plane->pv.x) && |
1614 | 0 | fabs(plane->pv.z) >= fabs(plane->pv.y)) /*If the z vector of the normal vector to the plane is larger than x |
1615 | | and y vector we project the ring to the xy-plane*/ |
1616 | 0 | { |
1617 | 0 | for (i = 0; i < ring->npoints - 1; i++) |
1618 | 0 | { |
1619 | 0 | double vt; |
1620 | 0 | getPoint3dz_p(ring, i + 1, &v2); |
1621 | | |
1622 | | /* edge from vertex i to vertex i+1 */ |
1623 | 0 | if ( |
1624 | | /* an upward crossing */ |
1625 | 0 | ((v1.y <= p->y) && (v2.y > p->y)) |
1626 | | /* a downward crossing */ |
1627 | 0 | || ((v1.y > p->y) && (v2.y <= p->y))) |
1628 | 0 | { |
1629 | |
|
1630 | 0 | vt = (double)(p->y - v1.y) / (v2.y - v1.y); |
1631 | | |
1632 | | /* P.x <intersect */ |
1633 | 0 | if (p->x < v1.x + vt * (v2.x - v1.x)) |
1634 | 0 | { |
1635 | | /* a valid crossing of y=p.y right of p.x */ |
1636 | 0 | ++cn; |
1637 | 0 | } |
1638 | 0 | } |
1639 | 0 | v1 = v2; |
1640 | 0 | } |
1641 | 0 | } |
1642 | 0 | else if (fabs(plane->pv.y) >= fabs(plane->pv.x) && |
1643 | 0 | fabs(plane->pv.y) >= fabs(plane->pv.z)) /*If the y vector of the normal vector to the plane is larger |
1644 | | than x and z vector we project the ring to the xz-plane*/ |
1645 | 0 | { |
1646 | 0 | for (i = 0; i < ring->npoints - 1; i++) |
1647 | 0 | { |
1648 | 0 | double vt; |
1649 | 0 | getPoint3dz_p(ring, i + 1, &v2); |
1650 | | |
1651 | | /* edge from vertex i to vertex i+1 */ |
1652 | 0 | if ( |
1653 | | /* an upward crossing */ |
1654 | 0 | ((v1.z <= p->z) && (v2.z > p->z)) |
1655 | | /* a downward crossing */ |
1656 | 0 | || ((v1.z > p->z) && (v2.z <= p->z))) |
1657 | 0 | { |
1658 | |
|
1659 | 0 | vt = (double)(p->z - v1.z) / (v2.z - v1.z); |
1660 | | |
1661 | | /* P.x <intersect */ |
1662 | 0 | if (p->x < v1.x + vt * (v2.x - v1.x)) |
1663 | 0 | { |
1664 | | /* a valid crossing of y=p.y right of p.x */ |
1665 | 0 | ++cn; |
1666 | 0 | } |
1667 | 0 | } |
1668 | 0 | v1 = v2; |
1669 | 0 | } |
1670 | 0 | } |
1671 | 0 | else /*Hopefully we only have the cases where x part of the normal vector is largest left*/ |
1672 | 0 | { |
1673 | 0 | for (i = 0; i < ring->npoints - 1; i++) |
1674 | 0 | { |
1675 | 0 | double vt; |
1676 | 0 | getPoint3dz_p(ring, i + 1, &v2); |
1677 | | |
1678 | | /* edge from vertex i to vertex i+1 */ |
1679 | 0 | if ( |
1680 | | /* an upward crossing */ |
1681 | 0 | ((v1.z <= p->z) && (v2.z > p->z)) |
1682 | | /* a downward crossing */ |
1683 | 0 | || ((v1.z > p->z) && (v2.z <= p->z))) |
1684 | 0 | { |
1685 | |
|
1686 | 0 | vt = (double)(p->z - v1.z) / (v2.z - v1.z); |
1687 | | |
1688 | | /* P.x <intersect */ |
1689 | 0 | if (p->y < v1.y + vt * (v2.y - v1.y)) |
1690 | 0 | { |
1691 | | /* a valid crossing of y=p.y right of p.x */ |
1692 | 0 | ++cn; |
1693 | 0 | } |
1694 | 0 | } |
1695 | 0 | v1 = v2; |
1696 | 0 | } |
1697 | 0 | } |
1698 | 0 | LWDEBUGF(3, "pt_in_ring_3d returning %d", cn & 1); |
1699 | |
|
1700 | 0 | return (cn & 1); /* 0 if even (out), and 1 if odd (in) */ |
1701 | 0 | } |
1702 | | |
1703 | | /*------------------------------------------------------------------------------------------------------------ |
1704 | | End of Brute force functions |
1705 | | --------------------------------------------------------------------------------------------------------------*/ |