OpenVDB 13.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
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 <OBJ/OBJ_Camera.h>
18#include <GU/GU_Detail.h>
19#include <GEO/GEO_Primitive.h>
20#include <UT/UT_Version.h>
21
22#include <algorithm> // for std::max/min()
23#include <memory>
24#include <string>
25#include <vector>
26
27
28 class GA_SplittableRange;
29 class OP_Context;
30 class OP_Node;
31
32
33#ifdef SESI_OPENVDB
34 #ifdef OPENVDB_HOUDINI_API
35 #undef OPENVDB_HOUDINI_API
36 #define OPENVDB_HOUDINI_API
37 #endif
38#endif
39
40
41 namespace openvdb_houdini {
42
43
44/// Add geometry to the given detail to indicate the extents of a frustum transform.
45 OPENVDB_HOUDINI_API
46 void
47 drawFrustum(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#if SYS_VERSION_MAJOR_INT >= 21
54openvdb::math::Transform::Ptr
55 frustumTransformFromCamera(
56 const OBJ_CameraParms&, const UT_Matrix4D&,
57 float offset, float nearPlaneDist, float farPlaneDist,
58 float voxelDepthSize = 1.0, int voxelCountX = 100);
59#else
60 OPENVDB_HOUDINI_API
61openvdb::math::Transform::Ptr
62 frustumTransformFromCamera(
63 OP_Node&, OP_Context&, OBJ_Camera&,
64 float offset, float nearPlaneDist, float farPlaneDist,
65 float voxelDepthSize = 1.0, int voxelCountX = 100);
66#endif
67
68////////////////////////////////////////
69
70
71/// @brief Return @c true if the point at the given offset is referenced
72/// by primitives from a certain primitive group.
73 OPENVDB_HOUDINI_API
74 bool
75 pointInPrimGroup(GA_Offset ptnOffset, GU_Detail&, const GA_PrimitiveGroup&);
76
77
78////////////////////////////////////////
79
80
81/// @brief Convert geometry to quads and triangles.
82/// @return a pointer to a new GU_Detail object if the geometry was
83/// converted or subdivided, otherwise a null pointer
84 OPENVDB_HOUDINI_API
85std::unique_ptr<GU_Detail>
86 convertGeometry(const GU_Detail&, std::string& warning, openvdb::util::NullInterrupter*);
87
88
89////////////////////////////////////////
90
91
92/// TBB body object for threaded world to voxel space transformation and copy of points
93 class OPENVDB_HOUDINI_API TransformOp
94{
95 public:
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
102 private:
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.
114 class OPENVDB_HOUDINI_API PrimCpyOp
115{
116 public:
117 PrimCpyOp(GU_Detail const * const gdp, std::vector<openvdb::Vec4I>& primList);
118 void operator()(const GA_SplittableRange&) const;
119
120 private:
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.
132 class OPENVDB_HOUDINI_API VertexNormalOp
133{
134 public:
135 VertexNormalOp(GU_Detail&, const GA_PrimitiveGroup* interiorPrims=nullptr, float angle=0.7f);
136 void operator()(const GA_SplittableRange&) const;
137
138 private:
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
156 class OPENVDB_HOUDINI_API SharpenFeaturesOp
157{
158 public:
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
167 private:
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
181 template<typename IndexTreeType, typename BoolTreeType>
182 class GenAdaptivityMaskOp
183{
184 public:
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
194 private:
195 const GU_Detail& mRefGeo;
196 const IndexTreeType& mIndexTree;
197 BoolLeafManager& mLeafs;
198 float mEdgeTolerance;
199};
200
201
202 template<typename IndexTreeType, typename BoolTreeType>
203 GenAdaptivityMaskOp<IndexTreeType, BoolTreeType>::GenAdaptivityMaskOp(const GU_Detail& refGeo,
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
215 template<typename IndexTreeType, typename BoolTreeType>
216 void
217 GenAdaptivityMaskOp<IndexTreeType, BoolTreeType>::run(bool threaded)
218{
219 if (threaded) {
220 tbb::parallel_for(mLeafs.getRange(), *this);
221 } else {
222 (*this)(mLeafs.getRange());
223 }
224}
225
226
227 template<typename IndexTreeType, typename BoolTreeType>
228 void
229 GenAdaptivityMaskOp<IndexTreeType, BoolTreeType>::operator()(
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
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:299
openvdb::v13_0::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:26
openvdb_houdini::GenAdaptivityMaskOp::run
void run(bool threaded=true)
Definition GeometryUtil.h:217
openvdb_houdini::GenAdaptivityMaskOp::operator()
void operator()(const tbb::blocked_range< size_t > &) const
Definition GeometryUtil.h:229
openvdb_houdini::GenAdaptivityMaskOp::BoolLeafManager
openvdb::tree::LeafManager< BoolTreeType > BoolLeafManager
Definition GeometryUtil.h:185
openvdb_houdini::GenAdaptivityMaskOp::GenAdaptivityMaskOp
GenAdaptivityMaskOp(const GU_Detail &refGeo, const IndexTreeType &indexTree, BoolLeafManager &, float edgetolerance=0.0)
Definition GeometryUtil.h:203
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::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:159
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::operator()
void operator()(const GA_SplittableRange &) const
openvdb_houdini::VertexNormalOp::VertexNormalOp
VertexNormalOp(GU_Detail &, const GA_PrimitiveGroup *interiorPrims=nullptr, float angle=0.7f)
openvdb::v13_0::BoolTree
tree::Tree4< bool, 5, 4, 3 >::Type BoolTree
Common tree types.
Definition openvdb.h:53
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.
Util.h
openvdb.h