23 #include <vnl/vnl_error.h> 24 #include <vnl/vnl_matrix.h> 25 #include <vnl/vnl_vector_fixed.h> 26 #include <vnl/algo/vnl_matrix_inverse.h> 31 #include <vtkPoints.h> 34 #include <milxGlobal.h> 43 template<
class Type =
double>
53 static Type Centroid(
const vnl_vector<Type> &data);
59 static vnl_vector<Type> Centroid(
const vnl_matrix<Type> &data);
63 static Type CentroidSize(
const vnl_matrix<Type> &data,
const vnl_vector<Type> ¢roid,
bool norm =
false);
65 static vnl_vector<Type> Centroid(vtkPoints *points);
75 static Type CentroidSize(vtkPoints *points,
const vnl_vector<Type> ¢roid,
bool norm =
false);
79 static Type* Centroid(vtkPoints *points);
89 static Type CentroidSize(vtkPoints *points,
const Type* centroid,
bool norm =
false);
93 static vnl_matrix<Type> CovarianceMatrix(vnl_vector<Type> sourceVector);
102 static vnl_matrix<Type> CovarianceMatrix(vnl_vector<Type> sourceVector, vnl_vector<Type> targetVector);
109 static vnl_matrix<Type> CovarianceMatrix(
const vnl_matrix<Type> &data,
const vnl_vector<Type> ¢roid);
116 static vnl_matrix<Type> CovarianceMatrix(
const vnl_matrix<Type> &data);
118 static vnl_matrix<Type> CovarianceMatrix(vtkPoints *points,
const vnl_vector<Type> ¢roid);
130 static Type MahalanobisDistance(
const vnl_vector<Type> &source,
const vnl_vector<Type> &target);
144 static Type MahalanobisDistance(
const vnl_vector<Type> &target,
const vnl_vector<Type> &mean,
const vnl_matrix<Type> &invCovMatrix);
152 static Type MahalanobisDistance(
const vnl_vector<Type> &target,
const vnl_vector<Type> &mean,
const vnl_matrix<Type> &covMatrix, vnl_matrix<Type> &invCovMatrix);
161 static Type MeanSquaredError(vtkPoints *sourcePoints, vtkPoints *targetPoints);
179 const vnl_size_t n = data.size();
182 for (vnl_size_t j = 0; j < n; j ++)
192 const vnl_size_t n = data.rows();
193 const vnl_size_t m = data.cols();
194 vnl_vector<Type> centroid(m, 0.0);
196 for (vnl_size_t j = 0; j < n; j ++)
198 centroid += data.get_row(j);
208 const vnl_size_t n = data.rows();
211 for (
int j = 0; j < n; j ++)
213 vnl_vector<Type> rowVector( data.get_row(j) );
216 newScale += vtkMath::Distance2BetweenPoints(rowVector.data_block(), centroid.data_block());
222 return sqrt(newScale);
229 const vtkIdType n = points->GetNumberOfPoints();
230 vnl_vector_fixed<Type, 3> centroid(0.0);
232 for (vtkIdType j = 0; j < n; j ++)
234 vnl_vector_fixed<Type, 3> location( points->GetPoint(j) );
236 centroid += location;
240 return centroid.as_vector();
248 for (vtkIdType j = 0; j < points->GetNumberOfPoints(); j ++)
250 vnl_vector_fixed<Type, 3> location( points->GetPoint(j) );
253 newScale += vtkMath::Distance2BetweenPoints(location.data_block(), centroid.data_block());
257 newScale /= points->GetNumberOfPoints();
259 return sqrt(newScale);
264 #ifdef VTK_ONLY //Code that does not depend on VNL 268 const vtkIdType n = points->GetNumberOfPoints();
270 Type *centroid =
new Type[3];
272 for (vtkIdType j = 0; j < n; j ++)
276 points->GetPoint(j, location);
278 centroid[0] += location[0]/n;
279 centroid[1] += location[1]/n;
280 centroid[2] += location[2]/n;
291 for (vtkIdType j = 0; j < points->GetNumberOfPoints(); j ++)
295 points->GetPoint(j, location);
298 newScale += vtkMath::Distance2BetweenPoints(location, centroid);
302 newScale /= points->GetNumberOfPoints();
304 return sqrt(newScale);
312 const vnl_size_t n = sourceVector.size();
314 sourceVector -= sourceVector.mean();
316 vnl_matrix<Type> covarianceMatrix(n, n, 0.0);
317 covarianceMatrix += outer_product(sourceVector, sourceVector);
319 return covarianceMatrix;
325 const vnl_size_t n = sourceVector.size();
326 const vnl_size_t m = targetVector.size();
330 vnl_error_vector_dimension (
"Math<Type>::CovarianceMatrix", m, n);
333 sourceVector -= sourceVector.mean();
334 targetVector -= targetVector.mean();
336 vnl_matrix<Type> covarianceMatrix(n, n, 0.0);
337 covarianceMatrix += outer_product(sourceVector, targetVector);
339 return covarianceMatrix;
345 const vnl_size_t n = data.rows();
346 const vnl_size_t m = data.cols();
352 if (m != centroid.size())
354 vnl_error_vector_dimension (
"Math<Type>::CovarianceMatrix", m, centroid.size());
357 vnl_matrix<Type> covarianceMatrix(m, m, 0.0);
358 for (vnl_size_t j = 0; j < n; j ++)
360 vnl_vector<Type> rowVector(data.get_row(j));
362 rowVector -= centroid;
364 covarianceMatrix += outer_product(rowVector, rowVector) / norm;
367 return covarianceMatrix;
373 const vnl_size_t n = data.rows();
374 const vnl_size_t m = data.cols();
380 vnl_matrix<Type> covarianceMatrix(m, m, 0.0);
381 for (vnl_size_t j = 0; j < n; j ++)
383 vnl_vector<Type> rowVector(data.get_row(j));
385 covarianceMatrix += outer_product(rowVector, rowVector) / norm;
388 return covarianceMatrix;
395 const vnl_size_t n = points->GetNumberOfPoints();
401 if (centroid.size() != 3)
403 vnl_error_vector_dimension (
"Math<Type>::CovarianceMatrix", 3, centroid.size());
406 vnl_matrix<Type> covarianceMatrix(3, 3, 0.0);
407 for (vnl_size_t j = 0; j < n; j ++)
409 vnl_vector_fixed<Type, 3> location( points->GetPoint(j) );
411 location -= centroid;
413 covarianceMatrix += outer_product(location.as_vector(), location.as_vector()) / norm;
416 return covarianceMatrix;
425 const vnl_size_t n = source.size();
426 const vnl_size_t m = target.size();
431 vnl_error_vector_dimension (
"Math<Type>::MahalanobisDistance", n, m);
448 vnl_matrix<Type> dataMatrix(n, 2, 0.0);
449 dataMatrix.set_column(0, target);
450 dataMatrix.set_column(1, source);
491 vnl_matrix<Type> dataDifference = dataMatrix;
498 vnl_matrix<Type> dataInvCovariance = vnl_matrix_inverse<Type>(dataCovariance);
502 const vnl_matrix<Type> distance = dataDifference*dataInvCovariance*dataDifference.transpose();
506 return 1.0/distance(0,0);
512 const vnl_size_t n = target.size();
513 const vnl_size_t m = mean.size();
518 vnl_error_vector_dimension (
"Math<Type>::MahalanobisDistance", n, m);
521 vnl_matrix<Type> dataDifference(n, 1, 0.0);
522 dataDifference.set_column(0, target - mean);
524 const vnl_matrix<Type> distance = dataDifference.transpose()*invCovMatrix*dataDifference;
526 return distance(0,0);
532 vnl_matrix<Type> dataInvCovariance = vnl_matrix_inverse<Type>(covMatrix);
534 invCovMatrix = dataInvCovariance;
536 return MahalanobisDistance(target, mean, invCovMatrix);
543 const vnl_size_t numberOfPoints = sourcePoints->GetNumberOfPoints();
546 for (vnl_size_t v = 0; v < numberOfPoints; v ++)
548 coordinate p( sourcePoints->GetPoint(v) );
549 coordinate p2( targetPoints->GetPoint(v) );
552 mse += vtkMath::Distance2BetweenPoints(p.data_block(), p2.data_block());
554 mse /= numberOfPoints;
561 #ifdef VTK_ONLY //Code that does not depend on VNL 565 const size_t numberOfPoints = sourcePoints->GetNumberOfPoints();
568 for (
size_t v = 0; v < numberOfPoints; v ++)
571 sourcePoints->GetPoint(v, p);
572 targetPoints->GetPoint(v, p2);
575 mse += vtkMath::Distance2BetweenPoints(p, p2)/numberOfPoints;
584 #endif //__MILXMATH_H static Type Centroid(const vnl_vector< Type > &data)
Compute the centroid (or the mean) of a data vector.
static Type MeanSquaredError(vtkPoints *sourcePoints, vtkPoints *targetPoints)
Compute the MSE of the given points sets.
A general math object for computing common operations such a centroids etc.
#define SMILI_EXPORT
DLL Function Symbol for Windows. It is empty for other OSes.
static vnl_matrix< Type > CovarianceMatrix(vnl_vector< Type > sourceVector)
Compute the covariance matrix from a vector. EXPERIMENTAL.
static Type CentroidSize(const vnl_matrix< Type > &data, const vnl_vector< Type > ¢roid, bool norm=false)
Compute the centroid size or scale for a series of points.
static Type MahalanobisDistance(const vnl_vector< Type > &source, const vnl_vector< Type > &target)
Compute the Mahalanobis distance between two vectors with an unknown covariance matrix. EXPERIMENTAL.