PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ ptarray_segmentize2d()

POINTARRAY* ptarray_segmentize2d ( const POINTARRAY ipa,
double  dist 
)

Returns a modified POINTARRAY so that no segment is longer than the given distance (computed using 2d).

Every input point is kept. Z and M values for added points (if needed) are set to 0.

Definition at line 414 of file ptarray.c.

References distance2d_pt_pt(), POINTARRAY::flags, FLAGS_GET_M, FLAGS_GET_Z, getPoint4d_p(), LW_FALSE, LW_ON_INTERRUPT, LW_TRUE, POINT4D::m, POINTARRAY::npoints, ptarray_append_point(), ptarray_construct_empty(), ptarray_free(), POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by lwline_segmentize2d(), and lwpoly_segmentize2d().

415 {
416  double segdist;
417  POINT4D p1, p2;
418  POINT4D pbuf;
419  POINTARRAY *opa;
420  uint32_t ipoff=0; /* input point offset */
421  int hasz = FLAGS_GET_Z(ipa->flags);
422  int hasm = FLAGS_GET_M(ipa->flags);
423 
424  pbuf.x = pbuf.y = pbuf.z = pbuf.m = 0;
425 
426  /* Initial storage */
427  opa = ptarray_construct_empty(hasz, hasm, ipa->npoints);
428 
429  /* Add first point */
430  getPoint4d_p(ipa, ipoff, &p1);
431  ptarray_append_point(opa, &p1, LW_FALSE);
432 
433  ipoff++;
434 
435  while (ipoff<ipa->npoints)
436  {
437  /*
438  * We use these pointers to avoid
439  * "strict-aliasing rules break" warning raised
440  * by gcc (3.3 and up).
441  *
442  * It looks that casting a variable address (also
443  * referred to as "type-punned pointer")
444  * breaks those "strict" rules.
445  *
446  */
447  POINT4D *p1ptr=&p1, *p2ptr=&p2;
448 
449  getPoint4d_p(ipa, ipoff, &p2);
450 
451  segdist = distance2d_pt_pt((POINT2D *)p1ptr, (POINT2D *)p2ptr);
452 
453  if (segdist > dist) /* add an intermediate point */
454  {
455  pbuf.x = p1.x + (p2.x-p1.x)/segdist * dist;
456  pbuf.y = p1.y + (p2.y-p1.y)/segdist * dist;
457  if( hasz )
458  pbuf.z = p1.z + (p2.z-p1.z)/segdist * dist;
459  if( hasm )
460  pbuf.m = p1.m + (p2.m-p1.m)/segdist * dist;
461  ptarray_append_point(opa, &pbuf, LW_FALSE);
462  p1 = pbuf;
463  }
464  else /* copy second point */
465  {
466  ptarray_append_point(opa, &p2, (ipa->npoints==2)?LW_TRUE:LW_FALSE);
467  p1 = p2;
468  ipoff++;
469  }
470 
471  LW_ON_INTERRUPT(ptarray_free(opa); return NULL);
472  }
473 
474  return opa;
475 }
double x
Definition: liblwgeom.h:354
double m
Definition: liblwgeom.h:354
#define LW_ON_INTERRUPT(x)
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int repeated_points)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE, then a duplicate point will not be added.
Definition: ptarray.c:156
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2313
unsigned int uint32_t
Definition: uthash.h:78
#define LW_FALSE
Definition: liblwgeom.h:76
uint8_t flags
Definition: liblwgeom.h:371
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:139
double z
Definition: liblwgeom.h:354
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:113
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:328
double y
Definition: liblwgeom.h:354
uint32_t npoints
Definition: liblwgeom.h:373
Here is the call graph for this function:
Here is the caller graph for this function: