Loading...
Searching...
No Matches
Transform.h
Go to the documentation of this file.
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
#ifndef OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
#define OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
#include "Maps.h"
#include <openvdb/Types.h>
#include <iosfwd>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace math {
// Forward declaration
class Transform;
// Utility methods
/// @brief Calculate an axis-aligned bounding box in index space from an
/// axis-aligned bounding box in world space.
/// @see Transform::worldToIndex(const BBoxd&) const
OPENVDB_API void
calculateBounds(const Transform& t, const Vec3d& minWS, const Vec3d& maxWS,
Vec3d& minIS, Vec3d& maxIS);
/// @todo Calculate an axis-aligned bounding box in index space from a
/// bounding sphere in world space.
//void calculateBounds(const Transform& t, const Vec3d& center, const Real radius,
// Vec3d& minIS, Vec3d& maxIS);
////////////////////////////////////////
/// @class Transform
class OPENVDB_API Transform
{
public:
using Ptr = SharedPtr<Transform>;
using ConstPtr = SharedPtr<const Transform>;
Transform(): mMap(MapBase::Ptr(new ScaleMap())) {}
Transform(const MapBase::Ptr&);
Transform(const Transform&);
~Transform() {}
Ptr copy() const { return Ptr(new Transform(mMap->copy())); }
//@{
/// @brief Create and return a shared pointer to a new transform.
static Transform::Ptr createLinearTransform(double voxelSize = 1.0);
static Transform::Ptr createLinearTransform(const Mat4R&);
static Transform::Ptr createFrustumTransform(const BBoxd&, double taper,
double depth, double voxelSize = 1.0);
//@}
/// Return @c true if the transformation map is exclusively linear/affine.
bool isLinear() const { return mMap->isLinear(); }
/// Return @c true if the transform is equivalent to an idenity.
bool isIdentity() const ;
/// Return the transformation map's type-name
Name mapType() const { return mMap->type(); }
//@{
/// @brief Update the linear (affine) map by prepending or
/// postfixing the appropriate operation. In the case of
/// a frustum, the pre-operations apply to the linear part
/// of the transform and not the entire transform, while the
/// post-operations are allways applied last.
void preRotate(double radians, const Axis axis = X_AXIS);
void preTranslate(const Vec3d&);
void preScale(const Vec3d&);
void preScale(double);
void preShear(double shear, Axis axis0, Axis axis1);
void preMult(const Mat4d&);
void preMult(const Mat3d&);
void postRotate(double radians, const Axis axis = X_AXIS);
void postTranslate(const Vec3d&);
void postScale(const Vec3d&);
void postScale(double);
void postShear(double shear, Axis axis0, Axis axis1);
void postMult(const Mat4d&);
void postMult(const Mat3d&);
//@}
/// Return the size of a voxel using the linear component of the map.
Vec3d voxelSize() const { return mMap->voxelSize(); }
/// @brief Return the size of a voxel at position (x, y, z).
/// @note Maps that have a nonlinear component (e.g., perspective and frustum maps)
/// have position-dependent voxel sizes.
Vec3d voxelSize(const Vec3d& xyz) const { return mMap->voxelSize(xyz); }
/// Return the voxel volume of the linear component of the map.
double voxelVolume() const { return mMap->determinant(); }
/// Return the voxel volume at position (x, y, z).
double voxelVolume(const Vec3d& xyz) const { return mMap->determinant(xyz); }
/// Return true if the voxels in world space are uniformly sized cubes
bool hasUniformScale() const { return mMap->hasUniformScale(); }
//@{
/// @brief Apply this transformation to the given coordinates.
Vec3d indexToWorld(const Vec3d& xyz) const { return mMap->applyMap(xyz); }
Vec3d indexToWorld(const Coord& ijk) const { return mMap->applyMap(ijk.asVec3d()); }
Vec3d worldToIndex(const Vec3d& xyz) const { return mMap->applyInverseMap(xyz); }
Coord worldToIndexCellCentered(const Vec3d& xyz) const {return Coord::round(worldToIndex(xyz));}
Coord worldToIndexNodeCentered(const Vec3d& xyz) const {return Coord::floor(worldToIndex(xyz));}
//@}
//@{
/// @brief Apply this transformation to the given index-space bounding box.
/// @return an axis-aligned world-space bounding box
BBoxd indexToWorld(const CoordBBox&) const;
BBoxd indexToWorld(const BBoxd&) const;
//@}
//@{
/// @brief Apply the inverse of this transformation to the given world-space bounding box.
/// @return an axis-aligned index-space bounding box
BBoxd worldToIndex(const BBoxd&) const;
CoordBBox worldToIndexCellCentered(const BBoxd&) const;
CoordBBox worldToIndexNodeCentered(const BBoxd&) const;
//@}
//@{
/// Return a base pointer to the transformation map.
MapBase::ConstPtr baseMap() const { return mMap; }
MapBase::Ptr baseMap() { return mMap; }
//@}
//@{
/// @brief Return the result of downcasting the base map pointer to a
/// @c MapType pointer, or return a null pointer if the types are incompatible.
template<typename MapType> typename MapType::Ptr map();
template<typename MapType> typename MapType::ConstPtr map() const;
template<typename MapType> typename MapType::ConstPtr constMap() const;
//@}
/// Unserialize this transform from the given stream.
void read(std::istream&);
/// Serialize this transform to the given stream.
void write(std::ostream&) const;
/// @brief Print a description of this transform.
/// @param os a stream to which to write textual information
/// @param indent a string with which to prefix each line of text
void print(std::ostream& os = std::cout, const std::string& indent = "") const;
bool operator==(const Transform& other) const;
inline bool operator!=(const Transform& other) const { return !(*this == other); }
private:
MapBase::Ptr mMap;
}; // class Transform
OPENVDB_API std::ostream& operator<<(std::ostream&, const Transform&);
////////////////////////////////////////
template<typename MapType>
inline typename MapType::Ptr
Transform::map()
{
if (mMap->type() == MapType::mapType()) {
return StaticPtrCast<MapType>(mMap);
}
return typename MapType::Ptr();
}
template<typename MapType>
inline typename MapType::ConstPtr
Transform::map() const
{
return ConstPtrCast<const MapType>(
const_cast<Transform*>(this)->map<MapType>());
}
template<typename MapType>
inline typename MapType::ConstPtr
Transform::constMap() const
{
return map<MapType>();
}
////////////////////////////////////////
/// Helper function used internally by processTypedMap()
template<typename ResolvedMapType, typename OpType>
inline void
doProcessTypedMap(Transform& transform, OpType& op)
{
ResolvedMapType& resolvedMap = *transform.map<ResolvedMapType>();
op.template operator()<ResolvedMapType>(resolvedMap);
}
/// Helper function used internally by processTypedMap()
template<typename ResolvedMapType, typename OpType>
inline void
doProcessTypedMap(const Transform& transform, OpType& op)
{
const ResolvedMapType& resolvedMap = *transform.map<ResolvedMapType>();
op.template operator()<ResolvedMapType>(resolvedMap);
}
/// @brief Utility function that, given a generic map pointer,
/// calls a functor on the fully-resoved map
///
/// Usage:
/// @code
/// struct Foo {
/// template<typename MapT>
/// void operator()(const MapT& map) const { blah }
/// };
///
/// processTypedMap(myMap, Foo());
/// @endcode
///
/// @return @c false if the grid type is unknown or unhandled.
template<typename TransformType, typename OpType>
bool
processTypedMap(TransformType& transform, OpType& op)
{
using namespace openvdb;
const Name mapType = transform.mapType();
if (mapType == UniformScaleMap::mapType()) {
doProcessTypedMap<UniformScaleMap, OpType>(transform, op);
} else if (mapType == UniformScaleTranslateMap::mapType()) {
doProcessTypedMap<UniformScaleTranslateMap, OpType>(transform, op);
} else if (mapType == ScaleMap::mapType()) {
doProcessTypedMap<ScaleMap, OpType>(transform, op);
} else if (mapType == ScaleTranslateMap::mapType()) {
doProcessTypedMap<ScaleTranslateMap, OpType>(transform, op);
} else if (mapType == UnitaryMap::mapType()) {
doProcessTypedMap<UnitaryMap, OpType>(transform, op);
} else if (mapType == AffineMap::mapType()) {
doProcessTypedMap<AffineMap, OpType>(transform, op);
} else if (mapType == TranslationMap::mapType()) {
doProcessTypedMap<TranslationMap, OpType>(transform, op);
} else if (mapType == NonlinearFrustumMap::mapType()) {
doProcessTypedMap<NonlinearFrustumMap, OpType>(transform, op);
} else {
return false;
}
return true;
}
} // namespace math
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
Maps.h
OPENVDB_API
#define OPENVDB_API
Definition Platform.h:251
openvdb::v10_0::math::AffineMap::mapType
static Name mapType()
Definition Maps.h:371
openvdb::v10_0::math::CoordBBox
Axis-aligned bounding box of signed integer coordinates.
Definition Coord.h:249
openvdb::v10_0::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:25
openvdb::v10_0::math::Coord::asVec3d
Vec3d asVec3d() const
Definition Coord.h:143
openvdb::v10_0::math::Coord::round
static Coord round(const Vec3< T > &xyz)
Return xyz rounded to the closest integer coordinates (cell centered conversion).
Definition Coord.h:50
openvdb::v10_0::math::Coord::floor
static Coord floor(const Vec3< T > &xyz)
Return the largest integer coordinates that are not greater than xyz (node centered conversion).
Definition Coord.h:56
openvdb::v10_0::math::MapBase
Abstract base class for maps.
Definition Maps.h:135
openvdb::v10_0::math::MapBase::Ptr
SharedPtr< MapBase > Ptr
Definition Maps.h:137
openvdb::v10_0::math::MapBase::ConstPtr
SharedPtr< const MapBase > ConstPtr
Definition Maps.h:138
openvdb::v10_0::math::NonlinearFrustumMap::mapType
static Name mapType()
Return NonlinearFrustumMap.
Definition Maps.h:2045
openvdb::v10_0::math::ScaleMap
A specialized Affine transform that scales along the principal axis the scaling need not be uniform i...
Definition Maps.h:659
openvdb::v10_0::math::ScaleMap::mapType
static Name mapType()
Definition Maps.h:713
openvdb::v10_0::math::ScaleTranslateMap::mapType
static Name mapType()
Definition Maps.h:1243
openvdb::v10_0::math::Transform
Definition Transform.h:40
openvdb::v10_0::math::Transform::worldToIndex
BBoxd worldToIndex(const BBoxd &) const
Apply the inverse of this transformation to the given world-space bounding box.
openvdb::v10_0::math::Transform::operator!=
bool operator!=(const Transform &other) const
Definition Transform.h:154
openvdb::v10_0::math::Transform::worldToIndexCellCentered
Coord worldToIndexCellCentered(const Vec3d &xyz) const
Definition Transform.h:111
openvdb::v10_0::math::Transform::mapType
Name mapType() const
Return the transformation map's type-name.
Definition Transform.h:66
openvdb::v10_0::math::Transform::isLinear
bool isLinear() const
Return true if the transformation map is exclusively linear/affine.
Definition Transform.h:61
openvdb::v10_0::math::Transform::preTranslate
void preTranslate(const Vec3d &)
openvdb::v10_0::math::Transform::indexToWorld
BBoxd indexToWorld(const CoordBBox &) const
Apply this transformation to the given index-space bounding box.
openvdb::v10_0::math::Transform::voxelVolume
double voxelVolume() const
Return the voxel volume of the linear component of the map.
Definition Transform.h:100
openvdb::v10_0::math::Transform::postMult
void postMult(const Mat3d &)
openvdb::v10_0::math::Transform::print
void print(std::ostream &os=std::cout, const std::string &indent="") const
Print a description of this transform.
openvdb::v10_0::math::Transform::indexToWorld
Vec3d indexToWorld(const Coord &ijk) const
Definition Transform.h:109
openvdb::v10_0::math::Transform::voxelSize
Vec3d voxelSize() const
Return the size of a voxel using the linear component of the map.
Definition Transform.h:93
openvdb::v10_0::math::Transform::Transform
Transform(const Transform &)
openvdb::v10_0::math::Transform::postRotate
void postRotate(double radians, const Axis axis=X_AXIS)
openvdb::v10_0::math::Transform::indexToWorld
Vec3d indexToWorld(const Vec3d &xyz) const
Apply this transformation to the given coordinates.
Definition Transform.h:108
openvdb::v10_0::math::Transform::worldToIndexNodeCentered
CoordBBox worldToIndexNodeCentered(const BBoxd &) const
openvdb::v10_0::math::Transform::copy
Ptr copy() const
Definition Transform.h:50
openvdb::v10_0::math::Transform::worldToIndex
Vec3d worldToIndex(const Vec3d &xyz) const
Definition Transform.h:110
openvdb::v10_0::math::Transform::write
void write(std::ostream &) const
Serialize this transform to the given stream.
openvdb::v10_0::math::Transform::preScale
void preScale(const Vec3d &)
openvdb::v10_0::math::Transform::postMult
void postMult(const Mat4d &)
openvdb::v10_0::math::Transform::baseMap
MapBase::Ptr baseMap()
Definition Transform.h:132
openvdb::v10_0::math::Transform::ConstPtr
SharedPtr< const Transform > ConstPtr
Definition Transform.h:43
openvdb::v10_0::math::Transform::postScale
void postScale(double)
openvdb::v10_0::math::Transform::preRotate
void preRotate(double radians, const Axis axis=X_AXIS)
Update the linear (affine) map by prepending or postfixing the appropriate operation....
openvdb::v10_0::math::Transform::Transform
Transform(const MapBase::Ptr &)
openvdb::v10_0::math::Transform::voxelSize
Vec3d voxelSize(const Vec3d &xyz) const
Return the size of a voxel at position (x, y, z).
Definition Transform.h:97
openvdb::v10_0::math::Transform::read
void read(std::istream &)
Unserialize this transform from the given stream.
openvdb::v10_0::math::Transform::~Transform
~Transform()
Definition Transform.h:48
openvdb::v10_0::math::Transform::constMap
MapType::ConstPtr constMap() const
Definition Transform.h:189
openvdb::v10_0::math::Transform::Transform
Transform()
Definition Transform.h:45
openvdb::v10_0::math::Transform::indexToWorld
BBoxd indexToWorld(const BBoxd &) const
openvdb::v10_0::math::Transform::postTranslate
void postTranslate(const Vec3d &)
openvdb::v10_0::math::Transform::hasUniformScale
bool hasUniformScale() const
Return true if the voxels in world space are uniformly sized cubes.
Definition Transform.h:104
openvdb::v10_0::math::Transform::createLinearTransform
static Transform::Ptr createLinearTransform(const Mat4R &)
openvdb::v10_0::math::Transform::operator==
bool operator==(const Transform &other) const
openvdb::v10_0::math::Transform::isIdentity
bool isIdentity() const
Return true if the transform is equivalent to an idenity.
openvdb::v10_0::math::Transform::worldToIndexCellCentered
CoordBBox worldToIndexCellCentered(const BBoxd &) const
openvdb::v10_0::math::Transform::worldToIndexNodeCentered
Coord worldToIndexNodeCentered(const Vec3d &xyz) const
Definition Transform.h:112
openvdb::v10_0::math::Transform::map
MapType::Ptr map()
Return the result of downcasting the base map pointer to a MapType pointer, or return a null pointer ...
Definition Transform.h:169
openvdb::v10_0::math::Transform::postScale
void postScale(const Vec3d &)
openvdb::v10_0::math::Transform::Ptr
SharedPtr< Transform > Ptr
Definition Transform.h:42
openvdb::v10_0::math::Transform::preMult
void preMult(const Mat4d &)
openvdb::v10_0::math::Transform::preScale
void preScale(double)
openvdb::v10_0::math::Transform::preShear
void preShear(double shear, Axis axis0, Axis axis1)
openvdb::v10_0::math::Transform::createLinearTransform
static Transform::Ptr createLinearTransform(double voxelSize=1.0)
Create and return a shared pointer to a new transform.
openvdb::v10_0::math::Transform::baseMap
MapBase::ConstPtr baseMap() const
Return a base pointer to the transformation map.
Definition Transform.h:131
openvdb::v10_0::math::Transform::voxelVolume
double voxelVolume(const Vec3d &xyz) const
Return the voxel volume at position (x, y, z).
Definition Transform.h:102
openvdb::v10_0::math::Transform::postShear
void postShear(double shear, Axis axis0, Axis axis1)
openvdb::v10_0::math::Transform::createFrustumTransform
static Transform::Ptr createFrustumTransform(const BBoxd &, double taper, double depth, double voxelSize=1.0)
openvdb::v10_0::math::Transform::preMult
void preMult(const Mat3d &)
openvdb::v10_0::math::TranslationMap::mapType
static Name mapType()
Definition Maps.h:1012
openvdb::v10_0::math::UniformScaleMap::mapType
static Name mapType()
Definition Maps.h:939
openvdb::v10_0::math::UniformScaleTranslateMap::mapType
static Name mapType()
Definition Maps.h:1520
openvdb::v10_0::math::UnitaryMap::mapType
static Name mapType()
Return UnitaryMap.
Definition Maps.h:1720
openvdb::v10_0::math::Mat3d
Mat3< double > Mat3d
Definition Mat3.h:834
openvdb::v10_0::math::operator<<
std::ostream & operator<<(std::ostream &os, const BBox< Vec3T > &b)
Definition BBox.h:413
openvdb::v10_0::math::calculateBounds
OPENVDB_API void calculateBounds(const Transform &t, const Vec3d &minWS, const Vec3d &maxWS, Vec3d &minIS, Vec3d &maxIS)
Calculate an axis-aligned bounding box in index space from an axis-aligned bounding box in world spac...
openvdb::v10_0::math::processTypedMap
bool processTypedMap(TransformType &transform, OpType &op)
Utility function that, given a generic map pointer, calls a functor on the fully-resoved map.
Definition Transform.h:233
openvdb::v10_0::math::doProcessTypedMap
void doProcessTypedMap(Transform &transform, OpType &op)
Helper function used internally by processTypedMap().
Definition Transform.h:201
openvdb::v10_0::math::Vec3d
Vec3< double > Vec3d
Definition Vec3.h:664
openvdb::v10_0::math::Mat4d
Mat4< double > Mat4d
Definition Mat4.h:1354
openvdb::v10_0::math::shear
MatType shear(Axis axis0, Axis axis1, typename MatType::value_type shear)
Set the matrix to a shear along axis0 by a fraction of axis1.
Definition Mat.h:688
openvdb::v10_0::math::Axis
Axis
Definition Math.h:901
openvdb::v10_0::math::X_AXIS
@ X_AXIS
Definition Math.h:902
openvdb::v10_0::Name
std::string Name
Definition Name.h:17
openvdb::v10_0::Mat4R
math::Mat4< Real > Mat4R
Definition Types.h:101
openvdb::v10_0::ConstPtrCast
SharedPtr< T > ConstPtrCast(const SharedPtr< U > &ptr)
Return a new shared pointer that points to the same object as the given pointer but with possibly dif...
Definition Types.h:126
openvdb::v10_0::BBoxd
math::BBox< Vec3d > BBoxd
Definition Types.h:84
openvdb::v10_0::SharedPtr
std::shared_ptr< T > SharedPtr
Definition Types.h:114
openvdb::v10_0::StaticPtrCast
SharedPtr< T > StaticPtrCast(const SharedPtr< U > &ptr)
Return a new shared pointer that points to the same object as the given pointer after a static_cast.
Definition Types.h:146
openvdb
Definition Exceptions.h:13
Types.h
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