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
|
// 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.1 (2010/10/01)
#include "Wm5MathematicsPCH.h"
#include "Wm5ContLozenge3.h"
#include "Wm5ApprGaussPointsFit3.h"
#include "Wm5DistPoint3Line3.h"
#include "Wm5DistPoint3Rectangle3.h"
namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
Lozenge3<Real> ContLozenge (int numPoints, const Vector3<Real>* points)
{
// Fit with Gaussian. Axis(0) corresponds to the smallest eigenvalue.
Box3<Real> box = GaussPointsFit3<Real>(numPoints, points);
Vector3<Real> diff = points[0] - box.Center;
Real wMin = box.Axis[0].Dot(diff);
Real wMax = wMin;
Real w;
int i;
for (i = 1; i < numPoints; ++i)
{
diff = points[i] - box.Center;
w = box.Axis[0].Dot(diff);
if (w < wMin)
{
wMin = w;
}
else if (w > wMax)
{
wMax = w;
}
}
Real radius = ((Real)0.5)*(wMax - wMin);
Real rSqr = radius*radius;
box.Center += (((Real)0.5)*(wMax + wMin))*box.Axis[0];
Real aMin = Math<Real>::MAX_REAL;
Real aMax = -aMin;
Real bMin = Math<Real>::MAX_REAL;
Real bMax = -bMin;
Real discr, radical, u, v, test;
for (i = 0; i < numPoints; ++i)
{
diff = points[i] - box.Center;
u = box.Axis[2].Dot(diff);
v = box.Axis[1].Dot(diff);
w = box.Axis[0].Dot(diff);
discr = rSqr - w*w;
radical = Math<Real>::Sqrt(Math<Real>::FAbs(discr));
test = u + radical;
if (test < aMin)
{
aMin = test;
}
test = u - radical;
if (test > aMax)
{
aMax = test;
}
test = v + radical;
if (test < bMin)
{
bMin = test;
}
test = v - radical;
if (test > bMax)
{
bMax = test;
}
}
// The enclosing region might be a capsule or a sphere.
if (aMin >= aMax)
{
test = ((Real)0.5)*(aMin + aMax);
aMin = test;
aMax = test;
}
if (bMin >= bMax)
{
test = ((Real)0.5)*(bMin + bMax);
bMin = test;
bMax = test;
}
// Make correction for points inside mitered corner but outside quarter
// sphere.
for (i = 0; i < numPoints; ++i)
{
diff = points[i] - box.Center;
u = box.Axis[2].Dot(diff);
v = box.Axis[1].Dot(diff);
Real* aExtreme = 0;
Real* bExtreme = 0;
if (u > aMax)
{
if (v > bMax)
{
aExtreme = &aMax;
bExtreme = &bMax;
}
else if (v < bMin)
{
aExtreme = &aMax;
bExtreme = &bMin;
}
}
else if (u < aMin)
{
if (v > bMax)
{
aExtreme = &aMin;
bExtreme = &bMax;
}
else if (v < bMin)
{
aExtreme = &aMin;
bExtreme = &bMin;
}
}
if (aExtreme)
{
Real deltaU = u - *aExtreme;
Real deltaV = v - *bExtreme;
Real deltaSumSqr = deltaU*deltaU + deltaV*deltaV;
w = box.Axis[0].Dot(diff);
Real wSqr = w*w;
test = deltaSumSqr + wSqr;
if (test > rSqr)
{
discr = (rSqr - wSqr)/deltaSumSqr;
Real t = -Math<Real>::Sqrt(Math<Real>::FAbs(discr));
*aExtreme = u + t*deltaU;
*bExtreme = v + t*deltaV;
}
}
}
Lozenge3<Real> lozenge;
lozenge.Radius = radius;
lozenge.Rectangle.Axis[0] = box.Axis[2];
lozenge.Rectangle.Axis[1] = box.Axis[1];
if (aMin < aMax)
{
if (bMin < bMax)
{
// Container is a lozenge.
lozenge.Rectangle.Center = box.Center + aMin*box.Axis[2] +
bMin*box.Axis[1];
lozenge.Rectangle.Extent[0] = ((Real)0.5)*(aMax - aMin);
lozenge.Rectangle.Extent[1] = ((Real)0.5)*(bMax - bMin);
}
else
{
// Container is a capsule.
lozenge.Rectangle.Center = box.Center + aMin*box.Axis[2] +
(((Real)0.5)*(bMin + bMax))*box.Axis[1];
lozenge.Rectangle.Extent[0] = ((Real)0.5)*(aMax - aMin);
lozenge.Rectangle.Extent[1] = (Real)0;
}
}
else
{
if (bMin < bMax)
{
// Container is a capsule.
lozenge.Rectangle.Center = box.Center + bMin*box.Axis[1] +
(((Real)0.5)*(aMin + aMax))*box.Axis[2];
lozenge.Rectangle.Extent[0] = (Real)0;
lozenge.Rectangle.Extent[1] = ((Real)0.5)*(bMax - bMin);
}
else
{
// Container is a sphere.
lozenge.Rectangle.Center = box.Center +
(((Real)0.5)*(aMin + aMax))*box.Axis[2] +
(((Real)0.5)*(bMin + bMax))*box.Axis[1];
lozenge.Rectangle.Extent[0] = (Real)0;
lozenge.Rectangle.Extent[1] = (Real)0;
}
}
return lozenge;
}
//----------------------------------------------------------------------------
template <typename Real>
bool InLozenge (const Vector3<Real>& point, const Lozenge3<Real>& lozenge)
{
Real dist = DistPoint3Rectangle3<Real>(point, lozenge.Rectangle).Get();
return dist <= lozenge.Radius;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template WM5_MATHEMATICS_ITEM
Lozenge3<float> ContLozenge<float> (int, const Vector3<float>*);
template WM5_MATHEMATICS_ITEM
bool InLozenge<float> (const Vector3<float>&, const Lozenge3<float>&);
template WM5_MATHEMATICS_ITEM
Lozenge3<double> ContLozenge<double> (int, const Vector3<double>*);
template WM5_MATHEMATICS_ITEM
bool InLozenge<double> (const Vector3<double>&, const Lozenge3<double>&);
//----------------------------------------------------------------------------
}
|