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 HD_MESH_UTIL_H
25 #define 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/base/gf/vec2i.h"
34 #include "pxr/usd/sdf/path.h"
35 #include "pxr/base/vt/array.h"
36 #include "pxr/base/vt/value.h"
37 
38 #include <unordered_map>
39 #include <utility> // std::{min,max}, std::pair
40 #include <boost/functional/hash.hpp> // boost::hash_combine
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  VtVec3iArray *trianglesEdgeIndices = nullptr);
113 
119  HD_API
120  bool ComputeTriangulatedFaceVaryingPrimvar(void const* source,
121  int numElements,
122  HdType dataType,
123  VtValue *triangulated);
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 
145  HD_API
146  static int ComputeNumQuads(VtIntArray const &numVerts,
147  VtIntArray const &holeIndices,
148  bool *invalidFaceFound=NULL);
149 
150 
152  HD_API
153  void ComputeQuadInfo(HdQuadInfo* quadInfo);
154 
157  HD_API
158  void ComputeQuadIndices(VtVec4iArray *indices,
159  VtVec2iArray *primitiveParams,
160  VtVec4iArray *quadsEdgeIndices = nullptr);
161 
167  HD_API
169  void const* source,
170  int numElements,
171  HdType dataType,
172  VtValue *quadrangulated);
173 
179  HD_API
180  bool ComputeQuadrangulatedFaceVaryingPrimvar(void const* source,
181  int numElements,
182  HdType dataType,
183  VtValue *quadrangulated);
184 
185  // --------------------------------------------------------------------
186  // Primitive param bit encoding
187 
188  // Per-primitive coarse-face-param encoding/decoding functions
189  static int EncodeCoarseFaceParam(int faceIndex, int edgeFlag) {
190  return ((faceIndex << 2) | (edgeFlag & 3));
191  }
192  static int DecodeFaceIndexFromCoarseFaceParam(int coarseFaceParam) {
193  return (coarseFaceParam >> 2);
194  }
195  static int DecodeEdgeFlagFromCoarseFaceParam(int coarseFaceParam) {
196  return (coarseFaceParam & 3);
197  }
198 
199  // --------------------------------------------------------------------
200  // Authored edge id computation
201  struct EdgeHash {
202  // Use a custom hash so that edges (a,b) and (b,a) are equivalent
203  inline size_t operator()(GfVec2i const& v) const {
204  size_t hash = 0;
205  boost::hash_combine(hash, std::min(v[0], v[1]));
206  boost::hash_combine(hash, std::max(v[0], v[1]));
207  return hash;
208  }
209  };
210 
211  struct EdgeEquality {
212  inline bool operator() (GfVec2i const& v1, GfVec2i const& v2) const
213  {
214  if (EdgeHash()(v1) == EdgeHash()(v2))
215  return true;
216 
217  return false;
218  }
219  };
220 
221  typedef std::unordered_map<GfVec2i, int, EdgeHash, EdgeEquality> EdgeMap;
222 
223  // Enumerates all the edges of the authored mesh topology, and returns a map
224  // of (vertex indices pair, edge id).
225  // If skipHoles is true, unshared edges of hole faces aren't enumerated.
226  HD_API
227  static EdgeMap ComputeAuthoredEdgeMap(HdMeshTopology const* topology,
228  bool skipHoles = false);
229 
230  // Translates an authored edge id to its vertex indices
231  // Returns a pair, with first indicating success of the look up, and
232  // second being the vertex indices for the edge.
233  HD_API
234  static std::pair<bool, GfVec2i>
235  GetVertexIndicesForEdge(HdMeshTopology const* topology,
236  int authoredEdgeId);
237 
238  // Translates an edge to its authored edge id
239  // Returns a pair, with first indicating success of the look up, and
240  // second being the authored edge id
241  HD_API
242  static std::pair<bool, int>
243  GetAuthoredEdgeID(HdMeshTopology const* topology,
244  GfVec2i edge);
245 private:
246  HdMeshTopology const* _topology;
247  SdfPath const _id;
248 };
249 
250 PXR_NAMESPACE_CLOSE_SCOPE
251 
252 #endif // HD_MESH_UTIL_H
bool IsAllQuads() const
Returns true if the mesh is all-quads.
Definition: meshUtil.h:69
HD_API void ComputeQuadInfo(HdQuadInfo *quadInfo)
Generate a quadInfo struct for the input topology.
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
static HD_API int ComputeNumQuads(VtIntArray const &numVerts, VtIntArray const &holeIndices, bool *invalidFaceFound=NULL)
Return the number of quadrangulated quads.
A helper class for quadrangulation computation.
Definition: meshUtil.h:65
HD_API bool ComputeTriangulatedFaceVaryingPrimvar(void const *source, int numElements, HdType dataType, VtValue *triangulated)
Return a triangulation of a face-varying primvar.
HD_API void ComputeTriangleIndices(VtVec3iArray *indices, VtIntArray *primitiveParams, VtVec3iArray *trianglesEdgeIndices=nullptr)
Return a triangulation of the input topology.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
HD_API bool ComputeQuadrangulatedPrimvar(HdQuadInfo const *qi, void const *source, int numElements, HdType dataType, VtValue *quadrangulated)
Return a quadrangulation of a per-vertex primvar.
HD_API void ComputeQuadIndices(VtVec4iArray *indices, VtVec2iArray *primitiveParams, VtVec4iArray *quadsEdgeIndices=nullptr)
Return quadrangulated indices of the input topology.
Topology data for meshes.
Definition: meshTopology.h:55
HD_API bool ComputeQuadrangulatedFaceVaryingPrimvar(void const *source, int numElements, HdType dataType, VtValue *quadrangulated)
Return a quadrangulation of a face-varying primvar.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
A collection of utility algorithms for generating triangulation and quadrangulation of an input topol...
Definition: meshUtil.h:82