OpenVDB  12.0.0
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 
26 class GA_SplittableRange;
27 class OBJ_Camera;
28 class OP_Context;
29 class 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 
40 namespace openvdb_houdini {
41 
42 class Interrupter;
43 
44 
45 /// Add geometry to the given detail to indicate the extents of a frustum transform.
47 void
48 drawFrustum(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.
55 openvdb::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.
68 bool
69 pointInPrimGroup(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
79 std::unique_ptr<GU_Detail>
80 convertGeometry(const GU_Detail&, std::string& warning, openvdb::util::NullInterrupter*);
81 
82 
83 OPENVDB_DEPRECATED_MESSAGE("openvdb_houdini::Interrupter has been deprecated, use openvdb_houdini::HoudiniInterrupter")
85 std::unique_ptr<GU_Detail>
86 convertGeometry(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 {
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.
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.
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
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>
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>
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
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
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
TBB body object for threaded primitive copy.
Definition: GeometryUtil.h:114
Definition: Tree.h:194
TBB body object for threaded vertex normal generation.
Definition: GeometryUtil.h:132
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.
#define OPENVDB_HOUDINI_API
Definition: Platform.h:276
TBB body object for threaded world to voxel space transformation and copy of points.
Definition: GeometryUtil.h:93
Definition: Coord.h:590
void run(bool threaded=true)
Definition: GeometryUtil.h:217
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:110
GenAdaptivityMaskOp(const GU_Detail &refGeo, const IndexTreeType &indexTree, BoolLeafManager &, float edgetolerance=0.0)
Definition: GeometryUtil.h:203
TBB body object for threaded sharp feature construction.
Definition: GeometryUtil.h:156
constexpr Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
Definition: Util.h:22
TBB body object for threaded sharp feature construction.
Definition: GeometryUtil.h:182
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 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: VoxToNanoVDB.h:15
Convert polygonal meshes that consist of quads and/or triangles into signed or unsigned distance fiel...
OutGridT XformOp bool threaded
Definition: ValueTransformer.h:140
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:446
OPENVDB_AX_API void run(const char *ax, openvdb::GridBase &grid, const AttributeBindings &bindings={})
Run a full AX pipeline (parse, compile and execute) on a single OpenVDB Grid.
Definition: AttributeTransferUtil.h:34
openvdb::tools::MeshToVoxelEdgeData EdgeData
Definition: GeometryUtil.h:159
OPENVDB_HOUDINI_API std::unique_ptr< GU_Detail > convertGeometry(const GU_Detail &, std::string &warning, openvdb::util::NullInterrupter *)
Convert geometry to quads and triangles.
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:106
A LeafManager manages a linear array of pointers to a given tree&#39;s leaf nodes, as well as optional au...
openvdb::tree::LeafManager< BoolTreeType > BoolLeafManager
Definition: GeometryUtil.h:185
void operator()(const tbb::blocked_range< size_t > &) const
Definition: GeometryUtil.h:229
#define OPENVDB_DEPRECATED_MESSAGE(msg)
Definition: Platform.h:148