Mean.h
Go to the documentation of this file.
1 #ifndef __GfxTL_MEAN_HEADER__
2 #define __GfxTL_MEAN_HEADER__
3 #include <GfxTL/WeightFunc.h>
4 #include <GfxTL/Covariance.h>
5 #include <GfxTL/MatrixXX.h>
6 #include <GfxTL/VectorXD.h>
7 #include <GfxTL/Jacobi.h>
8 #include <GfxTL/MathHelper.h>
9 #include <GfxTL/WeightFunc.h>
10 
11 namespace GfxTL
12 {
13  template< class PointT, class PointsForwardIt, class WeightsForwardIt >
14  void Mean(PointsForwardIt begin, PointsForwardIt end,
15  WeightsForwardIt weights, PointT* mean)
16  {
17  mean->Zero();
18  typename PointT::ScalarType totalWeight = 0;
19  for (; begin != end; ++begin, ++weights)
20  {
21  *mean += typename PointT::ScalarType(*weights)
22  * ((const PointT)(*begin));
23  totalWeight += *weights;
24  }
25  if (totalWeight)
26  {
27  *mean /= totalWeight;
28  }
29  }
30 
31  template< class PointsForwardIt, class WeightsForwardIt,
32  class ScalarT >
33  void Mean(PointsForwardIt begin, PointsForwardIt end,
34  WeightsForwardIt weights, VectorXD< 3, ScalarT >* mean)
35  {
36  mean->Zero();
37  ScalarT totalWeight = 0, w;
38  for (; begin != end; ++begin, ++weights)
39  {
40  w = (ScalarT) * weights;
41  (*mean)[0] += w * (*begin)[0];
42  (*mean)[1] += w * (*begin)[1];
43  (*mean)[2] += w * (*begin)[2];
44  totalWeight += w;
45  }
46  if (totalWeight)
47  {
48  *mean /= totalWeight;
49  }
50  }
51 
52  template< class PointT, class PointsForwardIt >
53  void Mean(PointsForwardIt begin, PointsForwardIt end, PointT* mean)
54  {
55  Mean(begin, end, UnitWeightIterator(), mean);
56  }
57 
58  // This computes an average of unoriented normals
59  template< class NormalsItT, class WeightsItT, class MeanT >
60  bool MeanOfNormals(NormalsItT begin, NormalsItT end, WeightsItT weights,
61  MeanT* mean)
62  {
63  typedef typename MeanT::ScalarType ScalarType;
64  enum { Dim = MeanT::Dim };
66  GfxTL::VectorXD< Dim, ScalarType > center, eigenValues;
67  center.Zero();
68  CovarianceMatrix(center, begin, end, weights, &cov);
69  if (!Jacobi(cov, &eigenValues, &eigenVectors))
70  {
71  mean->Zero();
72  return false;
73  }
74  // find the maximal eigenvalue and corresponding vector
75  ScalarType maxEigVal = eigenValues[0];
76  unsigned int maxEigIdx = 0;
77  for (unsigned int i = 1; i < Dim; ++i)
78  if (eigenValues[i] > maxEigVal)
79  {
80  maxEigVal = eigenValues[i];
81  maxEigIdx = i;
82  }
83  *mean = MeanT(eigenVectors[maxEigIdx]);
84  return true;
85  }
86 
87  template< class NormalsItT, class MeanT >
88  bool MeanOfNormals(NormalsItT begin, NormalsItT end, MeanT* mean)
89  {
90  return MeanOfNormals(begin, end, UnitWeightIterator(), mean);
91  }
92 };
93 
94 #endif
GfxTL::VectorXD
Definition: MatrixXX.h:21
WeightFunc.h
GfxTL::MatrixXX
Definition: MatrixXX.h:25
VectorXD.h
GfxTL::VectorXD::Zero
void Zero()
Definition: VectorXD.h:268
GfxTL::Mean
void Mean(PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, PointT *mean)
Definition: Mean.h:14
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1615
GfxTL::Jacobi
bool Jacobi(const MatrixXX< N, N, T > &m, VectorXD< N, T > *d, MatrixXX< N, N, T > *v, int *nrot=NULL)
Definition: Jacobi.h:22
Covariance.h
armarx::PointT
pcl::PointXYZRGBL PointT
Definition: Common.h:28
GfxTL
Definition: AABox.h:8
MatrixXX.h
Jacobi.h
GfxTL::CovarianceMatrix
void CovarianceMatrix(const PointT &center, PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, MatrixT *matrix)
Definition: Covariance.h:162
MathHelper.h
GfxTL::MeanOfNormals
bool MeanOfNormals(NormalsItT begin, NormalsItT end, WeightsItT weights, MeanT *mean)
Definition: Mean.h:60
GfxTL::UnitWeightIterator
Definition: WeightFunc.h:76