PostGIS  3.7.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 615 of file lwline.c.

616 {
617  double length, slength, tlength;
618  POINTARRAY *ipa;
619  POINT4D pt;
620  int nsegs, i;
621  LWGEOM *geom = lwline_as_lwgeom(line);
622  int has_z = lwgeom_has_z(geom);
623  int has_m = lwgeom_has_m(geom);
624  ipa = line->points;
625 
626  /* Empty.InterpolatePoint == Point Empty */
627  if (lwline_is_empty(line))
628  {
629  return lwpoint_construct_empty(line->srid, has_z, has_m);
630  }
631 
632  /* If distance is one of the two extremes, return the point on that
633  * end rather than doing any expensive computations
634  */
635  if (distance == 0.0 || distance == 1.0)
636  {
637  if (distance == 0.0)
638  getPoint4d_p(ipa, 0, &pt);
639  else
640  getPoint4d_p(ipa, ipa->npoints - 1, &pt);
641 
642  return lwpoint_make(line->srid, has_z, has_m, &pt);
643  }
644 
645  /* Interpolate a point on the line */
646  nsegs = ipa->npoints - 1;
647  length = ptarray_length(ipa);
648  tlength = 0;
649  for (i = 0; i < nsegs; i++)
650  {
651  POINT4D p1, p2;
652  POINT4D *p1ptr = &p1, *p2ptr = &p2; /* don't break
653  * strict-aliasing rules
654  */
655 
656  getPoint4d_p(ipa, i, &p1);
657  getPoint4d_p(ipa, i + 1, &p2);
658 
659  /* Find the relative length of this segment */
660  slength = distance3d_pt_pt((POINT3D *)p1ptr, (POINT3D *)p2ptr) / length;
661 
662  /* If our target distance is before the total length we've seen
663  * so far. create a new point some distance down the current
664  * segment.
665  */
666  if (distance < tlength + slength)
667  {
668  double dseg = (distance - tlength) / slength;
669  interpolate_point4d(&p1, &p2, &pt, dseg);
670  return lwpoint_make(line->srid, has_z, has_m, &pt);
671  }
672  tlength += slength;
673  }
674 
675  /* Return the last point on the line. This shouldn't happen, but
676  * could if there's some floating point rounding errors. */
677  getPoint4d_p(ipa, ipa->npoints - 1, &pt);
678  return lwpoint_make(line->srid, has_z, has_m, &pt);
679 }
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:367
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:646
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:962
double distance3d_pt_pt(const POINT3D *p1, const POINT3D *p2)
Definition: measures3d.c:1066
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:969
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:2003
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: