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

Last change on this file since 3734 was 3719, checked in by bastiK, 13 years ago

added missing license information

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