OpenVDB 7.0.0

Transform.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #ifndef OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
5 #define OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
6
7 #include "Maps.h"
8 #include <openvdb/Types.h>
9 #include <iosfwd>
10
11 namespace openvdb {
12 OPENVDB_USE_VERSION_NAMESPACE
13 namespace OPENVDB_VERSION_NAME {
14 namespace math {
15
16 // Forward declaration
17 class Transform;
18
19
20 // Utility methods
21
25 OPENVDB_API void
26 calculateBounds(const Transform& t, const Vec3d& minWS, const Vec3d& maxWS,
27 Vec3d& minIS, Vec3d& maxIS);
28
31 //void calculateBounds(const Transform& t, const Vec3d& center, const Real radius,
32 // Vec3d& minIS, Vec3d& maxIS);
33
34
36
37
39 class OPENVDB_API Transform
40 {
41 public:
42 using Ptr = SharedPtr<Transform>;
43 using ConstPtr = SharedPtr<const Transform>;
44
45 Transform(): mMap(MapBase::Ptr(new ScaleMap())) {}
46 Transform(const MapBase::Ptr&);
47 Transform(const Transform&);
48 ~Transform() {}
49
50 Ptr copy() const { return Ptr(new Transform(mMap->copy())); }
51
53 static Transform::Ptr createLinearTransform(double voxelSize = 1.0);
55 static Transform::Ptr createLinearTransform(const Mat4R&);
56 static Transform::Ptr createFrustumTransform(const BBoxd&, double taper,
57 double depth, double voxelSize = 1.0);
59
61 bool isLinear() const { return mMap->isLinear(); }
62
64 bool isIdentity() const ;
66 Name mapType() const { return mMap->type(); }
67
68
70 void preRotate(double radians, const Axis axis = X_AXIS);
76 void preTranslate(const Vec3d&);
77 void preScale(const Vec3d&);
78 void preScale(double);
79 void preShear(double shear, Axis axis0, Axis axis1);
80 void preMult(const Mat4d&);
81 void preMult(const Mat3d&);
82
83 void postRotate(double radians, const Axis axis = X_AXIS);
84 void postTranslate(const Vec3d&);
85 void postScale(const Vec3d&);
86 void postScale(double);
87 void postShear(double shear, Axis axis0, Axis axis1);
88 void postMult(const Mat4d&);
89 void postMult(const Mat3d&);
91
93 Vec3d voxelSize() const { return mMap->voxelSize(); }
97 Vec3d voxelSize(const Vec3d& xyz) const { return mMap->voxelSize(xyz); }
98
100 double voxelVolume() const { return mMap->determinant(); }
102 double voxelVolume(const Vec3d& xyz) const { return mMap->determinant(xyz); }
104 bool hasUniformScale() const { return mMap->hasUniformScale(); }
105
107 Vec3d indexToWorld(const Vec3d& xyz) const { return mMap->applyMap(xyz); }
109 Vec3d indexToWorld(const Coord& ijk) const { return mMap->applyMap(ijk.asVec3d()); }
110 Vec3d worldToIndex(const Vec3d& xyz) const { return mMap->applyInverseMap(xyz); }
111 Coord worldToIndexCellCentered(const Vec3d& xyz) const {return Coord::round(worldToIndex(xyz));}
112 Coord worldToIndexNodeCentered(const Vec3d& xyz) const {return Coord::floor(worldToIndex(xyz));}
114
116 BBoxd indexToWorld(const CoordBBox&) const;
119 BBoxd indexToWorld(const BBoxd&) const;
121
122 BBoxd worldToIndex(const BBoxd&) const;
125 CoordBBox worldToIndexCellCentered(const BBoxd&) const;
126 CoordBBox worldToIndexNodeCentered(const BBoxd&) const;
128
130 MapBase::ConstPtr baseMap() const { return mMap; }
132 MapBase::Ptr baseMap() { return mMap; }
134
136 template<typename MapType> typename MapType::Ptr map();
139 template<typename MapType> typename MapType::ConstPtr map() const;
140 template<typename MapType> typename MapType::ConstPtr constMap() const;
142
144 void read(std::istream&);
146 void write(std::ostream&) const;
147
151 void print(std::ostream& os = std::cout, const std::string& indent = "") const;
152
153 bool operator==(const Transform& other) const;
154 inline bool operator!=(const Transform& other) const { return !(*this == other); }
155
156 private:
157 MapBase::Ptr mMap;
158 }; // class Transform
159
160
161 OPENVDB_API std::ostream& operator<<(std::ostream&, const Transform&);
162
163
165
166
167 template<typename MapType>
168 inline typename MapType::Ptr
169 Transform::map()
170 {
171 if (mMap->type() == MapType::mapType()) {
172 return StaticPtrCast<MapType>(mMap);
173 }
174 return typename MapType::Ptr();
175 }
176
177
178 template<typename MapType>
179 inline typename MapType::ConstPtr
180 Transform::map() const
181 {
182 return ConstPtrCast<const MapType>(
183 const_cast<Transform*>(this)->map<MapType>());
184 }
185
186
187 template<typename MapType>
188 inline typename MapType::ConstPtr
189 Transform::constMap() const
190 {
191 return map<MapType>();
192 }
193
194
196
197
199 template<typename ResolvedMapType, typename OpType>
200 inline void
201 doProcessTypedMap(Transform& transform, OpType& op)
202 {
203 ResolvedMapType& resolvedMap = *transform.map<ResolvedMapType>();
204 #ifdef _MSC_VER
205 op.operator()<ResolvedMapType>(resolvedMap);
206 #else
207 op.template operator()<ResolvedMapType>(resolvedMap);
208 #endif
209 }
210
212 template<typename ResolvedMapType, typename OpType>
213 inline void
214 doProcessTypedMap(const Transform& transform, OpType& op)
215 {
216 const ResolvedMapType& resolvedMap = *transform.map<ResolvedMapType>();
217 #ifdef _MSC_VER
218 op.operator()<ResolvedMapType>(resolvedMap);
219 #else
220 op.template operator()<ResolvedMapType>(resolvedMap);
221 #endif
222 }
223
224
239 template<typename TransformType, typename OpType>
240 bool
241 processTypedMap(TransformType& transform, OpType& op)
242 {
243 using namespace openvdb;
244
245 const Name mapType = transform.mapType();
246 if (mapType == UniformScaleMap::mapType()) {
247 doProcessTypedMap<UniformScaleMap, OpType>(transform, op);
248
249 } else if (mapType == UniformScaleTranslateMap::mapType()) {
250 doProcessTypedMap<UniformScaleTranslateMap, OpType>(transform, op);
251
252 } else if (mapType == ScaleMap::mapType()) {
253 doProcessTypedMap<ScaleMap, OpType>(transform, op);
254
255 } else if (mapType == ScaleTranslateMap::mapType()) {
256 doProcessTypedMap<ScaleTranslateMap, OpType>(transform, op);
257
258 } else if (mapType == UnitaryMap::mapType()) {
259 doProcessTypedMap<UnitaryMap, OpType>(transform, op);
260
261 } else if (mapType == AffineMap::mapType()) {
262 doProcessTypedMap<AffineMap, OpType>(transform, op);
263
264 } else if (mapType == TranslationMap::mapType()) {
265 doProcessTypedMap<TranslationMap, OpType>(transform, op);
266
267 } else if (mapType == NonlinearFrustumMap::mapType()) {
268 doProcessTypedMap<NonlinearFrustumMap, OpType>(transform, op);
269 } else {
270 return false;
271 }
272 return true;
273 }
274
275 } // namespace math
276 } // namespace OPENVDB_VERSION_NAME
277 } // namespace openvdb
278
279 #endif // OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
openvdb::v7_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::v7_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:241
openvdb::v7_0::math::isIdentity
bool isIdentity(const MatType &m)
Determine if a matrix is an identity matrix.
Definition: Mat.h:865
openvdb::v7_0::math::Transform::voxelVolume
double voxelVolume() const
Return the voxel volume of the linear component of the map.
Definition: Transform.h:100
openvdb::v7_0::math::BBox< Vec3d >
openvdb::v7_0::math::Transform::operator!=
bool operator!=(const Transform &other) const
Definition: Transform.h:154
openvdb::v7_0::math::Transform::Ptr
SharedPtr< Transform > Ptr
Definition: Transform.h:42
openvdb::v7_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
Types.h
openvdb::v7_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:57
openvdb::v7_0::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:25
openvdb::v7_0::math::MapBase::Ptr
SharedPtr< MapBase > Ptr
Definition: Maps.h:137
openvdb::v7_0::math::MapBase
Abstract base class for maps.
Definition: Maps.h:134
openvdb::v7_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:51
openvdb::v7_0::math::X_AXIS
Definition: Math.h:850
openvdb::v7_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::v7_0::math::Transform::voxelVolume
double voxelVolume(const Vec3d &xyz) const
Return the voxel volume at position (x, y, z).
Definition: Transform.h:102
openvdb::v7_0::math::Axis
Axis
Definition: Math.h:849
openvdb::v7_0::SharedPtr
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
openvdb::v7_0::math::Transform::worldToIndexCellCentered
Coord worldToIndexCellCentered(const Vec3d &xyz) const
Definition: Transform.h:111
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
openvdb::v7_0::math::Transform::mapType
Name mapType() const
Return the transformation map's type-name.
Definition: Transform.h:66
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
openvdb::v7_0::math::Mat4< Real >
openvdb::v7_0::math::MapBase::ConstPtr
SharedPtr< const MapBase > ConstPtr
Definition: Maps.h:138
openvdb::v7_0::math::ScaleMap
A specialized Affine transform that scales along the principal axis the scaling need not be uniform i...
Definition: Maps.h:656
openvdb::v7_0::math::Transform::worldToIndex
Vec3d worldToIndex(const Vec3d &xyz) const
Definition: Transform.h:110
openvdb::v7_0::math::Coord::asVec3d
Vec3d asVec3d() const
Definition: Coord.h:144
openvdb::v7_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:693
openvdb::v7_0::math::Transform::~Transform
~Transform()
Definition: Transform.h:48
openvdb::v7_0::math::operator==
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:471
openvdb::v7_0::math::Transform::indexToWorld
Vec3d indexToWorld(const Coord &ijk) const
Definition: Transform.h:109
openvdb::v7_0::math::Vec3< double >
openvdb::v7_0::math::Transform::isLinear
bool isLinear() const
Return true if the transformation map is exclusively linear/affine.
Definition: Transform.h:61
openvdb::v7_0::math::Mat3< double >
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
Maps.h
openvdb::v7_0::math::Transform::ConstPtr
SharedPtr< const Transform > ConstPtr
Definition: Transform.h:43
openvdb::v7_0::math::operator<<
OPENVDB_API std::ostream & operator<<(std::ostream &, const Transform &)
openvdb::v7_0::math::Transform::hasUniformScale
bool hasUniformScale() const
Return true if the voxels in world space are uniformly sized cubes.
Definition: Transform.h:104
openvdb::v7_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::v7_0::Name
std::string Name
Definition: Name.h:17
openvdb::v7_0::math::Transform
Definition: Transform.h:39
openvdb::v7_0::math::Transform::worldToIndexNodeCentered
Coord worldToIndexNodeCentered(const Vec3d &xyz) const
Definition: Transform.h:112
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
openvdb::v7_0::math::Transform::copy
Ptr copy() const
Definition: Transform.h:50
openvdb
Definition: Exceptions.h:13
openvdb::v7_0::math::Transform::baseMap
MapBase::Ptr baseMap()
Definition: Transform.h:132
openvdb::v7_0::math::doProcessTypedMap
void doProcessTypedMap(const Transform &transform, OpType &op)
Helper function used internally by processTypedMap()
Definition: Transform.h:214
openvdb::v7_0::math::Transform::Transform
Transform()
Definition: Transform.h:45