PostGIS  3.7.0dev-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 proportionally.

Definition at line 413 of file ptarray.c.

414 {
415  double segdist;
416  POINT4D p1, p2;
417  POINT4D pbuf;
418  POINTARRAY *opa;
419  uint32_t i, j, nseg;
420  int hasz = FLAGS_GET_Z(ipa->flags);
421  int hasm = FLAGS_GET_M(ipa->flags);
422 
423  pbuf.x = pbuf.y = pbuf.z = pbuf.m = 0;
424 
425  /* Initial storage */
426  opa = ptarray_construct_empty(hasz, hasm, ipa->npoints);
427 
428  /* Add first point */
429  getPoint4d_p(ipa, 0, &p1);
430  ptarray_append_point(opa, &p1, LW_FALSE);
431 
432  /* Loop on all other input points */
433  for (i = 1; i < ipa->npoints; i++)
434  {
435  /*
436  * We use these pointers to avoid
437  * "strict-aliasing rules break" warning raised
438  * by gcc (3.3 and up).
439  *
440  * It looks that casting a variable address (also
441  * referred to as "type-punned pointer")
442  * breaks those "strict" rules.
443  */
444  POINT4D *p1ptr=&p1, *p2ptr=&p2;
445  double segments;
446 
447  getPoint4d_p(ipa, i, &p2);
448 
449  segdist = distance2d_pt_pt((POINT2D *)p1ptr, (POINT2D *)p2ptr);
450  /* Split input segment into shorter even chunks */
451  segments = ceil(segdist / dist);
452 
453  /* Uses INT32_MAX instead of UINT32_MAX to be safe that it fits */
454  if (segments >= INT32_MAX)
455  {
456  lwnotice("%s:%d - %s: Too many segments required (%e)",
457  __FILE__, __LINE__,__func__, segments);
458  ptarray_free(opa);
459  return NULL;
460  }
461  nseg = segments;
462 
463  for (j = 1; j < nseg; j++)
464  {
465  pbuf.x = p1.x + (p2.x - p1.x) * j / nseg;
466  pbuf.y = p1.y + (p2.y - p1.y) * j / nseg;
467  if (hasz)
468  pbuf.z = p1.z + (p2.z - p1.z) * j / nseg;
469  if (hasm)
470  pbuf.m = p1.m + (p2.m - p1.m) * j / nseg;
471  ptarray_append_point(opa, &pbuf, LW_FALSE);
472  LW_ON_INTERRUPT(ptarray_free(opa); return NULL);
473  }
474 
475  ptarray_append_point(opa, &p2, (ipa->npoints == 2) ? LW_TRUE : LW_FALSE);
476  p1 = p2;
477  LW_ON_INTERRUPT(ptarray_free(opa); return NULL);
478  }
479 
480  return opa;
481 }
#define LW_FALSE
Definition: liblwgeom.h:94
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2445
#define FLAGS_GET_Z(flags)
Definition: liblwgeom.h:165
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:166
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
#define LW_ON_INTERRUPT(x)
void lwnotice(const char *fmt,...) __attribute__((format(printf
Write a notice out to the notice handler.
#define INT32_MAX
Definition: lwin_wkt_lex.c:334
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:59
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:327
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,...
Definition: ptarray.c:147
double m
Definition: liblwgeom.h:414
double x
Definition: liblwgeom.h:414
double z
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414
lwflags_t flags
Definition: liblwgeom.h:431
uint32_t npoints
Definition: liblwgeom.h:427

References distance2d_pt_pt(), POINTARRAY::flags, FLAGS_GET_M, FLAGS_GET_Z, getPoint4d_p(), INT32_MAX, LW_FALSE, LW_ON_INTERRUPT, LW_TRUE, lwnotice(), 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().

Here is the call graph for this function:
Here is the caller graph for this function: