OpenVDB  1.2.0
Transform.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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  static Transform::Ptr createLinearTransform(double voxelSize = 1.0);
81  static Transform::Ptr createLinearTransform(const Mat4R&);
82  static Transform::Ptr createFrustumTransform(const BBoxd&, double taper,
83  double depth, double voxelSize = 1.0);
85 
87  bool isLinear() const { return mMap->isLinear(); }
88 
90  bool isIdentity() const ;
92  Name mapType() const { return mMap->type(); }
93 
94 
96  void preRotate(double radians, const Axis axis = X_AXIS);
102  void preTranslate(const Vec3d&);
103  void preScale(const Vec3d&);
104  void preScale(double);
105  void preShear(double shear, Axis axis0, Axis axis1);
106  void preMult(const Mat4d&);
107  void preMult(const Mat3d&);
108 
109  void postRotate(double radians, const Axis axis = X_AXIS);
110  void postTranslate(const Vec3d&);
111  void postScale(const Vec3d&);
112  void postScale(double);
113  void postShear(double shear, Axis axis0, Axis axis1);
114  void postMult(const Mat4d&);
115  void postMult(const Mat3d&);
117 
119  Vec3d voxelSize() const { return mMap->voxelSize(); }
123  Vec3d voxelSize(const Vec3d& xyz) const { return mMap->voxelSize(xyz); }
124 
126  double voxelVolume() const { return mMap->determinant(); }
128  double voxelVolume(const Vec3d& xyz) const { return mMap->determinant(xyz); }
130  bool hasUniformScale() const { return mMap->hasUniformScale(); }
131 
133  Vec3d indexToWorld(const Vec3d& xyz) const { return mMap->applyMap(xyz); }
135  Vec3d indexToWorld(const Coord& ijk) const { return mMap->applyMap(ijk.asVec3d()); }
136  Vec3d worldToIndex(const Vec3d& xyz) const { return mMap->applyInverseMap(xyz); }
137  Coord worldToIndexCellCentered(const Vec3d& xyz) const {return Coord::round(worldToIndex(xyz));}
138  Coord worldToIndexNodeCentered(const Vec3d& xyz) const {return Coord::floor(worldToIndex(xyz));}
140 
142  MapBase::ConstPtr baseMap() const { return mMap; }
144  MapBase::Ptr baseMap() { return mMap; }
146 
148  template<typename MapType> typename MapType::Ptr map();
151  template<typename MapType> typename MapType::ConstPtr map() const;
152  template<typename MapType> typename MapType::ConstPtr constMap() const;
154 
156  void read(std::istream&);
158  void write(std::ostream&) const;
159 
163  void print(std::ostream& os = std::cout, const std::string& indent = "") const;
164 
165  bool operator==(const Transform& other) const;
166  inline bool operator!=(const Transform& other) const { return !(*this == other); }
167 
168 private:
170 }; // class Transform
171 
172 
173 OPENVDB_API std::ostream& operator<<(std::ostream&, const Transform&);
174 
175 
177 
178 
179 template<typename MapType>
180 inline typename MapType::Ptr
181 Transform::map()
182 {
183  if (mMap->type() == MapType::mapType()) {
184  return boost::static_pointer_cast<MapType>(mMap);
185  }
186  return typename MapType::Ptr();
187 }
188 
189 
190 template<typename MapType>
191 inline typename MapType::ConstPtr
192 Transform::map() const
193 {
194  return boost::const_pointer_cast<const MapType>(
195  const_cast<Transform*>(this)->map<MapType>());
196 }
197 
198 
199 template<typename MapType>
200 inline typename MapType::ConstPtr
201 Transform::constMap() const
202 {
203  return map<MapType>();
204 }
205 
206 
208 
209 
211 template<typename ResolvedMapType, typename OpType>
212 inline void
213 doProcessTypedMap(Transform& transform, OpType& op)
214 {
215  ResolvedMapType& resolvedMap = *transform.map<ResolvedMapType>();
216 #ifdef _MSC_VER
217  op.operator()<ResolvedMapType>(resolvedMap);
218 #else
219  op.template operator()<ResolvedMapType>(resolvedMap);
220 #endif
221 }
222 
224 template<typename ResolvedMapType, typename OpType>
225 inline void
226 doProcessTypedMap(const Transform& transform, OpType& op)
227 {
228  const ResolvedMapType& resolvedMap = *transform.map<ResolvedMapType>();
229 #ifdef _MSC_VER
230  op.operator()<ResolvedMapType>(resolvedMap);
231 #else
232  op.template operator()<ResolvedMapType>(resolvedMap);
233 #endif
234 }
235 
236 
251 template<typename TransformType, typename OpType>
252 bool
253 processTypedMap(TransformType& transform, OpType& op)
254 {
255  using namespace openvdb;
256 
257  const Name mapType = transform.mapType();
258  if (mapType == UniformScaleMap::mapType()) {
259  doProcessTypedMap<UniformScaleMap, OpType>(transform, op);
260 
261  } else if (mapType == UniformScaleTranslateMap::mapType()) {
262  doProcessTypedMap<UniformScaleTranslateMap, OpType>(transform, op);
263 
264  } else if (mapType == ScaleMap::mapType()) {
265  doProcessTypedMap<ScaleMap, OpType>(transform, op);
266 
267  } else if (mapType == ScaleTranslateMap::mapType()) {
268  doProcessTypedMap<ScaleTranslateMap, OpType>(transform, op);
269 
270  } else if (mapType == UnitaryMap::mapType()) {
271  doProcessTypedMap<UnitaryMap, OpType>(transform, op);
272 
273  } else if (mapType == AffineMap::mapType()) {
274  doProcessTypedMap<AffineMap, OpType>(transform, op);
275 
276  } else if (mapType == TranslationMap::mapType()) {
277  doProcessTypedMap<TranslationMap, OpType>(transform, op);
278 
279  } else if (mapType == NonlinearFrustumMap::mapType()) {
280  doProcessTypedMap<NonlinearFrustumMap, OpType>(transform, op);
281  } else {
282  return false;
283  }
284  return true;
285 }
286 
287 } // namespace math
288 } // namespace OPENVDB_VERSION_NAME
289 } // namespace openvdb
290 
291 #endif // OPENVDB_MATH_TRANSFORM_HAS_BEEN_INCLUDED
292 
293 // Copyright (c) 2012-2013 DreamWorks Animation LLC
294 // All rights reserved. This software is distributed under the
295 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )