1 | package org.openstreetmap.josm.data.osm;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.data.Bounds;
|
---|
7 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
8 |
|
---|
9 | public 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 | private void sanity() {
|
---|
52 | if (xmin < -180.0) {
|
---|
53 | xmin = -180.0;
|
---|
54 | }
|
---|
55 | if (xmax > 180.0) {
|
---|
56 | xmax = 180.0;
|
---|
57 | }
|
---|
58 | if (ymin < -90.0) {
|
---|
59 | ymin = -90.0;
|
---|
60 | }
|
---|
61 | if (ymax > 90.0) {
|
---|
62 | ymax = 90.0;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void add(LatLon c) {
|
---|
67 | add(c.lon(), c.lat());
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void add(double x, double y) {
|
---|
71 | xmin = Math.min(xmin, x);
|
---|
72 | xmax = Math.max(xmax, x);
|
---|
73 | ymin = Math.min(ymin, y);
|
---|
74 | ymax = Math.max(ymax, y);
|
---|
75 | sanity();
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void add(BBox box) {
|
---|
79 | add(box.getTopLeft());
|
---|
80 | add(box.getBottomRight());
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void addPrimitive(OsmPrimitive primitive, double extraSpace) {
|
---|
84 | BBox primBbox = primitive.getBBox();
|
---|
85 | add(primBbox.xmin - extraSpace, primBbox.ymin - extraSpace);
|
---|
86 | add(primBbox.xmax + extraSpace, primBbox.ymax + extraSpace);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public double height() {
|
---|
90 | return ymax-ymin;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public double width() {
|
---|
94 | return xmax-xmin;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public boolean bounds(BBox b) {
|
---|
98 | if (!(xmin <= b.xmin) ||
|
---|
99 | !(xmax >= b.xmax) ||
|
---|
100 | !(ymin <= b.ymin) ||
|
---|
101 | !(ymax >= b.ymax))
|
---|
102 | return false;
|
---|
103 | return true;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public boolean bounds(LatLon c) {
|
---|
107 | if ((xmin <= c.lon()) &&
|
---|
108 | (xmax >= c.lon()) &&
|
---|
109 | (ymin <= c.lat()) &&
|
---|
110 | (ymax >= c.lat()))
|
---|
111 | return true;
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public boolean inside(BBox b) {
|
---|
116 | if (xmin > b.xmax)
|
---|
117 | return false;
|
---|
118 | if (xmax < b.xmin)
|
---|
119 | return false;
|
---|
120 | if (ymin > b.ymax)
|
---|
121 | return false;
|
---|
122 | if (ymax < b.ymin)
|
---|
123 | return false;
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public boolean intersects(BBox b) {
|
---|
128 | return this.inside(b) || b.inside(this);
|
---|
129 | }
|
---|
130 |
|
---|
131 | public List<LatLon> points() {
|
---|
132 | LatLon p1 = new LatLon(ymin, xmin);
|
---|
133 | LatLon p2 = new LatLon(ymin, xmax);
|
---|
134 | LatLon p3 = new LatLon(ymax, xmin);
|
---|
135 | LatLon p4 = new LatLon(ymax, xmax);
|
---|
136 | List<LatLon> ret = new ArrayList<LatLon>(4);
|
---|
137 | ret.add(p1);
|
---|
138 | ret.add(p2);
|
---|
139 | ret.add(p3);
|
---|
140 | ret.add(p4);
|
---|
141 | return ret;
|
---|
142 | }
|
---|
143 |
|
---|
144 | public LatLon getTopLeft() {
|
---|
145 | return new LatLon(ymax, xmin);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public LatLon getBottomRight() {
|
---|
149 | return new LatLon(ymin, xmax);
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public int hashCode() {
|
---|
154 | return (int)(ymin * xmin);
|
---|
155 | }
|
---|
156 |
|
---|
157 | @Override
|
---|
158 | public boolean equals(Object o) {
|
---|
159 | if (o instanceof BBox) {
|
---|
160 | BBox b = (BBox)o;
|
---|
161 | return b.xmax == xmax && b.ymax == ymax && b.xmin == xmin && b.ymin == ymin;
|
---|
162 | } else
|
---|
163 | return false;
|
---|
164 | }
|
---|
165 |
|
---|
166 | @Override
|
---|
167 | public String toString() {
|
---|
168 | return "[ x: " + xmin + " -> " + xmax +
|
---|
169 | ", y: " + ymin + " -> " + ymax + " ]";
|
---|
170 | }
|
---|
171 | }
|
---|