BoundingBox.java 4.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*-
 *  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.datastructure;

21
import java.util.Collection;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.List;

import de.hft.stuttgart.citydoctor2.math.Vector3d;
import de.hft.stuttgart.citydoctor2.utils.BoundingBoxCalculator;

/**
 * An axis aligned bounding box represented by its two corners
 * 
 * @author Matthias Betz
 *
 */
public class BoundingBox {

	private Vector3d[] bbox;

	/**
	 * Creates an axis aligned bounding box containing all points of all polygons
	 * 
	 * @param polygons containing the points from which the box will be created
	 * @return the bounding box around all points
	 */
43
	public static BoundingBox of(Collection<? extends Polygon> polygons) {
44
45
		return BoundingBoxCalculator.calculateBoundingBox(polygons);
	}
46
47
48
49
	
	public static BoundingBox ofPoints(List<? extends Vector3d> points) {
		return BoundingBoxCalculator.calculateBoundingBoxFromPoints(points);
	}
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
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
168
169

	/**
	 * Creates an axis aligned bounding box of the whole model.
	 * 
	 * @param model the model containing the features with geometries used for the
	 *              bounding box
	 * @return the bounding box around all features
	 */
	public static BoundingBox of(CityDoctorModel model) {
		return BoundingBoxCalculator.calculateBoundingBox(model);
	}

	/**
	 * Creates a new bounding box with two vectors as corner points.
	 * 
	 * @param box the array of length 2 containing both corners
	 * @return the new bounding box
	 */
	public static BoundingBox of(Vector3d[] box) {
		return new BoundingBox(box);
	}

	private BoundingBox(Vector3d[] bbox) {
		if (bbox == null || bbox.length != 2) {
			throw new IllegalArgumentException("BoundingBox must be an array of the length 2");
		}
		this.bbox = bbox;
	}

	/**
	 * Calculates the volume of the box
	 * 
	 * @return the volume of the box
	 */
	public double getVolume() {
		double length = getDepth();
		double width = getWidth();
		double height = getHeight();
		return height * width * length;
	}

	/**
	 * Calculates the center of the bounding box
	 * 
	 * @return the center of the bounding box
	 */
	public Vector3d getCenter() {
		return bbox[0].plus(bbox[1].minus(bbox[0]).mult(0.5));
	}

	/**
	 * Getter for the corner array
	 * 
	 * @return the array containing the corner points
	 */
	public Vector3d[] getBox() {
		return bbox;
	}

	/**
	 * Calculates the width of the bounding box
	 * 
	 * @return the width of the bounding box
	 */
	public double getWidth() {
		return bbox[1].getY() - bbox[0].getY();
	}

	/**
	 * Calculates the height of the bounding box
	 * 
	 * @return the height of the bounding box
	 */
	public double getHeight() {
		return bbox[1].getZ() - bbox[0].getZ();
	}

	/**
	 * Calculates the depth of the bounding box
	 * 
	 * @return the depth of the bounding box
	 */
	public double getDepth() {
		return bbox[1].getX() - bbox[0].getX();
	}

	/**
	 * Returns the length of the longest side of the bounding box, ignoring the
	 * height. Only X and Y axis are considered
	 * 
	 * @return the length of the longest side in X or Y direction
	 */
	public double getLongestSide() {
		double width = getWidth();
		double depth = getDepth();
		if (width > depth) {
			return width;
		} else {
			return depth;
		}
	}

	/**
	 * Calculates the direction vector from the lower corner to the upper corner
	 * 
	 * @return the direction vector from the lower corner to the upper corner
	 */
	public Vector3d getDiagonal() {
		return bbox[1].minus(bbox[0]);
	}

	/**
	 * Calculates the distance between the corner points
	 * 
	 * @return the distance between the corner points
	 */
	public double getDiagonalLength() {
		return bbox[1].getDistance(bbox[0]);
	}
}