GteIntrSphere3Sphere3.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/GteCircle3.h>
12 #include <Mathematics/GteFIQuery.h>
13 #include <Mathematics/GteTIQuery.h>
14 
15 // The queries consider the spheres to be solids.
16 
17 namespace gte
18 {
19 
20 template <typename Real>
21 class TIQuery<Real, Sphere3<Real>, Sphere3<Real>>
22 {
23 public:
24  struct Result
25  {
26  bool intersect;
27  };
28 
29  Result operator()(Sphere3<Real> const& sphere0,
30  Sphere3<Real> const& sphere1);
31 };
32 
33 template <typename Real>
34 class FIQuery<Real, Sphere3<Real>, Sphere3<Real>>
35 {
36 public:
37  struct Result
38  {
39  bool intersect;
40 
41  // The type of intersection.
42  // 0: spheres are disjoint and separated
43  // 1: spheres touch at point, each sphere outside the other
44  // 2: spheres intersect in a circle
45  // 3: sphere0 strictly contained in sphere1
46  // 4: sphere0 contained in sphere1, share common point
47  // 5: sphere1 strictly contained in sphere0
48  // 6: sphere1 contained in sphere0, share common point
49  int type;
50  Vector3<Real> point; // types 1, 4, 6
51  Circle3<Real> circle; // type 2
52  };
53 
54  Result operator()(Sphere3<Real> const& sphere0,
55  Sphere3<Real> const& sphere1);
56 };
57 
58 
59 template <typename Real>
62  Sphere3<Real> const& sphere0, Sphere3<Real> const& sphere1)
63 {
64  Result result;
65  Vector3<Real> diff = sphere1.center - sphere0.center;
66  Real rSum = sphere0.radius + sphere1.radius;
67  result.intersect = (Dot(diff, diff) <= rSum*rSum);
68  return result;
69 }
70 
71 template <typename Real>
74  Sphere3<Real> const& sphere0, Sphere3<Real> const& sphere1)
75 {
76  Result result;
77 
78  // The plane of intersection must have C1-C0 as its normal direction.
79  Vector3<Real> C1mC0 = sphere1.center - sphere0.center;
80  Real sqrLen = Dot(C1mC0, C1mC0);
81  Real r0 = sphere0.radius, r1 = sphere1.radius;
82  Real rSum = r0 + r1;
83  Real rSumSqr = rSum * rSum;
84 
85  if (sqrLen > rSumSqr)
86  {
87  // The spheres are disjoint/separated.
88  result.intersect = false;
89  result.type = 0;
90  return result;
91  }
92 
93  if (sqrLen == rSumSqr)
94  {
95  // The spheres are just touching with each sphere outside the other.
96  Normalize(C1mC0);
97  result.intersect = true;
98  result.type = 1;
99  result.point = sphere0.center + r0 * C1mC0;
100  return result;
101  }
102 
103  Real rDif = r0 - r1;
104  Real rDifSqr = rDif*rDif;
105  if (sqrLen < rDifSqr)
106  {
107  // One sphere is strictly contained in the other. Compute a point in
108  // the intersection set.
109  result.intersect = true;
110  result.type = (rDif <= (Real)0 ? 3 : 5);
111  result.point = ((Real)0.5)*(sphere0.center + sphere1.center);
112  return result;
113  }
114  if (sqrLen == rDifSqr)
115  {
116  // One sphere is contained in the other sphere but with a single point
117  // of contact.
118  Normalize(C1mC0);
119  result.intersect = true;
120  if (rDif <= (Real)0)
121  {
122  result.type = 4;
123  result.point = sphere1.center + r1 * C1mC0;
124  }
125  else
126  {
127  result.type = 6;
128  result.point = sphere0.center + r0 * C1mC0;
129  }
130  return result;
131  }
132 
133  // Compute t for which the circle of intersection has center
134  // K = C0 + t*(C1 - C0).
135  Real t = ((Real)0.5) * ((Real)1 + rDif * rSum / sqrLen);
136 
137  // Compute the center and radius of the circle of intersection.
138  result.circle.center = sphere0.center + t * C1mC0;
139  result.circle.radius = sqrt(std::max(r0*r0 - t*t*sqrLen, (Real)0));
140 
141  // Compute the normal for the plane of the circle.
142  Normalize(C1mC0);
143  result.circle.normal = C1mC0;
144 
145  // The intersection is a circle.
146  result.intersect = true;
147  result.type = 2;
148  return result;
149 }
150 
151 
152 }
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
Real Normalize(GVector< Real > &v, bool robust=false)
Definition: GteGVector.h:454
GLdouble GLdouble t
Definition: glext.h:239
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