OpenVDB 10.0.1
Loading...
Searching...
No Matches
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 <openvdb/openvdb.h>
13#include "ValueTransformer.h" // for tools::foreach()
14#include <type_traits>
15
16
17namespace openvdb {
19namespace OPENVDB_VERSION_NAME {
20namespace tools {
21
22/// @brief Apply an affine transform to the voxel values of a vector-valued grid
23/// in accordance with the grid's vector type (covariant, contravariant, etc.).
24/// @throw TypeError if the grid is not vector-valued
25template<typename GridType>
26void
27transformVectors(GridType&, const Mat4d&);
28
29
30////////////////////////////////////////
31
32
33// Functors for use with tools::foreach() to transform vector voxel values
34
36{
37 const Mat4d mat;
38 HomogeneousMatMul(const Mat4d& _mat): mat(_mat) {}
39 template<typename TreeIterT> void operator()(const TreeIterT& it) const
40 {
41 Vec3d v(*it);
42 it.setValue(mat.transformH(v));
43 }
44};
45
46struct MatMul
47{
48 const Mat4d mat;
49 MatMul(const Mat4d& _mat): mat(_mat) {}
50 template<typename TreeIterT>
51 void operator()(const TreeIterT& it) const
52 {
53 Vec3d v(*it);
54 it.setValue(mat.transform3x3(v));
55 }
56};
57
59{
60 const Mat4d mat;
61 MatMulNormalize(const Mat4d& _mat): mat(_mat) {}
62 template<typename TreeIterT>
63 void operator()(const TreeIterT& it) const
64 {
65 Vec3d v(*it);
66 v = mat.transform3x3(v);
67 v.normalize();
68 it.setValue(v);
69 }
70};
71
72
73//{
74/// @cond OPENVDB_DOCS_INTERNAL
75
76/// @internal This overload is enabled only for scalar-valued grids.
77template<typename GridType> inline
78typename std::enable_if<!VecTraits<typename GridType::ValueType>::IsVec, void>::type
79doTransformVectors(GridType&, const Mat4d&)
80{
81 OPENVDB_THROW(TypeError, "tools::transformVectors() requires a vector-valued grid");
82}
83
84/// @internal This overload is enabled only for vector-valued grids.
85template<typename GridType> inline
86typename std::enable_if<VecTraits<typename GridType::ValueType>::IsVec, void>::type
87doTransformVectors(GridType& grid, const Mat4d& mat)
88{
89 if (!grid.isInWorldSpace()) return;
90
91 const VecType vecType = grid.getVectorType();
92 switch (vecType) {
93 case VEC_COVARIANT:
95 {
96 Mat4d invmat = mat.inverse();
97 invmat = invmat.transpose();
98
99 if (vecType == VEC_COVARIANT_NORMALIZE) {
100 foreach(grid.beginValueAll(), MatMulNormalize(invmat));
101 } else {
102 foreach(grid.beginValueAll(), MatMul(invmat));
103 }
104 break;
105 }
106
108 foreach(grid.beginValueAll(), MatMul(mat));
109 break;
110
112 foreach(grid.beginValueAll(), HomogeneousMatMul(mat));
113 break;
114
115 case VEC_INVARIANT:
116 break;
117 }
118}
119
120/// @endcond
121//}
122
123
124template<typename GridType>
125void
126transformVectors(GridType& grid, const Mat4d& mat)
127{
128 doTransformVectors<GridType>(grid, mat);
129}
130
131
132////////////////////////////////////////
133
134
135// Explicit Template Instantiation
136
137#ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION
138
139#ifdef OPENVDB_INSTANTIATE_VECTORTRANSFORMER
141#endif
142
143#define _FUNCTION(TreeT) \
144 void transformVectors(Grid<TreeT>&, const Mat4d&)
146#undef _FUNCTION
147
148#endif // OPENVDB_USE_EXPLICIT_INSTANTIATION
149
150
151} // namespace tools
152} // namespace OPENVDB_VERSION_NAME
153} // namespace openvdb
154
155#endif // OPENVDB_TOOLS_VECTORTRANSFORMER_HAS_BEEN_INCLUDED
#define _mat(m, r, c)
Definition: Exceptions.h:64
Mat4 inverse(T tolerance=0) const
Definition: Mat4.h:485
Mat4 transpose() const
Definition: Mat4.h:472
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:126
VecType
Definition: Types.h:444
@ VEC_CONTRAVARIANT_ABSOLUTE
Definition: Types.h:449
@ VEC_CONTRAVARIANT_RELATIVE
Definition: Types.h:448
@ VEC_COVARIANT
Definition: Types.h:446
@ VEC_COVARIANT_NORMALIZE
Definition: Types.h:447
@ VEC_INVARIANT
Definition: Types.h:445
Definition: Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
Definition: VectorTransformer.h:36
HomogeneousMatMul(const Mat4d &_mat)
Definition: VectorTransformer.h:38
const Mat4d mat
Definition: VectorTransformer.h:37
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:39
Definition: VectorTransformer.h:59
MatMulNormalize(const Mat4d &_mat)
Definition: VectorTransformer.h:61
const Mat4d mat
Definition: VectorTransformer.h:60
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:63
Definition: VectorTransformer.h:47
MatMul(const Mat4d &_mat)
Definition: VectorTransformer.h:49
const Mat4d mat
Definition: VectorTransformer.h:48
void operator()(const TreeIterT &it) const
Definition: VectorTransformer.h:51
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212
#define OPENVDB_VEC3_TREE_INSTANTIATE(Function)
Definition: version.h.in:159