1 #ifndef __GfxTL_COVARIANCE_HEADER__
2 #define __GfxTL_COVARIANCE_HEADER__
9 template<
class MatrixT >
23 template<
class Po
intT >
29 template<
class WeightT,
class Po
intT >
43 if (m_totalWeight > 0)
73 template<
class ScalarT >
88 template<
class Po
intT >
94 template<
class WeightT,
class Po
intT >
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];
113 if (m_totalWeight > 0)
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;
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];
131 (*m)[1][0] = (*m)[0][1];
132 (*m)[2][0] = (*m)[0][2];
133 (*m)[2][1] = (*m)[1][2];
160 template<
class PointT,
class PointsForwardIt,
class WeightsForwardIt,
163 PointsForwardIt end, WeightsForwardIt weights, MatrixT* matrix)
165 typedef typename MatrixT::ScalarType ScalarType;
167 ScalarType totalWeight = 0;
168 for (; begin != end; ++begin, ++weights)
170 typename PointT::TransposedType diffT;
173 diff.Transpose(&diffT);
174 ScalarType w = ScalarType(*weights);
177 MatrixT dd = diff * diffT;
180 *matrix /= totalWeight;
183 template<
class Po
intT,
class Po
intsForwardIt,
class WeightsForwardIt >
185 PointsForwardIt end, WeightsForwardIt weights,
188 typedef typename PointT::ScalarType ScalarType;
190 ScalarType totalWeight = 0, dummy;
191 for (; begin != end; ++begin, ++weights)
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];
204 (*matrix)[1][0] = (*matrix)[0][1];
205 (*matrix)[2][0] = (*matrix)[0][2];
206 (*matrix)[2][1] = (*matrix)[1][2];
207 (*matrix) /= totalWeight;
210 template<
class Po
intT,
class Po
intsForwardIt,
class MatrixT >
212 PointsForwardIt end, MatrixT* matrix)
218 template<
class PointsIteratorT,
class WeightsIteratorT,
class PointT,
221 WeightsIteratorT weights,
PointT*
mean, MatrixT* matrix)
224 for (; begin != end; ++begin, ++weights)
226 cov.
Add(*weights, *begin);
232 template<
class PointsIteratorT,
class WeightsIteratorT,
class PointT,
238 typedef typename std::iterator_traits< WeightsIteratorT >
239 ::value_type WeightType;
241 typename std::iterator_traits< PointsIteratorT >::value_type >
242 ::ScalarType ScalarType;
248 WeightType w, totalWeight, oldTotalWeight;
252 for (++begin, ++weights; begin != end; ++begin, ++weights)
254 oldTotalWeight = totalWeight;
258 for (
unsigned int i = 0; i < 3; ++i)
260 diff[i] = (*begin)[i] - (*mean)[i];
262 ScalarType dummy, ww;
263 ww = ScalarType(oldTotalWeight);
264 PointT r = (ScalarType(w) / ScalarType(totalWeight)) * diff;
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];
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];