OrthogonalRegressionPlane.java 3.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.math;

import java.util.List;

import Jama.EigenvalueDecomposition;
import Jama.Matrix;

/**
 * Utility class to calculate regression planes
 * 
 * @author Matthias Betz
 *
 */
public class OrthogonalRegressionPlane {

	private OrthogonalRegressionPlane() {

	}

	/**
	 * calculates a regression plane using the PCA algorithm
	 * 
	 * @param vertices the vertices forming a plane
	 * @return the regression plane
	 */
	public static Plane calculateOrthogonalRegressionPlane(List<? extends Vector3d> vertices) {
		// get the center of all points, the plane has to go through this point
		Vector3d centroid = CovarianceMatrix.getCentroid(vertices);
		EigenvalueDecomposition ed = decompose(vertices, centroid);
		Vector3d eigenvalues = getEigenvalues(ed);
		return calculatePlane(centroid, ed, eigenvalues);
	}

	public static Plane calculatePlane(Vector3d centroid, EigenvalueDecomposition ed, Vector3d eigenvalues) {
		double eig0 = eigenvalues.getX();
		double eig1 = eigenvalues.getY();
		double eig2 = eigenvalues.getZ();
		Matrix v = ed.getV();
		Matrix eigenVector;
Matthias Betz's avatar
Matthias Betz committed
58
		if (eig0 <= eig1 && eig0 <= eig2) {
59
60
			// the first eigenvalue is the lowest
			eigenVector = v.getMatrix(0, 2, 0, 0);
Matthias Betz's avatar
Matthias Betz committed
61
		} else if (eig1 <= eig0 && eig1 <= eig2) {
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
			// the second eigenvalue is the lowest
			eigenVector = v.getMatrix(0, 2, 1, 1);
		} else {
			// the third eigenvalue is the lowest
			eigenVector = v.getMatrix(0, 2, 2, 2);
		}
		// the eigenvector of the lowest eigenvalue is the normal of the plane
		Vector3d normal = new Vector3d(eigenVector.get(0, 0), eigenVector.get(1, 0), eigenVector.get(2, 0));

		return new Plane(normal, centroid);
	}

	public static Vector3d getEigenvalues(EigenvalueDecomposition ed) {
		Matrix eigenValues = ed.getD();
		// get the lowest eigenvalue and the corresponding eigenvector
		double eig0 = eigenValues.get(0, 0);
		double eig1 = eigenValues.get(1, 1);
		double eig2 = eigenValues.get(2, 2);
		return new Vector3d(eig0, eig1, eig2);
	}

	public static EigenvalueDecomposition decompose(List<? extends Vector3d> vertices, Vector3d centroid) {
		// create covariance matrix (3 x 3) out of all used points
		double[][] covMatrixValues = CovarianceMatrix.calculateCovarianceMatrix(vertices, centroid);
		Matrix covMatrix = new Matrix(covMatrixValues);
		// calculate eigenvalues and eigenvectors
		return covMatrix.eig();
	}

}