HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LegacyFrustum.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 //
31 /// @file math/LegacyFrustum.h
32 
33 #ifndef OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
34 #define OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
35 
36 #include <iostream>
37 #include <openvdb/Types.h> // for Real typedef
38 #include "Coord.h"
39 #include "Mat4.h"
40 #include "Vec3.h"
41 
42 
43 namespace openvdb {
45 namespace OPENVDB_VERSION_NAME {
46 namespace math {
47 namespace internal {
48 
49 /// @brief LegacyFrustum class used at DreamWorks for converting old vdb files.
51 {
52 public:
53  LegacyFrustum(std::istream& is)
54  {
55  // First read in the old transform's base class.
56  // the "extents"
57  Vec3i tmpMin, tmpMax;
58  is.read(reinterpret_cast<char*>(&tmpMin), sizeof(Vec3i::ValueType) * 3);
59  is.read(reinterpret_cast<char*>(&tmpMax), sizeof(Vec3i::ValueType) * 3);
60 
61  Coord tmpMinCoord(tmpMin);
62  Coord tmpMaxCoord(tmpMax);
63 
64  // set the extents
65  mExtents = CoordBBox(tmpMinCoord, tmpMaxCoord);
66 
67  // read the old-frustum class member data
68  //Mat4d tmpW2C;
69  Mat4d tmpW2C, tmpC2S, tmpS2C, tmpWorldToLocal;
70  Mat4d tmpS2U, tmpXYLocalToUnit, tmpZLocalToUnit;
71  Real tmpWindow[6];
72  Real tmpPadding;
73 
74  //Mat4d tmpXYUnitToLocal, tmpZUnitToLocal
75 
76  // read in each matrix.
77  is.read(reinterpret_cast<char*>(&tmpW2C),
79  is.read(reinterpret_cast<char*>(&mC2W),
80  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
81  is.read(reinterpret_cast<char*>(&tmpC2S),
82  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
83  is.read(reinterpret_cast<char*>(&tmpS2C),
84  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
85  is.read(reinterpret_cast<char*>(&tmpWorldToLocal),
86  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
87  is.read(reinterpret_cast<char*>(&mLocalToWorld),
88  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
89 
90  is.read(reinterpret_cast<char*>(&tmpWindow[0]), sizeof(Real));
91  is.read(reinterpret_cast<char*>(&tmpWindow[1]), sizeof(Real));
92  is.read(reinterpret_cast<char*>(&tmpWindow[2]), sizeof(Real));
93  is.read(reinterpret_cast<char*>(&tmpWindow[3]), sizeof(Real));
94  is.read(reinterpret_cast<char*>(&tmpWindow[4]), sizeof(Real));
95  is.read(reinterpret_cast<char*>(&tmpWindow[5]), sizeof(Real));
96 
97  is.read(reinterpret_cast<char*>(&tmpPadding), sizeof(Real));
98 
99  is.read(reinterpret_cast<char*>(&tmpS2U),
100  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
101  is.read(reinterpret_cast<char*>(&mXYUnitToLocal),
102  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
103  is.read(reinterpret_cast<char*>(&tmpXYLocalToUnit),
104  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
105  is.read(reinterpret_cast<char*>(&mZUnitToLocal),
106  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
107  is.read(reinterpret_cast<char*>(&tmpZLocalToUnit),
108  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
109 
110 
111  mNearPlane = tmpWindow[4];
112  mFarPlane = tmpWindow[5];
113 
114  // Look up the world space corners of the
115  // frustum grid.
116  mFrNearOrigin = unitToLocalFrustum(Vec3R(0,0,0));
117  mFrFarOrigin = unitToLocalFrustum(Vec3R(0,0,1));
118 
119  Vec3d frNearXTip = unitToLocalFrustum(Vec3R(1,0,0));
120  Vec3d frNearYTip = unitToLocalFrustum(Vec3R(0,1,0));
121  mFrNearXBasis = frNearXTip - mFrNearOrigin;
122  mFrNearYBasis = frNearYTip - mFrNearOrigin;
123 
124  Vec3R frFarXTip = unitToLocalFrustum(Vec3R(1,0,1));
125  Vec3R frFarYTip = unitToLocalFrustum(Vec3R(0,1,1));
126  mFrFarXBasis = frFarXTip - mFrFarOrigin;
127  mFrFarYBasis = frFarYTip - mFrFarOrigin;
128  }
129 
131 
132  const Mat4d& getCamXForm() const {return mC2W; }
133 
134  double getDepth() const {return (mFarPlane - mNearPlane); }
135  double getTaper() const {
136 
137  return getNearPlaneWidth() / getFarPlaneWidth();
138  }
139 
140  double getNearPlaneWidth() const {
141  double nearPlaneWidth = (unitToWorld(Vec3d(0,0,0)) - unitToWorld(Vec3d(1,0,0))).length();
142  return nearPlaneWidth;
143  }
144 
145  double getFarPlaneWidth() const {
146  double farPlaneWidth = (unitToWorld(Vec3d(0,0,1)) - unitToWorld(Vec3d(1,0,1))).length();
147  return farPlaneWidth;
148  }
149 
150  double getNearPlaneDist() const { return mNearPlane; }
151 
152  const CoordBBox& getBBox() const {return mExtents; }
153 
154  Vec3d unitToWorld(const Vec3d& in) const {return mLocalToWorld.transform( unitToLocal(in) ); }
155 
156 private:
157  LegacyFrustum() {}
158 
159  Vec3d unitToLocal(const Vec3d& U) const {
160 
161  // We first find the local space coordinates
162  // of the unit point projected onto the near
163  // and far planes of the frustum by using a
164  // linear combination of the planes basis vectors
165  Vec3d nearLS = ( U[0] * mFrNearXBasis ) + ( U[1] * mFrNearYBasis ) + mFrNearOrigin;
166  Vec3d farLS = ( U[0] * mFrFarXBasis ) + ( U[1] * mFrFarYBasis ) + mFrFarOrigin;
167 
168  // then we lerp the two ws points in frustum z space
169  return U[2] * farLS + ( 1.0 - U[2] ) * nearLS;
170  }
171 
172  Vec3d unitToLocalFrustum(const Vec3d& u) const {
173  Vec3d fzu = mZUnitToLocal.transformH(u);
174  Vec3d fu = u;
175  fu[2] = fzu.z();
176  return mXYUnitToLocal.transformH(fu);
177  }
178 
179 private:
180  Mat4d mC2W, mLocalToWorld, mXYUnitToLocal, mZUnitToLocal;
181  CoordBBox mExtents;
182  Vec3d mFrNearXBasis, mFrNearYBasis, mFrFarXBasis, mFrFarYBasis;
183  Vec3d mFrNearOrigin, mFrFarOrigin;
184  double mNearPlane, mFarPlane;
185 };
186 
187 } // namespace internal
188 } // namespace math
189 } // namespace OPENVDB_VERSION_NAME
190 } // namespace openvdb
191 
192 #endif // OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
193 
194 // Copyright (c) 2012-2018 DreamWorks Animation LLC
195 // All rights reserved. This software is distributed under the
196 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
double value_type
Data type held by the matrix.
Definition: Mat4.h:61
math::Vec3< Real > Vec3R
Definition: Types.h:79
Vec3< T0 > transformH(const Vec3< T0 > &p) const
Transform a Vec3 by post-multiplication, doing homogenous divison.
Definition: Mat4.h:1072
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
void read(std::istream &is)
Definition: Mat.h:146
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:264
LegacyFrustum class used at DreamWorks for converting old vdb files.
Definition: LegacyFrustum.h:50
Vec4< T0 > transform(const Vec4< T0 > &v) const
Transform a Vec4 by post-multiplication.
Definition: Mat4.h:1044
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:794