PostGIS  2.1.10dev-r@@SVN_REVISION@@
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 684 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().

685 {
686  VECTOR3D v1, v2, vl;
687  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*/
688  POINT3DZ p1, p2;
689  double a, b, c, d, e, D;
690 
691  /*s1p1 and s1p2 are the same point */
692  if ( ( s1p1->x == s1p2->x) && (s1p1->y == s1p2->y) && (s1p1->z == s1p2->z) )
693  {
694  return lw_dist3d_pt_seg(s1p1,s2p1,s2p2,dl);
695  }
696  /*s2p1 and s2p2 are the same point */
697  if ( ( s2p1->x == s2p2->x) && (s2p1->y == s2p2->y) && (s2p1->z == s2p2->z) )
698  {
699  dl->twisted= ((dl->twisted) * (-1));
700  return lw_dist3d_pt_seg(s2p1,s1p1,s1p2,dl);
701  }
702 
703 /*
704  Here we use algorithm from softsurfer.com
705  that can be found here
706  http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
707 */
708 
709  if (!get_3dvector_from_points(s1p1, s1p2, &v1))
710  return LW_FALSE;
711 
712  if (!get_3dvector_from_points(s2p1, s2p2, &v2))
713  return LW_FALSE;
714 
715  if (!get_3dvector_from_points(s2p1, s1p1, &vl))
716  return LW_FALSE;
717 
718  a = DOT(v1,v1);
719  b = DOT(v1,v2);
720  c = DOT(v2,v2);
721  d = DOT(v1,vl);
722  e = DOT(v2,vl);
723  D = a*c - b*b;
724 
725 
726  if (D <0.000000001)
727  { /* the lines are almost parallel*/
728  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.*/
729  if(b>c) /* use the largest denominator*/
730  {
731  s2k=d/b;
732  }
733  else
734  {
735  s2k =e/c;
736  }
737  }
738  else
739  {
740  s1k = (b*e - c*d) / D;
741  s2k = (a*e - b*d) / D;
742  }
743 
744  /* 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*/
745  if(s1k<0.0||s1k>1.0||s2k<0.0||s2k>1.0)
746  {
747  if(s1k<0.0)
748  {
749 
750  if (!lw_dist3d_pt_seg(s1p1, s2p1, s2p2, dl))
751  {
752  return LW_FALSE;
753  }
754  }
755  if(s1k>1.0)
756  {
757 
758  if (!lw_dist3d_pt_seg(s1p2, s2p1, s2p2, dl))
759  {
760  return LW_FALSE;
761  }
762  }
763  if(s2k<0.0)
764  {
765  dl->twisted= ((dl->twisted) * (-1));
766  if (!lw_dist3d_pt_seg(s2p1, s1p1, s1p2, dl))
767  {
768  return LW_FALSE;
769  }
770  }
771  if(s2k>1.0)
772  {
773  dl->twisted= ((dl->twisted) * (-1));
774  if (!lw_dist3d_pt_seg(s2p2, s1p1, s1p2, dl))
775  {
776  return LW_FALSE;
777  }
778  }
779  }
780  else
781  {/*Find the closest point on the edges of both segments*/
782  p1.x=s1p1->x+s1k*(s1p2->x-s1p1->x);
783  p1.y=s1p1->y+s1k*(s1p2->y-s1p1->y);
784  p1.z=s1p1->z+s1k*(s1p2->z-s1p1->z);
785 
786  p2.x=s2p1->x+s2k*(s2p2->x-s2p1->x);
787  p2.y=s2p1->y+s2k*(s2p2->y-s2p1->y);
788  p2.z=s2p1->z+s2k*(s2p2->z-s2p1->z);
789 
790  if (!lw_dist3d_pt_pt(&p1,&p2,dl))/* Send the closest points to point-point calculation*/
791  {
792  return LW_FALSE;
793  }
794  }
795  return LW_TRUE;
796 }
double z
Definition: liblwgeom.h:290
double y
Definition: liblwgeom.h:290
double x
Definition: liblwgeom.h:290
#define DOT(u, v)
Definition: measures3d.h:16
#define LW_FALSE
Definition: liblwgeom.h:52
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
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:598
int twisted
Definition: measures3d.h:30
int get_3dvector_from_points(POINT3DZ *p1, POINT3DZ *p2, VECTOR3D *v)
Definition: measures3d.h:85
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:542

Here is the call graph for this function:

Here is the caller graph for this function: