1 #ifndef __GfxTL_COVARIANCE_HEADER__
2 #define __GfxTL_COVARIANCE_HEADER__
9 template <
class MatrixT>
24 template <
class Po
intT>
31 template <
class WeightT,
class Po
intT>
47 if (m_totalWeight > 0)
80 template <
class ScalarT>
95 template <
class Po
intT>
102 template <
class WeightT,
class Po
intT>
112 m_matrix[0][0] += (dummy = oldTotalWeight * diff[0]) * r[0];
113 m_matrix[0][1] += dummy * r[1];
114 m_matrix[0][2] += dummy * r[2];
115 m_matrix[1][1] += (dummy = oldTotalWeight * diff[1]) * r[1];
116 m_matrix[1][2] += dummy * r[2];
117 m_matrix[2][2] += oldTotalWeight * diff[2] * r[2];
123 if (m_totalWeight > 0)
125 (*m)[0][0] = m_matrix[0][0] / m_totalWeight;
126 (*m)[0][1] = m_matrix[0][1] / m_totalWeight;
127 (*m)[0][2] = m_matrix[0][2] / m_totalWeight;
128 (*m)[1][1] = m_matrix[1][1] / m_totalWeight;
129 (*m)[1][2] = m_matrix[1][2] / m_totalWeight;
130 (*m)[2][2] = m_matrix[2][2] / m_totalWeight;
134 (*m)[0][0] = m_matrix[0][0];
135 (*m)[0][1] = m_matrix[0][1];
136 (*m)[0][2] = m_matrix[0][2];
137 (*m)[1][1] = m_matrix[1][1];
138 (*m)[1][2] = m_matrix[1][2];
139 (*m)[2][2] = m_matrix[2][2];
141 (*m)[1][0] = (*m)[0][1];
142 (*m)[2][0] = (*m)[0][2];
143 (*m)[2][1] = (*m)[1][2];
173 template <
class Po
intT,
class Po
intsForwardIt,
class WeightsForwardIt,
class MatrixT>
176 PointsForwardIt begin,
178 WeightsForwardIt weights,
181 typedef typename MatrixT::ScalarType ScalarType;
183 ScalarType totalWeight = 0;
184 for (; begin != end; ++begin, ++weights)
186 typename PointT::TransposedType diffT;
189 diff.Transpose(&diffT);
190 ScalarType w = ScalarType(*weights);
193 MatrixT dd = diff * diffT;
196 *matrix /= totalWeight;
199 template <
class Po
intT,
class Po
intsForwardIt,
class WeightsForwardIt>
202 PointsForwardIt begin,
204 WeightsForwardIt weights,
207 typedef typename PointT::ScalarType ScalarType;
209 ScalarType totalWeight = 0, dummy;
210 for (; begin != end; ++begin, ++weights)
214 ScalarType w = ScalarType(*weights);
215 (*matrix)[0][0] += (dummy = w * diff[0]) * diff[0];
216 (*matrix)[0][1] += dummy * diff[1];
217 (*matrix)[0][2] += dummy * diff[2];
218 (*matrix)[1][1] += (dummy = w * diff[1]) * diff[1];
219 (*matrix)[1][2] += dummy * diff[2];
220 (*matrix)[2][2] += w * diff[2] * diff[2];
223 (*matrix)[1][0] = (*matrix)[0][1];
224 (*matrix)[2][0] = (*matrix)[0][2];
225 (*matrix)[2][1] = (*matrix)[1][2];
226 (*matrix) /= totalWeight;
229 template <
class Po
intT,
class Po
intsForwardIt,
class MatrixT>
232 PointsForwardIt begin,
240 template <
class Po
intsIteratorT,
class WeightsIteratorT,
class Po
intT,
class MatrixT>
244 WeightsIteratorT weights,
249 for (; begin != end; ++begin, ++weights)
251 cov.
Add(*weights, *begin);
257 template <
class Po
intsIteratorT,
class WeightsIteratorT,
class Po
intT,
class ScalarT>
261 WeightsIteratorT weights,
265 typedef typename std::iterator_traits<WeightsIteratorT>::value_type WeightType;
267 typename std::iterator_traits<PointsIteratorT>::value_type>::ScalarType ScalarType;
273 WeightType w, totalWeight, oldTotalWeight;
277 for (++begin, ++weights; begin != end; ++begin, ++weights)
279 oldTotalWeight = totalWeight;
283 for (
unsigned int i = 0; i < 3; ++i)
285 diff[i] = (*begin)[i] - (*mean)[i];
287 ScalarType dummy, ww;
288 ww = ScalarType(oldTotalWeight);
289 PointT r = (ScalarType(w) / ScalarType(totalWeight)) * diff;
291 (*matrix)[0][0] += (dummy = ww * diff[0]) * r[0];
292 (*matrix)[0][1] += dummy * r[1];
293 (*matrix)[0][2] += dummy * r[2];
294 (*matrix)[1][1] += (dummy = ww * diff[1]) * r[1];
295 (*matrix)[1][2] += dummy * r[2];
296 (*matrix)[2][2] += ww * diff[2] * r[2];
298 (*matrix)[0][0] /= ScalarType(totalWeight);
299 (*matrix)[0][1] /= ScalarType(totalWeight);
300 (*matrix)[0][2] /= ScalarType(totalWeight);
301 (*matrix)[1][1] /= ScalarType(totalWeight);
302 (*matrix)[1][2] /= ScalarType(totalWeight);
303 (*matrix)[2][2] /= ScalarType(totalWeight);
304 (*matrix)[1][0] = (*matrix)[0][1];
305 (*matrix)[2][0] = (*matrix)[0][2];
306 (*matrix)[2][1] = (*matrix)[1][2];