BoundingBoxCalculator.java 4.5 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*-
 *  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();
				} else if (v.getX() > highX) {
					highX = v.getX();
				}
				if (v.getY() < lowY) {
					lowY = v.getY();
				} else if (v.getY() > highY) {
					highY = v.getY();
				}
				if (v.getZ() < lowZ) {
					lowZ = v.getZ();
				} else 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);
	}

	/**
	 * 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);
		Vector3d high = new Vector3d(Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE);

		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);
	}

	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()) {
118
			if (geom.getVertices() == null) {
119
				geom.updateVertices();
120
			}
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
			for (Vertex v : geom.getVertices()) {
				if (v.getX() < low.getX()) {
					low.setX(v.getX());
				} else if (v.getX() > high.getX()) {
					high.setX(v.getX());
				}
				if (v.getY() < low.getY()) {
					low.setY(v.getY());
				} else if (v.getY() > high.getY()) {
					high.setY(v.getY());
				}
				if (v.getZ() < low.getZ()) {
					low.setZ(v.getZ());
				} else if (v.getZ() > high.getZ()) {
					high.setZ(v.getZ());
				}
			}
		}
	}

}