OpenVDB: VectorTransformer.h Source File

OpenVDB 5.0.0

VectorTransformer.h
Go to the documentation of this file.
1 //
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 //
30 //
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 {
44 OPENVDB_USE_VERSION_NAMESPACE
45 namespace OPENVDB_VERSION_NAME {
46 namespace tools {
47
51 template<typename GridType>
52 inline void
53 transformVectors(GridType&, const Mat4d&);
54
55
57
58
59 // Functors for use with tools::foreach() to transform vector voxel values
60
61 struct HomogeneousMatMul
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
84 struct MatMulNormalize
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 //{
101
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
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:
120 case VEC_COVARIANT_NORMALIZE:
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
133 case VEC_CONTRAVARIANT_RELATIVE:
134 foreach(grid.beginValueAll(), MatMul(mat));
135 break;
136
137 case VEC_CONTRAVARIANT_ABSOLUTE:
138 foreach(grid.beginValueAll(), HomogeneousMatMul(mat));
139 break;
140
141 case VEC_INVARIANT:
142 break;
143 }
144 }
145
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-2017 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/ )
openvdb::v5_0::tools::MatMul::operator()
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:77
Types.h
Vec3.h
openvdb::v5_0::math::Mat4::transform3x3
Vec3< T0 > transform3x3(const Vec3< T0 > &v) const
Transform a Vec3 by post-multiplication, without translation.
Definition: Mat4.h:1115
openvdb::v5_0::VEC_COVARIANT
Definition: Types.h:299
openvdb::v5_0::math::Mat4::inverse
Mat4 inverse(T tolerance=0) const
Definition: Mat4.h:531
ValueTransformer.h
openvdb::v5_0::TypeError
Definition: Exceptions.h:90
openvdb::v5_0::VecType
VecType
Definition: Types.h:297
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:108
openvdb::v5_0::tools::MatMulNormalize::MatMulNormalize
MatMulNormalize(const Mat4d &_mat)
Definition: VectorTransformer.h:87
openvdb::v5_0::VEC_CONTRAVARIANT_ABSOLUTE
Definition: Types.h:302
Mat4.h
openvdb::v5_0::tools::MatMulNormalize::operator()
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:89
openvdb::v5_0::math::Vec3d
Vec3< double > Vec3d
Definition: Vec3.h:679
openvdb::v5_0::tools::HomogeneousMatMul::HomogeneousMatMul
HomogeneousMatMul(const Mat4d &_mat)
Definition: VectorTransformer.h:64
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_0::tools::MatMul::MatMul
MatMul(const Mat4d &_mat)
Definition: VectorTransformer.h:75
openvdb
Definition: Exceptions.h:39
openvdb::v5_0::VEC_CONTRAVARIANT_RELATIVE
Definition: Types.h:301
openvdb::v5_0::math::Mat4::transpose
Mat4 transpose() const
Definition: Mat4.h:518
openvdb::v5_0::tools::transformVectors
void transformVectors(GridType &, const Mat4d &)
Apply an affine transform to the voxel values of a vector-valued grid in accordance with the grid&#39;s v...
Definition: VectorTransformer.h:152
openvdb::v5_0::math::Mat4< double >
openvdb::v5_0::math::Mat4::transformH
Vec3< T0 > transformH(const Vec3< T0 > &p) const
Transform a Vec3 by post-multiplication, doing homogenous divison.
Definition: Mat4.h:1072
openvdb::v5_0::tools::MatMul::mat
const Mat4d mat
Definition: VectorTransformer.h:74
openvdb::v5_0::tools::HomogeneousMatMul
Definition: VectorTransformer.h:61
openvdb::v5_0::tools::MatMulNormalize::mat
const Mat4d mat
Definition: VectorTransformer.h:86
openvdb::v5_0::VEC_INVARIANT
Definition: Types.h:298
openvdb::v5_0::tools::MatMulNormalize
Definition: VectorTransformer.h:84
openvdb::v5_0::tools::HomogeneousMatMul::operator()
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:65
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_0::tools::HomogeneousMatMul::mat
const Mat4d mat
Definition: VectorTransformer.h:63
openvdb::v5_0::VEC_COVARIANT_NORMALIZE
Definition: Types.h:300
openvdb::v5_0::Mat4d
math::Mat4< double > Mat4d
Definition: Types.h:106
openvdb::v5_0::tools::MatMul
Definition: VectorTransformer.h:72