HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_PrimSubdivisionMesh.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: GT_PrimSubdivisionMesh.h ( GT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GT_PrimSubdivisionMesh__
12 #define __GT_PrimSubdivisionMesh__
13 
14 #include "GT_API.h"
15 #include "GT_PrimPolygonMesh.h"
16 #include <UT/UT_Array.h>
17 
18 /// A subdivision mesh primitive
19 ///
20 /// Subdivision meshes can have a list of arbitrary tags. Each tag can have
21 /// integer, real or string values. Tags that are used in refinement:
22 /// - int crease[]@n
23 /// A list of pairs of vertices defining edges
24 /// - real crease[]@n
25 /// The crease weights for each edge
26 /// - int corner[]@n
27 /// A list of corner vertices
28 /// - real corner[]@n
29 /// The crease weights for corner
30 /// - int interpolateboundary@n
31 /// Deprecated: Determines how point attributes are interpolated
32 /// - int facevaryinginterpolateboundary@n
33 /// Deprecated: Determines how vertex attributes are interpolated
34 /// - int osd_vtxboundaryinterpolation @n
35 /// Determines how point attributes are interpolated (values match the enum
36 /// in OpenSubdiv.
37 /// - int osd_fvarlinearinterpolation @n
38 /// Determines how vertex attributes are interpolated (values match the enum
39 /// in OpenSubdiv.
40 /// - int osd_creasingmethod @n
41 /// Determines the creasing rule (values match the enum in OpenSubdiv)
42 /// - int osd_trianglesubdivision @n
43 /// Determines triangle subdivision smoothing for Catmull-Clark subdivision
44 /// (values match the enum in OpenSubdiv)
46 {
47 public:
50  , myTags()
51  , myScheme(GT_CATMULL_CLARK)
52  {
53  }
54 
56  const GT_DataArrayHandle &vtx_indices,
57  const GT_AttributeListHandle &shared,
58  const GT_AttributeListHandle &vertex,
59  const GT_AttributeListHandle &uniform,
60  const GT_AttributeListHandle &detail,
62  GT_Size min_vertex_count=0,
63  GT_Size max_vertex_count=0)
64  : GT_PrimPolygonMesh(vtx_counts, vtx_indices,
65  shared, vertex, uniform, detail, indexing,
66  min_vertex_count, max_vertex_count)
67  , myTags()
68  , myScheme(GT_CATMULL_CLARK)
69  {
70  }
72  const GT_DataArrayHandle &vtx_indices,
73  const GT_AttributeListHandle &shared,
74  const GT_AttributeListHandle &vertex,
75  const GT_AttributeListHandle &uniform,
76  const GT_AttributeListHandle &detail)
77  : GT_PrimPolygonMesh(vtx_counts, vtx_indices,
78  shared, vertex, uniform, detail)
79  , myTags()
80  , myScheme(GT_CATMULL_CLARK)
81  {
82  }
83 
85  const GT_AttributeListHandle &shared,
86  const GT_AttributeListHandle &vertex,
87  const GT_AttributeListHandle &uniform,
88  const GT_AttributeListHandle &detail)
89  : GT_PrimPolygonMesh(mesh, shared, vertex, uniform, detail)
90  , myTags(mesh.myTags)
91  , myScheme(mesh.myScheme)
92  {
93  }
95  const GT_DataArrayHandle &vtx_index,
96  const GT_AttributeListHandle &shared)
97  : GT_PrimPolygonMesh(mesh, vtx_index, shared)
98  , myTags(mesh.myTags)
99  , myScheme(mesh.myScheme)
100  {
101  }
103  : GT_PrimPolygonMesh(mesh)
104  , myTags(mesh.myTags)
105  , myScheme(mesh.myScheme)
106  {
107  }
108  /// Construct from a GT_PrimPolygonMesh
110  : GT_PrimPolygonMesh(mesh)
111  , myTags()
112  , myScheme(scheme)
113  {
114  }
115 
116  /// Given a data array of either a string or integer value, look-up the
117  /// corresponding scheme. This can be used to interpret the "osd_scheme"
118  /// attribute.
119  static GT_Scheme lookupScheme(const GT_DataArrayHandle &scheme,
120  GT_Scheme defscheme = GT_CATMULL_CLARK);
121 
122  virtual const char *className() const { return "GT_PrimSubdivisionMesh"; }
123  virtual bool save(UT_JSONWriter &w) const;
124  virtual int getPrimitiveType() const;
125  virtual bool refine(GT_Refine &refiner,
126  const GT_RefineParms *parms) const;
127  virtual int64 getMemoryUsage() const;
128 
129  void overrideAttributes(
130  const GT_AttributeListHandle &pt_attribs,
131  const GT_AttributeListHandle &vtx_attribs);
132  void clearOverrideAttributes();
133 
135  const UT_StringRef &P = GA_Names::P,
136  bool normalize = true) const;
137 
138  /// Subdivision tag
139  class GT_API Tag
140  {
141  public:
142  Tag()
143  : myName()
144  , myInt()
145  , myReal()
146  , myString()
147  {
148  }
149  Tag(const char *name)
150  : myName(name, true)
151  , myInt()
152  , myReal()
153  , myString()
154  {
155  }
156  Tag(const Tag &src)
157  : myName(src.myName, true)
158  , myInt(src.myInt)
159  , myReal(src.myReal)
160  , myString(src.myString)
161  {
162  }
163  Tag &operator=(const Tag &src)
164  {
165  if (this != &src)
166  {
167  myName.harden(src.myName);
168  myInt = src.myInt;
169  myReal = src.myReal;
170  myString = src.myString;
171  }
172  return *this;
173  }
174 
175  bool match(const char *name) const { return myName == name; }
176  bool valid() const { return myName.isstring(); }
178  {
179  int64 mem = sizeof(*this) + myName.getMemoryUsage();
180  for (exint i = 0; i < myInt.entries(); ++i)
181  mem += myInt(i)->getMemoryUsage();
182  for (exint i = 0; i < myReal.entries(); ++i)
183  mem += myReal(i)->getMemoryUsage();
184  for (exint i = 0; i < myString.entries(); ++i)
185  mem += myString(i)->getMemoryUsage();
186  return mem;
187  }
188 
189  void clear()
190  {
191  myInt.setCapacity(0);
192  myReal.setCapacity(0);
193  myString.setCapacity(0);
194  }
195  const char *name() const { return myName; }
196  exint intCount() const { return myInt.entries(); }
197  exint realCount() const { return myReal.entries(); }
198  exint stringCount() const { return myString.entries(); }
199 
201  { return myInt(i); }
203  { return myReal(i); }
205  { return myString(i); }
206 
207  void harden();
208 
210  { myInt.append(data); }
212  { myReal.append(data); }
214  { myString.append(data); }
215 
217  &getAllInt() const { return myInt; }
219  &getAllReal() const { return myReal; }
221  &getAllString() const { return myString; }
222 
223  private:
224  UT_String myName;
225  UT_Array<GT_DataArrayHandle> myInt; // Integer arrays
226  UT_Array<GT_DataArrayHandle> myReal; // Float arrays
227  UT_Array<GT_DataArrayHandle> myString; // String arrays
228  };
229 
231  {
232  public:
234  : myCurr(0)
235  , myTags(NULL)
236  {
237  }
239  : myCurr(s.myCurr)
240  , myTags(s.myTags)
241  {
242  }
243  bool operator==(const tag_iterator &tag) const
244  {
245  if (atEnd() && tag.atEnd())
246  return true;
247  if (atEnd() || tag.atEnd())
248  return false;
249  return myTags == tag.myTags && myCurr == tag.myCurr;
250  }
251  tag_iterator &operator++() { advance(); return *this; }
253  {
254  myCurr = src.myCurr;
255  myTags = src.myTags;
256  return *this;
257  }
259  { return (*myTags)(myCurr); }
261  { return tag(); }
262  int index() const { return myCurr; }
263 
264  bool atEnd() const
265  {
266  return !myTags || myCurr >= myTags->entries();
267  }
268  void advance() { myCurr++; }
269  void rewind() { myCurr = 0; }
270  private:
272  : myTags(&tags)
273  , myCurr(0)
274  {
275  }
277  int myCurr;
279  };
280 
281  /// Return a polygon mesh of the subdivision hull
283 
284  /// @{
285  /// get/set the subdivision scheme
286  GT_Scheme scheme() const { return myScheme; }
287  void setScheme(GT_Scheme s) { myScheme = s; }
288  /// @}
289 
290  /// @{
291  /// Add tags. If the @c replace tag is true, any existing tag will be
292  /// removed before the tag is added.
293  void appendTag(const Tag &src);
294  void appendIntTag(const char *name, const GT_DataArrayHandle &v,
295  bool replace=false);
296  void appendRealTag(const char *name, const GT_DataArrayHandle &v,
297  bool replace=false);
298  void appendStringTag(const char *name, const GT_DataArrayHandle &v,
299  bool replace=false);
300  /// @}
301  /// Find a tag by name
302  const Tag *findTag(const char *name) const
303  {
304  for (int i = 0; i < myTags.entries(); ++i)
305  {
306  if (myTags(i).match(name))
307  return &myTags(i);
308  }
309  return NULL;
310  }
311  /// Delete a tag
312  bool delTag(const char *name)
313  {
314  for (int i = 0; i < myTags.entries(); ++i)
315  {
316  if (myTags(i).match(name))
317  {
318  myTags.removeIndex(i);
319  return true;
320  }
321  }
322  return false;
323  }
324  /// Clear all tags
325  void clearTags()
326  {
327  myTags.setCapacity(0);
328  }
329 
330  /// @{
331  /// Tag iteration
332  tag_iterator beginTags() const { return tag_iterator(myTags); }
333  tag_iterator endTag() const { return tag_iterator(); }
334  /// @}
335 
336  /// Triangulate (for Loop subdivision)
338 
339 
340  /// Harden all attributes so there are no dangling dependencies
341  virtual GT_PrimitiveHandle doHarden() const;
343  { return new GT_PrimSubdivisionMesh(*this); }
344 
345  /// The virtual implementation of attribute merging
347  const UT_StringMMPattern *vertex,
348  const UT_StringMMPattern *point,
349  const UT_StringMMPattern *uniform,
350  const UT_StringMMPattern *detail) const;
351 protected:
353  {
354  smesh->myTags = myTags;
355  smesh->myScheme = myScheme;
356  return smesh;
357  }
358  virtual GT_PrimPolygonMesh *clone(const GT_DataArrayHandle &vtx_counts,
359  const GT_DataArrayHandle &vtx_indices,
360  const GT_AttributeListHandle &shared,
361  const GT_AttributeListHandle &vertex,
362  const GT_AttributeListHandle &uniform,
363  const GT_AttributeListHandle &detail,
365  GT_Size min_vertex_count=0,
366  GT_Size max_vertex_count=0) const
367  {
368  auto smesh = new GT_PrimSubdivisionMesh(vtx_counts, vtx_indices,
369  shared, vertex, uniform, detail, indexing,
370  min_vertex_count, max_vertex_count);
371  return copySubd(smesh);
372  }
373  virtual GT_PrimPolygonMesh *clone(const GT_CountArray &vtx_counts,
374  const GT_DataArrayHandle &vtx_indices,
375  const GT_AttributeListHandle &shared,
376  const GT_AttributeListHandle &vertex,
377  const GT_AttributeListHandle &uniform,
378  const GT_AttributeListHandle &detail) const
379  {
380  auto smesh = new GT_PrimSubdivisionMesh(vtx_counts, vtx_indices,
381  shared, vertex, uniform, detail);
382  return copySubd(smesh);
383  }
385  const GT_AttributeListHandle &vertex,
386  const GT_AttributeListHandle &uniform,
387  const GT_AttributeListHandle &detail) const
388  {
389  return new GT_PrimSubdivisionMesh(*this, shared, vertex, uniform, detail);
390  }
391  virtual GT_PrimPolygonMesh *clone(const GT_DataArrayHandle &vtx_indices,
392  const GT_AttributeListHandle &shared) const
393  {
394  return new GT_PrimSubdivisionMesh(*this, vtx_indices, shared);
395  }
396 
397 private:
398  void hardenTags();
399  Tag *findOrCreateTag(const char *name, bool replace)
400  {
401  for (int i = 0; i < myTags.entries(); ++i)
402  {
403  if (myTags(i).match(name))
404  {
405  if (replace)
406  myTags(i).clear();
407  return &myTags(i);
408  }
409  }
410  myTags.append(Tag(name));
411  return &myTags(myTags.entries()-1);
412  }
413 
414  UT_Array<Tag> myTags;
415  GT_Scheme myScheme;
416  GT_AttributeListHandle myStashedPoint;
417  GT_AttributeListHandle myStashedVertex;
418 };
419 
420 #endif
421 
tag_iterator endTag() const
A mesh of polygons.
const Tag * findTag(const char *name) const
GT_PrimitiveHandle triangulate() const
Triangulate (for Loop subdivision)
const GLdouble * v
Definition: glcorearb.h:836
void appendString(const GT_DataArrayHandle &data)
void appendRealTag(const char *name, const GT_DataArrayHandle &v, bool replace=false)
const UT_Array< GT_DataArrayHandle > & getAllReal() const
GT_IndexingMode
Definition: GT_Types.h:91
void appendStringTag(const char *name, const GT_DataArrayHandle &v, bool replace=false)
#define GT_API
Definition: GT_API.h:11
bool delTag(const char *name)
Delete a tag.
GT_PrimSubdivisionMesh(const GT_PrimSubdivisionMesh &mesh, const GT_DataArrayHandle &vtx_index, const GT_AttributeListHandle &shared)
virtual GT_PrimitiveHandle doAttributeMerge(const GT_Primitive &src, const UT_StringMMPattern *vertex, const UT_StringMMPattern *point, const UT_StringMMPattern *uniform, const UT_StringMMPattern *detail) const
The virtual implementation of attribute merging.
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
GA_API const UT_StringHolder P
tag_iterator beginTags() const
virtual int getPrimitiveType() const
virtual GT_PrimPolygonMesh * clone(const GT_AttributeListHandle &shared, const GT_AttributeListHandle &vertex, const GT_AttributeListHandle &uniform, const GT_AttributeListHandle &detail) const
png_uint_32 i
Definition: png.h:2877
bool operator==(const tag_iterator &tag) const
virtual GT_PrimPolygonMesh * clone(const GT_DataArrayHandle &vtx_indices, const GT_AttributeListHandle &shared) const
void appendTag(const Tag &src)
long long int64
Definition: SYS_Types.h:107
virtual GT_PrimPolygonMesh * clone(const GT_DataArrayHandle &vtx_counts, const GT_DataArrayHandle &vtx_indices, const GT_AttributeListHandle &shared, const GT_AttributeListHandle &vertex, const GT_AttributeListHandle &uniform, const GT_AttributeListHandle &detail, GT_IndexingMode indexing=GT_INDEXING_QUICK, GT_Size min_vertex_count=0, GT_Size max_vertex_count=0) const
GT_PrimSubdivisionMesh(const GT_PrimSubdivisionMesh &mesh, const GT_AttributeListHandle &shared, const GT_AttributeListHandle &vertex, const GT_AttributeListHandle &uniform, const GT_AttributeListHandle &detail)
virtual GT_PrimitiveHandle doSoftCopy() const
GT_Scheme
Subdivision schemes.
Definition: GT_Types.h:68
virtual bool refine(GT_Refine &refiner, const GT_RefineParms *parms) const
int64 exint
Definition: SYS_Types.h:116
const GT_DataArrayHandle & realArray(exint i=0) const
void clearTags()
Clear all tags.
void appendInt(const GT_DataArrayHandle &data)
GT_PrimSubdivisionMesh(const GT_PrimSubdivisionMesh &mesh)
virtual GT_PrimitiveHandle doHarden() const
Harden all attributes so there are no dangling dependencies.
const GT_PrimSubdivisionMesh::Tag & operator*() const
GT_PrimSubdivisionMesh(const GT_PrimPolygonMesh &mesh, GT_Scheme scheme)
Construct from a GT_PrimPolygonMesh.
GLboolean * data
Definition: glcorearb.h:130
GLuint const GLchar * name
Definition: glcorearb.h:785
GridType::Ptr normalize(const GridType &grid, bool threaded, InterruptT *interrupt)
Normalize the vectors of the given vector-valued grid.
const UT_Array< GT_DataArrayHandle > & getAllInt() const
The base class for all GT primitive types.
Definition: GT_Primitive.h:43
void appendIntTag(const char *name, const GT_DataArrayHandle &v, bool replace=false)
Processes primitives generated by refinement process.
Definition: GT_Refine.h:20
GT_PrimitiveHandle refineToHull() const
Return a polygon mesh of the subdivision hull.
virtual const char * className() const
bool match(const char *name) const
const UT_Array< GT_DataArrayHandle > & getAllString() const
GT_PrimSubdivisionMesh * copySubd(GT_PrimSubdivisionMesh *smesh) const
const GT_DataArrayHandle & intArray(exint i=0) const
virtual GT_PrimPolygonMesh * clone(const GT_CountArray &vtx_counts, const GT_DataArrayHandle &vtx_indices, const GT_AttributeListHandle &shared, const GT_AttributeListHandle &vertex, const GT_AttributeListHandle &uniform, const GT_AttributeListHandle &detail) const
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
virtual int64 getMemoryUsage() const
const GT_DataArrayHandle & stringArray(exint i=0) const
const GT_PrimSubdivisionMesh::Tag & tag() const
GT_PrimPolygonMesh * createPointNormalsIfMissing(int segment=0, const UT_StringRef &P=GA_Names::P, bool normalize=true) const
Create point normals on a new mesh if no normals are found. If no point or vertex normals are found...
virtual bool save(UT_JSONWriter &w) const
int64 GT_Size
Definition: GT_Types.h:112
GT_PrimSubdivisionMesh(const GT_DataArrayHandle &vtx_counts, const GT_DataArrayHandle &vtx_indices, const GT_AttributeListHandle &shared, const GT_AttributeListHandle &vertex, const GT_AttributeListHandle &uniform, const GT_AttributeListHandle &detail, GT_IndexingMode indexing=GT_INDEXING_QUICK, GT_Size min_vertex_count=0, GT_Size max_vertex_count=0)
tag_iterator & operator=(const tag_iterator &src)
void appendReal(const GT_DataArrayHandle &data)
GT_PrimSubdivisionMesh(const GT_CountArray &vtx_counts, const GT_DataArrayHandle &vtx_indices, const GT_AttributeListHandle &shared, const GT_AttributeListHandle &vertex, const GT_AttributeListHandle &uniform, const GT_AttributeListHandle &detail)
GLenum src
Definition: glcorearb.h:1792