PostGIS  2.2.7dev-r@@SVN_REVISION@@
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 874 of file measures3d.c.

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

Referenced by lw_dist3d_ptarray_ptarray().

875 {
876  VECTOR3D v1, v2, vl;
877  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*/
878  POINT3DZ p1, p2;
879  double a, b, c, d, e, D;
880 
881  /*s1p1 and s1p2 are the same point */
882  if ( ( s1p1->x == s1p2->x) && (s1p1->y == s1p2->y) && (s1p1->z == s1p2->z) )
883  {
884  return lw_dist3d_pt_seg(s1p1,s2p1,s2p2,dl);
885  }
886  /*s2p1 and s2p2 are the same point */
887  if ( ( s2p1->x == s2p2->x) && (s2p1->y == s2p2->y) && (s2p1->z == s2p2->z) )
888  {
889  dl->twisted= ((dl->twisted) * (-1));
890  return lw_dist3d_pt_seg(s2p1,s1p1,s1p2,dl);
891  }
892 
893 /*
894  Here we use algorithm from softsurfer.com
895  that can be found here
896  http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
897 */
898 
899  if (!get_3dvector_from_points(s1p1, s1p2, &v1))
900  return LW_FALSE;
901 
902  if (!get_3dvector_from_points(s2p1, s2p2, &v2))
903  return LW_FALSE;
904 
905  if (!get_3dvector_from_points(s2p1, s1p1, &vl))
906  return LW_FALSE;
907 
908  a = DOT(v1,v1);
909  b = DOT(v1,v2);
910  c = DOT(v2,v2);
911  d = DOT(v1,vl);
912  e = DOT(v2,vl);
913  D = a*c - b*b;
914 
915 
916  if (D <0.000000001)
917  { /* the lines are almost parallel*/
918  s1k = 0.0; /*If the lines are paralell 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.*/
919  if(b>c) /* use the largest denominator*/
920  {
921  s2k=d/b;
922  }
923  else
924  {
925  s2k =e/c;
926  }
927  }
928  else
929  {
930  s1k = (b*e - c*d) / D;
931  s2k = (a*e - b*d) / D;
932  }
933 
934  /* 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*/
935  if(s1k<0.0||s1k>1.0||s2k<0.0||s2k>1.0)
936  {
937  if(s1k<0.0)
938  {
939 
940  if (!lw_dist3d_pt_seg(s1p1, s2p1, s2p2, dl))
941  {
942  return LW_FALSE;
943  }
944  }
945  if(s1k>1.0)
946  {
947 
948  if (!lw_dist3d_pt_seg(s1p2, s2p1, s2p2, dl))
949  {
950  return LW_FALSE;
951  }
952  }
953  if(s2k<0.0)
954  {
955  dl->twisted= ((dl->twisted) * (-1));
956  if (!lw_dist3d_pt_seg(s2p1, s1p1, s1p2, dl))
957  {
958  return LW_FALSE;
959  }
960  }
961  if(s2k>1.0)
962  {
963  dl->twisted= ((dl->twisted) * (-1));
964  if (!lw_dist3d_pt_seg(s2p2, s1p1, s1p2, dl))
965  {
966  return LW_FALSE;
967  }
968  }
969  }
970  else
971  {/*Find the closest point on the edges of both segments*/
972  p1.x=s1p1->x+s1k*(s1p2->x-s1p1->x);
973  p1.y=s1p1->y+s1k*(s1p2->y-s1p1->y);
974  p1.z=s1p1->z+s1k*(s1p2->z-s1p1->z);
975 
976  p2.x=s2p1->x+s2k*(s2p2->x-s2p1->x);
977  p2.y=s2p1->y+s2k*(s2p2->y-s2p1->y);
978  p2.z=s2p1->z+s2k*(s2p2->z-s2p1->z);
979 
980  if (!lw_dist3d_pt_pt(&p1,&p2,dl))/* Send the closest points to point-point calculation*/
981  {
982  return LW_FALSE;
983  }
984  }
985  return LW_TRUE;
986 }
double z
Definition: liblwgeom.h:318
double y
Definition: liblwgeom.h:318
double x
Definition: liblwgeom.h:318
#define DOT(u, v)
Definition: measures3d.h:17
#define LW_FALSE
Definition: liblwgeom.h:62
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
int lw_dist3d_pt_pt(POINT3DZ *thep1, POINT3DZ *thep2, DISTPTS3D *dl)
Compares incomming points and stores the points closest to each other or most far away from each othe...
Definition: measures3d.c:788
int twisted
Definition: measures3d.h:31
static int get_3dvector_from_points(POINT3DZ *p1, POINT3DZ *p2, VECTOR3D *v)
Definition: measures3d.c:21
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:723

Here is the call graph for this function:

Here is the caller graph for this function: