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