PostGIS  2.5.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 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 
30 #include "../postgis_config.h"
31 /*#define POSTGIS_DEBUG_LEVEL 4*/
32 #include "liblwgeom_internal.h"
33 #include "lwgeom_log.h"
34 
35 int
37 {
38  if ( ! pa ) return LW_FALSE;
39  return FLAGS_GET_Z(pa->flags);
40 }
41 
42 int
44 {
45  if ( ! pa ) return LW_FALSE;
46  return FLAGS_GET_M(pa->flags);
47 }
48 
49 /*
50  * Size of point represeneted in the POINTARRAY
51  * 16 for 2d, 24 for 3d, 32 for 4d
52  */
53 inline size_t
55 {
56  LWDEBUGF(5, "ptarray_point_size: FLAGS_NDIMS(pa->flags)=%x",FLAGS_NDIMS(pa->flags));
57 
58  return sizeof(double)*FLAGS_NDIMS(pa->flags);
59 }
60 
62 ptarray_construct(char hasz, char hasm, uint32_t npoints)
63 {
64  POINTARRAY *pa = ptarray_construct_empty(hasz, hasm, npoints);
65  pa->npoints = npoints;
66  return pa;
67 }
68 
70 ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
71 {
72  POINTARRAY *pa = lwalloc(sizeof(POINTARRAY));
73  pa->serialized_pointlist = NULL;
74 
75  /* Set our dimsionality info on the bitmap */
76  pa->flags = gflags(hasz, hasm, 0);
77 
78  /* We will be allocating a bit of room */
79  pa->npoints = 0;
80  pa->maxpoints = maxpoints;
81 
82  /* Allocate the coordinate array */
83  if ( maxpoints > 0 )
84  pa->serialized_pointlist = lwalloc(maxpoints * ptarray_point_size(pa));
85  else
86  pa->serialized_pointlist = NULL;
87 
88  return pa;
89 }
90 
91 /*
92 * Add a point into a pointarray. Only adds as many dimensions as the
93 * pointarray supports.
94 */
95 int
97 {
98  size_t point_size = ptarray_point_size(pa);
99  LWDEBUGF(5,"pa = %p; p = %p; where = %d", pa, p, where);
100  LWDEBUGF(5,"pa->npoints = %d; pa->maxpoints = %d", pa->npoints, pa->maxpoints);
101 
102  if ( FLAGS_GET_READONLY(pa->flags) )
103  {
104  lwerror("ptarray_insert_point: called on read-only point array");
105  return LW_FAILURE;
106  }
107 
108  /* Error on invalid offset value */
109  if ( where > pa->npoints )
110  {
111  lwerror("ptarray_insert_point: offset out of range (%d)", where);
112  return LW_FAILURE;
113  }
114 
115  /* If we have no storage, let's allocate some */
116  if( pa->maxpoints == 0 || ! pa->serialized_pointlist )
117  {
118  pa->maxpoints = 32;
119  pa->npoints = 0;
121  }
122 
123  /* Error out if we have a bad situation */
124  if ( pa->npoints > pa->maxpoints )
125  {
126  lwerror("npoints (%d) is greated than maxpoints (%d)", pa->npoints, pa->maxpoints);
127  return LW_FAILURE;
128  }
129 
130  /* Check if we have enough storage, add more if necessary */
131  if( pa->npoints == pa->maxpoints )
132  {
133  pa->maxpoints *= 2;
135  }
136 
137  /* Make space to insert the new point */
138  if( where < pa->npoints )
139  {
140  size_t copy_size = point_size * (pa->npoints - where);
141  memmove(getPoint_internal(pa, where+1), getPoint_internal(pa, where), copy_size);
142  LWDEBUGF(5,"copying %d bytes to start vertex %d from start vertex %d", copy_size, where+1, where);
143  }
144 
145  /* We have one more point */
146  ++pa->npoints;
147 
148  /* Copy the new point into the gap */
149  ptarray_set_point4d(pa, where, p);
150  LWDEBUGF(5,"copying new point to start vertex %d", point_size, where);
151 
152  return LW_SUCCESS;
153 }
154 
155 int
156 ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int repeated_points)
157 {
158 
159  /* Check for pathology */
160  if( ! pa || ! pt )
161  {
162  lwerror("ptarray_append_point: null input");
163  return LW_FAILURE;
164  }
165 
166  /* Check for duplicate end point */
167  if ( repeated_points == LW_FALSE && pa->npoints > 0 )
168  {
169  POINT4D tmp;
170  getPoint4d_p(pa, pa->npoints-1, &tmp);
171  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);
172 
173  /* Return LW_SUCCESS and do nothing else if previous point in list is equal to this one */
174  if ( (pt->x == tmp.x) && (pt->y == tmp.y) &&
175  (FLAGS_GET_Z(pa->flags) ? pt->z == tmp.z : 1) &&
176  (FLAGS_GET_M(pa->flags) ? pt->m == tmp.m : 1) )
177  {
178  return LW_SUCCESS;
179  }
180  }
181 
182  /* Append is just a special case of insert */
183  return ptarray_insert_point(pa, pt, pa->npoints);
184 }
185 
186 int
187 ptarray_append_ptarray(POINTARRAY *pa1, POINTARRAY *pa2, double gap_tolerance)
188 {
189  unsigned int poff = 0;
190  unsigned int npoints;
191  unsigned int ncap;
192  unsigned int ptsize;
193 
194  /* Check for pathology */
195  if( ! pa1 || ! pa2 )
196  {
197  lwerror("ptarray_append_ptarray: null input");
198  return LW_FAILURE;
199  }
200 
201  npoints = pa2->npoints;
202 
203  if ( ! npoints ) return LW_SUCCESS; /* nothing more to do */
204 
205  if( FLAGS_GET_READONLY(pa1->flags) )
206  {
207  lwerror("ptarray_append_ptarray: target pointarray is read-only");
208  return LW_FAILURE;
209  }
210 
211  if( FLAGS_GET_ZM(pa1->flags) != FLAGS_GET_ZM(pa2->flags) )
212  {
213  lwerror("ptarray_append_ptarray: appending mixed dimensionality is not allowed");
214  return LW_FAILURE;
215  }
216 
217  ptsize = ptarray_point_size(pa1);
218 
219  /* Check for duplicate end point */
220  if ( pa1->npoints )
221  {
222  POINT2D tmp1, tmp2;
223  getPoint2d_p(pa1, pa1->npoints-1, &tmp1);
224  getPoint2d_p(pa2, 0, &tmp2);
225 
226  /* If the end point and start point are the same, then don't copy start point */
227  if (p2d_same(&tmp1, &tmp2)) {
228  poff = 1;
229  --npoints;
230  }
231  else if ( gap_tolerance == 0 || ( gap_tolerance > 0 &&
232  distance2d_pt_pt(&tmp1, &tmp2) > gap_tolerance ) )
233  {
234  lwerror("Second line start point too far from first line end point");
235  return LW_FAILURE;
236  }
237  }
238 
239  /* Check if we need extra space */
240  ncap = pa1->npoints + npoints;
241  if ( pa1->maxpoints < ncap )
242  {
243  pa1->maxpoints = ncap > pa1->maxpoints*2 ?
244  ncap : pa1->maxpoints*2;
245  pa1->serialized_pointlist = lwrealloc(pa1->serialized_pointlist, ptsize * pa1->maxpoints);
246  }
247 
248  memcpy(getPoint_internal(pa1, pa1->npoints),
249  getPoint_internal(pa2, poff), ptsize * npoints);
250 
251  pa1->npoints = ncap;
252 
253  return LW_SUCCESS;
254 }
255 
256 /*
257 * Add a point into a pointarray. Only adds as many dimensions as the
258 * pointarray supports.
259 */
260 int
262 {
263  /* Check for pathology */
264  if( ! pa )
265  {
266  lwerror("ptarray_remove_point: null input");
267  return LW_FAILURE;
268  }
269 
270  /* Error on invalid offset value */
271  if ( where >= pa->npoints )
272  {
273  lwerror("ptarray_remove_point: offset out of range (%d)", where);
274  return LW_FAILURE;
275  }
276 
277  /* If the point is any but the last, we need to copy the data back one point */
278  if (where < pa->npoints - 1)
279  memmove(getPoint_internal(pa, where),
280  getPoint_internal(pa, where + 1),
281  ptarray_point_size(pa) * (pa->npoints - where - 1));
282 
283  /* We have one less point */
284  pa->npoints--;
285 
286  return LW_SUCCESS;
287 }
288 
293 POINTARRAY* ptarray_construct_reference_data(char hasz, char hasm, uint32_t npoints, uint8_t *ptlist)
294 {
295  POINTARRAY *pa = lwalloc(sizeof(POINTARRAY));
296  LWDEBUGF(5, "hasz = %d, hasm = %d, npoints = %d, ptlist = %p", hasz, hasm, npoints, ptlist);
297  pa->flags = gflags(hasz, hasm, 0);
298  FLAGS_SET_READONLY(pa->flags, 1); /* We don't own this memory, so we can't alter or free it. */
299  pa->npoints = npoints;
300  pa->maxpoints = npoints;
301  pa->serialized_pointlist = ptlist;
302  return pa;
303 }
304 
305 
306 POINTARRAY*
307 ptarray_construct_copy_data(char hasz, char hasm, uint32_t npoints, const uint8_t *ptlist)
308 {
309  POINTARRAY *pa = lwalloc(sizeof(POINTARRAY));
310 
311  pa->flags = gflags(hasz, hasm, 0);
312  pa->npoints = npoints;
313  pa->maxpoints = npoints;
314 
315  if ( npoints > 0 )
316  {
317  pa->serialized_pointlist = lwalloc(ptarray_point_size(pa) * npoints);
318  memcpy(pa->serialized_pointlist, ptlist, ptarray_point_size(pa) * npoints);
319  }
320  else
321  {
322  pa->serialized_pointlist = NULL;
323  }
324 
325  return pa;
326 }
327 
329 {
330  if(pa)
331  {
332  if(pa->serialized_pointlist && ( ! FLAGS_GET_READONLY(pa->flags) ) )
334  lwfree(pa);
335  LWDEBUG(5,"Freeing a PointArray");
336  }
337 }
338 
339 
340 void
342 {
343  int i;
344  int last = pa->npoints-1;
345  int 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 
405 
413 POINTARRAY *
414 ptarray_segmentize2d(const POINTARRAY *ipa, double dist)
415 {
416  double segdist;
417  POINT4D p1, p2;
418  POINT4D pbuf;
419  POINTARRAY *opa;
420  uint32_t ipoff=0; /* input point offset */
421  int hasz = FLAGS_GET_Z(ipa->flags);
422  int hasm = FLAGS_GET_M(ipa->flags);
423 
424  pbuf.x = pbuf.y = pbuf.z = pbuf.m = 0;
425 
426  /* Initial storage */
427  opa = ptarray_construct_empty(hasz, hasm, ipa->npoints);
428 
429  /* Add first point */
430  getPoint4d_p(ipa, ipoff, &p1);
431  ptarray_append_point(opa, &p1, LW_FALSE);
432 
433  ipoff++;
434 
435  while (ipoff<ipa->npoints)
436  {
437  /*
438  * We use these pointers to avoid
439  * "strict-aliasing rules break" warning raised
440  * by gcc (3.3 and up).
441  *
442  * It looks that casting a variable address (also
443  * referred to as "type-punned pointer")
444  * breaks those "strict" rules.
445  *
446  */
447  POINT4D *p1ptr=&p1, *p2ptr=&p2;
448 
449  getPoint4d_p(ipa, ipoff, &p2);
450 
451  segdist = distance2d_pt_pt((POINT2D *)p1ptr, (POINT2D *)p2ptr);
452 
453  if (segdist > dist) /* add an intermediate point */
454  {
455  pbuf.x = p1.x + (p2.x-p1.x)/segdist * dist;
456  pbuf.y = p1.y + (p2.y-p1.y)/segdist * dist;
457  if( hasz )
458  pbuf.z = p1.z + (p2.z-p1.z)/segdist * dist;
459  if( hasm )
460  pbuf.m = p1.m + (p2.m-p1.m)/segdist * dist;
461  ptarray_append_point(opa, &pbuf, LW_FALSE);
462  p1 = pbuf;
463  }
464  else /* copy second point */
465  {
466  ptarray_append_point(opa, &p2, (ipa->npoints==2)?LW_TRUE:LW_FALSE);
467  p1 = p2;
468  ipoff++;
469  }
470 
471  LW_ON_INTERRUPT(ptarray_free(opa); return NULL);
472  }
473 
474  return opa;
475 }
476 
477 char
478 ptarray_same(const POINTARRAY *pa1, const POINTARRAY *pa2)
479 {
480  uint32_t i;
481  size_t ptsize;
482 
483  if ( FLAGS_GET_ZM(pa1->flags) != FLAGS_GET_ZM(pa2->flags) ) return LW_FALSE;
484  LWDEBUG(5,"dimensions are the same");
485 
486  if ( pa1->npoints != pa2->npoints ) return LW_FALSE;
487  LWDEBUG(5,"npoints are the same");
488 
489  ptsize = ptarray_point_size(pa1);
490  LWDEBUGF(5, "ptsize = %d", ptsize);
491 
492  for (i=0; i<pa1->npoints; i++)
493  {
494  if ( memcmp(getPoint_internal(pa1, i), getPoint_internal(pa2, i), ptsize) )
495  return LW_FALSE;
496  LWDEBUGF(5,"point #%d is the same",i);
497  }
498 
499  return LW_TRUE;
500 }
501 
502 POINTARRAY *
503 ptarray_addPoint(const POINTARRAY *pa, uint8_t *p, size_t pdims, uint32_t where)
504 {
505  POINTARRAY *ret;
506  POINT4D pbuf;
507  size_t ptsize = ptarray_point_size(pa);
508 
509  LWDEBUGF(3, "pa %x p %x size %d where %d",
510  pa, p, pdims, where);
511 
512  if ( pdims < 2 || pdims > 4 )
513  {
514  lwerror("ptarray_addPoint: point dimension out of range (%d)",
515  pdims);
516  return NULL;
517  }
518 
519  if ( where > pa->npoints )
520  {
521  lwerror("ptarray_addPoint: offset out of range (%d)",
522  where);
523  return NULL;
524  }
525 
526  LWDEBUG(3, "called with a %dD point");
527 
528  pbuf.x = pbuf.y = pbuf.z = pbuf.m = 0.0;
529  memcpy((uint8_t *)&pbuf, p, pdims*sizeof(double));
530 
531  LWDEBUG(3, "initialized point buffer");
532 
534  FLAGS_GET_M(pa->flags), pa->npoints+1);
535 
536 
537  if ( where )
538  {
539  memcpy(getPoint_internal(ret, 0), getPoint_internal(pa, 0), ptsize*where);
540  }
541 
542  memcpy(getPoint_internal(ret, where), (uint8_t *)&pbuf, ptsize);
543 
544  if ( where+1 != ret->npoints )
545  {
546  memcpy(getPoint_internal(ret, where+1),
547  getPoint_internal(pa, where),
548  ptsize*(pa->npoints-where));
549  }
550 
551  return ret;
552 }
553 
554 POINTARRAY *
556 {
557  POINTARRAY *ret;
558  size_t ptsize = ptarray_point_size(pa);
559 
560  LWDEBUGF(3, "pa %x which %d", pa, which);
561 
562 #if PARANOIA_LEVEL > 0
563  if ( which > pa->npoints-1 )
564  {
565  lwerror("%s [%d] offset (%d) out of range (%d..%d)", __FILE__, __LINE__,
566  which, 0, pa->npoints-1);
567  return NULL;
568  }
569 
570  if ( pa->npoints < 3 )
571  {
572  lwerror("%s [%d] can't remove a point from a 2-vertex POINTARRAY", __FILE__, __LINE__);
573  return NULL;
574  }
575 #endif
576 
578  FLAGS_GET_M(pa->flags), pa->npoints-1);
579 
580  /* copy initial part */
581  if ( which )
582  {
583  memcpy(getPoint_internal(ret, 0), getPoint_internal(pa, 0), ptsize*which);
584  }
585 
586  /* copy final part */
587  if ( which < pa->npoints-1 )
588  {
589  memcpy(getPoint_internal(ret, which), getPoint_internal(pa, which+1),
590  ptsize*(pa->npoints-which-1));
591  }
592 
593  return ret;
594 }
595 
596 POINTARRAY *
598 {
599  POINTARRAY *pa;
600  size_t ptsize = ptarray_point_size(pa1);
601 
602  if (FLAGS_GET_ZM(pa1->flags) != FLAGS_GET_ZM(pa2->flags))
603  lwerror("ptarray_cat: Mixed dimension");
604 
605  pa = ptarray_construct( FLAGS_GET_Z(pa1->flags),
606  FLAGS_GET_M(pa1->flags),
607  pa1->npoints + pa2->npoints);
608 
609  memcpy( getPoint_internal(pa, 0),
610  getPoint_internal(pa1, 0),
611  ptsize*(pa1->npoints));
612 
613  memcpy( getPoint_internal(pa, pa1->npoints),
614  getPoint_internal(pa2, 0),
615  ptsize*(pa2->npoints));
616 
617  ptarray_free(pa1);
618  ptarray_free(pa2);
619 
620  return pa;
621 }
622 
623 
627 POINTARRAY *
629 {
630  POINTARRAY *out = lwalloc(sizeof(POINTARRAY));
631  size_t size;
632 
633  LWDEBUG(3, "ptarray_clone_deep called.");
634 
635  out->flags = in->flags;
636  out->npoints = in->npoints;
637  out->maxpoints = in->npoints;
638 
639  FLAGS_SET_READONLY(out->flags, 0);
640 
641  size = in->npoints * ptarray_point_size(in);
642  out->serialized_pointlist = lwalloc(size);
643  memcpy(out->serialized_pointlist, in->serialized_pointlist, size);
644 
645  return out;
646 }
647 
651 POINTARRAY *
653 {
654  POINTARRAY *out = lwalloc(sizeof(POINTARRAY));
655 
656  LWDEBUG(3, "ptarray_clone_deep called.");
657 
658  out->flags = in->flags;
659  out->npoints = in->npoints;
660  out->maxpoints = in->maxpoints;
661 
662  FLAGS_SET_READONLY(out->flags, 1);
663 
665 
666  return out;
667 }
668 
673 int
675 {
676  if (!in)
677  {
678  lwerror("ptarray_is_closed: called with null point array");
679  return 0;
680  }
681  if (in->npoints <= 1 ) return in->npoints; /* single-point are closed, empty not closed */
682 
683  return 0 == memcmp(getPoint_internal(in, 0), getPoint_internal(in, in->npoints-1), ptarray_point_size(in));
684 }
685 
686 
687 int
689 {
690  if (!in)
691  {
692  lwerror("ptarray_is_closed_2d: called with null point array");
693  return 0;
694  }
695  if (in->npoints <= 1 ) return in->npoints; /* single-point are closed, empty not closed */
696 
697  return 0 == memcmp(getPoint_internal(in, 0), getPoint_internal(in, in->npoints-1), sizeof(POINT2D) );
698 }
699 
700 int
702 {
703  if (!in)
704  {
705  lwerror("ptarray_is_closed_3d: called with null point array");
706  return 0;
707  }
708  if (in->npoints <= 1 ) return in->npoints; /* single-point are closed, empty not closed */
709 
710  return 0 == memcmp(getPoint_internal(in, 0), getPoint_internal(in, in->npoints-1), sizeof(POINT3D) );
711 }
712 
713 int
715 {
716  if ( FLAGS_GET_Z(in->flags) )
717  return ptarray_is_closed_3d(in);
718  else
719  return ptarray_is_closed_2d(in);
720 }
721 
726 int
728 {
729  return ptarray_contains_point_partial(pa, pt, LW_TRUE, NULL);
730 }
731 
732 int
733 ptarray_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
734 {
735  int wn = 0;
736  uint32_t i;
737  double side;
738  const POINT2D *seg1;
739  const POINT2D *seg2;
740  double ymin, ymax;
741 
742  seg1 = getPoint2d_cp(pa, 0);
743  seg2 = getPoint2d_cp(pa, pa->npoints-1);
744  if ( check_closed && ! p2d_same(seg1, seg2) )
745  lwerror("ptarray_contains_point called on unclosed ring");
746 
747  for ( i=1; i < pa->npoints; i++ )
748  {
749  seg2 = getPoint2d_cp(pa, i);
750 
751  /* Zero length segments are ignored. */
752  if ( seg1->x == seg2->x && seg1->y == seg2->y )
753  {
754  seg1 = seg2;
755  continue;
756  }
757 
758  ymin = FP_MIN(seg1->y, seg2->y);
759  ymax = FP_MAX(seg1->y, seg2->y);
760 
761  /* Only test segments in our vertical range */
762  if ( pt->y > ymax || pt->y < ymin )
763  {
764  seg1 = seg2;
765  continue;
766  }
767 
768  side = lw_segment_side(seg1, seg2, pt);
769 
770  /*
771  * A point on the boundary of a ring is not contained.
772  * WAS: if (fabs(side) < 1e-12), see #852
773  */
774  if ( (side == 0) && lw_pt_in_seg(pt, seg1, seg2) )
775  {
776  return LW_BOUNDARY;
777  }
778 
779  /*
780  * If the point is to the left of the line, and it's rising,
781  * then the line is to the right of the point and
782  * circling counter-clockwise, so incremement.
783  */
784  if ( (side < 0) && (seg1->y <= pt->y) && (pt->y < seg2->y) )
785  {
786  wn++;
787  }
788 
789  /*
790  * If the point is to the right of the line, and it's falling,
791  * then the line is to the right of the point and circling
792  * clockwise, so decrement.
793  */
794  else if ( (side > 0) && (seg2->y <= pt->y) && (pt->y < seg1->y) )
795  {
796  wn--;
797  }
798 
799  seg1 = seg2;
800  }
801 
802  /* Sent out the winding number for calls that are building on this as a primitive */
803  if ( winding_number )
804  *winding_number = wn;
805 
806  /* Outside */
807  if (wn == 0)
808  {
809  return LW_OUTSIDE;
810  }
811 
812  /* Inside */
813  return LW_INSIDE;
814 }
815 
825 int
827 {
828  return ptarrayarc_contains_point_partial(pa, pt, LW_TRUE /* Check closed*/, NULL);
829 }
830 
831 int
832 ptarrayarc_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
833 {
834  int wn = 0;
835  uint32_t i;
836  int side;
837  const POINT2D *seg1;
838  const POINT2D *seg2;
839  const POINT2D *seg3;
840  GBOX gbox;
841 
842  /* Check for not an arc ring (always have odd # of points) */
843  if ( (pa->npoints % 2) == 0 )
844  {
845  lwerror("ptarrayarc_contains_point called with even number of points");
846  return LW_OUTSIDE;
847  }
848 
849  /* Check for not an arc ring (always have >= 3 points) */
850  if ( pa->npoints < 3 )
851  {
852  lwerror("ptarrayarc_contains_point called too-short pointarray");
853  return LW_OUTSIDE;
854  }
855 
856  /* Check for unclosed case */
857  seg1 = getPoint2d_cp(pa, 0);
858  seg3 = getPoint2d_cp(pa, pa->npoints-1);
859  if ( check_closed && ! p2d_same(seg1, seg3) )
860  {
861  lwerror("ptarrayarc_contains_point called on unclosed ring");
862  return LW_OUTSIDE;
863  }
864  /* OK, it's closed. Is it just one circle? */
865  else if ( p2d_same(seg1, seg3) && pa->npoints == 3 )
866  {
867  double radius, d;
868  POINT2D c;
869  seg2 = getPoint2d_cp(pa, 1);
870 
871  /* Wait, it's just a point, so it can't contain anything */
872  if ( lw_arc_is_pt(seg1, seg2, seg3) )
873  return LW_OUTSIDE;
874 
875  /* See if the point is within the circle radius */
876  radius = lw_arc_center(seg1, seg2, seg3, &c);
877  d = distance2d_pt_pt(pt, &c);
878  if ( FP_EQUALS(d, radius) )
879  return LW_BOUNDARY; /* Boundary of circle */
880  else if ( d < radius )
881  return LW_INSIDE; /* Inside circle */
882  else
883  return LW_OUTSIDE; /* Outside circle */
884  }
885  else if ( p2d_same(seg1, pt) || p2d_same(seg3, pt) )
886  {
887  return LW_BOUNDARY; /* Boundary case */
888  }
889 
890  /* Start on the ring */
891  seg1 = getPoint2d_cp(pa, 0);
892  for ( i=1; i < pa->npoints; i += 2 )
893  {
894  seg2 = getPoint2d_cp(pa, i);
895  seg3 = getPoint2d_cp(pa, i+1);
896 
897  /* Catch an easy boundary case */
898  if( p2d_same(seg3, pt) )
899  return LW_BOUNDARY;
900 
901  /* Skip arcs that have no size */
902  if ( lw_arc_is_pt(seg1, seg2, seg3) )
903  {
904  seg1 = seg3;
905  continue;
906  }
907 
908  /* Only test segments in our vertical range */
909  lw_arc_calculate_gbox_cartesian_2d(seg1, seg2, seg3, &gbox);
910  if ( pt->y > gbox.ymax || pt->y < gbox.ymin )
911  {
912  seg1 = seg3;
913  continue;
914  }
915 
916  /* Outside of horizontal range, and not between end points we also skip */
917  if ( (pt->x > gbox.xmax || pt->x < gbox.xmin) &&
918  (pt->y > FP_MAX(seg1->y, seg3->y) || pt->y < FP_MIN(seg1->y, seg3->y)) )
919  {
920  seg1 = seg3;
921  continue;
922  }
923 
924  side = lw_arc_side(seg1, seg2, seg3, pt);
925 
926  /* On the boundary */
927  if ( (side == 0) && lw_pt_in_arc(pt, seg1, seg2, seg3) )
928  {
929  return LW_BOUNDARY;
930  }
931 
932  /* Going "up"! Point to left of arc. */
933  if ( side < 0 && (seg1->y <= pt->y) && (pt->y < seg3->y) )
934  {
935  wn++;
936  }
937 
938  /* Going "down"! */
939  if ( side > 0 && (seg2->y <= pt->y) && (pt->y < seg1->y) )
940  {
941  wn--;
942  }
943 
944  /* Inside the arc! */
945  if ( pt->x <= gbox.xmax && pt->x >= gbox.xmin )
946  {
947  POINT2D C;
948  double radius = lw_arc_center(seg1, seg2, seg3, &C);
949  double d = distance2d_pt_pt(pt, &C);
950 
951  /* On the boundary! */
952  if ( d == radius )
953  return LW_BOUNDARY;
954 
955  /* Within the arc! */
956  if ( d < radius )
957  {
958  /* Left side, increment winding number */
959  if ( side < 0 )
960  wn++;
961  /* Right side, decrement winding number */
962  if ( side > 0 )
963  wn--;
964  }
965  }
966 
967  seg1 = seg3;
968  }
969 
970  /* Sent out the winding number for calls that are building on this as a primitive */
971  if ( winding_number )
972  *winding_number = wn;
973 
974  /* Outside */
975  if (wn == 0)
976  {
977  return LW_OUTSIDE;
978  }
979 
980  /* Inside */
981  return LW_INSIDE;
982 }
983 
989 double
991 {
992  const POINT2D *P1;
993  const POINT2D *P2;
994  const POINT2D *P3;
995  double sum = 0.0;
996  double x0, x, y1, y2;
997  uint32_t i;
998 
999  if (! pa || pa->npoints < 3 )
1000  return 0.0;
1001 
1002  P1 = getPoint2d_cp(pa, 0);
1003  P2 = getPoint2d_cp(pa, 1);
1004  x0 = P1->x;
1005  for ( i = 2; i < pa->npoints; i++ )
1006  {
1007  P3 = getPoint2d_cp(pa, i);
1008  x = P2->x - x0;
1009  y1 = P3->y;
1010  y2 = P1->y;
1011  sum += x * (y2-y1);
1012 
1013  /* Move forwards! */
1014  P1 = P2;
1015  P2 = P3;
1016  }
1017  return sum / 2.0;
1018 }
1019 
1020 int
1022 {
1023  double area = 0;
1024  area = ptarray_signed_area(pa);
1025  if ( area > 0 ) return LW_FALSE;
1026  else return LW_TRUE;
1027 }
1028 
1029 POINTARRAY*
1030 ptarray_force_dims(const POINTARRAY *pa, int hasz, int hasm)
1031 {
1032  /* TODO handle zero-length point arrays */
1033  uint32_t i;
1034  int in_hasz = FLAGS_GET_Z(pa->flags);
1035  int in_hasm = FLAGS_GET_M(pa->flags);
1036  POINT4D pt;
1037  POINTARRAY *pa_out = ptarray_construct_empty(hasz, hasm, pa->npoints);
1038 
1039  for( i = 0; i < pa->npoints; i++ )
1040  {
1041  getPoint4d_p(pa, i, &pt);
1042  if( hasz && ! in_hasz )
1043  pt.z = 0.0;
1044  if( hasm && ! in_hasm )
1045  pt.m = 0.0;
1046  ptarray_append_point(pa_out, &pt, LW_TRUE);
1047  }
1048 
1049  return pa_out;
1050 }
1051 
1052 POINTARRAY *
1053 ptarray_substring(POINTARRAY *ipa, double from, double to, double tolerance)
1054 {
1055  POINTARRAY *dpa;
1056  POINT4D pt;
1057  POINT4D p1, p2;
1058  POINT4D *p1ptr=&p1; /* don't break strict-aliasing rule */
1059  POINT4D *p2ptr=&p2;
1060  int nsegs, i;
1061  double length, slength, tlength;
1062  int state = 0; /* 0=before, 1=inside */
1063 
1064  /*
1065  * Create a dynamic pointarray with an initial capacity
1066  * equal to full copy of input points
1067  */
1069 
1070  /* Compute total line length */
1071  length = ptarray_length_2d(ipa);
1072 
1073 
1074  LWDEBUGF(3, "Total length: %g", length);
1075 
1076 
1077  /* Get 'from' and 'to' lengths */
1078  from = length*from;
1079  to = length*to;
1080 
1081 
1082  LWDEBUGF(3, "From/To: %g/%g", from, to);
1083 
1084 
1085  tlength = 0;
1086  getPoint4d_p(ipa, 0, &p1);
1087  nsegs = ipa->npoints - 1;
1088  for ( i = 0; i < nsegs; i++ )
1089  {
1090  double dseg;
1091 
1092  getPoint4d_p(ipa, i+1, &p2);
1093 
1094 
1095  LWDEBUGF(3 ,"Segment %d: (%g,%g,%g,%g)-(%g,%g,%g,%g)",
1096  i, p1.x, p1.y, p1.z, p1.m, p2.x, p2.y, p2.z, p2.m);
1097 
1098 
1099  /* Find the length of this segment */
1100  slength = distance2d_pt_pt((POINT2D *)p1ptr, (POINT2D *)p2ptr);
1101 
1102  /*
1103  * We are before requested start.
1104  */
1105  if ( state == 0 ) /* before */
1106  {
1107 
1108  LWDEBUG(3, " Before start");
1109 
1110  if ( fabs ( from - ( tlength + slength ) ) <= tolerance )
1111  {
1112 
1113  LWDEBUG(3, " Second point is our start");
1114 
1115  /*
1116  * Second point is our start
1117  */
1118  ptarray_append_point(dpa, &p2, LW_FALSE);
1119  state=1; /* we're inside now */
1120  goto END;
1121  }
1122 
1123  else if ( fabs(from - tlength) <= tolerance )
1124  {
1125 
1126  LWDEBUG(3, " First point is our start");
1127 
1128  /*
1129  * First point is our start
1130  */
1131  ptarray_append_point(dpa, &p1, LW_FALSE);
1132 
1133  /*
1134  * We're inside now, but will check
1135  * 'to' point as well
1136  */
1137  state=1;
1138  }
1139 
1140  /*
1141  * Didn't reach the 'from' point,
1142  * nothing to do
1143  */
1144  else if ( from > tlength + slength ) goto END;
1145 
1146  else /* tlength < from < tlength+slength */
1147  {
1148 
1149  LWDEBUG(3, " Seg contains first point");
1150 
1151  /*
1152  * Our start is between first and
1153  * second point
1154  */
1155  dseg = (from - tlength) / slength;
1156 
1157  interpolate_point4d(&p1, &p2, &pt, dseg);
1158 
1159  ptarray_append_point(dpa, &pt, LW_FALSE);
1160 
1161  /*
1162  * We're inside now, but will check
1163  * 'to' point as well
1164  */
1165  state=1;
1166  }
1167  }
1168 
1169  if ( state == 1 ) /* inside */
1170  {
1171 
1172  LWDEBUG(3, " Inside");
1173 
1174  /*
1175  * 'to' point is our second point.
1176  */
1177  if ( fabs(to - ( tlength + slength ) ) <= tolerance )
1178  {
1179 
1180  LWDEBUG(3, " Second point is our end");
1181 
1182  ptarray_append_point(dpa, &p2, LW_FALSE);
1183  break; /* substring complete */
1184  }
1185 
1186  /*
1187  * 'to' point is our first point.
1188  * (should only happen if 'to' is 0)
1189  */
1190  else if ( fabs(to - tlength) <= tolerance )
1191  {
1192 
1193  LWDEBUG(3, " First point is our end");
1194 
1195  ptarray_append_point(dpa, &p1, LW_FALSE);
1196 
1197  break; /* substring complete */
1198  }
1199 
1200  /*
1201  * Didn't reach the 'end' point,
1202  * just copy second point
1203  */
1204  else if ( to > tlength + slength )
1205  {
1206  ptarray_append_point(dpa, &p2, LW_FALSE);
1207  goto END;
1208  }
1209 
1210  /*
1211  * 'to' point falls on this segment
1212  * Interpolate and break.
1213  */
1214  else if ( to < tlength + slength )
1215  {
1216 
1217  LWDEBUG(3, " Seg contains our end");
1218 
1219  dseg = (to - tlength) / slength;
1220  interpolate_point4d(&p1, &p2, &pt, dseg);
1221 
1222  ptarray_append_point(dpa, &pt, LW_FALSE);
1223 
1224  break;
1225  }
1226 
1227  else
1228  {
1229  LWDEBUG(3, "Unhandled case");
1230  }
1231  }
1232 
1233 
1234 END:
1235 
1236  tlength += slength;
1237  memcpy(&p1, &p2, sizeof(POINT4D));
1238  }
1239 
1240  LWDEBUGF(3, "Out of loop, ptarray has %d points", dpa->npoints);
1241 
1242  return dpa;
1243 }
1244 
1245 /*
1246  * Write into the *ret argument coordinates of the closes point on
1247  * the given segment to the reference input point.
1248  */
1249 void
1250 closest_point_on_segment(const POINT4D *p, const POINT4D *A, const POINT4D *B, POINT4D *ret)
1251 {
1252  double r;
1253 
1254  if ( FP_EQUALS(A->x, B->x) && FP_EQUALS(A->y, B->y) )
1255  {
1256  *ret = *A;
1257  return;
1258  }
1259 
1260  /*
1261  * We use comp.graphics.algorithms Frequently Asked Questions method
1262  *
1263  * (1) AC dot AB
1264  * r = ----------
1265  * ||AB||^2
1266  * r has the following meaning:
1267  * r=0 P = A
1268  * r=1 P = B
1269  * r<0 P is on the backward extension of AB
1270  * r>1 P is on the forward extension of AB
1271  * 0<r<1 P is interior to AB
1272  *
1273  */
1274  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) );
1275 
1276  if (r<0)
1277  {
1278  *ret = *A;
1279  return;
1280  }
1281  if (r>1)
1282  {
1283  *ret = *B;
1284  return;
1285  }
1286 
1287  ret->x = A->x + ( (B->x - A->x) * r );
1288  ret->y = A->y + ( (B->y - A->y) * r );
1289  ret->z = A->z + ( (B->z - A->z) * r );
1290  ret->m = A->m + ( (B->m - A->m) * r );
1291 }
1292 
1293 /*
1294  * Given a point, returns the location of closest point on pointarray
1295  * and, optionally, it's actual distance from the point array.
1296  */
1297 double
1298 ptarray_locate_point(const POINTARRAY *pa, const POINT4D *p4d, double *mindistout, POINT4D *proj4d)
1299 {
1300  double mindist=DBL_MAX;
1301  double tlen, plen;
1302  uint32_t t, seg=0;
1303  POINT4D start4d, end4d, projtmp;
1304  POINT2D proj, p;
1305  const POINT2D *start = NULL, *end = NULL;
1306 
1307  /* Initialize our 2D copy of the input parameter */
1308  p.x = p4d->x;
1309  p.y = p4d->y;
1310 
1311  if ( ! proj4d ) proj4d = &projtmp;
1312 
1313  /* Check for special cases (length 0 and 1) */
1314  if ( pa->npoints <= 1 )
1315  {
1316  if ( pa->npoints == 1 )
1317  {
1318  getPoint4d_p(pa, 0, proj4d);
1319  if ( mindistout )
1320  *mindistout = distance2d_pt_pt(&p, getPoint2d_cp(pa, 0));
1321  }
1322  return 0.0;
1323  }
1324 
1325  start = getPoint2d_cp(pa, 0);
1326  /* Loop through pointarray looking for nearest segment */
1327  for (t=1; t<pa->npoints; t++)
1328  {
1329  double dist;
1330  end = getPoint2d_cp(pa, t);
1331  dist = distance2d_pt_seg(&p, start, end);
1332 
1333  if ( dist < mindist )
1334  {
1335  mindist = dist;
1336  seg=t-1;
1337  if ( mindist == 0 )
1338  {
1339  LWDEBUG(3, "Breaking on mindist=0");
1340  break;
1341  }
1342  }
1343 
1344  start = end;
1345  }
1346 
1347  if ( mindistout ) *mindistout = mindist;
1348 
1349  LWDEBUGF(3, "Closest segment: %d", seg);
1350  LWDEBUGF(3, "mindist: %g", mindist);
1351 
1352  /*
1353  * We need to project the
1354  * point on the closest segment.
1355  */
1356  getPoint4d_p(pa, seg, &start4d);
1357  getPoint4d_p(pa, seg+1, &end4d);
1358  closest_point_on_segment(p4d, &start4d, &end4d, proj4d);
1359 
1360  /* Copy 4D values into 2D holder */
1361  proj.x = proj4d->x;
1362  proj.y = proj4d->y;
1363 
1364  LWDEBUGF(3, "Closest segment:%d, npoints:%d", seg, pa->npoints);
1365 
1366  /* For robustness, force 1 when closest point == endpoint */
1367  if ( (seg >= (pa->npoints-2)) && p2d_same(&proj, end) )
1368  {
1369  return 1.0;
1370  }
1371 
1372  LWDEBUGF(3, "Closest point on segment: %g,%g", proj.x, proj.y);
1373 
1374  tlen = ptarray_length_2d(pa);
1375 
1376  LWDEBUGF(3, "tlen %g", tlen);
1377 
1378  /* Location of any point on a zero-length line is 0 */
1379  /* See http://trac.osgeo.org/postgis/ticket/1772#comment:2 */
1380  if ( tlen == 0 ) return 0;
1381 
1382  plen=0;
1383  start = getPoint2d_cp(pa, 0);
1384  for (t=0; t<seg; t++, start=end)
1385  {
1386  end = getPoint2d_cp(pa, t+1);
1387  plen += distance2d_pt_pt(start, end);
1388 
1389  LWDEBUGF(4, "Segment %d made plen %g", t, plen);
1390  }
1391 
1392  plen+=distance2d_pt_pt(&proj, start);
1393 
1394  LWDEBUGF(3, "plen %g, tlen %g", plen, tlen);
1395 
1396  return plen/tlen;
1397 }
1398 
1408 void
1410 {
1411  uint32_t i;
1412  double x;
1413 
1414  for (i=0; i<pa->npoints; i++)
1415  {
1416  memcpy(&x, getPoint_internal(pa, i), sizeof(double));
1417  if ( x < 0 ) x+= 360;
1418  else if ( x > 180 ) x -= 360;
1419  memcpy(getPoint_internal(pa, i), &x, sizeof(double));
1420  }
1421 }
1422 
1423 
1424 /*
1425  * Returns a POINTARRAY with consecutive equal points
1426  * removed. Equality test on all dimensions of input.
1427  *
1428  * Always returns a newly allocated object.
1429  */
1430 static POINTARRAY *
1431 ptarray_remove_repeated_points_minpoints(const POINTARRAY *in, double tolerance, int minpoints)
1432 {
1433  POINTARRAY *out = ptarray_clone_deep(in);
1434  ptarray_remove_repeated_points_in_place(out, tolerance, minpoints);
1435  return out;
1436 }
1437 
1438 POINTARRAY *
1439 ptarray_remove_repeated_points(const POINTARRAY *in, double tolerance)
1440 {
1441  return ptarray_remove_repeated_points_minpoints(in, tolerance, 2);
1442 }
1443 
1444 
1445 void
1447 {
1448  uint32_t i;
1449  double tolsq = tolerance * tolerance;
1450  const POINT2D *last = NULL;
1451  const POINT2D *pt;
1452  uint32_t n_points = pa->npoints;
1453  uint32_t n_points_out = 1;
1454  size_t pt_size = ptarray_point_size(pa);
1455 
1456  double dsq = FLT_MAX;
1457 
1458  /* No-op on short inputs */
1459  if ( n_points <= min_points ) return;
1460 
1461  last = getPoint2d_cp(pa, 0);
1462  for (i = 1; i < n_points; i++)
1463  {
1464  int last_point = (i == n_points-1);
1465 
1466  /* Look straight into the abyss */
1467  pt = getPoint2d_cp(pa, i);
1468 
1469  /* Don't drop points if we are running short of points */
1470  if (n_points + n_points_out > min_points + i)
1471  {
1472  if (tolerance > 0.0)
1473  {
1474  /* Only drop points that are within our tolerance */
1475  dsq = distance2d_sqr_pt_pt(last, pt);
1476  /* Allow any point but the last one to be dropped */
1477  if (!last_point && dsq <= tolsq)
1478  {
1479  continue;
1480  }
1481  }
1482  else
1483  {
1484  /* At tolerance zero, only skip exact dupes */
1485  if (memcmp((char*)pt, (char*)last, pt_size) == 0)
1486  continue;
1487  }
1488 
1489  /* Got to last point, and it's not very different from */
1490  /* the point that preceded it. We want to keep the last */
1491  /* point, not the second-to-last one, so we pull our write */
1492  /* index back one value */
1493  if (last_point && n_points_out > 1 && tolerance > 0.0 && dsq <= tolsq)
1494  {
1495  n_points_out--;
1496  }
1497  }
1498 
1499  /* Compact all remaining values to front of array */
1500  ptarray_copy_point(pa, i, n_points_out++);
1501  last = pt;
1502  }
1503  /* Adjust array length */
1504  pa->npoints = n_points_out;
1505  return;
1506 }
1507 
1508 
1509 /************************************************************************/
1510 
1511 static void
1512 ptarray_dp_findsplit_in_place(const POINTARRAY *pts, int p1, int p2, int *split, double *dist)
1513 {
1514  int k;
1515  const POINT2D *pk, *pa, *pb;
1516  double tmp, d;
1517 
1518  LWDEBUG(4, "function called");
1519 
1520  *split = p1;
1521  d = -1;
1522 
1523  if (p1 + 1 < p2)
1524  {
1525 
1526  pa = getPoint2d_cp(pts, p1);
1527  pb = getPoint2d_cp(pts, p2);
1528 
1529  LWDEBUGF(4, "P%d(%f,%f) to P%d(%f,%f)",
1530  p1, pa->x, pa->y, p2, pb->x, pb->y);
1531 
1532  for (k=p1+1; k<p2; k++)
1533  {
1534  pk = getPoint2d_cp(pts, k);
1535 
1536  LWDEBUGF(4, "P%d(%f,%f)", k, pk->x, pk->y);
1537 
1538  /* distance computation */
1539  tmp = distance2d_sqr_pt_seg(pk, pa, pb);
1540 
1541  if (tmp > d)
1542  {
1543  d = tmp; /* record the maximum */
1544  *split = k;
1545 
1546  LWDEBUGF(4, "P%d is farthest (%g)", k, d);
1547  }
1548  }
1549  *dist = d;
1550  }
1551  else
1552  {
1553  LWDEBUG(3, "segment too short, no split/no dist");
1554  *dist = -1;
1555  }
1556 }
1557 
1558 static int
1559 int_cmp(const void *a, const void *b)
1560 {
1561  /* casting pointer types */
1562  const int *ia = (const int *)a;
1563  const int *ib = (const int *)b;
1564  /* returns negative if b > a and positive if a > b */
1565  return *ia - *ib;
1566 }
1567 
1568 void
1569 ptarray_simplify_in_place(POINTARRAY *pa, double epsilon, uint32_t minpts)
1570 {
1571  static size_t stack_size = 256;
1572  int *stack, *outlist; /* recursion stack */
1573  int stack_static[stack_size];
1574  int outlist_static[stack_size];
1575  int sp = -1; /* recursion stack pointer */
1576  int p1, split;
1577  uint32_t outn = 0;
1578  int pai = 0;
1579  uint32_t i;
1580  double dist;
1581  double eps_sqr = epsilon * epsilon;
1582 
1583  /* Do not try to simplify really short things */
1584  if (pa->npoints < 3) return;
1585 
1586  /* Only heap allocate book-keeping arrays if necessary */
1587  if (pa->npoints > stack_size)
1588  {
1589  stack = lwalloc(sizeof(int) * pa->npoints);
1590  outlist = lwalloc(sizeof(int) * pa->npoints);
1591  }
1592  else
1593  {
1594  stack = stack_static;
1595  outlist = outlist_static;
1596  }
1597 
1598  p1 = 0;
1599  stack[++sp] = pa->npoints-1;
1600 
1601  /* Add first point to output list */
1602  outlist[outn++] = 0;
1603  do
1604  {
1605  ptarray_dp_findsplit_in_place(pa, p1, stack[sp], &split, &dist);
1606 
1607  if ((dist > eps_sqr) || ((outn + sp+1 < minpts) && (dist >= 0)))
1608  {
1609  stack[++sp] = split;
1610  }
1611  else
1612  {
1613  outlist[outn++] = stack[sp];
1614  p1 = stack[sp--];
1615  }
1616  }
1617  while (!(sp<0));
1618 
1619  /* Put list of retained points into order */
1620  qsort(outlist, outn, sizeof(int), int_cmp);
1621  /* Copy retained points to front of array */
1622  for (i = 0; i < outn; i++)
1623  {
1624  int j = outlist[i];
1625  /* Indexes the same, means no copy required */
1626  if (j == pai)
1627  {
1628  pai++;
1629  continue;
1630  }
1631  /* Indexes different, copy value down */
1632  ptarray_copy_point(pa, j, pai++);
1633  }
1634 
1635  /* Adjust point count on array */
1636  pa->npoints = outn;
1637 
1638  /* Only free if arrays are on heap */
1639  if (stack != stack_static)
1640  lwfree(stack);
1641  if (outlist != outlist_static)
1642  lwfree(outlist);
1643 
1644  return;
1645 }
1646 
1647 /************************************************************************/
1648 
1654 double
1656 {
1657  double dist = 0.0;
1658  uint32_t i;
1659  const POINT2D *a1;
1660  const POINT2D *a2;
1661  const POINT2D *a3;
1662 
1663  if ( pts->npoints % 2 != 1 )
1664  lwerror("arc point array with even number of points");
1665 
1666  a1 = getPoint2d_cp(pts, 0);
1667 
1668  for ( i=2; i < pts->npoints; i += 2 )
1669  {
1670  a2 = getPoint2d_cp(pts, i-1);
1671  a3 = getPoint2d_cp(pts, i);
1672  dist += lw_arc_length(a1, a2, a3);
1673  a1 = a3;
1674  }
1675  return dist;
1676 }
1677 
1681 double
1683 {
1684  double dist = 0.0;
1685  uint32_t i;
1686  const POINT2D *frm;
1687  const POINT2D *to;
1688 
1689  if ( pts->npoints < 2 ) return 0.0;
1690 
1691  frm = getPoint2d_cp(pts, 0);
1692 
1693  for ( i=1; i < pts->npoints; i++ )
1694  {
1695  to = getPoint2d_cp(pts, i);
1696 
1697  dist += sqrt( ((frm->x - to->x)*(frm->x - to->x)) +
1698  ((frm->y - to->y)*(frm->y - to->y)) );
1699 
1700  frm = to;
1701  }
1702  return dist;
1703 }
1704 
1709 double
1711 {
1712  double dist = 0.0;
1713  uint32_t i;
1714  POINT3DZ frm;
1715  POINT3DZ to;
1716 
1717  if ( pts->npoints < 2 ) return 0.0;
1718 
1719  /* compute 2d length if 3d is not available */
1720  if ( ! FLAGS_GET_Z(pts->flags) ) return ptarray_length_2d(pts);
1721 
1722  getPoint3dz_p(pts, 0, &frm);
1723  for ( i=1; i < pts->npoints; i++ )
1724  {
1725  getPoint3dz_p(pts, i, &to);
1726  dist += sqrt( ((frm.x - to.x)*(frm.x - to.x)) +
1727  ((frm.y - to.y)*(frm.y - to.y)) +
1728  ((frm.z - to.z)*(frm.z - to.z)) );
1729  frm = to;
1730  }
1731  return dist;
1732 }
1733 
1734 
1735 /*
1736  * Get a pointer to nth point of a POINTARRAY.
1737  *
1738  * Casting to returned pointer to POINT2D* should be safe,
1739  * as gserialized format always keeps the POINTARRAY pointer
1740  * aligned to double boundary.
1741  */
1742 uint8_t *
1744 {
1745  size_t size;
1746  uint8_t *ptr;
1747 
1748 #if PARANOIA_LEVEL > 0
1749  if ( pa == NULL )
1750  {
1751  lwerror("%s [%d] got NULL pointarray", __FILE__, __LINE__);
1752  return NULL;
1753  }
1754 
1755  LWDEBUGF(5, "(n=%d, pa.npoints=%d, pa.maxpoints=%d)",n,pa->npoints,pa->maxpoints);
1756 
1757  if ( ( n > pa->npoints ) ||
1758  ( n >= pa->maxpoints ) )
1759  {
1760  lwerror("%s [%d] called outside of ptarray range (n=%d, pa.npoints=%d, pa.maxpoints=%d)", __FILE__, __LINE__, n, pa->npoints, pa->maxpoints);
1761  return NULL; /*error */
1762  }
1763 #endif
1764 
1765  size = ptarray_point_size(pa);
1766 
1767  ptr = pa->serialized_pointlist + size * n;
1768  if ( FLAGS_NDIMS(pa->flags) == 2)
1769  {
1770  LWDEBUGF(5, "point = %g %g", *((double*)(ptr)), *((double*)(ptr+8)));
1771  }
1772  else if ( FLAGS_NDIMS(pa->flags) == 3)
1773  {
1774  LWDEBUGF(5, "point = %g %g %g", *((double*)(ptr)), *((double*)(ptr+8)), *((double*)(ptr+16)));
1775  }
1776  else if ( FLAGS_NDIMS(pa->flags) == 4)
1777  {
1778  LWDEBUGF(5, "point = %g %g %g %g", *((double*)(ptr)), *((double*)(ptr+8)), *((double*)(ptr+16)), *((double*)(ptr+24)));
1779  }
1780 
1781  return ptr;
1782 }
1783 
1784 
1788 void
1790 {
1791  uint32_t i;
1792  double x,y,z;
1793  POINT4D p4d;
1794 
1795  LWDEBUG(2, "lwgeom_affine_ptarray start");
1796 
1797  if ( FLAGS_GET_Z(pa->flags) )
1798  {
1799  LWDEBUG(3, " has z");
1800 
1801  for (i=0; i<pa->npoints; i++)
1802  {
1803  getPoint4d_p(pa, i, &p4d);
1804  x = p4d.x;
1805  y = p4d.y;
1806  z = p4d.z;
1807  p4d.x = a->afac * x + a->bfac * y + a->cfac * z + a->xoff;
1808  p4d.y = a->dfac * x + a->efac * y + a->ffac * z + a->yoff;
1809  p4d.z = a->gfac * x + a->hfac * y + a->ifac * z + a->zoff;
1810  ptarray_set_point4d(pa, i, &p4d);
1811 
1812  LWDEBUGF(3, " POINT %g %g %g => %g %g %g", x, y, z, p4d.x, p4d.y, p4d.z);
1813  }
1814  }
1815  else
1816  {
1817  LWDEBUG(3, " doesn't have z");
1818 
1819  for (i=0; i<pa->npoints; i++)
1820  {
1821  getPoint4d_p(pa, i, &p4d);
1822  x = p4d.x;
1823  y = p4d.y;
1824  p4d.x = a->afac * x + a->bfac * y + a->xoff;
1825  p4d.y = a->dfac * x + a->efac * y + a->yoff;
1826  ptarray_set_point4d(pa, i, &p4d);
1827 
1828  LWDEBUGF(3, " POINT %g %g => %g %g", x, y, p4d.x, p4d.y);
1829  }
1830  }
1831 
1832  LWDEBUG(3, "lwgeom_affine_ptarray end");
1833 
1834 }
1835 
1839 void
1841 {
1842  uint32_t i;
1843  POINT4D p4d;
1844  LWDEBUG(3, "ptarray_scale start");
1845  for (i=0; i<pa->npoints; i++)
1846  {
1847  getPoint4d_p(pa, i, &p4d);
1848  p4d.x *= fact->x;
1849  p4d.y *= fact->y;
1850  p4d.z *= fact->z;
1851  p4d.m *= fact->m;
1852  ptarray_set_point4d(pa, i, &p4d);
1853  }
1854  LWDEBUG(3, "ptarray_scale end");
1855 }
1856 
1857 int
1859 {
1860  return getPoint4d_p(pa, 0, pt);
1861 }
1862 
1863 
1864 /*
1865  * Stick an array of points to the given gridspec.
1866  * Return "gridded" points in *outpts and their number in *outptsn.
1867  *
1868  * Two consecutive points falling on the same grid cell are collapsed
1869  * into one single point.
1870  *
1871  */
1872 void
1874 {
1875  uint32_t i, j = 0;
1876  POINT4D *p, *p_out = NULL;
1877  int ndims = FLAGS_NDIMS(pa->flags);
1878  int has_z = FLAGS_GET_Z(pa->flags);
1879  int has_m = FLAGS_GET_M(pa->flags);
1880 
1881  LWDEBUGF(2, "%s called on %p", __func__, pa);
1882 
1883  for (i = 0; i < pa->npoints; i++)
1884  {
1885  /* Look straight into the abyss */
1886  p = (POINT4D*)(getPoint_internal(pa, i));
1887 
1888  if (grid->xsize > 0)
1889  {
1890  p->x = rint((p->x - grid->ipx)/grid->xsize) * grid->xsize + grid->ipx;
1891  }
1892 
1893  if (grid->ysize > 0)
1894  {
1895  p->y = rint((p->y - grid->ipy)/grid->ysize) * grid->ysize + grid->ipy;
1896  }
1897 
1898  /* Read and round this point */
1899  /* Z is always in third position */
1900  if (has_z)
1901  {
1902  if (grid->zsize > 0)
1903  p->z = rint((p->z - grid->ipz)/grid->zsize) * grid->zsize + grid->ipz;
1904  }
1905  /* M might be in 3rd or 4th position */
1906  if (has_m)
1907  {
1908  /* In POINT M, M is in 3rd position */
1909  if (grid->msize > 0 && !has_z)
1910  p->z = rint((p->z - grid->ipm)/grid->msize) * grid->msize + grid->ipm;
1911  /* In POINT ZM, M is in 4th position */
1912  if (grid->msize > 0 && has_z)
1913  p->m = rint((p->m - grid->ipm)/grid->msize) * grid->msize + grid->ipm;
1914  }
1915 
1916  /* Skip duplicates */
1917  if ( p_out && FP_EQUALS(p_out->x, p->x) && FP_EQUALS(p_out->y, p->y)
1918  && (ndims > 2 ? FP_EQUALS(p_out->z, p->z) : 1)
1919  && (ndims > 3 ? FP_EQUALS(p_out->m, p->m) : 1) )
1920  {
1921  continue;
1922  }
1923 
1924  /* Write rounded values into the next available point */
1925  p_out = (POINT4D*)(getPoint_internal(pa, j++));
1926  p_out->x = p->x;
1927  p_out->y = p->y;
1928  if (ndims > 2)
1929  p_out->z = p->z;
1930  if (ndims > 3)
1931  p_out->m = p->m;
1932  }
1933 
1934  /* Update output ptarray length */
1935  pa->npoints = j;
1936  return;
1937 }
1938 
1939 
1940 int
1941 ptarray_npoints_in_rect(const POINTARRAY *pa, const GBOX *gbox)
1942 {
1943  const POINT2D *pt;
1944  int n = 0;
1945  uint32_t i;
1946  for ( i = 0; i < pa->npoints; i++ )
1947  {
1948  pt = getPoint2d_cp(pa, i);
1949  if ( gbox_contains_point2d(gbox, pt) )
1950  n++;
1951  }
1952  return n;
1953 }
1954 
1955 
double x
Definition: liblwgeom.h:351
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:227
double z
Definition: liblwgeom.h:333
int ptarrayarc_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
Definition: ptarray.c:832
POINTARRAY * ptarray_substring(POINTARRAY *ipa, double from, double to, double tolerance)
start location (distance from start / total distance) end location (distance from start / total dist...
Definition: ptarray.c:1053
double y
Definition: liblwgeom.h:333
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:105
double m
Definition: liblwgeom.h:351
uint8_t * serialized_pointlist
Definition: liblwgeom.h:365
double x
Definition: liblwgeom.h:333
char * r
Definition: cu_in_wkt.c:24
POINTARRAY * ptarray_merge(POINTARRAY *pa1, POINTARRAY *pa2)
Merge two given POINTARRAY and returns a pointer on the new aggregate one.
Definition: ptarray.c:597
void lwfree(void *mem)
Definition: lwutil.c:244
#define FLAGS_GET_READONLY(flags)
Definition: liblwgeom.h:143
void ptarray_grid_in_place(POINTARRAY *pa, const gridspec *grid)
Definition: ptarray.c:1873
size_t ptarray_point_size(const POINTARRAY *pa)
Definition: ptarray.c:54
int lw_arc_calculate_gbox_cartesian_2d(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, GBOX *gbox)
Definition: g_box.c:459
double ptarray_length(const POINTARRAY *pts)
Find the 3d/2d length of the given POINTARRAY (depending on its dimensionality)
Definition: ptarray.c:1710
POINTARRAY * ptarray_clone_deep(const POINTARRAY *in)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:628
Datum area(PG_FUNCTION_ARGS)
double ptarray_locate_point(const POINTARRAY *pa, const POINT4D *p4d, double *mindistout, POINT4D *proj4d)
Definition: ptarray.c:1298
double xmax
Definition: liblwgeom.h:292
void ptarray_copy_point(POINTARRAY *pa, uint32_t from, uint32_t to)
Definition: lwgeom_api.c:450
POINTARRAY * ptarray_flip_coordinates(POINTARRAY *pa)
Reverse X and Y axis on a given POINTARRAY.
Definition: ptarray.c:368
void ptarray_reverse_in_place(POINTARRAY *pa)
Definition: ptarray.c:341
#define LW_SUCCESS
Definition: liblwgeom.h:79
static POINTARRAY * ptarray_remove_repeated_points_minpoints(const POINTARRAY *in, double tolerance, int minpoints)
Definition: ptarray.c:1431
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:425
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:293
#define FLAGS_GET_ZM(flags)
Definition: liblwgeom.h:152
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
void ptarray_scale(POINTARRAY *pa, const POINT4D *fact)
Scale a pointarray.
Definition: ptarray.c:1840
uint32_t maxpoints
Definition: liblwgeom.h:371
int ptarray_is_closed_2d(const POINTARRAY *in)
Definition: ptarray.c:688
#define LW_ON_INTERRUPT(x)
int ptarray_remove_point(POINTARRAY *pa, uint32_t where)
Remove a point from an existing POINTARRAY.
Definition: ptarray.c:261
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, then a duplicate point will not be added.
Definition: ptarray.c:156
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_isccw(const POINTARRAY *pa)
Definition: ptarray.c:1021
POINTARRAY * ptarray_removePoint(POINTARRAY *pa, uint32_t which)
Remove a point from a pointarray.
Definition: ptarray.c:555
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
The old function nessecary for ptarray_segmentize2d in ptarray.c.
Definition: measures.c:2312
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:704
double zoff
Definition: liblwgeom.h:269
#define FP_MIN(A, B)
int ptarray_npoints_in_rect(const POINTARRAY *pa, const GBOX *gbox)
Definition: ptarray.c:1941
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:307
uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
Definition: ptarray.c:1743
double distance2d_pt_seg(const POINT2D *p, const POINT2D *A, const POINT2D *B)
The old function nessecary for ptarray_segmentize2d in ptarray.c.
Definition: measures.c:2337
void ptarray_longitude_shift(POINTARRAY *pa)
Longitude shift for a pointarray.
Definition: ptarray.c:1409
double ifac
Definition: liblwgeom.h:269
POINTARRAY * ptarray_clone(const POINTARRAY *in)
Clone a POINTARRAY object.
Definition: ptarray.c:652
double ffac
Definition: liblwgeom.h:269
double xoff
Definition: liblwgeom.h:269
double afac
Definition: liblwgeom.h:269
int ptarray_startpoint(const POINTARRAY *pa, POINT4D *pt)
Definition: ptarray.c:1858
#define LW_FAILURE
Definition: liblwgeom.h:78
unsigned int uint32_t
Definition: uthash.h:78
double x
Definition: liblwgeom.h:327
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:49
static int int_cmp(const void *a, const void *b)
Definition: ptarray.c:1559
double ymin
Definition: liblwgeom.h:293
double lw_arc_length(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3)
Returns the length of a circular arc segment.
Definition: lwalgorithm.c:118
int ptarray_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
Definition: ptarray.c:733
double xmin
Definition: liblwgeom.h:291
#define LW_FALSE
Definition: liblwgeom.h:76
POINTARRAY * ptarray_addPoint(const POINTARRAY *pa, uint8_t *p, size_t pdims, uint32_t where)
Add a point in a pointarray.
Definition: ptarray.c:503
uint8_t flags
Definition: liblwgeom.h:368
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
void closest_point_on_segment(const POINT4D *p, const POINT4D *A, const POINT4D *B, POINT4D *ret)
Definition: ptarray.c:1250
void ptarray_simplify_in_place(POINTARRAY *pa, double epsilon, uint32_t minpts)
Definition: ptarray.c:1569
int ptarray_is_closed_z(const POINTARRAY *in)
Definition: ptarray.c:714
int ptarray_has_z(const POINTARRAY *pa)
Definition: ptarray.c:36
#define LW_INSIDE
Constants for point-in-polygon return values.
int ptarray_has_m(const POINTARRAY *pa)
Definition: ptarray.c:43
static void ptarray_dp_findsplit_in_place(const POINTARRAY *pts, int p1, int p2, int *split, double *dist)
Definition: ptarray.c:1512
double cfac
Definition: liblwgeom.h:269
double ymax
Definition: liblwgeom.h:294
enum LWORD_T LWORD
Ordinate names.
void ptarray_remove_repeated_points_in_place(POINTARRAY *pa, double tolerance, uint32_t min_points)
Definition: ptarray.c:1446
double y
Definition: liblwgeom.h:327
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:62
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
int ptarray_is_closed_3d(const POINTARRAY *in)
Definition: ptarray.c:701
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1682
double dfac
Definition: liblwgeom.h:269
double z
Definition: liblwgeom.h:351
tuple x
Definition: pixval.py:53
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:145
double ptarray_signed_area(const POINTARRAY *pa)
Returns the area in cartesian units.
Definition: ptarray.c:990
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:113
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:1655
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:338
int getPoint3dz_p(const POINTARRAY *pa, uint32_t n, POINT3DZ *point)
Definition: lwgeom_api.c:205
int lw_arc_side(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, const POINT2D *Q)
Definition: lwalgorithm.c:178
#define LW_BOUNDARY
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:85
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:237
double efac
Definition: liblwgeom.h:269
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
int ptarray_insert_point(POINTARRAY *pa, const POINT4D *p, uint32_t where)
Insert a point into an existing POINTARRAY.
Definition: ptarray.c:96
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:95
#define FP_EQUALS(A, B)
double yoff
Definition: liblwgeom.h:269
double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2322
#define LW_OUTSIDE
void * lwalloc(size_t size)
Definition: lwutil.c:229
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
Definition: lwalgorithm.c:64
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:328
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:414
double y
Definition: liblwgeom.h:351
int ptarrayarc_contains_point(const POINTARRAY *pa, const POINT2D *pt)
For POINTARRAYs representing CIRCULARSTRINGS.
Definition: ptarray.c:826
double distance2d_sqr_pt_seg(const POINT2D *p, const POINT2D *A, const POINT2D *B)
Definition: measures.c:2386
char ptarray_same(const POINTARRAY *pa1, const POINTARRAY *pa2)
Definition: ptarray.c:478
double gfac
Definition: liblwgeom.h:269
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:151
unsigned char uint8_t
Definition: uthash.h:79
void ptarray_affine(POINTARRAY *pa, const AFFINE *a)
Affine transform a pointarray.
Definition: ptarray.c:1789
POINTARRAY * ptarray_force_dims(const POINTARRAY *pa, int hasz, int hasm)
Definition: ptarray.c:1030
int ptarray_is_closed(const POINTARRAY *in)
Check for ring closure using whatever dimensionality is declared on the pointarray.
Definition: ptarray.c:674
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
tuple y
Definition: pixval.py:54
int gbox_contains_point2d(const GBOX *g, const POINT2D *p)
Definition: g_box.c:357
double hfac
Definition: liblwgeom.h:269
double bfac
Definition: liblwgeom.h:269
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:187
#define FLAGS_SET_READONLY(flags, value)
Definition: liblwgeom.h:149
POINTARRAY * ptarray_remove_repeated_points(const POINTARRAY *in, double tolerance)
Definition: ptarray.c:1439
#define FP_MAX(A, B)
int ptarray_contains_point(const POINTARRAY *pa, const POINT2D *pt)
Return 1 if the point is inside the POINTARRAY, -1 if it is outside, and 0 if it is on the boundary...
Definition: ptarray.c:727
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:364
Snap to grid.
uint32_t npoints
Definition: liblwgeom.h:370