OpenVDB  6.2.0
Transform.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 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 
31 #ifndef OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
32 #define OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
33 
34 #include "Maps.h"
35 #include <openvdb/Types.h>
36 #include <iosfwd>
37 
38 namespace openvdb {
40 namespace OPENVDB_VERSION_NAME {
41 namespace math {
42 
43 // Forward declaration
44 class Transform;
45 
46 
47 // Utility methods
48 
52 OPENVDB_API void
53 calculateBounds(const Transform& t, const Vec3d& minWS, const Vec3d& maxWS,
54  Vec3d& minIS, Vec3d& maxIS);
55 
58 //void calculateBounds(const Transform& t, const Vec3d& center, const Real radius,
59 // Vec3d& minIS, Vec3d& maxIS);
60 
61 
63 
64 
67 {
68 public:
71 
72  Transform(): mMap(MapBase::Ptr(new ScaleMap())) {}
73  Transform(const MapBase::Ptr&);
74  Transform(const Transform&);
76 
77  Ptr copy() const { return Ptr(new Transform(mMap->copy())); }
78 
80  static Transform::Ptr createLinearTransform(double voxelSize = 1.0);
82  static Transform::Ptr createLinearTransform(const Mat4R&);
83  static Transform::Ptr createFrustumTransform(const BBoxd&, double taper,
84  double depth, double voxelSize = 1.0);
86 
88  bool isLinear() const { return mMap->isLinear(); }
89 
91  bool isIdentity() const ;
93  Name mapType() const { return mMap->type(); }
94 
95 
97  void preRotate(double radians, const Axis axis = X_AXIS);
103  void preTranslate(const Vec3d&);
104  void preScale(const Vec3d&);
105  void preScale(double);
106  void preShear(double shear, Axis axis0, Axis axis1);
107  void preMult(const Mat4d&);
108  void preMult(const Mat3d&);
109 
110  void postRotate(double radians, const Axis axis = X_AXIS);
111  void postTranslate(const Vec3d&);
112  void postScale(const Vec3d&);
113  void postScale(double);
114  void postShear(double shear, Axis axis0, Axis axis1);
115  void postMult(const Mat4d&);
116  void postMult(const Mat3d&);
118 
120  Vec3d voxelSize() const { return mMap->voxelSize(); }
124  Vec3d voxelSize(const Vec3d& xyz) const { return mMap->voxelSize(xyz); }
125 
127  double voxelVolume() const { return mMap->determinant(); }
129  double voxelVolume(const Vec3d& xyz) const { return mMap->determinant(xyz); }
131  bool hasUniformScale() const { return mMap->hasUniformScale(); }
132 
134  Vec3d indexToWorld(const Vec3d& xyz) const { return mMap->applyMap(xyz); }
136  Vec3d indexToWorld(const Coord& ijk) const { return mMap->applyMap(ijk.asVec3d()); }
137  Vec3d worldToIndex(const Vec3d& xyz) const { return mMap->applyInverseMap(xyz); }
138  Coord worldToIndexCellCentered(const Vec3d& xyz) const {return Coord::round(worldToIndex(xyz));}
139  Coord worldToIndexNodeCentered(const Vec3d& xyz) const {return Coord::floor(worldToIndex(xyz));}
141 
143  BBoxd indexToWorld(const CoordBBox&) const;
146  BBoxd indexToWorld(const BBoxd&) const;
148 
149  BBoxd worldToIndex(const BBoxd&) const;
152  CoordBBox worldToIndexCellCentered(const BBoxd&) const;
153  CoordBBox worldToIndexNodeCentered(const BBoxd&) const;
155 
157  MapBase::ConstPtr baseMap() const { return mMap; }
159  MapBase::Ptr baseMap() { return mMap; }
161 
163  template<typename MapType> typename MapType::Ptr map();
166  template<typename MapType> typename MapType::ConstPtr map() const;
167  template<typename MapType> typename MapType::ConstPtr constMap() const;
169 
171  void read(std::istream&);
173  void write(std::ostream&) const;
174 
178  void print(std::ostream& os = std::cout, const std::string& indent = "") const;
179 
180  bool operator==(const Transform& other) const;
181  inline bool operator!=(const Transform& other) const { return !(*this == other); }
182 
183 private:
184  MapBase::Ptr mMap;
185 }; // class Transform
186 
187 
188 OPENVDB_API std::ostream& operator<<(std::ostream&, const Transform&);
189 
190 
192 
193 
194 template<typename MapType>
195 inline typename MapType::Ptr
196 Transform::map()
197 {
198  if (mMap->type() == MapType::mapType()) {
199  return StaticPtrCast<MapType>(mMap);
200  }
201  return typename MapType::Ptr();
202 }
203 
204 
205 template<typename MapType>
206 inline typename MapType::ConstPtr
207 Transform::map() const
208 {
209  return ConstPtrCast<const MapType>(
210  const_cast<Transform*>(this)->map<MapType>());
211 }
212 
213 
214 template<typename MapType>
215 inline typename MapType::ConstPtr
216 Transform::constMap() const
217 {
218  return map<MapType>();
219 }
220 
221 
223 
224 
226 template<typename ResolvedMapType, typename OpType>
227 inline void
228 doProcessTypedMap(Transform& transform, OpType& op)
229 {
230  ResolvedMapType& resolvedMap = *transform.map<ResolvedMapType>();
231 #ifdef _MSC_VER
232  op.operator()<ResolvedMapType>(resolvedMap);
233 #else
234  op.template operator()<ResolvedMapType>(resolvedMap);
235 #endif
236 }
237 
239 template<typename ResolvedMapType, typename OpType>
240 inline void
241 doProcessTypedMap(const Transform& transform, OpType& op)
242 {
243  const ResolvedMapType& resolvedMap = *transform.map<ResolvedMapType>();
244 #ifdef _MSC_VER
245  op.operator()<ResolvedMapType>(resolvedMap);
246 #else
247  op.template operator()<ResolvedMapType>(resolvedMap);
248 #endif
249 }
250 
251 
266 template<typename TransformType, typename OpType>
267 bool
268 processTypedMap(TransformType& transform, OpType& op)
269 {
270  using namespace openvdb;
271 
272  const Name mapType = transform.mapType();
273  if (mapType == UniformScaleMap::mapType()) {
274  doProcessTypedMap<UniformScaleMap, OpType>(transform, op);
275 
276  } else if (mapType == UniformScaleTranslateMap::mapType()) {
277  doProcessTypedMap<UniformScaleTranslateMap, OpType>(transform, op);
278 
279  } else if (mapType == ScaleMap::mapType()) {
280  doProcessTypedMap<ScaleMap, OpType>(transform, op);
281 
282  } else if (mapType == ScaleTranslateMap::mapType()) {
283  doProcessTypedMap<ScaleTranslateMap, OpType>(transform, op);
284 
285  } else if (mapType == UnitaryMap::mapType()) {
286  doProcessTypedMap<UnitaryMap, OpType>(transform, op);
287 
288  } else if (mapType == AffineMap::mapType()) {
289  doProcessTypedMap<AffineMap, OpType>(transform, op);
290 
291  } else if (mapType == TranslationMap::mapType()) {
292  doProcessTypedMap<TranslationMap, OpType>(transform, op);
293 
294  } else if (mapType == NonlinearFrustumMap::mapType()) {
295  doProcessTypedMap<NonlinearFrustumMap, OpType>(transform, op);
296  } else {
297  return false;
298  }
299  return true;
300 }
301 
302 } // namespace math
303 } // namespace OPENVDB_VERSION_NAME
304 } // namespace openvdb
305 
306 #endif // OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
307 
308 // Copyright (c) DreamWorks Animation LLC
309 // All rights reserved. This software is distributed under the
310 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
SharedPtr< const MapBase > ConstPtr
Definition: Maps.h:165
SharedPtr< Transform > Ptr
Definition: Transform.h:69
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:288
bool isLinear() const
Return true if the transformation map is exclusively linear/affine.
Definition: Transform.h:88
Ptr copy() const
Definition: Transform.h:77
bool hasUniformScale() const
Return true if the voxels in world space are uniformly sized cubes.
Definition: Transform.h:131
Vec3d worldToIndex(const Vec3d &xyz) const
Apply this transformation to the given coordinates.
Definition: Transform.h:137
SharedPtr< MapBase > Ptr
Definition: Maps.h:164
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...
Vec3d indexToWorld(const Coord &ijk) const
Apply this transformation to the given coordinates.
Definition: Transform.h:136
SharedPtr< const Transform > ConstPtr
Definition: Transform.h:70
Vec3d voxelSize() const
Return the size of a voxel using the linear component of the map.
Definition: Transform.h:120
double voxelVolume(const Vec3d &xyz) const
Return the voxel volume at position (x, y, z).
Definition: Transform.h:129
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:52
Transform()
Definition: Transform.h:72
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
void doProcessTypedMap(const Transform &transform, OpType &op)
Helper function used internally by processTypedMap()
Definition: Transform.h:241
bool operator!=(const Transform &other) const
Definition: Transform.h:181
bool isIdentity(const MatType &m)
Determine if a matrix is an identity matrix.
Definition: Mat.h:892
MapType::Ptr map()
Return the result of downcasting the base map pointer to a MapType pointer, or return a null pointer ...
Definition: Transform.h:196
Vec3< double > Vec3d
Definition: Vec3.h:689
MapBase::Ptr baseMap()
Return a base pointer to the transformation map.
Definition: Transform.h:159
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:128
std::string Name
Definition: Name.h:44
Vec3d voxelSize(const Vec3d &xyz) const
Return the size of a voxel at position (x, y, z).
Definition: Transform.h:124
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:275
Abstract base class for maps.
Definition: Maps.h:161
OPENVDB_API std::ostream & operator<<(std::ostream &, const Transform &)
Coord worldToIndexCellCentered(const Vec3d &xyz) const
Apply this transformation to the given coordinates.
Definition: Transform.h:138
Definition: Exceptions.h:40
Axis
Definition: Math.h:876
Name mapType() const
Return the transformation map&#39;s type-name.
Definition: Transform.h:93
Coord worldToIndexNodeCentered(const Vec3d &xyz) const
Apply this transformation to the given coordinates.
Definition: Transform.h:139
Vec3d asVec3d() const
Definition: Coord.h:171
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:268
~Transform()
Definition: Transform.h:75
A specialized Affine transform that scales along the principal axis the scaling need not be uniform i...
Definition: Maps.h:685
Definition: Math.h:877
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:498
Definition: Transform.h:66
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:180
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:720
double voxelVolume() const
Return the voxel volume of the linear component of the map.
Definition: Transform.h:127