OpenVDB  1.1.0
LegacyFrustum.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012-2013 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 //
30 //
32 
33 #ifndef OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
34 #define OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
35 
36 #include <iostream>
37 #include "Coord.h"
38 #include "Mat4.h"
39 #include "Vec3.h"
40 
41 
42 namespace openvdb {
44 namespace OPENVDB_VERSION_NAME {
45 namespace math {
46 namespace internal {
47 
50 {
51 public:
52  LegacyFrustum(std::istream& is)
53  {
54  // First read in the old transform's base class.
55  // the "extents"
56  Coord tmpMin, tmpMax;
57  is.read(reinterpret_cast<char*>(&tmpMin), sizeof(Coord::ValueType) * 3);
58  is.read(reinterpret_cast<char*>(&tmpMax), sizeof(Coord::ValueType) * 3);
59 
60  // set the extents
61  mExtents = CoordBBox(tmpMin, tmpMax);
62 
63  // read the old-frustum class member data
64  //Mat4d tmpW2C;
65  Mat4d tmpW2C, tmpC2S, tmpS2C, tmpWorldToLocal;
66  Mat4d tmpS2U, tmpXYLocalToUnit, tmpZLocalToUnit;
67  Real tmpWindow[6];
68  Real tmpPadding;
69 
70  //Mat4d tmpXYUnitToLocal, tmpZUnitToLocal
71 
72  // read in each matrix.
73  is.read(reinterpret_cast<char*>(&tmpW2C),
75  is.read(reinterpret_cast<char*>(&mC2W),
76  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
77  is.read(reinterpret_cast<char*>(&tmpC2S),
78  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
79  is.read(reinterpret_cast<char*>(&tmpS2C),
80  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
81  is.read(reinterpret_cast<char*>(&tmpWorldToLocal),
82  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
83  is.read(reinterpret_cast<char*>(&mLocalToWorld),
84  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
85 
86  is.read(reinterpret_cast<char*>(&tmpWindow[0]), sizeof(Real));
87  is.read(reinterpret_cast<char*>(&tmpWindow[1]), sizeof(Real));
88  is.read(reinterpret_cast<char*>(&tmpWindow[2]), sizeof(Real));
89  is.read(reinterpret_cast<char*>(&tmpWindow[3]), sizeof(Real));
90  is.read(reinterpret_cast<char*>(&tmpWindow[4]), sizeof(Real));
91  is.read(reinterpret_cast<char*>(&tmpWindow[5]), sizeof(Real));
92 
93  is.read(reinterpret_cast<char*>(&tmpPadding), sizeof(Real));
94 
95  is.read(reinterpret_cast<char*>(&tmpS2U),
96  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
97  is.read(reinterpret_cast<char*>(&mXYUnitToLocal),
98  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
99  is.read(reinterpret_cast<char*>(&tmpXYLocalToUnit),
100  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
101  is.read(reinterpret_cast<char*>(&mZUnitToLocal),
102  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
103  is.read(reinterpret_cast<char*>(&tmpZLocalToUnit),
104  sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
105 
106 
107  mNearPlane = tmpWindow[4];
108  mFarPlane = tmpWindow[5];
109 
110  // Look up the world space corners of the
111  // frustum grid.
112  mFrNearOrigin = unitToLocalFrustum(Vec3R(0,0,0));
113  mFrFarOrigin = unitToLocalFrustum(Vec3R(0,0,1));
114 
115  Vec3d frNearXTip = unitToLocalFrustum(Vec3R(1,0,0));
116  Vec3d frNearYTip = unitToLocalFrustum(Vec3R(0,1,0));
117  mFrNearXBasis = frNearXTip - mFrNearOrigin;
118  mFrNearYBasis = frNearYTip - mFrNearOrigin;
119 
120  Vec3R frFarXTip = unitToLocalFrustum(Vec3R(1,0,1));
121  Vec3R frFarYTip = unitToLocalFrustum(Vec3R(0,1,1));
122  mFrFarXBasis = frFarXTip - mFrFarOrigin;
123  mFrFarYBasis = frFarYTip - mFrFarOrigin;
124  }
125 
127 
128  const Mat4d& getCamXForm() const {return mC2W; }
129 
130  double getDepth() const {return (mFarPlane - mNearPlane); }
131  double getTaper() const {
132 
133  return getNearPlaneWidth() / getFarPlaneWidth();
134  }
135 
136  double getNearPlaneWidth() const {
137  double nearPlaneWidth = (unitToWorld(Vec3d(0,0,0)) - unitToWorld(Vec3d(1,0,0))).length();
138  return nearPlaneWidth;
139  }
140 
141  double getFarPlaneWidth() const {
142  double farPlaneWidth = (unitToWorld(Vec3d(0,0,1)) - unitToWorld(Vec3d(1,0,1))).length();
143  return farPlaneWidth;
144  }
145 
146  double getNearPlaneDist() const { return mNearPlane; }
147 
148  const CoordBBox& getBBox() const {return mExtents; }
149 
150  Vec3d unitToWorld(const Vec3d& in) const {return mLocalToWorld.transform( unitToLocal(in) ); }
151 
152 private:
153  LegacyFrustum(){};
154 
155  Vec3d unitToLocal(const Vec3d& U) const {
156 
157  // We first find the local space coordinates
158  // of the unit point projected onto the near
159  // and far planes of the frustum by using a
160  // linear combination of the planes basis vectors
161  Vec3d nearLS = ( U[0] * mFrNearXBasis ) + ( U[1] * mFrNearYBasis ) + mFrNearOrigin;
162  Vec3d farLS = ( U[0] * mFrFarXBasis ) + ( U[1] * mFrFarYBasis ) + mFrFarOrigin;
163 
164  // then we lerp the two ws points in frustum z space
165  return U[2] * farLS + ( 1.0 - U[2] ) * nearLS;
166  }
167 
168  Vec3d unitToLocalFrustum(const Vec3d& u) const {
169  Vec3d fzu = mZUnitToLocal.transformH(u);
170  Vec3d fu = u;
171  fu[2] = fzu.z();
172  return mXYUnitToLocal.transformH(fu);
173  }
174 
175 private:
176  Mat4d mC2W, mLocalToWorld, mXYUnitToLocal, mZUnitToLocal;
177  CoordBBox mExtents;
178  Vec3d mFrNearXBasis, mFrNearYBasis, mFrFarXBasis, mFrFarYBasis;
179  Vec3d mFrNearOrigin, mFrFarOrigin;
180  double mNearPlane, mFarPlane;
181 };
182 
183 } // namespace internal
184 } // namespace math
185 } // namespace OPENVDB_VERSION_NAME
186 } // namespace openvdb
187 
188 #endif // OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
189 
190 // Copyright (c) 2012-2013 DreamWorks Animation LLC
191 // All rights reserved. This software is distributed under the
192 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )