PlanarCheck.java 9.29 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
/*-
 *  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.checks.geometry;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import Jama.EigenvalueDecomposition;
import de.hft.stuttgart.citydoctor2.check.Check;
import de.hft.stuttgart.citydoctor2.check.CheckError;
import de.hft.stuttgart.citydoctor2.check.CheckId;
import de.hft.stuttgart.citydoctor2.check.CheckResult;
import de.hft.stuttgart.citydoctor2.check.CheckType;
import de.hft.stuttgart.citydoctor2.check.Checkable;
import de.hft.stuttgart.citydoctor2.check.DefaultParameter;
import de.hft.stuttgart.citydoctor2.check.ResultStatus;
import de.hft.stuttgart.citydoctor2.check.Unit;
Matthias Betz's avatar
Matthias Betz committed
36
import de.hft.stuttgart.citydoctor2.check.error.NonPlanarPolygonDistancePlaneError;
Matthias Betz's avatar
Matthias Betz committed
37
import de.hft.stuttgart.citydoctor2.check.error.NonPlanarPolygonNormalsDeviation;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import de.hft.stuttgart.citydoctor2.check.error.TinyEdgeError;
import de.hft.stuttgart.citydoctor2.datastructure.LinearRing;
import de.hft.stuttgart.citydoctor2.datastructure.Polygon;
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;
import de.hft.stuttgart.citydoctor2.math.CovarianceMatrix;
import de.hft.stuttgart.citydoctor2.math.OrthogonalRegressionPlane;
import de.hft.stuttgart.citydoctor2.math.Plane;
import de.hft.stuttgart.citydoctor2.math.Triangle3d;
import de.hft.stuttgart.citydoctor2.math.Vector3d;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;
import de.hft.stuttgart.citydoctor2.tesselation.JoglTesselator;
import de.hft.stuttgart.citydoctor2.tesselation.TesselatedPolygon;

/**
 * Check class to check for planarity issues
 * 
 * @author Matthias Betz
 *
 */
public class PlanarCheck extends Check {

	private static final String DISTANCE = "distance";
Matthias Betz's avatar
Matthias Betz committed
60
61
	private static final String DISTANCE_TOLERANCE = "distanceTolerance";
	private static final String ANGLE_TOLERANCE = "angleTolerance";
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	private static final String TYPE = "type";
	private static final String TINY_EDGE_TOLERANCE = "tinyEdgeTolerance";

	private static final List<CheckId> dependencies;
	private static final List<Class<? extends Checkable>> applicableToClasses;
	private static final List<DefaultParameter> defaultParameters;

	static {
		ArrayList<CheckId> deps = new ArrayList<>(4);
		deps.add(CheckId.C_GE_R_TOO_FEW_POINTS);
		deps.add(CheckId.C_GE_R_NOT_CLOSED);
		deps.add(CheckId.C_GE_R_DUPLICATE_POINT);
		deps.add(CheckId.C_GE_R_SELF_INTERSECTION);
		dependencies = Collections.unmodifiableList(deps);

		ArrayList<Class<? extends Checkable>> classes = new ArrayList<>(1);
		classes.add(Polygon.class);
		applicableToClasses = Collections.unmodifiableList(classes);

		ArrayList<DefaultParameter> defParameters = new ArrayList<>(3);
		defParameters.add(new DefaultParameter(TYPE, DISTANCE, Unit.NONE));
Matthias Betz's avatar
Matthias Betz committed
83
84
		defParameters.add(new DefaultParameter(DISTANCE_TOLERANCE, "0.01", Unit.METER));
		defParameters.add(new DefaultParameter(ANGLE_TOLERANCE, "1", Unit.DEGREE));
85
86
87
88
89
90
91
		defParameters.add(new DefaultParameter(TINY_EDGE_TOLERANCE, "0.00000", Unit.METER));
		defaultParameters = Collections.unmodifiableList(defParameters);

	}

	private String planarCheckType = DISTANCE;

Matthias Betz's avatar
Matthias Betz committed
92
	private double rad = Math.toRadians(1);
93
94
95
96
97
98
99
100
101
102
	private double delta = 0.01;
	private double tinyEdgeTolerance = 0.00000;

	public PlanarCheck() {
		super(CheckId.C_GE_P_NON_PLANAR);
	}

	@Override
	public void init(Map<String, String> parameters, ParserConfiguration config) {
		if (parameters.containsKey(TYPE)) {
Matthias Betz's avatar
Matthias Betz committed
103
			planarCheckType = parameters.get(TYPE).toLowerCase();
104
105
106
		} else {
			planarCheckType = DISTANCE;
		}
Matthias Betz's avatar
Matthias Betz committed
107
108
		if (parameters.containsKey(ANGLE_TOLERANCE)) {
			rad = Math.toRadians(Double.parseDouble(parameters.get(ANGLE_TOLERANCE)));
109
		} else {
Matthias Betz's avatar
Matthias Betz committed
110
			rad = Math.toRadians(1);
111
		}
Matthias Betz's avatar
Matthias Betz committed
112
113
		if (parameters.containsKey(DISTANCE_TOLERANCE)) {
			delta = Double.parseDouble(parameters.get(DISTANCE_TOLERANCE));
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
		} else {
			delta = 0.01;
		}
		if (parameters.containsKey(TINY_EDGE_TOLERANCE)) {
			tinyEdgeTolerance = Double.parseDouble(parameters.get(TINY_EDGE_TOLERANCE));
		} else {
			tinyEdgeTolerance = 0.00002;
		}
	}

	@Override
	public List<DefaultParameter> getDefaultParameter() {
		return defaultParameters;
	}

	@Override
	public void check(Polygon p) {
		if (DISTANCE.equals(planarCheckType)) {
			planarDistance(p);
		} else if ("angle".equals(planarCheckType)) {
			// check for tiny edge as well
			// store all used points in temporary list
			ArrayList<Vertex> vertices = collectVertices(p);
			Vector3d centroid = CovarianceMatrix.getCentroid(vertices);
			EigenvalueDecomposition ed = OrthogonalRegressionPlane.decompose(vertices, centroid);
			Vector3d eigenvalues = OrthogonalRegressionPlane.getEigenvalues(ed);
			if (checkEigenvalues(p, eigenvalues)) {
				// found tiny edge error, abort further checking
				return;
			}
			planarNormalDeviation(p);
		} else if ("both".equals(planarCheckType)) {
			planarDistance(p);
			planarNormalDeviation(p);
		} else {
			throw new IllegalStateException("Illegal planar check type was given: " + planarCheckType
					+ "\nChoose one of: distance, angle, both");
		}
	}

	private void planarNormalDeviation(Polygon p) {
		TesselatedPolygon tp = JoglTesselator.tesselatePolygon(p);
		ArrayList<Vector3d> normals = new ArrayList<>();
		for (Triangle3d t : tp.getTriangles()) {
			Vector3d normal = t.getNormal();
			normals.add(normal);
		}
		Vector3d averageNormal = calculateAverageNormal(normals);
		averageNormal.normalize();
		for (Vector3d normal : normals) {
			normal.normalize();
			double deviation = normal.dot(averageNormal);
			double radiant = Math.acos(deviation);
			if (radiant > rad) {
Matthias Betz's avatar
Matthias Betz committed
168
				CheckError err = new NonPlanarPolygonNormalsDeviation(p, radiant);
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
				CheckResult cr = new CheckResult(this, ResultStatus.ERROR, err);
				p.addCheckResult(cr);
				return;
			}
		}
		CheckResult cr = p.getCheckResult(this.getCheckId());
		// only change check result if missing, otherwise might override error from
		// distance check
		if (cr == null) {
			p.addCheckResult(new CheckResult(this, ResultStatus.OK, null));
		}
	}

	private Vector3d calculateAverageNormal(ArrayList<Vector3d> normals) {
		double x = 0D;
		double y = 0D;
		double z = 0D;
		for (Vector3d normal : normals) {
			x += normal.getX();
			y += normal.getY();
			z += normal.getZ();
		}
		x = x / normals.size();
		y = y / normals.size();
		z = z / normals.size();
		return new Vector3d(x, y, z);
	}

	private void planarDistance(Polygon p) {
		// store all used points in temporary list
		ArrayList<Vertex> vertices = collectVertices(p);
		Vector3d centroid = CovarianceMatrix.getCentroid(vertices);
		EigenvalueDecomposition ed = OrthogonalRegressionPlane.decompose(vertices, centroid);
		Vector3d eigenvalues = OrthogonalRegressionPlane.getEigenvalues(ed);

		if (checkEigenvalues(p, eigenvalues)) {
			// found tiny edge error, abort further checking
			return;
		}

		Plane plane = OrthogonalRegressionPlane.calculatePlane(centroid, ed, eigenvalues);
		for (Vertex v : vertices) {
			double distance = plane.getDistance(v);
			if (distance > delta) {
Matthias Betz's avatar
Matthias Betz committed
213
				CheckError err = new NonPlanarPolygonDistancePlaneError(p, distance, v, plane);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
				p.addCheckResult(new CheckResult(this, ResultStatus.ERROR, err));
				return;
			}
		}
		CheckResult cr = p.getCheckResult(this.getCheckId());
		// only change check result if missing
		if (cr == null) {
			p.addCheckResult(new CheckResult(this, ResultStatus.OK, null));
		}
	}

	private ArrayList<Vertex> collectVertices(Polygon p) {
		ArrayList<Vertex> vertices = new ArrayList<>();
		// only go to n - 1 points, because last point = first point
		for (int i = 0; i < p.getExteriorRing().getVertices().size() - 1; i++) {
			Vertex v = p.getExteriorRing().getVertices().get(i);
			vertices.add(v);
		}
		for (LinearRing lr : p.getInnerRings()) {
			for (int i = 0; i < lr.getVertices().size() - 1; i++) {
				Vertex v = lr.getVertices().get(i);
				vertices.add(v);
			}
		}
		return vertices;
	}

	private boolean checkEigenvalues(Polygon p, Vector3d eigenvalues) {
		int nrOfEigenvaluesBelowTolerance = 0;
		for (double d : eigenvalues.getCoordinates()) {
			if (d <= tinyEdgeTolerance) {
				nrOfEigenvaluesBelowTolerance++;
			}
		}
		if (nrOfEigenvaluesBelowTolerance >= 2) {
			CheckError err = new TinyEdgeError(p);
			p.addCheckResult(new CheckResult(this, ResultStatus.ERROR, err));
			return true;
		}
		return false;
	}

	@Override
	public List<Class<? extends Checkable>> getApplicableToClasses() {
		return applicableToClasses;
	}

	@Override
	public List<CheckId> getDependencies() {
		return dependencies;
	}

	@Override
	public CheckType getType() {
		return CheckType.GEOMETRY;
	}

	@Override
	public Check createNewInstance() {
		return new PlanarCheck();
	}
}