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 261 262 263 264 265 266 267 268 269
|
// 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 (2015/11/21)
#include "Wm5GraphicsPCH.h"
#include "Wm5Bound.h"
using namespace Wm5;
//----------------------------------------------------------------------------
Bound::Bound ()
:
mCenter(APoint::ORIGIN),
mRadius(0.0f)
{
// The radius must be nonnegative. A radius of zero denotes an invalid
// bound.
}
//----------------------------------------------------------------------------
Bound::~Bound ()
{
}
//----------------------------------------------------------------------------
Bound& Bound::operator= (const Bound& bound)
{
mCenter = bound.mCenter;
mRadius = bound.mRadius;
return *this;
}
//----------------------------------------------------------------------------
int Bound::WhichSide (const HPlane& plane) const
{
float signedDistance = plane.DistanceTo(mCenter);
if (signedDistance <= -mRadius)
{
return -1;
}
if (signedDistance >= mRadius)
{
return +1;
}
return 0;
}
//----------------------------------------------------------------------------
void Bound::GrowToContain (const Bound& bound)
{
if (bound.mRadius == 0.0f)
{
// The incoming bound is invalid and cannot affect growth.
return;
}
if (mRadius == 0.0f)
{
// The current bound is invalid, so just assign the incoming bound.
*this = bound;
return;
}
AVector centerDiff = bound.mCenter - mCenter;
float lengthSqr = centerDiff.SquaredLength();
float radiusDiff = bound.mRadius - mRadius;
float radiusDiffSqr = radiusDiff*radiusDiff;
if (radiusDiffSqr >= lengthSqr)
{
if (radiusDiff >= 0.0f)
{
mCenter = bound.mCenter;
mRadius = bound.mRadius;
}
return;
}
float length = Mathf::Sqrt(lengthSqr);
if (length > Mathf::ZERO_TOLERANCE)
{
float coeff = (length + radiusDiff)/(2.0f*length);
mCenter += coeff*centerDiff;
}
mRadius = 0.5f*(length + mRadius + bound.mRadius);
}
//----------------------------------------------------------------------------
void Bound::TransformBy (const Transform& transform, Bound& bound)
{
bound.mCenter = transform*mCenter;
bound.mRadius = transform.GetNorm()*mRadius;
}
//----------------------------------------------------------------------------
void Bound::ComputeFromData (int numElements, int stride, const char* data)
{
// The center is the average of the positions.
float sum[3] = { 0.0f, 0.0f, 0.0f };
int i;
for (i = 0; i < numElements; ++i)
{
// This assumes the positions are at offset zero, which they should be
// for vertex buffer data.
const float* position = (const float*)(data + i*stride);
sum[0] += position[0];
sum[1] += position[1];
sum[2] += position[2];
}
float invNumElements = 1.0f/(float)numElements;
mCenter[0] = sum[0]*invNumElements;
mCenter[1] = sum[1]*invNumElements;
mCenter[2] = sum[2]*invNumElements;
mCenter[3] = 1.0f;
// The radius is the largest distance from the center to the positions.
mRadius = 0.0f;
for (i = 0; i < numElements; ++i)
{
// This assumes the positions are at offset zero, which they should be
// for vertex buffer data.
const float* position = (const float*)(data + i*stride);
float diff[3] =
{
position[0] - mCenter[0],
position[1] - mCenter[1],
position[2] - mCenter[2]
};
float radiusSqr = diff[0]*diff[0] + diff[1]*diff[1] + diff[2]*diff[2];
if (radiusSqr > mRadius)
{
mRadius = radiusSqr;
}
}
mRadius = Mathf::Sqrt(mRadius);
}
//----------------------------------------------------------------------------
bool Bound::TestIntersection (const APoint& origin, const AVector& direction,
float tmin, float tmax) const
{
if (mRadius == 0.0f)
{
// The bound is invalid and cannot be intersected.
return false;
}
AVector diff;
float a0, a1, discr;
if (tmin == -Mathf::MAX_REAL)
{
assertion(tmax == Mathf::MAX_REAL,
"tmax must be infinity for a line.\n");
// Test for sphere-line intersection.
diff = origin - mCenter;
a0 = diff.Dot(diff) - mRadius*mRadius;
a1 = direction.Dot(diff);
discr = a1*a1 - a0;
return discr >= 0.0f;
}
if (tmax == Mathf::MAX_REAL)
{
assertion(tmin == 0.0f, "tmin must be zero for a ray.\n");
// Test for sphere-ray intersection.
diff = origin - mCenter;
a0 = diff.Dot(diff) - mRadius*mRadius;
if (a0 <= 0.0f)
{
// The ray origin is inside the sphere.
return true;
}
// else: The ray origin is outside the sphere.
a1 = direction.Dot(diff);
if (a1 >= 0.0f)
{
// The ray forms an acute angle with diff, and so the ray is
// directed from the sphere. Thus, the ray origin is outside
// the sphere, and points P+t*D for t >= 0 are even farther
// away from the sphere.
return false;
}
discr = a1*a1 - a0;
return discr >= 0.0f;
}
assertion(tmax > tmin, "tmin < tmax is required for a segment.\n");
// Test for sphere-segment intersection.
float segExtent = 0.5f*(tmin + tmax);
APoint segOrigin = origin + segExtent*direction;
diff = segOrigin - mCenter;
a0 = diff.Dot(diff) - mRadius*mRadius;
a1 = direction.Dot(diff);
discr = a1*a1 - a0;
if (discr < 0.0f)
{
return false;
}
float tmp0 = segExtent*segExtent + a0;
float tmp1 = 2.0f*a1*segExtent;
float qm = tmp0 - tmp1;
float qp = tmp0 + tmp1;
if (qm*qp <= 0.0f)
{
return true;
}
return qm > 0.0f && Mathf::FAbs(a1) < segExtent;
}
//----------------------------------------------------------------------------
bool Bound::TestIntersection (const Bound& bound) const
{
if (bound.mRadius == 0.0f || mRadius == 0.0f)
{
// One of the bounds is invalid and cannot be intersected.
return false;
}
// Test for staticSphere-staticSphere intersection.
AVector diff = mCenter - bound.mCenter;
float rSum = mRadius + bound.mRadius;
return diff.SquaredLength() <= rSum*rSum;
}
//----------------------------------------------------------------------------
bool Bound::TestIntersection (const Bound& bound, float tmax,
const AVector& velocity0, const AVector& velocity1) const
{
if (bound.mRadius == 0.0f || mRadius == 0.0f)
{
// One of the bounds is invalid and cannot be intersected.
return false;
}
// Test for movingSphere-movingSphere intersection.
AVector relVelocity = velocity1 - velocity0;
AVector cenDiff = bound.mCenter - mCenter;
float a = relVelocity.SquaredLength();
float c = cenDiff.SquaredLength();
float rSum = bound.mRadius + mRadius;
float rSumSqr = rSum*rSum;
if (a > 0.0f)
{
float b = cenDiff.Dot(relVelocity);
if (b <= 0.0f)
{
if (-tmax*a <= b)
{
return a*c - b*b <= a*rSumSqr;
}
else
{
return tmax*(tmax*a + 2.0f*b) + c <= rSumSqr;
}
}
}
return c <= rSumSqr;
}
//----------------------------------------------------------------------------
|