OpenVDB: VectorTransformer.h Source File

OpenVDB 7.0.0

VectorTransformer.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
5
6 #ifndef OPENVDB_TOOLS_VECTORTRANSFORMER_HAS_BEEN_INCLUDED
7 #define OPENVDB_TOOLS_VECTORTRANSFORMER_HAS_BEEN_INCLUDED
8
9 #include <openvdb/Types.h>
10 #include <openvdb/math/Mat4.h>
11 #include <openvdb/math/Vec3.h>
12 #include "ValueTransformer.h" // for tools::foreach()
13 #include <type_traits>
14
15
16 namespace openvdb {
17 OPENVDB_USE_VERSION_NAMESPACE
18 namespace OPENVDB_VERSION_NAME {
19 namespace tools {
20
24 template<typename GridType>
25 inline void
26 transformVectors(GridType&, const Mat4d&);
27
28
30
31
32 // Functors for use with tools::foreach() to transform vector voxel values
33
34 struct HomogeneousMatMul
35 {
36 const Mat4d mat;
37 HomogeneousMatMul(const Mat4d& _mat): mat(_mat) {}
38 template<typename TreeIterT> void operator()(const TreeIterT& it) const
39 {
40 Vec3d v(*it);
41 it.setValue(mat.transformH(v));
42 }
43 };
44
45 struct MatMul
46 {
47 const Mat4d mat;
48 MatMul(const Mat4d& _mat): mat(_mat) {}
49 template<typename TreeIterT>
50 void operator()(const TreeIterT& it) const
51 {
52 Vec3d v(*it);
53 it.setValue(mat.transform3x3(v));
54 }
55 };
56
57 struct MatMulNormalize
58 {
59 const Mat4d mat;
60 MatMulNormalize(const Mat4d& _mat): mat(_mat) {}
61 template<typename TreeIterT>
62 void operator()(const TreeIterT& it) const
63 {
64 Vec3d v(*it);
65 v = mat.transform3x3(v);
66 v.normalize();
67 it.setValue(v);
68 }
69 };
70
71
72 //{
74
76 template<typename GridType> inline
77 typename std::enable_if<!VecTraits<typename GridType::ValueType>::IsVec, void>::type
78 doTransformVectors(GridType&, const Mat4d&)
79 {
80 OPENVDB_THROW(TypeError, "tools::transformVectors() requires a vector-valued grid");
81 }
82
84 template<typename GridType> inline
85 typename std::enable_if<VecTraits<typename GridType::ValueType>::IsVec, void>::type
86 doTransformVectors(GridType& grid, const Mat4d& mat)
87 {
88 if (!grid.isInWorldSpace()) return;
89
90 const VecType vecType = grid.getVectorType();
91 switch (vecType) {
92 case VEC_COVARIANT:
93 case VEC_COVARIANT_NORMALIZE:
94 {
95 Mat4d invmat = mat.inverse();
96 invmat = invmat.transpose();
97
98 if (vecType == VEC_COVARIANT_NORMALIZE) {
99 foreach(grid.beginValueAll(), MatMulNormalize(invmat));
100 } else {
101 foreach(grid.beginValueAll(), MatMul(invmat));
102 }
103 break;
104 }
105
106 case VEC_CONTRAVARIANT_RELATIVE:
107 foreach(grid.beginValueAll(), MatMul(mat));
108 break;
109
110 case VEC_CONTRAVARIANT_ABSOLUTE:
111 foreach(grid.beginValueAll(), HomogeneousMatMul(mat));
112 break;
113
114 case VEC_INVARIANT:
115 break;
116 }
117 }
118
120 //}
121
122
123 template<typename GridType>
124 inline void
125 transformVectors(GridType& grid, const Mat4d& mat)
126 {
127 doTransformVectors<GridType>(grid, mat);
128 }
129
130 } // namespace tools
131 } // namespace OPENVDB_VERSION_NAME
132 } // namespace openvdb
133
134 #endif // OPENVDB_TOOLS_VECTORTRANSFORMER_HAS_BEEN_INCLUDED
openvdb::v7_0::tools::HomogeneousMatMul::HomogeneousMatMul
HomogeneousMatMul(const Mat4d &_mat)
Definition: VectorTransformer.h:37
openvdb::v7_0::VecType
VecType
Definition: Types.h:482
openvdb::v7_0::VEC_CONTRAVARIANT_ABSOLUTE
@ VEC_CONTRAVARIANT_ABSOLUTE
Definition: Types.h:487
openvdb::v7_0::VEC_INVARIANT
@ VEC_INVARIANT
Definition: Types.h:483
Types.h
openvdb::v7_0::tools::HomogeneousMatMul::mat
const Mat4d mat
Definition: VectorTransformer.h:36
openvdb::v7_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's v...
Definition: VectorTransformer.h:125
openvdb::v7_0::math::Mat4d
Mat4< double > Mat4d
Definition: Mat4.h:1334
openvdb::v7_0::tools::MatMul::MatMul
MatMul(const Mat4d &_mat)
Definition: VectorTransformer.h:48
openvdb::v7_0::tools::MatMulNormalize::operator()
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:62
openvdb::v7_0::math::Vec3d
Vec3< double > Vec3d
Definition: Vec3.h:662
openvdb::v7_0::tools::MatMul::mat
const Mat4d mat
Definition: VectorTransformer.h:47
Vec3.h
openvdb::v7_0::VEC_CONTRAVARIANT_RELATIVE
@ VEC_CONTRAVARIANT_RELATIVE
Definition: Types.h:486
openvdb::v7_0::math::Mat4::transpose
Mat4 transpose() const
Definition: Mat4.h:491
openvdb::v7_0::TypeError
Definition: Exceptions.h:64
openvdb::v7_0::tools::MatMulNormalize::mat
const Mat4d mat
Definition: VectorTransformer.h:59
openvdb::v7_0::VEC_COVARIANT_NORMALIZE
@ VEC_COVARIANT_NORMALIZE
Definition: Types.h:485
ValueTransformer.h
Mat4.h
openvdb::v7_0::tools::MatMul::operator()
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:50
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::tools::MatMul
Definition: VectorTransformer.h:45
openvdb::v7_0::tools::HomogeneousMatMul::operator()
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:38
openvdb::v7_0::VEC_COVARIANT
@ VEC_COVARIANT
Definition: Types.h:484
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:108
openvdb::v7_0::tools::MatMulNormalize
Definition: VectorTransformer.h:57
openvdb::v7_0::math::Mat4::inverse
Mat4 inverse(T tolerance=0) const
Definition: Mat4.h:504
openvdb
Definition: Exceptions.h:13
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
openvdb::v7_0::tools::HomogeneousMatMul
Definition: VectorTransformer.h:34
openvdb::v7_0::tools::MatMulNormalize::MatMulNormalize
MatMulNormalize(const Mat4d &_mat)
Definition: VectorTransformer.h:60