HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_AttributeList.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_AttributeList.h ( GT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GT_AttributeList__
12 #define __GT_AttributeList__
13 
14 #include "GT_API.h"
15 #include "GT_AttributeMap.h"
16 #include "GT_DataArray.h"
17 #include "GT_Handles.h"
18 #include "GT_Types.h"
19 
20 #include <UT/UT_Assert.h>
21 #include <UT/UT_IntrusivePtr.h>
22 #include <UT/UT_StringHolder.h>
23 #include <SYS/SYS_Inline.h>
24 
25 #include <utility>
26 
27 
28 class GT_DataArray;
29 class GT_AttributeMerge;
30 class UT_JSONWriter;
31 class GU_Detail;
32 
34  : public UT_IntrusiveRefCounter<GT_AttributeList>
35 {
36 public:
37  /// An attribute list stores a list of GT_DataArray values for each
38  /// attribute defined in the map. @c motion_segments specifies the number
39  /// of different motion segments.
40  GT_AttributeList(const GT_AttributeMapHandle &map, int motion_segments=1);
43 
44  /// Create an attribute list using varying number of
45  /// (const UT_StringHolder &name, const GT_DataArrayHandle &data) pairs.
46  /// For example: @code
47  /// GT_AttributeList::createAttributeList(
48  /// "P", new GT_Real32Array(...),
49  /// "N", new GT_Real32Array(...),
50  /// "uv", new GT_Real32Array(...));
51  /// @endcode
52  template <typename... Pairs>
54  {
55  int n = gt_CountAttribs(std::forward<Pairs>(pairs)...);
57  list(new GT_AttributeList(n, new GT_AttributeMap(n), 1));
58  gt_CreateAttributeList(*list, *list->getMap(),
59  std::forward<Pairs>(pairs)...);
60  return list;
61  }
62 
63  /// Test whether two attribute lists match in their definition. If both
64  /// attribute lists are empty, this will count as a match.
65  static bool matchDefinition(const GT_AttributeListHandle &a,
66  const GT_AttributeListHandle &b);
67 
68 
69  /// Create a new attribute list, adding a new attribute in the process.
70  GT_AttributeListHandle addAttribute(const UT_StringHolder &name,
71  const GT_DataArrayHandle *handles,
72  int num_segments,
73  bool replace_existing) const;
75  const GT_DataArrayHandle &h,
76  bool replace_existing) const
77  {
78  return addAttribute(name, &h, 1, replace_existing);
79  }
80 
81  /// Remove an attribute from this list, returning a new attribute list.
82  GT_AttributeListHandle removeAttribute(const UT_StringRef &name) const;
83 
84  /// Remove multiple attributes from this, returning a new attribute list.
85  GT_AttributeListHandle removeAttributes(const UT_StringArray &name) const;
86 
87 
88  /// Harden all the attribute lists
89  void harden();
90 
91  /// Harden a single attribute
92  void harden(int idx);
93 
94  /// Get a handle to the name map for this list.
95  const GT_AttributeMapHandle &getMap() const { return myMap; }
96 
97  /// Return the number of attributes in the list.
98  int entries() const
99  { return myMap->entries(); }
100 
101  /// Return the approximate memory usage. Because data can be shared, this
102  /// may not reflect the usage accurately.
103  int64 getMemoryUsage() const;
104 
105  /// Return the number of motion segements
106  int getSegments() const
107  { return myMotionSegments; }
108 
109  /// Return the list of names
110  const UT_StringArray &getNames() const
111  { return myMap->getNames(); }
112 
113  /// Return the data for the named array
115  int segment=0) const
116  {
117  return get(myMap->get(name), segment);
118  }
119  /// Return the data for a given index
120  const GT_DataArrayHandle &get(int index, int motion_segment=0) const;
121 
122  /// Return the name for a given index
123  const UT_StringHolder &getName(int index) const
124  { return myMap->getName(index); }
125 
126  /// Return the export name for a given index.
128  { return myMap->getExportName(index); }
129 
130  /// Get original attribute owner for the given attribute.
131  GT_Owner getOriginalOwner(int idx) const
132  { return myMap->getOriginalOwner(idx); }
133 
134  /// Return the index for a given name (returns -1 if miss)
135  int getIndex(const UT_StringRef &name) const
136  { return myMap->get(name); }
137 
138  /// Test of an attribute name exists
139  bool hasName(const UT_StringRef &name) const
140  { return myMap->hasName(name); }
141 
142  /// Assign an array to a given index in the list.
143  void set(int idx, const GT_DataArrayHandle &h,
144  int motion_segment=0);
145 
146  /// Assign an array to a given index in the list for @b all motion segments
147  void setAllSegments(int idx,
148  const GT_DataArrayHandle &h);
149 
150  /// Copies an entire segment of data arrays from a source list to this list.
151  /// This is useful when combining multiple primitives into a single motion
152  /// blurred primitive for example.
153  /// @param dest_segment @n
154  /// Data arrays are copied to this segment in this list
155  /// @param source_list @n
156  /// Data arrays are copied from this source list
157  /// @param source_segment @n
158  /// Data arrays are copied from this segment in the source_list
159  /// @warning The source attribute list @b must have the same maps. The
160  /// code does @b not verify that the maps are the same.
161  bool copySegment(int dest_segment,
162  const GT_AttributeListHandle &source_list,
163  int source_segment=0);
164 
165  /// Create a transformed version of the attributes
167 
168  /// Create a new attribute list containing
169  GT_AttributeListHandle createConstant(GT_Offset offset,
170  GT_Size size) const;
171 
172  /// Create an attribute list of a subset of the contents
173  GT_AttributeListHandle createSubArray(GT_Offset start,
174  GT_Size size) const;
175 
176  /// Create a new attribute list based on the indirect indicies in the
177  /// @c list
178  GT_AttributeListHandle createIndirect(
180  bool flatten_nested = true) const;
181 
182  /// Merge attributes which don't already exist in this attribute list
183  GT_AttributeListHandle mergeNewAttributes(
184  const GT_AttributeListHandle &s) const;
185 
186  /// Create the "s" and "t" attributes for the given u/v vertex count.
187  /// The attributes will only be created if @c nu or @c nv is greater than 0
188  GT_AttributeListHandle createUV(int nu, int nv,
189  fpreal64 s0=0, fpreal64 s1=1,
190  fpreal64 t0=0, fpreal64 t1=1) const;
191 
192  /// Create a merged list of the two lists. This will merge a sub-array of
193  /// the first list and an indirect mapping of the second list.
194  /// This is used to merge a vertex list (l0) with a point list (l1)
195  static GT_AttributeListHandle createMerged(
196  const GT_AttributeMerge &map,
197  const GT_AttributeListHandle &l0,
198  GT_Offset l0_offset, GT_Size l0_size,
199  const GT_AttributeListHandle &l1,
200  const GT_DataArrayHandle &l1_indirect);
201 
202  /// Merge two lists, expanding the constant list to have arrays matching
203  /// the length of the varying array.
204  static GT_AttributeListHandle expandConstantMerge(
205  const GT_AttributeMerge &map,
206  const GT_AttributeListHandle &varying,
207  const GT_AttributeListHandle &constant,
208  GT_Offset const_index=0);
209 
210  /// Merge two lists, creating a constant value list, selecting a single
211  /// element from a varying list.
212  static GT_AttributeListHandle createConstantMerge(
213  const GT_AttributeMerge &map,
214  const GT_AttributeListHandle &varying,
216  const GT_AttributeListHandle &constant);
217 
218  /// Merge an array of N lists to form a single attribute list. All the
219  /// data is concatenated in order.
220  ///
221  /// This method will fail (return an empty list) if any lists don't have
222  /// matching attributes (including motion segments & order of attributes)
223  ///
224  /// The list may have NULL handles.
225  static GT_AttributeListHandle concatenateLists(
227 
228  /// Get a 64b hash of all data ids in the list. Return false if any attribs
229  /// have an invalid data id (-1)
230  bool getDataIdHash(int64 &hash, int segment=0) const;
231 
232  /// Refresh the any detail references and data IDs on all data arrays.
233  void updateGeoDetail(const GU_ConstDetailHandle &dtl,
234  GT_Owner scope) const;
235 
236  /// Debug method to dump the list to stdout
237  void dumpList(const char *label="", bool data_too = true);
238 
239  /// Save to a JSON stream
240  bool save(UT_JSONWriter &w) const;
241 
243  {
244  public:
246  : myList(NULL), myCurr(0), mySize(0), mySegment(0) {}
247  iterator(const iterator &src)
248  { *this = src; }
250 
252  {
253  return myList->get(myCurr,
254  mySegment);
255  }
256  const UT_StringHolder &getName() const
257  {
258  return myList->getName(myCurr);
259  }
260 
261  void rewind() { myCurr = 0; }
262  void advance() { myCurr++; }
263  bool atEnd() const { return myCurr >= mySize; }
264 
265  iterator &operator++() { advance(); return *this; }
266  // No post increment as it is dangerous.
267  const iterator &operator=(const iterator &src)
268  {
269  myList = src.myList;
270  myCurr = src.myCurr;
271  mySize = src.mySize;
272  return *this;
273  }
274 
275  private:
276  iterator(const GT_AttributeList *list, int segment)
277  : myList(list),
278  mySegment(segment),
279  myCurr(0),
280  mySize(list->entries()) {}
281 
282  const GT_AttributeList *myList;
283  int myCurr, mySize, mySegment;
284  friend class GT_AttributeList;
285  };
286  /// Traverse over all attributes for a given motion segment
287  iterator begin(int segment=0) const { return iterator(this, segment); }
288 
289  // Forward declare nested class (and make it a friend)
290  class gt_CreateIndirectTask;
291  friend class gt_CreateIndirectTask;
292 
293 private:
294 
295  // Private constructor for createAttributeList() only
297  int motion_segments);
298 
299  static SYS_FORCE_INLINE int
300  gt_CountAttribs()
301  {
302  return 0;
303  }
304  template <typename DataT, typename... Pairs>
305  static int
306  gt_CountAttribs(const UT_StringRef &name, DataT&& data,
307  Pairs&&... pairs)
308  {
309  return (data ? 1 : 0) + gt_CountAttribs(std::forward<Pairs>(pairs)...);
310  }
311 
312  static SYS_FORCE_INLINE void
313  gt_CreateAttributeList(GT_AttributeList &, GT_AttributeMap &)
314  {
315  }
316  template <typename DataT, typename... Pairs>
317  static void
318  gt_CreateAttributeList(
319  GT_AttributeList &list, GT_AttributeMap &map,
320  const UT_StringHolder &name, DataT&& data,
321  Pairs&&... pairs)
322  {
323  // Ignore NULL data arrays. Makes it easier to make lists where it's
324  // not known if some of the data arrays are valid at compile time.
325  if (data)
326  {
327  int i = map.add(name, /*replace*/false);
328  UT_ASSERT_MSG(i >= 0, "Should not have duplicates!");
329  list.set(i, data);
330  }
331  gt_CreateAttributeList(list, map, std::forward<Pairs>(pairs)...);
332  }
333 
334 private:
335  /// Create a new constant data array for the i'th element
336  const GT_DataArrayHandle createConstant(int idx, GT_Offset offset,
337  GT_Size size,
338  int segment) const;
339 
340  /// Create a new sub-array data array for the i'th element
341  const GT_DataArrayHandle createSubArray(int idx, GT_Offset start,
342  GT_Size size,
343  int segment) const;
344 
345  /// Create a new sub-array data array for the i'th element
346  const GT_DataArrayHandle createIndirect(
347  int idx,
349  int segment,
350  bool flatten_nested) const;
351 
352  inline int getSegmentIndex(int index, int segment) const
353  { return index + segment*entries(); }
354 
355  // Keep the 32 bit int first in the structure. Since we inherit from
356  // GA_IntrusiveRefCounter, this alignment makes the object smaller.
357  int myMotionSegments;
358  const GT_AttributeMapHandle myMap;
359  GT_DataArrayHandle *myData;
360 
361  // For threading of creating indirect arrays
362 };
363 
364 #endif
Keep track of merging of attribute maps.
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
GLenum const void * lists
Definition: glad.h:2100
void set(int idx, const GT_DataArrayHandle &h, int motion_segment=0)
Assign an array to a given index in the list.
A symbol table for attribute data.
const void * indirect
Definition: glcorearb.h:1795
const GT_DataArrayHandle & getData() const
GLuint start
Definition: glcorearb.h:475
#define GT_API
Definition: GT_API.h:13
int getIndex(const UT_StringRef &name) const
Return the index for a given name (returns -1 if miss)
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
const GT_AttributeMapHandle & getMap() const
Get a handle to the name map for this list.
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
A reference counter base class for use with UT_IntrusivePtr.
const UT_StringHolder & getName(int index) const
Return the name for a given index.
int add(const UT_StringHolder &name, bool replace_existing)
iterator(const iterator &src)
double fpreal64
Definition: SYS_Types.h:201
#define UT_ASSERT_MSG(ZZ,...)
Definition: UT_Assert.h:159
const UT_StringArray & getNames() const
Return the list of names.
const UT_StringHolder & getExportName(int index) const
Return the export name for a given index.
GLdouble n
Definition: glcorearb.h:2008
GLintptr offset
Definition: glcorearb.h:665
Abstract data class for an array of float, int or string data.
Definition: GT_DataArray.h:40
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
int64 GT_Offset
Definition: GT_Types.h:129
long long int64
Definition: SYS_Types.h:116
static GT_AttributeListHandle createAttributeList(Pairs &&...pairs)
GLuint const GLchar * name
Definition: glcorearb.h:786
int entries() const
Return the number of attributes in the list.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GA_API const UT_StringHolder transform
GLint GLenum GLint x
Definition: glcorearb.h:409
const UT_StringHolder & getName() const
GT_Owner
Definition: GT_Types.h:90
GT_AttributeListHandle addAttribute(const UT_StringHolder &name, const GT_DataArrayHandle &h, bool replace_existing) const
int64 GT_Size
Definition: GT_Types.h:128
GLsizeiptr size
Definition: glcorearb.h:664
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
GLuint index
Definition: glcorearb.h:786
int getSegments() const
Return the number of motion segements.
iterator begin(int segment=0) const
Traverse over all attributes for a given motion segment.
GT_Owner getOriginalOwner(int idx) const
Get original attribute owner for the given attribute.
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
const iterator & operator=(const iterator &src)
bool hasName(const UT_StringRef &name) const
Test of an attribute name exists.
Definition: format.h:895
GLenum src
Definition: glcorearb.h:1793