File: Wm5IntrBox3Sphere3.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 90,124 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 40
file content (681 lines) | stat: -rw-r--r-- 23,419 bytes parent folder | download | duplicates (3)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
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
466
467
468
469
470
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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
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
// 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/07/23)

#include "Wm5MathematicsPCH.h"
#include "Wm5IntrBox3Sphere3.h"

namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
IntrBox3Sphere3<Real>::IntrBox3Sphere3 (const Box3<Real>& box,
    const Sphere3<Real>& sphere)
    :
    mBox(&box),
    mSphere(&sphere)
{
}
//----------------------------------------------------------------------------
template <typename Real>
const Box3<Real>& IntrBox3Sphere3<Real>::GetBox () const
{
    return *mBox;
}
//----------------------------------------------------------------------------
template <typename Real>
const Sphere3<Real>& IntrBox3Sphere3<Real>::GetSphere () const
{
    return *mSphere;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IntrBox3Sphere3<Real>::Test ()
{
    // Test for intersection in the coordinate system of the box by
    // transforming the sphere into that coordinate system.
    Vector3<Real> cdiff = mSphere->Center - mBox->Center;

    Real ax = Math<Real>::FAbs(cdiff.Dot(mBox->Axis[0]));
    Real ay = Math<Real>::FAbs(cdiff.Dot(mBox->Axis[1]));
    Real az = Math<Real>::FAbs(cdiff.Dot(mBox->Axis[2]));
    Real dx = ax - mBox->Extent[0];
    Real dy = ay - mBox->Extent[1];
    Real dz = az - mBox->Extent[2];

    if (ax <= mBox->Extent[0])
    {
        if (ay <= mBox->Extent[1])
        {
            if (az <= mBox->Extent[2])
            {
                // Sphere center inside box.
                return true;
            }
            else
            {
                // Potential sphere-face intersection with face z.
                return dz <= mSphere->Radius;
            }
        }
        else
        {
            if (az <= mBox->Extent[2])
            {
                // Potential sphere-face intersection with face y.
                return dy <= mSphere->Radius;
            }
            else
            {
                // Potential sphere-edge intersection with edge formed
                // by faces y and z.
                Real rsqr = mSphere->Radius*mSphere->Radius;
                return dy*dy + dz*dz <= rsqr;
            }
        }
    }
    else
    {
        if (ay <= mBox->Extent[1])
        {
            if (az <= mBox->Extent[2])
            {
                // Potential sphere-face intersection with face x.
                return dx <= mSphere->Radius;
            }
            else
            {
                // Potential sphere-edge intersection with edge formed
                // by faces x and z.
                Real rsqr = mSphere->Radius*mSphere->Radius;
                return dx*dx + dz*dz <= rsqr;
            }
        }
        else
        {
            if (az <= mBox->Extent[2])
            {
                // Potential sphere-edge intersection with edge formed
                // by faces x and y.
                Real rsqr = mSphere->Radius*mSphere->Radius;
                return dx*dx + dy*dy <= rsqr;
            }
            else
            {
                // Potential sphere-vertex intersection at corner formed
                // by faces x,y,z.
                Real rsqr = mSphere->Radius*mSphere->Radius;
                return dx*dx + dy*dy + dz*dz <= rsqr;
            }
        }
    }
}
//----------------------------------------------------------------------------
template <typename Real>
bool IntrBox3Sphere3<Real>::Find (Real tmax,
    const Vector3<Real>& velocity0, const Vector3<Real>& velocity1)
{
    // Find intersections relative to the coordinate system of the box.
    // The sphere is transformed to the box coordinates and the velocity of
    // the sphere is relative to the box.
    Vector3<Real> cdiff = mSphere->Center - mBox->Center;
    Vector3<Real> relVelocity = velocity1 - velocity0;
    Real ax = cdiff.Dot(mBox->Axis[0]);
    Real ay = cdiff.Dot(mBox->Axis[1]);
    Real az = cdiff.Dot(mBox->Axis[2]);
    Real vx = relVelocity.Dot(mBox->Axis[0]);
    Real vy = relVelocity.Dot(mBox->Axis[1]);
    Real vz = relVelocity.Dot(mBox->Axis[2]);

    // Flip coordinate frame into the first octant.
    int signX = 1;
    if (ax < (Real)0)
    {
        ax = -ax;
        vx = -vx;
        signX = -1;
    }

    int signY = 1;
    if (ay < (Real)0)
    {
        ay = -ay;
        vy = -vy;
        signY = -1;
    }

    int signZ = 1;
    if (az < (Real)0)
    {
        az = -az;
        vz = -vz;
        signZ = -1;
    }

    // Intersection coordinates.
    Real ix, iy, iz;
    int retVal;

    if (ax <= mBox->Extent[0])
    {
        if (ay <= mBox->Extent[1])
        {
            if (az <= mBox->Extent[2])
            {
                // The sphere center is inside box.  Return it as the contact
                // point, but report an "other" intersection type.
                mContactTime = (Real)0;
                mContactPoint = mSphere->Center;
                mIntersectionType = IT_OTHER;
                return true;
            }
            else
            {
                // Sphere above face on axis Z.
                retVal = FindFaceRegionIntersection(mBox->Extent[0], 
                    mBox->Extent[1], mBox->Extent[2], ax, ay, az, vx, vy,
                    vz, ix, iy, iz, true);     
            }
        }
        else
        {
            if (az <= mBox->Extent[2])
            {
                // Sphere above face on axis Y.
                retVal = FindFaceRegionIntersection(mBox->Extent[0], 
                    mBox->Extent[2] ,mBox->Extent[1], ax, az, ay, vx, vz,
                    vy, ix, iz, iy, true);   
            }
            else
            {
                // Sphere is above the edge formed by faces y and z.
                retVal = FindEdgeRegionIntersection(mBox->Extent[1], 
                    mBox->Extent[0], mBox->Extent[2], ay, ax, az, vy, vx,
                    vz, iy, ix, iz, true);
            }
        }
    }
    else
    {
        if (ay <= mBox->Extent[1])
        {
            if (az <= mBox->Extent[2])
            {
                // Sphere above face on axis X.
                retVal = FindFaceRegionIntersection(mBox->Extent[1],
                    mBox->Extent[2], mBox->Extent[0], ay, az, ax, vy, vz,
                    vx, iy, iz, ix, true);
            }
            else
            {
                // Sphere is above the edge formed by faces x and z.
                retVal = FindEdgeRegionIntersection(mBox->Extent[0], 
                    mBox->Extent[1], mBox->Extent[2], ax, ay, az, vx, vy,
                    vz, ix, iy, iz, true);
            }
        }
        else
        {
            if (az <= mBox->Extent[2])
            {
                // Sphere is above the edge formed by faces x and y.
                retVal = FindEdgeRegionIntersection(mBox->Extent[0], 
                    mBox->Extent[2], mBox->Extent[1], ax, az, ay, vx, vz,
                    vy, ix, iz, iy, true);
            }
            else
            {
                // sphere is above the corner formed by faces x,y,z
                retVal = FindVertexRegionIntersection(mBox->Extent[0],
                    mBox->Extent[1], mBox->Extent[2], ax, ay, az, vx, vy,
                    vz, ix, iy, iz);
            }
        }
    }

    if (retVal == 0 || mContactTime > tmax)
    {
        mIntersectionType = IT_EMPTY;
        return false;
    }

    // Calculate actual intersection (move point back into world coordinates).
    mIntersectionType = IT_POINT;
    mContactPoint = mBox->Center + (signX*ix)*mBox->Axis[0] +
        (signY*iy)*mBox->Axis[1] + (signZ*iz)*mBox->Axis[2];
    return true;
}
//----------------------------------------------------------------------------
template <typename Real>
const Vector3<Real>& IntrBox3Sphere3<Real>::GetContactPoint () const
{
    return mContactPoint;
}
//----------------------------------------------------------------------------
template <typename Real>
Real IntrBox3Sphere3<Real>::GetVertexIntersection (Real dx, Real dy,
    Real dz, Real vx, Real vy, Real vz, Real rsqr)
{
    // Finds the time of a 3D line-sphere intersection between a line
    // P = Dt, where P = (dx, dy, dz) and D = (vx, vy, vz) and
    // a sphere of radius^2 rsqr.  Note: only valid if there is, in fact,
    // an intersection.

    Real vsqr = vx*vx + vy*vy + vz*vz;
    Real dot = dx*vx + dy*vy + dz*vz;
    Real diff = dx*dx + dy*dy + dz*dz - rsqr;
    Real inv = Math<Real>::InvSqrt(Math<Real>::FAbs(dot*dot - vsqr*diff));
    return diff*inv/((Real)1 - dot*inv);
}
//----------------------------------------------------------------------------
template <typename Real>
Real IntrBox3Sphere3<Real>::GetEdgeIntersection (Real dx, Real dz, Real vx,
    Real vz, Real vsqr, Real rsqr)
{
    // Finds the time of a 2D line-circle intersection between a line
    // P = Dt where P = (dx,dz) and D = (vx, vz) and a circle of radius^2
    // rsqr.  Note: only valid if there is, in fact, an intersection.

    Real dot = vx*dx + vz*dz;
    Real diff = dx*dx + dz*dz - rsqr;
    Real inv = Math<Real>::InvSqrt(Math<Real>::FAbs(dot*dot - vsqr*diff));
    return diff*inv/((Real)1 - dot*inv);
}
//----------------------------------------------------------------------------
template <typename Real>
int IntrBox3Sphere3<Real>::FindFaceRegionIntersection (Real ex, Real ey,
    Real ez, Real cx, Real cy, Real cz, Real vx, Real vy, Real vz,
    Real& ix, Real& iy, Real& iz, bool aboveFace)
{
    // Returns when and whether a sphere in the region above face +Z
    // intersects face +Z or any of its vertices or edges.  The input
    // aboveFace is true when the x and y coordinates are within the x and y
    // extents.  The function will still work if they are not, but it needs
    // to be false then, to avoid some checks that assume that x and y are
    // within the extents.  This function checks face z, and the vertex and
    // two edges that the velocity is headed towards on the face.

    // Check for already intersecting if above face.
    if (cz <= ez + mSphere->Radius && aboveFace)
    {
        mContactTime = (Real)0;
        return -1;
    }

    // Check for easy out (moving away on Z axis).
    if (vz >= (Real)0)
    {
        return 0;
    }

    Real rsqr = mSphere->Radius*mSphere->Radius;

    Real vsqrX = vz*vz + vx*vx;
    Real vsqrY = vz*vz + vy*vy;
    Real dx, dy, dz = cz - ez;
    Real crossX, crossY;
    int signX, signY;

    // This determines which way the box is heading and finds the values of
    // CrossX and CrossY which are positive if the sphere center will not
    // pass through the box.  Then it is only necessary to check two edges,
    // the face and the vertex for intersection.

    if (vx >= (Real)0)
    {
        signX = 1;
        dx = cx - ex;
        crossX = vx*dz - vz*dx;
    }
    else
    {
        signX = -1;
        dx = cx + ex;
        crossX = vz*dx - vx*dz;
    }

    if (vy >= (Real)0)
    {
        signY = 1;
        dy = cy - ey;
        crossY = vy*dz - vz*dy;
    }
    else
    {
        signY = -1;
        dy = cy + ey;
        crossY = vz*dy - vy*dz;
    }

    // Does the circle intersect along the x edge?
    if (crossX > mSphere->Radius*vx*signX)
    {
        if (crossX*crossX > rsqr*vsqrX)
        {
            // Sphere overshoots box on the x-axis (either side).
            return 0;
        }

        // Does the circle hit the y edge?
        if (crossY > mSphere->Radius*vy*signY)
        {
            // Potential vertex intersection.
            if (crossY*crossY > rsqr*vsqrY)
            {
                // Sphere overshoots box on the y-axis (either side).
                return 0;
            }

            Vector3<Real> relVelocity(vx,vy,vz);
            Vector3<Real> D(dx,dy,dz);
            Vector3<Real> cross = D.Cross(relVelocity);
            if (cross.SquaredLength() > rsqr*relVelocity.SquaredLength())
            {
                // Sphere overshoots the box on the corner.
                return 0;
            }

            mContactTime = GetVertexIntersection(dx, dy, dz, vx, vy, vz,
                rsqr);
            ix = ex*signX;
            iy = ey*signY;
        }
        else
        {
            // x-edge intersection
            mContactTime = GetEdgeIntersection(dx, dz, vx, vz, vsqrX, rsqr);
            ix = ex*signX;
            iy = cy + vy*mContactTime;
        }
    }
    else
    {
        // Does the circle hit the y edge?
        if (crossY > mSphere->Radius*vy*signY)
        {
            // Potential y-edge intersection.
            if (crossY*crossY > rsqr*vsqrY)
            {
                // Sphere overshoots box on the y-axis (either side).
                return 0;
            }

            mContactTime = GetEdgeIntersection(dy, dz, vy, vz, vsqrY, rsqr);
            ix = cx + vx*mContactTime;
            iy = ey*signY;
        }
        else
        {
            // Face intersection (easy).
            mContactTime = (-dz + mSphere->Radius)/vz;
            ix = mContactTime*vx + cx;
            iy = mContactTime*vy + cy;
        }
    }

    // z coordinate of any intersection must be the face of z.
    iz = ez;
    return 1;
}
//----------------------------------------------------------------------------
template <typename Real>
int IntrBox3Sphere3<Real>::FindJustEdgeIntersection (Real cy, Real ex,
    Real ey, Real ez, Real dx, Real dz, Real vx, Real vy, Real vz,
    Real& ix, Real& iy, Real& iz)
{
    // Finds the intersection of a point dx and dz away from an edge with
    // direction y.  The sphere is at a point cy, and the edge is at the
    // point ex.  Checks the edge and the vertex the velocity is heading
    // towards.

    Real rsqr = mSphere->Radius*mSphere->Radius;
    Real dy, crossZ, crossX;  // possible edge/vertex intersection
    int signY;

    // Depending on the sign of Vy, pick the vertex that the velocity is
    // heading towards on the edge, as well as creating crossX and crossZ
    // such that their sign will always be positive if the sphere center goes
    // over that edge.

    if (vy >= (Real)0)
    {
        signY = 1;
        dy = cy - ey;
        crossZ = dx*vy - dy*vx;
        crossX = dz*vy - dy*vz;
    }
    else
    {
        signY = -1;
        dy = cy + ey;
        crossZ = dy*vx - dx*vy;
        crossX = dy*vz - dz*vy;
    }

    // Check where on edge this intersection will occur.
    if (crossZ >= (Real)0 && crossX >= (Real)0
    &&  crossX*crossX + crossZ*crossZ >
        vy*vy*mSphere->Radius*mSphere->Radius)
    {
        // Sphere potentially intersects with vertex.
        Vector3<Real> relVelocity(vx, vy, vz);
        Vector3<Real> D(dx, dy, dz);
        Vector3<Real> cross = D.Cross(relVelocity);
        if (cross.SquaredLength() > rsqr*relVelocity.SquaredLength())
        {
            // Sphere overshoots the box on the vertex.
            return 0;
        }

        // Sphere actually does intersect the vertex.
        mContactTime = GetVertexIntersection(dx, dy, dz, vx, vy, vz, rsqr);
        ix = ex;
        iy = signY*ey;
        iz = ez;
    }
    else
    {
        // Sphere intersects with edge.
        Real vsqrX = vz*vz + vx*vx;
        mContactTime = GetEdgeIntersection(dx, dz, vx, vz, vsqrX, rsqr);
        ix = ex;
        iy = cy + mContactTime*vy;
        iz = ez;
    }
    return 1;
}
//----------------------------------------------------------------------------
template <typename Real>
int IntrBox3Sphere3<Real>::FindEdgeRegionIntersection (Real ex, Real ey,
    Real ez, Real cx, Real cy, Real cz, Real vx, Real vy, Real vz,
    Real& ix, Real& iy, Real& iz, bool aboveEdge)
{
    // Assumes the sphere center is in the region above the x and z planes.
    // The input aboveEdge is true when the y coordinate is within the y
    // extents.  The function will still work if it is not, but it needs to be
    // false then, to avoid some checks that assume that y is within the
    // extent.  This function checks the edge that the region is above, as
    // well as does a "face region" check on the face it is heading towards.

    Real dx = cx - ex;
    Real dz = cz - ez;
    Real rsqr = mSphere->Radius*mSphere->Radius;

    if (aboveEdge)
    {
        Real diff = dx*dx + dz*dz - rsqr;
        if (diff <= (Real)0)
        {
            // Circle is already intersecting the box.
            mContactTime = (Real)0;
            return -1;
        }
    }

    Real dot = vx*dx + vz*dz;
    if (dot >= (Real)0)
    {
        // Circle not moving towards box.
        return 0;
    }

    // The value dotPerp splits the region of interest along the edge in the
    // middle of that region.
    Real dotPerp = vz*dx - vx*dz; 
    if (dotPerp >= (Real)0)
    {
        // Sphere moving towards +z face.
        if (vx >= (Real)0)
        {
            // Passed corner, moving away from box.
            return 0;
        }

        // Intersection with x-z edge.  If there is trouble with objects that
        // "scrape" the surface (velocity perpendicular to face normal, and
        // point of contact with a radius direction parallel to the face
        // normal), this check may need to be a little more inclusive (small
        // tolerance due to floating point errors) as the edge check needs
        // to get "scraping" objects (as they hit the edge with the point)
        // and the face region check will not catch it because the object is
        // not moving towards the face.
        if (dotPerp <= -mSphere->Radius*vx)
        {
            return FindJustEdgeIntersection(cy, ez, ey, ex, dz, dx, vz, vy,
                vx, iz, iy, ix);
        }

        // Now, check the face of z for intersections.
        return FindFaceRegionIntersection(ex, ey, ez, cx, cy, cz, vx, vy,
            vz, ix, iy, iz, false);
    }
    else
    {
        // Sphere moving towards +x face.
        if (vz >= (Real)0)
        {
            // Passed corner, moving away from box.
            return 0;
        }

        // Check intersection with x-z edge.  See the note above about
        // "scraping" objects.
        if (dotPerp >= mSphere->Radius*vz)
        {
            // Possible edge/vertex intersection.
            return FindJustEdgeIntersection(cy, ex, ey, ez, dx, dz, vx, vy,
                vz, ix, iy, iz);
        }

        // Now, check the face of x for intersections.
        return FindFaceRegionIntersection(ez, ey, ex, cz, cy, cx, vz, vy,
            vx, iz, iy, ix, false);
    }
}
//----------------------------------------------------------------------------
template <typename Real>
int IntrBox3Sphere3<Real>::FindVertexRegionIntersection (Real ex, Real ey,
    Real ez, Real cx, Real cy, Real cz, Real vx, Real vy, Real vz,
    Real& ix, Real& iy, Real& iz)
{
    // Assumes the sphere is above the vertex +ex, +ey, +ez.

    Real dx = cx - ex;
    Real dy = cy - ey;
    Real dz = cz - ez;
    Real rsqr = mSphere->Radius*mSphere->Radius;
    Real diff = dx*dx + dy*dy + dz*dz - rsqr;
    if (diff <= (Real)0)
    {
        // Sphere is already intersecting the box.
        mContactTime = (Real)0;
        return -1;
    }

    if (vx*dx + vy*dy + vz*dz >= (Real)0)
    {
        // Sphere not moving towards box.
        return 0;
    }

    // The box can be divided up into 3 regions, which simplifies checking to
    // see what the sphere hits.  The regions are divided by which edge
    // (formed by the vertex and some axis) is closest to the velocity
    // vector.
    //
    // To check if it hits the vertex, look at the edge (E) it is going
    // towards.  Create a plane formed by the other two edges (with E as the
    // plane normal) with the vertex at the origin.  The intercept along an
    // axis, in that plane, of the line formed by the sphere center and the
    // velocity as its direction, will be fCrossAxis/fVEdge.  Thus, the
    // distance from the origin to the point in the plane that that line from
    // the sphere in the velocity direction crosses will be the squared sum
    // of those two intercepts.  If that sum is less than the radius squared,
    // then the sphere will hit the vertex otherwise, it will continue on
    // past the vertex.  If it misses, since it is known which edge the box
    // is near, the find edge region test can be used.
    //
    // Also, due to the constrained conditions, only those six cases (two for
    // each region, since fCrossEdge can be + or -) of signs of fCross values
    // can occur.
    //
    // The 3rd case will also pick up cases where D = V, causing a ZERO cross.

    Real crossX = vy*dz - vz*dy;
    Real crossY = vx*dz - vz*dx;
    Real crossZ = vy*dx - vx*dy;
    Real crX2 = crossX*crossX;
    Real crY2 = crossY*crossY;
    Real crZ2 = crossZ*crossZ;
    Real vx2 = vx*vx;
    Real vy2 = vy*vy;
    Real vz2 = vz*vz;

    // Intersection with the vertex?
    if ((crossY <  (Real)0 && crossZ >= (Real)0 && crY2 + crZ2 <= rsqr*vx2)
    ||  (crossZ <  (Real)0 && crossX <  (Real)0 && crX2 + crZ2 <= rsqr*vy2)
    ||  (crossY >= (Real)0 && crossX >= (Real)0 && crX2 + crY2 <= rsqr*vz2))
    {
        // Standard line-sphere intersection.
        mContactTime = GetVertexIntersection(dx, dy, dz, vx, vy, vz,
            mSphere->Radius*mSphere->Radius);
        ix = mContactTime*vx + cx;
        iy = mContactTime*vy + cy;
        iz = mContactTime*vz + cz;
        return 1;
    }
    else if (crossY < (Real)0 && crossZ >= (Real)0)
    {
        // x edge region, check y,z planes.
        return FindEdgeRegionIntersection(ey, ex, ez, cy, cx, cz, vy, vx,
            vz, iy, ix, iz, false);
    }
    else if (crossZ < (Real)0 && crossX < (Real)0)
    {
        // y edge region, check x,z planes.
        return FindEdgeRegionIntersection(ex, ey, ez, cx, cy, cz, vx, vy,
            vz, ix, iy, iz, false);
    }
    else // crossY >= 0 && crossX >= 0
    {
        // z edge region, check x,y planes.
        return FindEdgeRegionIntersection(ex, ez, ey, cx, cz, cy, vx, vz,
            vy, ix, iz, iy, false);
    }
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template WM5_MATHEMATICS_ITEM
class IntrBox3Sphere3<float>;

template WM5_MATHEMATICS_ITEM
class IntrBox3Sphere3<double>;
//----------------------------------------------------------------------------
}