OpenVDB  12.0.0
LegacyFrustum.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 /// @file math/LegacyFrustum.h
5 
6 #ifndef OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
7 #define OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
8 
9 #include <iostream>
10 #include <openvdb/Types.h> // for Real typedef
11 #include "Coord.h"
12 #include "Mat4.h"
13 #include "Vec3.h"
14 
15 
16 namespace openvdb {
18 namespace OPENVDB_VERSION_NAME {
19 namespace math {
20 
21 /// @cond OPENVDB_DOCS_INTERNAL
22 
23 namespace internal {
24 
25 /// @brief LegacyFrustum class used at DreamWorks for converting old vdb files.
26 class LegacyFrustum
27 {
28 public:
29  LegacyFrustum(std::istream& is)
30  {
31  // First read in the old transform's base class.
32  // the "extents"
33  Vec3i tmpMin, tmpMax;
34  is.read(reinterpret_cast<char*>(&tmpMin), sizeof(Vec3i::ValueType) * 3);
35  is.read(reinterpret_cast<char*>(&tmpMax), sizeof(Vec3i::ValueType) * 3);
36 
37  Coord tmpMinCoord(tmpMin);
38  Coord tmpMaxCoord(tmpMax);
39 
40  // set the extents
41  mExtents = CoordBBox(tmpMinCoord, tmpMaxCoord);
42 
43  // read the old-frustum class member data
44  //Mat4d tmpW2C;
45  Mat4d tmpW2C, tmpC2S, tmpS2C, tmpWorldToLocal;
46  Mat4d tmpS2U, tmpXYLocalToUnit, tmpZLocalToUnit;
47  Real tmpWindow[6];
48  Real tmpPadding;
49 
50  //Mat4d tmpXYUnitToLocal, tmpZUnitToLocal
51 
52  // read in each matrix.
53  is.read(reinterpret_cast<char*>(&tmpW2C),
54  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
55  is.read(reinterpret_cast<char*>(&mC2W),
56  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
57  is.read(reinterpret_cast<char*>(&tmpC2S),
58  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
59  is.read(reinterpret_cast<char*>(&tmpS2C),
60  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
61  is.read(reinterpret_cast<char*>(&tmpWorldToLocal),
62  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
63  is.read(reinterpret_cast<char*>(&mLocalToWorld),
64  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
65 
66  is.read(reinterpret_cast<char*>(&tmpWindow[0]), sizeof(Real));
67  is.read(reinterpret_cast<char*>(&tmpWindow[1]), sizeof(Real));
68  is.read(reinterpret_cast<char*>(&tmpWindow[2]), sizeof(Real));
69  is.read(reinterpret_cast<char*>(&tmpWindow[3]), sizeof(Real));
70  is.read(reinterpret_cast<char*>(&tmpWindow[4]), sizeof(Real));
71  is.read(reinterpret_cast<char*>(&tmpWindow[5]), sizeof(Real));
72 
73  is.read(reinterpret_cast<char*>(&tmpPadding), sizeof(Real));
74 
75  is.read(reinterpret_cast<char*>(&tmpS2U),
76  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
77  is.read(reinterpret_cast<char*>(&mXYUnitToLocal),
78  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
79  is.read(reinterpret_cast<char*>(&tmpXYLocalToUnit),
80  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
81  is.read(reinterpret_cast<char*>(&mZUnitToLocal),
82  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
83  is.read(reinterpret_cast<char*>(&tmpZLocalToUnit),
84  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
85 
86 
87  mNearPlane = tmpWindow[4];
88  mFarPlane = tmpWindow[5];
89 
90  // Look up the world space corners of the
91  // frustum grid.
92  mFrNearOrigin = unitToLocalFrustum(Vec3R(0,0,0));
93  mFrFarOrigin = unitToLocalFrustum(Vec3R(0,0,1));
94 
95  Vec3d frNearXTip = unitToLocalFrustum(Vec3R(1,0,0));
96  Vec3d frNearYTip = unitToLocalFrustum(Vec3R(0,1,0));
97  mFrNearXBasis = frNearXTip - mFrNearOrigin;
98  mFrNearYBasis = frNearYTip - mFrNearOrigin;
99 
100  Vec3R frFarXTip = unitToLocalFrustum(Vec3R(1,0,1));
101  Vec3R frFarYTip = unitToLocalFrustum(Vec3R(0,1,1));
102  mFrFarXBasis = frFarXTip - mFrFarOrigin;
103  mFrFarYBasis = frFarYTip - mFrFarOrigin;
104  }
105 
106  ~LegacyFrustum() {}
107 
108  const Mat4d& getCamXForm() const {return mC2W; }
109 
110  double getDepth() const {return (mFarPlane - mNearPlane); }
111  double getTaper() const {
112 
113  return getNearPlaneWidth() / getFarPlaneWidth();
114  }
115 
116  double getNearPlaneWidth() const {
117  double nearPlaneWidth = (unitToWorld(Vec3d(0,0,0)) - unitToWorld(Vec3d(1,0,0))).length();
118  return nearPlaneWidth;
119  }
120 
121  double getFarPlaneWidth() const {
122  double farPlaneWidth = (unitToWorld(Vec3d(0,0,1)) - unitToWorld(Vec3d(1,0,1))).length();
123  return farPlaneWidth;
124  }
125 
126  double getNearPlaneDist() const { return mNearPlane; }
127 
128  const CoordBBox& getBBox() const {return mExtents; }
129 
130  Vec3d unitToWorld(const Vec3d& in) const {return mLocalToWorld.transform( unitToLocal(in) ); }
131 
132 private:
133  LegacyFrustum() {}
134 
135  Vec3d unitToLocal(const Vec3d& U) const {
136 
137  // We first find the local space coordinates
138  // of the unit point projected onto the near
139  // and far planes of the frustum by using a
140  // linear combination of the planes basis vectors
141  Vec3d nearLS = ( U[0] * mFrNearXBasis ) + ( U[1] * mFrNearYBasis ) + mFrNearOrigin;
142  Vec3d farLS = ( U[0] * mFrFarXBasis ) + ( U[1] * mFrFarYBasis ) + mFrFarOrigin;
143 
144  // then we lerp the two ws points in frustum z space
145  return U[2] * farLS + ( 1.0 - U[2] ) * nearLS;
146  }
147 
148  Vec3d unitToLocalFrustum(const Vec3d& u) const {
149  Vec3d fzu = mZUnitToLocal.transformH(u);
150  Vec3d fu = u;
151  fu[2] = fzu.z();
152  return mXYUnitToLocal.transformH(fu);
153  }
154 
155 private:
156  Mat4d mC2W, mLocalToWorld, mXYUnitToLocal, mZUnitToLocal;
157  CoordBBox mExtents;
158  Vec3d mFrNearXBasis, mFrNearYBasis, mFrFarXBasis, mFrFarYBasis;
159  Vec3d mFrNearOrigin, mFrFarOrigin;
160  double mNearPlane, mFarPlane;
161 };
162 
163 } // namespace internal
164 
165 /// @endcond
166 
167 } // namespace math
168 } // namespace OPENVDB_VERSION_NAME
169 } // namespace openvdb
170 
171 #endif // OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
Mat4< double > Mat4d
Definition: Mat4.h:1355
T & z()
Definition: Vec3.h:88
Vec3< double > Vec3d
Definition: Vec3.h:665
Definition: Exceptions.h:13
double Real
Definition: Types.h:60
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
Vec3< int32_t > Vec3i
Definition: Vec3.h:662
math::Vec3< Real > Vec3R
Definition: Types.h:72
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218