ImathBoxAlgo.h 23.8 KB
Newer Older
1 2 3 4
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2002-2010, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
5
//
6
// All rights reserved.
7
//
8 9 10 11 12 13 14 15 16 17 18
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// *       Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// *       Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// *       Neither the name of Industrial Light & Magic nor the names of
// its contributors may be used to endorse or promote products derived
19 20
// from this software without specific prior written permission.
//
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
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////



#ifndef INCLUDED_IMATHBOXALGO_H
#define INCLUDED_IMATHBOXALGO_H


//---------------------------------------------------------------------------
//
//	This file contains algorithms applied to or in conjunction
//	with bounding boxes (Imath::Box). These algorithms require
//	more headers to compile. The assumption made is that these
//	functions are called much less often than the basic box
//	functions or these functions require more support classes.
//
//	Contains:
//
//	T clip<T>(const T& in, const Box<T>& box)
//
//	Vec3<T> closestPointOnBox(const Vec3<T>&, const Box<Vec3<T>>& )
//
//	Vec3<T> closestPointInBox(const Vec3<T>&, const Box<Vec3<T>>& )
//
//	Box< Vec3<S> > transform(const Box<Vec3<S>>&, const Matrix44<T>&)
//	Box< Vec3<S> > affineTransform(const Box<Vec3<S>>&, const Matrix44<T>&)
//
//	void transform(const Box<Vec3<S>>&, const Matrix44<T>&, Box<V3ec3<S>>&)
//	void affineTransform(const Box<Vec3<S>>&,
//                           const Matrix44<T>&,
//                           Box<V3ec3<S>>&)
//
//	bool findEntryAndExitPoints(const Line<T> &line,
//				    const Box< Vec3<T> > &box,
//				    Vec3<T> &enterPoint,
//				    Vec3<T> &exitPoint)
//
70 71
//	bool intersects(const Box<Vec3<T>> &box,
//			const Line3<T> &ray,
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
//			Vec3<T> intersectionPoint)
//
//	bool intersects(const Box<Vec3<T>> &box, const Line3<T> &ray)
//
//---------------------------------------------------------------------------

#include "ImathBox.h"
#include "ImathMatrix.h"
#include "ImathLineAlgo.h"
#include "ImathPlane.h"

namespace Imath {


template <class T>
inline T
clip (const T &p, const Box<T> &box)
{
    //
    // Clip the coordinates of a point, p, against a box.
    // The result, q, is the closest point to p that is inside the box.
    //

    T q;

    for (int i = 0; i < int (box.min.dimensions()); i++)
    {
99 100 101 102 103 104
    if (p[i] < box.min[i])
        q[i] = box.min[i];
    else if (p[i] > box.max[i])
        q[i] = box.max[i];
    else
        q[i] = p[i];
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
    }

    return q;
}


template <class T>
inline T
closestPointInBox (const T &p, const Box<T> &box)
{
    return clip (p, box);
}


template <class T>
Vec3<T>
closestPointOnBox (const Vec3<T> &p, const Box< Vec3<T> > &box)
{
    //
    // Find the point, q, on the surface of
    // the box, that is closest to point p.
    //
    // If the box is empty, return p.
    //

    if (box.isEmpty())
131
    return p;
132 133 134 135 136

    Vec3<T> q = closestPointInBox (p, box);

    if (q == p)
    {
137 138
    Vec3<T> d1 = p - box.min;
    Vec3<T> d2 = box.max - p;
139

140 141 142
    Vec3<T> d ((d1.x < d2.x)? d1.x: d2.x,
           (d1.y < d2.y)? d1.y: d2.y,
           (d1.z < d2.z)? d1.z: d2.z);
143

144 145 146 147 148 149 150 151 152 153 154 155
    if (d.x < d.y && d.x < d.z)
    {
        q.x = (d1.x < d2.x)? box.min.x: box.max.x;
    }
    else if (d.y < d.z)
    {
        q.y = (d1.y < d2.y)? box.min.y: box.max.y;
    }
    else
    {
        q.z = (d1.z < d2.z)? box.min.z: box.max.z;
    }
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
    }

    return q;
}


template <class S, class T>
Box< Vec3<S> >
transform (const Box< Vec3<S> > &box, const Matrix44<T> &m)
{
    //
    // Transform a 3D box by a matrix, and compute a new box that
    // tightly encloses the transformed box.
    //
    // If m is an affine transform, then we use James Arvo's fast
    // method as described in "Graphics Gems", Academic Press, 1990,
    // pp. 548-550.
    //

    //
    // A transformed empty box is still empty, and a transformed infinite box
    // is still infinite
    //

    if (box.isEmpty() || box.isInfinite())
181
    return box;
182 183 184 185 186 187 188 189

    //
    // If the last column of m is (0 0 0 1) then m is an affine
    // transform, and we use the fast Graphics Gems trick.
    //

    if (m[0][3] == 0 && m[1][3] == 0 && m[2][3] == 0 && m[3][3] == 1)
    {
190
    Box< Vec3<S> > newBox;
191

192
    for (int i = 0; i < 3; i++)
193
        {
194
        newBox.min[i] = newBox.max[i] = (S) m[3][i];
195

196
        for (int j = 0; j < 3; j++)
197
            {
198
        S a, b;
199

200 201
        a = (S) m[j][i] * box.min[j];
        b = (S) m[j][i] * box.max[j];
202

203
        if (a < b)
204
                {
205 206 207 208
            newBox.min[i] += a;
            newBox.max[i] += b;
        }
        else
209
                {
210 211 212 213 214
            newBox.min[i] += b;
            newBox.max[i] += a;
        }
        }
    }
215

216
    return newBox;
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    }

    //
    // M is a projection matrix.  Do things the naive way:
    // Transform the eight corners of the box, and find an
    // axis-parallel box that encloses the transformed corners.
    //

    Vec3<S> points[8];

    points[0][0] = points[1][0] = points[2][0] = points[3][0] = box.min[0];
    points[4][0] = points[5][0] = points[6][0] = points[7][0] = box.max[0];

    points[0][1] = points[1][1] = points[4][1] = points[5][1] = box.min[1];
    points[2][1] = points[3][1] = points[6][1] = points[7][1] = box.max[1];

    points[0][2] = points[2][2] = points[4][2] = points[6][2] = box.min[2];
    points[1][2] = points[3][2] = points[5][2] = points[7][2] = box.max[2];

    Box< Vec3<S> > newBox;

238 239
    for (int i = 0; i < 8; i++)
    newBox.extendBy (points[i] * m);
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

    return newBox;
}

template <class S, class T>
void
transform (const Box< Vec3<S> > &box,
           const Matrix44<T>    &m,
           Box< Vec3<S> >       &result)
{
    //
    // Transform a 3D box by a matrix, and compute a new box that
    // tightly encloses the transformed box.
    //
    // If m is an affine transform, then we use James Arvo's fast
    // method as described in "Graphics Gems", Academic Press, 1990,
    // pp. 548-550.
    //

    //
    // A transformed empty box is still empty, and a transformed infinite
    // box is still infinite
    //

    if (box.isEmpty() || box.isInfinite())
    {
266
    return;
267 268 269 270 271 272 273 274 275
    }

    //
    // If the last column of m is (0 0 0 1) then m is an affine
    // transform, and we use the fast Graphics Gems trick.
    //

    if (m[0][3] == 0 && m[1][3] == 0 && m[2][3] == 0 && m[3][3] == 1)
    {
276
    for (int i = 0; i < 3; i++)
277
        {
278
        result.min[i] = result.max[i] = (S) m[3][i];
279

280
        for (int j = 0; j < 3; j++)
281
            {
282
        S a, b;
283

284 285
        a = (S) m[j][i] * box.min[j];
        b = (S) m[j][i] * box.max[j];
286

287
        if (a < b)
288
                {
289 290 291 292
            result.min[i] += a;
            result.max[i] += b;
        }
        else
293
                {
294 295 296 297 298
            result.min[i] += b;
            result.max[i] += a;
        }
        }
    }
299

300
    return;
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    }

    //
    // M is a projection matrix.  Do things the naive way:
    // Transform the eight corners of the box, and find an
    // axis-parallel box that encloses the transformed corners.
    //

    Vec3<S> points[8];

    points[0][0] = points[1][0] = points[2][0] = points[3][0] = box.min[0];
    points[4][0] = points[5][0] = points[6][0] = points[7][0] = box.max[0];

    points[0][1] = points[1][1] = points[4][1] = points[5][1] = box.min[1];
    points[2][1] = points[3][1] = points[6][1] = points[7][1] = box.max[1];

    points[0][2] = points[2][2] = points[4][2] = points[6][2] = box.min[2];
    points[1][2] = points[3][2] = points[5][2] = points[7][2] = box.max[2];

320 321
    for (int i = 0; i < 8; i++)
    result.extendBy (points[i] * m);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
}


template <class S, class T>
Box< Vec3<S> >
affineTransform (const Box< Vec3<S> > &box, const Matrix44<T> &m)
{
    //
    // Transform a 3D box by a matrix whose rightmost column
    // is (0 0 0 1), and compute a new box that tightly encloses
    // the transformed box.
    //
    // As in the transform() function, above, we use James Arvo's
    // fast method.
    //

    if (box.isEmpty() || box.isInfinite())
    {
340 341 342
    //
    // A transformed empty or infinite box is still empty or infinite
    //
343

344
    return box;
345 346 347 348
    }

    Box< Vec3<S> > newBox;

349
    for (int i = 0; i < 3; i++)
350
    {
351
    newBox.min[i] = newBox.max[i] = (S) m[3][i];
352

353 354 355
    for (int j = 0; j < 3; j++)
    {
        S a, b;
356

357 358
        a = (S) m[j][i] * box.min[j];
        b = (S) m[j][i] * box.max[j];
359

360 361 362 363 364 365 366 367 368 369 370
        if (a < b)
        {
        newBox.min[i] += a;
        newBox.max[i] += b;
        }
        else
        {
        newBox.min[i] += b;
        newBox.max[i] += a;
        }
    }
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    }

    return newBox;
}

template <class S, class T>
void
affineTransform (const Box< Vec3<S> > &box,
                 const Matrix44<T>    &m,
                 Box<Vec3<S> >        &result)
{
    //
    // Transform a 3D box by a matrix whose rightmost column
    // is (0 0 0 1), and compute a new box that tightly encloses
    // the transformed box.
    //
    // As in the transform() function, above, we use James Arvo's
    // fast method.
    //

    if (box.isEmpty())
    {
393 394 395
    //
    // A transformed empty box is still empty
    //
396
        result.makeEmpty();
397
    return;
398 399 400 401
    }

    if (box.isInfinite())
    {
402 403 404
    //
    // A transformed infinite box is still infinite
    //
405
        result.makeInfinite();
406
    return;
407 408
    }

409
    for (int i = 0; i < 3; i++)
410
    {
411
    result.min[i] = result.max[i] = (S) m[3][i];
412

413 414 415
    for (int j = 0; j < 3; j++)
    {
        S a, b;
416

417 418
        a = (S) m[j][i] * box.min[j];
        b = (S) m[j][i] * box.max[j];
419

420 421 422 423 424 425 426 427 428 429 430
        if (a < b)
        {
        result.min[i] += a;
        result.max[i] += b;
        }
        else
        {
        result.min[i] += b;
        result.max[i] += a;
        }
    }
431 432 433 434 435 436 437
    }
}


template <class T>
bool
findEntryAndExitPoints (const Line3<T> &r,
438 439 440
            const Box<Vec3<T> > &b,
            Vec3<T> &entry,
            Vec3<T> &exit)
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
{
    //
    // Compute the points where a ray, r, enters and exits a box, b:
    //
    // findEntryAndExitPoints() returns
    //
    //     - true if the ray starts inside the box or if the
    //       ray starts outside and intersects the box
    //
    //	   - false otherwise (that is, if the ray does not
    //       intersect the box)
    //
    // The entry and exit points are
    //
    //     - points on two of the faces of the box when
    //       findEntryAndExitPoints() returns true
    //       (The entry end exit points may be on either
    //       side of the ray's origin)
    //
    //     - undefined when findEntryAndExitPoints()
    //       returns false
    //

    if (b.isEmpty())
    {
466 467 468
    //
    // No ray intersects an empty box
    //
469

470
    return false;
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    }

    //
    // The following description assumes that the ray's origin is outside
    // the box, but the code below works even if the origin is inside the
    // box:
    //
    // Between one and three "frontfacing" sides of the box are oriented
    // towards the ray's origin, and between one and three "backfacing"
    // sides are oriented away from the ray's origin.
    // We intersect the ray with the planes that contain the sides of the
    // box, and compare the distances between the ray's origin and the
    // ray-plane intersections.  The ray intersects the box if the most
    // distant frontfacing intersection is nearer than the nearest
    // backfacing intersection.  If the ray does intersect the box, then
    // the most distant frontfacing ray-plane intersection is the entry
    // point and the nearest backfacing ray-plane intersection is the
    // exit point.
    //

    const T TMAX = limits<T>::max();

    T tFrontMax = -TMAX;
    T tBackMin = TMAX;

    //
    // Minimum and maximum X sides.
    //

    if (r.dir.x >= 0)
    {
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
    T d1 = b.max.x - r.pos.x;
    T d2 = b.min.x - r.pos.x;

    if (r.dir.x > 1 ||
        (abs (d1) < TMAX * r.dir.x &&
         abs (d2) < TMAX * r.dir.x))
    {
        T t1 = d1 / r.dir.x;
        T t2 = d2 / r.dir.x;

        if (tBackMin > t1)
        {
        tBackMin = t1;

        exit.x = b.max.x;
        exit.y = clamp (r.pos.y + t1 * r.dir.y, b.min.y, b.max.y);
        exit.z = clamp (r.pos.z + t1 * r.dir.z, b.min.z, b.max.z);
        }

        if (tFrontMax < t2)
        {
        tFrontMax = t2;

        entry.x = b.min.x;
        entry.y = clamp (r.pos.y + t2 * r.dir.y, b.min.y, b.max.y);
        entry.z = clamp (r.pos.z + t2 * r.dir.z, b.min.z, b.max.z);
        }
    }
    else if (r.pos.x < b.min.x || r.pos.x > b.max.x)
    {
        return false;
    }
534 535 536
    }
    else // r.dir.x < 0
    {
537 538
    T d1 = b.min.x - r.pos.x;
    T d2 = b.max.x - r.pos.x;
539

540 541 542 543 544 545
    if (r.dir.x < -1 ||
        (abs (d1) < -TMAX * r.dir.x &&
         abs (d2) < -TMAX * r.dir.x))
    {
        T t1 = d1 / r.dir.x;
        T t2 = d2 / r.dir.x;
546

547 548 549
        if (tBackMin > t1)
        {
        tBackMin = t1;
550

551 552 553 554
        exit.x = b.min.x;
        exit.y = clamp (r.pos.y + t1 * r.dir.y, b.min.y, b.max.y);
        exit.z = clamp (r.pos.z + t1 * r.dir.z, b.min.z, b.max.z);
        }
555

556 557 558
        if (tFrontMax < t2)
        {
        tFrontMax = t2;
559

560 561 562 563 564 565 566 567 568
        entry.x = b.max.x;
        entry.y = clamp (r.pos.y + t2 * r.dir.y, b.min.y, b.max.y);
        entry.z = clamp (r.pos.z + t2 * r.dir.z, b.min.z, b.max.z);
        }
    }
    else if (r.pos.x < b.min.x || r.pos.x > b.max.x)
    {
        return false;
    }
569 570 571 572 573 574 575 576
    }

    //
    // Minimum and maximum Y sides.
    //

    if (r.dir.y >= 0)
    {
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    T d1 = b.max.y - r.pos.y;
    T d2 = b.min.y - r.pos.y;

    if (r.dir.y > 1 ||
        (abs (d1) < TMAX * r.dir.y &&
         abs (d2) < TMAX * r.dir.y))
    {
        T t1 = d1 / r.dir.y;
        T t2 = d2 / r.dir.y;

        if (tBackMin > t1)
        {
        tBackMin = t1;

        exit.x = clamp (r.pos.x + t1 * r.dir.x, b.min.x, b.max.x);
        exit.y = b.max.y;
        exit.z = clamp (r.pos.z + t1 * r.dir.z, b.min.z, b.max.z);
        }

        if (tFrontMax < t2)
        {
        tFrontMax = t2;

        entry.x = clamp (r.pos.x + t2 * r.dir.x, b.min.x, b.max.x);
        entry.y = b.min.y;
        entry.z = clamp (r.pos.z + t2 * r.dir.z, b.min.z, b.max.z);
        }
    }
    else if (r.pos.y < b.min.y || r.pos.y > b.max.y)
    {
        return false;
    }
609 610 611
    }
    else // r.dir.y < 0
    {
612 613
    T d1 = b.min.y - r.pos.y;
    T d2 = b.max.y - r.pos.y;
614

615 616 617 618 619 620
    if (r.dir.y < -1 ||
        (abs (d1) < -TMAX * r.dir.y &&
         abs (d2) < -TMAX * r.dir.y))
    {
        T t1 = d1 / r.dir.y;
        T t2 = d2 / r.dir.y;
621

622 623 624
        if (tBackMin > t1)
        {
        tBackMin = t1;
625

626 627 628 629
        exit.x = clamp (r.pos.x + t1 * r.dir.x, b.min.x, b.max.x);
        exit.y = b.min.y;
        exit.z = clamp (r.pos.z + t1 * r.dir.z, b.min.z, b.max.z);
        }
630

631 632 633
        if (tFrontMax < t2)
        {
        tFrontMax = t2;
634

635 636 637 638 639 640 641 642 643
        entry.x = clamp (r.pos.x + t2 * r.dir.x, b.min.x, b.max.x);
        entry.y = b.max.y;
        entry.z = clamp (r.pos.z + t2 * r.dir.z, b.min.z, b.max.z);
        }
    }
    else if (r.pos.y < b.min.y || r.pos.y > b.max.y)
    {
        return false;
    }
644 645 646 647 648 649 650 651
    }

    //
    // Minimum and maximum Z sides.
    //

    if (r.dir.z >= 0)
    {
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
    T d1 = b.max.z - r.pos.z;
    T d2 = b.min.z - r.pos.z;

    if (r.dir.z > 1 ||
        (abs (d1) < TMAX * r.dir.z &&
         abs (d2) < TMAX * r.dir.z))
    {
        T t1 = d1 / r.dir.z;
        T t2 = d2 / r.dir.z;

        if (tBackMin > t1)
        {
        tBackMin = t1;

        exit.x = clamp (r.pos.x + t1 * r.dir.x, b.min.x, b.max.x);
        exit.y = clamp (r.pos.y + t1 * r.dir.y, b.min.y, b.max.y);
        exit.z = b.max.z;
        }

        if (tFrontMax < t2)
        {
        tFrontMax = t2;

        entry.x = clamp (r.pos.x + t2 * r.dir.x, b.min.x, b.max.x);
        entry.y = clamp (r.pos.y + t2 * r.dir.y, b.min.y, b.max.y);
        entry.z = b.min.z;
        }
    }
    else if (r.pos.z < b.min.z || r.pos.z > b.max.z)
    {
        return false;
    }
684 685 686
    }
    else // r.dir.z < 0
    {
687 688
    T d1 = b.min.z - r.pos.z;
    T d2 = b.max.z - r.pos.z;
689

690 691 692 693 694 695
    if (r.dir.z < -1 ||
        (abs (d1) < -TMAX * r.dir.z &&
         abs (d2) < -TMAX * r.dir.z))
    {
        T t1 = d1 / r.dir.z;
        T t2 = d2 / r.dir.z;
696

697 698 699
        if (tBackMin > t1)
        {
        tBackMin = t1;
700

701 702 703 704
        exit.x = clamp (r.pos.x + t1 * r.dir.x, b.min.x, b.max.x);
        exit.y = clamp (r.pos.y + t1 * r.dir.y, b.min.y, b.max.y);
        exit.z = b.min.z;
        }
705

706 707 708
        if (tFrontMax < t2)
        {
        tFrontMax = t2;
709

710 711 712 713 714 715 716 717 718
        entry.x = clamp (r.pos.x + t2 * r.dir.x, b.min.x, b.max.x);
        entry.y = clamp (r.pos.y + t2 * r.dir.y, b.min.y, b.max.y);
        entry.z = b.max.z;
        }
    }
    else if (r.pos.z < b.min.z || r.pos.z > b.max.z)
    {
        return false;
    }
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
    }

    return tFrontMax <= tBackMin;
}


template<class T>
bool
intersects (const Box< Vec3<T> > &b, const Line3<T> &r, Vec3<T> &ip)
{
    //
    // Intersect a ray, r, with a box, b, and compute the intersection
    // point, ip:
    //
    // intersect() returns
    //
    //     - true if the ray starts inside the box or if the
    //       ray starts outside and intersects the box
    //
    //     - false if the ray starts outside the box and intersects it,
    //       but the intersection is behind the ray's origin.
    //
    //     - false if the ray starts outside and does not intersect it
    //
    // The intersection point is
    //
    //     - the ray's origin if the ray starts inside the box
    //
    //     - a point on one of the faces of the box if the ray
    //       starts outside the box
    //
    //     - undefined when intersect() returns false
    //

    if (b.isEmpty())
    {
755 756 757
    //
    // No ray intersects an empty box
    //
758

759
    return false;
760 761 762 763
    }

    if (b.intersects (r.pos))
    {
764 765 766
    //
    // The ray starts inside the box
    //
767

768 769
    ip = r.pos;
    return true;
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    }

    //
    // The ray starts outside the box.  Between one and three "frontfacing"
    // sides of the box are oriented towards the ray, and between one and
    // three "backfacing" sides are oriented away from the ray.
    // We intersect the ray with the planes that contain the sides of the
    // box, and compare the distances between ray's origin and the ray-plane
    // intersections.
    // The ray intersects the box if the most distant frontfacing intersection
    // is nearer than the nearest backfacing intersection.  If the ray does
    // intersect the box, then the most distant frontfacing ray-plane
    // intersection is the ray-box intersection.
    //

    const T TMAX = limits<T>::max();

    T tFrontMax = -1;
    T tBackMin = TMAX;

    //
    // Minimum and maximum X sides.
    //

    if (r.dir.x > 0)
    {
796 797
    if (r.pos.x > b.max.x)
        return false;
798

799
    T d = b.max.x - r.pos.x;
800

801 802 803
    if (r.dir.x > 1 || d < TMAX * r.dir.x)
    {
        T t = d / r.dir.x;
804

805 806 807
        if (tBackMin > t)
        tBackMin = t;
    }
808

809 810 811 812
    if (r.pos.x <= b.min.x)
    {
        T d = b.min.x - r.pos.x;
        T t = (r.dir.x > 1 || d < TMAX * r.dir.x)? d / r.dir.x: TMAX;
813

814 815 816
        if (tFrontMax < t)
        {
        tFrontMax = t;
817

818 819 820 821 822
        ip.x = b.min.x;
        ip.y = clamp (r.pos.y + t * r.dir.y, b.min.y, b.max.y);
        ip.z = clamp (r.pos.z + t * r.dir.z, b.min.z, b.max.z);
        }
    }
823 824 825
    }
    else if (r.dir.x < 0)
    {
826 827
    if (r.pos.x < b.min.x)
        return false;
828

829
    T d = b.min.x - r.pos.x;
830

831 832 833
    if (r.dir.x < -1 || d > TMAX * r.dir.x)
    {
        T t = d / r.dir.x;
834

835 836 837
        if (tBackMin > t)
        tBackMin = t;
    }
838

839 840 841 842
    if (r.pos.x >= b.max.x)
    {
        T d = b.max.x - r.pos.x;
        T t = (r.dir.x < -1 || d > TMAX * r.dir.x)? d / r.dir.x: TMAX;
843

844 845 846
        if (tFrontMax < t)
        {
        tFrontMax = t;
847

848 849 850 851 852
        ip.x = b.max.x;
        ip.y = clamp (r.pos.y + t * r.dir.y, b.min.y, b.max.y);
        ip.z = clamp (r.pos.z + t * r.dir.z, b.min.z, b.max.z);
        }
    }
853 854 855
    }
    else // r.dir.x == 0
    {
856 857
    if (r.pos.x < b.min.x || r.pos.x > b.max.x)
        return false;
858 859 860 861 862 863 864 865
    }

    //
    // Minimum and maximum Y sides.
    //

    if (r.dir.y > 0)
    {
866 867
    if (r.pos.y > b.max.y)
        return false;
868

869
    T d = b.max.y - r.pos.y;
870

871 872 873
    if (r.dir.y > 1 || d < TMAX * r.dir.y)
    {
        T t = d / r.dir.y;
874

875 876 877
        if (tBackMin > t)
        tBackMin = t;
    }
878

879 880 881 882
    if (r.pos.y <= b.min.y)
    {
        T d = b.min.y - r.pos.y;
        T t = (r.dir.y > 1 || d < TMAX * r.dir.y)? d / r.dir.y: TMAX;
883

884 885 886
        if (tFrontMax < t)
        {
        tFrontMax = t;
887

888 889 890 891 892
        ip.x = clamp (r.pos.x + t * r.dir.x, b.min.x, b.max.x);
        ip.y = b.min.y;
        ip.z = clamp (r.pos.z + t * r.dir.z, b.min.z, b.max.z);
        }
    }
893 894 895
    }
    else if (r.dir.y < 0)
    {
896 897
    if (r.pos.y < b.min.y)
        return false;
898

899
    T d = b.min.y - r.pos.y;
900

901 902 903
    if (r.dir.y < -1 || d > TMAX * r.dir.y)
    {
        T t = d / r.dir.y;
904

905 906 907
        if (tBackMin > t)
        tBackMin = t;
    }
908

909 910 911 912
    if (r.pos.y >= b.max.y)
    {
        T d = b.max.y - r.pos.y;
        T t = (r.dir.y < -1 || d > TMAX * r.dir.y)? d / r.dir.y: TMAX;
913

914 915 916 917 918 919 920 921 922
        if (tFrontMax < t)
        {
        tFrontMax = t;

        ip.x = clamp (r.pos.x + t * r.dir.x, b.min.x, b.max.x);
        ip.y = b.max.y;
        ip.z = clamp (r.pos.z + t * r.dir.z, b.min.z, b.max.z);
        }
    }
923 924 925
    }
    else // r.dir.y == 0
    {
926 927
    if (r.pos.y < b.min.y || r.pos.y > b.max.y)
        return false;
928 929 930 931 932 933 934 935
    }

    //
    // Minimum and maximum Z sides.
    //

    if (r.dir.z > 0)
    {
936 937
    if (r.pos.z > b.max.z)
        return false;
938

939
    T d = b.max.z - r.pos.z;
940

941 942 943
    if (r.dir.z > 1 || d < TMAX * r.dir.z)
    {
        T t = d / r.dir.z;
944

945 946 947
        if (tBackMin > t)
        tBackMin = t;
    }
948

949 950 951 952 953 954 955 956
    if (r.pos.z <= b.min.z)
    {
        T d = b.min.z - r.pos.z;
        T t = (r.dir.z > 1 || d < TMAX * r.dir.z)? d / r.dir.z: TMAX;

        if (tFrontMax < t)
        {
        tFrontMax = t;
957

958 959 960 961 962
        ip.x = clamp (r.pos.x + t * r.dir.x, b.min.x, b.max.x);
        ip.y = clamp (r.pos.y + t * r.dir.y, b.min.y, b.max.y);
        ip.z = b.min.z;
        }
    }
963 964 965
    }
    else if (r.dir.z < 0)
    {
966 967
    if (r.pos.z < b.min.z)
        return false;
968

969
    T d = b.min.z - r.pos.z;
970

971 972 973
    if (r.dir.z < -1 || d > TMAX * r.dir.z)
    {
        T t = d / r.dir.z;
974

975 976 977
        if (tBackMin > t)
        tBackMin = t;
    }
978

979 980 981 982
    if (r.pos.z >= b.max.z)
    {
        T d = b.max.z - r.pos.z;
        T t = (r.dir.z < -1 || d > TMAX * r.dir.z)? d / r.dir.z: TMAX;
983

984 985 986 987 988 989 990 991 992
        if (tFrontMax < t)
        {
        tFrontMax = t;

        ip.x = clamp (r.pos.x + t * r.dir.x, b.min.x, b.max.x);
        ip.y = clamp (r.pos.y + t * r.dir.y, b.min.y, b.max.y);
        ip.z = b.max.z;
        }
    }
993 994 995
    }
    else // r.dir.z == 0
    {
996 997
    if (r.pos.z < b.min.z || r.pos.z > b.max.z)
        return false;
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
    }

    return tFrontMax <= tBackMin;
}


template<class T>
bool
intersects (const Box< Vec3<T> > &box, const Line3<T> &ray)
{
    Vec3<T> ignored;
    return intersects (box, ray, ignored);
}


} // namespace Imath

#endif