GteIntrSegment3Sphere3.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/GteSegment.h>
13 
14 namespace gte
15 {
16 
17 template <typename Real>
18 class TIQuery<Real, Segment3<Real>, Sphere3<Real>>
19 {
20 public:
21  struct Result
22  {
23  bool intersect;
24  };
25 
26  Result operator()(Segment3<Real> const& segment,
27  Sphere3<Real> const& sphere);
28 };
29 
30 template <typename Real>
31 class FIQuery<Real, Segment3<Real>, Sphere3<Real>>
32  :
33  public FIQuery<Real, Line3<Real>, Sphere3<Real>>
34 {
35 public:
36  struct Result
37  :
38  public FIQuery<Real, Line3<Real>, Sphere3<Real>>::Result
39  {
40  // No additional information to compute.
41  };
42 
43  Result operator()(Segment3<Real> const& segment,
44  Sphere3<Real> const& sphere);
45 
46 protected:
47  void DoQuery(Vector3<Real> const& segOrigin,
48  Vector3<Real> const& segDirection, Real segExtent,
49  Sphere3<Real> const& sphere, Result& result);
50 };
51 
52 
53 template <typename Real>
56  Segment3<Real> const& segment, Sphere3<Real> const& sphere)
57 {
58  // The sphere is (X-C)^T*(X-C)-1 = 0 and the line is X = P+t*D.
59  // Substitute the line equation into the sphere equation to obtain a
60  // quadratic equation Q(t) = t^2 + 2*a1*t + a0 = 0, where a1 = D^T*(P-C),
61  // and a0 = (P-C)^T*(P-C)-1.
62  Result result;
63 
64  Vector3<Real> segOrigin, segDirection;
65  Real segExtent;
66  segment.GetCenteredForm(segOrigin, segDirection, segExtent);
67 
68  Vector3<Real> diff = segOrigin - sphere.center;
69  Real a0 = Dot(diff, diff) - sphere.radius * sphere.radius;
70  Real a1 = Dot(segDirection, diff);
71  Real discr = a1*a1 - a0;
72  if (discr < (Real)0)
73  {
74  result.intersect = false;
75  return result;
76  }
77 
78  Real tmp0 = segExtent*segExtent + a0;
79  Real tmp1 = ((Real)2)*a1*segExtent;
80  Real qm = tmp0 - tmp1;
81  Real qp = tmp0 + tmp1;
82  if (qm*qp <= (Real)0)
83  {
84  result.intersect = true;
85  return result;
86  }
87 
88  result.intersect = (qm > (Real)0 && std::abs(a1) < segExtent);
89  return result;
90 }
91 
92 template <typename Real>
95  Segment3<Real> const& segment, Sphere3<Real> const& sphere)
96 {
97  Vector3<Real> segOrigin, segDirection;
98  Real segExtent;
99  segment.GetCenteredForm(segOrigin, segDirection, segExtent);
100 
101  Result result;
102  DoQuery(segOrigin, segDirection, segExtent, sphere, result);
103  for (int i = 0; i < result.numIntersections; ++i)
104  {
105  result.point[i] = segOrigin + result.parameter[i] * segDirection;
106  }
107  return result;
108 }
109 
110 template <typename Real>
112  Vector3<Real> const& segOrigin, Vector3<Real> const& segDirection,
113  Real segExtent, Sphere3<Real> const& sphere, Result& result)
114 {
115  FIQuery<Real, Line3<Real>, Sphere3<Real>>::DoQuery(segOrigin,
116  segDirection, sphere, result);
117 
118  if (result.intersect)
119  {
120  // The line containing the segment intersects the sphere; the
121  // t-interval is [t0,t1]. The segment intersects the sphere as
122  // long as [t0,t1] overlaps the segment t-interval
123  // [-segExtent,+segExtent].
124  std::array<Real, 2> segInterval = { -segExtent, segExtent };
125  FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
126  auto iiResult = iiQuery(result.parameter, segInterval);
127  if (iiResult.intersect)
128  {
129  result.numIntersections = iiResult.numIntersections;
130  result.parameter = iiResult.overlap;
131  }
132  else
133  {
134  result.intersect = false;
135  result.numIntersections = 0;
136  }
137  }
138 }
139 
140 
141 }
gte::BSNumber< UIntegerType > abs(gte::BSNumber< UIntegerType > const &number)
Definition: GteBSNumber.h:966
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


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