PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ lw_dist3d_seg_seg()

int lw_dist3d_seg_seg ( POINT3DZ s1p1,
POINT3DZ s1p2,
POINT3DZ s2p1,
POINT3DZ s2p2,
DISTPTS3D dl 
)

Finds the two closest points on two linesegments.

Definition at line 918 of file measures3d.c.

919 {
920  VECTOR3D v1, v2, vl;
921  double s1k, s2k; /*two variables representing where on Line 1 (s1k) and where on Line 2 (s2k) a connecting line between the two lines is perpendicular to both lines*/
922  POINT3DZ p1, p2;
923  double a, b, c, d, e, D;
924 
925  /*s1p1 and s1p2 are the same point */
926  if ( ( s1p1->x == s1p2->x) && (s1p1->y == s1p2->y) && (s1p1->z == s1p2->z) )
927  {
928  return lw_dist3d_pt_seg(s1p1,s2p1,s2p2,dl);
929  }
930  /*s2p1 and s2p2 are the same point */
931  if ( ( s2p1->x == s2p2->x) && (s2p1->y == s2p2->y) && (s2p1->z == s2p2->z) )
932  {
933  dl->twisted= ((dl->twisted) * (-1));
934  return lw_dist3d_pt_seg(s2p1,s1p1,s1p2,dl);
935  }
936 
937 /*
938  Here we use algorithm from softsurfer.com
939  that can be found here
940  http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
941 */
942 
943  if (!get_3dvector_from_points(s1p1, s1p2, &v1))
944  return LW_FALSE;
945 
946  if (!get_3dvector_from_points(s2p1, s2p2, &v2))
947  return LW_FALSE;
948 
949  if (!get_3dvector_from_points(s2p1, s1p1, &vl))
950  return LW_FALSE;
951 
952  a = DOT(v1,v1);
953  b = DOT(v1,v2);
954  c = DOT(v2,v2);
955  d = DOT(v1,vl);
956  e = DOT(v2,vl);
957  D = a*c - b*b;
958 
959 
960  if (D <0.000000001)
961  { /* the lines are almost parallel*/
962  s1k = 0.0; /*If the lines are parallel we try by using the startpoint of first segment. If that gives a projected point on the second line outside segment 2 it wil be found that s2k is >1 or <0.*/
963  if(b>c) /* use the largest denominator*/
964  {
965  s2k=d/b;
966  }
967  else
968  {
969  s2k =e/c;
970  }
971  }
972  else
973  {
974  s1k = (b*e - c*d) / D;
975  s2k = (a*e - b*d) / D;
976  }
977 
978  /* Now we check if the projected closest point on the infinite lines is outside our segments. If so the combinations with start and end points will be tested*/
979  if(s1k<0.0||s1k>1.0||s2k<0.0||s2k>1.0)
980  {
981  if(s1k<0.0)
982  {
983 
984  if (!lw_dist3d_pt_seg(s1p1, s2p1, s2p2, dl))
985  {
986  return LW_FALSE;
987  }
988  }
989  if(s1k>1.0)
990  {
991 
992  if (!lw_dist3d_pt_seg(s1p2, s2p1, s2p2, dl))
993  {
994  return LW_FALSE;
995  }
996  }
997  if(s2k<0.0)
998  {
999  dl->twisted= ((dl->twisted) * (-1));
1000  if (!lw_dist3d_pt_seg(s2p1, s1p1, s1p2, dl))
1001  {
1002  return LW_FALSE;
1003  }
1004  }
1005  if(s2k>1.0)
1006  {
1007  dl->twisted= ((dl->twisted) * (-1));
1008  if (!lw_dist3d_pt_seg(s2p2, s1p1, s1p2, dl))
1009  {
1010  return LW_FALSE;
1011  }
1012  }
1013  }
1014  else
1015  {/*Find the closest point on the edges of both segments*/
1016  p1.x=s1p1->x+s1k*(s1p2->x-s1p1->x);
1017  p1.y=s1p1->y+s1k*(s1p2->y-s1p1->y);
1018  p1.z=s1p1->z+s1k*(s1p2->z-s1p1->z);
1019 
1020  p2.x=s2p1->x+s2k*(s2p2->x-s2p1->x);
1021  p2.y=s2p1->y+s2k*(s2p2->y-s2p1->y);
1022  p2.z=s2p1->z+s2k*(s2p2->z-s2p1->z);
1023 
1024  if (!lw_dist3d_pt_pt(&p1,&p2,dl))/* Send the closest points to point-point calculation*/
1025  {
1026  return LW_FALSE;
1027  }
1028  }
1029  return LW_TRUE;
1030 }
#define LW_FALSE
Definition: liblwgeom.h:77
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
static int get_3dvector_from_points(POINT3DZ *p1, POINT3DZ *p2, VECTOR3D *v)
Definition: measures3d.c:45
int lw_dist3d_pt_pt(POINT3DZ *thep1, POINT3DZ *thep2, DISTPTS3D *dl)
Compares incoming points and stores the points closest to each other or most far away from each other...
Definition: measures3d.c:832
int lw_dist3d_pt_seg(POINT3DZ *p, POINT3DZ *A, POINT3DZ *B, DISTPTS3D *dl)
If searching for min distance, this one finds the closest point on segment A-B from p.
Definition: measures3d.c:767
#define DOT(u, v)
Definition: measures3d.h:31
int twisted
Definition: measures3d.h:45
double z
Definition: liblwgeom.h:337
double x
Definition: liblwgeom.h:337
double y
Definition: liblwgeom.h:337

References DOT, get_3dvector_from_points(), lw_dist3d_pt_pt(), lw_dist3d_pt_seg(), LW_FALSE, LW_TRUE, DISTPTS3D::twisted, POINT3DZ::x, POINT3DZ::y, and POINT3DZ::z.

Referenced by lw_dist3d_ptarray_ptarray().

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