OpenVDB 13.0.0

Loading...
Searching...
No Matches
UT_VDBTools.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3//
4/// @file UT_VDBTools.h
5/// @author FX R&D Simulation team
6/// @brief Less commonly-used utility classes and functions for OpenVDB plugins
7
8#ifndef OPENVDB_HOUDINI_UT_VDBTOOLS_HAS_BEEN_INCLUDED
9#define OPENVDB_HOUDINI_UT_VDBTOOLS_HAS_BEEN_INCLUDED
10
11#include <openvdb/openvdb.h>
12#include <openvdb/tools/GridTransformer.h>
13#include "Utils.h" // for GridPtr
14
15 namespace openvdb_houdini {
16
17/// @brief GridTransformOp is a functor class for use with GridBase::apply()
18/// that samples an input grid into an output grid of the same type through
19/// a given affine transform.
20/// @details The output grid's transform is unchanged by this operation.
21/// @sa GridResampleOp, GridResampleToMatchOp
22/// @par Example:
23/// @code
24/// const Grid& inGrid = ...; // generic reference to a grid of any type
25///
26/// // Create a new, empty output grid of the same (so far, unknown) type
27/// // as the input grid and with the same transform and metadata.
28/// GridPtr outGrid = inGrid.copyGridWithNewTree();
29///
30/// // Initialize a GridTransformer with the parameters of an affine transform.
31/// openvdb::tools::GridTransformer xform(pivot, scale, rotate, ...);
32///
33/// // Resolve the input grid's type and resample it into the output grid,
34/// // using a second-order sampling kernel.
35/// GridTransformOp<openvdb::tools::QuadraticSampler> op(outGrid, xform);
36/// inGrid.apply<openvdb_houdini::ScalarGridTypes>(op);
37/// @endcode
38 template<typename Sampler>
39 class GridTransformOp
40{
41 public:
42 /// @param outGrid a generic pointer to an output grid of the same type
43 /// as the grid to be resampled
44 /// @param t a @c GridTransformer that defines an affine transform
45 /// @note GridTransformOp makes an internal copy of the @c GridTransformer
46 /// and supplies the copy with a default Interrupter that replaces any
47 /// existing interrupter.
48 GridTransformOp(GridPtr& outGrid, const openvdb::tools::GridTransformer& t):
49 mOutGrid(outGrid), mTransformer(t) {}
50
51 template<typename GridType>
52 void operator()(const GridType& inGrid)
53 {
54 typename GridType::Ptr outGrid = openvdb::gridPtrCast<GridType>(mOutGrid);
55
56 HoudiniInterrupter interrupter;
57 mTransformer.setInterrupter(interrupter.interrupter());
58
59 mTransformer.transformGrid<Sampler, GridType>(inGrid, *outGrid);
60 }
61
62 private:
63 GridPtr mOutGrid;
64 openvdb::tools::GridTransformer mTransformer;
65};
66
67
68////////////////////////////////////////
69
70
71/// @brief GridResampleOp is a functor class for use with UTvdbProcessTypedGrid()
72/// that samples an input grid into an output grid of the same type through
73/// a given transform.
74/// @details The output grid's transform is unchanged by this operation.
75/// @sa GridTransformOp, GridResampleToMatchOp
76/// @par Example:
77/// @code
78/// namespace {
79/// // Class that implements GridResampler's Transformer interface
80/// struct MyXform
81/// {
82/// bool isAffine() const { ... }
83/// openvdb::Vec3d transform(const openvdb::Vec3d&) const { ... }
84/// openvdb::Vec3d invTransform(const openvdb::Vec3d&) const { ... }
85/// };
86/// }
87///
88/// const Grid& inGrid = ...; // generic reference to a grid of any type
89///
90/// // Create a new, empty output grid of the same (so far, unknown) type
91/// // as the input grid and with the same transform and metadata.
92/// GridPtr outGrid = inGrid.copyGridWithNewTree();
93///
94/// // Resolve the input grid's type and resample it into the output grid,
95/// // using a trilinear sampling kernel.
96/// GridResampleOp<openvdb::tools::BoxSampler, MyXform> op(outGrid, MyXform());
97/// inGrid.apply<openvdb_houdini::ScalarGridTypes>(op);
98/// @endcode
99 template<typename Sampler, typename TransformerType>
100 class GridResampleOp
101{
102 public:
103 /// @param outGrid a generic pointer to an output grid of the same type
104 /// as the grid to be resampled
105 /// @param t an object that implements <tt>GridResampler</tt>'s
106 /// Transformer interface
107 /// @note GridResampleOp makes an internal copy of @a t.
108 GridResampleOp(GridPtr& outGrid, const TransformerType& t):
109 mOutGrid(outGrid), mTransformer(t) {}
110
111 template<typename GridType>
112 void operator()(const GridType& inGrid)
113 {
114 typename GridType::Ptr outGrid = openvdb::gridPtrCast<GridType>(mOutGrid);
115
116 openvdb::tools::GridResampler resampler;
117
118 HoudiniInterrupter interrupter;
119 resampler.setInterrupter(interrupter.interrupter());
120
121 resampler.transformGrid<Sampler>(mTransformer, inGrid, *outGrid);
122 }
123
124 private:
125 GridPtr mOutGrid;
126 const TransformerType mTransformer;
127};
128
129
130////////////////////////////////////////
131
132
133/// @brief GridResampleToMatchOp is a functor class for use with
134/// GridBase::apply() that samples an input grid into an output grid
135/// of the same type such that, after resampling, the input and output grids
136/// coincide, but the output grid's transform is unchanged.
137/// @sa GridTransformOp, GridResampleOp
138/// @par Example:
139/// @code
140/// const Grid& inGrid = ...; // generic reference to a grid of any type
141///
142/// // Create a new, empty output grid of the same (so far, unknown) type as
143/// // the input grid and with the same metadata, but with a different transform.
144/// GridPtr outGrid = inGrid.copyGridWithNewTree();
145/// outGrid->setTransform(myTransform);
146///
147/// // Resolve the input grid's type and resample it into the output grid,
148/// // using a second-order sampling kernel.
149/// GridResampleToMatchOp<openvdb::tools::QuadraticSampler> op(outGrid);
150/// inGrid.apply<openvdb_houdini::ScalarGridTypes>(op);
151/// @endcode
152 template<typename Sampler>
153 class GridResampleToMatchOp
154{
155 public:
156 GridResampleToMatchOp(GridPtr outGrid): mOutGrid(outGrid) {}
157
158 template<typename GridType>
159 void operator()(const GridType& inGrid)
160 {
161 typename GridType::Ptr outGrid = openvdb::gridPtrCast<GridType>(mOutGrid);
162 HoudiniInterrupter interrupter;
163 openvdb::tools::resampleToMatch<Sampler>(inGrid, *outGrid, interrupter.interrupter());
164 }
165
166 private:
167 GridPtr mOutGrid;
168};
169
170} // namespace openvdb_houdini
171
172#endif // OPENVDB_HOUDINI_UT_VDBTOOLS_HAS_BEEN_INCLUDED
GridTransformer.h
openvdb_houdini::GridResampleOp::operator()
void operator()(const GridType &inGrid)
Definition UT_VDBTools.h:112
openvdb_houdini::GridResampleOp::GridResampleOp
GridResampleOp(GridPtr &outGrid, const TransformerType &t)
Definition UT_VDBTools.h:108
openvdb_houdini::GridResampleToMatchOp::operator()
void operator()(const GridType &inGrid)
Definition UT_VDBTools.h:159
openvdb_houdini::GridResampleToMatchOp::GridResampleToMatchOp
GridResampleToMatchOp(GridPtr outGrid)
Definition UT_VDBTools.h:156
openvdb_houdini::GridTransformOp::GridTransformOp
GridTransformOp(GridPtr &outGrid, const openvdb::tools::GridTransformer &t)
Definition UT_VDBTools.h:48
openvdb_houdini::GridTransformOp::operator()
void operator()(const GridType &inGrid)
Definition UT_VDBTools.h:52
openvdb_houdini::HoudiniInterrupter
Wrapper class that adapts a Houdini UT_Interrupt object for use with OpenVDB library routines.
Definition Utils.h:184
openvdb::v13_0::gridPtrCast
GridType::Ptr gridPtrCast(const GridBase::Ptr &grid)
Cast a generic grid pointer to a pointer to a grid of a concrete class.
Definition Grid.h:998
openvdb_houdini
Definition AttributeTransferUtil.h:34
openvdb_houdini::GridPtr
openvdb::GridBase::Ptr GridPtr
Definition Utils.h:44
openvdb.h
Utils.h
Utility classes and functions for OpenVDB plugins.