OpenVDB 3.2.0

VectorTransformer.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2016 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 <boost/utility/enable_if.hpp>
41
42 namespace openvdb {
43 OPENVDB_USE_VERSION_NAMESPACE
44 namespace OPENVDB_VERSION_NAME {
45 namespace tools {
46
50 template<typename GridType>
51 inline void
52 transformVectors(GridType&, const Mat4d&);
53
54
56
57
58 // Functors for use with tools::foreach() to transform vector voxel values
59
60 struct HomogeneousMatMul
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
83 struct MatMulNormalize
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
99 template<typename GridType> inline
100 typename boost::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
107 template<typename GridType> inline
108 typename boost::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:
116 case VEC_COVARIANT_NORMALIZE:
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
129 case VEC_CONTRAVARIANT_RELATIVE:
130 foreach(grid.beginValueAll(), MatMul(mat));
131 break;
132
133 case VEC_CONTRAVARIANT_ABSOLUTE:
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-2016 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/ )
openvdb::v3_2_0::Mat4d
math::Mat4< double > Mat4d
Definition: Types.h:102
openvdb::v3_2_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:145
Types.h
Vec3.h
openvdb::v3_2_0::tools::MatMulNormalize
Definition: VectorTransformer.h:83
ValueTransformer.h
openvdb::v3_2_0::tools::doTransformVectors
boost::enable_if_c< VecTraits< typename GridType::ValueType >::IsVec, void >::type doTransformVectors(GridType &grid, const Mat4d &mat)
Definition: VectorTransformer.h:109
openvdb::v3_2_0::tools::MatMulNormalize::operator()
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:88
openvdb::v3_2_0::VecType
VecType
Definition: Types.h:241
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Mat4.h
openvdb::v3_2_0::tools::MatMul::MatMul
MatMul(const Mat4d &_mat)
Definition: VectorTransformer.h:74
openvdb::v3_2_0::math::Mat4::transformH
Vec3< T0 > transformH(const Vec3< T0 > &p) const
Transform a Vec3 by post-multiplication, doing homogenous divison.
Definition: Mat4.h:1068
openvdb::v3_2_0::math::Mat4< double >
openvdb::v3_2_0::math::Mat4::inverse
Mat4 inverse(T tolerance=0) const
Definition: Mat4.h:527
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v3_2_0::math::Vec3d
Vec3< double > Vec3d
Definition: Vec3.h:651
openvdb::v3_2_0::math::Mat4::transpose
Mat4 transpose() const
Definition: Mat4.h:514
openvdb::v3_2_0::VEC_INVARIANT
Definition: Types.h:242
openvdb::v3_2_0::tools::MatMul::mat
const Mat4d mat
Definition: VectorTransformer.h:73
openvdb::v3_2_0::tools::MatMulNormalize::mat
const Mat4d mat
Definition: VectorTransformer.h:85
openvdb
Definition: Exceptions.h:39
openvdb::v3_2_0::tools::HomogeneousMatMul::mat
const Mat4d mat
Definition: VectorTransformer.h:62
openvdb::v3_2_0::VEC_COVARIANT
Definition: Types.h:243
openvdb::v3_2_0::VEC_CONTRAVARIANT_ABSOLUTE
Definition: Types.h:246
openvdb::v3_2_0::tools::HomogeneousMatMul
Definition: VectorTransformer.h:60
openvdb::v3_2_0::TypeError
Definition: Exceptions.h:87
openvdb::v3_2_0::tools::MatMulNormalize::MatMulNormalize
MatMulNormalize(const Mat4d &_mat)
Definition: VectorTransformer.h:86
openvdb::v3_2_0::tools::MatMul::operator()
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:76
openvdb::v3_2_0::VEC_COVARIANT_NORMALIZE
Definition: Types.h:244
openvdb::v3_2_0::math::Mat4::transform3x3
Vec3< T0 > transform3x3(const Vec3< T0 > &v) const
Transform a Vec3 by post-multiplication, without translation.
Definition: Mat4.h:1111
openvdb::v3_2_0::tools::HomogeneousMatMul::operator()
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:64
openvdb::v3_2_0::tools::HomogeneousMatMul::HomogeneousMatMul
HomogeneousMatMul(const Mat4d &_mat)
Definition: VectorTransformer.h:63
openvdb::v3_2_0::VEC_CONTRAVARIANT_RELATIVE
Definition: Types.h:245
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v3_2_0::tools::MatMul
Definition: VectorTransformer.h:71