HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GA_ElementWrangler.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_ElementWrangler.h ( GA Library, C++)
7  *
8  * COMMENTS: A utility class to abstract common preamble from GA_GBElement
9  * methods to a higher level.
10  */
11 
12 #ifndef __GA_ElementWrangler__
13 #define __GA_ElementWrangler__
14 
15 #include "GA_API.h"
16 #include "GA_AttributeFilter.h"
17 #include "GA_AttributeRefMap.h"
18 #include "GA_Types.h"
19 
20 #include <UT/UT_NonCopyable.h>
21 #include <SYS/SYS_Types.h>
22 
23 
24 class GA_Attribute;
25 class GA_Detail;
26 class GA_Range;
27 
28 
29 /// @brief A utility class to amortize overhead from GA_GBElement attribute
30 /// handling methods.
31 ///
32 /// The GA_ElementWrangler class provides a simplified interface for common
33 /// attribute operations as elements are added or removed.
34 ///
35 /// This class is not thread-safe, so each thread should have its own copy,
36 /// and any attributes added after its instantiation will not be processed.
37 
39 {
40 public:
41  // This builds a write-cache. This accelerates string and
42  // dictionary writes, but means that you can't necessarily read
43  // from stuff that has been written.
44  void buildCache()
45  { myCache = UTmakeUnique<GA_AttributeRefMap::Cache>(); }
46  // Clear and flush the write cache.
47  void clearCache()
48  { myCache.reset(); }
50  {
51  myMap.copyValue(myOwner, dest, myOwner, src,
52  cache());
53  }
54  void copyAttributeValues(const GA_Range &dest, GA_Offset src);
56  {
57  myMap.multiply(myOwner, dest, scale);
58  }
60  fpreal scale=1)
61  {
62  myMap.addValue(myOwner, dest, myOwner, src, scale);
63  }
65  GA_Offset src1, fpreal bias)
66  {
67  myMap.lerpValue(myOwner, dest, myOwner, src0, src1, bias);
68  }
69 
70  bool isValid(GA_Detail &dest) const
71  {
72  return myMap.getDestDetail() == &dest &&
73  myMap.getSourceDetail() == &dest;
74  }
75  bool isValid(GA_Detail &dest, const GA_Detail &src) const
76  {
77  return myMap.getDestDetail() == &dest &&
78  myMap.getSourceDetail() == &src;
79  }
80  int getNumAttributes() const
81  { return myMap.entries(); }
83  {
84  myMap.bumpAllDestDataIds();
85  }
86 
87  GA_AttributeRefMap &getMap() { return myMap; }
88 
89 protected:
91  const GA_Attribute *exclude = 0);
93  const GA_Detail &src, const GA_Attribute *exclude = 0);
94 
96  const GA_AttributeFilter &filter);
98  const GA_Detail &src, const GA_AttributeFilter &filter);
99 
100  /// Protected destructor as we don't support polymorphic destruction.
102 
103  GA_AttributeRefMap::Cache *cache() { return myCache.get(); }
104 
105 private:
107  GA_AttributeOwner myOwner;
108  GA_AttributeRefMap myMap;
109 };
110 
111 /// By default, operations on "P" are performed as with any other attribute.
112 /// However, it's possible to turn on the homogeneous flag which will ensure
113 /// that operations are done using homogeneous coordinates.
115 {
116 public:
117  enum IncludeP { EXCLUDE_P, INCLUDE_P };
118 
119  /// The GA_PointWrangler::IncludeP argument passed to the constructor
120  /// determines whether the base class operations include "P".
121  GA_PointWrangler(GA_Detail &dest, IncludeP p);
122  GA_PointWrangler(GA_Detail &dest, const GA_Detail &src, IncludeP p);
123 
125  GA_PointWrangler(GA_Detail &dest, const GA_Detail &src,
126  const GA_AttributeFilter &filter);
127 
129  {
130  delete myAlternateMap;
131  }
132 
133  bool includesP() const { return myBaseIncludeP == INCLUDE_P; }
134 
139 
141  IncludeP copy_p)
142  {
143  if (copy_p == myBaseIncludeP)
144  copyAttributeValues(dest, src);
145  else
146  {
147  initAlternateHandle();
148  myAlternateMap->copyValue(
149  GA_ATTRIB_POINT, dest,
150  GA_ATTRIB_POINT, src,
151  cache());
152  }
153  }
155  IncludeP scale_p)
156  {
157  if (scale_p == myBaseIncludeP)
158  scaleAttributeValues(dest, scale);
159  else
160  {
161  initAlternateHandle();
162  myAlternateMap->multiply(
163  GA_ATTRIB_POINT, dest, scale);
164  }
165  }
167  fpreal scale, IncludeP add_p)
168  {
169  if (add_p == myBaseIncludeP)
170  addAttributeValues(dest, src, scale);
171  else
172  {
173  initAlternateHandle();
174  myAlternateMap->addValue(
175  GA_ATTRIB_POINT, dest,
176  GA_ATTRIB_POINT, src, scale);
177  }
178  }
180  GA_Offset src1, fpreal bias,
181  IncludeP lerp_p)
182  {
183  if (lerp_p == myBaseIncludeP)
184  lerpAttributeValues(dest, src0, src1, bias);
185  else
186  {
187  initAlternateHandle();
188  myAlternateMap->lerpValue(
189  GA_ATTRIB_POINT, dest,
190  GA_ATTRIB_POINT, src0, src1, bias);
191  }
192  }
193 private:
194  void initAlternateHandle()
195  {
196  if (!myAlternateMap)
197  allocAlternateHandle();
198  }
199  void allocAlternateHandle();
200 
201  // We keep track of whether the base class handle includes "P" since we
202  // may need to allocate an alternate handle when an operation requests
203  // the opposite behavior.
204  IncludeP myBaseIncludeP;
205  GA_AttributeFilter myFilter;
206  GA_AttributeRefMap *myAlternateMap;
207 };
208 
210 {
211 public:
215  : GA_ElementWrangler(GA_ATTRIB_VERTEX, dest, src) {}
216 
218  : GA_ElementWrangler(GA_ATTRIB_VERTEX, dest, filter) {}
220  const GA_AttributeFilter &filter)
221  : GA_ElementWrangler(GA_ATTRIB_VERTEX, dest, src, filter) {}
222 private:
223 };
224 
226 {
227 public:
231  : GA_ElementWrangler(GA_ATTRIB_PRIMITIVE, dest, src) {}
232 
234  : GA_ElementWrangler(GA_ATTRIB_PRIMITIVE, dest, filter) {}
236  const GA_AttributeFilter &filter)
237  : GA_ElementWrangler(GA_ATTRIB_PRIMITIVE, dest, src, filter) {}
238 private:
239 };
240 
242 {
243 public:
247  : GA_ElementWrangler(GA_ATTRIB_GLOBAL, dest, src) {}
248 
250  : GA_ElementWrangler(GA_ATTRIB_GLOBAL, dest, filter) {}
252  const GA_AttributeFilter &filter)
253  : GA_ElementWrangler(GA_ATTRIB_GLOBAL, dest, src, filter) {}
254 private:
255 };
256 
258 {
259 public:
261  GA_PointWrangler::IncludeP include_p)
262  : myDestGdp(&dest), mySrcGdp(&dest), myPrimitiveWrangler(0),
263  myVertexWrangler(0), myPointWrangler(0), myIncludeP(include_p) {}
265  GA_PointWrangler::IncludeP include_p)
266  : myDestGdp(&dest), mySrcGdp(&src), myPrimitiveWrangler(0),
267  myVertexWrangler(0), myPointWrangler(0), myIncludeP(include_p) {}
268 
270  const GA_AttributeFilter &filter)
271  : myDestGdp(&dest), mySrcGdp(&dest), myPrimitiveWrangler(0),
272  myVertexWrangler(0), myPointWrangler(0), myFilter(filter) {}
274  const GA_AttributeFilter &filter)
275  : myDestGdp(&dest), mySrcGdp(&src), myPrimitiveWrangler(0),
276  myVertexWrangler(0), myPointWrangler(0), myFilter(filter) {}
277 
279  {
280  if (myPrimitiveWrangler)
281  delete myPrimitiveWrangler;
282  if (myVertexWrangler)
283  delete myVertexWrangler;
284  if (myPointWrangler)
285  delete myPointWrangler;
286  }
287 
288  void clear()
289  {
290  if (myPrimitiveWrangler)
291  {
292  delete myPrimitiveWrangler;
293  myPrimitiveWrangler = 0;
294  }
295  if (myVertexWrangler)
296  {
297  delete myVertexWrangler;
298  myVertexWrangler = 0;
299  }
300  if (myPointWrangler)
301  {
302  delete myPointWrangler;
303  myPointWrangler = 0;
304  }
305  }
306 
307  void reset(GA_Detail &dest)
308  {
309  myDestGdp = &dest;
310  mySrcGdp = &dest;
311  clear();
312  }
313  void reset(GA_Detail &dest, const GA_Detail &src)
314  {
315  myDestGdp = &dest;
316  mySrcGdp = &src;
317  clear();
318  }
319  bool isValid(const GA_Detail &dest) const
320  {
321  return myDestGdp == &dest &&
322  mySrcGdp == &dest;
323  }
324  bool isValid(const GA_Detail &dest,
325  const GA_Detail &src) const
326  {
327  return myDestGdp == &dest &&
328  mySrcGdp == &src;
329  }
330 
332  {
333  if (!myPrimitiveWrangler)
334  {
335  if (myFilter.isValid())
336  myPrimitiveWrangler = new GA_PrimitiveWrangler(*myDestGdp,
337  *mySrcGdp,
338  myFilter);
339  else
340  myPrimitiveWrangler = new GA_PrimitiveWrangler(*myDestGdp,
341  *mySrcGdp);
342  }
343  return *myPrimitiveWrangler;
344  }
346  {
347  if (!myVertexWrangler)
348  {
349  if (myFilter.isValid())
350  myVertexWrangler = new GA_VertexWrangler(*myDestGdp,
351  *mySrcGdp,
352  myFilter);
353  else
354  myVertexWrangler = new GA_VertexWrangler(*myDestGdp,
355  *mySrcGdp);
356  }
357  return *myVertexWrangler;
358  }
360  {
361  if (!myPointWrangler)
362  {
363  if (myFilter.isValid())
364  myPointWrangler = new GA_PointWrangler(*myDestGdp, *mySrcGdp,
365  myFilter);
366  else
367  myPointWrangler = new GA_PointWrangler(*myDestGdp, *mySrcGdp,
368  myIncludeP);
369  }
370  return *myPointWrangler;
371  }
372 
373 private:
374  GA_Detail *myDestGdp;
375  const GA_Detail *mySrcGdp;
376  GA_PrimitiveWrangler *myPrimitiveWrangler;
377  GA_VertexWrangler *myVertexWrangler;
378  GA_PointWrangler *myPointWrangler;
379  GA_PointWrangler::IncludeP myIncludeP;
380  GA_AttributeFilter myFilter;
381 };
382 
383 #endif
GA_DetailWrangler(GA_Detail &dest, const GA_Detail &src, const GA_AttributeFilter &filter)
void scaleAttributeValues(GA_Offset dest, fpreal scale)
GA_VertexWrangler & getVertex()
void addAttributeValues(GA_Offset dest, GA_Offset src, fpreal scale, IncludeP add_p)
Definition of a geometry attribute.
Definition: GA_Attribute.h:196
void copyAttributeValues(GA_Offset dest, GA_Offset src)
GLenum src
Definition: glew.h:2410
GA_PrimitiveWrangler(GA_Detail &dest, const GA_Detail &src, const GA_AttributeFilter &filter)
bool includesP() const
GA_DetailWrangler(GA_Detail &dest)
void addAttributeValues(GA_Offset dest, GA_Offset src, fpreal scale=1)
GA_ElementWranglerCache(GA_Detail &dest, const GA_AttributeFilter &filter)
bool isValid(GA_Detail &dest, const GA_Detail &src) const
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:13880
~GA_ElementWrangler()
Protected destructor as we don't support polymorphic destruction.
bool isValid(GA_Detail &dest) const
A utility class to amortize overhead from GA_GBElement attribute handling methods.
#define GA_API
Definition: GA_API.h:12
GA_ElementWranglerCache(GA_Detail &dest, const GA_Detail &src, const GA_AttributeFilter &filter)
GA_VertexWrangler(GA_Detail &dest)
void reset(GA_Detail &dest)
A range of elements in an index-map.
Definition: GA_Range.h:42
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
GA_Size GA_Offset
Definition: GA_Types.h:639
GA_DetailWrangler(GA_Detail &dest, const GA_AttributeFilter &filter)
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glew.h:2981
GA_DetailWrangler(GA_Detail &dest, const GA_Detail &src)
GA_AttributeRefMap::Cache * cache()
void lerpAttributeValues(GA_Offset dest, GA_Offset src0, GA_Offset src1, fpreal bias, IncludeP lerp_p)
void reset(GA_Detail &dest, const GA_Detail &src)
GA_ElementWranglerCache(GA_Detail &dest, GA_PointWrangler::IncludeP include_p)
A handle to simplify manipulation of multiple attributes.
bool isValid(const GA_Detail &dest, const GA_Detail &src) const
GA_VertexWrangler(GA_Detail &dest, const GA_Detail &src)
GLfloat bias
Definition: glew.h:10274
GLfloat GLfloat p
Definition: glew.h:16321
GA_PointWrangler & getPoint()
GA_PrimitiveWrangler(GA_Detail &dest, const GA_Detail &src)
GA_AttributeOwner
Definition: GA_Types.h:33
GA_VertexWrangler(GA_Detail &dest, const GA_Detail &src, const GA_AttributeFilter &filter)
GA_ElementWranglerCache(GA_Detail &dest, const GA_Detail &src, GA_PointWrangler::IncludeP include_p)
GA_AttributeRefMap & getMap()
void scaleAttributeValues(GA_Offset dest, fpreal scale, IncludeP scale_p)
fpreal64 fpreal
Definition: SYS_Types.h:277
GA_PrimitiveWrangler(GA_Detail &dest)
void lerpAttributeValues(GA_Offset dest, GA_Offset src0, GA_Offset src1, fpreal bias)
GA_VertexWrangler(GA_Detail &dest, const GA_AttributeFilter &filter)
Container class for all geometry.
Definition: GA_Detail.h:95
void copyAttributeValues(GA_Offset dest, GA_Offset src, IncludeP copy_p)
GA_PrimitiveWrangler(GA_Detail &dest, const GA_AttributeFilter &filter)
GA_PrimitiveWrangler & getPrimitive()
int getNumAttributes() const
bool isValid(const GA_Detail &dest) const