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


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