source: josm/trunk/src/org/openstreetmap/josm/data/osm/BBox.java@ 9371

Last change on this file since 9371 was 9371, checked in by simon04, 8 years ago

Java 7: use Objects.equals and Objects.hash

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.awt.geom.Rectangle2D;
5import java.util.Arrays;
6import java.util.Objects;
7
8import org.openstreetmap.josm.data.coor.LatLon;
9import org.openstreetmap.josm.data.coor.QuadTiling;
10import org.openstreetmap.josm.tools.Utils;
11
12public class BBox {
13
14 private double xmin = Double.POSITIVE_INFINITY;
15 private double xmax = Double.NEGATIVE_INFINITY;
16 private double ymin = Double.POSITIVE_INFINITY;
17 private double ymax = Double.NEGATIVE_INFINITY;
18
19 /**
20 * Constructs a new {@code BBox} defined by a single point.
21 *
22 * @param x X coordinate
23 * @param y Y coordinate
24 * @since 6203
25 */
26 public BBox(final double x, final double y) {
27 xmax = xmin = x;
28 ymax = ymin = y;
29 sanity();
30 }
31
32 /**
33 * Constructs a new {@code BBox} defined by points <code>a</code> and <code>b</code>.
34 * Result is minimal BBox containing both points.
35 *
36 * @param a first point
37 * @param b second point
38 */
39 public BBox(LatLon a, LatLon b) {
40 this(a.lon(), a.lat(), b.lon(), b.lat());
41 }
42
43 /**
44 * Constructs a new {@code BBox} from another one.
45 *
46 * @param copy the BBox to copy
47 */
48 public BBox(BBox copy) {
49 this.xmin = copy.xmin;
50 this.xmax = copy.xmax;
51 this.ymin = copy.ymin;
52 this.ymax = copy.ymax;
53 }
54
55 public BBox(double a_x, double a_y, double b_x, double b_y) {
56
57 if (a_x > b_x) {
58 xmax = a_x;
59 xmin = b_x;
60 } else {
61 xmax = b_x;
62 xmin = a_x;
63 }
64
65 if (a_y > b_y) {
66 ymax = a_y;
67 ymin = b_y;
68 } else {
69 ymax = b_y;
70 ymin = a_y;
71 }
72
73 sanity();
74 }
75
76 public BBox(Way w) {
77 for (Node n : w.getNodes()) {
78 LatLon coor = n.getCoor();
79 if (coor == null) {
80 continue;
81 }
82 add(coor);
83 }
84 }
85
86 public BBox(Node n) {
87 LatLon coor = n.getCoor();
88 if (coor == null) {
89 xmin = xmax = ymin = ymax = 0;
90 } else {
91 xmin = xmax = coor.lon();
92 ymin = ymax = coor.lat();
93 }
94 }
95
96 private void sanity() {
97 if (xmin < -180.0) {
98 xmin = -180.0;
99 }
100 if (xmax > 180.0) {
101 xmax = 180.0;
102 }
103 if (ymin < -90.0) {
104 ymin = -90.0;
105 }
106 if (ymax > 90.0) {
107 ymax = 90.0;
108 }
109 }
110
111 public final void add(LatLon c) {
112 add(c.lon(), c.lat());
113 }
114
115 /**
116 * Extends this bbox to include the point (x, y)
117 * @param x X coordinate
118 * @param y Y coordinate
119 */
120 public final void add(double x, double y) {
121 xmin = Math.min(xmin, x);
122 xmax = Math.max(xmax, x);
123 ymin = Math.min(ymin, y);
124 ymax = Math.max(ymax, y);
125 sanity();
126 }
127
128 public final void add(BBox box) {
129 xmin = Math.min(xmin, box.xmin);
130 xmax = Math.max(xmax, box.xmax);
131 ymin = Math.min(ymin, box.ymin);
132 ymax = Math.max(ymax, box.ymax);
133 sanity();
134 }
135
136 public void addPrimitive(OsmPrimitive primitive, double extraSpace) {
137 BBox primBbox = primitive.getBBox();
138 add(primBbox.xmin - extraSpace, primBbox.ymin - extraSpace);
139 add(primBbox.xmax + extraSpace, primBbox.ymax + extraSpace);
140 }
141
142 public double height() {
143 return ymax-ymin;
144 }
145
146 public double width() {
147 return xmax-xmin;
148 }
149
150 /**
151 * Tests, whether the bbox {@code b} lies completely inside this bbox.
152 * @param b bounding box
153 * @return {@code true} if {@code b} lies completely inside this bbox
154 */
155 public boolean bounds(BBox b) {
156 if (!(xmin <= b.xmin) ||
157 !(xmax >= b.xmax) ||
158 !(ymin <= b.ymin) ||
159 !(ymax >= b.ymax))
160 return false;
161 return true;
162 }
163
164 /**
165 * Tests, whether the Point {@code c} lies within the bbox.
166 * @param c point
167 * @return {@code true} if {@code c} lies within the bbox
168 */
169 public boolean bounds(LatLon c) {
170 if ((xmin <= c.lon()) &&
171 (xmax >= c.lon()) &&
172 (ymin <= c.lat()) &&
173 (ymax >= c.lat()))
174 return true;
175 return false;
176 }
177
178 /**
179 * Tests, whether two BBoxes intersect as an area.
180 * I.e. whether there exists a point that lies in both of them.
181 * @param b other bounding box
182 * @return {@code true} if this bbox intersects with the other
183 */
184 public boolean intersects(BBox b) {
185 if (xmin > b.xmax)
186 return false;
187 if (xmax < b.xmin)
188 return false;
189 if (ymin > b.ymax)
190 return false;
191 if (ymax < b.ymin)
192 return false;
193 return true;
194 }
195
196 /**
197 * Returns the top-left point.
198 * @return The top-left point
199 */
200 public LatLon getTopLeft() {
201 return new LatLon(ymax, xmin);
202 }
203
204 /**
205 * Returns the latitude of top-left point.
206 * @return The latitude of top-left point
207 * @since 6203
208 */
209 public double getTopLeftLat() {
210 return ymax;
211 }
212
213 /**
214 * Returns the longitude of top-left point.
215 * @return The longitude of top-left point
216 * @since 6203
217 */
218 public double getTopLeftLon() {
219 return xmin;
220 }
221
222 /**
223 * Returns the bottom-right point.
224 * @return The bottom-right point
225 */
226 public LatLon getBottomRight() {
227 return new LatLon(ymin, xmax);
228 }
229
230 /**
231 * Returns the latitude of bottom-right point.
232 * @return The latitude of bottom-right point
233 * @since 6203
234 */
235 public double getBottomRightLat() {
236 return ymin;
237 }
238
239 /**
240 * Returns the longitude of bottom-right point.
241 * @return The longitude of bottom-right point
242 * @since 6203
243 */
244 public double getBottomRightLon() {
245 return xmax;
246 }
247
248 public LatLon getCenter() {
249 return new LatLon(ymin + (ymax-ymin)/2.0, xmin + (xmax-xmin)/2.0);
250 }
251
252 int getIndex(final int level) {
253
254 int idx1 = QuadTiling.index(ymin, xmin, level);
255
256 final int idx2 = QuadTiling.index(ymin, xmax, level);
257 if (idx1 == -1) idx1 = idx2;
258 else if (idx1 != idx2) return -1;
259
260 final int idx3 = QuadTiling.index(ymax, xmin, level);
261 if (idx1 == -1) idx1 = idx3;
262 else if (idx1 != idx3) return -1;
263
264 final int idx4 = QuadTiling.index(ymax, xmax, level);
265 if (idx1 == -1) idx1 = idx4;
266 else if (idx1 != idx4) return -1;
267
268 return idx1;
269 }
270
271 public Rectangle2D toRectangle() {
272 return new Rectangle2D.Double(xmin, ymin, xmax - xmin, ymax - ymin);
273 }
274
275 @Override
276 public int hashCode() {
277 return (int) (ymin * xmin);
278 }
279
280 @Override
281 public boolean equals(Object o) {
282 if (o instanceof BBox) {
283 BBox b = (BBox) o;
284 return b.xmax == xmax && b.ymax == ymax
285 && b.xmin == xmin && b.ymin == ymin;
286 } else
287 return false;
288 }
289
290 @Override
291 public String toString() {
292 return "[ x: " + xmin + " -> " + xmax + ", y: " + ymin + " -> " + ymax + " ]";
293 }
294
295 public String toStringCSV(String separator) {
296 return Utils.join(separator, Arrays.asList(
297 LatLon.cDdFormatter.format(xmin),
298 LatLon.cDdFormatter.format(ymin),
299 LatLon.cDdFormatter.format(xmax),
300 LatLon.cDdFormatter.format(ymax)));
301 }
302}
Note: See TracBrowser for help on using the repository browser.