HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GA_GBElement.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: GA_GBElement.h ( GA Library, C++)
7  *
8  * COMMENTS:
9  * Class to maintain old GB/GD/GEO/GU/ interfaces to elements. For
10  * points and vertices, instances are mere interface stubs wrapping
11  * an index map and an offset. GA_Primitive, however, derives from
12  * GA_GBElement to permit maximum legacy compatibility.
13  *
14  * This class provides methods from GB_Element, GB_AttributeElem, as
15  * well as utility methods to aid in implementing the interfaces of
16  * GB_Vertex (and its subclasses) and [GD|GEO]_Point.
17  */
18 
19 #ifndef __GA_GBElement__
20 #define __GA_GBElement__
21 
22 #include "GA_API.h"
23 
25 #include "GA_AIFStringTuple.h"
26 #include "GA_Attribute.h"
27 #include "GA_AttributeDict.h"
28 #include "GA_AttributeRef.h"
29 #include "GA_AttributeSet.h"
30 #include "GA_Detail.h"
31 #include "GA_IndexMap.h"
32 #include "GA_Types.h"
33 
34 #include <UT/UT_Assert.h>
35 #include <UT/UT_Quaternion.h>
36 #include <UT/UT_Vector2.h>
37 #include <UT/UT_Vector3.h>
38 #include <UT/UT_Vector4.h>
39 
40 #include <SYS/SYS_Types.h>
41 
42 #include <stddef.h>
43 #include <string.h>
44 
45 class GA_AttributeRefMap;
46 class GA_Defragment;
47 
49 
50 /// @note This class has no virtual methods nor is the destructor virtual.
51 /// Instances of derived classes are either passed by value (vertices),
52 /// managed by an object derived from GA_GBElementPage (points), or managed
53 /// through pointers to the subclass GA_Primitive (primitives).
55 {
56 protected:
57  /// This class is only for supporting legacy code and is slated for future
58  /// removal. We protect the constructors to discourage extraneous usage.
59  inline GA_GBElement(const GA_IndexMap &index_map, GA_Offset offset)
60  : myIndexMap(&index_map)
61  , myOffset(offset)
62  {
63  }
64  /// Default c-tor
66  : myIndexMap(NULL)
67  , myOffset(-1) // Start with an invalid offset
68  { }
69  /// Copy c-tor
70  GA_GBElement(const GA_GBElement &src)
71  : myIndexMap(src.myIndexMap)
72  , myOffset(src.myOffset)
73  { }
74 
75  /// We protect the destructor as nobody has any business using a reference
76  /// to this class to delete an instance of a class derived from us.
78 
79  /// @{
80  /// Copy/Compare the @b handle (not the contents)
81  GA_GBElement &operator=(const GA_GBElement &src)
82  {
83  myIndexMap = src.myIndexMap;
84  myOffset = src.myOffset;
85  return *this;
86  }
87  bool operator==(const GA_GBElement &src) const
88  {
89  return myIndexMap == src.myIndexMap &&
90  myOffset == src.myOffset;
91  }
92  /// @}
93 
94 public:
95 
96  /// Provide access to the GA_Detail containing this element
97  GA_Detail &getDetail() const { return myIndexMap->getDetail(); }
98  /// Provide access to the GA_IndexMap containing this element
99  const GA_IndexMap &getIndexMap() const { return *myIndexMap; }
100 
101  /// Provide the GA_Offset of this element in its GA_IndexMap. This should
102  /// be used to access attribute data (not the ordered index).
103  GA_Offset getMapOffset() const { return myOffset; }
104  /// Provide the ordered GA_Index of this element in its GA_IndexMap
106  { return myIndexMap->indexFromOffset(myOffset); }
107 
108  /// Test to see whether the GA_GBElement is bound to a valid offset
109  SYS_DEPRECATED_HDK(13.0)
110  bool isGAValid() const { return GAisValid(myOffset); }
111 
112  /// Copy a single attribute over.
113  SYS_DEPRECATED_HDK(13.0)
114  void copyAttributeData(const GA_RWAttributeRef &dstref,
115  const GA_GBElement &src,
116  const GA_ROAttributeRef &srcref);
117 
118  /// For defragmentation, we need to update the offset
119  void swapOffsetValue(const GA_Defragment &defrag);
120 
121  // --------------------------------------------------------------------
122  //
123  // Methods from GB_Element
124  //
125  // --------------------------------------------------------------------
126  /// Return the ordered position of the element. This provides backwards
127  /// compatibility with the older GB library.
128  SYS_DEPRECATED_HDK(13.0)
129  GA_Index getNum() const { return getMapIndex(); }
130 
131  /// This method returns true if the element (point/primitive/vertex) is
132  /// part of 3D geometry. Otherwise it returns false.
133  bool isPrimary() const;
134 
135  // Methods which don't have any support in GA
136  // const GB_BitArray &getGroups();
137  // void setNum();
138 
139  // --------------------------------------------------------------------
140  //
141  // Methods from GB_AttributeElem
142  //
143  // --------------------------------------------------------------------
144  SYS_DEPRECATED_HDK(13.0)
145  void copyAttribData(const GA_GBElement &src,
146  const GA_AttributeSet &src_dict,
147  const GA_AttributeSet &dest_dict,
148  bool copy_point_P=false)
149  {
150  gaCopyAttribData(src, src_dict, dest_dict,
151  copy_point_P ? NULL : getDetail().getP());
152  }
153  SYS_DEPRECATED_HDK(13.0)
154  void copyAttribData(const GA_GBElement &src,
155  const GA_AttributeSet &dict,
156  bool copy_point_P=false)
157  {
158  copyAttribData(src, dict, dict, copy_point_P);
159  }
160  SYS_DEPRECATED_HDK(13.0)
161  void copyAttributeValues(const GA_GBElement &src,
162  const GA_AttributeSet &dict,
163  bool copy_point_P=false)
164  {
165  copyAttribData(src, dict, copy_point_P);
166  }
167 
168  SYS_DEPRECATED_HDK(13.0)
169  void scaleAttributeValues(fpreal scale,
170  const GA_AttributeSet &dest_dict,
171  bool scale_point_P=false);
172  SYS_DEPRECATED_HDK(13.0)
173  void addAttributeValues(const GA_GBElement &src,
174  const GA_AttributeSet &src_dict,
175  const GA_AttributeSet &dest_dict,
176  fpreal scale=1,
177  bool add_point_P=false);
178  SYS_DEPRECATED_HDK(13.0)
179  void addAttributeValues(const GA_GBElement &src,
180  const GA_AttributeSet &dict,
181  fpreal scale=1,
182  bool add_point_P=false)
183  {
184  addAttributeValues(src, dict, dict, scale, add_point_P);
185  }
186  SYS_DEPRECATED_HDK(13.0)
187  void lerpAttributeValues(const GA_GBElement &v0,
188  const GA_GBElement &v1,
189  fpreal bias,
190  const GA_AttributeSet &src_dict,
191  const GA_AttributeSet &dest_dict,
192  bool lerp_point_P=false);
193  SYS_DEPRECATED_HDK(13.0)
194  void lerpAttributeValues(const GA_GBElement &v0,
195  const GA_GBElement &v1,
196  fpreal bias,
197  const GA_AttributeSet &dict,
198  bool lerp_point_P=false)
199  {
200  lerpAttributeValues(v0, v1, bias, dict, dict, lerp_point_P);
201  }
202 
203 private:
204  /// @cond
205 
206  /// Effectively a template namespace for access to basic types supported by
207  /// GA_AIFTuple.
208  template <typename T> class BasicAccessors
209  {
210  public:
211  static T get(const GA_ROAttributeRef &h, GA_Offset off,
212  int index)
213  {
214  T val;
215  get(h, off, val, index);
216  return val;
217  }
218  static void get(const GA_ROAttributeRef &h, GA_Offset off,
219  T &val, int index)
220  {
221  if (!h.getAIFTuple() ||
222  !h.getAIFTuple()->get(h.getAttribute(), off,
223  val, index))
224  {
225  val = 0;
226  }
227  }
228  static void set(const GA_WOAttributeRef &h, GA_Offset off,
229  const T &val, int index)
230  {
231  if (h.getAIFTuple())
232  h.getAIFTuple()->set(h.getAttribute(), off,
233  val, index);
234  }
235  static void get(const GA_ROAttributeRef &h, GA_Offset off,
236  T *val, int count)
237  {
238  if (!h.getAIFTuple() ||
239  !h.getAIFTuple()->get(h.getAttribute(), off,
240  val, count))
241  {
242  memset(val, 0, sizeof(T)*count);
243  }
244  }
245  static void set(const GA_WOAttributeRef &h, GA_Offset off,
246  const T *val, int count)
247  {
248  if (h.getAIFTuple())
249  h.getAIFTuple()->set(h.getAttribute(), off,
250  val, count);
251  }
252  };
253 
254  /// Effectively a template namespace for access to POD types consisting of
255  /// a simple tuple of a basic type supported by GA_AIFTuple.
256  template <typename T, typename BASIC_T> class PODCompoundAccessors
257  {
258  public:
259  static T get(const GA_ROAttributeRef &h, GA_Offset off,
260  int index)
261  {
262  T val;
263  get(h, off, val, index);
264  return val;
265  }
266  static void get(const GA_ROAttributeRef &h, GA_Offset off,
267  T &val, int index)
268  {
269  const size_t n_basic =
270  sizeof(T)/sizeof(BASIC_T);
271  if (!h.getAIFTuple() ||
272  !h.getAIFTuple()->get(h.getAttribute(), off,
273  (BASIC_T *)&val,
274  n_basic,
275  index*n_basic))
276  val = T();
277  }
278  static void set(const GA_WOAttributeRef &h, GA_Offset off,
279  const T &val, int index)
280  {
281  const size_t n_basic =
282  sizeof(T)/sizeof(BASIC_T);
283  if (h.getAIFTuple())
284  h.getAIFTuple()->set(h.getAttribute(), off,
285  (const BASIC_T *)&val,
286  n_basic,
287  index*n_basic);
288  }
289  static void get(const GA_ROAttributeRef &h, GA_Offset off,
290  T *val, int count)
291  {
292  if (!h.getAIFTuple() ||
293  !h.getAIFTuple()->get(h.getAttribute(), off,
294  (BASIC_T *)val,
295  count*sizeof(T)/sizeof(BASIC_T)))
296  {
297  T def;
298  for (int i = 0; i < count; i++)
299  val[i] = def;
300  }
301  }
302  static void set(const GA_WOAttributeRef &h, GA_Offset off,
303  const T *val, int count)
304  {
305  if (h.getAIFTuple())
306  h.getAIFTuple()->set(h.getAttribute(), off,
307  (const BASIC_T *)val,
308  count*sizeof(T)/sizeof(BASIC_T));
309  }
310  };
311  /// @endcond
312  /// @cond
313 
314  /// A private template class used to register allowable type parameters for
315  /// the public access method templates that follow.
316  ///
317  /// Explicit specializations of this class follow, each containing a single
318  /// Accessors type which behaves effectively as a namespace.
319  ///
320  /// The non-type FAKE parameter is to ensure the following specializations
321  /// are only partial, not full. Full specializations are not allowed at
322  /// class scope.
323  template <typename T, int FAKE> class TypeInfo
324  {
325  };
326  /// @endcond
327 
328  /// @cond
329  template <int FAKE> class TypeInfo<fpreal32, FAKE>
330  { public: typedef BasicAccessors<fpreal32> Accessors; };
331  template <int FAKE> class TypeInfo<int32, FAKE>
332  { public: typedef BasicAccessors<int32> Accessors; };
333 
334  template <int FAKE> class TypeInfo<fpreal64, FAKE>
335  { public: typedef BasicAccessors<fpreal64> Accessors; };
336  template <int FAKE> class TypeInfo<int64, FAKE>
337  { public: typedef BasicAccessors<int64> Accessors; };
338 
339  template <int FAKE> class TypeInfo<UT_Vector2, FAKE>
340  { public: typedef PODCompoundAccessors<UT_Vector2, fpreal32> Accessors; };
341  template <int FAKE> class TypeInfo<UT_Vector3, FAKE>
342  { public: typedef PODCompoundAccessors<UT_Vector3, fpreal32> Accessors; };
343  template <int FAKE> class TypeInfo<UT_Vector4, FAKE>
344  { public: typedef PODCompoundAccessors<UT_Vector4, fpreal32> Accessors; };
345  template <int FAKE> class TypeInfo<UT_Quaternion, FAKE>
346  { public: typedef PODCompoundAccessors<UT_Quaternion, fpreal32> Accessors; };
347  template <int FAKE> class TypeInfo<UT_Matrix3, FAKE>
348  { public: typedef PODCompoundAccessors<UT_Matrix3, fpreal32> Accessors; };
349  template <int FAKE> class TypeInfo<UT_Matrix4, FAKE>
350  { public: typedef PODCompoundAccessors<UT_Matrix4, fpreal32> Accessors; };
351  /// @endcond
352 
353 public:
354 
355 
356  // Methods which don't have any support in GA
357  // template<typename T> *castAttribData(int n);
358  // void *getAttribData(int n);
359  // GB_AttributeData &getAttrib(int n);
360 
361  /// @name Get/Set Accessors For Individual Attributes
362  /// @{
363 
364  /// These template methods can only be instantiated with the following
365  /// types:
366  /// fpreal32, fpreal64, int32, int64, UT_Vector[34], UT_Matrix[34]
367  ///
368  /// These methods use AIFs and so may fail gracefully or convert data
369  /// types appropriately.
370 
371  SYS_DEPRECATED_HDK(14.0)
372  const char *getString(const GA_ROAttributeRef &h, int index = 0) const
373  {
374  const GA_AIFStringTuple *aif = h.getAIFStringTuple();
375  if (!aif)
376  return 0;
377  UT_ASSERT( h.getAttribute() &&
378  &h.getAttribute()->getDetail() == &getDetail());
379  return aif->getString(h.getAttribute(), getMapOffset(),
380  index);
381  }
382 
383  SYS_DEPRECATED_HDK(14.0)
384  void setString(const GA_WOAttributeRef &h, const char *str,
385  int index = 0)
386  {
387  const GA_AIFStringTuple *aif = h.getAIFStringTuple();
388  if (!aif)
389  return;
390  UT_ASSERT( h.getAttribute() &&
391  &h.getAttribute()->getDetail() == &getDetail());
392  aif->setString(h.getAttribute(), getMapOffset(),
393  str, index);
394  }
395 
396  template <typename T>
397  SYS_DEPRECATED_HDK(14.0)
398  T getValue(const GA_ROAttributeRef &h, int index = 0) const
399  {
400  UT_ASSERT( h.getAttribute() &&
401  &h.getAttribute()->getDetail() == &getDetail());
403  h, getMapOffset(), index);
404  }
405  template <typename T>
406  SYS_DEPRECATED_HDK(14.0)
407  void setValue(const GA_WOAttributeRef &h, const T &val,
408  int index = 0)
409  {
410  UT_ASSERT( h.getAttribute() &&
411  &h.getAttribute()->getDetail() == &getDetail());
412  TypeInfo<T,0>::Accessors::set(
413  h, getMapOffset(), val, index);
414  }
415  template <typename T>
416  SYS_DEPRECATED_HDK(14.0)
417  void get(const GA_ROAttributeRef &h, T &val, int index = 0) const
418  {
419  UT_ASSERT( h.getAttribute() &&
420  &h.getAttribute()->getDetail() == &getDetail());
422  h, getMapOffset(), val, index);
423  }
424  template <typename T>
425  SYS_DEPRECATED_HDK(14.0)
426  void get(const GA_ROAttributeRef &h, T *val, int count) const
427  {
428  UT_ASSERT( h.getAttribute() &&
429  &h.getAttribute()->getDetail() == &getDetail());
431  h, getMapOffset(), val, count);
432  }
433  template <typename T>
434  SYS_DEPRECATED_HDK(14.0)
435  void set(const GA_WOAttributeRef &h, const T *val, int count)
436  {
437  UT_ASSERT( h.getAttribute() &&
438  &h.getAttribute()->getDetail() == &getDetail());
439  TypeInfo<T,0>::Accessors::set(
440  h, getMapOffset(), val, count);
441  }
442  /// @}
443 
444 
445 protected:
446  void gaCopyAttribData(const GA_GBElement *src,
447  const GA_Attribute *skip=NULL);
448  void gaCopyAttribData(const GA_GBElement &src,
449  const GA_AttributeSet &src_dict,
450  const GA_AttributeSet &dest_dict,
451  const GA_Attribute *skip = NULL);
452  void gaCopyAttribData(const GA_GBElement &src,
453  const GA_AttributeDict &src_dict,
454  const GA_AttributeDict &dest_dict,
455  const GA_Attribute *skip = NULL);
456 
457 private:
458  const GA_IndexMap *myIndexMap;
459  GA_Offset myOffset;
460 };
461 
463 
464 #endif
465 
A class to manage an ordered array which has fixed offset handles.
Definition: GA_IndexMap.h:63
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
GA_Index getMapIndex() const
Provide the ordered GA_Index of this element in its GA_IndexMap.
Definition: GA_GBElement.h:105
int int32
Definition: SYS_Types.h:39
GA_Attribute * getAttribute() const
GA_Offset getMapOffset() const
Definition: GA_GBElement.h:103
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
void
Definition: png.h:1083
This class provides a way to manage a reference to an attribute permitting Write-Only access...
void skip(T &in, int n)
Definition: ImfXdr.h:711
#define SYS_DEPRECATED_PUSH_DISABLE()
#define SYS_DEPRECATED_POP_DISABLE()
bool GAisValid(GA_Size v)
Definition: GA_Types.h:649
#define GA_API
Definition: GA_API.h:14
GA_GBElement & operator=(const GA_GBElement &src)
Definition: GA_GBElement.h:81
float fpreal32
Definition: SYS_Types.h:200
virtual const char * getString(const GA_Attribute *attrib, GA_Offset ai, int tuple_index=0) const =0
Get a single string from the array for a single tuple of an element.
virtual bool get(const GA_Attribute *attrib, GA_Offset ai, int32 &data, int index=0) const =0
A string map of attributes to ease backward compatibility In the GB/GEO/GU library code would often p...
double fpreal64
Definition: SYS_Types.h:201
const GA_AIFTuple * getAIFTuple() const
GA_Size GA_Offset
Definition: GA_Types.h:641
GA_API const UT_StringHolder scale
This class provides a way to manage a reference to an attribute permitting Read-Write access...
auto get(const UT_JSONValue::map_traverser &m) -> decltype(m.key())
Definition: UT_JSONValue.h:972
GLintptr offset
Definition: glcorearb.h:665
This class provides a way to manage a reference to an attribute permitting Read-Only access...
GA_GBElement(const GA_IndexMap &index_map, GA_Offset offset)
Definition: GA_GBElement.h:59
long long int64
Definition: SYS_Types.h:116
A handle to simplify manipulation of multiple attributes.
GA_Detail & getDetail() const
Provide access to the GA_Detail containing this element.
Definition: GA_GBElement.h:97
Defragmentation of IndexMaps.
Definition: GA_Defragment.h:45
#define SYS_DEPRECATED_HDK(__V__)
GA_GBElement(const GA_GBElement &src)
Copy c-tor.
Definition: GA_GBElement.h:70
virtual bool set(GA_Attribute *attrib, GA_Offset ai, int32 data, int comp=-1) const =0
GA_Size GA_Index
Define the strictness of GA_Offset/GA_Index.
Definition: GA_Types.h:635
GLfloat v0
Definition: glcorearb.h:816
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
fpreal64 fpreal
Definition: SYS_Types.h:277
GA_GBElement()
Default c-tor.
Definition: GA_GBElement.h:65
GLuint index
Definition: glcorearb.h:786
bool operator==(const GA_GBElement &src) const
Definition: GA_GBElement.h:87
GLfloat GLfloat v1
Definition: glcorearb.h:817
GLuint GLfloat * val
Definition: glcorearb.h:1608
Container class for all geometry.
Definition: GA_Detail.h:96
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
#define const
Definition: zconf.h:214
virtual bool setString(GA_Attribute *attrib, GA_Offset ai, const char *string, int tuple_index) const =0
Set a single component for a single element.
GLint GLsizei count
Definition: glcorearb.h:405
Generic Attribute Interface class to work with string indices directly, rather than string values...
GLenum src
Definition: glcorearb.h:1793
const GA_IndexMap & getIndexMap() const
Provide access to the GA_IndexMap containing this element.
Definition: GA_GBElement.h:99