PostGIS  3.7.0dev-r@@SVN_REVISION@@
ptarray.c
Go to the documentation of this file.
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 (C) 2012-2021 Sandro Santilli <strk@kbt.io>
22  * Copyright (C) 2001-2006 Refractions Research Inc.
23  *
24  **********************************************************************/
25 
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 
31 #include "../postgis_config.h"
32 /*#define POSTGIS_DEBUG_LEVEL 4*/
33 #include "liblwgeom_internal.h"
34 #include "lwgeom_log.h"
35 
36 int
38 {
39  if ( ! pa ) return LW_FALSE;
40  return FLAGS_GET_Z(pa->flags);
41 }
42 
43 int
45 {
46  if ( ! pa ) return LW_FALSE;
47  return FLAGS_GET_M(pa->flags);
48 }
49 
51 ptarray_construct(char hasz, char hasm, uint32_t npoints)
52 {
53  POINTARRAY *pa = ptarray_construct_empty(hasz, hasm, npoints);
54  pa->npoints = npoints;
55  return pa;
56 }
57 
59 ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
60 {
61  POINTARRAY *pa = lwalloc(sizeof(POINTARRAY));
62  pa->serialized_pointlist = NULL;
63 
64  /* Set our dimensionality info on the bitmap */
65  pa->flags = lwflags(hasz, hasm, 0);
66 
67  /* We will be allocating a bit of room */
68  pa->npoints = 0;
69  pa->maxpoints = maxpoints;
70 
71  /* Allocate the coordinate array */
72  if ( maxpoints > 0 )
73  pa->serialized_pointlist = lwalloc(maxpoints * ptarray_point_size(pa));
74  else
75  pa->serialized_pointlist = NULL;
76 
77  return pa;
78 }
79 
80 /*
81 * Add a point into a pointarray. Only adds as many dimensions as the
82 * pointarray supports.
83 */
84 int
85 ptarray_insert_point(POINTARRAY *pa, const POINT4D *p, uint32_t where)
86 {
87  if (!pa || !p)
88  return LW_FAILURE;
89  size_t point_size = ptarray_point_size(pa);
90  LWDEBUGF(5,"pa = %p; p = %p; where = %d", pa, p, where);
91  LWDEBUGF(5,"pa->npoints = %d; pa->maxpoints = %d", pa->npoints, pa->maxpoints);
92 
93  if ( FLAGS_GET_READONLY(pa->flags) )
94  {
95  lwerror("ptarray_insert_point: called on read-only point array");
96  return LW_FAILURE;
97  }
98 
99  /* Error on invalid offset value */
100  if ( where > pa->npoints )
101  {
102  lwerror("ptarray_insert_point: offset out of range (%d)", where);
103  return LW_FAILURE;
104  }
105 
106  /* If we have no storage, let's allocate some */
107  if( pa->maxpoints == 0 || ! pa->serialized_pointlist )
108  {
109  pa->maxpoints = 32;
110  pa->npoints = 0;
112  }
113 
114  /* Error out if we have a bad situation */
115  if ( pa->npoints > pa->maxpoints )
116  {
117  lwerror("npoints (%d) is greater than maxpoints (%d)", pa->npoints, pa->maxpoints);
118  return LW_FAILURE;
119  }
120 
121  /* Check if we have enough storage, add more if necessary */
122  if( pa->npoints == pa->maxpoints )
123  {
124  pa->maxpoints *= 2;
126  }
127 
128  /* Make space to insert the new point */
129  if( where < pa->npoints )
130  {
131  size_t copy_size = point_size * (pa->npoints - where);
132  memmove(getPoint_internal(pa, where+1), getPoint_internal(pa, where), copy_size);
133  LWDEBUGF(5,"copying %zu bytes to start vertex %d from start vertex %d", copy_size, where+1, where);
134  }
135 
136  /* We have one more point */
137  ++pa->npoints;
138 
139  /* Copy the new point into the gap */
140  ptarray_set_point4d(pa, where, p);
141  LWDEBUGF(5,"copying new point to start vertex %zu", point_size);
142 
143  return LW_SUCCESS;
144 }
145 
146 int
147 ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int repeated_points)
148 {
149  /* Check for pathology */
150  if( ! pa || ! pt )
151  {
152  lwerror("ptarray_append_point: null input");
153  return LW_FAILURE;
154  }
155 
156  /* Check for duplicate end point */
157  if ( repeated_points == LW_FALSE && pa->npoints > 0 )
158  {
159  POINT4D tmp;
160  getPoint4d_p(pa, pa->npoints-1, &tmp);
161  LWDEBUGF(4,"checking for duplicate end point (pt = POINT(%g %g) pa->npoints-q = POINT(%g %g))",pt->x,pt->y,tmp.x,tmp.y);
162 
163  /* Return LW_SUCCESS and do nothing else if previous point in list is equal to this one */
164  if ( (pt->x == tmp.x) && (pt->y == tmp.y) &&
165  (FLAGS_GET_Z(pa->flags) ? pt->z == tmp.z : 1) &&
166  (FLAGS_GET_M(pa->flags) ? pt->m == tmp.m : 1) )
167  {
168  return LW_SUCCESS;
169  }
170  }
171 
172  /* Append is just a special case of insert */
173  return ptarray_insert_point(pa, pt, pa->npoints);
174 }
175 
176 int
177 ptarray_append_ptarray(POINTARRAY *pa1, POINTARRAY *pa2, double gap_tolerance)
178 {
179  unsigned int poff = 0;
180  unsigned int npoints;
181  unsigned int ncap;
182  unsigned int ptsize;
183 
184  /* Check for pathology */
185  if( ! pa1 || ! pa2 )
186  {
187  lwerror("ptarray_append_ptarray: null input");
188  return LW_FAILURE;
189  }
190 
191  npoints = pa2->npoints;
192 
193  if ( ! npoints ) return LW_SUCCESS; /* nothing more to do */
194 
195  if( FLAGS_GET_READONLY(pa1->flags) )
196  {
197  lwerror("ptarray_append_ptarray: target pointarray is read-only");
198  return LW_FAILURE;
199  }
200 
201  if( FLAGS_GET_ZM(pa1->flags) != FLAGS_GET_ZM(pa2->flags) )
202  {
203  lwerror("ptarray_append_ptarray: appending mixed dimensionality is not allowed");
204  return LW_FAILURE;
205  }
206 
207  ptsize = ptarray_point_size(pa1);
208 
209  /* Check for duplicate end point */
210  if ( pa1->npoints )
211  {
212  POINT2D tmp1, tmp2;
213  getPoint2d_p(pa1, pa1->npoints-1, &tmp1);
214  getPoint2d_p(pa2, 0, &tmp2);
215 
216  /* If the end point and start point are the same, then don't copy start point */
217  if (p2d_same(&tmp1, &tmp2)) {
218  poff = 1;
219  --npoints;
220  }
221  else if ( gap_tolerance == 0 || ( gap_tolerance > 0 &&
222  distance2d_pt_pt(&tmp1, &tmp2) > gap_tolerance ) )
223  {
224  lwerror("Second line start point too far from first line end point");
225  return LW_FAILURE;
226  }
227  }
228 
229  /* Check if we need extra space */
230  ncap = pa1->npoints + npoints;
231  if ( pa1->maxpoints < ncap )
232  {
233  if ( pa1->maxpoints )
234  {
235  pa1->maxpoints = ncap > pa1->maxpoints*2 ?
236  ncap : pa1->maxpoints*2;
237  pa1->serialized_pointlist = lwrealloc(pa1->serialized_pointlist, (size_t)ptsize * pa1->maxpoints);
238  }
239  else
240  {
241  pa1->maxpoints = ncap;
242  pa1->serialized_pointlist = lwalloc((size_t)ptsize * pa1->maxpoints);
243  }
244  }
245 
246  memcpy(getPoint_internal(pa1, pa1->npoints),
247  getPoint_internal(pa2, poff), (size_t)ptsize * npoints);
248 
249  pa1->npoints = ncap;
250 
251  return LW_SUCCESS;
252 }
253 
254 /*
255 * Add a point into a pointarray. Only adds as many dimensions as the
256 * pointarray supports.
257 */
258 int
259 ptarray_remove_point(POINTARRAY *pa, uint32_t where)
260 {
261  /* Check for pathology */
262  if( ! pa )
263  {
264  lwerror("ptarray_remove_point: null input");
265  return LW_FAILURE;
266  }
267 
268  /* Error on invalid offset value */
269  if ( where >= pa->npoints )
270  {
271  lwerror("ptarray_remove_point: offset out of range (%d)", where);
272  return LW_FAILURE;
273  }
274 
275  /* If the point is any but the last, we need to copy the data back one point */
276  if (where < pa->npoints - 1)
277  memmove(getPoint_internal(pa, where),
278  getPoint_internal(pa, where + 1),
279  ptarray_point_size(pa) * (pa->npoints - where - 1));
280 
281  /* We have one less point */
282  pa->npoints--;
283 
284  return LW_SUCCESS;
285 }
286 
291 POINTARRAY* ptarray_construct_reference_data(char hasz, char hasm, uint32_t npoints, uint8_t *ptlist)
292 {
293  POINTARRAY *pa = lwalloc(sizeof(POINTARRAY));
294  LWDEBUGF(5, "hasz = %d, hasm = %d, npoints = %d, ptlist = %p", hasz, hasm, npoints, ptlist);
295  pa->flags = lwflags(hasz, hasm, 0);
296  FLAGS_SET_READONLY(pa->flags, 1); /* We don't own this memory, so we can't alter or free it. */
297  pa->npoints = npoints;
298  pa->maxpoints = npoints;
299  pa->serialized_pointlist = ptlist;
300  return pa;
301 }
302 
303 
304 POINTARRAY*
305 ptarray_construct_copy_data(char hasz, char hasm, uint32_t npoints, const uint8_t *ptlist)
306 {
307  POINTARRAY *pa = lwalloc(sizeof(POINTARRAY));
308 
309  pa->flags = lwflags(hasz, hasm, 0);
310  pa->npoints = npoints;
311  pa->maxpoints = npoints;
312 
313  if ( npoints > 0 )
314  {
315  pa->serialized_pointlist = lwalloc(ptarray_point_size(pa) * npoints);
316  memcpy(pa->serialized_pointlist, ptlist, ptarray_point_size(pa) * npoints);
317  }
318  else
319  {
320  pa->serialized_pointlist = NULL;
321  }
322 
323  return pa;
324 }
325 
326 void
328 {
329  if (pa)
330  {
331  if (pa->serialized_pointlist && (!FLAGS_GET_READONLY(pa->flags)))
333  lwfree(pa);
334  }
335 }
336 
337 
338 void
340 {
341  if (!pa->npoints)
342  return;
343  uint32_t i;
344  uint32_t last = pa->npoints - 1;
345  uint32_t mid = pa->npoints / 2;
346 
347  double *d = (double*)(pa->serialized_pointlist);
348  int j;
349  int ndims = FLAGS_NDIMS(pa->flags);
350  for (i = 0; i < mid; i++)
351  {
352  for (j = 0; j < ndims; j++)
353  {
354  double buf;
355  buf = d[i*ndims+j];
356  d[i*ndims+j] = d[(last-i)*ndims+j];
357  d[(last-i)*ndims+j] = buf;
358  }
359  }
360  return;
361 }
362 
363 
367 POINTARRAY*
369 {
370  uint32_t i;
371  double d;
372  POINT4D p;
373 
374  for (i=0 ; i < pa->npoints ; i++)
375  {
376  getPoint4d_p(pa, i, &p);
377  d = p.y;
378  p.y = p.x;
379  p.x = d;
380  ptarray_set_point4d(pa, i, &p);
381  }
382 
383  return pa;
384 }
385 
386 void
388 {
389  uint32_t i;
390  double d, *dp1, *dp2;
391  POINT4D p;
392 
393  dp1 = ((double*)&p)+(unsigned)o1;
394  dp2 = ((double*)&p)+(unsigned)o2;
395  for (i=0 ; i < pa->npoints ; i++)
396  {
397  getPoint4d_p(pa, i, &p);
398  d = *dp2;
399  *dp2 = *dp1;
400  *dp1 = d;
401  ptarray_set_point4d(pa, i, &p);
402  }
403 }
404 
412 POINTARRAY *
413 ptarray_segmentize2d(const POINTARRAY *ipa, double dist)
414 {
415  double segdist;
416  POINT4D p1, p2;
417  POINT4D pbuf;
418  POINTARRAY *opa;
419  uint32_t i, j, nseg;
420  int hasz = FLAGS_GET_Z(ipa->flags);
421  int hasm = FLAGS_GET_M(ipa->flags);
422 
423  pbuf.x = pbuf.y = pbuf.z = pbuf.m = 0;
424 
425  /* Initial storage */
426  opa = ptarray_construct_empty(hasz, hasm, ipa->npoints);
427 
428  /* Add first point */
429  getPoint4d_p(ipa, 0, &p1);
430  ptarray_append_point(opa, &p1, LW_FALSE);
431 
432  /* Loop on all other input points */
433  for (i = 1; i < ipa->npoints; i++)
434  {
435  /*
436  * We use these pointers to avoid
437  * "strict-aliasing rules break" warning raised
438  * by gcc (3.3 and up).
439  *
440  * It looks that casting a variable address (also
441  * referred to as "type-punned pointer")
442  * breaks those "strict" rules.
443  */
444  POINT4D *p1ptr=&p1, *p2ptr=&p2;
445  double segments;
446 
447  getPoint4d_p(ipa, i, &p2);
448 
449  segdist = distance2d_pt_pt((POINT2D *)p1ptr, (POINT2D *)p2ptr);
450  /* Split input segment into shorter even chunks */
451  segments = ceil(segdist / dist);
452 
453  /* Uses INT32_MAX instead of UINT32_MAX to be safe that it fits */
454  if (segments >= INT32_MAX)
455  {
456  lwnotice("%s:%d - %s: Too many segments required (%e)",
457  __FILE__, __LINE__,__func__, segments);
458  ptarray_free(opa);
459  return NULL;
460  }
461  nseg = segments;
462 
463  for (j = 1; j < nseg; j++)
464  {
465  pbuf.x = p1.x + (p2.x - p1.x) * j / nseg;
466  pbuf.y = p1.y + (p2.y - p1.y) * j / nseg;
467  if (hasz)
468  pbuf.z = p1.z + (p2.z - p1.z) * j / nseg;
469  if (hasm)
470  pbuf.m = p1.m + (p2.m - p1.m) * j / nseg;
471  ptarray_append_point(opa, &pbuf, LW_FALSE);
472  LW_ON_INTERRUPT(ptarray_free(opa); return NULL);
473  }
474 
475  ptarray_append_point(opa, &p2, (ipa->npoints == 2) ? LW_TRUE : LW_FALSE);
476  p1 = p2;
477  LW_ON_INTERRUPT(ptarray_free(opa); return NULL);
478  }
479 
480  return opa;
481 }
482 
483 char
484 ptarray_same(const POINTARRAY *pa1, const POINTARRAY *pa2)
485 {
486  uint32_t i;
487  size_t ptsize;
488 
489  if ( FLAGS_GET_ZM(pa1->flags) != FLAGS_GET_ZM(pa2->flags) ) return LW_FALSE;
490  LWDEBUG(5,"dimensions are the same");
491 
492  if ( pa1->npoints != pa2->npoints ) return LW_FALSE;
493  LWDEBUG(5,"npoints are the same");
494 
495  ptsize = ptarray_point_size(pa1);
496  LWDEBUGF(5, "ptsize = %zu", ptsize);
497 
498  for (i=0; i<pa1->npoints; i++)
499  {
500  if ( memcmp(getPoint_internal(pa1, i), getPoint_internal(pa2, i), ptsize) )
501  return LW_FALSE;
502  LWDEBUGF(5,"point #%d is the same",i);
503  }
504 
505  return LW_TRUE;
506 }
507 
508 char
509 ptarray_same2d(const POINTARRAY *pa1, const POINTARRAY *pa2)
510 {
511  uint32_t i;
512 
513  if ( FLAGS_GET_ZM(pa1->flags) != FLAGS_GET_ZM(pa2->flags) ) return LW_FALSE;
514  LWDEBUG(5,"dimensions are the same");
515 
516  if ( pa1->npoints != pa2->npoints ) return LW_FALSE;
517  LWDEBUG(5,"npoints are the same");
518 
519  for (i=0; i<pa1->npoints; i++)
520  {
521  if ( memcmp(getPoint_internal(pa1, i), getPoint_internal(pa2, i), sizeof(POINT2D)) )
522  return LW_FALSE;
523  LWDEBUGF(5,"point #%d is the same",i);
524  }
525 
526  return LW_TRUE;
527 }
528 
529 POINTARRAY *
530 ptarray_addPoint(const POINTARRAY *pa, uint8_t *p, size_t pdims, uint32_t where)
531 {
532  POINTARRAY *ret;
533  POINT4D pbuf;
534  size_t ptsize = ptarray_point_size(pa);
535 
536  LWDEBUGF(3, "pa %p p %p size %zu where %u",
537  pa, p, pdims, where);
538 
539  if ( pdims < 2 || pdims > 4 )
540  {
541  lwerror("ptarray_addPoint: point dimension out of range (%zu)",
542  pdims);
543  return NULL;
544  }
545 
546  if ( where > pa->npoints )
547  {
548  lwerror("ptarray_addPoint: offset out of range (%d)",
549  where);
550  return NULL;
551  }
552 
553  LWDEBUG(3, "called with a point");
554 
555  pbuf.x = pbuf.y = pbuf.z = pbuf.m = 0.0;
556  memcpy((uint8_t *)&pbuf, p, pdims*sizeof(double));
557 
558  LWDEBUG(3, "initialized point buffer");
559 
561  FLAGS_GET_M(pa->flags), pa->npoints+1);
562 
563 
564  if ( where )
565  {
566  memcpy(getPoint_internal(ret, 0), getPoint_internal(pa, 0), ptsize*where);
567  }
568 
569  memcpy(getPoint_internal(ret, where), (uint8_t *)&pbuf, ptsize);
570 
571  if ( where+1 != ret->npoints )
572  {
573  memcpy(getPoint_internal(ret, where+1),
574  getPoint_internal(pa, where),
575  ptsize*(pa->npoints-where));
576  }
577 
578  return ret;
579 }
580 
581 POINTARRAY *
582 ptarray_removePoint(POINTARRAY *pa, uint32_t which)
583 {
584  POINTARRAY *ret;
585  size_t ptsize = ptarray_point_size(pa);
586 
587  LWDEBUGF(3, "pa %p which %u", pa, which);
588 
589  assert(which <= pa->npoints-1);
590  assert(pa->npoints >= 3);
591 
593  FLAGS_GET_M(pa->flags), pa->npoints-1);
594 
595  /* copy initial part */
596  if ( which )
597  {
598  memcpy(getPoint_internal(ret, 0), getPoint_internal(pa, 0), ptsize*which);
599  }
600 
601  /* copy final part */
602  if ( which < pa->npoints-1 )
603  {
604  memcpy(getPoint_internal(ret, which), getPoint_internal(pa, which+1),
605  ptsize*(pa->npoints-which-1));
606  }
607 
608  return ret;
609 }
610 
611 POINTARRAY *
613 {
614  POINTARRAY *pa;
615  size_t ptsize = ptarray_point_size(pa1);
616 
617  if (FLAGS_GET_ZM(pa1->flags) != FLAGS_GET_ZM(pa2->flags))
618  lwerror("ptarray_cat: Mixed dimension");
619 
620  pa = ptarray_construct( FLAGS_GET_Z(pa1->flags),
621  FLAGS_GET_M(pa1->flags),
622  pa1->npoints + pa2->npoints);
623 
624  memcpy( getPoint_internal(pa, 0),
625  getPoint_internal(pa1, 0),
626  ptsize*(pa1->npoints));
627 
628  memcpy( getPoint_internal(pa, pa1->npoints),
629  getPoint_internal(pa2, 0),
630  ptsize*(pa2->npoints));
631 
632  ptarray_free(pa1);
633  ptarray_free(pa2);
634 
635  return pa;
636 }
637 
638 
642 POINTARRAY *
644 {
645  POINTARRAY *out = lwalloc(sizeof(POINTARRAY));
646 
647  LWDEBUG(3, "ptarray_clone_deep called.");
648 
649  out->flags = in->flags;
650  out->npoints = in->npoints;
651  out->maxpoints = in->npoints;
652 
653  FLAGS_SET_READONLY(out->flags, 0);
654 
655  if (!in->npoints)
656  {
657  // Avoid calling lwalloc of 0 bytes
658  out->serialized_pointlist = NULL;
659  }
660  else
661  {
662  size_t size = in->npoints * ptarray_point_size(in);
663  out->serialized_pointlist = lwalloc(size);
664  memcpy(out->serialized_pointlist, in->serialized_pointlist, size);
665  }
666 
667  return out;
668 }
669 
673 POINTARRAY *
675 {
676  POINTARRAY *out = lwalloc(sizeof(POINTARRAY));
677 
678  LWDEBUG(3, "ptarray_clone called.");
679 
680  out->flags = in->flags;
681  out->npoints = in->npoints;
682  out->maxpoints = in->maxpoints;
683 
684  FLAGS_SET_READONLY(out->flags, 1);
685 
687 
688  return out;
689 }
690 
695 int
697 {
698  if (!in)
699  {
700  lwerror("ptarray_is_closed: called with null point array");
701  return 0;
702  }
703  if (in->npoints <= 1 ) return in->npoints; /* single-point are closed, empty not closed */
704 
705  return 0 == memcmp(getPoint_internal(in, 0), getPoint_internal(in, in->npoints-1), ptarray_point_size(in));
706 }
707 
708 
709 int
711 {
712  if (!in)
713  {
714  lwerror("ptarray_is_closed_2d: called with null point array");
715  return 0;
716  }
717  if (in->npoints <= 1 ) return in->npoints; /* single-point are closed, empty not closed */
718 
719  return 0 == memcmp(getPoint_internal(in, 0), getPoint_internal(in, in->npoints-1), sizeof(POINT2D) );
720 }
721 
722 int
724 {
725  if (!in)
726  {
727  lwerror("ptarray_is_closed_3d: called with null point array");
728  return 0;
729  }
730  if (in->npoints <= 1 ) return in->npoints; /* single-point are closed, empty not closed */
731 
732  return 0 == memcmp(getPoint_internal(in, 0), getPoint_internal(in, in->npoints-1), sizeof(POINT3D) );
733 }
734 
735 int
737 {
738  if ( FLAGS_GET_Z(in->flags) )
739  return ptarray_is_closed_3d(in);
740  else
741  return ptarray_is_closed_2d(in);
742 }
743 
750 int
752 {
753  return ptarray_contains_point_partial(pa, pt, LW_TRUE, NULL);
754 }
755 
756 
757 /*
758  * The following is based on the "Fast Winding Number Inclusion of a Point
759  * in a Polygon" algorithm by Dan Sunday.
760  * http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm#Winding%20Number
761  */
762 int
763 ptarray_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
764 {
765  int wn = 0;
766  uint32_t i;
767  double side;
768  const POINT2D *seg1;
769  const POINT2D *seg2;
770  double ymin, ymax;
771 
772  seg1 = getPoint2d_cp(pa, 0);
773  seg2 = getPoint2d_cp(pa, pa->npoints-1);
774  if ( check_closed && ! p2d_same(seg1, seg2) )
775  lwerror("ptarray_contains_point called on unclosed ring");
776 
777  for ( i=1; i < pa->npoints; i++ )
778  {
779  seg2 = getPoint2d_cp(pa, i);
780 
781  /* Zero length segments are ignored. */
782  if ( seg1->x == seg2->x && seg1->y == seg2->y )
783  {
784  seg1 = seg2;
785  continue;
786  }
787 
788  ymin = FP_MIN(seg1->y, seg2->y);
789  ymax = FP_MAX(seg1->y, seg2->y);
790 
791  /* Only test segments in our vertical range */
792  if ( pt->y > ymax || pt->y < ymin )
793  {
794  seg1 = seg2;
795  continue;
796  }
797 
798  side = lw_segment_side(seg1, seg2, pt);
799 
800  /*
801  * A point on the boundary of a ring is not contained.
802  * WAS: if (fabs(side) < 1e-12), see #852
803  */
804  if ( (side == 0) && lw_pt_in_seg(pt, seg1, seg2) )
805  {
806  return LW_BOUNDARY;
807  }
808 
809  /*
810  * If the point is to the left of the line, and it's rising,
811  * then the line is to the right of the point and
812  * circling counter-clockwise, so increment.
813  */
814  if ( (side < 0) && (seg1->y <= pt->y) && (pt->y < seg2->y) )
815  {
816  wn++;
817  }
818 
819  /*
820  * If the point is to the right of the line, and it's falling,
821  * then the line is to the right of the point and circling
822  * clockwise, so decrement.
823  */
824  else if ( (side > 0) && (seg2->y <= pt->y) && (pt->y < seg1->y) )
825  {
826  wn--;
827  }
828 
829  seg1 = seg2;
830  }
831 
832  /* Sent out the winding number for calls that are building on this as a primitive */
833  if ( winding_number )
834  *winding_number = wn;
835 
836  /* Outside */
837  if (wn == 0)
838  {
839  return LW_OUTSIDE;
840  }
841 
842  /* Inside */
843  return LW_INSIDE;
844 }
845 
855 int
857 {
858  return ptarrayarc_contains_point_partial(pa, pt, LW_TRUE /* Check closed*/, NULL);
859 }
860 
861 int
862 ptarrayarc_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
863 {
864  int wn = 0;
865  uint32_t i;
866  int side;
867  const POINT2D *seg1;
868  const POINT2D *seg2;
869  const POINT2D *seg3;
870  GBOX gbox;
871 
872  /* Check for not an arc ring (always have odd # of points) */
873  if ( (pa->npoints % 2) == 0 )
874  {
875  lwerror("ptarrayarc_contains_point called with even number of points");
876  return LW_OUTSIDE;
877  }
878 
879  /* Check for not an arc ring (always have >= 3 points) */
880  if ( pa->npoints < 3 )
881  {
882  lwerror("ptarrayarc_contains_point called too-short pointarray");
883  return LW_OUTSIDE;
884  }
885 
886  /* Check for unclosed case */
887  seg1 = getPoint2d_cp(pa, 0);
888  seg3 = getPoint2d_cp(pa, pa->npoints-1);
889  if ( check_closed && ! p2d_same(seg1, seg3) )
890  {
891  lwerror("ptarrayarc_contains_point called on unclosed ring");
892  return LW_OUTSIDE;
893  }
894  /* OK, it's closed. Is it just one circle? */
895  else if ( p2d_same(seg1, seg3) && pa->npoints == 3 )
896  {
897  double radius, d;
898  POINT2D c;
899  seg2 = getPoint2d_cp(pa, 1);
900 
901  /* Wait, it's just a point, so it can't contain anything */
902  if ( lw_arc_is_pt(seg1, seg2, seg3) )
903  return LW_OUTSIDE;
904 
905  /* See if the point is within the circle radius */
906  radius = lw_arc_center(seg1, seg2, seg3, &c);
907  d = distance2d_pt_pt(pt, &c);
908  if ( FP_EQUALS(d, radius) )
909  return LW_BOUNDARY; /* Boundary of circle */
910  else if ( d < radius )
911  return LW_INSIDE; /* Inside circle */
912  else
913  return LW_OUTSIDE; /* Outside circle */
914  }
915  else if ( p2d_same(seg1, pt) || p2d_same(seg3, pt) )
916  {
917  return LW_BOUNDARY; /* Boundary case */
918  }
919 
920  /* Start on the ring */
921  seg1 = getPoint2d_cp(pa, 0);
922  for ( i=1; i < pa->npoints; i += 2 )
923  {
924  seg2 = getPoint2d_cp(pa, i);
925  seg3 = getPoint2d_cp(pa, i+1);
926 
927  /* Catch an easy boundary case */
928  if( p2d_same(seg3, pt) )
929  return LW_BOUNDARY;
930 
931  /* Skip arcs that have no size */
932  if ( lw_arc_is_pt(seg1, seg2, seg3) )
933  {
934  seg1 = seg3;
935  continue;
936  }
937 
938  /* Only test segments in our vertical range */
939  lw_arc_calculate_gbox_cartesian_2d(seg1, seg2, seg3, &gbox);
940  if ( pt->y > gbox.ymax || pt->y < gbox.ymin )
941  {
942  seg1 = seg3;
943  continue;
944  }
945 
946  /* Outside of horizontal range, and not between end points we also skip */
947  if ( (pt->x > gbox.xmax || pt->x < gbox.xmin) &&
948  (pt->y > FP_MAX(seg1->y, seg3->y) || pt->y < FP_MIN(seg1->y, seg3->y)) )
949  {
950  seg1 = seg3;
951  continue;
952  }
953 
954  side = lw_arc_side(seg1, seg2, seg3, pt);
955 
956  /* On the boundary */
957  if ( (side == 0) && lw_pt_in_arc(pt, seg1, seg2, seg3) )
958  {
959  return LW_BOUNDARY;
960  }
961 
962  /* Going "up"! Point to left of arc. */
963  if ( side < 0 && (seg1->y <= pt->y) && (pt->y < seg3->y) )
964  {
965  wn++;
966  }
967 
968  /* Going "down"! */
969  if ( side > 0 && (seg3->y <= pt->y) && (pt->y < seg1->y) )
970  {
971  wn--;
972  }
973 
974  /* Inside the arc! */
975  if ( pt->x <= gbox.xmax && pt->x >= gbox.xmin )
976  {
977  POINT2D C;
978  double radius = lw_arc_center(seg1, seg2, seg3, &C);
979  double d = distance2d_pt_pt(pt, &C);
980 
981  /* On the boundary! */
982  if ( d == radius )
983  return LW_BOUNDARY;
984 
985  /* Within the arc! */
986  if ( d < radius )
987  {
988  /* Left side, increment winding number */
989  if ( side < 0 )
990  wn++;
991  /* Right side, decrement winding number */
992  if ( side > 0 )
993  wn--;
994  }
995  }
996 
997  seg1 = seg3;
998  }
999 
1000  /* Sent out the winding number for calls that are building on this as a primitive */
1001  if ( winding_number )
1002  *winding_number = wn;
1003 
1004  /* Outside */
1005  if (wn == 0)
1006  {
1007  return LW_OUTSIDE;
1008  }
1009 
1010  /* Inside */
1011  return LW_INSIDE;
1012 }
1013 
1019 double
1021 {
1022  const POINT2D *P1;
1023  const POINT2D *P2;
1024  const POINT2D *P3;
1025  double sum = 0.0;
1026  double x0, x, y1, y2;
1027  uint32_t i;
1028 
1029  if (! pa || pa->npoints < 3 )
1030  return 0.0;
1031 
1032  P1 = getPoint2d_cp(pa, 0);
1033  P2 = getPoint2d_cp(pa, 1);
1034  x0 = P1->x;
1035  for ( i = 2; i < pa->npoints; i++ )
1036  {
1037  P3 = getPoint2d_cp(pa, i);
1038  x = P2->x - x0;
1039  y1 = P3->y;
1040  y2 = P1->y;
1041  sum += x * (y2-y1);
1042 
1043  /* Move forwards! */
1044  P1 = P2;
1045  P2 = P3;
1046  }
1047  return sum / 2.0;
1048 }
1049 
1050 int
1052 {
1053  double area = 0;
1054  area = ptarray_signed_area(pa);
1055  if ( area > 0 ) return LW_FALSE;
1056  else return LW_TRUE;
1057 }
1058 
1059 POINTARRAY*
1060 ptarray_force_dims(const POINTARRAY *pa, int hasz, int hasm, double zval, double mval)
1061 {
1062  /* TODO handle zero-length point arrays */
1063  uint32_t i;
1064  int in_hasz = FLAGS_GET_Z(pa->flags);
1065  int in_hasm = FLAGS_GET_M(pa->flags);
1066  POINT4D pt;
1067  POINTARRAY *pa_out = ptarray_construct_empty(hasz, hasm, pa->npoints);
1068 
1069  for( i = 0; i < pa->npoints; i++ )
1070  {
1071  getPoint4d_p(pa, i, &pt);
1072  if( hasz && ! in_hasz )
1073  pt.z = zval;
1074  if( hasm && ! in_hasm )
1075  pt.m = mval;
1076  ptarray_append_point(pa_out, &pt, LW_TRUE);
1077  }
1078 
1079  return pa_out;
1080 }
1081 
1082 POINTARRAY *
1083 ptarray_substring(POINTARRAY *ipa, double from, double to, double tolerance)
1084 {
1085  POINTARRAY *dpa;
1086  POINT4D pt;
1087  POINT4D p1, p2;
1088  POINT4D *p1ptr=&p1; /* don't break strict-aliasing rule */
1089  POINT4D *p2ptr=&p2;
1090  int nsegs, i;
1091  double length, slength, tlength;
1092  int state = 0; /* 0=before, 1=inside */
1093 
1094  /*
1095  * Create a dynamic pointarray with an initial capacity
1096  * equal to full copy of input points
1097  */
1099 
1100  /* Compute total line length */
1101  length = ptarray_length_2d(ipa);
1102 
1103 
1104  LWDEBUGF(3, "Total length: %g", length);
1105 
1106 
1107  /* Get 'from' and 'to' lengths */
1108  from = length*from;
1109  to = length*to;
1110 
1111 
1112  LWDEBUGF(3, "From/To: %g/%g", from, to);
1113 
1114 
1115  tlength = 0;
1116  getPoint4d_p(ipa, 0, &p1);
1117  nsegs = ipa->npoints - 1;
1118  for ( i = 0; i < nsegs; i++ )
1119  {
1120  double dseg;
1121 
1122  getPoint4d_p(ipa, i+1, &p2);
1123 
1124 
1125  LWDEBUGF(3 ,"Segment %d: (%g,%g,%g,%g)-(%g,%g,%g,%g)",
1126  i, p1.x, p1.y, p1.z, p1.m, p2.x, p2.y, p2.z, p2.m);
1127 
1128 
1129  /* Find the length of this segment */
1130  slength = distance2d_pt_pt((POINT2D *)p1ptr, (POINT2D *)p2ptr);
1131 
1132  /*
1133  * We are before requested start.
1134  */
1135  if ( state == 0 ) /* before */
1136  {
1137 
1138  LWDEBUG(3, " Before start");
1139 
1140  if ( fabs ( from - ( tlength + slength ) ) <= tolerance )
1141  {
1142 
1143  LWDEBUG(3, " Second point is our start");
1144 
1145  /*
1146  * Second point is our start
1147  */
1148  ptarray_append_point(dpa, &p2, LW_FALSE);
1149  state=1; /* we're inside now */
1150  goto END;
1151  }
1152 
1153  else if ( fabs(from - tlength) <= tolerance )
1154  {
1155 
1156  LWDEBUG(3, " First point is our start");
1157 
1158  /*
1159  * First point is our start
1160  */
1161  ptarray_append_point(dpa, &p1, LW_FALSE);
1162 
1163  /*
1164  * We're inside now, but will check
1165  * 'to' point as well
1166  */
1167  state=1;
1168  }
1169 
1170  /*
1171  * Didn't reach the 'from' point,
1172  * nothing to do
1173  */
1174  else if ( from > tlength + slength ) goto END;
1175 
1176  else /* tlength < from < tlength+slength */
1177  {
1178 
1179  LWDEBUG(3, " Seg contains first point");
1180 
1181  /*
1182  * Our start is between first and
1183  * second point
1184  */
1185  dseg = (from - tlength) / slength;
1186 
1187  interpolate_point4d(&p1, &p2, &pt, dseg);
1188 
1189  ptarray_append_point(dpa, &pt, LW_FALSE);
1190 
1191  /*
1192  * We're inside now, but will check
1193  * 'to' point as well
1194  */
1195  state=1;
1196  }
1197  }
1198 
1199  if ( state == 1 ) /* inside */
1200  {
1201 
1202  LWDEBUG(3, " Inside");
1203 
1204  /*
1205  * 'to' point is our second point.
1206  */
1207  if ( fabs(to - ( tlength + slength ) ) <= tolerance )
1208  {
1209 
1210  LWDEBUG(3, " Second point is our end");
1211 
1212  ptarray_append_point(dpa, &p2, LW_FALSE);
1213  break; /* substring complete */
1214  }
1215 
1216  /*
1217  * 'to' point is our first point.
1218  * (should only happen if 'to' is 0)
1219  */
1220  else if ( fabs(to - tlength) <= tolerance )
1221  {
1222 
1223  LWDEBUG(3, " First point is our end");
1224 
1225  ptarray_append_point(dpa, &p1, LW_FALSE);
1226 
1227  break; /* substring complete */
1228  }
1229 
1230  /*
1231  * Didn't reach the 'end' point,
1232  * just copy second point
1233  */
1234  else if ( to > tlength + slength )
1235  {
1236  ptarray_append_point(dpa, &p2, LW_FALSE);
1237  goto END;
1238  }
1239 
1240  /*
1241  * 'to' point falls on this segment
1242  * Interpolate and break.
1243  */
1244  else if ( to < tlength + slength )
1245  {
1246 
1247  LWDEBUG(3, " Seg contains our end");
1248 
1249  dseg = (to - tlength) / slength;
1250  interpolate_point4d(&p1, &p2, &pt, dseg);
1251 
1252  ptarray_append_point(dpa, &pt, LW_FALSE);
1253 
1254  break;
1255  }
1256 
1257  else
1258  {
1259  LWDEBUG(3, "Unhandled case");
1260  }
1261  }
1262 
1263 
1264 END:
1265 
1266  tlength += slength;
1267  memcpy(&p1, &p2, sizeof(POINT4D));
1268  }
1269 
1270  LWDEBUGF(3, "Out of loop, ptarray has %d points", dpa->npoints);
1271 
1272  return dpa;
1273 }
1274 
1275 /*
1276  * Write into the *ret argument coordinates of the closes point on
1277  * the given segment to the reference input point.
1278  */
1279 void
1280 closest_point_on_segment(const POINT4D *p, const POINT4D *A, const POINT4D *B, POINT4D *ret)
1281 {
1282  double r;
1283 
1284  if ( FP_EQUALS(A->x, B->x) && FP_EQUALS(A->y, B->y) )
1285  {
1286  *ret = *A;
1287  return;
1288  }
1289 
1290  /*
1291  * We use comp.graphics.algorithms Frequently Asked Questions method
1292  *
1293  * (1) AC dot AB
1294  * r = ----------
1295  * ||AB||^2
1296  * r has the following meaning:
1297  * r=0 P = A
1298  * r=1 P = B
1299  * r<0 P is on the backward extension of AB
1300  * r>1 P is on the forward extension of AB
1301  * 0<r<1 P is interior to AB
1302  *
1303  */
1304  r = ( (p->x-A->x) * (B->x-A->x) + (p->y-A->y) * (B->y-A->y) )/( (B->x-A->x)*(B->x-A->x) +(B->y-A->y)*(B->y-A->y) );
1305 
1306  if (r<=0)
1307  {
1308  *ret = *A;
1309  return;
1310  }
1311  if (r>=1)
1312  {
1313  *ret = *B;
1314  return;
1315  }
1316 
1317  ret->x = A->x + ( (B->x - A->x) * r );
1318  ret->y = A->y + ( (B->y - A->y) * r );
1319  ret->z = A->z + ( (B->z - A->z) * r );
1320  ret->m = A->m + ( (B->m - A->m) * r );
1321 }
1322 
1323 int
1324 ptarray_closest_segment_2d(const POINTARRAY *pa, const POINT2D *qp, double *dist)
1325 {
1326  const POINT2D *start = getPoint2d_cp(pa, 0), *end = NULL;
1327  uint32_t t, seg=0;
1328  double mindist=DBL_MAX;
1329 
1330  /* Loop through pointarray looking for nearest segment */
1331  for (t=1; t<pa->npoints; t++)
1332  {
1333  double dist_sqr;
1334  end = getPoint2d_cp(pa, t);
1335  dist_sqr = distance2d_sqr_pt_seg(qp, start, end);
1336 
1337  if (dist_sqr < mindist)
1338  {
1339  mindist = dist_sqr;
1340  seg=t-1;
1341  if ( mindist == 0 )
1342  {
1343  LWDEBUG(3, "Breaking on mindist=0");
1344  break;
1345  }
1346  }
1347 
1348  start = end;
1349  }
1350 
1351  if ( dist ) *dist = sqrt(mindist);
1352  return seg;
1353 }
1354 
1355 
1356 int
1357 ptarray_closest_vertex_2d(const POINTARRAY *pa, const POINT2D *qp, double *dist)
1358 {
1359  uint32_t t, pn=0;
1360  const POINT2D *p;
1361  double mindist = DBL_MAX;
1362 
1363  /* Loop through pointarray looking for nearest segment */
1364  for (t=0; t<pa->npoints; t++)
1365  {
1366  double dist_sqr;
1367  p = getPoint2d_cp(pa, t);
1368  dist_sqr = distance2d_sqr_pt_pt(p, qp);
1369 
1370  if (dist_sqr < mindist)
1371  {
1372  mindist = dist_sqr;
1373  pn = t;
1374  if ( mindist == 0 )
1375  {
1376  LWDEBUG(3, "Breaking on mindist=0");
1377  break;
1378  }
1379  }
1380  }
1381  if ( dist ) *dist = sqrt(mindist);
1382  return pn;
1383 }
1384 
1385 /*
1386  * Given a point, returns the location of closest point on pointarray
1387  * and, optionally, it's actual distance from the point array.
1388  */
1389 double
1390 ptarray_locate_point(const POINTARRAY *pa, const POINT4D *p4d, double *mindistout, POINT4D *proj4d)
1391 {
1392  double mindist=DBL_MAX;
1393  double tlen, plen;
1394  uint32_t t, seg=0;
1395  POINT4D start4d, end4d, projtmp;
1396  POINT2D proj, p;
1397  const POINT2D *start = NULL, *end = NULL;
1398 
1399  /* Initialize our 2D copy of the input parameter */
1400  p.x = p4d->x;
1401  p.y = p4d->y;
1402 
1403  if ( ! proj4d ) proj4d = &projtmp;
1404 
1405  /* Check for special cases (length 0 and 1) */
1406  if ( pa->npoints <= 1 )
1407  {
1408  if ( pa->npoints == 1 )
1409  {
1410  getPoint4d_p(pa, 0, proj4d);
1411  if ( mindistout )
1412  *mindistout = distance2d_pt_pt(&p, getPoint2d_cp(pa, 0));
1413  }
1414  return 0.0;
1415  }
1416 
1417  start = getPoint2d_cp(pa, 0);
1418  /* Loop through pointarray looking for nearest segment */
1419  for (t=1; t<pa->npoints; t++)
1420  {
1421  double dist_sqr;
1422  end = getPoint2d_cp(pa, t);
1423  dist_sqr = distance2d_sqr_pt_seg(&p, start, end);
1424 
1425  if (dist_sqr < mindist)
1426  {
1427  mindist = dist_sqr;
1428  seg=t-1;
1429  if ( mindist == 0 )
1430  {
1431  LWDEBUG(3, "Breaking on mindist=0");
1432  break;
1433  }
1434  }
1435 
1436  start = end;
1437  }
1438  mindist = sqrt(mindist);
1439 
1440  if ( mindistout ) *mindistout = mindist;
1441 
1442  LWDEBUGF(3, "Closest segment: %d", seg);
1443  LWDEBUGF(3, "mindist: %g", mindist);
1444 
1445  /*
1446  * We need to project the
1447  * point on the closest segment.
1448  */
1449  getPoint4d_p(pa, seg, &start4d);
1450  getPoint4d_p(pa, seg+1, &end4d);
1451  closest_point_on_segment(p4d, &start4d, &end4d, proj4d);
1452 
1453  /* Copy 4D values into 2D holder */
1454  proj.x = proj4d->x;
1455  proj.y = proj4d->y;
1456 
1457  LWDEBUGF(3, "Closest segment:%d, npoints:%d", seg, pa->npoints);
1458 
1459  /* For robustness, force 1 when closest point == endpoint */
1460  if ( (seg >= (pa->npoints-2)) && p2d_same(&proj, end) )
1461  {
1462  return 1.0;
1463  }
1464 
1465  LWDEBUGF(3, "Closest point on segment: %g,%g", proj.x, proj.y);
1466 
1467  tlen = ptarray_length_2d(pa);
1468 
1469  LWDEBUGF(3, "tlen %g", tlen);
1470 
1471  /* Location of any point on a zero-length line is 0 */
1472  /* See http://trac.osgeo.org/postgis/ticket/1772#comment:2 */
1473  if ( tlen == 0 ) return 0;
1474 
1475  plen=0;
1476  start = getPoint2d_cp(pa, 0);
1477  for (t=0; t<seg; t++, start=end)
1478  {
1479  end = getPoint2d_cp(pa, t+1);
1480  plen += distance2d_pt_pt(start, end);
1481 
1482  LWDEBUGF(4, "Segment %d made plen %g", t, plen);
1483  }
1484 
1485  plen+=distance2d_pt_pt(&proj, start);
1486 
1487  LWDEBUGF(3, "plen %g, tlen %g", plen, tlen);
1488 
1489  return plen/tlen;
1490 }
1491 
1501 void
1503 {
1504  uint32_t i;
1505  double x;
1506 
1507  for (i=0; i<pa->npoints; i++)
1508  {
1509  memcpy(&x, getPoint_internal(pa, i), sizeof(double));
1510  if ( x < 0 ) x+= 360;
1511  else if ( x > 180 ) x -= 360;
1512  memcpy(getPoint_internal(pa, i), &x, sizeof(double));
1513  }
1514 }
1515 
1516 
1517 /*
1518  * Returns a POINTARRAY with consecutive equal points
1519  * removed. Equality test on all dimensions of input.
1520  *
1521  * Always returns a newly allocated object.
1522  */
1523 static POINTARRAY *
1524 ptarray_remove_repeated_points_minpoints(const POINTARRAY *in, double tolerance, int minpoints)
1525 {
1526  POINTARRAY *out = ptarray_clone_deep(in);
1527  ptarray_remove_repeated_points_in_place(out, tolerance, minpoints);
1528  return out;
1529 }
1530 
1531 POINTARRAY *
1532 ptarray_remove_repeated_points(const POINTARRAY *in, double tolerance)
1533 {
1534  return ptarray_remove_repeated_points_minpoints(in, tolerance, 2);
1535 }
1536 
1537 
1538 void
1539 ptarray_remove_repeated_points_in_place(POINTARRAY *pa, double tolerance, uint32_t min_points)
1540 {
1541  uint32_t i;
1542  double tolsq = tolerance * tolerance;
1543  const POINT2D *last = NULL;
1544  const POINT2D *pt;
1545  uint32_t n_points = pa->npoints;
1546  uint32_t n_points_out = 1;
1547  size_t pt_size = ptarray_point_size(pa);
1548 
1549  double dsq = FLT_MAX;
1550 
1551  /* No-op on short inputs */
1552  if ( n_points <= min_points ) return;
1553 
1554  last = getPoint2d_cp(pa, 0);
1555  void *p_to = ((char *)last) + pt_size;
1556  for (i = 1; i < n_points; i++)
1557  {
1558  int last_point = (i == n_points - 1);
1559 
1560  /* Look straight into the abyss */
1561  pt = getPoint2d_cp(pa, i);
1562 
1563  /* Don't drop points if we are running short of points */
1564  if (n_points + n_points_out > min_points + i)
1565  {
1566  if (tolerance > 0.0)
1567  {
1568  /* Only drop points that are within our tolerance */
1569  dsq = distance2d_sqr_pt_pt(last, pt);
1570  /* Allow any point but the last one to be dropped */
1571  if (!last_point && dsq <= tolsq)
1572  {
1573  continue;
1574  }
1575  }
1576  else
1577  {
1578  /* At tolerance zero, only skip exact dupes */
1579  if (memcmp((char*)pt, (char*)last, pt_size) == 0)
1580  continue;
1581  }
1582 
1583  /* Got to last point, and it's not very different from */
1584  /* the point that preceded it. We want to keep the last */
1585  /* point, not the second-to-last one, so we pull our write */
1586  /* index back one value */
1587  if (last_point && n_points_out > 1 && tolerance > 0.0 && dsq <= tolsq)
1588  {
1589  n_points_out--;
1590  p_to = (char*)p_to - pt_size;
1591  }
1592  }
1593 
1594  /* Compact all remaining values to front of array */
1595  memcpy(p_to, pt, pt_size);
1596  n_points_out++;
1597  p_to = (char*)p_to + pt_size;
1598  last = pt;
1599  }
1600  /* Adjust array length */
1601  pa->npoints = n_points_out;
1602  return;
1603 }
1604 
1605 /* Out of the points in pa [itfist .. itlast], finds the one that's farthest away from
1606  * the segment determined by pts[itfist] and pts[itlast].
1607  * Returns itfirst if no point was found further away than max_distance_sqr
1608  */
1609 static uint32_t
1610 ptarray_dp_findsplit_in_place(const POINTARRAY *pts, uint32_t it_first, uint32_t it_last, double max_distance_sqr)
1611 {
1612  uint32_t split = it_first;
1613  if ((it_first - it_last) < 2)
1614  return it_first;
1615 
1616  const POINT2D *A = getPoint2d_cp(pts, it_first);
1617  const POINT2D *B = getPoint2d_cp(pts, it_last);
1618 
1619  if (distance2d_sqr_pt_pt(A, B) < DBL_EPSILON)
1620  {
1621  /* If p1 == p2, we can just calculate the distance from each point to A */
1622  for (uint32_t itk = it_first + 1; itk < it_last; itk++)
1623  {
1624  const POINT2D *pk = getPoint2d_cp(pts, itk);
1625  double distance_sqr = distance2d_sqr_pt_pt(pk, A);
1626  if (distance_sqr > max_distance_sqr)
1627  {
1628  split = itk;
1629  max_distance_sqr = distance_sqr;
1630  }
1631  }
1632  return split;
1633  }
1634 
1635  /* This is based on distance2d_sqr_pt_seg, but heavily inlined here to avoid recalculations */
1636  double ba_x = (B->x - A->x);
1637  double ba_y = (B->y - A->y);
1638  double ab_length_sqr = (ba_x * ba_x + ba_y * ba_y);
1639  /* To avoid the division by ab_length_sqr in the 3rd path, we normalize here
1640  * and multiply in the first two paths [(dot_ac_ab < 0) and (> ab_length_sqr)] */
1641  max_distance_sqr *= ab_length_sqr;
1642  for (uint32_t itk = it_first + 1; itk < it_last; itk++)
1643  {
1644  const POINT2D *C = getPoint2d_cp(pts, itk);
1645  double distance_sqr;
1646  double ca_x = (C->x - A->x);
1647  double ca_y = (C->y - A->y);
1648  double dot_ac_ab = (ca_x * ba_x + ca_y * ba_y);
1649 
1650  if (dot_ac_ab <= 0.0)
1651  {
1652  distance_sqr = distance2d_sqr_pt_pt(C, A) * ab_length_sqr;
1653  }
1654  else if (dot_ac_ab >= ab_length_sqr)
1655  {
1656  distance_sqr = distance2d_sqr_pt_pt(C, B) * ab_length_sqr;
1657  }
1658  else
1659  {
1660  double s_numerator = ca_x * ba_y - ca_y * ba_x;
1661  distance_sqr = s_numerator * s_numerator; /* Missing division by ab_length_sqr on purpose */
1662  }
1663 
1664  if (distance_sqr > max_distance_sqr)
1665  {
1666  split = itk;
1667  max_distance_sqr = distance_sqr;
1668  }
1669  }
1670  return split;
1671 }
1672 
1673 /* O(N) simplification for tolerance = 0 */
1674 static void
1676 {
1677  uint32_t kept_it = 0;
1678  uint32_t last_it = pa->npoints - 1;
1679  const POINT2D *kept_pt = getPoint2d_cp(pa, 0);
1680  const size_t pt_size = ptarray_point_size(pa);
1681 
1682  for (uint32_t i = 1; i < last_it; i++)
1683  {
1684  const POINT2D *curr_pt = getPoint2d_cp(pa, i);
1685  const POINT2D *next_pt = getPoint2d_cp(pa, i + 1);
1686 
1687  double ba_x = next_pt->x - kept_pt->x;
1688  double ba_y = next_pt->y - kept_pt->y;
1689  double ab_length_sqr = ba_x * ba_x + ba_y * ba_y;
1690 
1691  double ca_x = curr_pt->x - kept_pt->x;
1692  double ca_y = curr_pt->y - kept_pt->y;
1693  double dot_ac_ab = ca_x * ba_x + ca_y * ba_y;
1694  double s_numerator = ca_x * ba_y - ca_y * ba_x;
1695 
1696  if (p2d_same(kept_pt, next_pt) ||
1697  dot_ac_ab < 0.0 ||
1698  dot_ac_ab > ab_length_sqr ||
1699  s_numerator != 0)
1700 
1701  {
1702  kept_it++;
1703  kept_pt = curr_pt;
1704  if (kept_it != i)
1705  memcpy(pa->serialized_pointlist + pt_size * kept_it,
1706  pa->serialized_pointlist + pt_size * i,
1707  pt_size);
1708  }
1709  }
1710 
1711  /* Append last point */
1712  kept_it++;
1713  if (kept_it != last_it)
1714  memcpy(pa->serialized_pointlist + pt_size * kept_it,
1715  pa->serialized_pointlist + pt_size * last_it,
1716  pt_size);
1717  pa->npoints = kept_it + 1;
1718 }
1719 
1720 void
1721 ptarray_simplify_in_place(POINTARRAY *pa, double tolerance, uint32_t minpts)
1722 {
1723  /* Do not try to simplify really short things */
1724  if (pa->npoints < 3 || pa->npoints <= minpts)
1725  return;
1726 
1727  if (tolerance == 0 && minpts <= 2)
1728  {
1730  return;
1731  }
1732 
1733  /* We use this array to keep track of the points we are keeping, so
1734  * we store just TRUE / FALSE in their position */
1735  uint8_t *kept_points = lwalloc(sizeof(uint8_t) * pa->npoints);
1736  memset(kept_points, LW_FALSE, sizeof(uint8_t) * pa->npoints);
1737  kept_points[0] = LW_TRUE;
1738  kept_points[pa->npoints - 1] = LW_TRUE;
1739  uint32_t keptn = 2;
1740 
1741  /* We use this array as a stack to store the iterators that we are going to need
1742  * in the following steps.
1743  * This is ~10% faster than iterating over @kept_points looking for them
1744  */
1745  uint32_t *iterator_stack = lwalloc(sizeof(uint32_t) * pa->npoints);
1746  iterator_stack[0] = 0;
1747  uint32_t iterator_stack_size = 1;
1748 
1749  uint32_t it_first = 0;
1750  uint32_t it_last = pa->npoints - 1;
1751 
1752  const double tolerance_sqr = tolerance * tolerance;
1753  /* For the first @minpts points we ignore the tolerance */
1754  double it_tol = keptn >= minpts ? tolerance_sqr : -1.0;
1755 
1756  while (iterator_stack_size)
1757  {
1758  uint32_t split = ptarray_dp_findsplit_in_place(pa, it_first, it_last, it_tol);
1759  if (split == it_first)
1760  {
1761  it_first = it_last;
1762  it_last = iterator_stack[--iterator_stack_size];
1763  }
1764  else
1765  {
1766  kept_points[split] = LW_TRUE;
1767  keptn++;
1768 
1769  iterator_stack[iterator_stack_size++] = it_last;
1770  it_last = split;
1771  it_tol = keptn >= minpts ? tolerance_sqr : -1.0;
1772  }
1773  }
1774 
1775  const size_t pt_size = ptarray_point_size(pa);
1776  /* The first point is already in place, so we don't need to copy it */
1777  size_t kept_it = 1;
1778  if (keptn == 2)
1779  {
1780  /* If there are 2 points remaining, it has to be first and last as
1781  * we added those at the start */
1782  memcpy(pa->serialized_pointlist + pt_size * kept_it,
1783  pa->serialized_pointlist + pt_size * (pa->npoints - 1),
1784  pt_size);
1785  }
1786  else if (pa->npoints != keptn) /* We don't need to move any points if we are keeping them all */
1787  {
1788  for (uint32_t i = 1; i < pa->npoints; i++)
1789  {
1790  if (kept_points[i])
1791  {
1792  memcpy(pa->serialized_pointlist + pt_size * kept_it,
1793  pa->serialized_pointlist + pt_size * i,
1794  pt_size);
1795  kept_it++;
1796  }
1797  }
1798  }
1799  pa->npoints = keptn;
1800 
1801  lwfree(kept_points);
1802  lwfree(iterator_stack);
1803 }
1804 
1805 /************************************************************************/
1806 
1812 double
1814 {
1815  double dist = 0.0;
1816  uint32_t i;
1817  const POINT2D *a1;
1818  const POINT2D *a2;
1819  const POINT2D *a3;
1820 
1821  if ( pts->npoints % 2 != 1 )
1822  lwerror("arc point array with even number of points");
1823 
1824  a1 = getPoint2d_cp(pts, 0);
1825 
1826  for ( i=2; i < pts->npoints; i += 2 )
1827  {
1828  a2 = getPoint2d_cp(pts, i-1);
1829  a3 = getPoint2d_cp(pts, i);
1830  dist += lw_arc_length(a1, a2, a3);
1831  a1 = a3;
1832  }
1833  return dist;
1834 }
1835 
1839 double
1841 {
1842  double dist = 0.0;
1843  uint32_t i;
1844  const POINT2D *frm;
1845  const POINT2D *to;
1846 
1847  if ( pts->npoints < 2 ) return 0.0;
1848 
1849  frm = getPoint2d_cp(pts, 0);
1850 
1851  for ( i=1; i < pts->npoints; i++ )
1852  {
1853  to = getPoint2d_cp(pts, i);
1854 
1855  dist += sqrt( ((frm->x - to->x)*(frm->x - to->x)) +
1856  ((frm->y - to->y)*(frm->y - to->y)) );
1857 
1858  frm = to;
1859  }
1860  return dist;
1861 }
1862 
1867 double
1869 {
1870  double dist = 0.0;
1871  uint32_t i;
1872  POINT3DZ frm;
1873  POINT3DZ to;
1874 
1875  if ( pts->npoints < 2 ) return 0.0;
1876 
1877  /* compute 2d length if 3d is not available */
1878  if ( ! FLAGS_GET_Z(pts->flags) ) return ptarray_length_2d(pts);
1879 
1880  getPoint3dz_p(pts, 0, &frm);
1881  for ( i=1; i < pts->npoints; i++ )
1882  {
1883  getPoint3dz_p(pts, i, &to);
1884  dist += sqrt( ((frm.x - to.x)*(frm.x - to.x)) +
1885  ((frm.y - to.y)*(frm.y - to.y)) +
1886  ((frm.z - to.z)*(frm.z - to.z)) );
1887  frm = to;
1888  }
1889  return dist;
1890 }
1891 
1892 
1893 
1897 void
1899 {
1900  if (FLAGS_GET_Z(pa->flags))
1901  {
1902  for (uint32_t i = 0; i < pa->npoints; i++)
1903  {
1904  POINT4D *p4d = (POINT4D *)(getPoint_internal(pa, i));
1905  double x = p4d->x;
1906  double y = p4d->y;
1907  double z = p4d->z;
1908  p4d->x = a->afac * x + a->bfac * y + a->cfac * z + a->xoff;
1909  p4d->y = a->dfac * x + a->efac * y + a->ffac * z + a->yoff;
1910  p4d->z = a->gfac * x + a->hfac * y + a->ifac * z + a->zoff;
1911  }
1912  }
1913  else
1914  {
1915  for (uint32_t i = 0; i < pa->npoints; i++)
1916  {
1917  POINT2D *pt = (POINT2D *)(getPoint_internal(pa, i));
1918  double x = pt->x;
1919  double y = pt->y;
1920  pt->x = a->afac * x + a->bfac * y + a->xoff;
1921  pt->y = a->dfac * x + a->efac * y + a->yoff;
1922  }
1923  }
1924 }
1925 
1930 #if 0
1931 static int gluInvertMatrix(const double *m, double *invOut)
1932 {
1933  double inv[16], det;
1934  int i;
1935 
1936  inv[0] = m[5] * m[10] * m[15] -
1937  m[5] * m[11] * m[14] -
1938  m[9] * m[6] * m[15] +
1939  m[9] * m[7] * m[14] +
1940  m[13] * m[6] * m[11] -
1941  m[13] * m[7] * m[10];
1942 
1943  inv[4] = -m[4] * m[10] * m[15] +
1944  m[4] * m[11] * m[14] +
1945  m[8] * m[6] * m[15] -
1946  m[8] * m[7] * m[14] -
1947  m[12] * m[6] * m[11] +
1948  m[12] * m[7] * m[10];
1949 
1950  inv[8] = m[4] * m[9] * m[15] -
1951  m[4] * m[11] * m[13] -
1952  m[8] * m[5] * m[15] +
1953  m[8] * m[7] * m[13] +
1954  m[12] * m[5] * m[11] -
1955  m[12] * m[7] * m[9];
1956 
1957  inv[12] = -m[4] * m[9] * m[14] +
1958  m[4] * m[10] * m[13] +
1959  m[8] * m[5] * m[14] -
1960  m[8] * m[6] * m[13] -
1961  m[12] * m[5] * m[10] +
1962  m[12] * m[6] * m[9];
1963 
1964  inv[1] = -m[1] * m[10] * m[15] +
1965  m[1] * m[11] * m[14] +
1966  m[9] * m[2] * m[15] -
1967  m[9] * m[3] * m[14] -
1968  m[13] * m[2] * m[11] +
1969  m[13] * m[3] * m[10];
1970 
1971  inv[5] = m[0] * m[10] * m[15] -
1972  m[0] * m[11] * m[14] -
1973  m[8] * m[2] * m[15] +
1974  m[8] * m[3] * m[14] +
1975  m[12] * m[2] * m[11] -
1976  m[12] * m[3] * m[10];
1977 
1978  inv[9] = -m[0] * m[9] * m[15] +
1979  m[0] * m[11] * m[13] +
1980  m[8] * m[1] * m[15] -
1981  m[8] * m[3] * m[13] -
1982  m[12] * m[1] * m[11] +
1983  m[12] * m[3] * m[9];
1984 
1985  inv[13] = m[0] * m[9] * m[14] -
1986  m[0] * m[10] * m[13] -
1987  m[8] * m[1] * m[14] +
1988  m[8] * m[2] * m[13] +
1989  m[12] * m[1] * m[10] -
1990  m[12] * m[2] * m[9];
1991 
1992  inv[2] = m[1] * m[6] * m[15] -
1993  m[1] * m[7] * m[14] -
1994  m[5] * m[2] * m[15] +
1995  m[5] * m[3] * m[14] +
1996  m[13] * m[2] * m[7] -
1997  m[13] * m[3] * m[6];
1998 
1999  inv[6] = -m[0] * m[6] * m[15] +
2000  m[0] * m[7] * m[14] +
2001  m[4] * m[2] * m[15] -
2002  m[4] * m[3] * m[14] -
2003  m[12] * m[2] * m[7] +
2004  m[12] * m[3] * m[6];
2005 
2006  inv[10] = m[0] * m[5] * m[15] -
2007  m[0] * m[7] * m[13] -
2008  m[4] * m[1] * m[15] +
2009  m[4] * m[3] * m[13] +
2010  m[12] * m[1] * m[7] -
2011  m[12] * m[3] * m[5];
2012 
2013  inv[14] = -m[0] * m[5] * m[14] +
2014  m[0] * m[6] * m[13] +
2015  m[4] * m[1] * m[14] -
2016  m[4] * m[2] * m[13] -
2017  m[12] * m[1] * m[6] +
2018  m[12] * m[2] * m[5];
2019 
2020  inv[3] = -m[1] * m[6] * m[11] +
2021  m[1] * m[7] * m[10] +
2022  m[5] * m[2] * m[11] -
2023  m[5] * m[3] * m[10] -
2024  m[9] * m[2] * m[7] +
2025  m[9] * m[3] * m[6];
2026 
2027  inv[7] = m[0] * m[6] * m[11] -
2028  m[0] * m[7] * m[10] -
2029  m[4] * m[2] * m[11] +
2030  m[4] * m[3] * m[10] +
2031  m[8] * m[2] * m[7] -
2032  m[8] * m[3] * m[6];
2033 
2034  inv[11] = -m[0] * m[5] * m[11] +
2035  m[0] * m[7] * m[9] +
2036  m[4] * m[1] * m[11] -
2037  m[4] * m[3] * m[9] -
2038  m[8] * m[1] * m[7] +
2039  m[8] * m[3] * m[5];
2040 
2041  inv[15] = m[0] * m[5] * m[10] -
2042  m[0] * m[6] * m[9] -
2043  m[4] * m[1] * m[10] +
2044  m[4] * m[2] * m[9] +
2045  m[8] * m[1] * m[6] -
2046  m[8] * m[2] * m[5];
2047 
2048  det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
2049 
2050  if (det == 0)
2051  return LW_FALSE;
2052 
2053  det = 1.0 / det;
2054 
2055  for (i = 0; i < 16; i++)
2056  invOut[i] = inv[i] * det;
2057 
2058  return LW_TRUE;
2059 }
2060 #endif
2061 
2065 void
2067 {
2068  uint32_t i;
2069  POINT4D p4d;
2070  LWDEBUG(3, "ptarray_scale start");
2071  for (i=0; i<pa->npoints; i++)
2072  {
2073  getPoint4d_p(pa, i, &p4d);
2074  p4d.x *= fact->x;
2075  p4d.y *= fact->y;
2076  p4d.z *= fact->z;
2077  p4d.m *= fact->m;
2078  ptarray_set_point4d(pa, i, &p4d);
2079  }
2080  LWDEBUG(3, "ptarray_scale end");
2081 }
2082 
2083 int
2085 {
2086  return getPoint4d_p(pa, 0, pt);
2087 }
2088 
2089 
2090 /*
2091  * Stick an array of points to the given gridspec.
2092  * Return "gridded" points in *outpts and their number in *outptsn.
2093  *
2094  * Two consecutive points falling on the same grid cell are collapsed
2095  * into one single point.
2096  *
2097  */
2098 void
2100 {
2101  uint32_t j = 0;
2102  POINT4D *p, *p_out = NULL;
2103  double x, y, z = 0, m = 0;
2104  uint32_t ndims = FLAGS_NDIMS(pa->flags);
2105  uint32_t has_z = FLAGS_GET_Z(pa->flags);
2106  uint32_t has_m = FLAGS_GET_M(pa->flags);
2107 
2108  for (uint32_t i = 0; i < pa->npoints; i++)
2109  {
2110  /* Look straight into the abyss */
2111  p = (POINT4D *)(getPoint_internal(pa, i));
2112  x = p->x;
2113  y = p->y;
2114  if (ndims > 2)
2115  z = p->z;
2116  if (ndims > 3)
2117  m = p->m;
2118 
2119  /*
2120  * See https://github.com/libgeos/geos/pull/956
2121  * We use scale for rounding when gridsize is < 1 and
2122  * gridsize for rounding when scale < 1.
2123  */
2124  if (grid->xsize > 0) {
2125  if (grid->xsize < 1)
2126  x = rint((x - grid->ipx) * grid->xscale) / grid->xscale + grid->ipx;
2127  else
2128  x = rint((x - grid->ipx) / grid->xsize) * grid->xsize + grid->ipx;
2129  }
2130 
2131  if (grid->ysize > 0) {
2132  if (grid->ysize < 1)
2133  y = rint((y - grid->ipy) * grid->yscale) / grid->yscale + grid->ipy;
2134  else
2135  y = rint((y - grid->ipy) / grid->ysize) * grid->ysize + grid->ipy;
2136  }
2137 
2138  /* Read and round this point */
2139  /* Z is always in third position */
2140  if (has_z && grid->zsize > 0)
2141  z = rint((z - grid->ipz) / grid->zsize) * grid->zsize + grid->ipz;
2142 
2143  /* M might be in 3rd or 4th position */
2144  if (has_m && grid->msize > 0)
2145  {
2146  /* In POINT ZM, M is in 4th position, in POINT M, M is in 3rd position which is Z in POINT4D */
2147  if (has_z)
2148  m = rint((m - grid->ipm) / grid->msize) * grid->msize + grid->ipm;
2149  else
2150  z = rint((z - grid->ipm) / grid->msize) * grid->msize + grid->ipm;
2151  }
2152 
2153  /* Skip duplicates */
2154  if (p_out &&
2155  p_out->x == x &&
2156  p_out->y == y &&
2157  (ndims > 2 ? p_out->z == z : 1) &&
2158  (ndims > 3 ? p_out->m == m : 1))
2159  {
2160  continue;
2161  }
2162 
2163  /* Write rounded values into the next available point */
2164  p_out = (POINT4D *)(getPoint_internal(pa, j++));
2165  p_out->x = x;
2166  p_out->y = y;
2167  if (ndims > 2)
2168  p_out->z = z;
2169  if (ndims > 3)
2170  p_out->m = m;
2171  }
2172 
2173  /* Update output ptarray length */
2174  pa->npoints = j;
2175  return;
2176 }
2177 
2178 int
2179 ptarray_npoints_in_rect(const POINTARRAY *pa, const GBOX *gbox)
2180 {
2181  const POINT2D *pt;
2182  int n = 0;
2183  uint32_t i;
2184  for ( i = 0; i < pa->npoints; i++ )
2185  {
2186  pt = getPoint2d_cp(pa, i);
2187  if ( gbox_contains_point2d(gbox, pt) )
2188  n++;
2189  }
2190  return n;
2191 }
2192 
2193 
2194 /*
2195  * Reorder the vertices of a closed pointarray so that the
2196  * given point is the first/last one.
2197  *
2198  * Error out if pointarray is not closed or it does not
2199  * contain the given point.
2200  */
2201 int
2203 {
2204  POINTARRAY *tmp;
2205  int found;
2206  uint32_t it;
2207  int ptsize;
2208 
2209  if ( ! ptarray_is_closed_2d(pa) )
2210  {
2211  lwerror("ptarray_scroll_in_place: input POINTARRAY is not closed");
2212  return LW_FAILURE;
2213  }
2214 
2215  ptsize = ptarray_point_size(pa);
2216 
2217  /* Find the point in the array */
2218  found = 0;
2219  for ( it = 0; it < pa->npoints; ++it )
2220  {
2221  if ( ! memcmp(getPoint_internal(pa, it), pt, ptsize) )
2222  {
2223  found = 1;
2224  break;
2225  }
2226  }
2227 
2228  if ( ! found )
2229  {
2230  lwerror("ptarray_scroll_in_place: input POINTARRAY does not contain the given point");
2231  return LW_FAILURE;
2232  }
2233 
2234  if ( 0 == it )
2235  {
2236  /* Point is already the start/end point, just clone the input */
2237  return LW_SUCCESS;
2238  }
2239 
2240  /* TODO: reduce allocations */
2242 
2243  memset(getPoint_internal(tmp, 0), 0, (size_t)ptsize * pa->npoints);
2244  /* Copy the block from found point to last point into the output array */
2245  memcpy(
2246  getPoint_internal(tmp, 0),
2247  getPoint_internal(pa, it),
2248  (size_t)ptsize * ( pa->npoints - it )
2249  );
2250 
2251  /* Copy the block from second point to the found point into the last portion of the
2252  * return */
2253  memcpy(
2254  getPoint_internal(tmp, pa->npoints - it),
2255  getPoint_internal(pa, 1),
2256  (size_t)ptsize * ( it )
2257  );
2258 
2259  /* Copy the resulting pointarray back to source one */
2260  memcpy(
2261  getPoint_internal(pa, 0),
2262  getPoint_internal(tmp, 0),
2263  (size_t)ptsize * ( pa->npoints )
2264  );
2265 
2266  ptarray_free(tmp);
2267 
2268  return LW_SUCCESS;
2269 }
char * r
Definition: cu_in_wkt.c:24
int gbox_contains_point2d(const GBOX *g, const POINT2D *p)
Definition: gbox.c:362
int lw_arc_calculate_gbox_cartesian_2d(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, GBOX *gbox)
Definition: gbox.c:465
#define LW_FALSE
Definition: liblwgeom.h:94
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2445
#define LW_FAILURE
Definition: liblwgeom.h:96
void interpolate_point4d(const POINT4D *A, const POINT4D *B, POINT4D *I, double F)
Find interpolation point I between point A and point B so that the len(AI) == len(AB)*F and I falls o...
Definition: lwgeom_api.c:646
#define LW_SUCCESS
Definition: liblwgeom.h:97
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:342
#define FLAGS_GET_READONLY(flags)
Definition: liblwgeom.h:169
#define FLAGS_GET_Z(flags)
Definition: liblwgeom.h:165
int getPoint3dz_p(const POINTARRAY *pa, uint32_t n, POINT3DZ *point)
Definition: lwgeom_api.c:215
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:242
void lwfree(void *mem)
Definition: lwutil.c:248
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:179
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:166
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
double distance2d_sqr_pt_seg(const POINT2D *p, const POINT2D *A, const POINT2D *B)
Definition: measures.c:2455
#define FLAGS_GET_ZM(flags)
Definition: liblwgeom.h:180
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define FLAGS_SET_READONLY(flags, value)
Definition: liblwgeom.h:176
lwflags_t lwflags(int hasz, int hasm, int geodetic)
Construct a new flags bitmask.
Definition: lwutil.c:477
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:369
enum LWORD_T LWORD
Ordinate names.
#define LW_INSIDE
Constants for point-in-polygon return values.
#define LW_BOUNDARY
double lw_arc_length(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3)
Returns the length of a circular arc segment.
Definition: lwalgorithm.c:124
#define LW_ON_INTERRUPT(x)
#define FP_MAX(A, B)
#define FP_MIN(A, B)
double lw_arc_center(const POINT2D *p1, const POINT2D *p2, const POINT2D *p3, POINT2D *result)
Determines the center of the circle defined by the three given points.
Definition: lwalgorithm.c:234
int lw_arc_side(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, const POINT2D *Q)
Definition: lwalgorithm.c:184
#define FP_EQUALS(A, B)
int lw_pt_in_seg(const POINT2D *P, const POINT2D *A1, const POINT2D *A2)
Returns true if P is between A1/A2.
Definition: lwalgorithm.c:101
#define LW_OUTSIDE
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
Definition: lwalgorithm.c:70
int lw_arc_is_pt(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3)
Returns true if arc A is actually a point (all vertices are the same) .
Definition: lwalgorithm.c:111
int lw_pt_in_arc(const POINT2D *P, const POINT2D *A1, const POINT2D *A2, const POINT2D *A3)
Returns true if P is on the same side of the plane partition defined by A1/A3 as A2 is.
Definition: lwalgorithm.c:91
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:57
static double det(double m00, double m01, double m10, double m11)
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:101
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:106
void lwnotice(const char *fmt,...) __attribute__((format(printf
Write a notice out to the notice handler.
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
#define INT32_MAX
Definition: lwin_wkt_lex.c:334
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:97
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: lwinline.h:33
static size_t ptarray_point_size(const POINTARRAY *pa)
Definition: lwinline.h:56
static uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
Definition: lwinline.h:75
int ptarray_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
Definition: ptarray.c:763
POINTARRAY * ptarray_force_dims(const POINTARRAY *pa, int hasz, int hasm, double zval, double mval)
Definition: ptarray.c:1060
static uint32_t ptarray_dp_findsplit_in_place(const POINTARRAY *pts, uint32_t it_first, uint32_t it_last, double max_distance_sqr)
Definition: ptarray.c:1610
POINTARRAY * ptarray_construct_reference_data(char hasz, char hasm, uint32_t npoints, uint8_t *ptlist)
Build a new POINTARRAY, but on top of someone else's ordinate array.
Definition: ptarray.c:291
int ptarray_remove_point(POINTARRAY *pa, uint32_t where)
Remove a point from an existing POINTARRAY.
Definition: ptarray.c:259
void ptarray_longitude_shift(POINTARRAY *pa)
Longitude shift for a pointarray.
Definition: ptarray.c:1502
POINTARRAY * ptarray_construct_copy_data(char hasz, char hasm, uint32_t npoints, const uint8_t *ptlist)
Construct a new POINTARRAY, copying in the data from ptlist.
Definition: ptarray.c:305
int ptarray_closest_segment_2d(const POINTARRAY *pa, const POINT2D *qp, double *dist)
Definition: ptarray.c:1324
POINTARRAY * ptarray_clone(const POINTARRAY *in)
Clone a POINTARRAY object.
Definition: ptarray.c:674
int ptarray_append_ptarray(POINTARRAY *pa1, POINTARRAY *pa2, double gap_tolerance)
Append a POINTARRAY, pa2 to the end of an existing POINTARRAY, pa1.
Definition: ptarray.c:177
void ptarray_reverse_in_place(POINTARRAY *pa)
Definition: ptarray.c:339
int ptarray_closest_vertex_2d(const POINTARRAY *pa, const POINT2D *qp, double *dist)
Definition: ptarray.c:1357
int ptarrayarc_contains_point(const POINTARRAY *pa, const POINT2D *pt)
For POINTARRAYs representing CIRCULARSTRINGS.
Definition: ptarray.c:856
int ptarray_is_closed_2d(const POINTARRAY *in)
Definition: ptarray.c:710
int ptarray_is_closed_z(const POINTARRAY *in)
Definition: ptarray.c:736
double ptarray_length(const POINTARRAY *pts)
Find the 3d/2d length of the given POINTARRAY (depending on its dimensionality)
Definition: ptarray.c:1868
double ptarray_signed_area(const POINTARRAY *pa)
Returns the area in cartesian units.
Definition: ptarray.c:1020
POINTARRAY * ptarray_addPoint(const POINTARRAY *pa, uint8_t *p, size_t pdims, uint32_t where)
Add a point in a pointarray.
Definition: ptarray.c:530
void closest_point_on_segment(const POINT4D *p, const POINT4D *A, const POINT4D *B, POINT4D *ret)
Definition: ptarray.c:1280
int ptarray_startpoint(const POINTARRAY *pa, POINT4D *pt)
Definition: ptarray.c:2084
int ptarray_is_closed(const POINTARRAY *in)
Check for ring closure using whatever dimensionality is declared on the pointarray.
Definition: ptarray.c:696
POINTARRAY * ptarray_clone_deep(const POINTARRAY *in)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:643
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
Definition: ptarray.c:51
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1840
char ptarray_same(const POINTARRAY *pa1, const POINTARRAY *pa2)
Definition: ptarray.c:484
int ptarray_is_closed_3d(const POINTARRAY *in)
Definition: ptarray.c:723
void ptarray_remove_repeated_points_in_place(POINTARRAY *pa, double tolerance, uint32_t min_points)
Definition: ptarray.c:1539
POINTARRAY * ptarray_removePoint(POINTARRAY *pa, uint32_t which)
Remove a point from a pointarray.
Definition: ptarray.c:582
int ptarray_scroll_in_place(POINTARRAY *pa, const POINT4D *pt)
Definition: ptarray.c:2202
void ptarray_simplify_in_place(POINTARRAY *pa, double tolerance, uint32_t minpts)
Definition: ptarray.c:1721
int ptarray_insert_point(POINTARRAY *pa, const POINT4D *p, uint32_t where)
Insert a point into an existing POINTARRAY.
Definition: ptarray.c:85
void ptarray_grid_in_place(POINTARRAY *pa, gridspec *grid)
Snap to grid.
Definition: ptarray.c:2099
double ptarray_arc_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY, using circular arc interpolation between each coordinate ...
Definition: ptarray.c:1813
POINTARRAY * ptarray_remove_repeated_points(const POINTARRAY *in, double tolerance)
Definition: ptarray.c:1532
void ptarray_affine(POINTARRAY *pa, const AFFINE *a)
Affine transform a pointarray.
Definition: ptarray.c:1898
int ptarray_contains_point(const POINTARRAY *pa, const POINT2D *pt)
Return LW_INSIDE if the point is inside the POINTARRAY, LW_OUTSIDE if it is outside,...
Definition: ptarray.c:751
int ptarrayarc_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
Definition: ptarray.c:862
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:59
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:327
static void ptarray_simplify_in_place_tolerance0(POINTARRAY *pa)
Definition: ptarray.c:1675
int ptarray_isccw(const POINTARRAY *pa)
Definition: ptarray.c:1051
int ptarray_has_z(const POINTARRAY *pa)
Definition: ptarray.c:37
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int repeated_points)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE,...
Definition: ptarray.c:147
void ptarray_scale(POINTARRAY *pa, const POINT4D *fact)
WARNING, make sure you send in only 16-member double arrays or obviously things will go pear-shaped f...
Definition: ptarray.c:2066
static POINTARRAY * ptarray_remove_repeated_points_minpoints(const POINTARRAY *in, double tolerance, int minpoints)
Definition: ptarray.c:1524
void ptarray_swap_ordinates(POINTARRAY *pa, LWORD o1, LWORD o2)
Swap ordinate values o1 and o2 on a given POINTARRAY.
Definition: ptarray.c:387
int ptarray_npoints_in_rect(const POINTARRAY *pa, const GBOX *gbox)
Definition: ptarray.c:2179
char ptarray_same2d(const POINTARRAY *pa1, const POINTARRAY *pa2)
Definition: ptarray.c:509
POINTARRAY * ptarray_substring(POINTARRAY *ipa, double from, double to, double tolerance)
@d1 start location (distance from start / total distance) @d2 end location (distance from start / tot...
Definition: ptarray.c:1083
POINTARRAY * ptarray_merge(POINTARRAY *pa1, POINTARRAY *pa2)
Merge two given POINTARRAY and returns a pointer on the new aggregate one.
Definition: ptarray.c:612
POINTARRAY * ptarray_flip_coordinates(POINTARRAY *pa)
Reverse X and Y axis on a given POINTARRAY.
Definition: ptarray.c:368
int ptarray_has_m(const POINTARRAY *pa)
Definition: ptarray.c:44
POINTARRAY * ptarray_segmentize2d(const POINTARRAY *ipa, double dist)
Returns a modified POINTARRAY so that no segment is longer than the given distance (computed using 2d...
Definition: ptarray.c:413
double ptarray_locate_point(const POINTARRAY *pa, const POINT4D *p4d, double *mindistout, POINT4D *proj4d)
Definition: ptarray.c:1390
double gfac
Definition: liblwgeom.h:332
double zoff
Definition: liblwgeom.h:332
double bfac
Definition: liblwgeom.h:332
double ifac
Definition: liblwgeom.h:332
double xoff
Definition: liblwgeom.h:332
double dfac
Definition: liblwgeom.h:332
double afac
Definition: liblwgeom.h:332
double ffac
Definition: liblwgeom.h:332
double cfac
Definition: liblwgeom.h:332
double hfac
Definition: liblwgeom.h:332
double efac
Definition: liblwgeom.h:332
double yoff
Definition: liblwgeom.h:332
double ymax
Definition: liblwgeom.h:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
double z
Definition: liblwgeom.h:396
double x
Definition: liblwgeom.h:396
double y
Definition: liblwgeom.h:396
double m
Definition: liblwgeom.h:414
double x
Definition: liblwgeom.h:414
double z
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414
lwflags_t flags
Definition: liblwgeom.h:431
uint32_t maxpoints
Definition: liblwgeom.h:428
uint32_t npoints
Definition: liblwgeom.h:427
uint8_t * serialized_pointlist
Definition: liblwgeom.h:434
double xscale
Definition: liblwgeom.h:1407
double ipm
Definition: liblwgeom.h:1402
double zsize
Definition: liblwgeom.h:1405
double ysize
Definition: liblwgeom.h:1404
double xsize
Definition: liblwgeom.h:1403
double yscale
Definition: liblwgeom.h:1408
double ipx
Definition: liblwgeom.h:1399
double msize
Definition: liblwgeom.h:1406
double ipy
Definition: liblwgeom.h:1400
double ipz
Definition: liblwgeom.h:1401
Snap-to-grid.
Definition: liblwgeom.h:1398