Mean.h
Go to the documentation of this file.
1#ifndef __GfxTL_MEAN_HEADER__
2#define __GfxTL_MEAN_HEADER__
3#include <GfxTL/Covariance.h>
4#include <GfxTL/Jacobi.h>
5#include <GfxTL/MathHelper.h>
6#include <GfxTL/MatrixXX.h>
7#include <GfxTL/VectorXD.h>
8#include <GfxTL/WeightFunc.h>
9
10namespace GfxTL
11{
12 template <class PointT, class PointsForwardIt, class WeightsForwardIt>
13 void
14 Mean(PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, PointT* mean)
15 {
16 mean->Zero();
17 typename PointT::ScalarType totalWeight = 0;
18 for (; begin != end; ++begin, ++weights)
19 {
20 *mean += typename PointT::ScalarType(*weights) * ((const PointT)(*begin));
21 totalWeight += *weights;
22 }
23 if (totalWeight)
24 {
25 *mean /= totalWeight;
26 }
27 }
28
29 template <class PointsForwardIt, class WeightsForwardIt, class ScalarT>
30 void
31 Mean(PointsForwardIt begin,
32 PointsForwardIt end,
33 WeightsForwardIt weights,
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
54 Mean(PointsForwardIt begin, PointsForwardIt end, PointT* mean)
55 {
56 Mean(begin, end, UnitWeightIterator(), mean);
57 }
58
59 // This computes an average of unoriented normals
60 template <class NormalsItT, class WeightsItT, class MeanT>
61 bool
62 MeanOfNormals(NormalsItT begin, NormalsItT end, WeightsItT weights, MeanT* mean)
63 {
64 typedef typename MeanT::ScalarType ScalarType;
65
66 enum
67 {
68 Dim = MeanT::Dim
69 };
70
72 GfxTL::VectorXD<Dim, ScalarType> center, eigenValues;
73 center.Zero();
74 CovarianceMatrix(center, begin, end, weights, &cov);
75 if (!Jacobi(cov, &eigenValues, &eigenVectors))
76 {
77 mean->Zero();
78 return false;
79 }
80 // find the maximal eigenvalue and corresponding vector
81 ScalarType maxEigVal = eigenValues[0];
82 unsigned int maxEigIdx = 0;
83 for (unsigned int i = 1; i < Dim; ++i)
84 if (eigenValues[i] > maxEigVal)
85 {
86 maxEigVal = eigenValues[i];
87 maxEigIdx = i;
88 }
89 *mean = MeanT(eigenVectors[maxEigIdx]);
90 return true;
91 }
92
93 template <class NormalsItT, class MeanT>
94 bool
95 MeanOfNormals(NormalsItT begin, NormalsItT end, MeanT* mean)
96 {
97 return MeanOfNormals(begin, end, UnitWeightIterator(), mean);
98 }
99}; // namespace GfxTL
100
101#endif
Definition AABox.h:10
void CovarianceMatrix(const PointT &center, PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, MatrixT *matrix)
Definition Covariance.h:175
void Mean(PointsForwardIt begin, PointsForwardIt end, WeightsForwardIt weights, PointT *mean)
Definition Mean.h:14
bool Jacobi(const MatrixXX< N, N, T > &m, VectorXD< N, T > *d, MatrixXX< N, N, T > *v, int *nrot=NULL)
Definition Jacobi.h:24
bool MeanOfNormals(NormalsItT begin, NormalsItT end, WeightsItT weights, MeanT *mean)
Definition Mean.h:62