| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
#ifdef NDEBUG |
| 7 |
# undef NDEBUG |
| 8 |
#endif |
| 9 |
|
| 10 |
#include <ImathBoxAlgo.h> |
| 11 |
#include <ImathRandom.h> |
| 12 |
#include <algorithm> |
| 13 |
#include <assert.h> |
| 14 |
#include <iostream> |
| 15 |
#include "testBoxAlgo.h" |
| 16 |
|
| 17 |
using namespace std; |
| 18 |
using namespace IMATH_INTERNAL_NAMESPACE; |
| 19 |
|
| 20 |
namespace |
| 21 |
{ |
| 22 |
|
| 23 |
bool |
| 24 |
approximatelyEqual (const V3f& p1, const V3f& p2, float e) |
| 25 |
{ |
| 26 |
float m = 0; |
| 27 |
|
| 28 |
for (int i = 0; i < 3; ++i) |
| 29 |
{ |
| 30 |
m = max (m, abs (p1[i])); |
| 31 |
m = max (m, abs (p2[i])); |
| 32 |
} |
| 33 |
|
| 34 |
for (int i = 0; i < 3; ++i) |
| 35 |
if (!equalWithAbsError (p1[i], p2[i], m * e)) |
| 36 |
return false; |
| 37 |
|
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
void |
| 42 |
testEntryAndExitPoints (const Box3f& box) |
| 43 |
{ |
| 44 |
Rand48 random (2007); |
| 45 |
|
| 46 |
float e = 50 * std::numeric_limits<float>::epsilon(); |
| 47 |
|
| 48 |
if (box.isEmpty()) |
| 49 |
{ |
| 50 |
cout << " empty box, no rays intersect" << endl; |
| 51 |
|
| 52 |
for (int i = 0; i < 100000; ++i) |
| 53 |
{ |
| 54 |
V3f p1 ( |
| 55 |
float(random.nextf (box.max.x, box.min.x)), |
| 56 |
float(random.nextf (box.max.y, box.min.y)), |
| 57 |
float(random.nextf (box.max.z, box.min.z))); |
| 58 |
|
| 59 |
V3f p2 (p1 + hollowSphereRand<V3f> (random)); |
| 60 |
|
| 61 |
V3f r, s; |
| 62 |
assert (!findEntryAndExitPoints (Line3f (p1, p2), box, r, s)); |
| 63 |
} |
| 64 |
|
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
cout << " box = (" << box.min << " " << box.max << ")" << endl; |
| 69 |
|
| 70 |
if (box.max == box.min) |
| 71 |
{ |
| 72 |
cout << " single-point box, ray intersects" << endl; |
| 73 |
|
| 74 |
static const float off[6][3] = { { -1, 0, 0 }, { 1, 0, 0 }, { 0, -1, 0 }, |
| 75 |
{ 0, 1, 0 }, { 0, 0, -1 }, { 0, 0, 1 } }; |
| 76 |
|
| 77 |
for (int i = 0; i < 6; ++i) |
| 78 |
{ |
| 79 |
V3f p1 (box.min.x + off[i][0], box.min.y + off[i][1], box.min.z + off[i][2]); |
| 80 |
|
| 81 |
V3f r, s; |
| 82 |
assert (findEntryAndExitPoints (Line3f (p1, box.min), box, r, s)); |
| 83 |
assert (r == box.min && s == box.min); |
| 84 |
} |
| 85 |
|
| 86 |
cout << " single-point box, ray does not intersect" << endl; |
| 87 |
|
| 88 |
for (int i = 0; i < 100000; ++i) |
| 89 |
{ |
| 90 |
// |
| 91 |
// The ray starts at a distance of r2 from the of the |
| 92 |
// box, and it passes the box at a minimum distance of r1. |
| 93 |
// |
| 94 |
|
| 95 |
const float r1 = 0.00001f; |
| 96 |
const float r2 = 1.0f; |
| 97 |
|
| 98 |
V3f p1 = box.min + r2 * hollowSphereRand<V3f> (random); |
| 99 |
V3f p2; |
| 100 |
float r3; |
| 101 |
|
| 102 |
do |
| 103 |
{ |
| 104 |
do |
| 105 |
{ |
| 106 |
p2 = box.min + r2 * hollowSphereRand<V3f> (random); |
| 107 |
} while (approximatelyEqual (p1, p2, e)); |
| 108 |
|
| 109 |
V3f d1 = (p2 - p1).normalized(); |
| 110 |
V3f d2 = (box.min - p1); |
| 111 |
r3 = (d2 - d1 * (d1 ^ d2)).length(); |
| 112 |
} while (r3 < r1); |
| 113 |
|
| 114 |
Line3f ray (p1, p2); |
| 115 |
V3f r, s; |
| 116 |
|
| 117 |
assert (!findEntryAndExitPoints (ray, box, r, s)); |
| 118 |
} |
| 119 |
|
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
cout << " ray starts outside box, intersects" << endl; |
| 124 |
|
| 125 |
Box3f bigBox (box.min - (box.max - box.min), box.max + (box.max - box.min)); |
| 126 |
|
| 127 |
for (int i = 0; i < 100000; ++i) |
| 128 |
{ |
| 129 |
// |
| 130 |
// Find starting point outside the box, end point inside the box |
| 131 |
// |
| 132 |
|
| 133 |
V3f p1; |
| 134 |
|
| 135 |
do |
| 136 |
{ |
| 137 |
p1 = V3f ( |
| 138 |
float(random.nextf (bigBox.min.x, bigBox.max.x)), |
| 139 |
float(random.nextf (bigBox.min.y, bigBox.max.y)), |
| 140 |
float(random.nextf (bigBox.min.z, bigBox.max.z))); |
| 141 |
} while (box.intersects (p1)); |
| 142 |
|
| 143 |
V3f p2; |
| 144 |
|
| 145 |
do |
| 146 |
{ |
| 147 |
p2 = V3f ( |
| 148 |
float(random.nextf (box.min.x, box.max.x)), |
| 149 |
float(random.nextf (box.min.y, box.max.y)), |
| 150 |
float(random.nextf (box.min.z, box.max.z))); |
| 151 |
} while (approximatelyEqual (p1, p2, e)); |
| 152 |
|
| 153 |
Line3f ray (p1, p2); |
| 154 |
|
| 155 |
V3f r, s; |
| 156 |
bool b = findEntryAndExitPoints (ray, box, r, s); |
| 157 |
|
| 158 |
// |
| 159 |
// Ray and box must intersect, entry and exit points |
| 160 |
// must be on the surface of the box. |
| 161 |
// |
| 162 |
|
| 163 |
assert (b); |
| 164 |
|
| 165 |
assert (r.x == box.min.x || r.x == box.max.x || r.y == box.min.y || r.y == box.max.y || |
| 166 |
r.z == box.min.z || r.z == box.max.z); |
| 167 |
|
| 168 |
assert (s.x == box.min.x || s.x == box.max.x || s.y == box.min.y || s.y == box.max.y || |
| 169 |
s.z == box.min.z || s.z == box.max.z); |
| 170 |
|
| 171 |
// |
| 172 |
// Entry and exit points must be consistent |
| 173 |
// with the direction of the ray |
| 174 |
// |
| 175 |
|
| 176 |
if (r.x == box.min.x) |
| 177 |
assert (ray.dir.x >= 0); |
| 178 |
|
| 179 |
if (r.x == box.max.x) |
| 180 |
assert (ray.dir.x <= 0); |
| 181 |
|
| 182 |
if (r.y == box.min.y) |
| 183 |
assert (ray.dir.y >= 0); |
| 184 |
|
| 185 |
if (r.y == box.max.y) |
| 186 |
assert (ray.dir.y <= 0); |
| 187 |
|
| 188 |
if (r.z == box.min.z) |
| 189 |
assert (ray.dir.z >= 0); |
| 190 |
|
| 191 |
if (r.z == box.max.z) |
| 192 |
assert (ray.dir.z <= 0); |
| 193 |
|
| 194 |
if (s.x == box.max.x) |
| 195 |
assert (ray.dir.x >= 0); |
| 196 |
|
| 197 |
if (s.x == box.min.x) |
| 198 |
assert (ray.dir.x <= 0); |
| 199 |
|
| 200 |
if (s.y == box.max.y) |
| 201 |
assert (ray.dir.y >= 0); |
| 202 |
|
| 203 |
if (s.y == box.min.y) |
| 204 |
assert (ray.dir.y <= 0); |
| 205 |
|
| 206 |
if (s.z == box.max.z) |
| 207 |
assert (ray.dir.z >= 0); |
| 208 |
|
| 209 |
if (s.z == box.min.z) |
| 210 |
assert (ray.dir.z <= 0); |
| 211 |
|
| 212 |
// |
| 213 |
// Entry and exit points must be approximately on the ray |
| 214 |
// How far they can be off depends on how far p1 and the |
| 215 |
// entry and exit points are are from the origin. |
| 216 |
// |
| 217 |
|
| 218 |
{ |
| 219 |
V3f p3 = p1 + ray.dir * (ray.dir ^ (r - p1)); |
| 220 |
float m = 0; |
| 221 |
|
| 222 |
for (int j = 0; j < 3; ++j) |
| 223 |
{ |
| 224 |
m = max (abs (p1[j]), m); |
| 225 |
m = max (abs (r[j]), m); |
| 226 |
} |
| 227 |
|
| 228 |
float err = 30 * m * std::numeric_limits<float>::epsilon(); |
| 229 |
assert (p3.equalWithAbsError (r, err)); |
| 230 |
} |
| 231 |
|
| 232 |
{ |
| 233 |
V3f p3 = p1 + ray.dir * (ray.dir ^ (s - p1)); |
| 234 |
float m = 0; |
| 235 |
|
| 236 |
for (int j = 0; j < 3; ++j) |
| 237 |
{ |
| 238 |
m = max (abs (p1[j]), m); |
| 239 |
m = max (abs (s[j]), m); |
| 240 |
} |
| 241 |
|
| 242 |
float err = 30 * m * std::numeric_limits<float>::epsilon(); |
| 243 |
assert (p3.equalWithAbsError (s, err)); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
cout << " ray starts outside box, does not intersect" << endl; |
| 248 |
|
| 249 |
V3f center = (box.min + box.max) * 0.5f; |
| 250 |
float r1 = (box.max - box.min).length() * 0.51f; |
| 251 |
float r2 = 2 * r1; |
| 252 |
|
| 253 |
for (int i = 0; i < 100000; ++i) |
| 254 |
{ |
| 255 |
// |
| 256 |
// The ray starts at a distance of r2 from the center |
| 257 |
// of the box, and it passes the center at a minimum |
| 258 |
// distance of r1. (r1 and r2 are both greater than |
| 259 |
// the distance between the center and the corners |
| 260 |
// of the box.) |
| 261 |
// |
| 262 |
|
| 263 |
V3f p1 = center + r2 * hollowSphereRand<V3f> (random); |
| 264 |
V3f p2; |
| 265 |
float r3; |
| 266 |
|
| 267 |
do |
| 268 |
{ |
| 269 |
do |
| 270 |
{ |
| 271 |
p2 = center + r2 * hollowSphereRand<V3f> (random); |
| 272 |
} while (approximatelyEqual (p1, p2, e)); |
| 273 |
|
| 274 |
V3f d1 = (p2 - p1).normalized(); |
| 275 |
V3f d2 = (center - p1); |
| 276 |
r3 = (d2 - d1 * (d1 ^ d2)).length(); |
| 277 |
} while (r3 < r1); |
| 278 |
|
| 279 |
Line3f ray (p1, p2); |
| 280 |
V3f r, s; |
| 281 |
|
| 282 |
assert (!findEntryAndExitPoints (ray, box, r, s)); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
void |
| 287 |
entryAndExitPoints1() |
| 288 |
{ |
| 289 |
cout << " ray-box entry and exit, random rays" << endl; |
| 290 |
|
| 291 |
Box3f boxes[] = { // Boxes with a positive volume |
| 292 |
|
| 293 |
Box3f (V3f (-1, -1, -1), V3f (1, 1, 1)), |
| 294 |
Box3f (V3f (10, 20, 30), V3f (1010, 21, 31)), |
| 295 |
Box3f (V3f (10, 20, 30), V3f (11, 1020, 31)), |
| 296 |
Box3f (V3f (10, 20, 30), V3f (11, 21, 1030)), |
| 297 |
Box3f (V3f (-1e10f, -2e10f, -3e10f), V3f (5e15f, 6e15f, 7e15f)), |
| 298 |
|
| 299 |
// Non-empty, zero-volume boxes |
| 300 |
|
| 301 |
Box3f (V3f (1, 1, 1), V3f (2, 1, 1)), |
| 302 |
Box3f (V3f (1, 1, 1), V3f (1, 2, 1)), |
| 303 |
Box3f (V3f (1, 1, 1), V3f (1, 1, 2)), |
| 304 |
Box3f (V3f (1, 1, 1), V3f (1, 2, 3)), |
| 305 |
Box3f (V3f (1, 1, 1), V3f (2, 3, 1)), |
| 306 |
Box3f (V3f (1, 1, 1), V3f (2, 1, 3)), |
| 307 |
Box3f (V3f (-1, -2, 1), V3f (-1, -2, 1)), |
| 308 |
Box3f (V3f (1, 1, 1), V3f (1, 1, 1)), |
| 309 |
Box3f (V3f (0, 0, 0), V3f (0, 0, 0)), |
| 310 |
|
| 311 |
// empty box |
| 312 |
|
| 313 |
Box3f() |
| 314 |
}; |
| 315 |
|
| 316 |
for (size_t i = 0; i < sizeof (boxes) / sizeof (boxes[0]); ++i) |
| 317 |
testEntryAndExitPoints (boxes[i]); |
| 318 |
} |
| 319 |
|
| 320 |
void |
| 321 |
testPerturbedRayBoxEntryExit (const Box3f& box, const Line3f& ray, bool result) |
| 322 |
{ |
| 323 |
cout << " dir ~ " << ray.dir << ", result = " << result << endl; |
| 324 |
|
| 325 |
{ |
| 326 |
V3f r, s; |
| 327 |
assert (result == findEntryAndExitPoints (ray, box, r, s)); |
| 328 |
} |
| 329 |
|
| 330 |
Rand48 random (19); |
| 331 |
const float e = 1e-25f; |
| 332 |
|
| 333 |
for (int i = 0; i < 10000; ++i) |
| 334 |
{ |
| 335 |
Line3f ray1 (ray); |
| 336 |
ray1.dir += e * solidSphereRand<V3f> (random); |
| 337 |
|
| 338 |
V3f r, s; |
| 339 |
assert (result == findEntryAndExitPoints (ray1, box, r, s)); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
void |
| 344 |
entryAndExitPoints2() |
| 345 |
{ |
| 346 |
|
| 347 |
cout << " ray-box entry and exit, nearly axis-parallel rays" << endl; |
| 348 |
|
| 349 |
Box3f box (V3f (-1e15f, -1e15f, -1e15f), V3f (1e15f, 1e15f, 1e15f)); |
| 350 |
Line3f ray; |
| 351 |
V3f r, s; |
| 352 |
bool b; |
| 353 |
|
| 354 |
ray = Line3f (V3f (-2e15f, 0, 0), V3f (2e15f, 0, 0)); |
| 355 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 356 |
assert (b && r == V3f (-1e15f, 0, 0) && s == V3f (1e15f, 0, 0)); |
| 357 |
testPerturbedRayBoxEntryExit (box, ray, true); |
| 358 |
|
| 359 |
ray = Line3f (V3f (2e15f, 0, 0), V3f (-2e15f, 0, 0)); |
| 360 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 361 |
assert (b && r == V3f (1e15f, 0, 0) && s == V3f (-1e15f, 0, 0)); |
| 362 |
testPerturbedRayBoxEntryExit (box, ray, true); |
| 363 |
|
| 364 |
ray = Line3f (V3f (-2e15f, 2e15f, 0), V3f (2e15f, 2e15f, 0)); |
| 365 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 366 |
assert (!b); |
| 367 |
testPerturbedRayBoxEntryExit (box, ray, false); |
| 368 |
|
| 369 |
ray = Line3f (V3f (2e15f, 2e15f, 0), V3f (-2e15f, 2e15f, 0)); |
| 370 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 371 |
assert (!b); |
| 372 |
testPerturbedRayBoxEntryExit (box, ray, false); |
| 373 |
|
| 374 |
ray = Line3f (V3f (0, -2e15f, 0), V3f (0, 2e15f, 0)); |
| 375 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 376 |
assert (b && r == V3f (0, -1e15f, 0) && s == V3f (0, 1e15f, 0)); |
| 377 |
testPerturbedRayBoxEntryExit (box, ray, true); |
| 378 |
|
| 379 |
ray = Line3f (V3f (0, 2e15f, 0), V3f (0, -2e15f, 0)); |
| 380 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 381 |
assert (b && r == V3f (0, 1e15f, 0) && s == V3f (0, -1e15f, 0)); |
| 382 |
testPerturbedRayBoxEntryExit (box, ray, true); |
| 383 |
|
| 384 |
ray = Line3f (V3f (0, -2e15f, 2e15f), V3f (0, 2e15f, 2e15f)); |
| 385 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 386 |
assert (!b); |
| 387 |
testPerturbedRayBoxEntryExit (box, ray, false); |
| 388 |
|
| 389 |
ray = Line3f (V3f (0, 2e15f, 2e15f), V3f (0, -2e15f, 2e15f)); |
| 390 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 391 |
assert (!b); |
| 392 |
testPerturbedRayBoxEntryExit (box, ray, false); |
| 393 |
|
| 394 |
ray = Line3f (V3f (0, 0, -2e15f), V3f (0, 0, 2e15f)); |
| 395 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 396 |
assert (b && r == V3f (0, 0, -1e15f) && s == V3f (0, 0, 1e15f)); |
| 397 |
testPerturbedRayBoxEntryExit (box, ray, true); |
| 398 |
|
| 399 |
ray = Line3f (V3f (0, 0, 2e15f), V3f (0, 0, -2e15f)); |
| 400 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 401 |
assert (b && r == V3f (0, 0, 1e15f) && s == V3f (0, 0, -1e15f)); |
| 402 |
testPerturbedRayBoxEntryExit (box, ray, true); |
| 403 |
|
| 404 |
ray = Line3f (V3f (2e15f, 0, -2e15f), V3f (2e15f, 0, 2e15f)); |
| 405 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 406 |
assert (!b); |
| 407 |
testPerturbedRayBoxEntryExit (box, ray, false); |
| 408 |
|
| 409 |
ray = Line3f (V3f (2e15f, 0, 2e15f), V3f (2e15f, 0, -2e15f)); |
| 410 |
b = findEntryAndExitPoints (ray, box, r, s); |
| 411 |
assert (!b); |
| 412 |
testPerturbedRayBoxEntryExit (box, ray, false); |
| 413 |
} |
| 414 |
|
| 415 |
void |
| 416 |
testRayBoxIntersection (const Box3f& box) |
| 417 |
{ |
| 418 |
Rand48 random (2007); |
| 419 |
|
| 420 |
float e = 50 * std::numeric_limits<float>::epsilon(); |
| 421 |
|
| 422 |
if (box.isEmpty()) |
| 423 |
{ |
| 424 |
cout << " empty box, no rays intersect" << endl; |
| 425 |
|
| 426 |
for (int i = 0; i < 100000; ++i) |
| 427 |
{ |
| 428 |
V3f p1 ( |
| 429 |
float(random.nextf (box.max.x, box.min.x)), |
| 430 |
float(random.nextf (box.max.y, box.min.y)), |
| 431 |
float(random.nextf (box.max.z, box.min.z))); |
| 432 |
|
| 433 |
V3f p2 (p1 + hollowSphereRand<V3f> (random)); |
| 434 |
|
| 435 |
V3f ip; |
| 436 |
assert (!intersects (box, Line3f (p1, p2), ip)); |
| 437 |
} |
| 438 |
|
| 439 |
return; |
| 440 |
} |
| 441 |
|
| 442 |
cout << " box = (" << box.min << " " << box.max << ")" << endl; |
| 443 |
|
| 444 |
if (box.max == box.min) |
| 445 |
{ |
| 446 |
cout << " single-point box, ray intersects" << endl; |
| 447 |
|
| 448 |
static const float off[6][3] = { { -1, 0, 0 }, { 1, 0, 0 }, { 0, -1, 0 }, |
| 449 |
{ 0, 1, 0 }, { 0, 0, -1 }, { 0, 0, 1 } }; |
| 450 |
|
| 451 |
for (int i = 0; i < 6; ++i) |
| 452 |
{ |
| 453 |
V3f p1 (box.min.x + off[i][0], box.min.y + off[i][1], box.min.z + off[i][2]); |
| 454 |
|
| 455 |
V3f ip; |
| 456 |
assert (intersects (box, Line3f (p1, box.min), ip)); |
| 457 |
assert (ip == box.min); |
| 458 |
} |
| 459 |
|
| 460 |
cout << " single-point box, ray does not intersect" << endl; |
| 461 |
|
| 462 |
for (int i = 0; i < 100000; ++i) |
| 463 |
{ |
| 464 |
// |
| 465 |
// The ray starts at a distance of r2 from the of the |
| 466 |
// box, and it passes the box at a minimum distance of r1. |
| 467 |
// |
| 468 |
|
| 469 |
const float r1 = 0.00001f; |
| 470 |
const float r2 = 1.0f; |
| 471 |
|
| 472 |
V3f p1 = box.min + r2 * hollowSphereRand<V3f> (random); |
| 473 |
V3f p2; |
| 474 |
float r3; |
| 475 |
|
| 476 |
do |
| 477 |
{ |
| 478 |
do |
| 479 |
{ |
| 480 |
p2 = box.min + r2 * hollowSphereRand<V3f> (random); |
| 481 |
} while (approximatelyEqual (p1, p2, e)); |
| 482 |
|
| 483 |
V3f d1 = (p2 - p1).normalized(); |
| 484 |
V3f d2 = (box.min - p1); |
| 485 |
r3 = (d2 - d1 * (d1 ^ d2)).length(); |
| 486 |
} while (r3 < r1); |
| 487 |
|
| 488 |
Line3f ray (p1, p2); |
| 489 |
V3f ip; |
| 490 |
|
| 491 |
assert (!intersects (box, ray, ip)); |
| 492 |
} |
| 493 |
|
| 494 |
return; |
| 495 |
} |
| 496 |
|
| 497 |
cout << " ray starts inside box" << endl; |
| 498 |
|
| 499 |
for (int i = 0; i < 1000; ++i) |
| 500 |
{ |
| 501 |
V3f p1 ( |
| 502 |
float(random.nextf (box.min.x, box.max.x)), |
| 503 |
float(random.nextf (box.min.y, box.max.y)), |
| 504 |
float(random.nextf (box.min.z, box.max.z))); |
| 505 |
|
| 506 |
V3f p2 (p1 + hollowSphereRand<V3f> (random)); |
| 507 |
|
| 508 |
V3f ip; |
| 509 |
bool b = intersects (box, Line3f (p1, p2), ip); |
| 510 |
|
| 511 |
assert (b && ip == p1); |
| 512 |
} |
| 513 |
|
| 514 |
cout << " ray starts outside box, intersects" << endl; |
| 515 |
|
| 516 |
Box3f bigBox (box.min - (box.max - box.min), box.max + (box.max - box.min)); |
| 517 |
|
| 518 |
for (int i = 0; i < 100000; ++i) |
| 519 |
{ |
| 520 |
// |
| 521 |
// Find starting point outside the box, end point inside the box |
| 522 |
// |
| 523 |
|
| 524 |
V3f p1; |
| 525 |
|
| 526 |
do |
| 527 |
{ |
| 528 |
p1 = V3f ( |
| 529 |
float(random.nextf (bigBox.min.x, bigBox.max.x)), |
| 530 |
float(random.nextf (bigBox.min.y, bigBox.max.y)), |
| 531 |
float(random.nextf (bigBox.min.z, bigBox.max.z))); |
| 532 |
} while (box.intersects (p1)); |
| 533 |
|
| 534 |
V3f p2; |
| 535 |
|
| 536 |
do |
| 537 |
{ |
| 538 |
p2 = V3f ( |
| 539 |
float(random.nextf (box.min.x, box.max.x)), |
| 540 |
float(random.nextf (box.min.y, box.max.y)), |
| 541 |
float(random.nextf (box.min.z, box.max.z))); |
| 542 |
} while (approximatelyEqual (p1, p2, e)); |
| 543 |
|
| 544 |
Line3f ray (p1, p2); |
| 545 |
|
| 546 |
V3f ip; |
| 547 |
bool b = intersects (box, ray, ip); |
| 548 |
|
| 549 |
// |
| 550 |
// Ray and box must intersect, intersection point |
| 551 |
// must be on the surface of the box. |
| 552 |
// |
| 553 |
|
| 554 |
assert (b); |
| 555 |
|
| 556 |
assert (ip.x == box.min.x || ip.x == box.max.x || ip.y == box.min.y || ip.y == box.max.y || |
| 557 |
ip.z == box.min.z || ip.z == box.max.z); |
| 558 |
|
| 559 |
// |
| 560 |
// Intersection point must be consistent with the origin |
| 561 |
// and direction of the ray |
| 562 |
// |
| 563 |
|
| 564 |
if (ip.x == box.min.x) |
| 565 |
assert (ray.pos.x <= box.min.x && ray.dir.x >= 0); |
| 566 |
|
| 567 |
if (ip.x == box.max.x) |
| 568 |
assert (ray.pos.x >= box.max.x && ray.dir.x <= 0); |
| 569 |
|
| 570 |
if (ip.y == box.min.y) |
| 571 |
assert (ray.pos.y <= box.min.y && ray.dir.y >= 0); |
| 572 |
|
| 573 |
if (ip.y == box.max.y) |
| 574 |
assert (ray.pos.y >= box.max.y && ray.dir.y <= 0); |
| 575 |
|
| 576 |
if (ip.z == box.min.z) |
| 577 |
assert (ray.pos.z <= box.min.z && ray.dir.z >= 0); |
| 578 |
|
| 579 |
if (ip.z == box.max.z) |
| 580 |
assert (ray.pos.z >= box.max.z && ray.dir.z <= 0); |
| 581 |
|
| 582 |
// |
| 583 |
// Intersection point must be approximately on the ray |
| 584 |
// How far it can be off depends on how far p1 and ip |
| 585 |
// are from the origin. |
| 586 |
// |
| 587 |
|
| 588 |
V3f p3 = p1 + ray.dir * (ray.dir ^ (ip - p1)); |
| 589 |
float m = 0; |
| 590 |
|
| 591 |
for (int j = 0; j < 3; ++j) |
| 592 |
{ |
| 593 |
m = max (abs (p1[j]), m); |
| 594 |
m = max (abs (ip[j]), m); |
| 595 |
} |
| 596 |
|
| 597 |
float err = 30 * m * std::numeric_limits<float>::epsilon(); |
| 598 |
assert (p3.equalWithAbsError (ip, err)); |
| 599 |
|
| 600 |
// |
| 601 |
// Try same starting point, opposite direction |
| 602 |
// |
| 603 |
|
| 604 |
ray.dir *= -1; |
| 605 |
V3f ip2; |
| 606 |
|
| 607 |
assert (!intersects (box, ray, ip2)); |
| 608 |
} |
| 609 |
|
| 610 |
cout << " ray starts outside box, does not intersect" << endl; |
| 611 |
|
| 612 |
V3f center = (box.min + box.max) * 0.5f; |
| 613 |
float r1 = (box.max - box.min).length() * 0.51f; |
| 614 |
float r2 = 2 * r1; |
| 615 |
|
| 616 |
for (int i = 0; i < 100000; ++i) |
| 617 |
{ |
| 618 |
// |
| 619 |
// The ray starts at a distance of r2 from the center |
| 620 |
// of the box, and it passes the center at a minimum |
| 621 |
// distance of r1. (r1 and r2 are both greater than |
| 622 |
// the distance between the center and the corners |
| 623 |
// of the box.) |
| 624 |
// |
| 625 |
|
| 626 |
V3f p1 = center + r2 * hollowSphereRand<V3f> (random); |
| 627 |
V3f p2; |
| 628 |
float r3; |
| 629 |
|
| 630 |
do |
| 631 |
{ |
| 632 |
do |
| 633 |
{ |
| 634 |
p2 = center + r2 * hollowSphereRand<V3f> (random); |
| 635 |
} while (approximatelyEqual (p1, p2, e)); |
| 636 |
|
| 637 |
V3f d1 = (p2 - p1).normalized(); |
| 638 |
V3f d2 = (center - p1); |
| 639 |
r3 = (d2 - d1 * (d1 ^ d2)).length(); |
| 640 |
} while (r3 < r1); |
| 641 |
|
| 642 |
Line3f ray (p1, p2); |
| 643 |
V3f ip; |
| 644 |
|
| 645 |
assert (!intersects (box, ray, ip)); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
void |
| 650 |
rayBoxIntersection1() |
| 651 |
{ |
| 652 |
cout << " ray-box intersection, random rays" << endl; |
| 653 |
|
| 654 |
Box3f boxes[] = { // Boxes with a positive volume |
| 655 |
|
| 656 |
Box3f (V3f (-1, -1, -1), V3f (1, 1, 1)), |
| 657 |
Box3f (V3f (10, 20, 30), V3f (1010, 21, 31)), |
| 658 |
Box3f (V3f (10, 20, 30), V3f (11, 1020, 31)), |
| 659 |
Box3f (V3f (10, 20, 30), V3f (11, 21, 1030)), |
| 660 |
Box3f (V3f (-1e10f, -2e10f, -3e10f), V3f (5e15f, 6e15f, 7e15f)), |
| 661 |
|
| 662 |
// Non-empty, zero-volume boxes |
| 663 |
|
| 664 |
Box3f (V3f (1, 1, 1), V3f (2, 1, 1)), |
| 665 |
Box3f (V3f (1, 1, 1), V3f (1, 2, 1)), |
| 666 |
Box3f (V3f (1, 1, 1), V3f (1, 1, 2)), |
| 667 |
Box3f (V3f (1, 1, 1), V3f (1, 2, 3)), |
| 668 |
Box3f (V3f (1, 1, 1), V3f (2, 3, 1)), |
| 669 |
Box3f (V3f (1, 1, 1), V3f (2, 1, 3)), |
| 670 |
Box3f (V3f (-1, -2, 1), V3f (-1, -2, 1)), |
| 671 |
Box3f (V3f (1, 1, 1), V3f (1, 1, 1)), |
| 672 |
Box3f (V3f (0, 0, 0), V3f (0, 0, 0)), |
| 673 |
|
| 674 |
// empty box |
| 675 |
|
| 676 |
Box3f() |
| 677 |
}; |
| 678 |
|
| 679 |
for (size_t i = 0; i < sizeof (boxes) / sizeof (boxes[0]); ++i) |
| 680 |
testRayBoxIntersection (boxes[i]); |
| 681 |
} |
| 682 |
|
| 683 |
void |
| 684 |
testPerturbedRayBox (const Box3f& box, const Line3f& ray, bool result) |
| 685 |
{ |
| 686 |
cout << " dir ~ " << ray.dir << ", result = " << result << endl; |
| 687 |
|
| 688 |
{ |
| 689 |
V3f ip; |
| 690 |
assert (result == intersects (box, ray, ip)); |
| 691 |
} |
| 692 |
|
| 693 |
Rand48 random (19); |
| 694 |
const float e = 1e-25f; |
| 695 |
|
| 696 |
for (int i = 0; i < 10000; ++i) |
| 697 |
{ |
| 698 |
Line3f ray1 (ray); |
| 699 |
ray1.dir += e * solidSphereRand<V3f> (random); |
| 700 |
|
| 701 |
V3f ip; |
| 702 |
assert (result == intersects (box, ray1, ip)); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
void |
| 707 |
rayBoxIntersection2() |
| 708 |
{ |
| 709 |
|
| 710 |
cout << " ray-box intersection, nearly axis-parallel rays" << endl; |
| 711 |
|
| 712 |
Box3f box (V3f (-1e15f, -1e15f, -1e15f), V3f (1e15f, 1e15f, 1e15f)); |
| 713 |
Line3f ray; |
| 714 |
V3f ip; |
| 715 |
bool b; |
| 716 |
|
| 717 |
ray = Line3f (V3f (-2e15f, 0, 0), V3f (2e15f, 0, 0)); |
| 718 |
b = intersects (box, ray, ip); |
| 719 |
assert (b && ip == V3f (-1e15f, 0, 0)); |
| 720 |
testPerturbedRayBox (box, ray, true); |
| 721 |
|
| 722 |
ray = Line3f (V3f (2e15f, 0, 0), V3f (-2e15f, 0, 0)); |
| 723 |
b = intersects (box, ray, ip); |
| 724 |
assert (b && ip == V3f (1e15f, 0, 0)); |
| 725 |
testPerturbedRayBox (box, ray, true); |
| 726 |
|
| 727 |
ray = Line3f (V3f (-2e15f, 2e15f, 0), V3f (2e15f, 2e15f, 0)); |
| 728 |
b = intersects (box, ray, ip); |
| 729 |
assert (!b); |
| 730 |
testPerturbedRayBox (box, ray, false); |
| 731 |
|
| 732 |
ray = Line3f (V3f (2e15f, 2e15f, 0), V3f (-2e15f, 2e15f, 0)); |
| 733 |
b = intersects (box, ray, ip); |
| 734 |
assert (!b); |
| 735 |
testPerturbedRayBox (box, ray, false); |
| 736 |
|
| 737 |
ray = Line3f (V3f (0, -2e15f, 0), V3f (0, 2e15f, 0)); |
| 738 |
b = intersects (box, ray, ip); |
| 739 |
assert (b && ip == V3f (0, -1e15f, 0)); |
| 740 |
testPerturbedRayBox (box, ray, true); |
| 741 |
|
| 742 |
ray = Line3f (V3f (0, 2e15f, 0), V3f (0, -2e15f, 0)); |
| 743 |
b = intersects (box, ray, ip); |
| 744 |
assert (b && ip == V3f (0, 1e15f, 0)); |
| 745 |
testPerturbedRayBox (box, ray, true); |
| 746 |
|
| 747 |
ray = Line3f (V3f (0, -2e15f, 2e15f), V3f (0, 2e15f, 2e15f)); |
| 748 |
b = intersects (box, ray, ip); |
| 749 |
assert (!b); |
| 750 |
testPerturbedRayBox (box, ray, false); |
| 751 |
|
| 752 |
ray = Line3f (V3f (0, 2e15f, 2e15f), V3f (0, -2e15f, 2e15f)); |
| 753 |
b = intersects (box, ray, ip); |
| 754 |
assert (!b); |
| 755 |
testPerturbedRayBox (box, ray, false); |
| 756 |
|
| 757 |
ray = Line3f (V3f (0, 0, -2e15f), V3f (0, 0, 2e15f)); |
| 758 |
b = intersects (box, ray, ip); |
| 759 |
assert (b && ip == V3f (0, 0, -1e15f)); |
| 760 |
testPerturbedRayBox (box, ray, true); |
| 761 |
|
| 762 |
ray = Line3f (V3f (0, 0, 2e15f), V3f (0, 0, -2e15f)); |
| 763 |
b = intersects (box, ray, ip); |
| 764 |
assert (b && ip == V3f (0, 0, 1e15f)); |
| 765 |
testPerturbedRayBox (box, ray, true); |
| 766 |
|
| 767 |
ray = Line3f (V3f (2e15f, 0, -2e15f), V3f (2e15f, 0, 2e15f)); |
| 768 |
b = intersects (box, ray, ip); |
| 769 |
assert (!b); |
| 770 |
testPerturbedRayBox (box, ray, false); |
| 771 |
|
| 772 |
ray = Line3f (V3f (2e15f, 0, 2e15f), V3f (2e15f, 0, -2e15f)); |
| 773 |
b = intersects (box, ray, ip); |
| 774 |
assert (!b); |
| 775 |
testPerturbedRayBox (box, ray, false); |
| 776 |
} |
| 777 |
|
| 778 |
Box3f |
| 779 |
transformSimple (const Box3f& b, const M44f& M) |
| 780 |
{ |
| 781 |
Box3f b1; |
| 782 |
|
| 783 |
for (int i = 0; i < 8; ++i) |
| 784 |
{ |
| 785 |
V3f p; |
| 786 |
|
| 787 |
for (int j = 0; j < 3; ++j) |
| 788 |
p[j] = ((i >> j) & 1) ? b.max[j] : b.min[j]; |
| 789 |
|
| 790 |
b1.extendBy (p * M); |
| 791 |
} |
| 792 |
|
| 793 |
return b1; |
| 794 |
} |
| 795 |
|
| 796 |
void |
| 797 |
boxMatrixTransform() |
| 798 |
{ |
| 799 |
cout << " transform box by matrix" << endl; |
| 800 |
|
| 801 |
const float e = 5 * std::numeric_limits<float>::epsilon(); |
| 802 |
|
| 803 |
Box3f b1 (V3f (4, 5, 6), V3f (7, 8, 9)); |
| 804 |
|
| 805 |
M44f M; |
| 806 |
M.setEulerAngles (V3f (1, 2, 3)); |
| 807 |
M.translate (V3f (20, -15, 2)); |
| 808 |
|
| 809 |
Box3f b2 = transform (b1, M); |
| 810 |
Box3f b3 = affineTransform (b1, M); |
| 811 |
Box3f b4 = transformSimple (b1, M); |
| 812 |
Box3f b21; |
| 813 |
Box3f b31; |
| 814 |
|
| 815 |
transform (b1, M, b21); |
| 816 |
affineTransform (b1, M, b31); |
| 817 |
|
| 818 |
assert (approximatelyEqual (b2.min, b4.min, e)); |
| 819 |
assert (approximatelyEqual (b2.max, b4.max, e)); |
| 820 |
assert (approximatelyEqual (b3.max, b4.max, e)); |
| 821 |
assert (approximatelyEqual (b3.max, b4.max, e)); |
| 822 |
|
| 823 |
assert (b21 == b2); |
| 824 |
assert (b31 == b3); |
| 825 |
|
| 826 |
M[0][3] = 1; |
| 827 |
M[1][3] = 2; |
| 828 |
M[2][3] = 3; |
| 829 |
M[3][3] = 4; |
| 830 |
|
| 831 |
Box3f b5 = transform (b1, M); |
| 832 |
Box3f b6 = transformSimple (b1, M); |
| 833 |
Box3f b51; |
| 834 |
|
| 835 |
transform (b1, M, b51); |
| 836 |
|
| 837 |
assert (approximatelyEqual (b5.min, b6.min, e)); |
| 838 |
assert (approximatelyEqual (b5.max, b6.max, e)); |
| 839 |
assert (b51 == b5); |
| 840 |
|
| 841 |
Box3f b7; |
| 842 |
Box3f r; |
| 843 |
b7.makeEmpty(); |
| 844 |
assert (b7.isEmpty() && !b7.isInfinite()); |
| 845 |
affineTransform (b7, M, r); |
| 846 |
assert (r.isEmpty()); |
| 847 |
|
| 848 |
Box3f b8; |
| 849 |
b8.makeInfinite(); |
| 850 |
assert (b8.isInfinite() && !b8.isEmpty()); |
| 851 |
affineTransform (b8, M, r); |
| 852 |
assert (r.isInfinite()); |
| 853 |
} |
| 854 |
|
| 855 |
void |
| 856 |
pointInAndOnBox() |
| 857 |
{ |
| 858 |
cout << " closest points in and on box" << endl; |
| 859 |
|
| 860 |
Box3f box (V3f (1, 2, 3), V3f (5, 4, 6)); |
| 861 |
|
| 862 |
// |
| 863 |
// Points outside the box |
| 864 |
// |
| 865 |
|
| 866 |
assert (closestPointOnBox (V3f (0, 0, 0), box) == V3f (1, 2, 3)); |
| 867 |
assert (closestPointInBox (V3f (0, 0, 0), box) == V3f (1, 2, 3)); |
| 868 |
assert (closestPointOnBox (V3f (7, 7, 7), box) == V3f (5, 4, 6)); |
| 869 |
assert (closestPointInBox (V3f (7, 7, 7), box) == V3f (5, 4, 6)); |
| 870 |
|
| 871 |
assert (closestPointOnBox (V3f (2, 3, 0), box) == V3f (2, 3, 3)); |
| 872 |
assert (closestPointInBox (V3f (2, 3, 0), box) == V3f (2, 3, 3)); |
| 873 |
assert (closestPointOnBox (V3f (2, 3, 7), box) == V3f (2, 3, 6)); |
| 874 |
assert (closestPointInBox (V3f (2, 3, 7), box) == V3f (2, 3, 6)); |
| 875 |
|
| 876 |
assert (closestPointOnBox (V3f (2, 0, 4), box) == V3f (2, 2, 4)); |
| 877 |
assert (closestPointInBox (V3f (2, 0, 4), box) == V3f (2, 2, 4)); |
| 878 |
assert (closestPointOnBox (V3f (2, 7, 4), box) == V3f (2, 4, 4)); |
| 879 |
assert (closestPointInBox (V3f (2, 7, 4), box) == V3f (2, 4, 4)); |
| 880 |
|
| 881 |
assert (closestPointOnBox (V3f (0, 3, 4), box) == V3f (1, 3, 4)); |
| 882 |
assert (closestPointInBox (V3f (0, 3, 4), box) == V3f (1, 3, 4)); |
| 883 |
assert (closestPointOnBox (V3f (7, 3, 4), box) == V3f (5, 3, 4)); |
| 884 |
assert (closestPointInBox (V3f (7, 3, 4), box) == V3f (5, 3, 4)); |
| 885 |
|
| 886 |
// |
| 887 |
// Points inside the box |
| 888 |
// |
| 889 |
|
| 890 |
assert (closestPointOnBox (V3f (1.5, 3, 5), box) == V3f (1, 3, 5)); |
| 891 |
assert (closestPointInBox (V3f (1.5, 3, 5), box) == V3f (1.5, 3, 5)); |
| 892 |
assert (closestPointOnBox (V3f (4.5, 3, 5), box) == V3f (5, 3, 5)); |
| 893 |
assert (closestPointInBox (V3f (4.5, 3, 5), box) == V3f (4.5, 3, 5)); |
| 894 |
|
| 895 |
assert (closestPointOnBox (V3f (2, 2.5, 4), box) == V3f (2, 2, 4)); |
| 896 |
assert (closestPointInBox (V3f (2, 2.5, 4), box) == V3f (2, 2.5, 4)); |
| 897 |
assert (closestPointOnBox (V3f (2, 3.5, 4), box) == V3f (2, 4, 4)); |
| 898 |
assert (closestPointInBox (V3f (2, 3.5, 4), box) == V3f (2, 3.5, 4)); |
| 899 |
|
| 900 |
assert (closestPointOnBox (V3f (2, 3, 3.5), box) == V3f (2, 3, 3)); |
| 901 |
assert (closestPointInBox (V3f (2, 3, 3.5), box) == V3f (2, 3, 3.5)); |
| 902 |
assert (closestPointOnBox (V3f (2, 3, 5.5), box) == V3f (2, 3, 6)); |
| 903 |
assert (closestPointInBox (V3f (2, 3, 5.5), box) == V3f (2, 3, 5.5)); |
| 904 |
|
| 905 |
// |
| 906 |
// Point at the center of the box. "Closest point on box" is |
| 907 |
// in at the center of +Y side |
| 908 |
// |
| 909 |
|
| 910 |
assert (closestPointOnBox (V3f (3, 3, 4.5), box) == V3f (3, 4, 4.5)); |
| 911 |
assert (closestPointInBox (V3f (3, 3, 4.5), box) == V3f (3, 3, 4.5)); |
| 912 |
} |
| 913 |
|
| 914 |
} // namespace |
| 915 |
|
| 916 |
void |
| 917 |
testBoxAlgo() |
| 918 |
{ |
| 919 |
cout << "Testing box algorithms" << endl; |
| 920 |
|
| 921 |
entryAndExitPoints1(); |
| 922 |
entryAndExitPoints2(); |
| 923 |
rayBoxIntersection1(); |
| 924 |
rayBoxIntersection2(); |
| 925 |
boxMatrixTransform(); |
| 926 |
pointInAndOnBox(); |
| 927 |
|
| 928 |
cout << "ok\n" << endl; |
| 929 |
} |