ImathFrustum.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. #ifndef INCLUDED_IMATHFRUSTUM_H
  35. #define INCLUDED_IMATHFRUSTUM_H
  36. #include "ImathVec.h"
  37. #include "ImathPlane.h"
  38. #include "ImathLine.h"
  39. #include "ImathMatrix.h"
  40. #include "ImathLimits.h"
  41. #include "ImathFun.h"
  42. #include "ImathNamespace.h"
  43. #include "IexMathExc.h"
  44. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  45. //
  46. // template class Frustum<T>
  47. //
  48. // The frustum is always located with the eye point at the
  49. // origin facing down -Z. This makes the Frustum class
  50. // compatable with OpenGL (or anything that assumes a camera
  51. // looks down -Z, hence with a right-handed coordinate system)
  52. // but not with RenderMan which assumes the camera looks down
  53. // +Z. Additional functions are provided for conversion from
  54. // and from various camera coordinate spaces.
  55. //
  56. // nearPlane/farPlane: near/far are keywords used by Microsoft's
  57. // compiler, so we use nearPlane/farPlane instead to avoid
  58. // issues.
  59. template<class T>
  60. class Frustum
  61. {
  62. public:
  63. Frustum();
  64. Frustum(const Frustum &);
  65. Frustum(T nearPlane, T farPlane, T left, T right, T top, T bottom, bool ortho=false);
  66. Frustum(T nearPlane, T farPlane, T fovx, T fovy, T aspect);
  67. virtual ~Frustum();
  68. //--------------------
  69. // Assignment operator
  70. //--------------------
  71. const Frustum & operator = (const Frustum &);
  72. //--------------------
  73. // Operators: ==, !=
  74. //--------------------
  75. bool operator == (const Frustum<T> &src) const;
  76. bool operator != (const Frustum<T> &src) const;
  77. //--------------------------------------------------------
  78. // Set functions change the entire state of the Frustum
  79. //--------------------------------------------------------
  80. void set(T nearPlane, T farPlane,
  81. T left, T right,
  82. T top, T bottom,
  83. bool ortho=false);
  84. void set(T nearPlane, T farPlane, T fovx, T fovy, T aspect);
  85. //------------------------------------------------------
  86. // These functions modify an already valid frustum state
  87. //------------------------------------------------------
  88. void modifyNearAndFar(T nearPlane, T farPlane);
  89. void setOrthographic(bool);
  90. //--------------
  91. // Access
  92. //--------------
  93. bool orthographic() const { return _orthographic; }
  94. T nearPlane() const { return _nearPlane; }
  95. T hither() const { return _nearPlane; }
  96. T farPlane() const { return _farPlane; }
  97. T yon() const { return _farPlane; }
  98. T left() const { return _left; }
  99. T right() const { return _right; }
  100. T bottom() const { return _bottom; }
  101. T top() const { return _top; }
  102. //-----------------------------------------------------------------------
  103. // Sets the planes in p to be the six bounding planes of the frustum, in
  104. // the following order: top, right, bottom, left, near, far.
  105. // Note that the planes have normals that point out of the frustum.
  106. // The version of this routine that takes a matrix applies that matrix
  107. // to transform the frustum before setting the planes.
  108. //-----------------------------------------------------------------------
  109. void planes(Plane3<T> p[6]) const;
  110. void planes(Plane3<T> p[6], const Matrix44<T> &M) const;
  111. //----------------------
  112. // Derived Quantities
  113. //----------------------
  114. T fovx() const;
  115. T fovy() const;
  116. T aspect() const;
  117. Matrix44<T> projectionMatrix() const;
  118. bool degenerate() const;
  119. //-----------------------------------------------------------------------
  120. // Takes a rectangle in the screen space (i.e., -1 <= left <= right <= 1
  121. // and -1 <= bottom <= top <= 1) of this Frustum, and returns a new
  122. // Frustum whose near clipping-plane window is that rectangle in local
  123. // space.
  124. //-----------------------------------------------------------------------
  125. Frustum<T> window(T left, T right, T top, T bottom) const;
  126. //----------------------------------------------------------
  127. // Projection is in screen space / Conversion from Z-Buffer
  128. //----------------------------------------------------------
  129. Line3<T> projectScreenToRay( const Vec2<T> & ) const;
  130. Vec2<T> projectPointToScreen( const Vec3<T> & ) const;
  131. T ZToDepth(long zval, long min, long max) const;
  132. T normalizedZToDepth(T zval) const;
  133. long DepthToZ(T depth, long zmin, long zmax) const;
  134. T worldRadius(const Vec3<T> &p, T radius) const;
  135. T screenRadius(const Vec3<T> &p, T radius) const;
  136. protected:
  137. Vec2<T> screenToLocal( const Vec2<T> & ) const;
  138. Vec2<T> localToScreen( const Vec2<T> & ) const;
  139. protected:
  140. T _nearPlane;
  141. T _farPlane;
  142. T _left;
  143. T _right;
  144. T _top;
  145. T _bottom;
  146. bool _orthographic;
  147. };
  148. template<class T>
  149. inline Frustum<T>::Frustum()
  150. {
  151. set(T (0.1),
  152. T (1000.0),
  153. T (-1.0),
  154. T (1.0),
  155. T (1.0),
  156. T (-1.0),
  157. false);
  158. }
  159. template<class T>
  160. inline Frustum<T>::Frustum(const Frustum &f)
  161. {
  162. *this = f;
  163. }
  164. template<class T>
  165. inline Frustum<T>::Frustum(T n, T f, T l, T r, T t, T b, bool o)
  166. {
  167. set(n,f,l,r,t,b,o);
  168. }
  169. template<class T>
  170. inline Frustum<T>::Frustum(T nearPlane, T farPlane, T fovx, T fovy, T aspect)
  171. {
  172. set(nearPlane,farPlane,fovx,fovy,aspect);
  173. }
  174. template<class T>
  175. Frustum<T>::~Frustum()
  176. {
  177. }
  178. template<class T>
  179. const Frustum<T> &
  180. Frustum<T>::operator = (const Frustum &f)
  181. {
  182. _nearPlane = f._nearPlane;
  183. _farPlane = f._farPlane;
  184. _left = f._left;
  185. _right = f._right;
  186. _top = f._top;
  187. _bottom = f._bottom;
  188. _orthographic = f._orthographic;
  189. return *this;
  190. }
  191. template <class T>
  192. bool
  193. Frustum<T>::operator == (const Frustum<T> &src) const
  194. {
  195. return
  196. _nearPlane == src._nearPlane &&
  197. _farPlane == src._farPlane &&
  198. _left == src._left &&
  199. _right == src._right &&
  200. _top == src._top &&
  201. _bottom == src._bottom &&
  202. _orthographic == src._orthographic;
  203. }
  204. template <class T>
  205. inline bool
  206. Frustum<T>::operator != (const Frustum<T> &src) const
  207. {
  208. return !operator== (src);
  209. }
  210. template<class T>
  211. void Frustum<T>::set(T n, T f, T l, T r, T t, T b, bool o)
  212. {
  213. _nearPlane = n;
  214. _farPlane = f;
  215. _left = l;
  216. _right = r;
  217. _bottom = b;
  218. _top = t;
  219. _orthographic = o;
  220. }
  221. template<class T>
  222. void Frustum<T>::modifyNearAndFar(T n, T f)
  223. {
  224. if ( _orthographic )
  225. {
  226. _nearPlane = n;
  227. }
  228. else
  229. {
  230. Line3<T> lowerLeft( Vec3<T>(0,0,0), Vec3<T>(_left,_bottom,-_nearPlane) );
  231. Line3<T> upperRight( Vec3<T>(0,0,0), Vec3<T>(_right,_top,-_nearPlane) );
  232. Plane3<T> nearPlane( Vec3<T>(0,0,-1), n );
  233. Vec3<T> ll,ur;
  234. nearPlane.intersect(lowerLeft,ll);
  235. nearPlane.intersect(upperRight,ur);
  236. _left = ll.x;
  237. _right = ur.x;
  238. _top = ur.y;
  239. _bottom = ll.y;
  240. _nearPlane = n;
  241. _farPlane = f;
  242. }
  243. _farPlane = f;
  244. }
  245. template<class T>
  246. void Frustum<T>::setOrthographic(bool ortho)
  247. {
  248. _orthographic = ortho;
  249. }
  250. template<class T>
  251. void Frustum<T>::set(T nearPlane, T farPlane, T fovx, T fovy, T aspect)
  252. {
  253. if (fovx != 0 && fovy != 0)
  254. throw IEX_NAMESPACE::ArgExc ("fovx and fovy cannot both be non-zero.");
  255. const T two = static_cast<T>(2);
  256. if (fovx != 0)
  257. {
  258. _right = nearPlane * Math<T>::tan(fovx / two);
  259. _left = -_right;
  260. _top = ((_right - _left) / aspect) / two;
  261. _bottom = -_top;
  262. }
  263. else
  264. {
  265. _top = nearPlane * Math<T>::tan(fovy / two);
  266. _bottom = -_top;
  267. _right = (_top - _bottom) * aspect / two;
  268. _left = -_right;
  269. }
  270. _nearPlane = nearPlane;
  271. _farPlane = farPlane;
  272. _orthographic = false;
  273. }
  274. template<class T>
  275. T Frustum<T>::fovx() const
  276. {
  277. return Math<T>::atan2(_right,_nearPlane) - Math<T>::atan2(_left,_nearPlane);
  278. }
  279. template<class T>
  280. T Frustum<T>::fovy() const
  281. {
  282. return Math<T>::atan2(_top,_nearPlane) - Math<T>::atan2(_bottom,_nearPlane);
  283. }
  284. template<class T>
  285. T Frustum<T>::aspect() const
  286. {
  287. T rightMinusLeft = _right-_left;
  288. T topMinusBottom = _top-_bottom;
  289. if (abs(topMinusBottom) < 1 &&
  290. abs(rightMinusLeft) > limits<T>::max() * abs(topMinusBottom))
  291. {
  292. throw IEX_NAMESPACE::DivzeroExc ("Bad viewing frustum: "
  293. "aspect ratio cannot be computed.");
  294. }
  295. return rightMinusLeft / topMinusBottom;
  296. }
  297. template<class T>
  298. Matrix44<T> Frustum<T>::projectionMatrix() const
  299. {
  300. T rightPlusLeft = _right+_left;
  301. T rightMinusLeft = _right-_left;
  302. T topPlusBottom = _top+_bottom;
  303. T topMinusBottom = _top-_bottom;
  304. T farPlusNear = _farPlane+_nearPlane;
  305. T farMinusNear = _farPlane-_nearPlane;
  306. if ((abs(rightMinusLeft) < 1 &&
  307. abs(rightPlusLeft) > limits<T>::max() * abs(rightMinusLeft)) ||
  308. (abs(topMinusBottom) < 1 &&
  309. abs(topPlusBottom) > limits<T>::max() * abs(topMinusBottom)) ||
  310. (abs(farMinusNear) < 1 &&
  311. abs(farPlusNear) > limits<T>::max() * abs(farMinusNear)))
  312. {
  313. throw IEX_NAMESPACE::DivzeroExc ("Bad viewing frustum: "
  314. "projection matrix cannot be computed.");
  315. }
  316. if ( _orthographic )
  317. {
  318. T tx = -rightPlusLeft / rightMinusLeft;
  319. T ty = -topPlusBottom / topMinusBottom;
  320. T tz = -farPlusNear / farMinusNear;
  321. if ((abs(rightMinusLeft) < 1 &&
  322. 2 > limits<T>::max() * abs(rightMinusLeft)) ||
  323. (abs(topMinusBottom) < 1 &&
  324. 2 > limits<T>::max() * abs(topMinusBottom)) ||
  325. (abs(farMinusNear) < 1 &&
  326. 2 > limits<T>::max() * abs(farMinusNear)))
  327. {
  328. throw IEX_NAMESPACE::DivzeroExc ("Bad viewing frustum: "
  329. "projection matrix cannot be computed.");
  330. }
  331. T A = 2 / rightMinusLeft;
  332. T B = 2 / topMinusBottom;
  333. T C = -2 / farMinusNear;
  334. return Matrix44<T>( A, 0, 0, 0,
  335. 0, B, 0, 0,
  336. 0, 0, C, 0,
  337. tx, ty, tz, 1.f );
  338. }
  339. else
  340. {
  341. T A = rightPlusLeft / rightMinusLeft;
  342. T B = topPlusBottom / topMinusBottom;
  343. T C = -farPlusNear / farMinusNear;
  344. T farTimesNear = -2 * _farPlane * _nearPlane;
  345. if (abs(farMinusNear) < 1 &&
  346. abs(farTimesNear) > limits<T>::max() * abs(farMinusNear))
  347. {
  348. throw IEX_NAMESPACE::DivzeroExc ("Bad viewing frustum: "
  349. "projection matrix cannot be computed.");
  350. }
  351. T D = farTimesNear / farMinusNear;
  352. T twoTimesNear = 2 * _nearPlane;
  353. if ((abs(rightMinusLeft) < 1 &&
  354. abs(twoTimesNear) > limits<T>::max() * abs(rightMinusLeft)) ||
  355. (abs(topMinusBottom) < 1 &&
  356. abs(twoTimesNear) > limits<T>::max() * abs(topMinusBottom)))
  357. {
  358. throw IEX_NAMESPACE::DivzeroExc ("Bad viewing frustum: "
  359. "projection matrix cannot be computed.");
  360. }
  361. T E = twoTimesNear / rightMinusLeft;
  362. T F = twoTimesNear / topMinusBottom;
  363. return Matrix44<T>( E, 0, 0, 0,
  364. 0, F, 0, 0,
  365. A, B, C, -1,
  366. 0, 0, D, 0 );
  367. }
  368. }
  369. template<class T>
  370. bool Frustum<T>::degenerate() const
  371. {
  372. return (_nearPlane == _farPlane) ||
  373. (_left == _right) ||
  374. (_top == _bottom);
  375. }
  376. template<class T>
  377. Frustum<T> Frustum<T>::window(T l, T r, T t, T b) const
  378. {
  379. // move it to 0->1 space
  380. Vec2<T> bl = screenToLocal( Vec2<T>(l,b) );
  381. Vec2<T> tr = screenToLocal( Vec2<T>(r,t) );
  382. return Frustum<T>(_nearPlane, _farPlane, bl.x, tr.x, tr.y, bl.y, _orthographic);
  383. }
  384. template<class T>
  385. Vec2<T> Frustum<T>::screenToLocal(const Vec2<T> &s) const
  386. {
  387. return Vec2<T>( _left + (_right-_left) * (1.f+s.x) / 2.f,
  388. _bottom + (_top-_bottom) * (1.f+s.y) / 2.f );
  389. }
  390. template<class T>
  391. Vec2<T> Frustum<T>::localToScreen(const Vec2<T> &p) const
  392. {
  393. T leftPlusRight = _left - T (2) * p.x + _right;
  394. T leftMinusRight = _left-_right;
  395. T bottomPlusTop = _bottom - T (2) * p.y + _top;
  396. T bottomMinusTop = _bottom-_top;
  397. if ((abs(leftMinusRight) < T (1) &&
  398. abs(leftPlusRight) > limits<T>::max() * abs(leftMinusRight)) ||
  399. (abs(bottomMinusTop) < T (1) &&
  400. abs(bottomPlusTop) > limits<T>::max() * abs(bottomMinusTop)))
  401. {
  402. throw IEX_NAMESPACE::DivzeroExc
  403. ("Bad viewing frustum: "
  404. "local-to-screen transformation cannot be computed");
  405. }
  406. return Vec2<T>( leftPlusRight / leftMinusRight,
  407. bottomPlusTop / bottomMinusTop );
  408. }
  409. template<class T>
  410. Line3<T> Frustum<T>::projectScreenToRay(const Vec2<T> &p) const
  411. {
  412. Vec2<T> point = screenToLocal(p);
  413. if (orthographic())
  414. return Line3<T>( Vec3<T>(point.x,point.y, 0.0),
  415. Vec3<T>(point.x,point.y,-1.0));
  416. else
  417. return Line3<T>( Vec3<T>(0, 0, 0), Vec3<T>(point.x,point.y,-_nearPlane));
  418. }
  419. template<class T>
  420. Vec2<T> Frustum<T>::projectPointToScreen(const Vec3<T> &point) const
  421. {
  422. if (orthographic() || point.z == T (0))
  423. return localToScreen( Vec2<T>( point.x, point.y ) );
  424. else
  425. return localToScreen( Vec2<T>( point.x * _nearPlane / -point.z,
  426. point.y * _nearPlane / -point.z ) );
  427. }
  428. template<class T>
  429. T Frustum<T>::ZToDepth(long zval,long zmin,long zmax) const
  430. {
  431. int zdiff = zmax - zmin;
  432. if (zdiff == 0)
  433. {
  434. throw IEX_NAMESPACE::DivzeroExc
  435. ("Bad call to Frustum::ZToDepth: zmax == zmin");
  436. }
  437. if ( zval > zmax+1 ) zval -= zdiff;
  438. T fzval = (T(zval) - T(zmin)) / T(zdiff);
  439. return normalizedZToDepth(fzval);
  440. }
  441. template<class T>
  442. T Frustum<T>::normalizedZToDepth(T zval) const
  443. {
  444. T Zp = zval * 2.0 - 1;
  445. if ( _orthographic )
  446. {
  447. return -(Zp*(_farPlane-_nearPlane) + (_farPlane+_nearPlane))/2;
  448. }
  449. else
  450. {
  451. T farTimesNear = 2 * _farPlane * _nearPlane;
  452. T farMinusNear = Zp * (_farPlane - _nearPlane) - _farPlane - _nearPlane;
  453. if (abs(farMinusNear) < 1 &&
  454. abs(farTimesNear) > limits<T>::max() * abs(farMinusNear))
  455. {
  456. throw IEX_NAMESPACE::DivzeroExc
  457. ("Frustum::normalizedZToDepth cannot be computed. The "
  458. "near and far clipping planes of the viewing frustum "
  459. "may be too close to each other");
  460. }
  461. return farTimesNear / farMinusNear;
  462. }
  463. }
  464. template<class T>
  465. long Frustum<T>::DepthToZ(T depth,long zmin,long zmax) const
  466. {
  467. long zdiff = zmax - zmin;
  468. T farMinusNear = _farPlane-_nearPlane;
  469. if ( _orthographic )
  470. {
  471. T farPlusNear = 2*depth + _farPlane + _nearPlane;
  472. if (abs(farMinusNear) < 1 &&
  473. abs(farPlusNear) > limits<T>::max() * abs(farMinusNear))
  474. {
  475. throw IEX_NAMESPACE::DivzeroExc
  476. ("Bad viewing frustum: near and far clipping planes "
  477. "are too close to each other");
  478. }
  479. T Zp = -farPlusNear/farMinusNear;
  480. return long(0.5*(Zp+1)*zdiff) + zmin;
  481. }
  482. else
  483. {
  484. // Perspective
  485. T farTimesNear = 2*_farPlane*_nearPlane;
  486. if (abs(depth) < 1 &&
  487. abs(farTimesNear) > limits<T>::max() * abs(depth))
  488. {
  489. throw IEX_NAMESPACE::DivzeroExc
  490. ("Bad call to DepthToZ function: value of `depth' "
  491. "is too small");
  492. }
  493. T farPlusNear = farTimesNear/depth + _farPlane + _nearPlane;
  494. if (abs(farMinusNear) < 1 &&
  495. abs(farPlusNear) > limits<T>::max() * abs(farMinusNear))
  496. {
  497. throw IEX_NAMESPACE::DivzeroExc
  498. ("Bad viewing frustum: near and far clipping planes "
  499. "are too close to each other");
  500. }
  501. T Zp = farPlusNear/farMinusNear;
  502. return long(0.5*(Zp+1)*zdiff) + zmin;
  503. }
  504. }
  505. template<class T>
  506. T Frustum<T>::screenRadius(const Vec3<T> &p, T radius) const
  507. {
  508. // Derivation:
  509. // Consider X-Z plane.
  510. // X coord of projection of p = xp = p.x * (-_nearPlane / p.z)
  511. // Let q be p + (radius, 0, 0).
  512. // X coord of projection of q = xq = (p.x - radius) * (-_nearPlane / p.z)
  513. // X coord of projection of segment from p to q = r = xp - xq
  514. // = radius * (-_nearPlane / p.z)
  515. // A similar analysis holds in the Y-Z plane.
  516. // So r is the quantity we want to return.
  517. if (abs(p.z) > 1 || abs(-_nearPlane) < limits<T>::max() * abs(p.z))
  518. {
  519. return radius * (-_nearPlane / p.z);
  520. }
  521. else
  522. {
  523. throw IEX_NAMESPACE::DivzeroExc
  524. ("Bad call to Frustum::screenRadius: the magnitude of `p' "
  525. "is too small");
  526. }
  527. return radius * (-_nearPlane / p.z);
  528. }
  529. template<class T>
  530. T Frustum<T>::worldRadius(const Vec3<T> &p, T radius) const
  531. {
  532. if (abs(-_nearPlane) > 1 || abs(p.z) < limits<T>::max() * abs(-_nearPlane))
  533. {
  534. return radius * (p.z / -_nearPlane);
  535. }
  536. else
  537. {
  538. throw IEX_NAMESPACE::DivzeroExc
  539. ("Bad viewing frustum: the near clipping plane is too "
  540. "close to zero");
  541. }
  542. }
  543. template<class T>
  544. void Frustum<T>::planes(Plane3<T> p[6]) const
  545. {
  546. //
  547. // Plane order: Top, Right, Bottom, Left, Near, Far.
  548. // Normals point outwards.
  549. //
  550. if (! _orthographic)
  551. {
  552. Vec3<T> a( _left, _bottom, -_nearPlane);
  553. Vec3<T> b( _left, _top, -_nearPlane);
  554. Vec3<T> c( _right, _top, -_nearPlane);
  555. Vec3<T> d( _right, _bottom, -_nearPlane);
  556. Vec3<T> o(0,0,0);
  557. p[0].set( o, c, b );
  558. p[1].set( o, d, c );
  559. p[2].set( o, a, d );
  560. p[3].set( o, b, a );
  561. }
  562. else
  563. {
  564. p[0].set( Vec3<T>( 0, 1, 0), _top );
  565. p[1].set( Vec3<T>( 1, 0, 0), _right );
  566. p[2].set( Vec3<T>( 0,-1, 0),-_bottom );
  567. p[3].set( Vec3<T>(-1, 0, 0),-_left );
  568. }
  569. p[4].set( Vec3<T>(0, 0, 1), -_nearPlane );
  570. p[5].set( Vec3<T>(0, 0,-1), _farPlane );
  571. }
  572. template<class T>
  573. void Frustum<T>::planes(Plane3<T> p[6], const Matrix44<T> &M) const
  574. {
  575. //
  576. // Plane order: Top, Right, Bottom, Left, Near, Far.
  577. // Normals point outwards.
  578. //
  579. Vec3<T> a = Vec3<T>( _left, _bottom, -_nearPlane) * M;
  580. Vec3<T> b = Vec3<T>( _left, _top, -_nearPlane) * M;
  581. Vec3<T> c = Vec3<T>( _right, _top, -_nearPlane) * M;
  582. Vec3<T> d = Vec3<T>( _right, _bottom, -_nearPlane) * M;
  583. if (! _orthographic)
  584. {
  585. double s = _farPlane / double(_nearPlane);
  586. T farLeft = (T) (s * _left);
  587. T farRight = (T) (s * _right);
  588. T farTop = (T) (s * _top);
  589. T farBottom = (T) (s * _bottom);
  590. Vec3<T> e = Vec3<T>( farLeft, farBottom, -_farPlane) * M;
  591. Vec3<T> f = Vec3<T>( farLeft, farTop, -_farPlane) * M;
  592. Vec3<T> g = Vec3<T>( farRight, farTop, -_farPlane) * M;
  593. Vec3<T> o = Vec3<T>(0,0,0) * M;
  594. p[0].set( o, c, b );
  595. p[1].set( o, d, c );
  596. p[2].set( o, a, d );
  597. p[3].set( o, b, a );
  598. p[4].set( a, d, c );
  599. p[5].set( e, f, g );
  600. }
  601. else
  602. {
  603. Vec3<T> e = Vec3<T>( _left, _bottom, -_farPlane) * M;
  604. Vec3<T> f = Vec3<T>( _left, _top, -_farPlane) * M;
  605. Vec3<T> g = Vec3<T>( _right, _top, -_farPlane) * M;
  606. Vec3<T> h = Vec3<T>( _right, _bottom, -_farPlane) * M;
  607. p[0].set( c, g, f );
  608. p[1].set( d, h, g );
  609. p[2].set( a, e, h );
  610. p[3].set( b, f, e );
  611. p[4].set( a, d, c );
  612. p[5].set( e, f, g );
  613. }
  614. }
  615. typedef Frustum<float> Frustumf;
  616. typedef Frustum<double> Frustumd;
  617. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  618. #if defined _WIN32 || defined _WIN64
  619. #ifdef _redef_near
  620. #define near
  621. #endif
  622. #ifdef _redef_far
  623. #define far
  624. #endif
  625. #endif
  626. #endif // INCLUDED_IMATHFRUSTUM_H