HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GA_WeightedSum.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_WeightedSum.h (GA Library, C++)
7  *
8  * COMMENTS:
9  * This class maintains context information while performing a weighted
10  * sum of attribute data.
11  *
12  * An example of implementing linear interpolation using a weighted sum:
13  * GA_WeightedSum sum;
14  * attribute_set.startSum(sum);
15  * attribute_set.addPartial(sum, a, 1-bias);
16  * attribute_set.addPartial(sum, b, bias);
17  * attribute_set.finishSum();
18  */
19 
20 #ifndef __GA_WeightedSum__
21 #define __GA_WeightedSum__
22 
23 #include "GA_API.h"
24 
25 #include <SYS/SYS_Math.h>
26 #include <SYS/SYS_Types.h>
27 
28 
29 /// @brief Context to keep track of weighted sums
30 ///
31 /// Weighted sums are used in metaball blending, TP surfaces and various other
32 /// places. The weighted sum class is used to keep track of progress through
33 /// the weighted sum. The class keeps track of the current, maximum and total
34 /// of the weights. This allows discrete attributes to perform conditional
35 /// copying (on the maximum value), or for normalization at the end of the sum.
37 {
38 public:
39  /// The weighted sum is used to keep track of partial sums.
40  ///
41  /// The @c homogeneous flag will change weighted sums of homogeneous
42  /// attributes (GA_TYPE_HPOINT) so that they will be homogenized before
43  /// summing and de-homogenized at the conclusion of the sum.
44  /// This only affects homogeneous attributes.
45  GA_WeightedSum() { rewind(); }
47 
48  /// Rewind will reset the values of the sum (i.e. the count and weights)
49  void rewind(); // Reset the value
50 
51  /// Advance is called when starting a new iteration. The weight passed in
52  /// is the weight associated with this iteration.
53  void advance(fpreal weight)
54  {
55  myMaxWeight = SYSmax(myMaxWeight, myCurrentWeight);
56  myCurrentWeight = weight;
57  myTotalWeight += weight;
58  myCount++;
59  }
60 
61  /// Finish is called after the last partial sum has been called. This lets
62  /// the max-weight get set correctly.
63  void finish();
64 
65  /// The partial index runs from 0...N
66  /// On the call to initSum(), the value will be -1, on the call to
67  /// finishSum(), the value will be N-1.
68  int getPartialIndex() const { return myCount; }
69 
70  /// Like the partial index, but returns the "count". That is, on
71  /// finishSum(), this method will return the count of the iterations.
72  int getIterationCount() const { return myCount+1; }
73 
74  /// Get the current weight to sum
75  fpreal getCurrentWeight() const { return myCurrentWeight; }
76 
77  /// Get the sum of the weights for all iterations (for normalization)
78  fpreal getTotalWeight() const { return myTotalWeight; }
79 
80  /// A convenince method for conditional copying of the maximum weight
81  bool getIsMaxWeight() const { return myCurrentWeight>myMaxWeight; }
82 
83 private:
84  fpreal myMaxWeight; // My maximum weight
85  fpreal myCurrentWeight; // My current weight
86  fpreal myTotalWeight; // Total weight over all iterations
87  int myCount; // My iteration count
88 };
89 
90 #endif
91 
#define SYSmax(a, b)
Definition: SYS_Math.h:1365
bool getIsMaxWeight() const
A convenince method for conditional copying of the maximum weight.
int getPartialIndex() const
fpreal getCurrentWeight() const
Get the current weight to sum.
Context to keep track of weighted sums.
#define GA_API
Definition: GA_API.h:12
fpreal getTotalWeight() const
Get the sum of the weights for all iterations (for normalization)
double fpreal
Definition: SYS_Types.h:269
void advance(fpreal weight)
int getIterationCount() const