All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
meshUtil.h
1 //
2 // Copyright 2017 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_MESH_UTIL_H
25 #define PXR_IMAGING_HD_MESH_UTIL_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/imaging/hd/types.h"
31 #include "pxr/imaging/hd/meshTopology.h"
32 
33 #include "pxr/usd/sdf/path.h"
34 
35 #include "pxr/base/gf/vec2i.h"
36 #include "pxr/base/gf/vec3i.h"
37 #include "pxr/base/gf/vec4i.h"
38 
39 #include "pxr/base/vt/array.h"
40 #include "pxr/base/vt/value.h"
41 
42 PXR_NAMESPACE_OPEN_SCOPE
43 
46 
47 // v0 v2
48 // +-----e2----+
49 // \ | /
50 // \ __c__ /
51 // e0 e1
52 // \ /
53 // \ /
54 // + v1
55 //
56 //
57 // original points additional center and edge points
58 // +------------ ... ----+--------------------------------+
59 // | v0 v1 v2 vn | e0 e1 e2 c0, e3 e4 e5 c1 ... |
60 // +------------ ... ----+--------------------------------+
61 // ^
62 // pointsOffset
63 // <----- numAdditionalPoints ---->
64 
65 struct HdQuadInfo {
66  HdQuadInfo() : pointsOffset(0), numAdditionalPoints(0), maxNumVert(0) { }
67 
69  bool IsAllQuads() const { return numAdditionalPoints == 0; }
70 
71  int pointsOffset;
72  int numAdditionalPoints;
73  int maxNumVert;
74  std::vector<int> numVerts; // num vertices of non-quads
75  std::vector<int> verts; // vertex indices of non-quads
76 };
77 
81 
83 {
84 public:
85  HdMeshUtil(HdMeshTopology const* topology, SdfPath const& id)
86  : _topology(topology), _id(id) {}
87  virtual ~HdMeshUtil() {}
88 
89  // --------------------------------------------------------------------
90  // Triangulation
91 
92  // In order to access per-face signals (face color, face selection etc)
93  // we need a mapping from primitiveID to authored face index domain.
94  // This is stored in primitiveParams, and computed along with indices.
95  /*
96  +--------+-------+
97  /| \ |\ |\
98  / | \ 1 | \ 2 | \
99  / | \ | \ | \
100  / | \ | \ | 2 +
101  / 0 | 1 \ | 2 \ | /
102  / | \ | \ | /
103  / | \| \|/
104  +-------+--------+-------+
105  */
106 
109  HD_API
110  void ComputeTriangleIndices(VtVec3iArray *indices,
111  VtIntArray *primitiveParams,
112  VtIntArray *edgeIndices = nullptr) const;
113 
119  HD_API
120  bool ComputeTriangulatedFaceVaryingPrimvar(void const* source,
121  int numElements,
122  HdType dataType,
123  VtValue *triangulated) const;
124 
125  // --------------------------------------------------------------------
126  // Quadrangulation
127 
128  // In order to access per-face signals (face color, face selection etc)
129  // we need a mapping from primitiveID to authored face index domain.
130  // This is stored in primitiveParams, and computed along with indices.
131  /*
132  +--------+-------+
133  /| | | \
134  / | | 2 | 2 /\
135  / | | \ / \
136  / 0 | 1 |------+ 2 +
137  /\ /| | / \ /
138  / \/ | | 2 | 2 \/
139  / 0 | 0| | | /
140  +-------+--------+-------+
141  */
142 
144  HD_API
145  void ComputeQuadInfo(HdQuadInfo* quadInfo) const;
146 
149  HD_API
150  void ComputeQuadIndices(VtVec4iArray *indices,
151  VtIntArray *primitiveParams,
152  VtVec2iArray *edgeIndices = nullptr) const;
153 
159  HD_API
161  void const* source,
162  int numElements,
163  HdType dataType,
164  VtValue *quadrangulated) const;
165 
171  HD_API
172  bool ComputeQuadrangulatedFaceVaryingPrimvar(void const* source,
173  int numElements,
174  HdType dataType,
175  VtValue *quadrangulated) const;
176 
184  HD_API
185  void EnumerateEdges(std::vector<GfVec2i> * edgeVerticesOut) const;
186 
187  // --------------------------------------------------------------------
188  // Primitive param bit encoding
189 
190  // Per-primitive coarse-face-param encoding/decoding functions
191  static int EncodeCoarseFaceParam(int faceIndex, int edgeFlag) {
192  return ((faceIndex << 2) | (edgeFlag & 3));
193  }
194  static int DecodeFaceIndexFromCoarseFaceParam(int coarseFaceParam) {
195  return (coarseFaceParam >> 2);
196  }
197  static int DecodeEdgeFlagFromCoarseFaceParam(int coarseFaceParam) {
198  return (coarseFaceParam & 3);
199  }
200 
201 private:
204  int _ComputeNumQuads(VtIntArray const &numVerts,
205  VtIntArray const &holeIndices,
206  bool *invalidFaceFound = nullptr) const;
207 
208  HdMeshTopology const* _topology;
209  SdfPath const _id;
210 };
211 
258 class HdMeshEdgeIndexTable
259 {
260 public:
261  explicit HdMeshEdgeIndexTable(HdMeshTopology const * topology);
262  ~HdMeshEdgeIndexTable();
263 
264  bool GetVerticesForEdgeIndex(int edgeId, GfVec2i * edgeVerticesOut) const;
265 
266  bool GetVerticesForEdgeIndices(
267  std::vector<int> const & edgeIndices,
268  std::vector<GfVec2i> * edgeVerticesOut) const;
269 
270  bool GetEdgeIndices(GfVec2i const & edgeVertices,
271  std::vector<int> * edgeIndicesOut) const;
272 
273 private:
274  struct _Edge{
275  _Edge(GfVec2i const & verts_ = GfVec2i(-1), int index_ = -1)
276  : verts(verts_)
277  , index(index_)
278  {
279  // Simplify sorting and searching by keeping the vertices ordered.
280  if (verts[0] > verts[1]) {
281  std::swap(verts[0], verts[1]);
282  }
283  }
284  GfVec2i verts;
285  int index;
286 
287  };
288 
289  struct _CompareEdgeVertices {
290  bool operator() (_Edge const &lhs, _Edge const & rhs) const {
291  return (lhs.verts[0] < rhs.verts[0] ||
292  (lhs.verts[0] == rhs.verts[0] &&
293  lhs.verts[1] < rhs.verts[1]));
294  }
295  };
296 
297  struct _EdgeVerticesHash {
298  // Use a custom hash so that edges (a,b) and (b,a) are equivalent
299  inline size_t operator()(GfVec2i const& v) const {
300  // Triangular numbers for 2-d hash.
301  int theMin = v[0], theMax = v[1];
302  if (theMin > theMax) {
303  std::swap(theMin, theMax);
304  }
305  size_t x = theMin;
306  size_t y = x + theMax;
307  return x + (y * (y + 1)) / 2;
308  }
309  };
310 
311  std::vector<GfVec2i> _edgeVertices;
312  std::vector<_Edge> _edgesByIndex;
313 };
314 
315 
316 PXR_NAMESPACE_CLOSE_SCOPE
317 
318 #endif // PXR_IMAGING_HD_MESH_UTIL_H
bool IsAllQuads() const
Returns true if the mesh is all-quads.
Definition: meshUtil.h:69
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
HD_API void EnumerateEdges(std::vector< GfVec2i > *edgeVerticesOut) const
Return a buffer filled with face vertex index pairs corresponding to the sequence in which edges are ...
A helper class for quadrangulation computation.
Definition: meshUtil.h:65
HD_API void ComputeQuadIndices(VtVec4iArray *indices, VtIntArray *primitiveParams, VtVec2iArray *edgeIndices=nullptr) const
Return quadrangulated indices of the input topology.
HD_API bool ComputeQuadrangulatedPrimvar(HdQuadInfo const *qi, void const *source, int numElements, HdType dataType, VtValue *quadrangulated) const
Return a quadrangulation of a per-vertex primvar.
HD_API void ComputeQuadInfo(HdQuadInfo *quadInfo) const
Generate a quadInfo struct for the input topology.
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
HD_API bool ComputeQuadrangulatedFaceVaryingPrimvar(void const *source, int numElements, HdType dataType, VtValue *quadrangulated) const
Return a quadrangulation of a face-varying primvar.
HD_API bool ComputeTriangulatedFaceVaryingPrimvar(void const *source, int numElements, HdType dataType, VtValue *triangulated) const
Return a triangulation of a face-varying primvar.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
HD_API void ComputeTriangleIndices(VtVec3iArray *indices, VtIntArray *primitiveParams, VtIntArray *edgeIndices=nullptr) const
Return a triangulation of the input topology.
Topology data for meshes.
Definition: meshTopology.h:55
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:168
A collection of utility algorithms for generating triangulation and quadrangulation of an input topol...
Definition: meshUtil.h:82