parry3d/utils/cov.rs
1use crate::math::{Matrix, Point, Real};
2
3/// Computes the covariance matrix of a set of points.
4pub fn cov(pts: &[Point<Real>]) -> Matrix<Real> {
5 center_cov(pts).1
6}
7
8/// Computes the center and the covariance matrix of a set of points.
9pub fn center_cov(pts: &[Point<Real>]) -> (Point<Real>, Matrix<Real>) {
10 let center = crate::utils::center(pts);
11 let mut cov: Matrix<Real> = na::zero();
12 let normalizer: Real = 1.0 / (pts.len() as Real);
13
14 for p in pts.iter() {
15 let cp = *p - center;
16 // NOTE: this is more numerically stable than using cov.syger.
17 cov += cp * (cp * normalizer).transpose();
18 }
19
20 (center, cov)
21}