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 {
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 
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 
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:
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 
107  foreach(grid.beginValueAll(), MatMul(mat));
108  break;
109 
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
Mat4< double > Mat4d
Definition: Mat4.h:1334
Vec3< double > Vec3d
Definition: Vec3.h:662
HomogeneousMatMul(const Mat4d &_mat)
Definition: VectorTransformer.h:37
Definition: Types.h:483
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
const Mat4d mat
Definition: VectorTransformer.h:36
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:62
MatMulNormalize(const Mat4d &_mat)
Definition: VectorTransformer.h:60
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
Definition: VectorTransformer.h:34
MatMul(const Mat4d &_mat)
Definition: VectorTransformer.h:48
Mat4 transpose() const
Definition: Mat4.h:491
Definition: Exceptions.h:13
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:50
VecType
Definition: Types.h:482
Definition: Types.h:484
const Mat4d mat
Definition: VectorTransformer.h:47
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:38
Definition: VectorTransformer.h:45
const Mat4d mat
Definition: VectorTransformer.h:59
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
Mat4 inverse(T tolerance=0) const
Definition: Mat4.h:504
Definition: VectorTransformer.h:57
Definition: Exceptions.h:64
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:125