GteDistLineRay.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 #include <Mathematics/GteRay.h>
13 
14 namespace gte
15 {
16 
17 template <int N, typename Real>
18 class DCPQuery<Real, Line<N, Real>, Ray<N, Real>>
19 {
20 public:
21  struct Result
22  {
24  Real parameter[2];
25  Vector<N, Real> closestPoint[2];
26  };
27 
28  Result operator()(Line<N, Real> const& line, Ray<N, Real> const& ray);
29 };
30 
31 // Template aliases for convenience.
32 template <int N, typename Real>
34 
35 template <typename Real>
37 
38 template <typename Real>
40 
41 
42 template <int N, typename Real>
43 typename DCPQuery<Real, Line<N, Real>, Ray<N, Real>>::Result
44 DCPQuery<Real, Line<N, Real>, Ray<N, Real>>::operator()(
45  Line<N, Real> const& line, Ray<N, Real> const& ray)
46 {
47  Result result;
48 
49  Vector<N, Real> diff = line.origin - ray.origin;
50  Real a01 = -Dot(line.direction, ray.direction);
51  Real b0 = Dot(diff, line.direction);
52  Real s0, s1;
53 
54  if (std::abs(a01) < (Real)1)
55  {
56  Real b1 = -Dot(diff, ray.direction);
57  s1 = a01 * b0 - b1;
58 
59  if (s1 >= (Real)0)
60  {
61  // Two interior points are closest, one on line and one on ray.
62  Real det = (Real)1 - a01 * a01;
63  s0 = (a01 * b1 - b0) / det;
64  s1 /= det;
65  }
66  else
67  {
68  // Origin of ray and interior point of line are closest.
69  s0 = -b0;
70  s1 = (Real)0;
71  }
72  }
73  else
74  {
75  // Lines are parallel, closest pair with one point at ray origin.
76  s0 = -b0;
77  s1 = (Real)0;
78  }
79 
80  result.parameter[0] = s0;
81  result.parameter[1] = s1;
82  result.closestPoint[0] = line.origin + s0 * line.direction;
83  result.closestPoint[1] = ray.origin + s1 * ray.direction;
84  diff = result.closestPoint[0] - result.closestPoint[1];
85  result.sqrDistance = Dot(diff, diff);
86  result.distance = sqrt(result.sqrDistance);
87  return result;
88 }
89 
90 
91 }
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