OpenVDB: openvdb_houdini/GeometryUtil.h Source File

OpenVDB 10.1.0

Loading...
Searching...
No Matches
GeometryUtil.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 GeometryUtil.h
5/// @author FX R&D Simulation team
6/// @brief Utility methods and tools for geometry processing
7
8#ifndef OPENVDB_HOUDINI_GEOMETRY_UTIL_HAS_BEEN_INCLUDED
9#define OPENVDB_HOUDINI_GEOMETRY_UTIL_HAS_BEEN_INCLUDED
10
11#include <openvdb/openvdb.h>
12#include <openvdb/tools/MeshToVolume.h> // for openvdb::tools::MeshToVoxelEdgeData
13#include <openvdb/tree/LeafManager.h>
14#include <openvdb/util/NullInterrupter.h>
15#include <openvdb/util/Util.h> // for openvdb::util::COORD_OFFSETS
16
17#include <GU/GU_Detail.h>
18
19#include <algorithm> // for std::max/min()
20#include <memory>
21#include <string>
22#include <vector>
23
24
25class GA_SplittableRange;
26class OBJ_Camera;
27class OP_Context;
28class OP_Node;
29
30
31#ifdef SESI_OPENVDB
32 #ifdef OPENVDB_HOUDINI_API
33 #undef OPENVDB_HOUDINI_API
34 #define OPENVDB_HOUDINI_API
35 #endif
36#endif
37
38
39namespace openvdb_houdini {
40
41class Interrupter;
42
43
44/// Add geometry to the given detail to indicate the extents of a frustum transform.
45 OPENVDB_HOUDINI_API
46void
47drawFrustum(GU_Detail&, const openvdb::math::Transform&,
48 const UT_Vector3* boxColor, const UT_Vector3* tickColor,
49 bool shaded, bool drawTicks = true);
50
51
52/// Construct a frustum transform from a Houdini camera.
53 OPENVDB_HOUDINI_API
54openvdb::math::Transform::Ptr
55frustumTransformFromCamera(
56 OP_Node&, OP_Context&, OBJ_Camera&,
57 float offset, float nearPlaneDist, float farPlaneDist,
58 float voxelDepthSize = 1.0, int voxelCountX = 100);
59
60
61////////////////////////////////////////
62
63
64/// @brief Return @c true if the point at the given offset is referenced
65/// by primitives from a certain primitive group.
66 OPENVDB_HOUDINI_API
67bool
68pointInPrimGroup(GA_Offset ptnOffset, GU_Detail&, const GA_PrimitiveGroup&);
69
70
71////////////////////////////////////////
72
73
74/// @brief Convert geometry to quads and triangles.
75/// @return a pointer to a new GU_Detail object if the geometry was
76/// converted or subdivided, otherwise a null pointer
77 OPENVDB_HOUDINI_API
78std::unique_ptr<GU_Detail>
79convertGeometry(const GU_Detail&, std::string& warning, openvdb::util::NullInterrupter*);
80
81
82 OPENVDB_DEPRECATED_MESSAGE("openvdb_houdini::Interrupter has been deprecated, use openvdb_houdini::HoudiniInterrupter")
83 OPENVDB_HOUDINI_API
84 std::unique_ptr<GU_Detail>
85 convertGeometry(const GU_Detail& detail, std::string& warning, Interrupter* boss);
86
87
88////////////////////////////////////////
89
90
91/// TBB body object for threaded world to voxel space transformation and copy of points
92class OPENVDB_HOUDINI_API TransformOp
93{
94public:
95 TransformOp(GU_Detail const * const gdp,
96 const openvdb::math::Transform& transform,
97 std::vector<openvdb::Vec3s>& pointList);
98
99 void operator()(const GA_SplittableRange&) const;
100
101private:
102 GU_Detail const * const mGdp;
103 const openvdb::math::Transform& mTransform;
104 std::vector<openvdb::Vec3s>* const mPointList;
105};
106
107
108////////////////////////////////////////
109
110
111/// @brief TBB body object for threaded primitive copy
112/// @details Produces a primitive-vertex index list.
113 class OPENVDB_HOUDINI_API PrimCpyOp
114{
115public:
116 PrimCpyOp(GU_Detail const * const gdp, std::vector<openvdb::Vec4I>& primList);
117 void operator()(const GA_SplittableRange&) const;
118
119private:
120 GU_Detail const * const mGdp;
121 std::vector<openvdb::Vec4I>* const mPrimList;
122};
123
124
125////////////////////////////////////////
126
127
128/// @brief TBB body object for threaded vertex normal generation
129/// @details Averages face normals from all similarly oriented primitives,
130/// that share the same vertex-point, to maintain sharp features.
131 class OPENVDB_HOUDINI_API VertexNormalOp
132{
133public:
134 VertexNormalOp(GU_Detail&, const GA_PrimitiveGroup* interiorPrims=nullptr, float angle=0.7f);
135 void operator()(const GA_SplittableRange&) const;
136
137private:
138 bool isInteriorPrim(GA_Offset primOffset) const
139 {
140 return mInteriorPrims && mInteriorPrims->containsIndex(
141 mDetail.primitiveIndex(primOffset));
142 }
143
144 const GU_Detail& mDetail;
145 const GA_PrimitiveGroup* mInteriorPrims;
146 GA_RWHandleV3 mNormalHandle;
147 const float mAngle;
148};
149
150
151////////////////////////////////////////
152
153
154/// TBB body object for threaded sharp feature construction
155 class OPENVDB_HOUDINI_API SharpenFeaturesOp
156{
157public:
158 using EdgeData = openvdb::tools::MeshToVoxelEdgeData;
159
160 SharpenFeaturesOp(GU_Detail& meshGeo, const GU_Detail& refGeo, EdgeData& edgeData,
161 const openvdb::math::Transform& xform, const GA_PrimitiveGroup* surfacePrims = nullptr,
162 const openvdb::BoolTree* mask = nullptr);
163
164 void operator()(const GA_SplittableRange&) const;
165
166private:
167 GU_Detail& mMeshGeo;
168 const GU_Detail& mRefGeo;
169 EdgeData& mEdgeData;
170 const openvdb::math::Transform& mXForm;
171 const GA_PrimitiveGroup* mSurfacePrims;
172 const openvdb::BoolTree* mMaskTree;
173};
174
175
176////////////////////////////////////////
177
178
179/// TBB body object for threaded sharp feature construction
180template<typename IndexTreeType, typename BoolTreeType>
181 class GenAdaptivityMaskOp
182{
183public:
184 using BoolLeafManager = openvdb::tree::LeafManager<BoolTreeType>;
185
186 GenAdaptivityMaskOp(const GU_Detail& refGeo,
187 const IndexTreeType& indexTree, BoolLeafManager&, float edgetolerance = 0.0);
188
189 void run(bool threaded = true);
190
191 void operator()(const tbb::blocked_range<size_t>&) const;
192
193private:
194 const GU_Detail& mRefGeo;
195 const IndexTreeType& mIndexTree;
196 BoolLeafManager& mLeafs;
197 float mEdgeTolerance;
198};
199
200
201template<typename IndexTreeType, typename BoolTreeType>
202GenAdaptivityMaskOp<IndexTreeType, BoolTreeType>::GenAdaptivityMaskOp(const GU_Detail& refGeo,
203 const IndexTreeType& indexTree, BoolLeafManager& leafMgr, float edgetolerance)
204 : mRefGeo(refGeo)
205 , mIndexTree(indexTree)
206 , mLeafs(leafMgr)
207 , mEdgeTolerance(edgetolerance)
208{
209 mEdgeTolerance = std::max(0.0f, mEdgeTolerance);
210 mEdgeTolerance = std::min(1.0f, mEdgeTolerance);
211}
212
213
214template<typename IndexTreeType, typename BoolTreeType>
215void
216GenAdaptivityMaskOp<IndexTreeType, BoolTreeType>::run(bool threaded)
217{
218 if (threaded) {
219 tbb::parallel_for(mLeafs.getRange(), *this);
220 } else {
221 (*this)(mLeafs.getRange());
222 }
223}
224
225
226template<typename IndexTreeType, typename BoolTreeType>
227void
228GenAdaptivityMaskOp<IndexTreeType, BoolTreeType>::operator()(
229 const tbb::blocked_range<size_t>& range) const
230{
231 using IndexAccessorType = typename openvdb::tree::ValueAccessor<const IndexTreeType>;
232 IndexAccessorType idxAcc(mIndexTree);
233
234 UT_Vector3 tmpN, normal;
235 GA_Offset primOffset;
236 int tmpIdx;
237
238 openvdb::Coord ijk, nijk;
239 typename BoolTreeType::LeafNodeType::ValueOnIter iter;
240
241 for (size_t n = range.begin(); n < range.end(); ++n) {
242 iter = mLeafs.leaf(n).beginValueOn();
243 for (; iter; ++iter) {
244 ijk = iter.getCoord();
245
246 bool edgeVoxel = false;
247
248 int idx = idxAcc.getValue(ijk);
249
250 primOffset = mRefGeo.primitiveOffset(idx);
251 normal = mRefGeo.getGEOPrimitive(primOffset)->computeNormal();
252
253 for (size_t i = 0; i < 18; ++i) {
254 nijk = ijk + openvdb::util::COORD_OFFSETS[i];
255 if (idxAcc.probeValue(nijk, tmpIdx) && tmpIdx != idx) {
256 primOffset = mRefGeo.primitiveOffset(tmpIdx);
257 tmpN = mRefGeo.getGEOPrimitive(primOffset)->computeNormal();
258
259 if (normal.dot(tmpN) < mEdgeTolerance) {
260 edgeVoxel = true;
261 break;
262 }
263 }
264 }
265
266 if (!edgeVoxel) iter.setValueOff();
267 }
268 }
269}
270
271
272} // namespace openvdb_houdini
273
274
275////////////////////////////////////////
276
277
278#endif // OPENVDB_HOUDINI_GEOMETRY_UTIL_HAS_BEEN_INCLUDED
LeafManager.h
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
MeshToVolume.h
Convert polygonal meshes that consist of quads and/or triangles into signed or unsigned distance fiel...
NullInterrupter.h
OPENVDB_HOUDINI_API
#define OPENVDB_HOUDINI_API
Definition Platform.h:288
OPENVDB_DEPRECATED_MESSAGE
#define OPENVDB_DEPRECATED_MESSAGE(msg)
Definition Platform.h:154
Util.h
openvdb::v10_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:25
openvdb::v10_1::tree::Tree
Definition Tree.h:178
openvdb_houdini::GenAdaptivityMaskOp
TBB body object for threaded sharp feature construction.
Definition GeometryUtil.h:182
openvdb_houdini::GenAdaptivityMaskOp::run
void run(bool threaded=true)
Definition GeometryUtil.h:216
openvdb_houdini::GenAdaptivityMaskOp::operator()
void operator()(const tbb::blocked_range< size_t > &) const
Definition GeometryUtil.h:228
openvdb_houdini::GenAdaptivityMaskOp::BoolLeafManager
openvdb::tree::LeafManager< BoolTreeType > BoolLeafManager
Definition GeometryUtil.h:184
openvdb_houdini::GenAdaptivityMaskOp::GenAdaptivityMaskOp
GenAdaptivityMaskOp(const GU_Detail &refGeo, const IndexTreeType &indexTree, BoolLeafManager &, float edgetolerance=0.0)
Definition GeometryUtil.h:202
openvdb_houdini::Interrupter
Deprecated wrapper class with the same interface as HoudiniInterrupter, however it does not derive fr...
Definition Utils.h:209
openvdb_houdini::PrimCpyOp
TBB body object for threaded primitive copy.
Definition GeometryUtil.h:114
openvdb_houdini::PrimCpyOp::operator()
void operator()(const GA_SplittableRange &) const
openvdb_houdini::PrimCpyOp::PrimCpyOp
PrimCpyOp(GU_Detail const *const gdp, std::vector< openvdb::Vec4I > &primList)
openvdb_houdini::SharpenFeaturesOp
TBB body object for threaded sharp feature construction.
Definition GeometryUtil.h:156
openvdb_houdini::SharpenFeaturesOp::operator()
void operator()(const GA_SplittableRange &) const
openvdb_houdini::SharpenFeaturesOp::SharpenFeaturesOp
SharpenFeaturesOp(GU_Detail &meshGeo, const GU_Detail &refGeo, EdgeData &edgeData, const openvdb::math::Transform &xform, const GA_PrimitiveGroup *surfacePrims=nullptr, const openvdb::BoolTree *mask=nullptr)
openvdb_houdini::SharpenFeaturesOp::EdgeData
openvdb::tools::MeshToVoxelEdgeData EdgeData
Definition GeometryUtil.h:158
openvdb_houdini::TransformOp
TBB body object for threaded world to voxel space transformation and copy of points.
Definition GeometryUtil.h:93
openvdb_houdini::TransformOp::operator()
void operator()(const GA_SplittableRange &) const
openvdb_houdini::TransformOp::TransformOp
TransformOp(GU_Detail const *const gdp, const openvdb::math::Transform &transform, std::vector< openvdb::Vec3s > &pointList)
openvdb_houdini::VertexNormalOp
TBB body object for threaded vertex normal generation.
Definition GeometryUtil.h:132
openvdb_houdini::VertexNormalOp::operator()
void operator()(const GA_SplittableRange &) const
openvdb_houdini::VertexNormalOp::VertexNormalOp
VertexNormalOp(GU_Detail &, const GA_PrimitiveGroup *interiorPrims=nullptr, float angle=0.7f)
detail
Definition VoxToNanoVDB.h:14
openvdb_houdini
Definition AttributeTransferUtil.h:34
openvdb_houdini::pointInPrimGroup
OPENVDB_HOUDINI_API bool pointInPrimGroup(GA_Offset ptnOffset, GU_Detail &, const GA_PrimitiveGroup &)
Return true if the point at the given offset is referenced by primitives from a certain primitive gro...
openvdb_houdini::drawFrustum
OPENVDB_HOUDINI_API void drawFrustum(GU_Detail &, const openvdb::math::Transform &, const UT_Vector3 *boxColor, const UT_Vector3 *tickColor, bool shaded, bool drawTicks=true)
Add geometry to the given detail to indicate the extents of a frustum transform.
openvdb_houdini::convertGeometry
OPENVDB_HOUDINI_API std::unique_ptr< GU_Detail > convertGeometry(const GU_Detail &, std::string &warning, openvdb::util::NullInterrupter *)
Convert geometry to quads and triangles.
openvdb_houdini::frustumTransformFromCamera
OPENVDB_HOUDINI_API openvdb::math::Transform::Ptr frustumTransformFromCamera(OP_Node &, OP_Context &, OBJ_Camera &, float offset, float nearPlaneDist, float farPlaneDist, float voxelDepthSize=1.0, int voxelCountX=100)
Construct a frustum transform from a Houdini camera.
std
Definition Coord.h:589
openvdb.h