PostGIS  2.2.8dev-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  * Copyright 2001-2006 Refractions Research Inc.
6  *
7  * This is free software; you can redistribute and/or modify it under
8  * the terms of the GNU General Public Licence. See the COPYING file.
9  *
10  **********************************************************************/
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 
17 #include "liblwgeom_internal.h"
18 
19 /* #define POSTGIS_DEBUG_LEVEL 4 */
20 
21 #include "lwgeom_log.h"
22 
23 
24 LWMLINE* lwmcurve_stroke(const LWMCURVE *mcurve, uint32_t perQuad);
25 LWMPOLY* lwmsurface_stroke(const LWMSURFACE *msurface, uint32_t perQuad);
26 LWCOLLECTION* lwcollection_stroke(const LWCOLLECTION *collection, uint32_t perQuad);
27 
28 LWGEOM* pta_unstroke(const POINTARRAY *points, int type, int srid);
29 LWGEOM* lwline_unstroke(const LWLINE *line);
30 LWGEOM* lwpolygon_unstroke(const LWPOLY *poly);
31 LWGEOM* lwmline_unstroke(const LWMLINE *mline);
32 LWGEOM* lwmpolygon_unstroke(const LWMPOLY *mpoly);
33 LWGEOM* lwgeom_unstroke(const LWGEOM *geom);
34 
35 
36 /*
37  * Determines (recursively in the case of collections) whether the geometry
38  * contains at least on arc geometry or segment.
39  */
40 int
41 lwgeom_has_arc(const LWGEOM *geom)
42 {
43  LWCOLLECTION *col;
44  int i;
45 
46  LWDEBUG(2, "lwgeom_has_arc called.");
47 
48  switch (geom->type)
49  {
50  case POINTTYPE:
51  case LINETYPE:
52  case POLYGONTYPE:
53  case TRIANGLETYPE:
54  case MULTIPOINTTYPE:
55  case MULTILINETYPE:
56  case MULTIPOLYGONTYPE:
58  case TINTYPE:
59  return LW_FALSE;
60  case CIRCSTRINGTYPE:
61  return LW_TRUE;
62  /* It's a collection that MAY contain an arc */
63  default:
64  col = (LWCOLLECTION *)geom;
65  for (i=0; i<col->ngeoms; i++)
66  {
67  if (lwgeom_has_arc(col->geoms[i]) == LW_TRUE)
68  return LW_TRUE;
69  }
70  return LW_FALSE;
71  }
72 }
73 
74 
75 
76 /*******************************************************************************
77  * Begin curve segmentize functions
78  ******************************************************************************/
79 
80 static double interpolate_arc(double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3)
81 {
82  LWDEBUGF(4,"angle %.05g a1 %.05g a2 %.05g a3 %.05g zm1 %.05g zm2 %.05g zm3 %.05g",angle,a1,a2,a3,zm1,zm2,zm3);
83  /* Counter-clockwise sweep */
84  if ( a1 < a2 )
85  {
86  if ( angle <= a2 )
87  return zm1 + (zm2-zm1) * (angle-a1) / (a2-a1);
88  else
89  return zm2 + (zm3-zm2) * (angle-a2) / (a3-a2);
90  }
91  /* Clockwise sweep */
92  else
93  {
94  if ( angle >= a2 )
95  return zm1 + (zm2-zm1) * (a1-angle) / (a1-a2);
96  else
97  return zm2 + (zm3-zm2) * (a2-angle) / (a2-a3);
98  }
99 }
100 
101 static POINTARRAY *
102 lwcircle_stroke(const POINT4D *p1, const POINT4D *p2, const POINT4D *p3, uint32_t perQuad)
103 {
104  POINT2D center;
105  POINT2D *t1 = (POINT2D*)p1;
106  POINT2D *t2 = (POINT2D*)p2;
107  POINT2D *t3 = (POINT2D*)p3;
108  POINT4D pt;
109  int p2_side = 0;
110  int clockwise = LW_TRUE;
111  double radius; /* Arc radius */
112  double increment; /* Angle per segment */
113  double a1, a2, a3, angle;
114  POINTARRAY *pa;
115  int is_circle = LW_FALSE;
116 
117  LWDEBUG(2, "lwcircle_calculate_gbox called.");
118 
119  radius = lw_arc_center(t1, t2, t3, &center);
120  p2_side = lw_segment_side(t1, t3, t2);
121 
122  /* Matched start/end points imply circle */
123  if ( p1->x == p3->x && p1->y == p3->y )
124  is_circle = LW_TRUE;
125 
126  /* Negative radius signals straight line, p1/p2/p3 are colinear */
127  if ( (radius < 0.0 || p2_side == 0) && ! is_circle )
128  return NULL;
129 
130  /* The side of the p1/p3 line that p2 falls on dictates the sweep
131  direction from p1 to p3. */
132  if ( p2_side == -1 )
133  clockwise = LW_TRUE;
134  else
135  clockwise = LW_FALSE;
136 
137  increment = fabs(M_PI_2 / perQuad);
138 
139  /* Angles of each point that defines the arc section */
140  a1 = atan2(p1->y - center.y, p1->x - center.x);
141  a2 = atan2(p2->y - center.y, p2->x - center.x);
142  a3 = atan2(p3->y - center.y, p3->x - center.x);
143 
144  /* p2 on left side => clockwise sweep */
145  if ( clockwise )
146  {
147  increment *= -1;
148  /* Adjust a3 down so we can decrement from a1 to a3 cleanly */
149  if ( a3 > a1 )
150  a3 -= 2.0 * M_PI;
151  if ( a2 > a1 )
152  a2 -= 2.0 * M_PI;
153  }
154  /* p2 on right side => counter-clockwise sweep */
155  else
156  {
157  /* Adjust a3 up so we can increment from a1 to a3 cleanly */
158  if ( a3 < a1 )
159  a3 += 2.0 * M_PI;
160  if ( a2 < a1 )
161  a2 += 2.0 * M_PI;
162  }
163 
164  /* Override angles for circle case */
165  if( is_circle )
166  {
167  a3 = a1 + 2.0 * M_PI;
168  a2 = a1 + M_PI;
169  increment = fabs(increment);
170  clockwise = LW_FALSE;
171  }
172 
173  /* Initialize point array */
174  pa = ptarray_construct_empty(1, 1, 32);
175 
176  /* Sweep from a1 to a3 */
178  for ( angle = a1 + increment; clockwise ? angle > a3 : angle < a3; angle += increment )
179  {
180  pt.x = center.x + radius * cos(angle);
181  pt.y = center.y + radius * sin(angle);
182  pt.z = interpolate_arc(angle, a1, a2, a3, p1->z, p2->z, p3->z);
183  pt.m = interpolate_arc(angle, a1, a2, a3, p1->m, p2->m, p3->m);
184  ptarray_append_point(pa, &pt, LW_FALSE);
185  }
186  return pa;
187 }
188 
189 LWLINE *
190 lwcircstring_stroke(const LWCIRCSTRING *icurve, uint32_t perQuad)
191 {
192  LWLINE *oline;
193  POINTARRAY *ptarray;
194  POINTARRAY *tmp;
195  uint32_t i, j;
196  POINT4D p1, p2, p3, p4;
197 
198  LWDEBUGF(2, "lwcircstring_stroke called., dim = %d", icurve->points->flags);
199 
200  ptarray = ptarray_construct_empty(FLAGS_GET_Z(icurve->points->flags), FLAGS_GET_M(icurve->points->flags), 64);
201 
202  for (i = 2; i < icurve->points->npoints; i+=2)
203  {
204  LWDEBUGF(3, "lwcircstring_stroke: arc ending at point %d", i);
205 
206  getPoint4d_p(icurve->points, i - 2, &p1);
207  getPoint4d_p(icurve->points, i - 1, &p2);
208  getPoint4d_p(icurve->points, i, &p3);
209  tmp = lwcircle_stroke(&p1, &p2, &p3, perQuad);
210 
211  if (tmp)
212  {
213  LWDEBUGF(3, "lwcircstring_stroke: generated %d points", tmp->npoints);
214 
215  for (j = 0; j < tmp->npoints; j++)
216  {
217  getPoint4d_p(tmp, j, &p4);
218  ptarray_append_point(ptarray, &p4, LW_TRUE);
219  }
220  ptarray_free(tmp);
221  }
222  else
223  {
224  LWDEBUG(3, "lwcircstring_stroke: points are colinear, returning curve points as line");
225 
226  for (j = i - 2 ; j < i ; j++)
227  {
228  getPoint4d_p(icurve->points, j, &p4);
229  ptarray_append_point(ptarray, &p4, LW_TRUE);
230  }
231  }
232 
233  }
234  getPoint4d_p(icurve->points, icurve->points->npoints-1, &p1);
235  ptarray_append_point(ptarray, &p1, LW_TRUE);
236 
237  oline = lwline_construct(icurve->srid, NULL, ptarray);
238  return oline;
239 }
240 
241 LWLINE *
242 lwcompound_stroke(const LWCOMPOUND *icompound, uint32_t perQuad)
243 {
244  LWGEOM *geom;
245  POINTARRAY *ptarray = NULL, *ptarray_out = NULL;
246  LWLINE *tmp = NULL;
247  uint32_t i, j;
248  POINT4D p;
249 
250  LWDEBUG(2, "lwcompound_stroke called.");
251 
252  ptarray = ptarray_construct_empty(FLAGS_GET_Z(icompound->flags), FLAGS_GET_M(icompound->flags), 64);
253 
254  for (i = 0; i < icompound->ngeoms; i++)
255  {
256  geom = icompound->geoms[i];
257  if (geom->type == CIRCSTRINGTYPE)
258  {
259  tmp = lwcircstring_stroke((LWCIRCSTRING *)geom, perQuad);
260  for (j = 0; j < tmp->points->npoints; j++)
261  {
262  getPoint4d_p(tmp->points, j, &p);
263  ptarray_append_point(ptarray, &p, LW_TRUE);
264  }
265  lwline_free(tmp);
266  }
267  else if (geom->type == LINETYPE)
268  {
269  tmp = (LWLINE *)geom;
270  for (j = 0; j < tmp->points->npoints; j++)
271  {
272  getPoint4d_p(tmp->points, j, &p);
273  ptarray_append_point(ptarray, &p, LW_TRUE);
274  }
275  }
276  else
277  {
278  lwerror("Unsupported geometry type %d found.",
279  geom->type, lwtype_name(geom->type));
280  return NULL;
281  }
282  }
283  ptarray_out = ptarray_remove_repeated_points(ptarray, 0.0);
284  ptarray_free(ptarray);
285  return lwline_construct(icompound->srid, NULL, ptarray_out);
286 }
287 
288 LWPOLY *
289 lwcurvepoly_stroke(const LWCURVEPOLY *curvepoly, uint32_t perQuad)
290 {
291  LWPOLY *ogeom;
292  LWGEOM *tmp;
293  LWLINE *line;
294  POINTARRAY **ptarray;
295  int i;
296 
297  LWDEBUG(2, "lwcurvepoly_stroke called.");
298 
299  ptarray = lwalloc(sizeof(POINTARRAY *)*curvepoly->nrings);
300 
301  for (i = 0; i < curvepoly->nrings; i++)
302  {
303  tmp = curvepoly->rings[i];
304  if (tmp->type == CIRCSTRINGTYPE)
305  {
306  line = lwcircstring_stroke((LWCIRCSTRING *)tmp, perQuad);
307  ptarray[i] = ptarray_clone_deep(line->points);
308  lwline_free(line);
309  }
310  else if (tmp->type == LINETYPE)
311  {
312  line = (LWLINE *)tmp;
313  ptarray[i] = ptarray_clone_deep(line->points);
314  }
315  else if (tmp->type == COMPOUNDTYPE)
316  {
317  line = lwcompound_stroke((LWCOMPOUND *)tmp, perQuad);
318  ptarray[i] = ptarray_clone_deep(line->points);
319  lwline_free(line);
320  }
321  else
322  {
323  lwerror("Invalid ring type found in CurvePoly.");
324  return NULL;
325  }
326  }
327 
328  ogeom = lwpoly_construct(curvepoly->srid, NULL, curvepoly->nrings, ptarray);
329  return ogeom;
330 }
331 
332 LWMLINE *
333 lwmcurve_stroke(const LWMCURVE *mcurve, uint32_t perQuad)
334 {
335  LWMLINE *ogeom;
336  LWGEOM **lines;
337  int i;
338 
339  LWDEBUGF(2, "lwmcurve_stroke called, geoms=%d, dim=%d.", mcurve->ngeoms, FLAGS_NDIMS(mcurve->flags));
340 
341  lines = lwalloc(sizeof(LWGEOM *)*mcurve->ngeoms);
342 
343  for (i = 0; i < mcurve->ngeoms; i++)
344  {
345  const LWGEOM *tmp = mcurve->geoms[i];
346  if (tmp->type == CIRCSTRINGTYPE)
347  {
348  lines[i] = (LWGEOM *)lwcircstring_stroke((LWCIRCSTRING *)tmp, perQuad);
349  }
350  else if (tmp->type == LINETYPE)
351  {
352  lines[i] = (LWGEOM *)lwline_construct(mcurve->srid, NULL, ptarray_clone_deep(((LWLINE *)tmp)->points));
353  }
354  else if (tmp->type == COMPOUNDTYPE)
355  {
356  lines[i] = (LWGEOM *)lwcompound_stroke((LWCOMPOUND *)tmp, perQuad);
357  }
358  else
359  {
360  lwerror("Unsupported geometry found in MultiCurve.");
361  return NULL;
362  }
363  }
364 
365  ogeom = (LWMLINE *)lwcollection_construct(MULTILINETYPE, mcurve->srid, NULL, mcurve->ngeoms, lines);
366  return ogeom;
367 }
368 
369 LWMPOLY *
370 lwmsurface_stroke(const LWMSURFACE *msurface, uint32_t perQuad)
371 {
372  LWMPOLY *ogeom;
373  LWGEOM *tmp;
374  LWPOLY *poly;
375  LWGEOM **polys;
376  POINTARRAY **ptarray;
377  int i, j;
378 
379  LWDEBUG(2, "lwmsurface_stroke called.");
380 
381  polys = lwalloc(sizeof(LWGEOM *)*msurface->ngeoms);
382 
383  for (i = 0; i < msurface->ngeoms; i++)
384  {
385  tmp = msurface->geoms[i];
386  if (tmp->type == CURVEPOLYTYPE)
387  {
388  polys[i] = (LWGEOM *)lwcurvepoly_stroke((LWCURVEPOLY *)tmp, perQuad);
389  }
390  else if (tmp->type == POLYGONTYPE)
391  {
392  poly = (LWPOLY *)tmp;
393  ptarray = lwalloc(sizeof(POINTARRAY *)*poly->nrings);
394  for (j = 0; j < poly->nrings; j++)
395  {
396  ptarray[j] = ptarray_clone_deep(poly->rings[j]);
397  }
398  polys[i] = (LWGEOM *)lwpoly_construct(msurface->srid, NULL, poly->nrings, ptarray);
399  }
400  }
401  ogeom = (LWMPOLY *)lwcollection_construct(MULTIPOLYGONTYPE, msurface->srid, NULL, msurface->ngeoms, polys);
402  return ogeom;
403 }
404 
405 LWCOLLECTION *
406 lwcollection_stroke(const LWCOLLECTION *collection, uint32_t perQuad)
407 {
408  LWCOLLECTION *ocol;
409  LWGEOM *tmp;
410  LWGEOM **geoms;
411  int i;
412 
413  LWDEBUG(2, "lwcollection_stroke called.");
414 
415  geoms = lwalloc(sizeof(LWGEOM *)*collection->ngeoms);
416 
417  for (i=0; i<collection->ngeoms; i++)
418  {
419  tmp = collection->geoms[i];
420  switch (tmp->type)
421  {
422  case CIRCSTRINGTYPE:
423  geoms[i] = (LWGEOM *)lwcircstring_stroke((LWCIRCSTRING *)tmp, perQuad);
424  break;
425  case COMPOUNDTYPE:
426  geoms[i] = (LWGEOM *)lwcompound_stroke((LWCOMPOUND *)tmp, perQuad);
427  break;
428  case CURVEPOLYTYPE:
429  geoms[i] = (LWGEOM *)lwcurvepoly_stroke((LWCURVEPOLY *)tmp, perQuad);
430  break;
431  case MULTICURVETYPE:
432  case MULTISURFACETYPE:
433  case COLLECTIONTYPE:
434  geoms[i] = (LWGEOM *)lwcollection_stroke((LWCOLLECTION *)tmp, perQuad);
435  break;
436  default:
437  geoms[i] = lwgeom_clone(tmp);
438  break;
439  }
440  }
441  ocol = lwcollection_construct(COLLECTIONTYPE, collection->srid, NULL, collection->ngeoms, geoms);
442  return ocol;
443 }
444 
445 LWGEOM *
446 lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
447 {
448  LWGEOM * ogeom = NULL;
449  switch (geom->type)
450  {
451  case CIRCSTRINGTYPE:
452  ogeom = (LWGEOM *)lwcircstring_stroke((LWCIRCSTRING *)geom, perQuad);
453  break;
454  case COMPOUNDTYPE:
455  ogeom = (LWGEOM *)lwcompound_stroke((LWCOMPOUND *)geom, perQuad);
456  break;
457  case CURVEPOLYTYPE:
458  ogeom = (LWGEOM *)lwcurvepoly_stroke((LWCURVEPOLY *)geom, perQuad);
459  break;
460  case MULTICURVETYPE:
461  ogeom = (LWGEOM *)lwmcurve_stroke((LWMCURVE *)geom, perQuad);
462  break;
463  case MULTISURFACETYPE:
464  ogeom = (LWGEOM *)lwmsurface_stroke((LWMSURFACE *)geom, perQuad);
465  break;
466  case COLLECTIONTYPE:
467  ogeom = (LWGEOM *)lwcollection_stroke((LWCOLLECTION *)geom, perQuad);
468  break;
469  default:
470  ogeom = lwgeom_clone(geom);
471  }
472  return ogeom;
473 }
474 
479 static double
480 lw_arc_angle(const POINT2D *a, const POINT2D *b, const POINT2D *c)
481 {
482  POINT2D ab, cb;
483 
484  ab.x = b->x - a->x;
485  ab.y = b->y - a->y;
486 
487  cb.x = b->x - c->x;
488  cb.y = b->y - c->y;
489 
490  double dot = (ab.x * cb.x + ab.y * cb.y); /* dot product */
491  double cross = (ab.x * cb.y - ab.y * cb.x); /* cross product */
492 
493  double alpha = atan2(cross, dot);
494 
495  return alpha;
496 }
497 
502 static int pt_continues_arc(const POINT4D *a1, const POINT4D *a2, const POINT4D *a3, const POINT4D *b)
503 {
504  POINT2D center;
505  POINT2D *t1 = (POINT2D*)a1;
506  POINT2D *t2 = (POINT2D*)a2;
507  POINT2D *t3 = (POINT2D*)a3;
508  POINT2D *tb = (POINT2D*)b;
509  double radius = lw_arc_center(t1, t2, t3, &center);
510  double b_distance, diff;
511 
512  /* Co-linear a1/a2/a3 */
513  if ( radius < 0.0 )
514  return LW_FALSE;
515 
516  b_distance = distance2d_pt_pt(tb, &center);
517  diff = fabs(radius - b_distance);
518  LWDEBUGF(4, "circle_radius=%g, b_distance=%g, diff=%g, percentage=%g", radius, b_distance, diff, diff/radius);
519 
520  /* Is the point b on the circle? */
521  if ( diff < EPSILON_SQLMM )
522  {
523  int a2_side = lw_segment_side(t1, t3, t2);
524  int b_side = lw_segment_side(t1, t3, tb);
525  double angle1 = lw_arc_angle(t1, t2, t3);
526  double angle2 = lw_arc_angle(t2, t3, tb);
527 
528  /* Is the angle similar to the previous one ? */
529  diff = fabs(angle1 - angle2);
530  LWDEBUGF(4, " angle1: %g, angle2: %g, diff:%g", angle1, angle2, diff);
531  if ( diff > EPSILON_SQLMM )
532  {
533  return LW_FALSE;
534  }
535 
536  /* Is the point b on the same side of a1/a3 as the mid-point a2 is? */
537  /* If not, it's in the unbounded part of the circle, so it continues the arc, return true. */
538  if ( b_side != a2_side )
539  return LW_TRUE;
540  }
541  return LW_FALSE;
542 }
543 
544 static LWGEOM*
545 linestring_from_pa(const POINTARRAY *pa, int srid, int start, int end)
546 {
547  int i = 0, j = 0;
548  POINT4D p;
549  POINTARRAY *pao = ptarray_construct(ptarray_has_z(pa), ptarray_has_m(pa), end-start+2);
550  LWDEBUGF(4, "srid=%d, start=%d, end=%d", srid, start, end);
551  for( i = start; i < end + 2; i++ )
552  {
553  getPoint4d_p(pa, i, &p);
554  ptarray_set_point4d(pao, j++, &p);
555  }
556  return lwline_as_lwgeom(lwline_construct(srid, NULL, pao));
557 }
558 
559 static LWGEOM*
560 circstring_from_pa(const POINTARRAY *pa, int srid, int start, int end)
561 {
562 
563  POINT4D p0, p1, p2;
565  LWDEBUGF(4, "srid=%d, start=%d, end=%d", srid, start, end);
566  getPoint4d_p(pa, start, &p0);
567  ptarray_set_point4d(pao, 0, &p0);
568  getPoint4d_p(pa, (start+end+1)/2, &p1);
569  ptarray_set_point4d(pao, 1, &p1);
570  getPoint4d_p(pa, end+1, &p2);
571  ptarray_set_point4d(pao, 2, &p2);
572  return lwcircstring_as_lwgeom(lwcircstring_construct(srid, NULL, pao));
573 }
574 
575 static LWGEOM*
576 geom_from_pa(const POINTARRAY *pa, int srid, int is_arc, int start, int end)
577 {
578  LWDEBUGF(4, "srid=%d, is_arc=%d, start=%d, end=%d", srid, is_arc, start, end);
579  if ( is_arc )
580  return circstring_from_pa(pa, srid, start, end);
581  else
582  return linestring_from_pa(pa, srid, start, end);
583 }
584 
585 LWGEOM*
586 pta_unstroke(const POINTARRAY *points, int type, int srid)
587 {
588  int i = 0, j, k;
589  POINT4D a1, a2, a3, b;
590  POINT4D first, center;
591  char *edges_in_arcs;
592  int found_arc = LW_FALSE;
593  int current_arc = 1;
594  int num_edges;
595  int edge_type; /* non-zero if edge is part of an arc */
596  int start, end;
597  LWCOLLECTION *outcol;
598  /* Minimum number of edges, per quadrant, required to define an arc */
599  const unsigned int min_quad_edges = 2;
600 
601  /* Die on null input */
602  if ( ! points )
603  lwerror("pta_unstroke called with null pointarray");
604 
605  /* Null on empty input? */
606  if ( points->npoints == 0 )
607  return NULL;
608 
609  /* We can't desegmentize anything shorter than four points */
610  if ( points->npoints < 4 )
611  {
612  /* Return a linestring here*/
613  lwerror("pta_unstroke needs implementation for npoints < 4");
614  }
615 
616  /* Allocate our result array of vertices that are part of arcs */
617  num_edges = points->npoints - 1;
618  edges_in_arcs = lwalloc(num_edges + 1);
619  memset(edges_in_arcs, 0, num_edges + 1);
620 
621  /* We make a candidate arc of the first two edges, */
622  /* And then see if the next edge follows it */
623  while( i < num_edges-2 )
624  {
625  unsigned int arc_edges;
626  double num_quadrants;
627  double angle;
628 
629  found_arc = LW_FALSE;
630  /* Make candidate arc */
631  getPoint4d_p(points, i , &a1);
632  getPoint4d_p(points, i+1, &a2);
633  getPoint4d_p(points, i+2, &a3);
634  memcpy(&first, &a1, sizeof(POINT4D));
635 
636  for( j = i+3; j < num_edges+1; j++ )
637  {
638  LWDEBUGF(4, "i=%d, j=%d", i, j);
639  getPoint4d_p(points, j, &b);
640  /* Does this point fall on our candidate arc? */
641  if ( pt_continues_arc(&a1, &a2, &a3, &b) )
642  {
643  /* Yes. Mark this edge and the two preceding it as arc components */
644  LWDEBUGF(4, "pt_continues_arc #%d", current_arc);
645  found_arc = LW_TRUE;
646  for ( k = j-1; k > j-4; k-- )
647  edges_in_arcs[k] = current_arc;
648  }
649  else
650  {
651  /* No. So we're done with this candidate arc */
652  LWDEBUG(4, "pt_continues_arc = false");
653  current_arc++;
654  break;
655  }
656 
657  memcpy(&a1, &a2, sizeof(POINT4D));
658  memcpy(&a2, &a3, sizeof(POINT4D));
659  memcpy(&a3, &b, sizeof(POINT4D));
660  }
661  /* Jump past all the edges that were added to the arc */
662  if ( found_arc )
663  {
664  /* Check if an arc was composed by enough edges to be
665  * really considered an arc
666  * See http://trac.osgeo.org/postgis/ticket/2420
667  */
668  arc_edges = j - 1 - i;
669  LWDEBUGF(4, "arc defined by %d edges found", arc_edges);
670  if ( first.x == b.x && first.y == b.y ) {
671  LWDEBUG(4, "arc is a circle");
672  num_quadrants = 4;
673  }
674  else {
675  lw_arc_center((POINT2D*)&first, (POINT2D*)&b, (POINT2D*)&a1, (POINT2D*)&center);
676  angle = lw_arc_angle((POINT2D*)&first, (POINT2D*)&center, (POINT2D*)&b);
677  int p2_side = lw_segment_side((POINT2D*)&first, (POINT2D*)&a1, (POINT2D*)&b);
678  if ( p2_side >= 0 ) angle = -angle;
679 
680  if ( angle < 0 ) angle = 2 * M_PI + angle;
681  num_quadrants = ( 4 * angle ) / ( 2 * M_PI );
682  LWDEBUGF(4, "arc angle (%g %g, %g %g, %g %g) is %g (side is %d), quandrants:%g", first.x, first.y, center.x, center.y, b.x, b.y, angle, p2_side, num_quadrants);
683  }
684  /* a1 is first point, b is last point */
685  if ( arc_edges < min_quad_edges * num_quadrants ) {
686  LWDEBUGF(4, "Not enough edges for a %g quadrants arc, %g needed", num_quadrants, min_quad_edges * num_quadrants);
687  for ( k = j-1; k >= i; k-- )
688  edges_in_arcs[k] = 0;
689  }
690 
691  i = j-1;
692  }
693  else
694  {
695  /* Mark this edge as a linear edge */
696  edges_in_arcs[i] = 0;
697  i = i+1;
698  }
699  }
700 
701 #if POSTGIS_DEBUG_LEVEL > 3
702  {
703  char *edgestr = lwalloc(num_edges+1);
704  for ( i = 0; i < num_edges; i++ )
705  {
706  if ( edges_in_arcs[i] )
707  edgestr[i] = 48 + edges_in_arcs[i];
708  else
709  edgestr[i] = '.';
710  }
711  edgestr[num_edges] = 0;
712  LWDEBUGF(3, "edge pattern %s", edgestr);
713  lwfree(edgestr);
714  }
715 #endif
716 
717  start = 0;
718  edge_type = edges_in_arcs[0];
719  outcol = lwcollection_construct_empty(COMPOUNDTYPE, srid, ptarray_has_z(points), ptarray_has_m(points));
720  for( i = 1; i < num_edges; i++ )
721  {
722  if( edge_type != edges_in_arcs[i] )
723  {
724  end = i - 1;
725  lwcollection_add_lwgeom(outcol, geom_from_pa(points, srid, edge_type, start, end));
726  start = i;
727  edge_type = edges_in_arcs[i];
728  }
729  }
730  lwfree(edges_in_arcs); /* not needed anymore */
731 
732  /* Roll out last item */
733  end = num_edges - 1;
734  lwcollection_add_lwgeom(outcol, geom_from_pa(points, srid, edge_type, start, end));
735 
736  /* Strip down to singleton if only one entry */
737  if ( outcol->ngeoms == 1 )
738  {
739  LWGEOM *outgeom = outcol->geoms[0];
740  outcol->ngeoms = 0; lwcollection_free(outcol);
741  return outgeom;
742  }
743  return lwcollection_as_lwgeom(outcol);
744 }
745 
746 
747 LWGEOM *
749 {
750  LWDEBUG(2, "lwline_unstroke called.");
751 
752  if ( line->points->npoints < 4 ) return lwline_as_lwgeom(lwline_clone(line));
753  else return pta_unstroke(line->points, line->flags, line->srid);
754 }
755 
756 LWGEOM *
758 {
759  LWGEOM **geoms;
760  int i, hascurve = 0;
761 
762  LWDEBUG(2, "lwpolygon_unstroke called.");
763 
764  geoms = lwalloc(sizeof(LWGEOM *)*poly->nrings);
765  for (i=0; i<poly->nrings; i++)
766  {
767  geoms[i] = pta_unstroke(poly->rings[i], poly->flags, poly->srid);
768  if (geoms[i]->type == CIRCSTRINGTYPE || geoms[i]->type == COMPOUNDTYPE)
769  {
770  hascurve = 1;
771  }
772  }
773  if (hascurve == 0)
774  {
775  for (i=0; i<poly->nrings; i++)
776  {
777  lwfree(geoms[i]); /* TODO: should this be lwgeom_free instead ? */
778  }
779  return lwgeom_clone((LWGEOM *)poly);
780  }
781 
782  return (LWGEOM *)lwcollection_construct(CURVEPOLYTYPE, poly->srid, NULL, poly->nrings, geoms);
783 }
784 
785 LWGEOM *
787 {
788  LWGEOM **geoms;
789  int i, hascurve = 0;
790 
791  LWDEBUG(2, "lwmline_unstroke called.");
792 
793  geoms = lwalloc(sizeof(LWGEOM *)*mline->ngeoms);
794  for (i=0; i<mline->ngeoms; i++)
795  {
796  geoms[i] = lwline_unstroke((LWLINE *)mline->geoms[i]);
797  if (geoms[i]->type == CIRCSTRINGTYPE || geoms[i]->type == COMPOUNDTYPE)
798  {
799  hascurve = 1;
800  }
801  }
802  if (hascurve == 0)
803  {
804  for (i=0; i<mline->ngeoms; i++)
805  {
806  lwfree(geoms[i]); /* TODO: should this be lwgeom_free instead ? */
807  }
808  return lwgeom_clone((LWGEOM *)mline);
809  }
810  return (LWGEOM *)lwcollection_construct(MULTICURVETYPE, mline->srid, NULL, mline->ngeoms, geoms);
811 }
812 
813 LWGEOM *
815 {
816  LWGEOM **geoms;
817  int i, hascurve = 0;
818 
819  LWDEBUG(2, "lwmpoly_unstroke called.");
820 
821  geoms = lwalloc(sizeof(LWGEOM *)*mpoly->ngeoms);
822  for (i=0; i<mpoly->ngeoms; i++)
823  {
824  geoms[i] = lwpolygon_unstroke((LWPOLY *)mpoly->geoms[i]);
825  if (geoms[i]->type == CURVEPOLYTYPE)
826  {
827  hascurve = 1;
828  }
829  }
830  if (hascurve == 0)
831  {
832  for (i=0; i<mpoly->ngeoms; i++)
833  {
834  lwfree(geoms[i]); /* TODO: should this be lwgeom_free instead ? */
835  }
836  return lwgeom_clone((LWGEOM *)mpoly);
837  }
838  return (LWGEOM *)lwcollection_construct(MULTISURFACETYPE, mpoly->srid, NULL, mpoly->ngeoms, geoms);
839 }
840 
841 LWGEOM *
843 {
844  LWDEBUG(2, "lwgeom_unstroke called.");
845 
846  switch (geom->type)
847  {
848  case LINETYPE:
849  return lwline_unstroke((LWLINE *)geom);
850  case POLYGONTYPE:
851  return lwpolygon_unstroke((LWPOLY *)geom);
852  case MULTILINETYPE:
853  return lwmline_unstroke((LWMLINE *)geom);
854  case MULTIPOLYGONTYPE:
855  return lwmpolygon_unstroke((LWMPOLY *)geom);
856  default:
857  return lwgeom_clone(geom);
858  }
859 }
860 
void ptarray_set_point4d(POINTARRAY *pa, int n, const POINT4D *p4d)
Definition: lwgeom_api.c:536
int ngeoms
Definition: liblwgeom.h:530
int32_t srid
Definition: liblwgeom.h:503
double x
Definition: liblwgeom.h:336
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:213
#define LINETYPE
Definition: liblwgeom.h:71
LWGEOM * lwgeom_unstroke(const LWGEOM *geom)
Definition: lwstroke.c:842
uint8_t flags
Definition: liblwgeom.h:527
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:480
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:336
#define MULTICURVETYPE
Definition: liblwgeom.h:80
LWMLINE * lwmcurve_stroke(const LWMCURVE *mcurve, uint32_t perQuad)
Definition: lwstroke.c:333
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:30
void lwfree(void *mem)
Definition: lwutil.c:214
static POINTARRAY * lwcircle_stroke(const POINT4D *p1, const POINT4D *p2, const POINT4D *p3, uint32_t perQuad)
Definition: lwstroke.c:102
LWGEOM ** rings
Definition: liblwgeom.h:519
LWLINE * lwline_clone(const LWLINE *lwgeom)
Definition: lwline.c:89
int npoints
Definition: liblwgeom.h:355
POINTARRAY * ptarray_remove_repeated_points(POINTARRAY *in, double tolerance)
Definition: ptarray.c:1499
int32_t srid
Definition: liblwgeom.h:427
#define POLYGONTYPE
Definition: liblwgeom.h:72
LWGEOM * lwcircstring_as_lwgeom(const LWCIRCSTRING *obj)
Definition: lwgeom.c:224
#define CURVEPOLYTYPE
Definition: liblwgeom.h:79
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:330
#define COMPOUNDTYPE
Definition: liblwgeom.h:78
#define MULTIPOINTTYPE
Definition: liblwgeom.h:73
void lwline_free(LWLINE *line)
Definition: lwline.c:63
int32_t srid
Definition: liblwgeom.h:529
LWMPOLY * lwmsurface_stroke(const LWMSURFACE *msurface, uint32_t perQuad)
Definition: lwstroke.c:370
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
#define TRIANGLETYPE
Definition: liblwgeom.h:83
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:82
LWCOLLECTION * lwcollection_stroke(const LWCOLLECTION *collection, uint32_t perQuad)
Definition: lwstroke.c:406
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:502
LWGEOM * pta_unstroke(const POINTARRAY *points, int type, int srid)
Definition: lwstroke.c:586
static LWGEOM * circstring_from_pa(const POINTARRAY *pa, int srid, int start, int end)
Definition: lwstroke.c:560
LWLINE * lwcircstring_stroke(const LWCIRCSTRING *icurve, uint32_t perQuad)
Definition: lwstroke.c:190
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
The old function nessecary for ptarray_segmentize2d in ptarray.c.
Definition: measures.c:2302
int32_t srid
Definition: liblwgeom.h:405
LWGEOM ** geoms
Definition: liblwgeom.h:506
uint8_t flags
Definition: liblwgeom.h:501
int ngeoms
Definition: liblwgeom.h:465
LWPOLY * lwcurvepoly_stroke(const LWCURVEPOLY *curvepoly, uint32_t perQuad)
Definition: lwstroke.c:289
double x
Definition: liblwgeom.h:312
LWGEOM * lwpolygon_unstroke(const LWPOLY *poly)
Definition: lwstroke.c:757
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:188
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
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_TRUE, then a duplicate point will not be added.
Definition: ptarray.c:156
int32_t srid
Definition: liblwgeom.h:516
#define LW_FALSE
Definition: liblwgeom.h:62
uint8_t flags
Definition: liblwgeom.h:353
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:29
LWPOLY ** geoms
Definition: liblwgeom.h:480
#define EPSILON_SQLMM
Tolerance used to determine equality.
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
LWGEOM ** geoms
Definition: liblwgeom.h:493
LWLINE * lwcompound_stroke(const LWCOMPOUND *icompound, uint32_t perQuad)
Definition: lwstroke.c:242
static LWGEOM * geom_from_pa(const POINTARRAY *pa, int srid, int is_arc, int start, int end)
Definition: lwstroke.c:576
#define TINTYPE
Definition: liblwgeom.h:84
POINTARRAY ** rings
Definition: liblwgeom.h:441
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:634
int nrings
Definition: liblwgeom.h:439
int32_t srid
Definition: liblwgeom.h:490
double y
Definition: liblwgeom.h:312
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:124
static double interpolate_arc(double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3)
Definition: lwstroke.c:80
double z
Definition: liblwgeom.h:336
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:395
int ngeoms
Definition: liblwgeom.h:478
LWCIRCSTRING * lwcircstring_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwcircstring.c:38
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
LWLINE ** geoms
Definition: liblwgeom.h:467
int ngeoms
Definition: liblwgeom.h:543
int ptarray_has_m(const POINTARRAY *pa)
Definition: ptarray.c:43
#define MULTISURFACETYPE
Definition: liblwgeom.h:81
int lwgeom_has_arc(const LWGEOM *geom)
Definition: lwstroke.c:41
int32_t srid
Definition: liblwgeom.h:438
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:446
LWGEOM ** geoms
Definition: liblwgeom.h:545
int ngeoms
Definition: liblwgeom.h:504
LWGEOM * lwline_unstroke(const LWLINE *line)
Definition: lwstroke.c:748
LWGEOM ** geoms
Definition: liblwgeom.h:532
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:70
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:125
int32_t srid
Definition: liblwgeom.h:542
uint8_t type
Definition: liblwgeom.h:380
type
Definition: ovdump.py:41
static LWGEOM * linestring_from_pa(const POINTARRAY *pa, int srid, int start, int end)
Definition: lwstroke.c:545
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:326
POINTARRAY * points
Definition: liblwgeom.h:428
uint8_t flags
Definition: liblwgeom.h:436
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:77
int ptarray_has_z(const POINTARRAY *pa)
Definition: ptarray.c:36
void * lwalloc(size_t size)
Definition: lwutil.c:199
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
Definition: lwalgorithm.c:50
double y
Definition: liblwgeom.h:336
#define MULTILINETYPE
Definition: liblwgeom.h:74
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:81
uint8_t flags
Definition: liblwgeom.h:403
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:136
LWGEOM * lwmpolygon_unstroke(const LWMPOLY *mpoly)
Definition: lwstroke.c:814
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:174
int32_t srid
Definition: liblwgeom.h:477
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74
int getPoint4d_p(const POINTARRAY *pa, int n, POINT4D *point)
Definition: lwgeom_api.c:231
#define COLLECTIONTYPE
Definition: liblwgeom.h:76
LWGEOM * lwmline_unstroke(const LWMLINE *mline)
Definition: lwstroke.c:786
int32_t srid
Definition: liblwgeom.h:464
POINTARRAY * points
Definition: liblwgeom.h:406
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:219