GteIntrLine3Sphere3.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 
10 #include <Mathematics/GteVector3.h>
12 #include <Mathematics/GteLine.h>
13 #include <Mathematics/GteFIQuery.h>
14 #include <Mathematics/GteTIQuery.h>
15 
16 namespace gte
17 {
18 
19 template <typename Real>
20 class TIQuery<Real, Line3<Real>, Sphere3<Real>>
21 {
22 public:
23  struct Result
24  {
25  bool intersect;
26  };
27 
28  Result operator()(Line3<Real> const& line, Sphere3<Real> const& sphere);
29 };
30 
31 template <typename Real>
32 class FIQuery<Real, Line3<Real>, Sphere3<Real>>
33 {
34 public:
35  struct Result
36  {
37  bool intersect;
39  std::array<Real, 2> parameter;
40  std::array<Vector3<Real>, 2> point;
41  };
42 
43  Result operator()(Line3<Real> const& line, Sphere3<Real> const& sphere);
44 
45 protected:
46  void DoQuery(Vector3<Real> const& lineOrigin,
47  Vector3<Real> const& lineDirection, Sphere3<Real> const& sphere,
48  Result& result);
49 };
50 
51 
52 template <typename Real>
55  Line3<Real> const& line, Sphere3<Real> const& sphere)
56 {
57  // The sphere is (X-C)^T*(X-C)-1 = 0 and the line is X = P+t*D.
58  // Substitute the line equation into the sphere equation to obtain a
59  // quadratic equation Q(t) = t^2 + 2*a1*t + a0 = 0, where a1 = D^T*(P-C),
60  // and a0 = (P-C)^T*(P-C)-1.
61  Result result;
62 
63  Vector3<Real> diff = line.origin - sphere.center;
64  Real a0 = Dot(diff, diff) - sphere.radius * sphere.radius;
65  Real a1 = Dot(line.direction, diff);
66 
67  // Intersection occurs when Q(t) has real roots.
68  Real discr = a1*a1 - a0;
69  result.intersect = (discr >= (Real)0);
70  return result;
71 }
72 
73 template <typename Real>
76  Line3<Real> const& line, Sphere3<Real> const& sphere)
77 {
78  Result result;
79  DoQuery(line.origin, line.direction, sphere, result);
80  for (int i = 0; i < result.numIntersections; ++i)
81  {
82  result.point[i] = line.origin + result.parameter[i] * line.direction;
83  }
84  return result;
85 }
86 
87 template <typename Real>
89  Vector3<Real> const& lineOrigin, Vector3<Real> const& lineDirection,
90  Sphere3<Real> const& sphere, Result& result)
91 {
92  // The sphere is (X-C)^T*(X-C)-1 = 0 and the line is X = P+t*D.
93  // Substitute the line equation into the sphere equation to obtain a
94  // quadratic equation Q(t) = t^2 + 2*a1*t + a0 = 0, where a1 = D^T*(P-C),
95  // and a0 = (P-C)^T*(P-C)-1.
96  Vector3<Real> diff = lineOrigin - sphere.center;
97  Real a0 = Dot(diff, diff) - sphere.radius * sphere.radius;
98  Real a1 = Dot(lineDirection, diff);
99 
100  // Intersection occurs when Q(t) has real roots.
101  Real discr = a1*a1 - a0;
102  if (discr > (Real)0)
103  {
104  result.intersect = true;
105  result.numIntersections = 2;
106  Real root = sqrt(discr);
107  result.parameter[0] = -a1 - root;
108  result.parameter[1] = -a1 + root;
109  }
110  else if (discr < (Real)0)
111  {
112  result.intersect = false;
113  result.numIntersections = 0;
114  }
115  else
116  {
117  result.intersect = true;
118  result.numIntersections = 1;
119  result.parameter[0] = -a1;
120  }
121 }
122 
123 
124 }
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
Result operator()(Type0 const &primitive0, Type1 const &primitive1)
Vector< N, Real > center
GLuint64EXT * result
Definition: glext.h:10003


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:00