PostGIS  2.5.0dev-r@@SVN_REVISION@@
lwstroke.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) 2001-2006 Refractions Research Inc.
22  * Copyright (C) 2017 Sandro Santilli <strk@kbt.io>
23  *
24  **********************************************************************/
25 
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 
32 #include "../postgis_config.h"
33 
34 /*#define POSTGIS_DEBUG_LEVEL 3*/
35 
36 #include "lwgeom_log.h"
37 
38 #include "liblwgeom_internal.h"
39 
40 
41 LWGEOM* pta_unstroke(const POINTARRAY *points, int srid);
42 LWGEOM* lwline_unstroke(const LWLINE *line);
43 LWGEOM* lwpolygon_unstroke(const LWPOLY *poly);
44 LWGEOM* lwmline_unstroke(const LWMLINE *mline);
45 LWGEOM* lwmpolygon_unstroke(const LWMPOLY *mpoly);
47 LWGEOM* lwgeom_unstroke(const LWGEOM *geom);
48 
49 
50 /*
51  * Determines (recursively in the case of collections) whether the geometry
52  * contains at least on arc geometry or segment.
53  */
54 int
55 lwgeom_has_arc(const LWGEOM *geom)
56 {
57  LWCOLLECTION *col;
58  uint32_t i;
59 
60  LWDEBUG(2, "lwgeom_has_arc called.");
61 
62  switch (geom->type)
63  {
64  case POINTTYPE:
65  case LINETYPE:
66  case POLYGONTYPE:
67  case TRIANGLETYPE:
68  case MULTIPOINTTYPE:
69  case MULTILINETYPE:
70  case MULTIPOLYGONTYPE:
72  case TINTYPE:
73  return LW_FALSE;
74  case CIRCSTRINGTYPE:
75  case CURVEPOLYTYPE:
76  case COMPOUNDTYPE:
77  return LW_TRUE;
78  /* It's a collection that MAY contain an arc */
79  default:
80  col = (LWCOLLECTION *)geom;
81  for (i=0; i<col->ngeoms; i++)
82  {
83  if (lwgeom_has_arc(col->geoms[i]) == LW_TRUE)
84  return LW_TRUE;
85  }
86  return LW_FALSE;
87  }
88 }
89 
90 
91 
92 /*******************************************************************************
93  * Begin curve segmentize functions
94  ******************************************************************************/
95 
96 static double interpolate_arc(double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3)
97 {
98  LWDEBUGF(4,"angle %.05g a1 %.05g a2 %.05g a3 %.05g zm1 %.05g zm2 %.05g zm3 %.05g",angle,a1,a2,a3,zm1,zm2,zm3);
99  /* Counter-clockwise sweep */
100  if ( a1 < a2 )
101  {
102  if ( angle <= a2 )
103  return zm1 + (zm2-zm1) * (angle-a1) / (a2-a1);
104  else
105  return zm2 + (zm3-zm2) * (angle-a2) / (a3-a2);
106  }
107  /* Clockwise sweep */
108  else
109  {
110  if ( angle >= a2 )
111  return zm1 + (zm2-zm1) * (a1-angle) / (a1-a2);
112  else
113  return zm2 + (zm3-zm2) * (a2-angle) / (a2-a3);
114  }
115 }
116 
134 static int
136  const POINT4D *p1, const POINT4D *p2, const POINT4D *p3,
137  double tol, LW_LINEARIZE_TOLERANCE_TYPE tolerance_type,
138  int flags)
139 {
140  POINT2D center;
141  POINT2D *t1 = (POINT2D*)p1;
142  POINT2D *t2 = (POINT2D*)p2;
143  POINT2D *t3 = (POINT2D*)p3;
144  POINT4D pt;
145  int p2_side = 0;
146  int clockwise = LW_TRUE;
147  double radius; /* Arc radius */
148  double increment; /* Angle per segment */
149  double angle_shift = 0;
150  double a1, a2, a3, angle;
151  POINTARRAY *pa = to;
152  int is_circle = LW_FALSE;
153  int points_added = 0;
154  int reverse = 0;
155 
156  LWDEBUG(2, "lwarc_linearize called.");
157 
158  p2_side = lw_segment_side(t1, t3, t2);
159 
160  /* Force counterclockwise scan if SYMMETRIC operation is requsested */
161  if ( p2_side == -1 && flags & LW_LINEARIZE_FLAG_SYMMETRIC )
162  {
163  /* swap p1-p3 */
164  t1 = (POINT2D*)p3;
165  t3 = (POINT2D*)p1;
166  p1 = (POINT4D*)t1;
167  p3 = (POINT4D*)t3;
168  p2_side = 1;
169  reverse = 1;
170  }
171 
172  radius = lw_arc_center(t1, t2, t3, &center);
173  LWDEBUGF(2, " center is POINT(%.15g %.15g) - radius:%g", center.x, center.y, radius);
174 
175  /* Matched start/end points imply circle */
176  if ( p1->x == p3->x && p1->y == p3->y )
177  is_circle = LW_TRUE;
178 
179  /* Negative radius signals straight line, p1/p2/p3 are colinear */
180  if ( (radius < 0.0 || p2_side == 0) && ! is_circle )
181  return 0;
182 
183  /* The side of the p1/p3 line that p2 falls on dictates the sweep
184  direction from p1 to p3. */
185  if ( p2_side == -1 )
186  clockwise = LW_TRUE;
187  else
188  clockwise = LW_FALSE;
189 
190  if ( tolerance_type == LW_LINEARIZE_TOLERANCE_TYPE_SEGS_PER_QUAD )
191  {{
192  int perQuad = rint(tol);
193  // error out if tol != perQuad ? (not-round)
194  if ( perQuad != tol )
195  {
196  lwerror("lwarc_linearize: segments per quadrant must be an integer value, got %.15g", tol, perQuad);
197  return -1;
198  }
199  if ( perQuad < 1 )
200  {
201  lwerror("lwarc_linearize: segments per quadrant must be at least 1, got %d", perQuad);
202  return -1;
203  }
204  increment = fabs(M_PI_2 / perQuad);
205  LWDEBUGF(2, "lwarc_linearize: perQuad:%d, increment:%g (%g degrees)", perQuad, increment, increment*180/M_PI);
206 
207  }}
208  else if ( tolerance_type == LW_LINEARIZE_TOLERANCE_TYPE_MAX_DEVIATION )
209  {{
210  double halfAngle, maxErr;
211  if ( tol <= 0 )
212  {
213  lwerror("lwarc_linearize: max deviation must be bigger than 0, got %.15g", tol);
214  return -1;
215  }
216 
217  /*
218  * Ref: https://en.wikipedia.org/wiki/Sagitta_(geometry)
219  *
220  * An arc "sagitta" (distance between middle point of arc and
221  * middle point of corresponding chord) is defined as:
222  *
223  * sagitta = radius * ( 1 - cos( angle ) );
224  *
225  * We want our sagitta to be at most "tolerance" long,
226  * and we want to find out angle, so we use the inverse
227  * formula:
228  *
229  * tol = radius * ( 1 - cos( angle ) );
230  * 1 - cos( angle ) = tol/radius
231  * - cos( angle ) = tol/radius - 1
232  * cos( angle ) = - tol/radius + 1
233  * angle = acos( 1 - tol/radius )
234  *
235  * Constraints: 1.0 - tol/radius must be between -1 and 1
236  * which means tol/radius must be between 0 and 2 times
237  * the radius, which makes sense as you cannot have a
238  * sagitta bigger than twice the radius!
239  *
240  */
241  maxErr = tol;
242  if ( maxErr > radius * 2 )
243  {
244  maxErr = radius * 2;
245  LWDEBUGF(2, "lwarc_linearize: tolerance %g is too big, "
246  "using arc-max 2 * radius == %g", tol, maxErr);
247  }
248  halfAngle = acos( 1.0 - maxErr / radius );
249  increment = 2 * halfAngle;
250  LWDEBUGF(2, "lwarc_linearize: maxDiff:%g, radius:%g, halfAngle:%g, increment:%g (%g degrees)", tol, radius, halfAngle, increment, increment*180/M_PI);
251  }}
252  else if ( tolerance_type == LW_LINEARIZE_TOLERANCE_TYPE_MAX_ANGLE )
253  {
254  increment = tol;
255  if ( increment <= 0 )
256  {
257  lwerror("lwarc_linearize: max angle must be bigger than 0, got %.15g", tol);
258  return -1;
259  }
260  }
261  else
262  {
263  lwerror("lwarc_linearize: unsupported tolerance type %d", tolerance_type);
264  return LW_FALSE;
265  }
266 
267  /* Angles of each point that defines the arc section */
268  a1 = atan2(p1->y - center.y, p1->x - center.x);
269  a2 = atan2(p2->y - center.y, p2->x - center.x);
270  a3 = atan2(p3->y - center.y, p3->x - center.x);
271 
272  LWDEBUGF(2, "lwarc_linearize A1:%g (%g) A2:%g (%g) A3:%g (%g)",
273  a1, a1*180/M_PI, a2, a2*180/M_PI, a3, a3*180/M_PI);
274 
275  if ( flags & LW_LINEARIZE_FLAG_SYMMETRIC )
276  {{
277  /* Calculate total arc angle, in radians */
278  double angle = clockwise ? a1 - a3 : a3 - a1;
279  if ( angle < 0 ) angle += M_PI * 2;
280  LWDEBUGF(2, "lwarc_linearize SYMMETRIC requested - total angle %g deg",
281  angle * 180 / M_PI);
282  if ( flags & LW_LINEARIZE_FLAG_RETAIN_ANGLE )
283  {{
284  /* Number of steps */
285  int steps = trunc(angle / increment);
286  /* Angle reminder */
287  double angle_reminder = angle - ( increment * steps );
288  angle_shift = angle_reminder / 2.0;
289 
290  LWDEBUGF(2, "lwarc_linearize RETAIN_ANGLE operation requested - "
291  "total angle %g, steps %d, increment %g, reminder %g",
292  angle * 180 / M_PI, steps, increment * 180 / M_PI,
293  angle_reminder * 180 / M_PI);
294  }}
295  else
296  {{
297  /* Number of segments in output */
298  int segs = ceil(angle / increment);
299  /* Tweak increment to be regular for all the arc */
300  increment = angle/segs;
301 
302  LWDEBUGF(2, "lwarc_linearize SYMMETRIC operation requested - "
303  "total angle %g degrees - LINESTRING(%g %g,%g %g,%g %g) - S:%d - I:%g",
304  angle*180/M_PI, p1->x, p1->y, center.x, center.y, p3->x, p3->y,
305  segs, increment*180/M_PI);
306  }}
307  }}
308 
309  /* p2 on left side => clockwise sweep */
310  if ( clockwise )
311  {
312  LWDEBUG(2, " Clockwise sweep");
313  increment *= -1;
314  angle_shift *= -1;
315  /* Adjust a3 down so we can decrement from a1 to a3 cleanly */
316  if ( a3 > a1 )
317  a3 -= 2.0 * M_PI;
318  if ( a2 > a1 )
319  a2 -= 2.0 * M_PI;
320  }
321  /* p2 on right side => counter-clockwise sweep */
322  else
323  {
324  LWDEBUG(2, " Counterclockwise sweep");
325  /* Adjust a3 up so we can increment from a1 to a3 cleanly */
326  if ( a3 < a1 )
327  a3 += 2.0 * M_PI;
328  if ( a2 < a1 )
329  a2 += 2.0 * M_PI;
330  }
331 
332  /* Override angles for circle case */
333  if( is_circle )
334  {
335  a3 = a1 + 2.0 * M_PI;
336  a2 = a1 + M_PI;
337  increment = fabs(increment);
338  clockwise = LW_FALSE;
339  }
340 
341  LWDEBUGF(2, "lwarc_linearize angle_shift:%g, increment:%g",
342  angle_shift * 180/M_PI, increment * 180/M_PI);
343 
344  if ( reverse ) {{
345  const int capacity = 8; /* TODO: compute exactly ? */
346  pa = ptarray_construct_empty(ptarray_has_z(to), ptarray_has_m(to), capacity);
347  }}
348 
349  /* Sweep from a1 to a3 */
350  if ( ! reverse )
351  {
353  }
354  ++points_added;
355  if ( angle_shift ) angle_shift -= increment;
356  LWDEBUGF(2, "a1:%g (%g deg), a3:%g (%g deg), inc:%g, shi:%g, cw:%d",
357  a1, a1 * 180 / M_PI, a3, a3 * 180 / M_PI, increment, angle_shift, clockwise);
358  for ( angle = a1 + increment + angle_shift; clockwise ? angle > a3 : angle < a3; angle += increment )
359  {
360  LWDEBUGF(2, " SA: %g ( %g deg )", angle, angle*180/M_PI);
361  pt.x = center.x + radius * cos(angle);
362  pt.y = center.y + radius * sin(angle);
363  pt.z = interpolate_arc(angle, a1, a2, a3, p1->z, p2->z, p3->z);
364  pt.m = interpolate_arc(angle, a1, a2, a3, p1->m, p2->m, p3->m);
365  ptarray_append_point(pa, &pt, LW_FALSE);
366  ++points_added;
367  angle_shift = 0;
368  }
369 
370  if ( reverse ) {{
371  int i;
373  for ( i=pa->npoints; i>0; i-- ) {
374  getPoint4d_p(pa, i-1, &pt);
375  ptarray_append_point(to, &pt, LW_FALSE);
376  }
377  ptarray_free(pa);
378  }}
379 
380  return points_added;
381 }
382 
383 /*
384  * @param icurve input curve
385  * @param tol tolerance, semantic driven by tolerance_type
386  * @param tolerance_type see LW_LINEARIZE_TOLERANCE_TYPE
387  * @param flags see flags in lwarc_linearize
388  *
389  * @return a newly allocated LWLINE
390  */
391 static LWLINE *
392 lwcircstring_linearize(const LWCIRCSTRING *icurve, double tol,
393  LW_LINEARIZE_TOLERANCE_TYPE tolerance_type,
394  int flags)
395 {
396  LWLINE *oline;
397  POINTARRAY *ptarray;
398  uint32_t i, j;
399  POINT4D p1, p2, p3, p4;
400  int ret;
401 
402  LWDEBUGF(2, "lwcircstring_linearize called., dim = %d", icurve->points->flags);
403 
404  ptarray = ptarray_construct_empty(FLAGS_GET_Z(icurve->points->flags), FLAGS_GET_M(icurve->points->flags), 64);
405 
406  for (i = 2; i < icurve->points->npoints; i+=2)
407  {
408  LWDEBUGF(3, "lwcircstring_linearize: arc ending at point %d", i);
409 
410  getPoint4d_p(icurve->points, i - 2, &p1);
411  getPoint4d_p(icurve->points, i - 1, &p2);
412  getPoint4d_p(icurve->points, i, &p3);
413 
414  ret = lwarc_linearize(ptarray, &p1, &p2, &p3, tol, tolerance_type, flags);
415  if ( ret > 0 )
416  {
417  LWDEBUGF(3, "lwcircstring_linearize: generated %d points", ptarray->npoints);
418  }
419  else if ( ret == 0 )
420  {
421  LWDEBUG(3, "lwcircstring_linearize: points are colinear, returning curve points as line");
422 
423  for (j = i - 2 ; j < i ; j++)
424  {
425  getPoint4d_p(icurve->points, j, &p4);
426  ptarray_append_point(ptarray, &p4, LW_TRUE);
427  }
428  }
429  else
430  {
431  /* An error occurred, lwerror should have been called by now */
432  ptarray_free(ptarray);
433  return NULL;
434  }
435  }
436  getPoint4d_p(icurve->points, icurve->points->npoints-1, &p1);
437  ptarray_append_point(ptarray, &p1, LW_TRUE);
438 
439  oline = lwline_construct(icurve->srid, NULL, ptarray);
440  return oline;
441 }
442 
443 /*
444  * @param icompound input compound curve
445  * @param tol tolerance, semantic driven by tolerance_type
446  * @param tolerance_type see LW_LINEARIZE_TOLERANCE_TYPE
447  * @param flags see flags in lwarc_linearize
448  *
449  * @return a newly allocated LWLINE
450  */
451 static LWLINE *
452 lwcompound_linearize(const LWCOMPOUND *icompound, double tol,
453  LW_LINEARIZE_TOLERANCE_TYPE tolerance_type,
454  int flags)
455 {
456  LWGEOM *geom;
457  POINTARRAY *ptarray = NULL, *ptarray_out = NULL;
458  LWLINE *tmp = NULL;
459  uint32_t i, j;
460  POINT4D p;
461 
462  LWDEBUG(2, "lwcompound_stroke called.");
463 
464  ptarray = ptarray_construct_empty(FLAGS_GET_Z(icompound->flags), FLAGS_GET_M(icompound->flags), 64);
465 
466  for (i = 0; i < icompound->ngeoms; i++)
467  {
468  geom = icompound->geoms[i];
469  if (geom->type == CIRCSTRINGTYPE)
470  {
471  tmp = lwcircstring_linearize((LWCIRCSTRING *)geom, tol, tolerance_type, flags);
472  for (j = 0; j < tmp->points->npoints; j++)
473  {
474  getPoint4d_p(tmp->points, j, &p);
475  ptarray_append_point(ptarray, &p, LW_TRUE);
476  }
477  lwline_free(tmp);
478  }
479  else if (geom->type == LINETYPE)
480  {
481  tmp = (LWLINE *)geom;
482  for (j = 0; j < tmp->points->npoints; j++)
483  {
484  getPoint4d_p(tmp->points, j, &p);
485  ptarray_append_point(ptarray, &p, LW_TRUE);
486  }
487  }
488  else
489  {
490  lwerror("Unsupported geometry type %d found.",
491  geom->type, lwtype_name(geom->type));
492  return NULL;
493  }
494  }
495  ptarray_out = ptarray_remove_repeated_points(ptarray, 0.0);
496  ptarray_free(ptarray);
497  return lwline_construct(icompound->srid, NULL, ptarray_out);
498 }
499 
500 /* Kept for backward compatibility - TODO: drop */
501 LWLINE *
502 lwcompound_stroke(const LWCOMPOUND *icompound, uint32_t perQuad)
503 {
505 }
506 
507 
508 /*
509  * @param icompound input curve polygon
510  * @param tol tolerance, semantic driven by tolerance_type
511  * @param tolerance_type see LW_LINEARIZE_TOLERANCE_TYPE
512  * @param flags see flags in lwarc_linearize
513  *
514  * @return a newly allocated LWPOLY
515  */
516 static LWPOLY *
517 lwcurvepoly_linearize(const LWCURVEPOLY *curvepoly, double tol,
518  LW_LINEARIZE_TOLERANCE_TYPE tolerance_type,
519  int flags)
520 {
521  LWPOLY *ogeom;
522  LWGEOM *tmp;
523  LWLINE *line;
524  POINTARRAY **ptarray;
525  uint32_t i;
526 
527  LWDEBUG(2, "lwcurvepoly_linearize called.");
528 
529  ptarray = lwalloc(sizeof(POINTARRAY *)*curvepoly->nrings);
530 
531  for (i = 0; i < curvepoly->nrings; i++)
532  {
533  tmp = curvepoly->rings[i];
534  if (tmp->type == CIRCSTRINGTYPE)
535  {
536  line = lwcircstring_linearize((LWCIRCSTRING *)tmp, tol, tolerance_type, flags);
537  ptarray[i] = ptarray_clone_deep(line->points);
538  lwline_free(line);
539  }
540  else if (tmp->type == LINETYPE)
541  {
542  line = (LWLINE *)tmp;
543  ptarray[i] = ptarray_clone_deep(line->points);
544  }
545  else if (tmp->type == COMPOUNDTYPE)
546  {
547  line = lwcompound_linearize((LWCOMPOUND *)tmp, tol, tolerance_type, flags);
548  ptarray[i] = ptarray_clone_deep(line->points);
549  lwline_free(line);
550  }
551  else
552  {
553  lwerror("Invalid ring type found in CurvePoly.");
554  return NULL;
555  }
556  }
557 
558  ogeom = lwpoly_construct(curvepoly->srid, NULL, curvepoly->nrings, ptarray);
559  return ogeom;
560 }
561 
562 /* Kept for backward compatibility - TODO: drop */
563 LWPOLY *
564 lwcurvepoly_stroke(const LWCURVEPOLY *curvepoly, uint32_t perQuad)
565 {
567 }
568 
569 
578 static LWMLINE *
579 lwmcurve_linearize(const LWMCURVE *mcurve, double tol,
581  int flags)
582 {
583  LWMLINE *ogeom;
584  LWGEOM **lines;
585  uint32_t i;
586 
587  LWDEBUGF(2, "lwmcurve_linearize called, geoms=%d, dim=%d.", mcurve->ngeoms, FLAGS_NDIMS(mcurve->flags));
588 
589  lines = lwalloc(sizeof(LWGEOM *)*mcurve->ngeoms);
590 
591  for (i = 0; i < mcurve->ngeoms; i++)
592  {
593  const LWGEOM *tmp = mcurve->geoms[i];
594  if (tmp->type == CIRCSTRINGTYPE)
595  {
596  lines[i] = (LWGEOM *)lwcircstring_linearize((LWCIRCSTRING *)tmp, tol, type, flags);
597  }
598  else if (tmp->type == LINETYPE)
599  {
600  lines[i] = (LWGEOM *)lwline_construct(mcurve->srid, NULL, ptarray_clone_deep(((LWLINE *)tmp)->points));
601  }
602  else if (tmp->type == COMPOUNDTYPE)
603  {
604  lines[i] = (LWGEOM *)lwcompound_linearize((LWCOMPOUND *)tmp, tol, type, flags);
605  }
606  else
607  {
608  lwerror("Unsupported geometry found in MultiCurve.");
609  return NULL;
610  }
611  }
612 
613  ogeom = (LWMLINE *)lwcollection_construct(MULTILINETYPE, mcurve->srid, NULL, mcurve->ngeoms, lines);
614  return ogeom;
615 }
616 
625 static LWMPOLY *
626 lwmsurface_linearize(const LWMSURFACE *msurface, double tol,
628  int flags)
629 {
630  LWMPOLY *ogeom;
631  LWGEOM *tmp;
632  LWPOLY *poly;
633  LWGEOM **polys;
634  POINTARRAY **ptarray;
635  uint32_t i, j;
636 
637  LWDEBUG(2, "lwmsurface_linearize called.");
638 
639  polys = lwalloc(sizeof(LWGEOM *)*msurface->ngeoms);
640 
641  for (i = 0; i < msurface->ngeoms; i++)
642  {
643  tmp = msurface->geoms[i];
644  if (tmp->type == CURVEPOLYTYPE)
645  {
646  polys[i] = (LWGEOM *)lwcurvepoly_linearize((LWCURVEPOLY *)tmp, tol, type, flags);
647  }
648  else if (tmp->type == POLYGONTYPE)
649  {
650  poly = (LWPOLY *)tmp;
651  ptarray = lwalloc(sizeof(POINTARRAY *)*poly->nrings);
652  for (j = 0; j < poly->nrings; j++)
653  {
654  ptarray[j] = ptarray_clone_deep(poly->rings[j]);
655  }
656  polys[i] = (LWGEOM *)lwpoly_construct(msurface->srid, NULL, poly->nrings, ptarray);
657  }
658  }
659  ogeom = (LWMPOLY *)lwcollection_construct(MULTIPOLYGONTYPE, msurface->srid, NULL, msurface->ngeoms, polys);
660  return ogeom;
661 }
662 
671 static LWCOLLECTION *
672 lwcollection_linearize(const LWCOLLECTION *collection, double tol,
674  int flags)
675 {
676  LWCOLLECTION *ocol;
677  LWGEOM *tmp;
678  LWGEOM **geoms;
679  uint32_t i;
680 
681  LWDEBUG(2, "lwcollection_linearize called.");
682 
683  geoms = lwalloc(sizeof(LWGEOM *)*collection->ngeoms);
684 
685  for (i=0; i<collection->ngeoms; i++)
686  {
687  tmp = collection->geoms[i];
688  switch (tmp->type)
689  {
690  case CIRCSTRINGTYPE:
691  geoms[i] = (LWGEOM *)lwcircstring_linearize((LWCIRCSTRING *)tmp, tol, type, flags);
692  break;
693  case COMPOUNDTYPE:
694  geoms[i] = (LWGEOM *)lwcompound_linearize((LWCOMPOUND *)tmp, tol, type, flags);
695  break;
696  case CURVEPOLYTYPE:
697  geoms[i] = (LWGEOM *)lwcurvepoly_linearize((LWCURVEPOLY *)tmp, tol, type, flags);
698  break;
699  case MULTICURVETYPE:
700  case MULTISURFACETYPE:
701  case COLLECTIONTYPE:
702  geoms[i] = (LWGEOM *)lwcollection_linearize((LWCOLLECTION *)tmp, tol, type, flags);
703  break;
704  default:
705  geoms[i] = lwgeom_clone_deep(tmp);
706  break;
707  }
708  }
709  ocol = lwcollection_construct(COLLECTIONTYPE, collection->srid, NULL, collection->ngeoms, geoms);
710  return ocol;
711 }
712 
713 LWGEOM *
714 lwcurve_linearize(const LWGEOM *geom, double tol,
716  int flags)
717 {
718  LWGEOM * ogeom = NULL;
719  switch (geom->type)
720  {
721  case CIRCSTRINGTYPE:
722  ogeom = (LWGEOM *)lwcircstring_linearize((LWCIRCSTRING *)geom, tol, type, flags);
723  break;
724  case COMPOUNDTYPE:
725  ogeom = (LWGEOM *)lwcompound_linearize((LWCOMPOUND *)geom, tol, type, flags);
726  break;
727  case CURVEPOLYTYPE:
728  ogeom = (LWGEOM *)lwcurvepoly_linearize((LWCURVEPOLY *)geom, tol, type, flags);
729  break;
730  case MULTICURVETYPE:
731  ogeom = (LWGEOM *)lwmcurve_linearize((LWMCURVE *)geom, tol, type, flags);
732  break;
733  case MULTISURFACETYPE:
734  ogeom = (LWGEOM *)lwmsurface_linearize((LWMSURFACE *)geom, tol, type, flags);
735  break;
736  case COLLECTIONTYPE:
737  ogeom = (LWGEOM *)lwcollection_linearize((LWCOLLECTION *)geom, tol, type, flags);
738  break;
739  default:
740  ogeom = lwgeom_clone_deep(geom);
741  }
742  return ogeom;
743 }
744 
745 /* Kept for backward compatibility - TODO: drop */
746 LWGEOM *
747 lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
748 {
750 }
751 
756 static double
757 lw_arc_angle(const POINT2D *a, const POINT2D *b, const POINT2D *c)
758 {
759  POINT2D ab, cb;
760 
761  ab.x = b->x - a->x;
762  ab.y = b->y - a->y;
763 
764  cb.x = b->x - c->x;
765  cb.y = b->y - c->y;
766 
767  double dot = (ab.x * cb.x + ab.y * cb.y); /* dot product */
768  double cross = (ab.x * cb.y - ab.y * cb.x); /* cross product */
769 
770  double alpha = atan2(cross, dot);
771 
772  return alpha;
773 }
774 
779 static int pt_continues_arc(const POINT4D *a1, const POINT4D *a2, const POINT4D *a3, const POINT4D *b)
780 {
781  POINT2D center;
782  POINT2D *t1 = (POINT2D*)a1;
783  POINT2D *t2 = (POINT2D*)a2;
784  POINT2D *t3 = (POINT2D*)a3;
785  POINT2D *tb = (POINT2D*)b;
786  double radius = lw_arc_center(t1, t2, t3, &center);
787  double b_distance, diff;
788 
789  /* Co-linear a1/a2/a3 */
790  if ( radius < 0.0 )
791  return LW_FALSE;
792 
793  b_distance = distance2d_pt_pt(tb, &center);
794  diff = fabs(radius - b_distance);
795  LWDEBUGF(4, "circle_radius=%g, b_distance=%g, diff=%g, percentage=%g", radius, b_distance, diff, diff/radius);
796 
797  /* Is the point b on the circle? */
798  if ( diff < EPSILON_SQLMM )
799  {
800  int a2_side = lw_segment_side(t1, t3, t2);
801  int b_side = lw_segment_side(t1, t3, tb);
802  double angle1 = lw_arc_angle(t1, t2, t3);
803  double angle2 = lw_arc_angle(t2, t3, tb);
804 
805  /* Is the angle similar to the previous one ? */
806  diff = fabs(angle1 - angle2);
807  LWDEBUGF(4, " angle1: %g, angle2: %g, diff:%g", angle1, angle2, diff);
808  if ( diff > EPSILON_SQLMM )
809  {
810  return LW_FALSE;
811  }
812 
813  /* Is the point b on the same side of a1/a3 as the mid-point a2 is? */
814  /* If not, it's in the unbounded part of the circle, so it continues the arc, return true. */
815  if ( b_side != a2_side )
816  return LW_TRUE;
817  }
818  return LW_FALSE;
819 }
820 
821 static LWGEOM*
822 linestring_from_pa(const POINTARRAY *pa, int srid, int start, int end)
823 {
824  int i = 0, j = 0;
825  POINT4D p;
826  POINTARRAY *pao = ptarray_construct(ptarray_has_z(pa), ptarray_has_m(pa), end-start+2);
827  LWDEBUGF(4, "srid=%d, start=%d, end=%d", srid, start, end);
828  for( i = start; i < end + 2; i++ )
829  {
830  getPoint4d_p(pa, i, &p);
831  ptarray_set_point4d(pao, j++, &p);
832  }
833  return lwline_as_lwgeom(lwline_construct(srid, NULL, pao));
834 }
835 
836 static LWGEOM*
837 circstring_from_pa(const POINTARRAY *pa, int srid, int start, int end)
838 {
839 
840  POINT4D p0, p1, p2;
842  LWDEBUGF(4, "srid=%d, start=%d, end=%d", srid, start, end);
843  getPoint4d_p(pa, start, &p0);
844  ptarray_set_point4d(pao, 0, &p0);
845  getPoint4d_p(pa, (start+end+1)/2, &p1);
846  ptarray_set_point4d(pao, 1, &p1);
847  getPoint4d_p(pa, end+1, &p2);
848  ptarray_set_point4d(pao, 2, &p2);
849  return lwcircstring_as_lwgeom(lwcircstring_construct(srid, NULL, pao));
850 }
851 
852 static LWGEOM*
853 geom_from_pa(const POINTARRAY *pa, int srid, int is_arc, int start, int end)
854 {
855  LWDEBUGF(4, "srid=%d, is_arc=%d, start=%d, end=%d", srid, is_arc, start, end);
856  if ( is_arc )
857  return circstring_from_pa(pa, srid, start, end);
858  else
859  return linestring_from_pa(pa, srid, start, end);
860 }
861 
862 LWGEOM*
863 pta_unstroke(const POINTARRAY *points, int srid)
864 {
865  int i = 0, j, k;
866  POINT4D a1, a2, a3, b;
867  POINT4D first, center;
868  char *edges_in_arcs;
869  int found_arc = LW_FALSE;
870  int current_arc = 1;
871  int num_edges;
872  int edge_type; /* non-zero if edge is part of an arc */
873  int start, end;
874  LWCOLLECTION *outcol;
875  /* Minimum number of edges, per quadrant, required to define an arc */
876  const unsigned int min_quad_edges = 2;
877 
878  /* Die on null input */
879  if ( ! points )
880  lwerror("pta_unstroke called with null pointarray");
881 
882  /* Null on empty input? */
883  if ( points->npoints == 0 )
884  return NULL;
885 
886  /* We can't desegmentize anything shorter than four points */
887  if ( points->npoints < 4 )
888  {
889  /* Return a linestring here*/
890  lwerror("pta_unstroke needs implementation for npoints < 4");
891  }
892 
893  /* Allocate our result array of vertices that are part of arcs */
894  num_edges = points->npoints - 1;
895  edges_in_arcs = lwalloc(num_edges + 1);
896  memset(edges_in_arcs, 0, num_edges + 1);
897 
898  /* We make a candidate arc of the first two edges, */
899  /* And then see if the next edge follows it */
900  while( i < num_edges-2 )
901  {
902  unsigned int arc_edges;
903  double num_quadrants;
904  double angle;
905 
906  found_arc = LW_FALSE;
907  /* Make candidate arc */
908  getPoint4d_p(points, i , &a1);
909  getPoint4d_p(points, i+1, &a2);
910  getPoint4d_p(points, i+2, &a3);
911  memcpy(&first, &a1, sizeof(POINT4D));
912 
913  for( j = i+3; j < num_edges+1; j++ )
914  {
915  LWDEBUGF(4, "i=%d, j=%d", i, j);
916  getPoint4d_p(points, j, &b);
917  /* Does this point fall on our candidate arc? */
918  if ( pt_continues_arc(&a1, &a2, &a3, &b) )
919  {
920  /* Yes. Mark this edge and the two preceding it as arc components */
921  LWDEBUGF(4, "pt_continues_arc #%d", current_arc);
922  found_arc = LW_TRUE;
923  for ( k = j-1; k > j-4; k-- )
924  edges_in_arcs[k] = current_arc;
925  }
926  else
927  {
928  /* No. So we're done with this candidate arc */
929  LWDEBUG(4, "pt_continues_arc = false");
930  current_arc++;
931  break;
932  }
933 
934  memcpy(&a1, &a2, sizeof(POINT4D));
935  memcpy(&a2, &a3, sizeof(POINT4D));
936  memcpy(&a3, &b, sizeof(POINT4D));
937  }
938  /* Jump past all the edges that were added to the arc */
939  if ( found_arc )
940  {
941  /* Check if an arc was composed by enough edges to be
942  * really considered an arc
943  * See http://trac.osgeo.org/postgis/ticket/2420
944  */
945  arc_edges = j - 1 - i;
946  LWDEBUGF(4, "arc defined by %d edges found", arc_edges);
947  if ( first.x == b.x && first.y == b.y ) {
948  LWDEBUG(4, "arc is a circle");
949  num_quadrants = 4;
950  }
951  else {
952  lw_arc_center((POINT2D*)&first, (POINT2D*)&b, (POINT2D*)&a1, (POINT2D*)&center);
953  angle = lw_arc_angle((POINT2D*)&first, (POINT2D*)&center, (POINT2D*)&b);
954  int p2_side = lw_segment_side((POINT2D*)&first, (POINT2D*)&a1, (POINT2D*)&b);
955  if ( p2_side >= 0 ) angle = -angle;
956 
957  if ( angle < 0 ) angle = 2 * M_PI + angle;
958  num_quadrants = ( 4 * angle ) / ( 2 * M_PI );
959  LWDEBUGF(4, "arc angle (%g %g, %g %g, %g %g) is %g (side is %d), quadrants:%g", first.x, first.y, center.x, center.y, b.x, b.y, angle, p2_side, num_quadrants);
960  }
961  /* a1 is first point, b is last point */
962  if ( arc_edges < min_quad_edges * num_quadrants ) {
963  LWDEBUGF(4, "Not enough edges for a %g quadrants arc, %g needed", num_quadrants, min_quad_edges * num_quadrants);
964  for ( k = j-1; k >= i; k-- )
965  edges_in_arcs[k] = 0;
966  }
967 
968  i = j-1;
969  }
970  else
971  {
972  /* Mark this edge as a linear edge */
973  edges_in_arcs[i] = 0;
974  i = i+1;
975  }
976  }
977 
978 #if POSTGIS_DEBUG_LEVEL > 3
979  {
980  char *edgestr = lwalloc(num_edges+1);
981  for ( i = 0; i < num_edges; i++ )
982  {
983  if ( edges_in_arcs[i] )
984  edgestr[i] = 48 + edges_in_arcs[i];
985  else
986  edgestr[i] = '.';
987  }
988  edgestr[num_edges] = 0;
989  LWDEBUGF(3, "edge pattern %s", edgestr);
990  lwfree(edgestr);
991  }
992 #endif
993 
994  start = 0;
995  edge_type = edges_in_arcs[0];
996  outcol = lwcollection_construct_empty(COMPOUNDTYPE, srid, ptarray_has_z(points), ptarray_has_m(points));
997  for( i = 1; i < num_edges; i++ )
998  {
999  if( edge_type != edges_in_arcs[i] )
1000  {
1001  end = i - 1;
1002  lwcollection_add_lwgeom(outcol, geom_from_pa(points, srid, edge_type, start, end));
1003  start = i;
1004  edge_type = edges_in_arcs[i];
1005  }
1006  }
1007  lwfree(edges_in_arcs); /* not needed anymore */
1008 
1009  /* Roll out last item */
1010  end = num_edges - 1;
1011  lwcollection_add_lwgeom(outcol, geom_from_pa(points, srid, edge_type, start, end));
1012 
1013  /* Strip down to singleton if only one entry */
1014  if ( outcol->ngeoms == 1 )
1015  {
1016  LWGEOM *outgeom = outcol->geoms[0];
1017  outcol->ngeoms = 0; lwcollection_free(outcol);
1018  return outgeom;
1019  }
1020  return lwcollection_as_lwgeom(outcol);
1021 }
1022 
1023 
1024 LWGEOM *
1026 {
1027  LWDEBUG(2, "lwline_unstroke called.");
1028 
1029  if ( line->points->npoints < 4 ) return lwline_as_lwgeom(lwline_clone_deep(line));
1030  else return pta_unstroke(line->points, line->srid);
1031 }
1032 
1033 LWGEOM *
1035 {
1036  LWGEOM **geoms;
1037  uint32_t i, hascurve = 0;
1038 
1039  LWDEBUG(2, "lwpolygon_unstroke called.");
1040 
1041  geoms = lwalloc(sizeof(LWGEOM *)*poly->nrings);
1042  for (i=0; i<poly->nrings; i++)
1043  {
1044  geoms[i] = pta_unstroke(poly->rings[i], poly->srid);
1045  if (geoms[i]->type == CIRCSTRINGTYPE || geoms[i]->type == COMPOUNDTYPE)
1046  {
1047  hascurve = 1;
1048  }
1049  }
1050  if (hascurve == 0)
1051  {
1052  for (i=0; i<poly->nrings; i++)
1053  {
1054  lwfree(geoms[i]); /* TODO: should this be lwgeom_free instead ? */
1055  }
1056  return lwgeom_clone_deep((LWGEOM *)poly);
1057  }
1058 
1059  return (LWGEOM *)lwcollection_construct(CURVEPOLYTYPE, poly->srid, NULL, poly->nrings, geoms);
1060 }
1061 
1062 LWGEOM *
1064 {
1065  LWGEOM **geoms;
1066  uint32_t i, hascurve = 0;
1067 
1068  LWDEBUG(2, "lwmline_unstroke called.");
1069 
1070  geoms = lwalloc(sizeof(LWGEOM *)*mline->ngeoms);
1071  for (i=0; i<mline->ngeoms; i++)
1072  {
1073  geoms[i] = lwline_unstroke((LWLINE *)mline->geoms[i]);
1074  if (geoms[i]->type == CIRCSTRINGTYPE || geoms[i]->type == COMPOUNDTYPE)
1075  {
1076  hascurve = 1;
1077  }
1078  }
1079  if (hascurve == 0)
1080  {
1081  for (i=0; i<mline->ngeoms; i++)
1082  {
1083  lwfree(geoms[i]); /* TODO: should this be lwgeom_free instead ? */
1084  }
1085  return lwgeom_clone_deep((LWGEOM *)mline);
1086  }
1087  return (LWGEOM *)lwcollection_construct(MULTICURVETYPE, mline->srid, NULL, mline->ngeoms, geoms);
1088 }
1089 
1090 LWGEOM *
1092 {
1093  LWGEOM **geoms;
1094  uint32_t i, hascurve = 0;
1095 
1096  LWDEBUG(2, "lwmpoly_unstroke called.");
1097 
1098  geoms = lwalloc(sizeof(LWGEOM *)*mpoly->ngeoms);
1099  for (i=0; i<mpoly->ngeoms; i++)
1100  {
1101  geoms[i] = lwpolygon_unstroke((LWPOLY *)mpoly->geoms[i]);
1102  if (geoms[i]->type == CURVEPOLYTYPE)
1103  {
1104  hascurve = 1;
1105  }
1106  }
1107  if (hascurve == 0)
1108  {
1109  for (i=0; i<mpoly->ngeoms; i++)
1110  {
1111  lwfree(geoms[i]); /* TODO: should this be lwgeom_free instead ? */
1112  }
1113  return lwgeom_clone_deep((LWGEOM *)mpoly);
1114  }
1115  return (LWGEOM *)lwcollection_construct(MULTISURFACETYPE, mpoly->srid, NULL, mpoly->ngeoms, geoms);
1116 }
1117 
1118 LWGEOM *
1120 {
1121  LWCOLLECTION *ret = lwalloc(sizeof(LWCOLLECTION));
1122  memcpy(ret, c, sizeof(LWCOLLECTION));
1123 
1124  if (c->ngeoms > 0)
1125  {
1126  uint32_t i;
1127  ret->geoms = lwalloc(sizeof(LWGEOM *)*c->ngeoms);
1128  for (i=0; i < c->ngeoms; i++)
1129  {
1130  ret->geoms[i] = lwgeom_unstroke(c->geoms[i]);
1131  }
1132  if (c->bbox)
1133  {
1134  ret->bbox = gbox_copy(c->bbox);
1135  }
1136  }
1137  else
1138  {
1139  ret->bbox = NULL;
1140  ret->geoms = NULL;
1141  }
1142  return (LWGEOM *)ret;
1143 }
1144 
1145 
1146 LWGEOM *
1148 {
1149  LWDEBUG(2, "lwgeom_unstroke called.");
1150 
1151  switch (geom->type)
1152  {
1153  case LINETYPE:
1154  return lwline_unstroke((LWLINE *)geom);
1155  case POLYGONTYPE:
1156  return lwpolygon_unstroke((LWPOLY *)geom);
1157  case MULTILINETYPE:
1158  return lwmline_unstroke((LWMLINE *)geom);
1159  case MULTIPOLYGONTYPE:
1160  return lwmpolygon_unstroke((LWMPOLY *)geom);
1161  case COLLECTIONTYPE:
1162  return lwcollection_unstroke((LWCOLLECTION *)geom);
1163  default:
1164  return lwgeom_clone_deep(geom);
1165  }
1166 }
1167 
int32_t srid
Definition: liblwgeom.h:521
double x
Definition: liblwgeom.h:354
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
#define LINETYPE
Definition: liblwgeom.h:85
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:433
LWGEOM * pta_unstroke(const POINTARRAY *points, int srid)
Definition: lwstroke.c:863
static LWMLINE * lwmcurve_linearize(const LWMCURVE *mcurve, double tol, LW_LINEARIZE_TOLERANCE_TYPE type, int flags)
Definition: lwstroke.c:579
LWGEOM * lwgeom_unstroke(const LWGEOM *geom)
Definition: lwstroke.c:1147
uint8_t flags
Definition: liblwgeom.h:545
POINTARRAY * ptarray_remove_repeated_points(const POINTARRAY *in, double tolerance)
Definition: ptarray.c:1439
static double lw_arc_angle(const POINT2D *a, const POINT2D *b, const POINT2D *c)
Return ABC angle in radians TODO: move to lwalgorithm.
Definition: lwstroke.c:757
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
double m
Definition: liblwgeom.h:354
uint32_t ngeoms
Definition: liblwgeom.h:561
#define MULTICURVETYPE
Definition: liblwgeom.h:94
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
Tolerance expresses the maximum angle between the radii generating approximation line vertices...
Definition: liblwgeom.h:2222
void lwfree(void *mem)
Definition: lwutil.c:244
LWGEOM ** rings
Definition: liblwgeom.h:537
uint32_t ngeoms
Definition: liblwgeom.h:496
int32_t srid
Definition: liblwgeom.h:445
#define POLYGONTYPE
Definition: liblwgeom.h:86
LWGEOM * lwcircstring_as_lwgeom(const LWCIRCSTRING *obj)
Definition: lwgeom.c:305
#define CURVEPOLYTYPE
Definition: liblwgeom.h:93
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:328
#define COMPOUNDTYPE
Definition: liblwgeom.h:92
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
static LWLINE * lwcompound_linearize(const LWCOMPOUND *icompound, double tol, LW_LINEARIZE_TOLERANCE_TYPE tolerance_type, int flags)
Definition: lwstroke.c:452
void lwline_free(LWLINE *line)
Definition: lwline.c:76
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:425
GBOX * bbox
Definition: liblwgeom.h:507
int32_t srid
Definition: liblwgeom.h:547
static LWPOLY * lwcurvepoly_linearize(const LWCURVEPOLY *curvepoly, double tol, LW_LINEARIZE_TOLERANCE_TYPE tolerance_type, int flags)
Definition: lwstroke.c:517
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define TRIANGLETYPE
Definition: liblwgeom.h:97
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:96
static int pt_continues_arc(const POINT4D *a1, const POINT4D *a2, const POINT4D *a3, const POINT4D *b)
Returns LW_TRUE if b is on the arc formed by a1/a2/a3, but not within that portion already described ...
Definition: lwstroke.c:779
static LWMPOLY * lwmsurface_linearize(const LWMSURFACE *msurface, double tol, LW_LINEARIZE_TOLERANCE_TYPE type, int flags)
Definition: lwstroke.c:626
LW_LINEARIZE_TOLERANCE_TYPE
Semantic of the tolerance argument passed to lwcurve_linearize.
Definition: liblwgeom.h:2201
static LWGEOM * circstring_from_pa(const POINTARRAY *pa, int srid, int start, int end)
Definition: lwstroke.c:837
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:520
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2312
Tolerance expresses the maximum distance between an arbitrary point on the curve and the closest poin...
Definition: liblwgeom.h:2214
int32_t srid
Definition: liblwgeom.h:423
LWGEOM ** geoms
Definition: liblwgeom.h:524
uint8_t flags
Definition: liblwgeom.h:519
uint32_t ngeoms
Definition: liblwgeom.h:509
uint32_t nrings
Definition: liblwgeom.h:457
LWPOLY * lwcurvepoly_stroke(const LWCURVEPOLY *curvepoly, uint32_t perQuad)
Definition: lwstroke.c:564
static int lwarc_linearize(POINTARRAY *to, const POINT4D *p1, const POINT4D *p2, const POINT4D *p3, double tol, LW_LINEARIZE_TOLERANCE_TYPE tolerance_type, int flags)
Segmentize an arc.
Definition: lwstroke.c:135
unsigned int uint32_t
Definition: uthash.h:78
double x
Definition: liblwgeom.h:330
LWGEOM * lwpolygon_unstroke(const LWPOLY *poly)
Definition: lwstroke.c:1034
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:330
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
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
int32_t srid
Definition: liblwgeom.h:534
#define LW_FALSE
Definition: liblwgeom.h:76
uint8_t flags
Definition: liblwgeom.h:371
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
LWPOLY ** geoms
Definition: liblwgeom.h:498
#define EPSILON_SQLMM
Tolerance used to determine equality.
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWGEOM ** geoms
Definition: liblwgeom.h:511
LWLINE * lwcompound_stroke(const LWCOMPOUND *icompound, uint32_t perQuad)
Definition: lwstroke.c:502
static LWGEOM * geom_from_pa(const POINTARRAY *pa, int srid, int is_arc, int start, int end)
Definition: lwstroke.c:853
#define TINTYPE
Definition: liblwgeom.h:98
uint32_t ngeoms
Definition: liblwgeom.h:522
POINTARRAY ** rings
Definition: liblwgeom.h:459
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:628
static LWLINE * lwcircstring_linearize(const LWCIRCSTRING *icurve, double tol, LW_LINEARIZE_TOLERANCE_TYPE tolerance_type, int flags)
Definition: lwstroke.c:392
int32_t srid
Definition: liblwgeom.h:508
double y
Definition: liblwgeom.h:330
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:139
static double interpolate_arc(double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3)
Definition: lwstroke.c:96
double z
Definition: liblwgeom.h:354
LWCIRCSTRING * lwcircstring_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwcircstring.c:50
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:113
Symmetric linearization means that the output vertices would be the same no matter the order of the p...
Definition: liblwgeom.h:2231
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
uint32_t ngeoms
Definition: liblwgeom.h:483
LWLINE ** geoms
Definition: liblwgeom.h:485
Tolerance expresses the number of segments to use for each quarter of circle (quadrant).
Definition: liblwgeom.h:2207
int ptarray_has_m(const POINTARRAY *pa)
Definition: ptarray.c:43
#define MULTISURFACETYPE
Definition: liblwgeom.h:95
int lwgeom_has_arc(const LWGEOM *geom)
Definition: lwstroke.c:55
int32_t srid
Definition: liblwgeom.h:456
LWGEOM * lwcurve_linearize(const LWGEOM *geom, double tol, LW_LINEARIZE_TOLERANCE_TYPE type, int flags)
Definition: lwstroke.c:714
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:747
LWGEOM ** geoms
Definition: liblwgeom.h:563
LWGEOM * lwline_unstroke(const LWLINE *line)
Definition: lwstroke.c:1025
LWGEOM ** geoms
Definition: liblwgeom.h:550
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
Retain angle instructs the engine to try its best to retain the requested angle between generating ra...
Definition: liblwgeom.h:2251
int32_t srid
Definition: liblwgeom.h:560
uint8_t type
Definition: liblwgeom.h:398
type
Definition: ovdump.py:41
static LWGEOM * linestring_from_pa(const POINTARRAY *pa, int srid, int start, int end)
Definition: lwstroke.c:822
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:356
POINTARRAY * points
Definition: liblwgeom.h:446
static LWCOLLECTION * lwcollection_linearize(const LWCOLLECTION *collection, double tol, LW_LINEARIZE_TOLERANCE_TYPE type, int flags)
Definition: lwstroke.c:672
uint32_t nrings
Definition: liblwgeom.h:535
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:91
int ptarray_has_z(const POINTARRAY *pa)
Definition: ptarray.c:36
void * lwalloc(size_t size)
Definition: lwutil.c:229
uint32_t ngeoms
Definition: liblwgeom.h:548
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
Definition: lwalgorithm.c:64
double y
Definition: liblwgeom.h:354
#define MULTILINETYPE
Definition: liblwgeom.h:88
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
LWLINE * lwline_clone_deep(const LWLINE *lwgeom)
Definition: lwline.c:118
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
LWGEOM * lwcollection_unstroke(const LWCOLLECTION *c)
Definition: lwstroke.c:1119
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:151
LWGEOM * lwmpolygon_unstroke(const LWMPOLY *mpoly)
Definition: lwstroke.c:1091
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:187
int32_t srid
Definition: liblwgeom.h:495
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
LWGEOM * lwmline_unstroke(const LWMLINE *mline)
Definition: lwstroke.c:1063
int32_t srid
Definition: liblwgeom.h:482
POINTARRAY * points
Definition: liblwgeom.h:424
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:300
uint32_t npoints
Definition: liblwgeom.h:373