37 #ifndef INCLUDED_IMATHFRUSTUM_H 38 #define INCLUDED_IMATHFRUSTUM_H 47 #include "IexMathExc.h" 49 #if defined _WIN32 || defined _WIN64 166 T
ZToDepth(
long zval,
long min,
long max)
const;
168 long DepthToZ(T depth,
long zmin,
long zmax)
const;
217 set(near,far,fovx,fovy,aspect);
245 _near == src.
_near &&
247 _left == src.
_left &&
304 _orthographic = ortho;
310 if (fovx != 0 && fovy != 0)
311 throw Iex::ArgExc (
"fovx and fovy cannot both be non-zero.");
317 _top = ((_right - _left) / aspect) / 2;
324 _right = (_top - _bottom) * aspect / 2;
329 _orthographic =
false;
347 T rightMinusLeft = _right-_left;
348 T topMinusBottom = _top-_bottom;
350 if (
abs(topMinusBottom) < 1 &&
353 throw Iex::DivzeroExc (
"Bad viewing frustum: " 354 "aspect ratio cannot be computed.");
357 return rightMinusLeft / topMinusBottom;
363 T rightPlusLeft = _right+_left;
364 T rightMinusLeft = _right-_left;
366 T topPlusBottom = _top+_bottom;
367 T topMinusBottom = _top-_bottom;
369 T farPlusNear = _far+_near;
370 T farMinusNear = _far-_near;
372 if ((
abs(rightMinusLeft) < 1 &&
374 (
abs(topMinusBottom) < 1 &&
376 (
abs(farMinusNear) < 1 &&
379 throw Iex::DivzeroExc (
"Bad viewing frustum: " 380 "projection matrix cannot be computed.");
385 T tx = -rightPlusLeft / rightMinusLeft;
386 T ty = -topPlusBottom / topMinusBottom;
387 T tz = -farPlusNear / farMinusNear;
389 if ((
abs(rightMinusLeft) < 1 &&
391 (
abs(topMinusBottom) < 1 &&
393 (
abs(farMinusNear) < 1 &&
396 throw Iex::DivzeroExc (
"Bad viewing frustum: " 397 "projection matrix cannot be computed.");
400 T A = 2 / rightMinusLeft;
401 T B = 2 / topMinusBottom;
402 T C = -2 / farMinusNear;
411 T A = rightPlusLeft / rightMinusLeft;
412 T B = topPlusBottom / topMinusBottom;
413 T C = -farPlusNear / farMinusNear;
415 T farTimesNear = -2 * _far * _near;
416 if (
abs(farMinusNear) < 1 &&
419 throw Iex::DivzeroExc (
"Bad viewing frustum: " 420 "projection matrix cannot be computed.");
423 T D = farTimesNear / farMinusNear;
425 T twoTimesNear = 2 * _near;
427 if ((
abs(rightMinusLeft) < 1 &&
429 (
abs(topMinusBottom) < 1 &&
432 throw Iex::DivzeroExc (
"Bad viewing frustum: " 433 "projection matrix cannot be computed.");
436 T E = twoTimesNear / rightMinusLeft;
437 T F = twoTimesNear / topMinusBottom;
454 return Frustum<T>(_near, _far, bl.
x, tr.
x, tr.
y, bl.
y, _orthographic);
461 return Vec2<T>( _left + (_right-_left) * (1.f+s.
x) / 2.f,
462 _bottom + (_top-_bottom) * (1.f+s.
y) / 2.f );
468 T leftPlusRight = _left - 2 * p.
x + _right;
469 T leftMinusRight = _left-_right;
470 T bottomPlusTop = _bottom - 2 * p.
y + _top;
471 T bottomMinusTop = _bottom-_top;
473 if ((
abs(leftMinusRight) < 1 &&
475 (
abs(bottomMinusTop) < 1 &&
478 throw Iex::DivzeroExc
479 (
"Bad viewing frustum: " 480 "local-to-screen transformation cannot be computed");
483 return Vec2<T>( leftPlusRight / leftMinusRight,
484 bottomPlusTop / bottomMinusTop );
490 Vec2<T> point = screenToLocal(p);
501 if (orthographic() || point.
z == 0)
502 return localToScreen(
Vec2<T>( point.
x, point.
y ) );
504 return localToScreen(
Vec2<T>( point.
x * _near / -point.
z,
505 point.
y * _near / -point.
z ) );
511 int zdiff = zmax - zmin;
515 throw Iex::DivzeroExc
516 (
"Bad call to Frustum::ZToDepth: zmax == zmin");
519 if ( zval > zmax+1 ) zval -= zdiff;
521 T fzval = (T(zval) - T(zmin)) / T(zdiff);
522 return normalizedZToDepth(fzval);
528 T Zp = zval * 2.0 - 1;
532 return -(Zp*(_far-_near) + (_far+_near))/2;
536 T farTimesNear = 2 * _far * _near;
537 T farMinusNear = Zp * (_far - _near) - _far - _near;
539 if (
abs(farMinusNear) < 1 &&
542 throw Iex::DivzeroExc
543 (
"Frustum::normalizedZToDepth cannot be computed. The " 544 "near and far clipping planes of the viewing frustum " 545 "may be too close to each other");
548 return farTimesNear / farMinusNear;
555 long zdiff = zmax - zmin;
556 T farMinusNear = _far-_near;
560 T farPlusNear = 2*depth + _far + _near;
562 if (
abs(farMinusNear) < 1 &&
565 throw Iex::DivzeroExc
566 (
"Bad viewing frustum: near and far clipping planes " 567 "are too close to each other");
570 T Zp = -farPlusNear/farMinusNear;
571 return long(0.5*(Zp+1)*zdiff) + zmin;
577 T farTimesNear = 2*_far*_near;
578 if (
abs(depth) < 1 &&
581 throw Iex::DivzeroExc
582 (
"Bad call to DepthToZ function: value of `depth' " 586 T farPlusNear = farTimesNear/depth + _far + _near;
587 if (
abs(farMinusNear) < 1 &&
590 throw Iex::DivzeroExc
591 (
"Bad viewing frustum: near and far clipping planes " 592 "are too close to each other");
595 T Zp = farPlusNear/farMinusNear;
596 return long(0.5*(Zp+1)*zdiff) + zmin;
615 return radius * (-_near / p.
z);
619 throw Iex::DivzeroExc
620 (
"Bad call to Frustum::screenRadius: the magnitude of `p' " 624 return radius * (-_near / p.
z);
632 return radius * (p.
z / -_near);
636 throw Iex::DivzeroExc
637 (
"Bad viewing frustum: the near clipping plane is too " 652 Vec3<T> a( _left, _bottom, -_near);
653 Vec3<T> b( _left, _top, -_near);
654 Vec3<T> c( _right, _top, -_near);
655 Vec3<T> d( _right, _bottom, -_near);
689 double s = _far / double(_near);
690 T farLeft = (T) (s * _left);
691 T farRight = (T) (s * _right);
692 T farTop = (T) (s * _top);
693 T farBottom = (T) (s * _bottom);
727 #if defined _WIN32 || defined _WIN64 Frustum< float > Frustumf
Definition: ImathFrustum.h:720
virtual ~Frustum()
Definition: ImathFrustum.h:221
void set(const Vec3< T > &normal, T distance)
Definition: ImathPlane.h:158
T x
Definition: ImathVec.h:76
T left() const
Definition: ImathFrustum.h:125
long DepthToZ(T depth, long zmin, long zmax) const
Definition: ImathFrustum.h:553
Vec2< T > projectPointToScreen(const Vec3< T > &) const
Definition: ImathFrustum.h:499
const Frustum & operator=(const Frustum &)
Definition: ImathFrustum.h:227
T bottom() const
Definition: ImathFrustum.h:127
T y
Definition: ImathVec.h:76
Definition: ImathFrame.h:42
static T tan(T x)
Definition: ImathMath.h:99
bool _orthographic
Definition: ImathFrustum.h:186
Definition: ImathFrame.h:43
Frustum()
Definition: ImathFrustum.h:191
Frustum< T > window(T left, T right, T top, T bottom) const
Definition: ImathFrustum.h:447
T normalizedZToDepth(T zval) const
Definition: ImathFrustum.h:526
Vec2< T > screenToLocal(const Vec2< T > &) const
Definition: ImathFrustum.h:459
T fovy() const
Definition: ImathFrustum.h:339
static T atan2(T x, T y)
Definition: ImathMath.h:96
T near() const
Definition: ImathFrustum.h:121
void planes(Plane3< T > p[6])
Definition: ImathFrustum.h:643
bool intersect(const Line3< T > &line, Vec3< T > &intersection) const
Definition: ImathPlane.h:186
void set(T near, T far, T left, T right, T top, T bottom, bool ortho=false)
Definition: ImathFrustum.h:262
Vec2< T > localToScreen(const Vec2< T > &) const
Definition: ImathFrustum.h:466
T abs(T a)
Definition: ImathFun.h:53
T _right
Definition: ImathFrustum.h:183
bool orthographic() const
Definition: ImathFrustum.h:120
Definition: ImathLimits.h:117
T x
Definition: ImathVec.h:274
T y
Definition: ImathVec.h:274
T hither() const
Definition: ImathFrustum.h:122
Line3< T > projectScreenToRay(const Vec2< T > &) const
Definition: ImathFrustum.h:488
Frustum< double > Frustumd
Definition: ImathFrustum.h:721
T yon() const
Definition: ImathFrustum.h:124
T z
Definition: ImathVec.h:274
T _left
Definition: ImathFrustum.h:182
Definition: ImathVec.h:61
Definition: ImathLine.h:54
T _near
Definition: ImathFrustum.h:180
T right() const
Definition: ImathFrustum.h:126
bool operator==(const C *str, const StringViewBase< C, STDS > &sv) noexcept
Definition: StringView.h:273
T _far
Definition: ImathFrustum.h:181
T ZToDepth(long zval, long min, long max) const
Definition: ImathFrustum.h:509
T _bottom
Definition: ImathFrustum.h:185
T screenRadius(const Vec3< T > &p, T radius) const
Definition: ImathFrustum.h:601
Matrix44< T > projectionMatrix() const
Definition: ImathFrustum.h:361
T top() const
Definition: ImathFrustum.h:128
bool operator!=(const Frustum< T > &src) const
Definition: ImathFrustum.h:256
T fovx() const
Definition: ImathFrustum.h:333
void setOrthographic(bool)
Definition: ImathFrustum.h:302
T _top
Definition: ImathFrustum.h:184
Definition: ImathPlane.h:62
Definition: ImathBox.h:67
T worldRadius(const Vec3< T > &p, T radius) const
Definition: ImathFrustum.h:628
Definition: ImathFrustum.h:76
bool operator==(const Frustum< T > &src) const
Definition: ImathFrustum.h:242
void modifyNearAndFar(T near, T far)
Definition: ImathFrustum.h:274
T far() const
Definition: ImathFrustum.h:123
T aspect() const
Definition: ImathFrustum.h:345