GteDistLineLine.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
11 #include <Mathematics/GteLine.h>
12 
13 namespace gte
14 {
15 
16 template <int N, typename Real>
17 class DCPQuery<Real, Line<N, Real>, Line<N, Real>>
18 {
19 public:
20  struct Result
21  {
23  Real parameter[2];
24  Vector<N, Real> closestPoint[2];
25  };
26 
27  Result operator()(Line<N, Real> const& line0, Line<N, Real> const& line1);
28 };
29 
30 // Template aliases for convenience.
31 template <int N, typename Real>
33 
34 template <typename Real>
36 
37 template <typename Real>
39 
40 
41 template <int N, typename Real>
42 typename DCPQuery<Real, Line<N, Real>, Line<N, Real>>::Result
43 DCPQuery<Real, Line<N, Real>, Line<N, Real>>::operator()(
44  Line<N, Real> const& line0, Line<N, Real> const& line1)
45 {
46  Result result;
47 
48  Vector<N, Real> diff = line0.origin - line1.origin;
49  Real a01 = -Dot(line0.direction, line1.direction);
50  Real b0 = Dot(diff, line0.direction);
51  Real s0, s1;
52 
53  if (std::abs(a01) < (Real)1)
54  {
55  // Lines are not parallel.
56  Real det = (Real)1 - a01 * a01;
57  Real b1 = -Dot(diff, line1.direction);
58  s0 = (a01 * b1 - b0) / det;
59  s1 = (a01 * b0 - b1) / det;
60  }
61  else
62  {
63  // Lines are parallel, select any pair of closest points.
64  s0 = -b0;
65  s1 = (Real)0;
66  }
67 
68  result.parameter[0] = s0;
69  result.parameter[1] = s1;
70  result.closestPoint[0] = line0.origin + s0 * line0.direction;
71  result.closestPoint[1] = line1.origin + s1 * line1.direction;
72  diff = result.closestPoint[0] - result.closestPoint[1];
73  result.sqrDistance = Dot(diff, diff);
74  result.distance = sqrt(result.sqrDistance);
75  return result;
76 }
77 
78 
79 }
gte::BSNumber< UIntegerType > abs(gte::BSNumber< UIntegerType > const &number)
Definition: GteBSNumber.h:966
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s0
Definition: glext.h:9013
GLsizei GLsizei GLfloat distance
Definition: glext.h:9704
Result operator()(Type0 const &primitive0, Type1 const &primitive1)
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
Definition: glext.h:9013
GLuint64EXT * result
Definition: glext.h:10003


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59