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

Last change on this file since 8470 was 8470, checked in by Don-vip, 9 years ago

javadoc fixes. Removed one duplicated method in exception handling

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