BoundingBoxCalculator.java 5.38 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
58
59
60
61
62
63
64
65
/*-
 *  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.utils;

import java.util.List;

import de.hft.stuttgart.citydoctor2.datastructure.BoundingBox;
import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import de.hft.stuttgart.citydoctor2.datastructure.CityObject;
import de.hft.stuttgart.citydoctor2.datastructure.Geometry;
import de.hft.stuttgart.citydoctor2.datastructure.Polygon;
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;
import de.hft.stuttgart.citydoctor2.math.Vector3d;

/**
 * Utility class for calculating axis aligned bounding boxes for different
 * inputs.
 * 
 * @author Matthias Betz
 *
 */
public class BoundingBoxCalculator {

	private BoundingBoxCalculator() {
		// only static use
	}

	/**
	 * This will calculate two points where every other point in the geometry is
	 * between those two. If the geometry does not contain points or is null, an
	 * empty array is returned. This is considered an axis aligned bounding box.
	 * Only the exterior rings of the polygons is used as inner rings should be
	 * within the exterior or they are faulty.
	 * 
	 * @param a list of polygons containing the vertices
	 * @return an BoundingBox containing the two end points of the bounding box.
	 */
	public static BoundingBox calculateBoundingBox(List<Polygon> polygons) {
		double lowX = Double.MAX_VALUE;
		double highX = Double.NEGATIVE_INFINITY;
		double lowY = Double.MAX_VALUE;
		double highY = Double.NEGATIVE_INFINITY;
		double lowZ = Double.MAX_VALUE;
		double highZ = Double.NEGATIVE_INFINITY;
		for (Polygon p : polygons) {
			// only need to check exterior rings
			for (Vertex v : p.getExteriorRing().getVertices()) {
				if (v.getX() < lowX) {
					lowX = v.getX();
66
67
				}
				if (v.getX() > highX) {
68
69
70
71
					highX = v.getX();
				}
				if (v.getY() < lowY) {
					lowY = v.getY();
72
73
				}
				if (v.getY() > highY) {
74
75
76
77
					highY = v.getY();
				}
				if (v.getZ() < lowZ) {
					lowZ = v.getZ();
78
79
				}
				if (v.getZ() > highZ) {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
					highZ = v.getZ();
				}
			}
		}
		Vector3d[] result = new Vector3d[2];
		result[0] = new Vector3d(lowX, lowY, lowZ);
		result[1] = new Vector3d(highX, highY, highZ);
		return BoundingBox.of(result);
	}

	/**
	 * This will calculate an axis aligned bounding box for the complete GML model.
	 * 
	 * @param model the model
	 * @return the bounding box of the model
	 */
	public static BoundingBox calculateBoundingBox(CityDoctorModel model) {
		Vector3d low = new Vector3d(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE);
98
		Vector3d high = new Vector3d(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
99
100
101
102
103
104
105
106
107
108
109
110
111
112

		findMinMax(low, high, model.getBuildings());
		findMinMax(low, high, model.getBridges());
		findMinMax(low, high, model.getLand());
		findMinMax(low, high, model.getTransportation());
		findMinMax(low, high, model.getWater());
		findMinMax(low, high, model.getVegetation());

		Vector3d[] result = new Vector3d[2];
		result[0] = low;
		result[1] = high;
		return BoundingBox.of(result);
	}

113
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
	public static BoundingBox calculateBoundingBoxFromPoints(List<? extends Vector3d> points) {
		double lowX = Double.MAX_VALUE;
		double highX = Double.NEGATIVE_INFINITY;
		double lowY = Double.MAX_VALUE;
		double highY = Double.NEGATIVE_INFINITY;
		double lowZ = Double.MAX_VALUE;
		double highZ = Double.NEGATIVE_INFINITY;
		// only need to check exterior rings
		for (Vector3d v : points) {
			if (v.getX() < lowX) {
				lowX = v.getX();
			}
			if (v.getX() > highX) {
				highX = v.getX();
			}
			if (v.getY() < lowY) {
				lowY = v.getY();
			}
			if (v.getY() > highY) {
				highY = v.getY();
			}
			if (v.getZ() < lowZ) {
				lowZ = v.getZ();
			}
			if (v.getZ() > highZ) {
				highZ = v.getZ();
			}
		}
		Vector3d[] result = new Vector3d[2];
		result[0] = new Vector3d(lowX, lowY, lowZ);
		result[1] = new Vector3d(highX, highY, highZ);
		return BoundingBox.of(result);
	}

147
148
149
150
151
152
153
154
	private static void findMinMax(Vector3d low, Vector3d high, List<? extends CityObject> features) {
		for (CityObject co : features) {
			findMinMax(low, high, co);
		}
	}

	private static void findMinMax(Vector3d low, Vector3d high, CityObject co) {
		for (Geometry geom : co.getGeometries()) {
155
			if (geom.getVertices() == null) {
156
				geom.updateVertices();
157
			}
158
159
160
			for (Vertex v : geom.getVertices()) {
				if (v.getX() < low.getX()) {
					low.setX(v.getX());
161
162
				}
				if (v.getX() > high.getX()) {
163
164
165
166
					high.setX(v.getX());
				}
				if (v.getY() < low.getY()) {
					low.setY(v.getY());
167
168
				}
				if (v.getY() > high.getY()) {
169
170
171
172
					high.setY(v.getY());
				}
				if (v.getZ() < low.getZ()) {
					low.setZ(v.getZ());
173
174
				}
				if (v.getZ() > high.getZ()) {
175
176
177
178
179
180
181
					high.setZ(v.getZ());
				}
			}
		}
	}

}