GteDistRayRay.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/GteRay.h>
12 
13 namespace gte
14 {
15 
16 template <int N, typename Real>
17 class DCPQuery<Real, Ray<N, Real>, Ray<N, Real>>
18 {
19 public:
20  struct Result
21  {
23  Real parameter[2];
24  Vector<N, Real> closestPoint[2];
25  };
26 
27  Result operator()(Ray<N, Real> const& ray0, Ray<N, Real> const& ray1);
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, Ray<N, Real>, Ray<N, Real>>::Result
43 DCPQuery<Real, Ray<N, Real>, Ray<N, Real>>::operator()(
44  Ray<N, Real> const& ray0, Ray<N, Real> const& ray1)
45 {
46  Result result;
47 
48  Vector<N, Real> diff = ray0.origin - ray1.origin;
49  Real a01 = -Dot(ray0.direction, ray1.direction);
50  Real b0 = Dot(diff, ray0.direction), b1;
51  Real s0, s1;
52 
53  if (std::abs(a01) < (Real)1)
54  {
55  // Rays are not parallel.
56  b1 = -Dot(diff, ray1.direction);
57  s0 = a01 * b1 - b0;
58  s1 = a01 * b0 - b1;
59 
60  if (s0 >= (Real)0)
61  {
62  if (s1 >= (Real)0) // region 0 (interior)
63  {
64  // Minimum at two interior points of rays.
65  Real det = (Real)1 - a01 * a01;
66  s0 /= det;
67  s1 /= det;
68  }
69  else // region 3 (side)
70  {
71  s1 = (Real)0;
72  if (b0 >= (Real)0)
73  {
74  s0 = (Real)0;
75  }
76  else
77  {
78  s0 = -b0;
79  }
80  }
81  }
82  else
83  {
84  if (s1 >= (Real)0) // region 1 (side)
85  {
86  s0 = (Real)0;
87  if (b1 >= (Real)0)
88  {
89  s1 = (Real)0;
90  }
91  else
92  {
93  s1 = -b1;
94  }
95  }
96  else // region 2 (corner)
97  {
98  if (b0 < (Real)0)
99  {
100  s0 = -b0;
101  s1 = (Real)0;
102  }
103  else
104  {
105  s0 = (Real)0;
106  if (b1 >= (Real)0)
107  {
108  s1 = (Real)0;
109  }
110  else
111  {
112  s1 = -b1;
113  }
114  }
115  }
116  }
117  }
118  else
119  {
120  // Rays are parallel.
121  if (a01 > (Real)0)
122  {
123  // Opposite direction vectors.
124  s1 = (Real)0;
125  if (b0 >= (Real)0)
126  {
127  s0 = (Real)0;
128  }
129  else
130  {
131  s0 = -b0;
132  }
133  }
134  else
135  {
136  // Same direction vectors.
137  if (b0 >= (Real)0)
138  {
139  b1 = -Dot(diff, ray1.direction);
140  s0 = (Real)0;
141  s1 = -b1;
142  }
143  else
144  {
145  s0 = -b0;
146  s1 = (Real)0;
147  }
148  }
149  }
150 
151  result.parameter[0] = s0;
152  result.parameter[1] = s1;
153  result.closestPoint[0] = ray0.origin + s0 * ray0.direction;
154  result.closestPoint[1] = ray1.origin + s1 * ray1.direction;
155  diff = result.closestPoint[0] - result.closestPoint[1];
156  result.sqrDistance = Dot(diff, diff);
157  result.distance = sqrt(result.sqrDistance);
158  return result;
159 }
160 
161 
162 }
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