OpenVDB  7.2.0
GeometryUtil.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
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
14 #include <openvdb/util/Util.h> // for openvdb::util::COORD_OFFSETS
15 
16 #include <GU/GU_Detail.h>
17 
18 #include <algorithm> // for std::max/min()
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 
24 class GA_SplittableRange;
25 class OBJ_Camera;
26 class OP_Context;
27 class OP_Node;
28 
29 
30 #ifdef SESI_OPENVDB
31  #ifdef OPENVDB_HOUDINI_API
32  #undef OPENVDB_HOUDINI_API
33  #define OPENVDB_HOUDINI_API
34  #endif
35 #endif
36 
37 
38 namespace openvdb_houdini {
39 
40 class Interrupter;
41 
42 
45 void
46 drawFrustum(GU_Detail&, const openvdb::math::Transform&,
47  const UT_Vector3* boxColor, const UT_Vector3* tickColor,
48  bool shaded, bool drawTicks = true);
49 
50 
53 openvdb::math::Transform::Ptr
55  OP_Node&, OP_Context&, OBJ_Camera&,
56  float offset, float nearPlaneDist, float farPlaneDist,
57  float voxelDepthSize = 1.0, int voxelCountX = 100);
58 
59 
61 
62 
66 bool
67 pointInPrimGroup(GA_Offset ptnOffset, GU_Detail&, const GA_PrimitiveGroup&);
68 
69 
71 
72 
77 std::unique_ptr<GU_Detail>
78 convertGeometry(const GU_Detail&, std::string& warning, Interrupter*);
79 
80 
82 
83 
86 {
87 public:
88  TransformOp(GU_Detail const * const gdp,
89  const openvdb::math::Transform& transform,
90  std::vector<openvdb::Vec3s>& pointList);
91 
92  void operator()(const GA_SplittableRange&) const;
93 
94 private:
95  GU_Detail const * const mGdp;
96  const openvdb::math::Transform& mTransform;
97  std::vector<openvdb::Vec3s>* const mPointList;
98 };
99 
100 
102 
103 
107 {
108 public:
109  PrimCpyOp(GU_Detail const * const gdp, std::vector<openvdb::Vec4I>& primList);
110  void operator()(const GA_SplittableRange&) const;
111 
112 private:
113  GU_Detail const * const mGdp;
114  std::vector<openvdb::Vec4I>* const mPrimList;
115 };
116 
117 
119 
120 
125 {
126 public:
127  VertexNormalOp(GU_Detail&, const GA_PrimitiveGroup* interiorPrims=nullptr, float angle=0.7f);
128  void operator()(const GA_SplittableRange&) const;
129 
130 private:
131  bool isInteriorPrim(GA_Offset primOffset) const
132  {
133  return mInteriorPrims && mInteriorPrims->containsIndex(
134  mDetail.primitiveIndex(primOffset));
135  }
136 
137  const GU_Detail& mDetail;
138  const GA_PrimitiveGroup* mInteriorPrims;
139  GA_RWHandleV3 mNormalHandle;
140  const float mAngle;
141 };
142 
143 
145 
146 
149 {
150 public:
151  using EdgeData = openvdb::tools::MeshToVoxelEdgeData;
152 
153  SharpenFeaturesOp(GU_Detail& meshGeo, const GU_Detail& refGeo, EdgeData& edgeData,
154  const openvdb::math::Transform& xform, const GA_PrimitiveGroup* surfacePrims = nullptr,
155  const openvdb::BoolTree* mask = nullptr);
156 
157  void operator()(const GA_SplittableRange&) const;
158 
159 private:
160  GU_Detail& mMeshGeo;
161  const GU_Detail& mRefGeo;
162  EdgeData& mEdgeData;
163  const openvdb::math::Transform& mXForm;
164  const GA_PrimitiveGroup* mSurfacePrims;
165  const openvdb::BoolTree* mMaskTree;
166 };
167 
168 
170 
171 
173 template<typename IndexTreeType, typename BoolTreeType>
175 {
176 public:
177  using BoolLeafManager = openvdb::tree::LeafManager<BoolTreeType>;
178 
179  GenAdaptivityMaskOp(const GU_Detail& refGeo,
180  const IndexTreeType& indexTree, BoolLeafManager&, float edgetolerance = 0.0);
181 
182  void run(bool threaded = true);
183 
184  void operator()(const tbb::blocked_range<size_t>&) const;
185 
186 private:
187  const GU_Detail& mRefGeo;
188  const IndexTreeType& mIndexTree;
189  BoolLeafManager& mLeafs;
190  float mEdgeTolerance;
191 };
192 
193 
194 template<typename IndexTreeType, typename BoolTreeType>
196  const IndexTreeType& indexTree, BoolLeafManager& leafMgr, float edgetolerance)
197  : mRefGeo(refGeo)
198  , mIndexTree(indexTree)
199  , mLeafs(leafMgr)
200  , mEdgeTolerance(edgetolerance)
201 {
202  mEdgeTolerance = std::max(0.0f, mEdgeTolerance);
203  mEdgeTolerance = std::min(1.0f, mEdgeTolerance);
204 }
205 
206 
207 template<typename IndexTreeType, typename BoolTreeType>
208 void
210 {
211  if (threaded) {
212  tbb::parallel_for(mLeafs.getRange(), *this);
213  } else {
214  (*this)(mLeafs.getRange());
215  }
216 }
217 
218 
219 template<typename IndexTreeType, typename BoolTreeType>
220 void
222  const tbb::blocked_range<size_t>& range) const
223 {
224  using IndexAccessorType = typename openvdb::tree::ValueAccessor<const IndexTreeType>;
225  IndexAccessorType idxAcc(mIndexTree);
226 
227  UT_Vector3 tmpN, normal;
228  GA_Offset primOffset;
229  int tmpIdx;
230 
231  openvdb::Coord ijk, nijk;
232  typename BoolTreeType::LeafNodeType::ValueOnIter iter;
233 
234  for (size_t n = range.begin(); n < range.end(); ++n) {
235  iter = mLeafs.leaf(n).beginValueOn();
236  for (; iter; ++iter) {
237  ijk = iter.getCoord();
238 
239  bool edgeVoxel = false;
240 
241  int idx = idxAcc.getValue(ijk);
242 
243  primOffset = mRefGeo.primitiveOffset(idx);
244  normal = mRefGeo.getGEOPrimitive(primOffset)->computeNormal();
245 
246  for (size_t i = 0; i < 18; ++i) {
247  nijk = ijk + openvdb::util::COORD_OFFSETS[i];
248  if (idxAcc.probeValue(nijk, tmpIdx) && tmpIdx != idx) {
249  primOffset = mRefGeo.primitiveOffset(tmpIdx);
250  tmpN = mRefGeo.getGEOPrimitive(primOffset)->computeNormal();
251 
252  if (normal.dot(tmpN) < mEdgeTolerance) {
253  edgeVoxel = true;
254  break;
255  }
256  }
257  }
258 
259  if (!edgeVoxel) iter.setValueOff();
260  }
261  }
262 }
263 
264 
265 } // namespace openvdb_houdini
266 
267 
269 
270 
271 #endif // OPENVDB_HOUDINI_GEOMETRY_UTIL_HAS_BEEN_INCLUDED
MeshToVolume.h
Convert polygonal meshes that consist of quads and/or triangles into signed or unsigned distance fiel...
Util.h
openvdb::v7_2::util::COORD_OFFSETS
OPENVDB_API const Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
openvdb_houdini
Definition: AttributeTransferUtil.h:33
openvdb::v7_2::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:26
openvdb_houdini::GenAdaptivityMaskOp
TBB body object for threaded sharp feature construction.
Definition: GeometryUtil.h:175
openvdb_houdini::TransformOp
TBB body object for threaded world to voxel space transformation and copy of points.
Definition: GeometryUtil.h:86
openvdb_houdini::PrimCpyOp::operator()
void operator()(const GA_SplittableRange &) const
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::v7_2::tree::Tree
Definition: Tree.h:175
openvdb::v7_2::math::angle
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:445
openvdb_houdini::TransformOp::TransformOp
TransformOp(GU_Detail const *const gdp, const openvdb::math::Transform &transform, std::vector< openvdb::Vec3s > &pointList)
openvdb::v7_2::tools::composite::min
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:103
openvdb_houdini::Interrupter
Wrapper class that adapts a Houdini UT_Interrupt object for use with OpenVDB library routines.
Definition: openvdb_houdini/openvdb_houdini/Utils.h:174
openvdb_houdini::SharpenFeaturesOp::operator()
void operator()(const GA_SplittableRange &) const
openvdb_houdini::convertGeometry
OPENVDB_HOUDINI_API std::unique_ptr< GU_Detail > convertGeometry(const GU_Detail &, std::string &warning, Interrupter *)
Convert geometry to quads and triangles.
openvdb_houdini::GenAdaptivityMaskOp::GenAdaptivityMaskOp
GenAdaptivityMaskOp(const GU_Detail &refGeo, const IndexTreeType &indexTree, BoolLeafManager &, float edgetolerance=0.0)
Definition: GeometryUtil.h:195
openvdb_houdini::VertexNormalOp::VertexNormalOp
VertexNormalOp(GU_Detail &, const GA_PrimitiveGroup *interiorPrims=nullptr, float angle=0.7f)
openvdb::v7_2::tools::composite::max
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:107
openvdb_houdini::GenAdaptivityMaskOp::run
void run(bool threaded=true)
Definition: GeometryUtil.h:209
openvdb_houdini::GenAdaptivityMaskOp::operator()
void operator()(const tbb::blocked_range< size_t > &) const
Definition: GeometryUtil.h:221
openvdb_houdini::VertexNormalOp
TBB body object for threaded vertex normal generation.
Definition: GeometryUtil.h:125
openvdb_houdini::SharpenFeaturesOp::EdgeData
openvdb::tools::MeshToVoxelEdgeData EdgeData
Definition: GeometryUtil.h:151
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::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.
openvdb_houdini::GenAdaptivityMaskOp::BoolLeafManager
openvdb::tree::LeafManager< BoolTreeType > BoolLeafManager
Definition: GeometryUtil.h:177
LeafManager.h
A LeafManager manages a linear array of pointers to a given tree's leaf nodes, as well as optional au...
openvdb_houdini::VertexNormalOp::operator()
void operator()(const GA_SplittableRange &) const
openvdb_houdini::PrimCpyOp
TBB body object for threaded primitive copy.
Definition: GeometryUtil.h:107
openvdb_houdini::TransformOp::operator()
void operator()(const GA_SplittableRange &) const
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::SharpenFeaturesOp
TBB body object for threaded sharp feature construction.
Definition: GeometryUtil.h:149
OPENVDB_HOUDINI_API
#define OPENVDB_HOUDINI_API
Definition: Platform.h:229
openvdb.h
openvdb_houdini::PrimCpyOp::PrimCpyOp
PrimCpyOp(GU_Detail const *const gdp, std::vector< openvdb::Vec4I > &primList)