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

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

fix #9002 - QuadBuckets improvements (patch by shinigami)

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.Arrays;
5
6import org.openstreetmap.josm.data.Bounds;
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 public BBox(Bounds bounds) {
19 add(bounds.getMin());
20 add(bounds.getMax());
21 }
22
23 public BBox(LatLon a, LatLon b) {
24 add(a);
25 add(b);
26 }
27
28 public BBox(BBox copy) {
29 this.xmin = copy.xmin;
30 this.xmax = copy.xmax;
31 this.ymin = copy.ymin;
32 this.ymax = copy.ymax;
33 }
34
35 public BBox(double a_x, double a_y, double b_x, double b_y) {
36 xmin = Math.min(a_x, b_x);
37 xmax = Math.max(a_x, b_x);
38 ymin = Math.min(a_y, b_y);
39 ymax = Math.max(a_y, b_y);
40 sanity();
41 }
42
43 public BBox(Way w) {
44 for (Node n : w.getNodes()) {
45 LatLon coor = n.getCoor();
46 if (coor == null) {
47 continue;
48 }
49 add(coor);
50 }
51 }
52
53 public BBox(Node n) {
54 LatLon coor = n.getCoor();
55 if (coor == null) {
56 xmin = xmax = ymin = ymax = 0;
57 } else {
58 xmin = xmax = coor.lon();
59 ymin = ymax = coor.lat();
60 }
61 }
62
63 private void sanity() {
64 if (xmin < -180.0) {
65 xmin = -180.0;
66 }
67 if (xmax > 180.0) {
68 xmax = 180.0;
69 }
70 if (ymin < -90.0) {
71 ymin = -90.0;
72 }
73 if (ymax > 90.0) {
74 ymax = 90.0;
75 }
76 }
77
78 public void add(LatLon c) {
79 add(c.lon(), c.lat());
80 }
81
82 /**
83 * Extends this bbox to include the point (x, y)
84 */
85 public void add(double x, double y) {
86 xmin = Math.min(xmin, x);
87 xmax = Math.max(xmax, x);
88 ymin = Math.min(ymin, y);
89 ymax = Math.max(ymax, y);
90 sanity();
91 }
92
93 public void add(BBox box) {
94 add(box.getTopLeft());
95 add(box.getBottomRight());
96 }
97
98 public void addPrimitive(OsmPrimitive primitive, double extraSpace) {
99 BBox primBbox = primitive.getBBox();
100 add(primBbox.xmin - extraSpace, primBbox.ymin - extraSpace);
101 add(primBbox.xmax + extraSpace, primBbox.ymax + extraSpace);
102 }
103
104 public double height() {
105 return ymax-ymin;
106 }
107
108 public double width() {
109 return xmax-xmin;
110 }
111
112 /**
113 * Tests, weather the bbox b lies completely inside
114 * this bbox.
115 */
116 public boolean bounds(BBox b) {
117 if (!(xmin <= b.xmin) ||
118 !(xmax >= b.xmax) ||
119 !(ymin <= b.ymin) ||
120 !(ymax >= b.ymax))
121 return false;
122 return true;
123 }
124
125 /**
126 * Tests, weather the Point c lies within the bbox.
127 */
128 public boolean bounds(LatLon c) {
129 if ((xmin <= c.lon()) &&
130 (xmax >= c.lon()) &&
131 (ymin <= c.lat()) &&
132 (ymax >= c.lat()))
133 return true;
134 return false;
135 }
136
137 /**
138 * Tests, weather two BBoxes intersect as an area.
139 * I.e. whether there exists a point that lies in both of them.
140 */
141 public boolean intersects(BBox b) {
142 if (xmin > b.xmax)
143 return false;
144 if (xmax < b.xmin)
145 return false;
146 if (ymin > b.ymax)
147 return false;
148 if (ymax < b.ymin)
149 return false;
150 return true;
151 }
152
153 public LatLon getTopLeft() {
154 return new LatLon(ymax, xmin);
155 }
156
157 public LatLon getBottomRight() {
158 return new LatLon(ymin, xmax);
159 }
160
161 public LatLon getCenter() {
162 return new LatLon(ymin + (ymax-ymin)/2.0, xmin + (xmax-xmin)/2.0);
163 }
164
165 int getIndex(final int level) {
166
167 int idx1 = QuadTiling.index(ymin, xmin, level);
168
169 final int idx2 = QuadTiling.index(ymin, xmax, level);
170 if (idx1 == -1) idx1 = idx2;
171 else if (idx1 != idx2) return -1;
172
173 final int idx3 = QuadTiling.index(ymax, xmin, level);
174 if (idx1 == -1) idx1 = idx3;
175 else if (idx1 != idx3) return -1;
176
177 final int idx4 = QuadTiling.index(ymax, xmax, level);
178 if (idx1 == -1) idx1 = idx4;
179 else if (idx1 != idx4) return -1;
180
181 return idx1;
182 }
183
184 @Override
185 public int hashCode() {
186 return (int)(ymin * xmin);
187 }
188
189 @Override
190 public boolean equals(Object o) {
191 if (o instanceof BBox) {
192 BBox b = (BBox)o;
193 return b.xmax == xmax && b.ymax == ymax && b.xmin == xmin && b.ymin == ymin;
194 } else
195 return false;
196 }
197
198 @Override
199 public String toString() {
200 return "[ x: " + xmin + " -> " + xmax +
201 ", y: " + ymin + " -> " + ymax + " ]";
202 }
203
204 public String toStringCSV(String separator) {
205 return Utils.join(separator, Arrays.asList(
206 LatLon.cDdFormatter.format(xmin),
207 LatLon.cDdFormatter.format(ymin),
208 LatLon.cDdFormatter.format(xmax),
209 LatLon.cDdFormatter.format(ymax)));
210 }
211}
Note: See TracBrowser for help on using the repository browser.