PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ lwline_interpolate_point_3d()

LWPOINT* lwline_interpolate_point_3d ( const LWLINE line,
double  distance 
)

Interpolate one point along a line in 3D.

Definition at line 605 of file lwline.c.

606 {
607  double length, slength, tlength;
608  POINTARRAY *ipa;
609  POINT4D pt;
610  int nsegs, i;
611  LWGEOM *geom = lwline_as_lwgeom(line);
612  int has_z = lwgeom_has_z(geom);
613  int has_m = lwgeom_has_m(geom);
614  ipa = line->points;
615 
616  /* Empty.InterpolatePoint == Point Empty */
617  if (lwline_is_empty(line))
618  {
619  return lwpoint_construct_empty(line->srid, has_z, has_m);
620  }
621 
622  /* If distance is one of the two extremes, return the point on that
623  * end rather than doing any expensive computations
624  */
625  if (distance == 0.0 || distance == 1.0)
626  {
627  if (distance == 0.0)
628  getPoint4d_p(ipa, 0, &pt);
629  else
630  getPoint4d_p(ipa, ipa->npoints - 1, &pt);
631 
632  return lwpoint_make(line->srid, has_z, has_m, &pt);
633  }
634 
635  /* Interpolate a point on the line */
636  nsegs = ipa->npoints - 1;
637  length = ptarray_length(ipa);
638  tlength = 0;
639  for (i = 0; i < nsegs; i++)
640  {
641  POINT4D p1, p2;
642  POINT4D *p1ptr = &p1, *p2ptr = &p2; /* don't break
643  * strict-aliasing rules
644  */
645 
646  getPoint4d_p(ipa, i, &p1);
647  getPoint4d_p(ipa, i + 1, &p2);
648 
649  /* Find the relative length of this segment */
650  slength = distance3d_pt_pt((POINT3D *)p1ptr, (POINT3D *)p2ptr) / length;
651 
652  /* If our target distance is before the total length we've seen
653  * so far. create a new point some distance down the current
654  * segment.
655  */
656  if (distance < tlength + slength)
657  {
658  double dseg = (distance - tlength) / slength;
659  interpolate_point4d(&p1, &p2, &pt, dseg);
660  return lwpoint_make(line->srid, has_z, has_m, &pt);
661  }
662  tlength += slength;
663  }
664 
665  /* Return the last point on the line. This shouldn't happen, but
666  * could if there's some floating point rounding errors. */
667  getPoint4d_p(ipa, ipa->npoints - 1, &pt);
668  return lwpoint_make(line->srid, has_z, has_m, &pt);
669 }
LWPOINT * lwpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwpoint.c:151
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:339
void interpolate_point4d(const POINT4D *A, const POINT4D *B, POINT4D *I, double F)
Find interpolation point I between point A and point B so that the len(AI) == len(AB)*F and I falls o...
Definition: lwgeom_api.c:649
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
double distance3d_pt_pt(const POINT3D *p1, const POINT3D *p2)
Definition: measures3d.c:1029
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:941
LWPOINT * lwpoint_make(int32_t srid, int hasz, int hasm, const POINT4D *p)
Definition: lwpoint.c:206
int lwline_is_empty(const LWLINE *line)
double ptarray_length(const POINTARRAY *pts)
Find the 3d/2d length of the given POINTARRAY (depending on its dimensionality)
Definition: ptarray.c:1860
static double distance(double x1, double y1, double x2, double y2)
Definition: lwtree.c:1032
POINTARRAY * points
Definition: liblwgeom.h:483
int32_t srid
Definition: liblwgeom.h:484
uint32_t npoints
Definition: liblwgeom.h:427

References distance(), distance3d_pt_pt(), getPoint4d_p(), interpolate_point4d(), lwgeom_has_m(), lwgeom_has_z(), lwline_as_lwgeom(), lwline_is_empty(), lwpoint_construct_empty(), lwpoint_make(), POINTARRAY::npoints, LWLINE::points, ptarray_length(), and LWLINE::srid.

Referenced by ST_3DLineInterpolatePoint(), and test_lwline_interpolate_point_3d().

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