Skip to content

Instantly share code, notes, and snippets.

@atandrau
atandrau / covariance_matrix.cpp
Created February 28, 2011 11:45
Compute the Covariance Matrix of a 3D Point Cloud
void PointCloud::computeCovarianceMatrix() {
double means[3] = {0, 0, 0};
for (int i = 0; i < points.size(); i++)
means[0] += points[i].x,
means[1] += points[i].y,
means[2] += points[i].z;
means[0] /= points.size(), means[1] /= points.size(), means[2] /= points.size();
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
@atandrau
atandrau / best_fitting_plane.cpp
Created February 28, 2011 11:48
Best Fitting Plane of a 3D Point Cloud with PCA and GSL
Plane PointCloud::bestFittingPlane() {
computeCovarianceMatrix();
gsl_matrix * m = gsl_matrix_alloc(3, 3);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
gsl_matrix_set(m, i, j, covarianceMatrix[i][j]);
gsl_vector *eval = gsl_vector_alloc(3);
gsl_matrix *evec = gsl_matrix_alloc(3, 3);
@atandrau
atandrau / point_plane_distance.cpp
Created February 28, 2011 12:09
Distance from Point (x, y, z) to Plane (ax + by + cz + d = 0)
double Point::distanceTo(Plane p) {
return abs( p.a * x + p.b * y + p.c * z + p.d) /
sqrt(p.a * p.a + p.b * p.b + p.c * p.c);
}
@atandrau
atandrau / planarity.cpp
Created February 28, 2011 12:25
Compute Planarity of a 3D point using approach 1.
double PointCloud::computePlanarity(int pointIndex) {
PointCloud p;
p.points.push_back(points[pointIndex]);
for (int i = 0; i < (int)points.size(); i++) {
if(i != pointIndex && points[i].distanceTo(points[pointIndex]) <= MaxEPS) {
p.points.push_back(points[i]);
}
}
Plane plane = p.bestFittingPlane();