Covariance.h
Go to the documentation of this file.
1 #ifndef __GfxTL_COVARIANCE_HEADER__
2 #define __GfxTL_COVARIANCE_HEADER__
3 #include <GfxTL/WeightFunc.h>
5 #include <GfxTL/VectorXD.h>
6 
7 namespace GfxTL
8 {
9  template< class MatrixT >
11  {
12  public:
13  typedef typename MatrixT::ScalarType ScalarType;
14  typedef MatrixT MatrixType;
17  {
18  m_matrix.Zero();
19  m_mean.Zero();
20  m_totalWeight = 0;
21  }
22 
23  template< class PointT >
24  void Add(const PointT& p)
25  {
26  Add(1, p);
27  }
28 
29  template< class WeightT, class PointT >
30  void Add(WeightT w, const PointT& p)
31  {
32  ScalarType oldTotalWeight = m_totalWeight;
33  m_totalWeight += ScalarType(w);
34  PointType diff = p - m_mean;
35  PointType r = (ScalarType(w) / ScalarType(m_totalWeight)) * diff;
36  m_mean += r;
37  m_matrix += OuterProduct(oldTotalWeight * r, diff);
38  }
39 
40  void Matrix(MatrixT* m) const
41  {
42  *m = m_matrix;
43  if (m_totalWeight > 0)
44  {
45  *m /= m_totalWeight;
46  }
47  }
48 
49  void Mean(PointType* mean) const
50  {
51  *mean = m_mean;
52  }
53 
54  void MeanAndMatrix(PointType* mean, MatrixT* m) const
55  {
56  Mean(mean);
57  Matrix(m);
58  }
59 
60  void Reset()
61  {
62  m_matrix.Zero();
63  m_mean.Zero();
64  m_totalWeight = 0;
65  }
66 
67  private:
68  MatrixType m_matrix;
69  PointType m_mean;
70  ScalarType m_totalWeight;
71  };
72 
73  template< class ScalarT >
74  class IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >
75  {
76  public:
77  typedef ScalarT ScalarType;
80 
82  {
83  m_matrix.Zero();
84  m_mean.Zero();
85  m_totalWeight = 0;
86  }
87 
88  template< class PointT >
89  void Add(const PointT& p)
90  {
91  Add(1, p);
92  }
93 
94  template< class WeightT, class PointT >
95  void Add(WeightT w, const PointT& p)
96  {
97  ScalarType oldTotalWeight = m_totalWeight;
98  m_totalWeight += ScalarType(w);
99  PointType diff = p - m_mean;
100  ScalarType dummy;
101  PointType r = (ScalarType(w) / m_totalWeight) * diff;
102  m_mean += r;
103  m_matrix[0][0] += (dummy = oldTotalWeight * diff[0]) * r[0];
104  m_matrix[0][1] += dummy * r[1];
105  m_matrix[0][2] += dummy * r[2];
106  m_matrix[1][1] += (dummy = oldTotalWeight * diff[1]) * r[1];
107  m_matrix[1][2] += dummy * r[2];
108  m_matrix[2][2] += oldTotalWeight * diff[2] * r[2];
109  }
110 
111  void Matrix(MatrixType* m) const
112  {
113  if (m_totalWeight > 0)
114  {
115  (*m)[0][0] = m_matrix[0][0] / m_totalWeight;
116  (*m)[0][1] = m_matrix[0][1] / m_totalWeight;
117  (*m)[0][2] = m_matrix[0][2] / m_totalWeight;
118  (*m)[1][1] = m_matrix[1][1] / m_totalWeight;
119  (*m)[1][2] = m_matrix[1][2] / m_totalWeight;
120  (*m)[2][2] = m_matrix[2][2] / m_totalWeight;
121  }
122  else
123  {
124  (*m)[0][0] = m_matrix[0][0];
125  (*m)[0][1] = m_matrix[0][1];
126  (*m)[0][2] = m_matrix[0][2];
127  (*m)[1][1] = m_matrix[1][1];
128  (*m)[1][2] = m_matrix[1][2];
129  (*m)[2][2] = m_matrix[2][2];
130  }
131  (*m)[1][0] = (*m)[0][1];
132  (*m)[2][0] = (*m)[0][2];
133  (*m)[2][1] = (*m)[1][2];
134  }
135 
136  void Mean(PointType* mean) const
137  {
138  *mean = m_mean;
139  }
140 
142  {
143  Mean(mean);
144  Matrix(m);
145  }
146 
147  void Reset()
148  {
149  m_matrix.Zero();
150  m_mean.Zero();
151  m_totalWeight = 0;
152  }
153 
154  private:
155  MatrixType m_matrix;
156  PointType m_mean;
157  ScalarType m_totalWeight;
158  };
159 
160  template< class PointT, class PointsForwardIt, class WeightsForwardIt,
161  class MatrixT >
162  void CovarianceMatrix(const PointT& center, PointsForwardIt begin,
163  PointsForwardIt end, WeightsForwardIt weights, MatrixT* matrix)
164  {
165  typedef typename MatrixT::ScalarType ScalarType;
166  matrix->Zero();
167  ScalarType totalWeight = 0;
168  for (; begin != end; ++begin, ++weights)
169  {
170  typename PointT::TransposedType diffT;
171  PointT diff = ((PointT)(*begin));
172  diff -= center;
173  diff.Transpose(&diffT);
174  ScalarType w = ScalarType(*weights);
175  totalWeight += w;
176  diff *= w;
177  MatrixT dd = diff * diffT;
178  *matrix += dd;
179  }
180  *matrix /= totalWeight;
181  }
182 
183  template< class PointT, class PointsForwardIt, class WeightsForwardIt >
184  void CovarianceMatrix(const PointT& center, PointsForwardIt begin,
185  PointsForwardIt end, WeightsForwardIt weights,
187  {
188  typedef typename PointT::ScalarType ScalarType;
189  matrix->Zero();
190  ScalarType totalWeight = 0, dummy;
191  for (; begin != end; ++begin, ++weights)
192  {
193  PointT diff = PointT(*begin);
194  diff -= center;
195  ScalarType w = ScalarType(*weights);
196  (*matrix)[0][0] += (dummy = w * diff[0]) * diff[0];
197  (*matrix)[0][1] += dummy * diff[1];
198  (*matrix)[0][2] += dummy * diff[2];
199  (*matrix)[1][1] += (dummy = w * diff[1]) * diff[1];
200  (*matrix)[1][2] += dummy * diff[2];
201  (*matrix)[2][2] += w * diff[2] * diff[2];
202  totalWeight += w;
203  }
204  (*matrix)[1][0] = (*matrix)[0][1];
205  (*matrix)[2][0] = (*matrix)[0][2];
206  (*matrix)[2][1] = (*matrix)[1][2];
207  (*matrix) /= totalWeight;
208  }
209 
210  template< class PointT, class PointsForwardIt, class MatrixT >
211  void CovarianceMatrix(const PointT& center, PointsForwardIt begin,
212  PointsForwardIt end, MatrixT* matrix)
213  {
214  CovarianceMatrix(center, begin, end, UnitWeightIterator(), matrix);
215  }
216 
217  // one pass covariance
218  template< class PointsIteratorT, class WeightsIteratorT, class PointT,
219  class MatrixT >
220  void CovarianceMatrix(PointsIteratorT begin, PointsIteratorT end,
221  WeightsIteratorT weights, PointT* mean, MatrixT* matrix)
222  {
224  for (; begin != end; ++begin, ++weights)
225  {
226  cov.Add(*weights, *begin);
227  }
228  cov.MeanAndMatrix(mean, matrix);
229  }
230 
231  // one pass covariance (optimized for 3x3 matrix)
232  template< class PointsIteratorT, class WeightsIteratorT, class PointT,
233  class ScalarT >
234  void CovarianceMatrix(PointsIteratorT begin, PointsIteratorT end,
235  WeightsIteratorT weights, PointT* mean,
237  {
238  typedef typename std::iterator_traits< WeightsIteratorT >
239  ::value_type WeightType;
240  typedef typename ScalarTypeDeferer <
241  typename std::iterator_traits< PointsIteratorT >::value_type >
242  ::ScalarType ScalarType;
243  matrix->Zero();
244  if (begin == end)
245  {
246  return;
247  }
248  WeightType w, totalWeight, oldTotalWeight;
249  w = *weights;
250  *mean = *begin;
251  totalWeight = w;
252  for (++begin, ++weights; begin != end; ++begin, ++weights)
253  {
254  oldTotalWeight = totalWeight;
255  w = *weights;
256  totalWeight += w;
257  PointT diff;
258  for (unsigned int i = 0; i < 3; ++i)
259  {
260  diff[i] = (*begin)[i] - (*mean)[i];
261  }
262  ScalarType dummy, ww;
263  ww = ScalarType(oldTotalWeight);
264  PointT r = (ScalarType(w) / ScalarType(totalWeight)) * diff;
265  *mean += r;
266  (*matrix)[0][0] += (dummy = ww * diff[0]) * r[0];
267  (*matrix)[0][1] += dummy * r[1];
268  (*matrix)[0][2] += dummy * r[2];
269  (*matrix)[1][1] += (dummy = ww * diff[1]) * r[1];
270  (*matrix)[1][2] += dummy * r[2];
271  (*matrix)[2][2] += ww * diff[2] * r[2];
272  }
273  (*matrix)[0][0] /= ScalarType(totalWeight);
274  (*matrix)[0][1] /= ScalarType(totalWeight);
275  (*matrix)[0][2] /= ScalarType(totalWeight);
276  (*matrix)[1][1] /= ScalarType(totalWeight);
277  (*matrix)[1][2] /= ScalarType(totalWeight);
278  (*matrix)[2][2] /= ScalarType(totalWeight);
279  (*matrix)[1][0] = (*matrix)[0][1];
280  (*matrix)[2][0] = (*matrix)[0][2];
281  (*matrix)[2][1] = (*matrix)[1][2];
282  }
283 };
284 
285 #endif
GfxTL::IncrementalCovarianceMatrix::Add
void Add(const PointT &p)
Definition: Covariance.h:24
GfxTL::VectorXD< MatrixT::Rows, ScalarType >
GfxTL::IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >::MatrixType
MatrixXX< 3, 3, ScalarT > MatrixType
Definition: Covariance.h:78
GfxTL::ScalarTypeDeferer
Definition: ScalarTypeDeferer.h:12
GfxTL::OuterProduct
MatrixXX< B, A, T > OuterProduct(const VectorXD< A, T > &a, const VectorXD< B, T > &b)
Definition: VectorXD.h:466
WeightFunc.h
GfxTL::MatrixXX
Definition: MatrixXX.h:25
GfxTL::IncrementalCovarianceMatrix::Matrix
void Matrix(MatrixT *m) const
Definition: Covariance.h:40
VectorXD.h
GfxTL::IncrementalCovarianceMatrix
Definition: Covariance.h:10
GfxTL::IncrementalCovarianceMatrix::Mean
void Mean(PointType *mean) const
Definition: Covariance.h:49
GfxTL::VectorXD::Zero
void Zero()
Definition: VectorXD.h:268
GfxTL::IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >::IncrementalCovarianceMatrix
IncrementalCovarianceMatrix()
Definition: Covariance.h:81
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1615
armarx::PointT
pcl::PointXYZRGBL PointT
Definition: Common.h:28
GfxTL::IncrementalCovarianceMatrix::ScalarType
MatrixT::ScalarType ScalarType
Definition: Covariance.h:13
GfxTL::IncrementalCovarianceMatrix::IncrementalCovarianceMatrix
IncrementalCovarianceMatrix()
Definition: Covariance.h:16
GfxTL
Definition: AABox.h:8
GfxTL::IncrementalCovarianceMatrix::MatrixType
MatrixT MatrixType
Definition: Covariance.h:14
GfxTL::IncrementalCovarianceMatrix::Add
void Add(WeightT w, const PointT &p)
Definition: Covariance.h:30
GfxTL::IncrementalCovarianceMatrix::Reset
void Reset()
Definition: Covariance.h:60
GfxTL::IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >::ScalarType
ScalarT ScalarType
Definition: Covariance.h:77
GfxTL::CovarianceMatrix
void CovarianceMatrix(const PointT &center, PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, MatrixT *matrix)
Definition: Covariance.h:162
GfxTL::IncrementalCovarianceMatrix::MeanAndMatrix
void MeanAndMatrix(PointType *mean, MatrixT *m) const
Definition: Covariance.h:54
GfxTL::IncrementalCovarianceMatrix::PointType
VectorXD< MatrixT::Rows, ScalarType > PointType
Definition: Covariance.h:15
GfxTL::IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >::Matrix
void Matrix(MatrixType *m) const
Definition: Covariance.h:111
GfxTL::IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >::MeanAndMatrix
void MeanAndMatrix(PointType *mean, MatrixType *m) const
Definition: Covariance.h:141
GfxTL::IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >::Add
void Add(WeightT w, const PointT &p)
Definition: Covariance.h:95
ScalarTypeDeferer.h
GfxTL::IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >::Reset
void Reset()
Definition: Covariance.h:147
GfxTL::IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >::PointType
VectorXD< 3, ScalarType > PointType
Definition: Covariance.h:79
GfxTL::UnitWeightIterator
Definition: WeightFunc.h:76
GfxTL::IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >::Mean
void Mean(PointType *mean) const
Definition: Covariance.h:136
GfxTL::IncrementalCovarianceMatrix< MatrixXX< 3, 3, ScalarT > >::Add
void Add(const PointT &p)
Definition: Covariance.h:89