PostGIS  2.5.0beta2dev-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  LWDEBUGF(2, " curve is CIRCULARSTRING(%.15g %.15f, %.15f %.15f, %.15f %15f)",
159  t1->x, t1->y, t2->x, t2->y, t3->x, t3->y);
160 
161  p2_side = lw_segment_side(t1, t3, t2);
162 
163  LWDEBUGF(2, " p2 side is %d", p2_side);
164 
165  /* Force counterclockwise scan if SYMMETRIC operation is requsested */
166  if ( p2_side == -1 && flags & LW_LINEARIZE_FLAG_SYMMETRIC )
167  {
168  /* swap p1-p3 */
169  t1 = (POINT2D*)p3;
170  t3 = (POINT2D*)p1;
171  p1 = (POINT4D*)t1;
172  p3 = (POINT4D*)t3;
173  p2_side = 1;
174  reverse = 1;
175  }
176 
177  radius = lw_arc_center(t1, t2, t3, &center);
178  LWDEBUGF(2, " center is POINT(%.15g %.15g) - radius:%g", center.x, center.y, radius);
179 
180  /* Matched start/end points imply circle */
181  if ( p1->x == p3->x && p1->y == p3->y )
182  is_circle = LW_TRUE;
183 
184  /* Negative radius signals straight line, p1/p2/p3 are colinear */
185  if ( (radius < 0.0 || p2_side == 0) && ! is_circle )
186  return 0;
187 
188  /* The side of the p1/p3 line that p2 falls on dictates the sweep
189  direction from p1 to p3. */
190  if ( p2_side == -1 )
191  clockwise = LW_TRUE;
192  else
193  clockwise = LW_FALSE;
194 
195  if ( tolerance_type == LW_LINEARIZE_TOLERANCE_TYPE_SEGS_PER_QUAD )
196  {{
197  int perQuad = rint(tol);
198  // error out if tol != perQuad ? (not-round)
199  if ( perQuad != tol )
200  {
201  lwerror("lwarc_linearize: segments per quadrant must be an integer value, got %.15g", tol, perQuad);
202  return -1;
203  }
204  if ( perQuad < 1 )
205  {
206  lwerror("lwarc_linearize: segments per quadrant must be at least 1, got %d", perQuad);
207  return -1;
208  }
209  increment = fabs(M_PI_2 / perQuad);
210  LWDEBUGF(2, "lwarc_linearize: perQuad:%d, increment:%g (%g degrees)", perQuad, increment, increment*180/M_PI);
211 
212  }}
213  else if ( tolerance_type == LW_LINEARIZE_TOLERANCE_TYPE_MAX_DEVIATION )
214  {{
215  double halfAngle, maxErr;
216  if ( tol <= 0 )
217  {
218  lwerror("lwarc_linearize: max deviation must be bigger than 0, got %.15g", tol);
219  return -1;
220  }
221 
222  /*
223  * Ref: https://en.wikipedia.org/wiki/Sagitta_(geometry)
224  *
225  * An arc "sagitta" (distance between middle point of arc and
226  * middle point of corresponding chord) is defined as:
227  *
228  * sagitta = radius * ( 1 - cos( angle ) );
229  *
230  * We want our sagitta to be at most "tolerance" long,
231  * and we want to find out angle, so we use the inverse
232  * formula:
233  *
234  * tol = radius * ( 1 - cos( angle ) );
235  * 1 - cos( angle ) = tol/radius
236  * - cos( angle ) = tol/radius - 1
237  * cos( angle ) = - tol/radius + 1
238  * angle = acos( 1 - tol/radius )
239  *
240  * Constraints: 1.0 - tol/radius must be between -1 and 1
241  * which means tol must be between 0 and 2 times
242  * the radius, which makes sense as you cannot have a
243  * sagitta bigger than twice the radius!
244  *
245  */
246  maxErr = tol;
247  if ( maxErr > radius * 2 )
248  {
249  maxErr = radius * 2;
250  LWDEBUGF(2, "lwarc_linearize: tolerance %g is too big, "
251  "using arc-max 2 * radius == %g", tol, maxErr);
252  }
253  do {
254  halfAngle = acos( 1.0 - maxErr / radius );
255  /* TODO: avoid a loop here, going rather straight to
256  * a minimum angle value */
257  if ( halfAngle != 0 ) break;
258  LWDEBUGF(2, "lwarc_linearize: tolerance %g is too small for this arc"
259  " to compute approximation angle, doubling it", maxErr);
260  maxErr *= 2;
261  } while(1);
262  increment = 2 * halfAngle;
263  LWDEBUGF(2, "lwarc_linearize: maxDiff:%g, radius:%g, halfAngle:%g, increment:%g (%g degrees)", tol, radius, halfAngle, increment, increment*180/M_PI);
264  }}
265  else if ( tolerance_type == LW_LINEARIZE_TOLERANCE_TYPE_MAX_ANGLE )
266  {
267  increment = tol;
268  if ( increment <= 0 )
269  {
270  lwerror("lwarc_linearize: max angle must be bigger than 0, got %.15g", tol);
271  return -1;
272  }
273  }
274  else
275  {
276  lwerror("lwarc_linearize: unsupported tolerance type %d", tolerance_type);
277  return LW_FALSE;
278  }
279 
280  /* Angles of each point that defines the arc section */
281  a1 = atan2(p1->y - center.y, p1->x - center.x);
282  a2 = atan2(p2->y - center.y, p2->x - center.x);
283  a3 = atan2(p3->y - center.y, p3->x - center.x);
284 
285  LWDEBUGF(2, "lwarc_linearize A1:%g (%g) A2:%g (%g) A3:%g (%g)",
286  a1, a1*180/M_PI, a2, a2*180/M_PI, a3, a3*180/M_PI);
287 
288  if ( flags & LW_LINEARIZE_FLAG_SYMMETRIC )
289  {{
290  /* Calculate total arc angle, in radians */
291  double angle = clockwise ? a1 - a3 : a3 - a1;
292  if ( angle < 0 ) angle += M_PI * 2;
293  LWDEBUGF(2, "lwarc_linearize SYMMETRIC requested - total angle %g deg",
294  angle * 180 / M_PI);
295  if ( flags & LW_LINEARIZE_FLAG_RETAIN_ANGLE )
296  {{
297  /* Number of steps */
298  int steps = trunc(angle / increment);
299  /* Angle reminder */
300  double angle_reminder = angle - ( increment * steps );
301  angle_shift = angle_reminder / 2.0;
302 
303  LWDEBUGF(2, "lwarc_linearize RETAIN_ANGLE operation requested - "
304  "total angle %g, steps %d, increment %g, reminder %g",
305  angle * 180 / M_PI, steps, increment * 180 / M_PI,
306  angle_reminder * 180 / M_PI);
307  }}
308  else
309  {{
310  /* Number of segments in output */
311  int segs = ceil(angle / increment);
312  /* Tweak increment to be regular for all the arc */
313  increment = angle/segs;
314 
315  LWDEBUGF(2, "lwarc_linearize SYMMETRIC operation requested - "
316  "total angle %g degrees - LINESTRING(%g %g,%g %g,%g %g) - S:%d - I:%g",
317  angle*180/M_PI, p1->x, p1->y, center.x, center.y, p3->x, p3->y,
318  segs, increment*180/M_PI);
319  }}
320  }}
321 
322  /* p2 on left side => clockwise sweep */
323  if ( clockwise )
324  {
325  LWDEBUG(2, " Clockwise sweep");
326  increment *= -1;
327  angle_shift *= -1;
328  /* Adjust a3 down so we can decrement from a1 to a3 cleanly */
329  if ( a3 > a1 )
330  a3 -= 2.0 * M_PI;
331  if ( a2 > a1 )
332  a2 -= 2.0 * M_PI;
333  }
334  /* p2 on right side => counter-clockwise sweep */
335  else
336  {
337  LWDEBUG(2, " Counterclockwise sweep");
338  /* Adjust a3 up so we can increment from a1 to a3 cleanly */
339  if ( a3 < a1 )
340  a3 += 2.0 * M_PI;
341  if ( a2 < a1 )
342  a2 += 2.0 * M_PI;
343  }
344 
345  /* Override angles for circle case */
346  if( is_circle )
347  {
348  a3 = a1 + 2.0 * M_PI;
349  a2 = a1 + M_PI;
350  increment = fabs(increment);
351  clockwise = LW_FALSE;
352  }
353 
354  LWDEBUGF(2, "lwarc_linearize angle_shift:%g, increment:%g",
355  angle_shift * 180/M_PI, increment * 180/M_PI);
356 
357  if ( reverse ) {{
358  const int capacity = 8; /* TODO: compute exactly ? */
359  pa = ptarray_construct_empty(ptarray_has_z(to), ptarray_has_m(to), capacity);
360  }}
361 
362  /* Sweep from a1 to a3 */
363  if ( ! reverse )
364  {
366  }
367  ++points_added;
368  if ( angle_shift ) angle_shift -= increment;
369  LWDEBUGF(2, "a1:%g (%g deg), a3:%g (%g deg), inc:%g, shi:%g, cw:%d",
370  a1, a1 * 180 / M_PI, a3, a3 * 180 / M_PI, increment, angle_shift, clockwise);
371  for ( angle = a1 + increment + angle_shift; clockwise ? angle > a3 : angle < a3; angle += increment )
372  {
373  LWDEBUGF(2, " SA: %g ( %g deg )", angle, angle*180/M_PI);
374  pt.x = center.x + radius * cos(angle);
375  pt.y = center.y + radius * sin(angle);
376  pt.z = interpolate_arc(angle, a1, a2, a3, p1->z, p2->z, p3->z);
377  pt.m = interpolate_arc(angle, a1, a2, a3, p1->m, p2->m, p3->m);
378  ptarray_append_point(pa, &pt, LW_FALSE);
379  ++points_added;
380  angle_shift = 0;
381  }
382 
383  if ( reverse ) {{
384  int i;
386  for ( i=pa->npoints; i>0; i-- ) {
387  getPoint4d_p(pa, i-1, &pt);
388  ptarray_append_point(to, &pt, LW_FALSE);
389  }
390  ptarray_free(pa);
391  }}
392 
393  return points_added;
394 }
395 
396 /*
397  * @param icurve input curve
398  * @param tol tolerance, semantic driven by tolerance_type
399  * @param tolerance_type see LW_LINEARIZE_TOLERANCE_TYPE
400  * @param flags see flags in lwarc_linearize
401  *
402  * @return a newly allocated LWLINE
403  */
404 static LWLINE *
405 lwcircstring_linearize(const LWCIRCSTRING *icurve, double tol,
406  LW_LINEARIZE_TOLERANCE_TYPE tolerance_type,
407  int flags)
408 {
409  LWLINE *oline;
410  POINTARRAY *ptarray;
411  uint32_t i, j;
412  POINT4D p1, p2, p3, p4;
413  int ret;
414 
415  LWDEBUGF(2, "lwcircstring_linearize called., dim = %d", icurve->points->flags);
416 
417  ptarray = ptarray_construct_empty(FLAGS_GET_Z(icurve->points->flags), FLAGS_GET_M(icurve->points->flags), 64);
418 
419  for (i = 2; i < icurve->points->npoints; i+=2)
420  {
421  LWDEBUGF(3, "lwcircstring_linearize: arc ending at point %d", i);
422 
423  getPoint4d_p(icurve->points, i - 2, &p1);
424  getPoint4d_p(icurve->points, i - 1, &p2);
425  getPoint4d_p(icurve->points, i, &p3);
426 
427  ret = lwarc_linearize(ptarray, &p1, &p2, &p3, tol, tolerance_type, flags);
428  if ( ret > 0 )
429  {
430  LWDEBUGF(3, "lwcircstring_linearize: generated %d points", ptarray->npoints);
431  }
432  else if ( ret == 0 )
433  {
434  LWDEBUG(3, "lwcircstring_linearize: points are colinear, returning curve points as line");
435 
436  for (j = i - 2 ; j < i ; j++)
437  {
438  getPoint4d_p(icurve->points, j, &p4);
439  ptarray_append_point(ptarray, &p4, LW_TRUE);
440  }
441  }
442  else
443  {
444  /* An error occurred, lwerror should have been called by now */
445  ptarray_free(ptarray);
446  return NULL;
447  }
448  }
449  getPoint4d_p(icurve->points, icurve->points->npoints-1, &p1);
450  ptarray_append_point(ptarray, &p1, LW_TRUE);
451 
452  oline = lwline_construct(icurve->srid, NULL, ptarray);
453  return oline;
454 }
455 
456 /*
457  * @param icompound input compound curve
458  * @param tol tolerance, semantic driven by tolerance_type
459  * @param tolerance_type see LW_LINEARIZE_TOLERANCE_TYPE
460  * @param flags see flags in lwarc_linearize
461  *
462  * @return a newly allocated LWLINE
463  */
464 static LWLINE *
465 lwcompound_linearize(const LWCOMPOUND *icompound, double tol,
466  LW_LINEARIZE_TOLERANCE_TYPE tolerance_type,
467  int flags)
468 {
469  LWGEOM *geom;
470  POINTARRAY *ptarray = NULL, *ptarray_out = NULL;
471  LWLINE *tmp = NULL;
472  uint32_t i, j;
473  POINT4D p;
474 
475  LWDEBUG(2, "lwcompound_stroke called.");
476 
477  ptarray = ptarray_construct_empty(FLAGS_GET_Z(icompound->flags), FLAGS_GET_M(icompound->flags), 64);
478 
479  for (i = 0; i < icompound->ngeoms; i++)
480  {
481  geom = icompound->geoms[i];
482  if (geom->type == CIRCSTRINGTYPE)
483  {
484  tmp = lwcircstring_linearize((LWCIRCSTRING *)geom, tol, tolerance_type, flags);
485  for (j = 0; j < tmp->points->npoints; j++)
486  {
487  getPoint4d_p(tmp->points, j, &p);
488  ptarray_append_point(ptarray, &p, LW_TRUE);
489  }
490  lwline_free(tmp);
491  }
492  else if (geom->type == LINETYPE)
493  {
494  tmp = (LWLINE *)geom;
495  for (j = 0; j < tmp->points->npoints; j++)
496  {
497  getPoint4d_p(tmp->points, j, &p);
498  ptarray_append_point(ptarray, &p, LW_TRUE);
499  }
500  }
501  else
502  {
503  lwerror("Unsupported geometry type %d found.",
504  geom->type, lwtype_name(geom->type));
505  return NULL;
506  }
507  }
508  ptarray_out = ptarray_remove_repeated_points(ptarray, 0.0);
509  ptarray_free(ptarray);
510  return lwline_construct(icompound->srid, NULL, ptarray_out);
511 }
512 
513 /* Kept for backward compatibility - TODO: drop */
514 LWLINE *
515 lwcompound_stroke(const LWCOMPOUND *icompound, uint32_t perQuad)
516 {
518 }
519 
520 
521 /*
522  * @param icompound input curve polygon
523  * @param tol tolerance, semantic driven by tolerance_type
524  * @param tolerance_type see LW_LINEARIZE_TOLERANCE_TYPE
525  * @param flags see flags in lwarc_linearize
526  *
527  * @return a newly allocated LWPOLY
528  */
529 static LWPOLY *
530 lwcurvepoly_linearize(const LWCURVEPOLY *curvepoly, double tol,
531  LW_LINEARIZE_TOLERANCE_TYPE tolerance_type,
532  int flags)
533 {
534  LWPOLY *ogeom;
535  LWGEOM *tmp;
536  LWLINE *line;
537  POINTARRAY **ptarray;
538  uint32_t i;
539 
540  LWDEBUG(2, "lwcurvepoly_linearize called.");
541 
542  ptarray = lwalloc(sizeof(POINTARRAY *)*curvepoly->nrings);
543 
544  for (i = 0; i < curvepoly->nrings; i++)
545  {
546  tmp = curvepoly->rings[i];
547  if (tmp->type == CIRCSTRINGTYPE)
548  {
549  line = lwcircstring_linearize((LWCIRCSTRING *)tmp, tol, tolerance_type, flags);
550  ptarray[i] = ptarray_clone_deep(line->points);
551  lwline_free(line);
552  }
553  else if (tmp->type == LINETYPE)
554  {
555  line = (LWLINE *)tmp;
556  ptarray[i] = ptarray_clone_deep(line->points);
557  }
558  else if (tmp->type == COMPOUNDTYPE)
559  {
560  line = lwcompound_linearize((LWCOMPOUND *)tmp, tol, tolerance_type, flags);
561  ptarray[i] = ptarray_clone_deep(line->points);
562  lwline_free(line);
563  }
564  else
565  {
566  lwerror("Invalid ring type found in CurvePoly.");
567  return NULL;
568  }
569  }
570 
571  ogeom = lwpoly_construct(curvepoly->srid, NULL, curvepoly->nrings, ptarray);
572  return ogeom;
573 }
574 
575 /* Kept for backward compatibility - TODO: drop */
576 LWPOLY *
577 lwcurvepoly_stroke(const LWCURVEPOLY *curvepoly, uint32_t perQuad)
578 {
580 }
581 
582 
591 static LWMLINE *
592 lwmcurve_linearize(const LWMCURVE *mcurve, double tol,
594  int flags)
595 {
596  LWMLINE *ogeom;
597  LWGEOM **lines;
598  uint32_t i;
599 
600  LWDEBUGF(2, "lwmcurve_linearize called, geoms=%d, dim=%d.", mcurve->ngeoms, FLAGS_NDIMS(mcurve->flags));
601 
602  lines = lwalloc(sizeof(LWGEOM *)*mcurve->ngeoms);
603 
604  for (i = 0; i < mcurve->ngeoms; i++)
605  {
606  const LWGEOM *tmp = mcurve->geoms[i];
607  if (tmp->type == CIRCSTRINGTYPE)
608  {
609  lines[i] = (LWGEOM *)lwcircstring_linearize((LWCIRCSTRING *)tmp, tol, type, flags);
610  }
611  else if (tmp->type == LINETYPE)
612  {
613  lines[i] = (LWGEOM *)lwline_construct(mcurve->srid, NULL, ptarray_clone_deep(((LWLINE *)tmp)->points));
614  }
615  else if (tmp->type == COMPOUNDTYPE)
616  {
617  lines[i] = (LWGEOM *)lwcompound_linearize((LWCOMPOUND *)tmp, tol, type, flags);
618  }
619  else
620  {
621  lwerror("Unsupported geometry found in MultiCurve.");
622  return NULL;
623  }
624  }
625 
626  ogeom = (LWMLINE *)lwcollection_construct(MULTILINETYPE, mcurve->srid, NULL, mcurve->ngeoms, lines);
627  return ogeom;
628 }
629 
638 static LWMPOLY *
639 lwmsurface_linearize(const LWMSURFACE *msurface, double tol,
641  int flags)
642 {
643  LWMPOLY *ogeom;
644  LWGEOM *tmp;
645  LWPOLY *poly;
646  LWGEOM **polys;
647  POINTARRAY **ptarray;
648  uint32_t i, j;
649 
650  LWDEBUG(2, "lwmsurface_linearize called.");
651 
652  polys = lwalloc(sizeof(LWGEOM *)*msurface->ngeoms);
653 
654  for (i = 0; i < msurface->ngeoms; i++)
655  {
656  tmp = msurface->geoms[i];
657  if (tmp->type == CURVEPOLYTYPE)
658  {
659  polys[i] = (LWGEOM *)lwcurvepoly_linearize((LWCURVEPOLY *)tmp, tol, type, flags);
660  }
661  else if (tmp->type == POLYGONTYPE)
662  {
663  poly = (LWPOLY *)tmp;
664  ptarray = lwalloc(sizeof(POINTARRAY *)*poly->nrings);
665  for (j = 0; j < poly->nrings; j++)
666  {
667  ptarray[j] = ptarray_clone_deep(poly->rings[j]);
668  }
669  polys[i] = (LWGEOM *)lwpoly_construct(msurface->srid, NULL, poly->nrings, ptarray);
670  }
671  }
672  ogeom = (LWMPOLY *)lwcollection_construct(MULTIPOLYGONTYPE, msurface->srid, NULL, msurface->ngeoms, polys);
673  return ogeom;
674 }
675 
684 static LWCOLLECTION *
685 lwcollection_linearize(const LWCOLLECTION *collection, double tol,
687  int flags)
688 {
689  LWCOLLECTION *ocol;
690  LWGEOM *tmp;
691  LWGEOM **geoms;
692  uint32_t i;
693 
694  LWDEBUG(2, "lwcollection_linearize called.");
695 
696  geoms = lwalloc(sizeof(LWGEOM *)*collection->ngeoms);
697 
698  for (i=0; i<collection->ngeoms; i++)
699  {
700  tmp = collection->geoms[i];
701  switch (tmp->type)
702  {
703  case CIRCSTRINGTYPE:
704  geoms[i] = (LWGEOM *)lwcircstring_linearize((LWCIRCSTRING *)tmp, tol, type, flags);
705  break;
706  case COMPOUNDTYPE:
707  geoms[i] = (LWGEOM *)lwcompound_linearize((LWCOMPOUND *)tmp, tol, type, flags);
708  break;
709  case CURVEPOLYTYPE:
710  geoms[i] = (LWGEOM *)lwcurvepoly_linearize((LWCURVEPOLY *)tmp, tol, type, flags);
711  break;
712  case MULTICURVETYPE:
713  case MULTISURFACETYPE:
714  case COLLECTIONTYPE:
715  geoms[i] = (LWGEOM *)lwcollection_linearize((LWCOLLECTION *)tmp, tol, type, flags);
716  break;
717  default:
718  geoms[i] = lwgeom_clone_deep(tmp);
719  break;
720  }
721  }
722  ocol = lwcollection_construct(COLLECTIONTYPE, collection->srid, NULL, collection->ngeoms, geoms);
723  return ocol;
724 }
725 
726 LWGEOM *
727 lwcurve_linearize(const LWGEOM *geom, double tol,
729  int flags)
730 {
731  LWGEOM * ogeom = NULL;
732  switch (geom->type)
733  {
734  case CIRCSTRINGTYPE:
735  ogeom = (LWGEOM *)lwcircstring_linearize((LWCIRCSTRING *)geom, tol, type, flags);
736  break;
737  case COMPOUNDTYPE:
738  ogeom = (LWGEOM *)lwcompound_linearize((LWCOMPOUND *)geom, tol, type, flags);
739  break;
740  case CURVEPOLYTYPE:
741  ogeom = (LWGEOM *)lwcurvepoly_linearize((LWCURVEPOLY *)geom, tol, type, flags);
742  break;
743  case MULTICURVETYPE:
744  ogeom = (LWGEOM *)lwmcurve_linearize((LWMCURVE *)geom, tol, type, flags);
745  break;
746  case MULTISURFACETYPE:
747  ogeom = (LWGEOM *)lwmsurface_linearize((LWMSURFACE *)geom, tol, type, flags);
748  break;
749  case COLLECTIONTYPE:
750  ogeom = (LWGEOM *)lwcollection_linearize((LWCOLLECTION *)geom, tol, type, flags);
751  break;
752  default:
753  ogeom = lwgeom_clone_deep(geom);
754  }
755  return ogeom;
756 }
757 
758 /* Kept for backward compatibility - TODO: drop */
759 LWGEOM *
760 lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
761 {
763 }
764 
769 static double
770 lw_arc_angle(const POINT2D *a, const POINT2D *b, const POINT2D *c)
771 {
772  POINT2D ab, cb;
773 
774  ab.x = b->x - a->x;
775  ab.y = b->y - a->y;
776 
777  cb.x = b->x - c->x;
778  cb.y = b->y - c->y;
779 
780  double dot = (ab.x * cb.x + ab.y * cb.y); /* dot product */
781  double cross = (ab.x * cb.y - ab.y * cb.x); /* cross product */
782 
783  double alpha = atan2(cross, dot);
784 
785  return alpha;
786 }
787 
792 static int pt_continues_arc(const POINT4D *a1, const POINT4D *a2, const POINT4D *a3, const POINT4D *b)
793 {
794  POINT2D center;
795  POINT2D *t1 = (POINT2D*)a1;
796  POINT2D *t2 = (POINT2D*)a2;
797  POINT2D *t3 = (POINT2D*)a3;
798  POINT2D *tb = (POINT2D*)b;
799  double radius = lw_arc_center(t1, t2, t3, &center);
800  double b_distance, diff;
801 
802  /* Co-linear a1/a2/a3 */
803  if ( radius < 0.0 )
804  return LW_FALSE;
805 
806  b_distance = distance2d_pt_pt(tb, &center);
807  diff = fabs(radius - b_distance);
808  LWDEBUGF(4, "circle_radius=%g, b_distance=%g, diff=%g, percentage=%g", radius, b_distance, diff, diff/radius);
809 
810  /* Is the point b on the circle? */
811  if ( diff < EPSILON_SQLMM )
812  {
813  int a2_side = lw_segment_side(t1, t3, t2);
814  int b_side = lw_segment_side(t1, t3, tb);
815  double angle1 = lw_arc_angle(t1, t2, t3);
816  double angle2 = lw_arc_angle(t2, t3, tb);
817 
818  /* Is the angle similar to the previous one ? */
819  diff = fabs(angle1 - angle2);
820  LWDEBUGF(4, " angle1: %g, angle2: %g, diff:%g", angle1, angle2, diff);
821  if ( diff > EPSILON_SQLMM )
822  {
823  return LW_FALSE;
824  }
825 
826  /* Is the point b on the same side of a1/a3 as the mid-point a2 is? */
827  /* If not, it's in the unbounded part of the circle, so it continues the arc, return true. */
828  if ( b_side != a2_side )
829  return LW_TRUE;
830  }
831  return LW_FALSE;
832 }
833 
834 static LWGEOM*
835 linestring_from_pa(const POINTARRAY *pa, int srid, int start, int end)
836 {
837  int i = 0, j = 0;
838  POINT4D p;
839  POINTARRAY *pao = ptarray_construct(ptarray_has_z(pa), ptarray_has_m(pa), end-start+2);
840  LWDEBUGF(4, "srid=%d, start=%d, end=%d", srid, start, end);
841  for( i = start; i < end + 2; i++ )
842  {
843  getPoint4d_p(pa, i, &p);
844  ptarray_set_point4d(pao, j++, &p);
845  }
846  return lwline_as_lwgeom(lwline_construct(srid, NULL, pao));
847 }
848 
849 static LWGEOM*
850 circstring_from_pa(const POINTARRAY *pa, int srid, int start, int end)
851 {
852 
853  POINT4D p0, p1, p2;
855  LWDEBUGF(4, "srid=%d, start=%d, end=%d", srid, start, end);
856  getPoint4d_p(pa, start, &p0);
857  ptarray_set_point4d(pao, 0, &p0);
858  getPoint4d_p(pa, (start+end+1)/2, &p1);
859  ptarray_set_point4d(pao, 1, &p1);
860  getPoint4d_p(pa, end+1, &p2);
861  ptarray_set_point4d(pao, 2, &p2);
862  return lwcircstring_as_lwgeom(lwcircstring_construct(srid, NULL, pao));
863 }
864 
865 static LWGEOM*
866 geom_from_pa(const POINTARRAY *pa, int srid, int is_arc, int start, int end)
867 {
868  LWDEBUGF(4, "srid=%d, is_arc=%d, start=%d, end=%d", srid, is_arc, start, end);
869  if ( is_arc )
870  return circstring_from_pa(pa, srid, start, end);
871  else
872  return linestring_from_pa(pa, srid, start, end);
873 }
874 
875 LWGEOM*
876 pta_unstroke(const POINTARRAY *points, int srid)
877 {
878  int i = 0, j, k;
879  POINT4D a1, a2, a3, b;
880  POINT4D first, center;
881  char *edges_in_arcs;
882  int found_arc = LW_FALSE;
883  int current_arc = 1;
884  int num_edges;
885  int edge_type; /* non-zero if edge is part of an arc */
886  int start, end;
887  LWCOLLECTION *outcol;
888  /* Minimum number of edges, per quadrant, required to define an arc */
889  const unsigned int min_quad_edges = 2;
890 
891  /* Die on null input */
892  if ( ! points )
893  lwerror("pta_unstroke called with null pointarray");
894 
895  /* Null on empty input? */
896  if ( points->npoints == 0 )
897  return NULL;
898 
899  /* We can't desegmentize anything shorter than four points */
900  if ( points->npoints < 4 )
901  {
902  /* Return a linestring here*/
903  lwerror("pta_unstroke needs implementation for npoints < 4");
904  }
905 
906  /* Allocate our result array of vertices that are part of arcs */
907  num_edges = points->npoints - 1;
908  edges_in_arcs = lwalloc(num_edges + 1);
909  memset(edges_in_arcs, 0, num_edges + 1);
910 
911  /* We make a candidate arc of the first two edges, */
912  /* And then see if the next edge follows it */
913  while( i < num_edges-2 )
914  {
915  unsigned int arc_edges;
916  double num_quadrants;
917  double angle;
918 
919  found_arc = LW_FALSE;
920  /* Make candidate arc */
921  getPoint4d_p(points, i , &a1);
922  getPoint4d_p(points, i+1, &a2);
923  getPoint4d_p(points, i+2, &a3);
924  memcpy(&first, &a1, sizeof(POINT4D));
925 
926  for( j = i+3; j < num_edges+1; j++ )
927  {
928  LWDEBUGF(4, "i=%d, j=%d", i, j);
929  getPoint4d_p(points, j, &b);
930  /* Does this point fall on our candidate arc? */
931  if ( pt_continues_arc(&a1, &a2, &a3, &b) )
932  {
933  /* Yes. Mark this edge and the two preceding it as arc components */
934  LWDEBUGF(4, "pt_continues_arc #%d", current_arc);
935  found_arc = LW_TRUE;
936  for ( k = j-1; k > j-4; k-- )
937  edges_in_arcs[k] = current_arc;
938  }
939  else
940  {
941  /* No. So we're done with this candidate arc */
942  LWDEBUG(4, "pt_continues_arc = false");
943  current_arc++;
944  break;
945  }
946 
947  memcpy(&a1, &a2, sizeof(POINT4D));
948  memcpy(&a2, &a3, sizeof(POINT4D));
949  memcpy(&a3, &b, sizeof(POINT4D));
950  }
951  /* Jump past all the edges that were added to the arc */
952  if ( found_arc )
953  {
954  /* Check if an arc was composed by enough edges to be
955  * really considered an arc
956  * See http://trac.osgeo.org/postgis/ticket/2420
957  */
958  arc_edges = j - 1 - i;
959  LWDEBUGF(4, "arc defined by %d edges found", arc_edges);
960  if ( first.x == b.x && first.y == b.y ) {
961  LWDEBUG(4, "arc is a circle");
962  num_quadrants = 4;
963  }
964  else {
965  lw_arc_center((POINT2D*)&first, (POINT2D*)&b, (POINT2D*)&a1, (POINT2D*)&center);
966  angle = lw_arc_angle((POINT2D*)&first, (POINT2D*)&center, (POINT2D*)&b);
967  int p2_side = lw_segment_side((POINT2D*)&first, (POINT2D*)&a1, (POINT2D*)&b);
968  if ( p2_side >= 0 ) angle = -angle;
969 
970  if ( angle < 0 ) angle = 2 * M_PI + angle;
971  num_quadrants = ( 4 * angle ) / ( 2 * M_PI );
972  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);
973  }
974  /* a1 is first point, b is last point */
975  if ( arc_edges < min_quad_edges * num_quadrants ) {
976  LWDEBUGF(4, "Not enough edges for a %g quadrants arc, %g needed", num_quadrants, min_quad_edges * num_quadrants);
977  for ( k = j-1; k >= i; k-- )
978  edges_in_arcs[k] = 0;
979  }
980 
981  i = j-1;
982  }
983  else
984  {
985  /* Mark this edge as a linear edge */
986  edges_in_arcs[i] = 0;
987  i = i+1;
988  }
989  }
990 
991 #if POSTGIS_DEBUG_LEVEL > 3
992  {
993  char *edgestr = lwalloc(num_edges+1);
994  for ( i = 0; i < num_edges; i++ )
995  {
996  if ( edges_in_arcs[i] )
997  edgestr[i] = 48 + edges_in_arcs[i];
998  else
999  edgestr[i] = '.';
1000  }
1001  edgestr[num_edges] = 0;
1002  LWDEBUGF(3, "edge pattern %s", edgestr);
1003  lwfree(edgestr);
1004  }
1005 #endif
1006 
1007  start = 0;
1008  edge_type = edges_in_arcs[0];
1009  outcol = lwcollection_construct_empty(COMPOUNDTYPE, srid, ptarray_has_z(points), ptarray_has_m(points));
1010  for( i = 1; i < num_edges; i++ )
1011  {
1012  if( edge_type != edges_in_arcs[i] )
1013  {
1014  end = i - 1;
1015  lwcollection_add_lwgeom(outcol, geom_from_pa(points, srid, edge_type, start, end));
1016  start = i;
1017  edge_type = edges_in_arcs[i];
1018  }
1019  }
1020  lwfree(edges_in_arcs); /* not needed anymore */
1021 
1022  /* Roll out last item */
1023  end = num_edges - 1;
1024  lwcollection_add_lwgeom(outcol, geom_from_pa(points, srid, edge_type, start, end));
1025 
1026  /* Strip down to singleton if only one entry */
1027  if ( outcol->ngeoms == 1 )
1028  {
1029  LWGEOM *outgeom = outcol->geoms[0];
1030  outcol->ngeoms = 0; lwcollection_free(outcol);
1031  return outgeom;
1032  }
1033  return lwcollection_as_lwgeom(outcol);
1034 }
1035 
1036 
1037 LWGEOM *
1039 {
1040  LWDEBUG(2, "lwline_unstroke called.");
1041 
1042  if ( line->points->npoints < 4 ) return lwline_as_lwgeom(lwline_clone_deep(line));
1043  else return pta_unstroke(line->points, line->srid);
1044 }
1045 
1046 LWGEOM *
1048 {
1049  LWGEOM **geoms;
1050  uint32_t i, hascurve = 0;
1051 
1052  LWDEBUG(2, "lwpolygon_unstroke called.");
1053 
1054  geoms = lwalloc(sizeof(LWGEOM *)*poly->nrings);
1055  for (i=0; i<poly->nrings; i++)
1056  {
1057  geoms[i] = pta_unstroke(poly->rings[i], poly->srid);
1058  if (geoms[i]->type == CIRCSTRINGTYPE || geoms[i]->type == COMPOUNDTYPE)
1059  {
1060  hascurve = 1;
1061  }
1062  }
1063  if (hascurve == 0)
1064  {
1065  for (i=0; i<poly->nrings; i++)
1066  {
1067  lwfree(geoms[i]); /* TODO: should this be lwgeom_free instead ? */
1068  }
1069  return lwgeom_clone_deep((LWGEOM *)poly);
1070  }
1071 
1072  return (LWGEOM *)lwcollection_construct(CURVEPOLYTYPE, poly->srid, NULL, poly->nrings, geoms);
1073 }
1074 
1075 LWGEOM *
1077 {
1078  LWGEOM **geoms;
1079  uint32_t i, hascurve = 0;
1080 
1081  LWDEBUG(2, "lwmline_unstroke called.");
1082 
1083  geoms = lwalloc(sizeof(LWGEOM *)*mline->ngeoms);
1084  for (i=0; i<mline->ngeoms; i++)
1085  {
1086  geoms[i] = lwline_unstroke((LWLINE *)mline->geoms[i]);
1087  if (geoms[i]->type == CIRCSTRINGTYPE || geoms[i]->type == COMPOUNDTYPE)
1088  {
1089  hascurve = 1;
1090  }
1091  }
1092  if (hascurve == 0)
1093  {
1094  for (i=0; i<mline->ngeoms; i++)
1095  {
1096  lwfree(geoms[i]); /* TODO: should this be lwgeom_free instead ? */
1097  }
1098  return lwgeom_clone_deep((LWGEOM *)mline);
1099  }
1100  return (LWGEOM *)lwcollection_construct(MULTICURVETYPE, mline->srid, NULL, mline->ngeoms, geoms);
1101 }
1102 
1103 LWGEOM *
1105 {
1106  LWGEOM **geoms;
1107  uint32_t i, hascurve = 0;
1108 
1109  LWDEBUG(2, "lwmpoly_unstroke called.");
1110 
1111  geoms = lwalloc(sizeof(LWGEOM *)*mpoly->ngeoms);
1112  for (i=0; i<mpoly->ngeoms; i++)
1113  {
1114  geoms[i] = lwpolygon_unstroke((LWPOLY *)mpoly->geoms[i]);
1115  if (geoms[i]->type == CURVEPOLYTYPE)
1116  {
1117  hascurve = 1;
1118  }
1119  }
1120  if (hascurve == 0)
1121  {
1122  for (i=0; i<mpoly->ngeoms; i++)
1123  {
1124  lwfree(geoms[i]); /* TODO: should this be lwgeom_free instead ? */
1125  }
1126  return lwgeom_clone_deep((LWGEOM *)mpoly);
1127  }
1128  return (LWGEOM *)lwcollection_construct(MULTISURFACETYPE, mpoly->srid, NULL, mpoly->ngeoms, geoms);
1129 }
1130 
1131 LWGEOM *
1133 {
1134  LWCOLLECTION *ret = lwalloc(sizeof(LWCOLLECTION));
1135  memcpy(ret, c, sizeof(LWCOLLECTION));
1136 
1137  if (c->ngeoms > 0)
1138  {
1139  uint32_t i;
1140  ret->geoms = lwalloc(sizeof(LWGEOM *)*c->ngeoms);
1141  for (i=0; i < c->ngeoms; i++)
1142  {
1143  ret->geoms[i] = lwgeom_unstroke(c->geoms[i]);
1144  }
1145  if (c->bbox)
1146  {
1147  ret->bbox = gbox_copy(c->bbox);
1148  }
1149  }
1150  else
1151  {
1152  ret->bbox = NULL;
1153  ret->geoms = NULL;
1154  }
1155  return (LWGEOM *)ret;
1156 }
1157 
1158 
1159 LWGEOM *
1161 {
1162  LWDEBUG(2, "lwgeom_unstroke called.");
1163 
1164  switch (geom->type)
1165  {
1166  case LINETYPE:
1167  return lwline_unstroke((LWLINE *)geom);
1168  case POLYGONTYPE:
1169  return lwpolygon_unstroke((LWPOLY *)geom);
1170  case MULTILINETYPE:
1171  return lwmline_unstroke((LWMLINE *)geom);
1172  case MULTIPOLYGONTYPE:
1173  return lwmpolygon_unstroke((LWMPOLY *)geom);
1174  case COLLECTIONTYPE:
1175  return lwcollection_unstroke((LWCOLLECTION *)geom);
1176  default:
1177  return lwgeom_clone_deep(geom);
1178  }
1179 }
1180 
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:228
#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:876
static LWMLINE * lwmcurve_linearize(const LWMCURVE *mcurve, double tol, LW_LINEARIZE_TOLERANCE_TYPE type, int flags)
Definition: lwstroke.c:592
LWGEOM * lwgeom_unstroke(const LWGEOM *geom)
Definition: lwstroke.c:1160
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:770
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:465
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:530
#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:792
static LWMPOLY * lwmsurface_linearize(const LWMSURFACE *msurface, double tol, LW_LINEARIZE_TOLERANCE_TYPE type, int flags)
Definition: lwstroke.c:639
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:850
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:2313
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:577
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:1047
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:515
static LWGEOM * geom_from_pa(const POINTARRAY *pa, int srid, int is_arc, int start, int end)
Definition: lwstroke.c:866
#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:405
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:727
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:760
LWGEOM ** geoms
Definition: liblwgeom.h:563
LWGEOM * lwline_unstroke(const LWLINE *line)
Definition: lwstroke.c:1038
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:835
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:685
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:1132
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:151
LWGEOM * lwmpolygon_unstroke(const LWMPOLY *mpoly)
Definition: lwstroke.c:1104
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:1076
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