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

Last change on this file since 3715 was 3209, checked in by bastiK, 14 years ago

minor cleanup

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