1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.3 (2011/10/26)
#include "Wm5MathematicsPCH.h"
#include "Wm5IntrSphere3Sphere3.h"
namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
IntrSphere3Sphere3<Real>::IntrSphere3Sphere3 (const Sphere3<Real>& rkSphere0,
const Sphere3<Real>& rkSphere1)
:
mSphere0(&rkSphere0),
mSphere1(&rkSphere1)
{
}
//----------------------------------------------------------------------------
template <typename Real>
const Sphere3<Real>& IntrSphere3Sphere3<Real>::GetSphere0 () const
{
return *mSphere0;
}
//----------------------------------------------------------------------------
template <typename Real>
const Sphere3<Real>& IntrSphere3Sphere3<Real>::GetSphere1 () const
{
return *mSphere1;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IntrSphere3Sphere3<Real>::Test ()
{
Vector3<Real> diff = mSphere1->Center - mSphere0->Center;
Real rSum = mSphere0->Radius + mSphere1->Radius;
return diff.SquaredLength() <= rSum*rSum;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IntrSphere3Sphere3<Real>::Find ()
{
// Plane of intersection must have N as its normal.
Vector3<Real> C1mC0 = mSphere1->Center - mSphere0->Center;
Real sqrLen = C1mC0.SquaredLength();
Real r0 = mSphere0->Radius, r1 = mSphere1->Radius;
Real rSum = r0 + r1;
Real rSumSqr = rSum*rSum;
if (sqrLen > rSumSqr)
{
// Spheres are disjoint/separated.
mIntersectionType = IT_EMPTY;
return false;
}
if (sqrLen == rSumSqr)
{
// Spheres are just touching. The caller must call
// GetIntersectionType() to determine what type of intersection has
// occurred. In this case, GetContactPoint() should be called, not
// GetCircle(). The circle parameters are set just in case the caller
// does not test for intersection type.
C1mC0.Normalize();
mContactPoint = mSphere0->Center + r0*C1mC0;
mCircle.Normal = C1mC0;
mCircle.Center = mContactPoint;
mCircle.Radius = (Real)0;
mIntersectionType = IT_POINT;
return true;
}
Real rDif = r0 - r1;
Real rDifSqr = rDif*rDif;
if (sqrLen < rDifSqr)
{
// One sphere is strictly contained in the other. The caller must
// call GetIntersectionType() to determine what type of intersection
// has occurred. In this case, neither GetCircle() nor
// GetContactPoint() should be called. The circle and contact
// parameters are set just in case the caller does not test for
// intersection type, but the choices are arbitrary.
C1mC0.Normalize();
mContactPoint = ((Real)0.5)*(mSphere0->Center + mSphere1->Center);
mCircle.Normal = C1mC0;
mCircle.Center = mContactPoint;
mCircle.Radius = (Real)0;
mIntersectionType = (rDif <= (Real)0 ? IT_SPHERE0 : IT_SPHERE1);
return true;
}
if (sqrLen == rDifSqr)
{
// One sphere is contained in the other sphere but with a single point
// of contact. The caller must call GetIntersectionType() to
// determine what type of intersection has occurred. In this case,
// GetContactPoint() should be called. The circle parameters are set
// just in case the caller does not test for intersection type.
C1mC0.Normalize();
mCircle.Radius = (Real)0;
mCircle.Normal = C1mC0;
if (rDif <= (Real)0)
{
mContactPoint = mSphere1->Center + r1*C1mC0;
mIntersectionType = IT_SPHERE0_POINT;
}
else
{
mContactPoint = mSphere0->Center + r0*C1mC0;
mIntersectionType = IT_SPHERE1_POINT;
}
mCircle.Center = mContactPoint;
return true;
}
// Compute t for which the circle of intersection has center
// K = C0 + t*(C1 - C0).
Real t = ((Real)0.5)*((Real)1 + rDif*rSum/sqrLen);
// Center and radius of circle of intersection.
mCircle.Center = mSphere0->Center + t*C1mC0;
mCircle.Radius = Math<Real>::Sqrt(Math<Real>::FAbs(r0*r0 - t*t*sqrLen));
// Compute N, U, and V for plane of circle.
C1mC0.Normalize();
mCircle.Normal = C1mC0;
Vector3<Real>::GenerateComplementBasis(mCircle.Direction0,
mCircle.Direction1, mCircle.Normal);
// The intersection is a circle.
mIntersectionType = IT_CIRCLE;
return true;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IntrSphere3Sphere3<Real>::Test (Real tmax,
const Vector3<Real>& velocity0, const Vector3<Real>& velocity1)
{
Vector3<Real> relVelocity = velocity1 - velocity0;
Real a = relVelocity.SquaredLength();
Vector3<Real> CDiff = mSphere1->Center - mSphere0->Center;
Real c = CDiff.SquaredLength();
Real rSum = mSphere0->Radius + mSphere1->Radius;
Real rSumSqr = rSum*rSum;
if (a > (Real)0)
{
Real b = CDiff.Dot(relVelocity);
if (b <= (Real)0)
{
if (-tmax*a <= b)
{
return a*c - b*b <= a*rSumSqr;
}
else
{
return tmax*(tmax*a + ((Real)2)*b) + c <= rSumSqr;
}
}
}
return c <= rSumSqr;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IntrSphere3Sphere3<Real>::Find (Real tmax,
const Vector3<Real>& velocity0, const Vector3<Real>& velocity1)
{
Vector3<Real> relVelocity = velocity1 - velocity0;
Real a = relVelocity.SquaredLength();
Vector3<Real> CDiff = mSphere1->Center - mSphere0->Center;
Real c = CDiff.SquaredLength();
Real rSum = mSphere0->Radius + mSphere1->Radius;
Real rSumSqr = rSum*rSum;
if (a > (Real)0)
{
Real b = CDiff.Dot(relVelocity);
if (b <= (Real)0)
{
if (-tmax*a <= b || tmax*(tmax*a + ((Real)2.0)*b) + c <= rSumSqr)
{
Real cdiff = c - rSumSqr;
Real discr = b*b - a*cdiff;
if (discr >= (Real)0)
{
if (cdiff <= (Real)0)
{
// The spheres are initially intersecting. Estimate a
// point of contact by using the midpoint of the line
// segment connecting the sphere centers.
mContactTime = (Real)0;
mContactPoint = ((Real)0.5)*(mSphere0->Center +
mSphere1->Center);
}
else
{
// The first time of contact is in [0,tmax].
mContactTime = -(b + Math<Real>::Sqrt(discr))/a;
if (mContactTime < (Real)0)
{
mContactTime = (Real)0;
}
else if (mContactTime > tmax)
{
mContactTime = tmax;
}
Vector3<Real> newCDiff = CDiff +
mContactTime*relVelocity;
mContactPoint = mSphere0->Center +
mContactTime*velocity0 +
(mSphere0->Radius/rSum)*newCDiff;
}
return true;
}
}
return false;
}
}
if (c <= rSumSqr)
{
// The spheres are initially intersecting. Estimate a point of
// contact by using the midpoint of the line segment connecting the
// sphere centers.
mContactTime = (Real)0;
mContactPoint = ((Real)0.5)*(mSphere0->Center + mSphere1->Center);
return true;
}
return false;
}
//----------------------------------------------------------------------------
template <typename Real>
const Circle3<Real>& IntrSphere3Sphere3<Real>::GetCircle () const
{
return mCircle;
}
//----------------------------------------------------------------------------
template <typename Real>
const Vector3<Real>& IntrSphere3Sphere3<Real>::GetContactPoint () const
{
return mContactPoint;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template WM5_MATHEMATICS_ITEM
class IntrSphere3Sphere3<float>;
template WM5_MATHEMATICS_ITEM
class IntrSphere3Sphere3<double>;
//----------------------------------------------------------------------------
}
|