OpenVDB: LegacyFrustum.h Source File

OpenVDB 7.0.0

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