PostGIS  2.3.8dev-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 898 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().

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