HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VectorTransformer.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
4 /// @file VectorTransformer.h
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 
21 /// @brief Apply an affine transform to the voxel values of a vector-valued grid
22 /// in accordance with the grid's vector type (covariant, contravariant, etc.).
23 /// @throw TypeError if the grid is not vector-valued
24 template<typename GridType>
25 inline void
26 transformVectors(GridType&, const Mat4d&);
27 
28 
29 ////////////////////////////////////////
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 //{
73 /// @cond OPENVDB_DOCS_INTERNAL
74 
75 /// @internal This overload is enabled only for scalar-valued grids.
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 
83 /// @internal This overload is enabled only for vector-valued grids.
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 
119 /// @endcond
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
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:178
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...
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
void operator()(const TreeIterT &it) const
const GLdouble * v
Definition: glcorearb.h:836
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:114
Mat4 inverse(T tolerance=0) const
Definition: Mat4.h:499
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74