OpenVDB  0.104.0
Transform.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012 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 
51 OPENVDB_API void
52 calculateBounds(const Transform& t, const Vec3d& minWS, const Vec3d& maxWS,
53  Vec3d& minIS, Vec3d& maxIS);
54 
59 
60 
62 
63 
66 {
67 public:
68  typedef boost::shared_ptr<Transform> Ptr;
69  typedef boost::shared_ptr<const Transform> ConstPtr;
70 
72  Transform(const MapBase::Ptr&);
73  Transform(const Transform&);
75 
76  Ptr copy() const { return Ptr(new Transform(mMap->copy())); }
77 
79 
80  static Transform::Ptr createLinearTransform(double voxelSize = 1.0);
81  static Transform::Ptr createLinearTransform(const Mat4R&);
82  OPENVDB_DEPRECATED static Transform::Ptr createFrustumTransform(const CoordBBox&, double taper,
83  double depth, double voxelSize = 1.0);
84  static Transform::Ptr createFrustumTransform(const BBoxd&, double taper,
85  double depth, double voxelSize = 1.0);
87 
89  bool isLinear() const { return mMap->isLinear(); }
90 
92  Name mapType() const { return mMap->type(); }
93 
94 
96  OPENVDB_DEPRECATED void rotate(double radians, const Axis axis = X_AXIS);
98  OPENVDB_DEPRECATED void translate(const Vec3d&);
100  OPENVDB_DEPRECATED void scale(const Vec3d&);
102  OPENVDB_DEPRECATED void scale(double);
104  OPENVDB_DEPRECATED void shear(double shear, Axis axis0, Axis axis1);
105 
107 
108 
109  void preRotate(double radians, const Axis axis = X_AXIS);
110  void preTranslate(const Vec3d&);
111  void preScale(const Vec3d&);
112  void preScale(double);
113  void preShear(double shear, Axis axis0, Axis axis1);
114 
115  void postRotate(double radians, const Axis axis = X_AXIS);
116  void postTranslate(const Vec3d&);
117  void postScale(const Vec3d&);
118  void postScale(double);
119  void postShear(double shear, Axis axis0, Axis axis1);
121 
123  Vec3d voxelSize() const { return mMap->voxelSize(); }
127  Vec3d voxelSize(const Vec3d& xyz) const { return mMap->voxelSize(xyz); }
130  OPENVDB_DEPRECATED Vec3d voxelDimensions() const { return mMap->voxelDimensions(); }
135  OPENVDB_DEPRECATED Vec3d voxelDimensions(const Vec3d& xyz) const { return mMap->voxelDimensions(xyz); }
136 
138  double voxelVolume() const { return mMap->determinant(); }
140  double voxelVolume(const Vec3d& xyz) const { return mMap->determinant(xyz); }
142  bool hasUniformScale() const { return mMap->hasUniformScale(); }
143 
145 
146  Vec3d indexToWorld(const Vec3d& xyz) const { return mMap->applyMap(xyz); }
147  Vec3d indexToWorld(const Coord& ijk) const { return mMap->applyMap(ijk.asVec3d()); }
148  Vec3d worldToIndex(const Vec3d& xyz) const { return mMap->applyInverseMap(xyz); }
149  Coord worldToIndexCellCentered(const Vec3d& xyz) const {return Coord::round(worldToIndex(xyz));}
150  Coord worldToIndexNodeCentered(const Vec3d& xyz) const {return Coord::floor(worldToIndex(xyz));}
152 
154 
155  MapBase::ConstPtr baseMap() const { return mMap; }
156  MapBase::Ptr baseMap() { return mMap; }
158 
160 
161 
162  template<typename MapType> typename MapType::Ptr map();
163  template<typename MapType> typename MapType::ConstPtr map() const;
164  template<typename MapType> typename MapType::ConstPtr constMap() const;
166 
168  void read(std::istream&);
170  void write(std::ostream&) const;
171 
175  void print(std::ostream& os = std::cout, const std::string& indent = "") const;
176 
177  bool operator==(const Transform& other) const;
178  inline bool operator!=(const Transform& other) const { return !(*this == other); }
179 
180 private:
182 }; // class Transform
183 
184 
185 OPENVDB_API std::ostream& operator<<(std::ostream&, const Transform&);
186 
187 
189 
190 
191 template<typename MapType>
192 inline typename MapType::Ptr
193 Transform::map()
194 {
195  if (mMap->type() == MapType::mapType()) {
196  return boost::static_pointer_cast<MapType>(mMap);
197  }
198  return typename MapType::Ptr();
199 }
200 
201 
202 template<typename MapType>
203 inline typename MapType::ConstPtr
204 Transform::map() const
205 {
206  return boost::const_pointer_cast<const MapType>(
207  const_cast<Transform*>(this)->map<MapType>());
208 }
209 
210 
211 template<typename MapType>
212 inline typename MapType::ConstPtr
213 Transform::constMap() const
214 {
215  return map<MapType>();
216 }
217 
218 
220 
221 
223 template<typename ResolvedMapType, typename OpType>
224 inline void
225 doProcessTypedMap(Transform& transform, OpType& op)
226 {
227  ResolvedMapType& resolvedMap = *transform.map<ResolvedMapType>();
228 #ifdef _MSC_VER
229  op.operator()<ResolvedMapType>(resolvedMap);
230 #else
231  op.template operator()<ResolvedMapType>(resolvedMap);
232 #endif
233 }
234 
236 template<typename ResolvedMapType, typename OpType>
237 inline void
238 doProcessTypedMap(const Transform& transform, OpType& op)
239 {
240  const ResolvedMapType& resolvedMap = *transform.map<ResolvedMapType>();
241 #ifdef _MSC_VER
242  op.operator()<ResolvedMapType>(resolvedMap);
243 #else
244  op.template operator()<ResolvedMapType>(resolvedMap);
245 #endif
246 }
247 
248 
263 template<typename TransformType, typename OpType>
264 bool
265 processTypedMap(TransformType& transform, OpType& op)
266 {
267  using namespace openvdb;
268 
269  const Name mapType = transform.mapType();
270  if (mapType == UniformScaleMap::mapType()) {
271  doProcessTypedMap<UniformScaleMap, OpType>(transform, op);
272 
273  } else if (mapType == UniformScaleTranslateMap::mapType()) {
274  doProcessTypedMap<UniformScaleTranslateMap, OpType>(transform, op);
275 
276  } else if (mapType == ScaleMap::mapType()) {
277  doProcessTypedMap<ScaleMap, OpType>(transform, op);
278 
279  } else if (mapType == ScaleTranslateMap::mapType()) {
280  doProcessTypedMap<ScaleTranslateMap, OpType>(transform, op);
281 
282  } else if (mapType == UnitaryMap::mapType()) {
283  doProcessTypedMap<UnitaryMap, OpType>(transform, op);
284 
285  } else if (mapType == AffineMap::mapType()) {
286  doProcessTypedMap<AffineMap, OpType>(transform, op);
287 
288  } else if (mapType == TranslationMap::mapType()) {
289  doProcessTypedMap<TranslationMap, OpType>(transform, op);
290 
291  } else if (mapType == NonlinearFrustumMap::mapType()) {
292  doProcessTypedMap<NonlinearFrustumMap, OpType>(transform, op);
293  } else {
294  return false;
295  }
296  return true;
297 }
298 
299 
300 } // namespace math
301 } // namespace OPENVDB_VERSION_NAME
302 } // namespace openvdb
303 
304 #endif // OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
305 
306 // Copyright (c) 2012 DreamWorks Animation LLC
307 // All rights reserved. This software is distributed under the
308 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )