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
25 class GA_SplittableRange;
26 class OBJ_Camera;
27 class OP_Context;
28 class 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
39 namespace openvdb_houdini {
40
41 class Interrupter;
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 OPENVDB_HOUDINI_API
54 openvdb::math::Transform::Ptr
55 frustumTransformFromCamera(
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
67 bool
68 pointInPrimGroup(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
78 std::unique_ptr<GU_Detail>
79 convertGeometry(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
92 class OPENVDB_HOUDINI_API TransformOp
93 {
94 public:
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
101 private:
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 {
115 public:
116 PrimCpyOp(GU_Detail const * const gdp, std::vector<openvdb::Vec4I>& primList);
117 void operator()(const GA_SplittableRange&) const;
118
119 private:
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 {
133 public:
134 VertexNormalOp(GU_Detail&, const GA_PrimitiveGroup* interiorPrims=nullptr, float angle=0.7f);
135 void operator()(const GA_SplittableRange&) const;
136
137 private:
isInteriorPrim(GA_Offset primOffset)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 {
157 public:
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
166 private:
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
180 template<typename IndexTreeType, typename BoolTreeType>
181 class GenAdaptivityMaskOp
182 {
183 public:
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
193 private:
194 const GU_Detail& mRefGeo;
195 const IndexTreeType& mIndexTree;
196 BoolLeafManager& mLeafs;
197 float mEdgeTolerance;
198 };
199
200
201 template<typename IndexTreeType, typename BoolTreeType>
GenAdaptivityMaskOp(const GU_Detail & refGeo,const IndexTreeType & indexTree,BoolLeafManager & leafMgr,float edgetolerance)202 GenAdaptivityMaskOp<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
214 template<typename IndexTreeType, typename BoolTreeType>
215 void
run(bool threaded)216 GenAdaptivityMaskOp<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
226 template<typename IndexTreeType, typename BoolTreeType>
227 void
operator()228 GenAdaptivityMaskOp<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
279