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