Loading...
Searching...
No Matches
LegacyFrustum.h
Go to the documentation of this file.
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
//
/// @file math/LegacyFrustum.h
#ifndef OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
#define OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
#include <iostream>
#include <openvdb/Types.h> // for Real typedef
#include "Coord.h"
#include "Mat4.h"
#include "Vec3.h"
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace math {
/// @cond OPENVDB_DOCS_INTERNAL
namespace internal {
/// @brief LegacyFrustum class used at DreamWorks for converting old vdb files.
class LegacyFrustum
{
public:
LegacyFrustum(std::istream& is)
{
// First read in the old transform's base class.
// the "extents"
Vec3i tmpMin, tmpMax;
is.read(reinterpret_cast<char*>(&tmpMin), sizeof(Vec3i::ValueType) * 3);
is.read(reinterpret_cast<char*>(&tmpMax), sizeof(Vec3i::ValueType) * 3);
Coord tmpMinCoord(tmpMin);
Coord tmpMaxCoord(tmpMax);
// set the extents
mExtents = CoordBBox(tmpMinCoord, tmpMaxCoord);
// read the old-frustum class member data
//Mat4d tmpW2C;
Mat4d tmpW2C, tmpC2S, tmpS2C, tmpWorldToLocal;
Mat4d tmpS2U, tmpXYLocalToUnit, tmpZLocalToUnit;
Real tmpWindow[6];
Real tmpPadding;
//Mat4d tmpXYUnitToLocal, tmpZUnitToLocal
// read in each matrix.
is.read(reinterpret_cast<char*>(&tmpW2C),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
is.read(reinterpret_cast<char*>(&mC2W),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
is.read(reinterpret_cast<char*>(&tmpC2S),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
is.read(reinterpret_cast<char*>(&tmpS2C),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
is.read(reinterpret_cast<char*>(&tmpWorldToLocal),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
is.read(reinterpret_cast<char*>(&mLocalToWorld),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
is.read(reinterpret_cast<char*>(&tmpWindow[0]), sizeof(Real));
is.read(reinterpret_cast<char*>(&tmpWindow[1]), sizeof(Real));
is.read(reinterpret_cast<char*>(&tmpWindow[2]), sizeof(Real));
is.read(reinterpret_cast<char*>(&tmpWindow[3]), sizeof(Real));
is.read(reinterpret_cast<char*>(&tmpWindow[4]), sizeof(Real));
is.read(reinterpret_cast<char*>(&tmpWindow[5]), sizeof(Real));
is.read(reinterpret_cast<char*>(&tmpPadding), sizeof(Real));
is.read(reinterpret_cast<char*>(&tmpS2U),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
is.read(reinterpret_cast<char*>(&mXYUnitToLocal),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
is.read(reinterpret_cast<char*>(&tmpXYLocalToUnit),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
is.read(reinterpret_cast<char*>(&mZUnitToLocal),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
is.read(reinterpret_cast<char*>(&tmpZLocalToUnit),
sizeof(Mat4d::value_type) * Mat4d::size * Mat4d::size);
mNearPlane = tmpWindow[4];
mFarPlane = tmpWindow[5];
// Look up the world space corners of the
// frustum grid.
mFrNearOrigin = unitToLocalFrustum(Vec3R(0,0,0));
mFrFarOrigin = unitToLocalFrustum(Vec3R(0,0,1));
Vec3d frNearXTip = unitToLocalFrustum(Vec3R(1,0,0));
Vec3d frNearYTip = unitToLocalFrustum(Vec3R(0,1,0));
mFrNearXBasis = frNearXTip - mFrNearOrigin;
mFrNearYBasis = frNearYTip - mFrNearOrigin;
Vec3R frFarXTip = unitToLocalFrustum(Vec3R(1,0,1));
Vec3R frFarYTip = unitToLocalFrustum(Vec3R(0,1,1));
mFrFarXBasis = frFarXTip - mFrFarOrigin;
mFrFarYBasis = frFarYTip - mFrFarOrigin;
}
~LegacyFrustum() {}
const Mat4d& getCamXForm() const {return mC2W; }
double getDepth() const {return (mFarPlane - mNearPlane); }
double getTaper() const {
return getNearPlaneWidth() / getFarPlaneWidth();
}
double getNearPlaneWidth() const {
double nearPlaneWidth = (unitToWorld(Vec3d(0,0,0)) - unitToWorld(Vec3d(1,0,0))).length();
return nearPlaneWidth;
}
double getFarPlaneWidth() const {
double farPlaneWidth = (unitToWorld(Vec3d(0,0,1)) - unitToWorld(Vec3d(1,0,1))).length();
return farPlaneWidth;
}
double getNearPlaneDist() const { return mNearPlane; }
const CoordBBox& getBBox() const {return mExtents; }
Vec3d unitToWorld(const Vec3d& in) const {return mLocalToWorld.transform( unitToLocal(in) ); }
private:
LegacyFrustum() {}
Vec3d unitToLocal(const Vec3d& U) const {
// We first find the local space coordinates
// of the unit point projected onto the near
// and far planes of the frustum by using a
// linear combination of the planes basis vectors
Vec3d nearLS = ( U[0] * mFrNearXBasis ) + ( U[1] * mFrNearYBasis ) + mFrNearOrigin;
Vec3d farLS = ( U[0] * mFrFarXBasis ) + ( U[1] * mFrFarYBasis ) + mFrFarOrigin;
// then we lerp the two ws points in frustum z space
return U[2] * farLS + ( 1.0 - U[2] ) * nearLS;
}
Vec3d unitToLocalFrustum(const Vec3d& u) const {
Vec3d fzu = mZUnitToLocal.transformH(u);
Vec3d fu = u;
fu[2] = fzu.z();
return mXYUnitToLocal.transformH(fu);
}
private:
Mat4d mC2W, mLocalToWorld, mXYUnitToLocal, mZUnitToLocal;
CoordBBox mExtents;
Vec3d mFrNearXBasis, mFrNearYBasis, mFrFarXBasis, mFrFarYBasis;
Vec3d mFrNearOrigin, mFrFarOrigin;
double mNearPlane, mFarPlane;
};
} // namespace internal
/// @endcond
} // namespace math
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_MATH_LEGACYFRUSTUM_HAS_BEEN_INCLUDED
Coord.h
Mat4.h
Types.h
Vec3.h
nanovdb::CoordBBox
BBox< Coord > CoordBBox
Definition NanoVDB.h:2535
openvdb::v11_0::math::Vec3d
Vec3< double > Vec3d
Definition Vec3.h:664
openvdb::v11_0::math::Mat4d
Mat4< double > Mat4d
Definition Mat4.h:1354
openvdb::v11_0::math::Vec3i
Vec3< int32_t > Vec3i
Definition Vec3.h:661
openvdb::v11_0::Real
double Real
Definition Types.h:60
openvdb::v11_0::Vec3R
math::Vec3< Real > Vec3R
Definition Types.h:72
openvdb
Definition Exceptions.h:13
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212