PostGIS  3.0.6dev-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 1139 of file measures3d.c.

1140 {
1141  VECTOR3D v1, v2, vl;
1142  double s1k, s2k; /*two variables representing where on Line 1 (s1k) and where on Line 2 (s2k) a connecting line
1143  between the two lines is perpendicular to both lines*/
1144  POINT3DZ p1, p2;
1145  double a, b, c, d, e, D;
1146 
1147  /*s1p1 and s1p2 are the same point */
1148  if ((s1p1->x == s1p2->x) && (s1p1->y == s1p2->y) && (s1p1->z == s1p2->z))
1149  {
1150  return lw_dist3d_pt_seg(s1p1, s2p1, s2p2, dl);
1151  }
1152  /*s2p1 and s2p2 are the same point */
1153  if ((s2p1->x == s2p2->x) && (s2p1->y == s2p2->y) && (s2p1->z == s2p2->z))
1154  {
1155  dl->twisted = ((dl->twisted) * (-1));
1156  return lw_dist3d_pt_seg(s2p1, s1p1, s1p2, dl);
1157  }
1158 
1159  /*
1160  Here we use algorithm from softsurfer.com
1161  that can be found here
1162  http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
1163  */
1164 
1165  if (!get_3dvector_from_points(s1p1, s1p2, &v1))
1166  return LW_FALSE;
1167 
1168  if (!get_3dvector_from_points(s2p1, s2p2, &v2))
1169  return LW_FALSE;
1170 
1171  if (!get_3dvector_from_points(s2p1, s1p1, &vl))
1172  return LW_FALSE;
1173 
1174  a = DOT(v1, v1);
1175  b = DOT(v1, v2);
1176  c = DOT(v2, v2);
1177  d = DOT(v1, vl);
1178  e = DOT(v2, vl);
1179  D = a * c - b * b;
1180 
1181  if (D < 0.000000001)
1182  { /* the lines are almost parallel*/
1183  s1k =
1184  0.0; /*If the lines are parallel we try by using the startpoint of first segment. If that gives a
1185  projected point on the second line outside segment 2 it wil be found that s2k is >1 or <0.*/
1186  if (b > c) /* use the largest denominator*/
1187  s2k = d / b;
1188  else
1189  s2k = e / c;
1190  }
1191  else
1192  {
1193  s1k = (b * e - c * d) / D;
1194  s2k = (a * e - b * d) / D;
1195  }
1196 
1197  /* Now we check if the projected closest point on the infinite lines is outside our segments. If so the
1198  * combinations with start and end points will be tested*/
1199 
1200  if (s1k <= 0.0 || s1k >= 1.0 || s2k <= 0.0 || s2k >= 1.0)
1201  {
1202  if (s1k <= 0.0)
1203  {
1204  if (!lw_dist3d_pt_seg(s1p1, s2p1, s2p2, dl))
1205  return LW_FALSE;
1206  }
1207  if (s1k >= 1.0)
1208  {
1209  if (!lw_dist3d_pt_seg(s1p2, s2p1, s2p2, dl))
1210  return LW_FALSE;
1211  }
1212  if (s2k <= 0.0)
1213  {
1214  dl->twisted = ((dl->twisted) * (-1));
1215  if (!lw_dist3d_pt_seg(s2p1, s1p1, s1p2, dl))
1216  return LW_FALSE;
1217  }
1218  if (s2k >= 1.0)
1219  {
1220  dl->twisted = ((dl->twisted) * (-1));
1221  if (!lw_dist3d_pt_seg(s2p2, s1p1, s1p2, dl))
1222  return LW_FALSE;
1223  }
1224  }
1225  else
1226  { /*Find the closest point on the edges of both segments*/
1227  p1.x = s1p1->x + s1k * (s1p2->x - s1p1->x);
1228  p1.y = s1p1->y + s1k * (s1p2->y - s1p1->y);
1229  p1.z = s1p1->z + s1k * (s1p2->z - s1p1->z);
1230 
1231  p2.x = s2p1->x + s2k * (s2p2->x - s2p1->x);
1232  p2.y = s2p1->y + s2k * (s2p2->y - s2p1->y);
1233  p2.z = s2p1->z + s2k * (s2p2->z - s2p1->z);
1234 
1235  if (!lw_dist3d_pt_pt(&p1, &p2, dl)) /* Send the closest points to point-point calculation*/
1236  {
1237  return LW_FALSE;
1238  }
1239  }
1240  return LW_TRUE;
1241 }
#define LW_FALSE
Definition: liblwgeom.h:108
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
static int get_3dvector_from_points(POINT3DZ *p1, POINT3DZ *p2, VECTOR3D *v)
Definition: measures3d.c:34
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:1048
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:992
#define DOT(u, v)
Definition: measures3d.h:31
int twisted
Definition: measures3d.h:45
double z
Definition: liblwgeom.h:382
double x
Definition: liblwgeom.h:382
double y
Definition: liblwgeom.h:382

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: