PostGIS  2.5.0beta1dev-r@@SVN_REVISION@@

◆ lw_dist3d_seg_seg()

int lw_dist3d_seg_seg ( POINT3DZ A,
POINT3DZ B,
POINT3DZ C,
POINT3DZ D,
DISTPTS3D dl 
)

Finds the two closest points on two linesegments.

Definition at line 895 of file measures3d.c.

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().

896 {
897  VECTOR3D v1, v2, vl;
898  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*/
899  POINT3DZ p1, p2;
900  double a, b, c, d, e, D;
901 
902  /*s1p1 and s1p2 are the same point */
903  if ( ( s1p1->x == s1p2->x) && (s1p1->y == s1p2->y) && (s1p1->z == s1p2->z) )
904  {
905  return lw_dist3d_pt_seg(s1p1,s2p1,s2p2,dl);
906  }
907  /*s2p1 and s2p2 are the same point */
908  if ( ( s2p1->x == s2p2->x) && (s2p1->y == s2p2->y) && (s2p1->z == s2p2->z) )
909  {
910  dl->twisted= ((dl->twisted) * (-1));
911  return lw_dist3d_pt_seg(s2p1,s1p1,s1p2,dl);
912  }
913 
914 /*
915  Here we use algorithm from softsurfer.com
916  that can be found here
917  http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
918 */
919 
920  if (!get_3dvector_from_points(s1p1, s1p2, &v1))
921  return LW_FALSE;
922 
923  if (!get_3dvector_from_points(s2p1, s2p2, &v2))
924  return LW_FALSE;
925 
926  if (!get_3dvector_from_points(s2p1, s1p1, &vl))
927  return LW_FALSE;
928 
929  a = DOT(v1,v1);
930  b = DOT(v1,v2);
931  c = DOT(v2,v2);
932  d = DOT(v1,vl);
933  e = DOT(v2,vl);
934  D = a*c - b*b;
935 
936 
937  if (D <0.000000001)
938  { /* the lines are almost parallel*/
939  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.*/
940  if(b>c) /* use the largest denominator*/
941  {
942  s2k=d/b;
943  }
944  else
945  {
946  s2k =e/c;
947  }
948  }
949  else
950  {
951  s1k = (b*e - c*d) / D;
952  s2k = (a*e - b*d) / D;
953  }
954 
955  /* 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*/
956  if(s1k<0.0||s1k>1.0||s2k<0.0||s2k>1.0)
957  {
958  if(s1k<0.0)
959  {
960 
961  if (!lw_dist3d_pt_seg(s1p1, s2p1, s2p2, dl))
962  {
963  return LW_FALSE;
964  }
965  }
966  if(s1k>1.0)
967  {
968 
969  if (!lw_dist3d_pt_seg(s1p2, s2p1, s2p2, dl))
970  {
971  return LW_FALSE;
972  }
973  }
974  if(s2k<0.0)
975  {
976  dl->twisted= ((dl->twisted) * (-1));
977  if (!lw_dist3d_pt_seg(s2p1, s1p1, s1p2, dl))
978  {
979  return LW_FALSE;
980  }
981  }
982  if(s2k>1.0)
983  {
984  dl->twisted= ((dl->twisted) * (-1));
985  if (!lw_dist3d_pt_seg(s2p2, s1p1, s1p2, dl))
986  {
987  return LW_FALSE;
988  }
989  }
990  }
991  else
992  {/*Find the closest point on the edges of both segments*/
993  p1.x=s1p1->x+s1k*(s1p2->x-s1p1->x);
994  p1.y=s1p1->y+s1k*(s1p2->y-s1p1->y);
995  p1.z=s1p1->z+s1k*(s1p2->z-s1p1->z);
996 
997  p2.x=s2p1->x+s2k*(s2p2->x-s2p1->x);
998  p2.y=s2p1->y+s2k*(s2p2->y-s2p1->y);
999  p2.z=s2p1->z+s2k*(s2p2->z-s2p1->z);
1000 
1001  if (!lw_dist3d_pt_pt(&p1,&p2,dl))/* Send the closest points to point-point calculation*/
1002  {
1003  return LW_FALSE;
1004  }
1005  }
1006  return LW_TRUE;
1007 }
double z
Definition: liblwgeom.h:336
double y
Definition: liblwgeom.h:336
double x
Definition: liblwgeom.h:336
#define DOT(u, v)
Definition: measures3d.h:31
#define LW_FALSE
Definition: liblwgeom.h:76
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
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:809
int twisted
Definition: measures3d.h:45
static int get_3dvector_from_points(POINT3DZ *p1, POINT3DZ *p2, VECTOR3D *v)
Definition: measures3d.c:45
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:744
Here is the call graph for this function:
Here is the caller graph for this function: