HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VectorTransformer.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 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 //
29 ///////////////////////////////////////////////////////////////////////////
30 //
31 /// @file VectorTransformer.h
32 
33 #ifndef OPENVDB_TOOLS_VECTORTRANSFORMER_HAS_BEEN_INCLUDED
34 #define OPENVDB_TOOLS_VECTORTRANSFORMER_HAS_BEEN_INCLUDED
35 
36 #include <openvdb/Types.h>
37 #include <openvdb/math/Mat4.h>
38 #include <openvdb/math/Vec3.h>
39 #include "ValueTransformer.h" // for tools::foreach()
40 #include <hboost/utility/enable_if.hpp>
41 
42 namespace openvdb {
44 namespace OPENVDB_VERSION_NAME {
45 namespace tools {
46 
47 /// @brief Apply an affine transform to the voxel values of a vector-valued grid
48 /// in accordance with the grid's vector type (covariant, contravariant, etc.).
49 /// @throw TypeError if the grid is not vector-valued
50 template<typename GridType>
51 inline void
52 transformVectors(GridType&, const Mat4d&);
53 
54 
55 ////////////////////////////////////////
56 
57 
58 // Functors for use with tools::foreach() to transform vector voxel values
59 
61 {
62  const Mat4d mat;
63  HomogeneousMatMul(const Mat4d& _mat): mat(_mat) {}
64  template<typename TreeIterT> void operator()(const TreeIterT& it) const
65  {
66  Vec3d v(*it);
67  it.setValue(mat.transformH(v));
68  }
69 };
70 
71 struct MatMul
72 {
73  const Mat4d mat;
74  MatMul(const Mat4d& _mat): mat(_mat) {}
75  template<typename TreeIterT>
76  void operator()(const TreeIterT& it) const
77  {
78  Vec3d v(*it);
79  it.setValue(mat.transform3x3(v));
80  }
81 };
82 
84 {
85  const Mat4d mat;
86  MatMulNormalize(const Mat4d& _mat): mat(_mat) {}
87  template<typename TreeIterT>
88  void operator()(const TreeIterT& it) const
89  {
90  Vec3d v(*it);
91  v = mat.transform3x3(v);
92  v.normalize();
93  it.setValue(v);
94  }
95 };
96 
97 
98 /// @internal This overload is enabled only for scalar-valued grids.
99 template<typename GridType> inline
100 typename hboost::disable_if_c<VecTraits<typename GridType::ValueType>::IsVec, void>::type
101 doTransformVectors(GridType&, const Mat4d&)
102 {
103  OPENVDB_THROW(TypeError, "tools::transformVectors() requires a vector-valued grid");
104 }
105 
106 /// @internal This overload is enabled only for vector-valued grids.
107 template<typename GridType> inline
108 typename hboost::enable_if_c<VecTraits<typename GridType::ValueType>::IsVec, void>::type
109 doTransformVectors(GridType& grid, const Mat4d& mat)
110 {
111  if (!grid.isInWorldSpace()) return;
112 
113  const VecType vecType = grid.getVectorType();
114  switch (vecType) {
115  case VEC_COVARIANT:
117  {
118  Mat4d invmat = mat.inverse();
119  invmat = invmat.transpose();
120 
121  if (vecType == VEC_COVARIANT_NORMALIZE) {
122  foreach(grid.beginValueAll(), MatMulNormalize(invmat));
123  } else {
124  foreach(grid.beginValueAll(), MatMul(invmat));
125  }
126  break;
127  }
128 
130  foreach(grid.beginValueAll(), MatMul(mat));
131  break;
132 
134  foreach(grid.beginValueAll(), HomogeneousMatMul(mat));
135  break;
136 
137  case VEC_INVARIANT:
138  break;
139  }
140 }
141 
142 
143 template<typename GridType>
144 inline void
145 transformVectors(GridType& grid, const Mat4d& mat)
146 {
147  doTransformVectors<GridType>(grid, mat);
148 }
149 
150 } // namespace tools
151 } // namespace OPENVDB_VERSION_NAME
152 } // namespace openvdb
153 
154 #endif // OPENVDB_TOOLS_VECTORTRANSFORMER_HAS_BEEN_INCLUDED
155 
156 // Copyright (c) 2012-2017 DreamWorks Animation LLC
157 // All rights reserved. This software is distributed under the
158 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Vec3< T0 > transformH(const Vec3< T0 > &p) const
Transform a Vec3 by post-multiplication, doing homogenous divison.
Definition: Mat4.h:1080
const GLdouble * v
Definition: glcorearb.h:836
void transformVectors(GridType &, const Mat4d &)
Apply an affine transform to the voxel values of a vector-valued grid in accordance with the grid's v...
void operator()(const TreeIterT &it) const
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Vec3< T0 > transform3x3(const Vec3< T0 > &v) const
Transform a Vec3 by post-multiplication, without translation.
Definition: Mat4.h:1123
hboost::disable_if_c< VecTraits< typename GridType::ValueType >::IsVec, void >::type doTransformVectors(GridType &, const Mat4d &)
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
math::Mat4< double > Mat4d
Definition: Types.h:103
Mat4 inverse(T tolerance=0) const
Definition: Mat4.h:539
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101