HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GA_AttributeRefMapDestHandle.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_AttributeRefMapDestHandle.h ( GA Library, C++)
7  *
8  * COMMENTS: A convenience class for factoring out the destination arguments
9  * from GA_AttributeRefMap methods.
10  */
11 
12 #ifndef __GA_AttributeRefMapDestHandle__
13 #define __GA_AttributeRefMapDestHandle__
14 
15 #include "GA_API.h"
16 #include "GA_AttributeRefMap.h"
18 #include "GA_Types.h"
19 
20 #include <UT/UT_VectorTypes.h>
21 
22 #include <SYS/SYS_Types.h>
23 
24 
25 class GA_WeightedSum;
26 
27 
28 /// @brief A handle to simplify manipulation of multiple attributes.
29 ///
30 /// The GA_AttributeRefMapDestHandle class simplifies interaction with a
31 /// GA_AttributeRefMap by tracking the current destination element.
32 ///
33 /// When performing operations on object elements, all attribute classes which
34 /// are uniquely identified by the owner of the object will be operated on.
35 /// For example, when operating on a vertex, it is possible to uniquely
36 /// identify a point object. Thus, when @c add() is called, vertex @b and
37 /// point attributes on the source detail will be processed. When @c add()
38 /// is called with the owner set to GA_ATTRIB_POINT, there may be multiple
39 /// vertices which share the point. So, only point attributes will be
40 /// processed.
41 ///
42 /// This can be summarized by:
43 /// - @c GA_AttributeRefMapDestHandlePoint: @n
44 /// Process GA_ATTRIB_POINT, GA_ATTRIB_GLOBAL
45 /// - @c GA_AttributeRefMapDestHandleVertex(): @n
46 /// Process GA_ATTRIB_VERTEX, GA_ATTRIB_POINT, GA_ATTRIB_PRIMITIVE and
47 /// GA_ATTRIB_DETAIL
48 /// - @c GA_AttributeRefMapDestHandlePrimitive(): @n
49 /// Process GA_ATTRIB_PRIMITIVE and GA_ATTRIB_DETAIL
50 /// - @c GA_AttributeRefMapDestHandleGlobal(): @n
51 /// Process GA_ATTRIB_DETAIL
52 ///
53 /// The same semantics are used when setting an element on the handle. If @c
54 /// setPrimitive() is called, destination primitive and global attributes will
55 /// be modified.
56 ///
57 /// By default, operations on "P" are performed as with any other attribute.
58 /// However, it's possible to turn on the homogeneous flag which will ensure
59 /// that operations are done using homogeneous coordinates.
60 template <GA_AttributeOwner DOwner>
62 {
63 private:
64  /// For parameter semantics - the source and destination should always
65  /// be the same owner.
66  static const GA_AttributeOwner SOwner = DOwner;
67 
68 public:
69  /// Minimal constructor
71  : myMap(map)
72  , myDestMap(DOwner, DOwner == GA_ATTRIB_GLOBAL ? GA_Offset(0) :
74  /// Copy constructor
76  : myMap(src.myMap), myDestMap(src.myDestMap) {}
77  /// Destructor
79 
80 
81  /// Return the number of attributes in the list
82  int entries() const { return myMap.entries(); }
83 
84 
85  /// Return the number of attributes in the list that are paired with the
86  /// source being P.
87  int entriesWithSourceP() const
88  { return myMap.entriesWithSourceP(); }
89 
90  /// @{
91  /// Set the destination element. Operations will write to the element
92  /// specified.
93  ///
94  /// Only attributes which are fully qualified by the element will be
95  /// operated on.
96  /// - Point: Point and detail attributes
97  /// - Vertex: Vertex, point, primitive and detail attributes
98  /// - Primitive: Primitive and detail attributes
99  /// - Global: Only global attributes
101  { myDestMap.reset(DOwner, offset); return true; }
102  /// @}
103 
104  /// Homogenize any rational attributes of the current element
105  void homogenize() const
106  { return myMap.homogenize(myDestMap); }
107  /// Dehomogenize any rational attributes of the current element
108  void dehomogenize() const
109  { return myMap.dehomogenize(myDestMap); }
110 
111  /// Copy operation: @code dest = source @endcode
112  /// This is the generic copy operation
113  void copy(GA_Offset offset) const
114  { return myMap.copyValue(myDestMap, SOwner, offset); }
115 
116  /// @{
117  /// Copy operations: @code dest = position @endcode
118  /// Sets any attributes that read the position attribute to an explicit
119  /// value.
120  void copyExplicitPosition(const UT_Vector2 &p) const
121  { myMap.copyExplicitPosition(myDestMap, p); }
122  void copyExplicitPosition(const UT_Vector2D &p) const
123  { myMap.copyExplicitPosition(myDestMap, p); }
124  void copyExplicitPosition(const UT_Vector3 &p) const
125  { myMap.copyExplicitPosition(myDestMap, p); }
126  void copyExplicitPosition(const UT_Vector3D &p) const
127  { myMap.copyExplicitPosition(myDestMap, p); }
128  void copyExplicitPosition(const UT_Vector4 &p) const
129  { myMap.copyExplicitPosition(myDestMap, p); }
130  void copyExplicitPosition(const UT_Vector4D &p) const
131  { myMap.copyExplicitPosition(myDestMap, p); }
132  /// @}
133 
134  /// Add operation: @code dest += source*scale @endcode
135  /// This is the generic add operation
136  void add(GA_Offset source_index, fpreal scale=1) const
137  {
138  myMap.addValue(myDestMap, SOwner, source_index,
139  scale);
140  }
141 
142  /// Sub operation: @code dest -= source @endcode
143  /// This is the generic sub operation
144  void sub(GA_Offset source_index) const
145  {
146  myMap.subValue(myDestMap, SOwner, source_index);
147  }
148 
149  /// Mul operation: @code dest *= source @endcode
150  /// This is the generic mul operation
151  void mul(GA_Offset source_index) const
152  {
153  myMap.mulValue(myDestMap, SOwner, source_index);
154  }
155 
156  /// Linear interpolation operation: @code dest = s0 + (s1 - s0)*t @endcode
157  /// Generic linear interpolation between two of the same elements
158  void lerp(GA_Offset s0, GA_Offset s1, fpreal t) const
159  { myMap.lerpValue(myDestMap, SOwner, s0, s1, t); }
160  void lerpH(GA_Offset s0, GA_Offset s1, fpreal t) const
161  { myMap.lerpHValue(myDestMap, SOwner, s0, s1, t); }
162 
163  /// @{
164  /// Weighted sum operation: @code dest = sum(w[i]*s[i]) @endcode
165  /// @code
166  /// // Compute the average value of the P attribute and store in a
167  /// // global attribute "averageP".
168  /// GA_WeightedSum sum;
169  /// GA_AttributeRefMap map(gdp);
170  /// GA_AttributeRefMapDestHandleGlobal gah(map);
171  /// int npts;
172  /// map.append( detail.findGlobalAttribute("averageP"),
173  /// detail.findPointAttribute("P") );
174  /// gah.startSum(sum);
175  /// for (GA_Iterator it = gdp.getPointRange(); !it.atEnd(); ++it)
176  /// gah.sumPoint(sum, it.getOffset(), 1);
177  /// npts = gdp.getNumPoints();
178  /// gah.finishSum(sum, npts ? 1./npts : 0);
179  /// @endcode
180  void startSum(const GA_WeightedSum &sum) const
181  { myMap.startSum(sum, myDestMap); }
182  void startHSum(const GA_WeightedSum &sum) const
183  { myMap.startHSum(sum, myDestMap); }
184  void finishSum(const GA_WeightedSum &sum,
185  fpreal normalization=1) const
186  { myMap.finishSum(sum, myDestMap, normalization); }
187  void finishHSum(const GA_WeightedSum &sum,
188  fpreal normalization=1) const
189  { myMap.finishHSum(sum, myDestMap, normalization); }
190  /// @}
191 
192  /// Add a value into the weighted sum. This advances the weighted sum with
193  /// the weight given.
194  void add(GA_WeightedSum &sum,
195  GA_Offset source_index,
196  fpreal w) const
197  {
198  myMap.addSumValue(sum, myDestMap, SOwner,
199  source_index, w);
200  }
201  void addH(GA_WeightedSum &sum,
202  GA_Offset source_index,
203  fpreal w) const
204  {
205  myMap.addHSumValue(sum, myDestMap, SOwner,
206  source_index, w);
207  }
208 
209  /// Compute barycentric coordinates for 3 values
210  /// @code result = (1 - u - v)*p0 + u*p1 + v*p2 @endcode
212  fpreal u, fpreal v) const
213  {
214  myMap.barycentricValue(myDestMap, SOwner,
215  p0, p1, p2, u, v);
216  }
217 
218  /// Compute bilinear interpolation over 4 values of a quadrilateral. The
219  /// math is the same as SYSbilerp(): @code
220  /// (u=0,v=1) u0v1 u1v1 (u=1,v=1)
221  /// +--------+
222  /// | |
223  /// | |
224  /// +--------+
225  /// (u=0,v=0) u0v0 u1v0 (u=1,v=0)
226  /// @endcode
227  void bilinear(GA_Offset u0v0, GA_Offset u1v0,
228  GA_Offset u0v1, GA_Offset u1v1,
229  fpreal u, fpreal v) const
230  {
231  myMap.bilinearValue(myDestMap, SOwner, u0v0, u1v0,
232  u0v1, u1v1, u, v);
233  }
234 
235  /// Zero an attribute. This is equivalent to a weighted sum of 0 elements.
236  void zero() const { myMap.zeroElement(myDestMap); }
237 
238  /// Multiply operation: @code dest *= scale @endcode
239  void multiply(fpreal scale) const
240  { myMap.multiply(myDestMap, scale); }
241 
242  /// @{
243  /// Transform attributes. This uses tags on the attribute to determine how
244  /// to perform the transform.
245  /// @param m The transform matrix
246  /// @param im The inverse of @m (i.e. @c m.invert(im) )
247  /// Transforms are independent of the source geometry.
248  void transform(const UT_Matrix4 &m, const UT_Matrix4 &im) const
249  { myMap.transform(m, im, myDestMap); }
250  void transform(const UT_DMatrix4 &m, const UT_DMatrix4 &im) const
251  { myMap.transform(m, im, myDestMap); }
252  /// @}
253 
254  /// @{
255  /// Compare value with another elements
256  bool isEqual(GA_Offset cmp) const
257  { return myMap.isEqual(myDestMap, SOwner, cmp); }
259  { return myMap.isAlmostEqual(myDestMap, SOwner, cmp); }
260  /// @}
261 
262  /// @{
263  /// Standard operations read the source and write to the destination.
264  /// However. These operations operate by reading and writing the
265  /// destination.
266  /// @see copyValue(), addValue(), subValue(), lerpValue()
268  { myMap.copyDestValue(myDestMap, SOwner, offset); }
269 
271  { myMap.addDestValue(myDestMap, SOwner, offset); }
272 
274  { myMap.subDestValue(myDestMap, SOwner, offset); }
275 
276  void lerpDest(GA_Offset s0, GA_Offset s1, fpreal t) const
277  {
278  myMap.lerpDestValue(myDestMap,
279  SOwner, s0, s1, t);
280  }
281 
283  GA_Offset offset, fpreal w) const
284  {
285  myMap.addSumDestValue(sum, myDestMap,
286  SOwner, offset, w);
287  }
290  fpreal w) const
291  {
292  myMap.addHSumDestValue(sum, myDestMap,
293  SOwner, offset, w);
294  }
295  /// @}
296 
297  /// Debug statement to dump all the attributes to stdout
298  void dump(const char *msg, ...) const;
299 
300 private:
301 
302  const GA_AttributeRefMap &myMap;
303  /// myDestMap caches topological lookups and so must be mutable. Note
304  /// that we need not concern ourselves with thread-safety as more than
305  /// one thread should not be writing to the bound element anyway.
306  mutable GA_AttributeRefMapOffsetMap myDestMap;
307 };
308 
313 
314 #endif
void zero() const
Zero an attribute. This is equivalent to a weighted sum of 0 elements.
void transform(const UT_DMatrix4 &m, const UT_DMatrix4 &im) const
void transform(const UT_Matrix4 &m, const UT_Matrix4 &im) const
void addH(GA_WeightedSum &sum, GA_Offset source_index, fpreal w) const
void lerp(GA_Offset s0, GA_Offset s1, fpreal t) const
void addHDest(GA_WeightedSum &sum, GA_Offset offset, fpreal w) const
const GLdouble * v
Definition: glcorearb.h:837
void lerpDest(GA_Offset s0, GA_Offset s1, fpreal t) const
void copyExplicitPosition(const UT_Vector4D &p) const
void addDest(GA_Offset offset) const
Context to keep track of weighted sums.
#define GA_API
Definition: GA_API.h:14
void finishSum(const GA_WeightedSum &sum, fpreal normalization=1) const
GA_AttributeRefMapDestHandle< GA_ATTRIB_POINT > GA_AttributeRefMapDestHandlePoint
void startSum(const GA_WeightedSum &sum) const
void copyExplicitPosition(const UT_Vector4 &p) const
void copyDest(GA_Offset offset) const
GA_AttributeRefMapDestHandle< GA_ATTRIB_GLOBAL > GA_AttributeRefMapDestHandleGlobal
#define GA_INVALID_OFFSET
Definition: GA_Types.h:687
GA_AttributeRefMapDestHandle< GA_ATTRIB_VERTEX > GA_AttributeRefMapDestHandleVertex
IMATH_HOSTDEVICE constexpr int cmp(T a, T b) IMATH_NOEXCEPT
Definition: ImathFun.h:84
GA_Size GA_Offset
Definition: GA_Types.h:646
GA_API const UT_StringHolder scale
void homogenize() const
Homogenize any rational attributes of the current element.
GLintptr offset
Definition: glcorearb.h:665
void add(GA_Offset source_index, fpreal scale=1) const
void subDest(GA_Offset offset) const
void copyExplicitPosition(const UT_Vector3 &p) const
bool isAlmostEqual(GA_Offset cmp) const
GA_AttributeRefMapDestHandle(const GA_AttributeRefMap &map)
Minimal constructor.
void add(GA_WeightedSum &sum, GA_Offset source_index, fpreal w) const
GA_AttributeRefMapDestHandle(const GA_AttributeRefMapDestHandle &src)
Copy constructor.
A handle to simplify manipulation of multiple attributes.
void copyExplicitPosition(const UT_Vector2 &p) const
int entries() const
Return the number of attributes in the list.
GLdouble t
Definition: glad.h:2397
void mul(GA_Offset source_index) const
GA_AttributeOwner
Definition: GA_Types.h:35
void copyExplicitPosition(const UT_Vector2D &p) const
void addDest(GA_WeightedSum &sum, GA_Offset offset, fpreal w) const
void barycentric(GA_Offset p0, GA_Offset p1, GA_Offset p2, fpreal u, fpreal v) const
fpreal64 fpreal
Definition: SYS_Types.h:277
void dehomogenize() const
Dehomogenize any rational attributes of the current element.
void copy(GA_Offset offset) const
void copyExplicitPosition(const UT_Vector3D &p) const
A handle to simplify manipulation of multiple attributes.
void startHSum(const GA_WeightedSum &sum) const
void finishHSum(const GA_WeightedSum &sum, fpreal normalization=1) const
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
void multiply(fpreal scale) const
Multiply operation:
void sub(GA_Offset source_index) const
GA_AttributeRefMapDestHandle< GA_ATTRIB_PRIMITIVE > GA_AttributeRefMapDestHandlePrimitive
void bilinear(GA_Offset u0v0, GA_Offset u1v0, GA_Offset u0v1, GA_Offset u1v1, fpreal u, fpreal v) const
void lerpH(GA_Offset s0, GA_Offset s1, fpreal t) const
GLenum src
Definition: glcorearb.h:1793