1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // *       Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // *       Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // *       Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 
37 #ifndef INCLUDED_IMATHFRUSTUM_H
38 #define INCLUDED_IMATHFRUSTUM_H
39 
40 
41 #include "ImathVec.h"
42 #include "ImathPlane.h"
43 #include "ImathLine.h"
44 #include "ImathMatrix.h"
45 #include "ImathLimits.h"
46 #include "ImathFun.h"
47 #include "ImathNamespace.h"
48 
49 #include "IexMathExc.h"
50 
51 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
52 
53 //
54 //  template class Frustum<T>
55 //
56 //  The frustum is always located with the eye point at the
57 //  origin facing down -Z. This makes the Frustum class
58 //  compatable with OpenGL (or anything that assumes a camera
59 //  looks down -Z, hence with a right-handed coordinate system)
60 //  but not with RenderMan which assumes the camera looks down
61 //  +Z. Additional functions are provided for conversion from
62 //  and from various camera coordinate spaces.
63 //
64 //  nearPlane/farPlane: near/far are keywords used by Microsoft's
65 //  compiler, so we use nearPlane/farPlane instead to avoid
66 //  issues.
67 
68 
69 template<class T>
70 class Frustum
71 {
72   public:
73     Frustum();
74     Frustum(const Frustum &);
75     Frustum(T nearPlane, T farPlane, T left, T right, T top, T bottom, bool ortho=false);
76     Frustum(T nearPlane, T farPlane, T fovx, T fovy, T aspect);
77     virtual ~Frustum();
78 
79     //--------------------
80     // Assignment operator
81     //--------------------
82 
83     const Frustum &     operator = (const Frustum &);
84 
85     //--------------------
86     //  Operators:  ==, !=
87     //--------------------
88 
89     bool                operator == (const Frustum<T> &src) const;
90     bool                operator != (const Frustum<T> &src) const;
91 
92     //--------------------------------------------------------
93     //  Set functions change the entire state of the Frustum
94     //--------------------------------------------------------
95 
96     void                set(T nearPlane, T farPlane,
97                             T left, T right,
98                             T top, T bottom,
99                             bool ortho=false);
100 
101     void                set(T nearPlane, T farPlane, T fovx, T fovy, T aspect);
102 
103     //------------------------------------------------------
104     //	These functions modify an already valid frustum state
105     //------------------------------------------------------
106 
107     void                modifyNearAndFar(T nearPlane, T farPlane);
108     void                setOrthographic(bool);
109 
110     //--------------
111     //  Access
112     //--------------
113 
orthographic()114     bool                orthographic() const  { return _orthographic; }
nearPlane()115     T                   nearPlane() const     { return _nearPlane;    }
hither()116     T                   hither() const        { return _nearPlane;    }
farPlane()117     T                   farPlane() const      { return _farPlane;     }
yon()118     T                   yon() const           { return _farPlane;     }
left()119     T                   left() const          { return _left;         }
right()120     T                   right() const         { return _right;        }
bottom()121     T                   bottom() const        { return _bottom;       }
top()122     T                   top() const           { return _top;          }
123 
124     //-----------------------------------------------------------------------
125     //  Sets the planes in p to be the six bounding planes of the frustum, in
126     //  the following order: top, right, bottom, left, near, far.
127     //  Note that the planes have normals that point out of the frustum.
128     //  The version of this routine that takes a matrix applies that matrix
129     //  to transform the frustum before setting the planes.
130     //-----------------------------------------------------------------------
131 
132     void                planes(Plane3<T> p[6]) const;
133     void                planes(Plane3<T> p[6], const Matrix44<T> &M) const;
134 
135     //----------------------
136     //  Derived Quantities
137     //----------------------
138 
139     T                   fovx() const;
140     T                   fovy() const;
141     T                   aspect() const;
142     Matrix44<T>         projectionMatrix() const;
143     bool                degenerate() const;
144 
145     //-----------------------------------------------------------------------
146     //  Takes a rectangle in the screen space (i.e., -1 <= left <= right <= 1
147     //  and -1 <= bottom <= top <= 1) of this Frustum, and returns a new
148     //  Frustum whose near clipping-plane window is that rectangle in local
149     //  space.
150     //-----------------------------------------------------------------------
151 
152     Frustum<T>          window(T left, T right, T top, T bottom) const;
153 
154     //----------------------------------------------------------
155     // Projection is in screen space / Conversion from Z-Buffer
156     //----------------------------------------------------------
157 
158     Line3<T>            projectScreenToRay( const Vec2<T> & ) const;
159     Vec2<T>             projectPointToScreen( const Vec3<T> & ) const;
160 
161     T                   ZToDepth(long zval, long min, long max) const;
162     T                   normalizedZToDepth(T zval) const;
163     long                DepthToZ(T depth, long zmin, long zmax) const;
164 
165     T                   worldRadius(const Vec3<T> &p, T radius) const;
166     T                   screenRadius(const Vec3<T> &p, T radius) const;
167 
168 
169   protected:
170 
171     Vec2<T>             screenToLocal( const Vec2<T> & ) const;
172     Vec2<T>             localToScreen( const Vec2<T> & ) const;
173 
174   protected:
175     T                   _nearPlane;
176     T                   _farPlane;
177     T                   _left;
178     T                   _right;
179     T                   _top;
180     T                   _bottom;
181     bool                _orthographic;
182 };
183 
184 
185 template<class T>
Frustum()186 inline Frustum<T>::Frustum()
187 {
188     set(T (0.1),
189         T (1000.0),
190         T (-1.0),
191         T (1.0),
192         T (1.0),
193         T (-1.0),
194         false);
195 }
196 
197 template<class T>
Frustum(const Frustum & f)198 inline Frustum<T>::Frustum(const Frustum &f)
199 {
200     *this = f;
201 }
202 
203 template<class T>
Frustum(T n,T f,T l,T r,T t,T b,bool o)204 inline Frustum<T>::Frustum(T n, T f, T l, T r, T t, T b, bool o)
205 {
206     set(n,f,l,r,t,b,o);
207 }
208 
209 template<class T>
Frustum(T nearPlane,T farPlane,T fovx,T fovy,T aspect)210 inline Frustum<T>::Frustum(T nearPlane, T farPlane, T fovx, T fovy, T aspect)
211 {
212     set(nearPlane,farPlane,fovx,fovy,aspect);
213 }
214 
215 template<class T>
~Frustum()216 Frustum<T>::~Frustum()
217 {
218 }
219 
220 template<class T>
221 const Frustum<T> &
222 Frustum<T>::operator = (const Frustum &f)
223 {
224     _nearPlane    = f._nearPlane;
225     _farPlane     = f._farPlane;
226     _left         = f._left;
227     _right        = f._right;
228     _top          = f._top;
229     _bottom       = f._bottom;
230     _orthographic = f._orthographic;
231 
232     return *this;
233 }
234 
235 template <class T>
236 bool
237 Frustum<T>::operator == (const Frustum<T> &src) const
238 {
239     return
240         _nearPlane    == src._nearPlane   &&
241         _farPlane     == src._farPlane    &&
242         _left         == src._left   &&
243         _right        == src._right  &&
244         _top          == src._top    &&
245         _bottom       == src._bottom &&
246         _orthographic == src._orthographic;
247 }
248 
249 template <class T>
250 inline bool
251 Frustum<T>::operator != (const Frustum<T> &src) const
252 {
253     return !operator== (src);
254 }
255 
256 template<class T>
set(T n,T f,T l,T r,T t,T b,bool o)257 void Frustum<T>::set(T n, T f, T l, T r, T t, T b, bool o)
258 {
259     _nearPlane      = n;
260     _farPlane       = f;
261     _left           = l;
262     _right          = r;
263     _bottom         = b;
264     _top            = t;
265     _orthographic   = o;
266 }
267 
268 template<class T>
modifyNearAndFar(T n,T f)269 void Frustum<T>::modifyNearAndFar(T n, T f)
270 {
271     if ( _orthographic )
272     {
273         _nearPlane = n;
274     }
275     else
276     {
277         Line3<T>  lowerLeft( Vec3<T>(0,0,0), Vec3<T>(_left,_bottom,-_nearPlane) );
278         Line3<T> upperRight( Vec3<T>(0,0,0), Vec3<T>(_right,_top,-_nearPlane) );
279         Plane3<T> nearPlane( Vec3<T>(0,0,-1), n );
280 
281         Vec3<T> ll,ur;
282         nearPlane.intersect(lowerLeft,ll);
283         nearPlane.intersect(upperRight,ur);
284 
285         _left      = ll.x;
286         _right     = ur.x;
287         _top       = ur.y;
288         _bottom    = ll.y;
289         _nearPlane = n;
290         _farPlane  = f;
291     }
292 
293     _farPlane = f;
294 }
295 
296 template<class T>
setOrthographic(bool ortho)297 void Frustum<T>::setOrthographic(bool ortho)
298 {
299     _orthographic   = ortho;
300 }
301 
302 template<class T>
set(T nearPlane,T farPlane,T fovx,T fovy,T aspect)303 void Frustum<T>::set(T nearPlane, T farPlane, T fovx, T fovy, T aspect)
304 {
305     if (fovx != 0 && fovy != 0)
306         throw IEX_NAMESPACE::ArgExc ("fovx and fovy cannot both be non-zero.");
307 
308     const T two = static_cast<T>(2);
309 
310     if (fovx != 0)
311     {
312         _right    = nearPlane * Math<T>::tan(fovx / two);
313         _left     = -_right;
314         _top      = ((_right - _left) / aspect) / two;
315         _bottom   = -_top;
316     }
317     else
318     {
319         _top      = nearPlane * Math<T>::tan(fovy / two);
320         _bottom   = -_top;
321         _right    = (_top - _bottom) * aspect / two;
322         _left     = -_right;
323     }
324     _nearPlane    = nearPlane;
325     _farPlane     = farPlane;
326     _orthographic = false;
327 }
328 
329 template<class T>
fovx()330 T Frustum<T>::fovx() const
331 {
332     return Math<T>::atan2(_right,_nearPlane) - Math<T>::atan2(_left,_nearPlane);
333 }
334 
335 template<class T>
fovy()336 T Frustum<T>::fovy() const
337 {
338     return Math<T>::atan2(_top,_nearPlane) - Math<T>::atan2(_bottom,_nearPlane);
339 }
340 
341 template<class T>
aspect()342 T Frustum<T>::aspect() const
343 {
344     T rightMinusLeft = _right-_left;
345     T topMinusBottom = _top-_bottom;
346 
347     if (abs(topMinusBottom) < 1 &&
348         abs(rightMinusLeft) > limits<T>::max() * abs(topMinusBottom))
349     {
350         throw IEX_NAMESPACE::DivzeroExc ("Bad viewing frustum: "
351                                "aspect ratio cannot be computed.");
352     }
353 
354     return rightMinusLeft / topMinusBottom;
355 }
356 
357 template<class T>
projectionMatrix()358 Matrix44<T> Frustum<T>::projectionMatrix() const
359 {
360     T rightPlusLeft  = _right+_left;
361     T rightMinusLeft = _right-_left;
362 
363     T topPlusBottom  = _top+_bottom;
364     T topMinusBottom = _top-_bottom;
365 
366     T farPlusNear    = _farPlane+_nearPlane;
367     T farMinusNear   = _farPlane-_nearPlane;
368 
369     if ((abs(rightMinusLeft) < 1 &&
370          abs(rightPlusLeft) > limits<T>::max() * abs(rightMinusLeft)) ||
371         (abs(topMinusBottom) < 1 &&
372          abs(topPlusBottom) > limits<T>::max() * abs(topMinusBottom)) ||
373         (abs(farMinusNear) < 1 &&
374          abs(farPlusNear) > limits<T>::max() * abs(farMinusNear)))
375     {
376         throw IEX_NAMESPACE::DivzeroExc ("Bad viewing frustum: "
377                                                  "projection matrix cannot be computed.");
378     }
379 
380     if ( _orthographic )
381     {
382         T tx = -rightPlusLeft / rightMinusLeft;
383         T ty = -topPlusBottom / topMinusBottom;
384         T tz = -farPlusNear   / farMinusNear;
385 
386         if ((abs(rightMinusLeft) < 1 &&
387              2 > limits<T>::max() * abs(rightMinusLeft)) ||
388             (abs(topMinusBottom) < 1 &&
389              2 > limits<T>::max() * abs(topMinusBottom)) ||
390             (abs(farMinusNear) < 1 &&
391              2 > limits<T>::max() * abs(farMinusNear)))
392         {
393             throw IEX_NAMESPACE::DivzeroExc ("Bad viewing frustum: "
394                                                          "projection matrix cannot be computed.");
395         }
396 
397         T A  =  2 / rightMinusLeft;
398         T B  =  2 / topMinusBottom;
399         T C  = -2 / farMinusNear;
400 
401         return Matrix44<T>( A,  0,  0,  0,
402                             0,  B,  0,  0,
403                             0,  0,  C,  0,
404                             tx, ty, tz, 1.f );
405     }
406     else
407     {
408         T A =  rightPlusLeft / rightMinusLeft;
409         T B =  topPlusBottom / topMinusBottom;
410         T C = -farPlusNear   / farMinusNear;
411 
412         T farTimesNear = -2 * _farPlane * _nearPlane;
413         if (abs(farMinusNear) < 1 &&
414             abs(farTimesNear) > limits<T>::max() * abs(farMinusNear))
415         {
416             throw IEX_NAMESPACE::DivzeroExc ("Bad viewing frustum: "
417                                                          "projection matrix cannot be computed.");
418         }
419 
420         T D = farTimesNear / farMinusNear;
421 
422         T twoTimesNear = 2 * _nearPlane;
423 
424         if ((abs(rightMinusLeft) < 1 &&
425              abs(twoTimesNear) > limits<T>::max() * abs(rightMinusLeft)) ||
426             (abs(topMinusBottom) < 1 &&
427              abs(twoTimesNear) > limits<T>::max() * abs(topMinusBottom)))
428         {
429             throw IEX_NAMESPACE::DivzeroExc ("Bad viewing frustum: "
430                                                          "projection matrix cannot be computed.");
431         }
432 
433         T E = twoTimesNear / rightMinusLeft;
434         T F = twoTimesNear / topMinusBottom;
435 
436         return Matrix44<T>( E,  0,  0,  0,
437                             0,  F,  0,  0,
438                             A,  B,  C, -1,
439                             0,  0,  D,  0 );
440     }
441 }
442 
443 template<class T>
degenerate()444 bool Frustum<T>::degenerate() const
445 {
446     return (_nearPlane == _farPlane) ||
447            (_left == _right) ||
448            (_top == _bottom);
449 }
450 
451 template<class T>
window(T l,T r,T t,T b)452 Frustum<T> Frustum<T>::window(T l, T r, T t, T b) const
453 {
454     // move it to 0->1 space
455 
456     Vec2<T> bl = screenToLocal( Vec2<T>(l,b) );
457     Vec2<T> tr = screenToLocal( Vec2<T>(r,t) );
458 
459     return Frustum<T>(_nearPlane, _farPlane, bl.x, tr.x, tr.y, bl.y, _orthographic);
460 }
461 
462 
463 template<class T>
screenToLocal(const Vec2<T> & s)464 Vec2<T> Frustum<T>::screenToLocal(const Vec2<T> &s) const
465 {
466     return Vec2<T>( _left + (_right-_left) * (1.f+s.x) / 2.f,
467                     _bottom + (_top-_bottom) * (1.f+s.y) / 2.f );
468 }
469 
470 template<class T>
localToScreen(const Vec2<T> & p)471 Vec2<T> Frustum<T>::localToScreen(const Vec2<T> &p) const
472 {
473     T leftPlusRight  = _left - T (2) * p.x + _right;
474     T leftMinusRight = _left-_right;
475     T bottomPlusTop  = _bottom - T (2) * p.y + _top;
476     T bottomMinusTop = _bottom-_top;
477 
478     if ((abs(leftMinusRight) < T (1) &&
479          abs(leftPlusRight) > limits<T>::max() * abs(leftMinusRight)) ||
480         (abs(bottomMinusTop) < T (1) &&
481          abs(bottomPlusTop) > limits<T>::max() * abs(bottomMinusTop)))
482     {
483         throw IEX_NAMESPACE::DivzeroExc
484             ("Bad viewing frustum: "
485              "local-to-screen transformation cannot be computed");
486     }
487 
488     return Vec2<T>( leftPlusRight / leftMinusRight,
489                     bottomPlusTop / bottomMinusTop );
490 }
491 
492 template<class T>
projectScreenToRay(const Vec2<T> & p)493 Line3<T> Frustum<T>::projectScreenToRay(const Vec2<T> &p) const
494 {
495     Vec2<T> point = screenToLocal(p);
496     if (orthographic())
497         return Line3<T>( Vec3<T>(point.x,point.y, 0.0),
498                          Vec3<T>(point.x,point.y,-1.0));
499     else
500         return Line3<T>( Vec3<T>(0, 0, 0), Vec3<T>(point.x,point.y,-_nearPlane));
501 }
502 
503 template<class T>
projectPointToScreen(const Vec3<T> & point)504 Vec2<T> Frustum<T>::projectPointToScreen(const Vec3<T> &point) const
505 {
506     if (orthographic() || point.z == T (0))
507         return localToScreen( Vec2<T>( point.x, point.y ) );
508     else
509         return localToScreen( Vec2<T>( point.x * _nearPlane / -point.z,
510                                        point.y * _nearPlane / -point.z ) );
511 }
512 
513 template<class T>
ZToDepth(long zval,long zmin,long zmax)514 T Frustum<T>::ZToDepth(long zval,long zmin,long zmax) const
515 {
516     int zdiff = zmax - zmin;
517 
518     if (zdiff == 0)
519     {
520         throw IEX_NAMESPACE::DivzeroExc
521             ("Bad call to Frustum::ZToDepth: zmax == zmin");
522     }
523 
524     if ( zval > zmax+1 ) zval -= zdiff;
525 
526     T fzval = (T(zval) - T(zmin)) / T(zdiff);
527     return normalizedZToDepth(fzval);
528 }
529 
530 template<class T>
normalizedZToDepth(T zval)531 T Frustum<T>::normalizedZToDepth(T zval) const
532 {
533     T Zp = zval * 2.0 - 1;
534 
535     if ( _orthographic )
536     {
537         return   -(Zp*(_farPlane-_nearPlane) + (_farPlane+_nearPlane))/2;
538     }
539     else
540     {
541         T farTimesNear = 2 * _farPlane * _nearPlane;
542         T farMinusNear = Zp * (_farPlane - _nearPlane) - _farPlane - _nearPlane;
543 
544         if (abs(farMinusNear) < 1 &&
545             abs(farTimesNear) > limits<T>::max() * abs(farMinusNear))
546         {
547             throw IEX_NAMESPACE::DivzeroExc
548                 ("Frustum::normalizedZToDepth cannot be computed.  The "
549                  "near and far clipping planes of the viewing frustum "
550                  "may be too close to each other");
551         }
552 
553         return farTimesNear / farMinusNear;
554     }
555 }
556 
557 template<class T>
DepthToZ(T depth,long zmin,long zmax)558 long Frustum<T>::DepthToZ(T depth,long zmin,long zmax) const
559 {
560     long zdiff     = zmax - zmin;
561     T farMinusNear = _farPlane-_nearPlane;
562 
563     if ( _orthographic )
564     {
565         T farPlusNear = 2*depth + _farPlane + _nearPlane;
566 
567         if (abs(farMinusNear) < 1 &&
568             abs(farPlusNear) > limits<T>::max() * abs(farMinusNear))
569         {
570             throw IEX_NAMESPACE::DivzeroExc
571                 ("Bad viewing frustum: near and far clipping planes "
572                  "are too close to each other");
573         }
574 
575         T Zp = -farPlusNear/farMinusNear;
576         return long(0.5*(Zp+1)*zdiff) + zmin;
577     }
578     else
579     {
580         // Perspective
581 
582         T farTimesNear = 2*_farPlane*_nearPlane;
583         if (abs(depth) < 1 &&
584             abs(farTimesNear) > limits<T>::max() * abs(depth))
585         {
586             throw IEX_NAMESPACE::DivzeroExc
587                 ("Bad call to DepthToZ function: value of `depth' "
588                  "is too small");
589         }
590 
591         T farPlusNear = farTimesNear/depth + _farPlane + _nearPlane;
592         if (abs(farMinusNear) < 1 &&
593             abs(farPlusNear) > limits<T>::max() * abs(farMinusNear))
594         {
595             throw IEX_NAMESPACE::DivzeroExc
596                 ("Bad viewing frustum: near and far clipping planes "
597                  "are too close to each other");
598         }
599 
600         T Zp = farPlusNear/farMinusNear;
601         return long(0.5*(Zp+1)*zdiff) + zmin;
602     }
603 }
604 
605 template<class T>
screenRadius(const Vec3<T> & p,T radius)606 T Frustum<T>::screenRadius(const Vec3<T> &p, T radius) const
607 {
608     // Derivation:
609     // Consider X-Z plane.
610     // X coord of projection of p = xp = p.x * (-_nearPlane / p.z)
611     // Let q be p + (radius, 0, 0).
612     // X coord of projection of q = xq = (p.x - radius)  * (-_nearPlane / p.z)
613     // X coord of projection of segment from p to q = r = xp - xq
614     //         = radius * (-_nearPlane / p.z)
615     // A similar analysis holds in the Y-Z plane.
616     // So r is the quantity we want to return.
617 
618     if (abs(p.z) > 1 || abs(-_nearPlane) < limits<T>::max() * abs(p.z))
619     {
620         return radius * (-_nearPlane / p.z);
621     }
622     else
623     {
624         throw IEX_NAMESPACE::DivzeroExc
625             ("Bad call to Frustum::screenRadius: the magnitude of `p' "
626              "is too small");
627     }
628 
629     return radius * (-_nearPlane / p.z);
630 }
631 
632 template<class T>
worldRadius(const Vec3<T> & p,T radius)633 T Frustum<T>::worldRadius(const Vec3<T> &p, T radius) const
634 {
635     if (abs(-_nearPlane) > 1 || abs(p.z) < limits<T>::max() * abs(-_nearPlane))
636     {
637         return radius * (p.z / -_nearPlane);
638     }
639     else
640     {
641         throw IEX_NAMESPACE::DivzeroExc
642             ("Bad viewing frustum: the near clipping plane is too "
643              "close to zero");
644     }
645 }
646 
647 template<class T>
planes(Plane3<T> p[6])648 void Frustum<T>::planes(Plane3<T> p[6]) const
649 {
650     //
651     //        Plane order: Top, Right, Bottom, Left, Near, Far.
652     //  Normals point outwards.
653     //
654 
655     if (! _orthographic)
656     {
657         Vec3<T> a( _left,  _bottom, -_nearPlane);
658         Vec3<T> b( _left,  _top,    -_nearPlane);
659         Vec3<T> c( _right, _top,    -_nearPlane);
660         Vec3<T> d( _right, _bottom, -_nearPlane);
661         Vec3<T> o(0,0,0);
662 
663         p[0].set( o, c, b );
664         p[1].set( o, d, c );
665         p[2].set( o, a, d );
666         p[3].set( o, b, a );
667     }
668     else
669     {
670         p[0].set( Vec3<T>( 0, 1, 0), _top );
671         p[1].set( Vec3<T>( 1, 0, 0), _right );
672         p[2].set( Vec3<T>( 0,-1, 0),-_bottom );
673         p[3].set( Vec3<T>(-1, 0, 0),-_left );
674     }
675     p[4].set( Vec3<T>(0, 0, 1), -_nearPlane );
676     p[5].set( Vec3<T>(0, 0,-1), _farPlane );
677 }
678 
679 
680 template<class T>
planes(Plane3<T> p[6],const Matrix44<T> & M)681 void Frustum<T>::planes(Plane3<T> p[6], const Matrix44<T> &M) const
682 {
683     //
684     //  Plane order: Top, Right, Bottom, Left, Near, Far.
685     //  Normals point outwards.
686     //
687 
688     Vec3<T> a   = Vec3<T>( _left,  _bottom, -_nearPlane) * M;
689     Vec3<T> b   = Vec3<T>( _left,  _top,    -_nearPlane) * M;
690     Vec3<T> c   = Vec3<T>( _right, _top,    -_nearPlane) * M;
691     Vec3<T> d   = Vec3<T>( _right, _bottom, -_nearPlane) * M;
692     if (! _orthographic)
693     {
694         double s    = _farPlane / double(_nearPlane);
695         T farLeft   = (T) (s * _left);
696         T farRight  = (T) (s * _right);
697         T farTop    = (T) (s * _top);
698         T farBottom = (T) (s * _bottom);
699         Vec3<T> e   = Vec3<T>( farLeft,  farBottom, -_farPlane) * M;
700         Vec3<T> f   = Vec3<T>( farLeft,  farTop,    -_farPlane) * M;
701         Vec3<T> g   = Vec3<T>( farRight, farTop,    -_farPlane) * M;
702         Vec3<T> o   = Vec3<T>(0,0,0) * M;
703         p[0].set( o, c, b );
704         p[1].set( o, d, c );
705         p[2].set( o, a, d );
706         p[3].set( o, b, a );
707         p[4].set( a, d, c );
708         p[5].set( e, f, g );
709      }
710     else
711     {
712         Vec3<T> e   = Vec3<T>( _left,  _bottom, -_farPlane) * M;
713         Vec3<T> f   = Vec3<T>( _left,  _top,    -_farPlane) * M;
714         Vec3<T> g   = Vec3<T>( _right, _top,    -_farPlane) * M;
715         Vec3<T> h   = Vec3<T>( _right, _bottom, -_farPlane) * M;
716         p[0].set( c, g, f );
717         p[1].set( d, h, g );
718         p[2].set( a, e, h );
719         p[3].set( b, f, e );
720         p[4].set( a, d, c );
721         p[5].set( e, f, g );
722     }
723 }
724 
725 typedef Frustum<float>  Frustumf;
726 typedef Frustum<double> Frustumd;
727 
728 
729 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
730 
731 
732 #if defined _WIN32 || defined _WIN64
733     #ifdef _redef_near
734         #define near
735     #endif
736     #ifdef _redef_far
737         #define far
738     #endif
739 #endif
740 
741 #endif // INCLUDED_IMATHFRUSTUM_H
742