HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
meshTopology.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_PX_OSD_MESH_TOPOLOGY_H
8 #define PXR_IMAGING_PX_OSD_MESH_TOPOLOGY_H
9 
10 /// \file pxOsd/meshTopology.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/imaging/pxOsd/api.h"
16 
17 #include "pxr/base/vt/array.h"
18 #include "pxr/base/vt/value.h"
19 
20 #include "pxr/base/tf/token.h"
21 
23 
24 /// \class PxOsdMeshTopology
25 ///
26 /// Topology data for meshes.
27 ///
28 /// Once constructed, this class is immutable (except when assigned
29 /// or moved).
30 ///
31 /// To make changing certain properties easier, several methods are
32 /// provided. WithScheme, WithOrientation, WithHoleIndices, and WithSubdivTags
33 /// will return copies of the object with certain specific properites changed.
34 ///
35 /// \code{.cpp}
36 /// PxOsdMeshTopology otherTopology =
37 /// originalTopology.WithScheme(PxOsdOpenSubdivTokens->catmullClark);
38 /// TF_VERIFY(otherTopology.GetScheme() ==
39 /// PxOsdOpenSubdivTokens->catmullClark);
40 /// TF_VERIFY(otherTopology.GetOrientation() ==
41 /// originalTopology.GetOrientation());
42 /// TF_VERIFY(otherTopology.GetSubdivTags() ==
43 /// originalTopology.GetSubdivTags());
44 /// TF_VERIFY(otherTopology.GetFaceVertexCounts() ==
45 /// originalTopology.GetFaceVertexCounts());
46 /// TF_VERIFY(otherTopology.GetFaceVertexIndices() ==
47 /// originalTopology.GetFaceVertexIndices());
48 /// \endcode
49 ///
50 /// The cost of copying should be mitigated by the copy semantics of VtArray and
51 /// TfToken.
53 
54 public:
55 
56  typedef uint64_t ID;
57 
58  PXOSD_API
60 
61  PxOsdMeshTopology& operator=(const PxOsdMeshTopology &) = default;
62  PxOsdMeshTopology(const PxOsdMeshTopology &) = default;
65  ~PxOsdMeshTopology() = default;
66 
67  /// Construct a topology without holes or subdiv tags
68  PXOSD_API
70  TfToken const& scheme,
71  TfToken const& orientation,
72  VtIntArray const& faceVertexCounts,
73  VtIntArray const& faceVertexIndices);
74 
75  /// Construct a topology with holes
76  PXOSD_API
78  TfToken const& scheme,
79  TfToken const& orientation,
80  VtIntArray const& faceVertexCounts,
81  VtIntArray const& faceVertexIndices,
82  VtIntArray const& holeIndices);
83 
84  /// Construct a topology with holes and subdiv tags
85  PXOSD_API
87  TfToken const& scheme,
88  TfToken const& orientation,
89  VtIntArray const& faceVertexCounts,
90  VtIntArray const& faceVertexIndices,
91  VtIntArray const& holeIndices,
92  PxOsdSubdivTags const& subdivTags);
93 
94  /// Construct a topology with subdiv tags
95  PXOSD_API
97  TfToken const& scheme,
98  TfToken const& orientation,
99  VtIntArray const& faceVertexCounts,
100  VtIntArray const& faceVertexIndices,
101  PxOsdSubdivTags const& subdivTags);
102 
103 public:
104 
105  /// Returns the subdivision scheme
106  TfToken const GetScheme() const {
107  return _scheme;
108  }
109 
110  /// Returns face vertex counts.
111  VtIntArray const &GetFaceVertexCounts() const {
112  return _faceVertexCounts;
113  }
114 
115  /// Returns face vertex indices.
116  VtIntArray const &GetFaceVertexIndices() const {
117  return _faceVertexIndices;
118  }
119 
120  /// Returns orientation.
121  TfToken const &GetOrientation() const {
122  return _orientation;
123  }
124 
125 
126  ///
127  /// \name Holes
128  /// @{
129  /// Returns the hole face indices.
130  VtIntArray const &GetHoleIndices() const {
131  return _holeIndices;
132  }
133 
134  /// @}
135 
136  ///
137  /// \name Tags
138  /// @{
139 
140  /// Returns subdivision tags
141  PxOsdSubdivTags const & GetSubdivTags() const {
142  return _subdivTags;
143  }
144 
145  /// @}
146 
147  /// Return a copy of the topology, changing only the scheme.
148  /// Valid values include: catmullClark, loop, bilinear.
149  ///
150  /// Note that the token "catmark" is also supported for backward
151  /// compatibility, but has been deprecated.
153  return PxOsdMeshTopology(scheme, GetOrientation(),
156  }
157 
158  /// Return a copy of the topology, changing only the orientation.
160  return PxOsdMeshTopology(GetScheme(), orient,
163  }
164 
165  /// Return a copy of the topology, changing only the subdiv tags.
169  GetHoleIndices(), tags);
170  }
171 
172  /// Return a copy of the topology, changing only the hole indices.
173  PXOSD_API PxOsdMeshTopology WithHoleIndices(VtIntArray const& holeIndices) const {
176  holeIndices, GetSubdivTags());
177  }
178 public:
179 
180  /// Returns the hash value of this topology to be used for instancing.
181  PXOSD_API
182  ID ComputeHash() const;
183 
184  /// Equality check between two mesh topologies.
185  PXOSD_API
186  bool operator==(PxOsdMeshTopology const &other) const;
187 
188  /// Returns a validation object which is empty if the topology is valid
189  ///
190  /// \code{.cpp}
191  /// // Validation with minimal reporting
192  /// if (!topology.Validate()) TF_CODING_ERROR("Invalid topology.");
193  /// \endcode
194  ///
195  /// \code{.cpp}
196  /// {
197  /// PxOsdMeshTopologyValidation validation = topology.Validate();
198  /// if (!validation){
199  /// for (auto const& elem: validation){
200  /// TF_WARN(elem.message);
201  /// }
202  /// }
203  /// }
204  /// \endcode
205  ///
206  /// \note Internally caches the result of the validation if the topology is
207  /// valid
208  PXOSD_API
210 
211 private:
212 
213  // note: if you're going to add more members, make sure
214  // ComputeHash will be updated too.
215 
216  TfToken _scheme,
217  _orientation;
218 
219  VtIntArray _faceVertexCounts;
220  VtIntArray _faceVertexIndices;
221  VtIntArray _holeIndices;
222 
223  PxOsdSubdivTags _subdivTags;
224 
225  struct _Validated {
226  std::atomic<bool> value;
227 
228  _Validated() : value(false) {}
229  _Validated(const _Validated& other) : value(other.value.load()) {}
230  _Validated(_Validated&& other) : value(other.value.load()) {
231  other.value = false;
232  }
233  _Validated& operator=(const _Validated& other) {
234  value.store(other.value.load());
235  return *this;
236  }
237  _Validated& operator=(_Validated&& other) {
238  value.store(other.value.load());
239  other.value = false;
240  return *this;
241  }
242  };
243 
244  // This should NOT be included in the hash
245  // This evaluates to true if the topology has been successfully
246  // pre-validated. If this is false, the topology is either invalid
247  // or it hasn't been validated yet.
248  mutable _Validated _validated;
249 };
250 
251 PXOSD_API
252 std::ostream& operator << (std::ostream &out, PxOsdMeshTopology const &);
253 PXOSD_API
254 bool operator!=(const PxOsdMeshTopology& lhs, const PxOsdMeshTopology& rhs);
255 
256 
258 
259 #endif // PXR_IMAGING_PX_OSD_MESH_TOPOLOGY_H
PXOSD_API PxOsdMeshTopology WithSubdivTags(PxOsdSubdivTags const &tags) const
Return a copy of the topology, changing only the subdiv tags.
Definition: meshTopology.h:166
PXOSD_API PxOsdMeshTopologyValidation Validate() const
TfToken const GetScheme() const
Returns the subdivision scheme.
Definition: meshTopology.h:106
GLsizei const GLfloat * value
Definition: glcorearb.h:824
VtIntArray const & GetFaceVertexCounts() const
Returns face vertex counts.
Definition: meshTopology.h:111
TfToken const & GetOrientation() const
Returns orientation.
Definition: meshTopology.h:121
PXOSD_API PxOsdMeshTopology WithOrientation(TfToken const &orient) const
Return a copy of the topology, changing only the orientation.
Definition: meshTopology.h:159
Definition: token.h:70
#define PXOSD_API
Definition: api.h:23
~PxOsdMeshTopology()=default
PXOSD_API std::ostream & operator<<(std::ostream &out, PxOsdMeshTopology const &)
PXOSD_API bool operator==(PxOsdMeshTopology const &other) const
Equality check between two mesh topologies.
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:556
VtIntArray const & GetFaceVertexIndices() const
Returns face vertex indices.
Definition: meshTopology.h:116
VtIntArray const & GetHoleIndices() const
Definition: meshTopology.h:130
GA_API const UT_StringHolder orient
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
PXOSD_API PxOsdMeshTopology WithHoleIndices(VtIntArray const &holeIndices) const
Return a copy of the topology, changing only the hole indices.
Definition: meshTopology.h:173
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
PXOSD_API PxOsdMeshTopology WithScheme(TfToken const &scheme) const
Definition: meshTopology.h:152
PxOsdMeshTopology & operator=(const PxOsdMeshTopology &)=default
PXOSD_API PxOsdMeshTopology()
PxOsdSubdivTags const & GetSubdivTags() const
Returns subdivision tags.
Definition: meshTopology.h:141
PXOSD_API ID ComputeHash() const
Returns the hash value of this topology to be used for instancing.