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(double a_x, double a_y, double b_x, double b_y) {
|
---|
27 | xmin = Math.min(a_x, b_x);
|
---|
28 | xmax = Math.max(a_x, b_x);
|
---|
29 | ymin = Math.min(a_y, b_y);
|
---|
30 | ymax = Math.max(a_y, b_y);
|
---|
31 | sanity();
|
---|
32 | }
|
---|
33 |
|
---|
34 | public BBox(Way w) {
|
---|
35 | for (Node n : w.getNodes()) {
|
---|
36 | LatLon coor = n.getCoor();
|
---|
37 | if (coor == null) {
|
---|
38 | continue;
|
---|
39 | }
|
---|
40 | add(coor);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private void sanity() {
|
---|
45 | if (xmin < -180.0) {
|
---|
46 | xmin = -180.0;
|
---|
47 | }
|
---|
48 | if (xmax > 180.0) {
|
---|
49 | xmax = 180.0;
|
---|
50 | }
|
---|
51 | if (ymin < -90.0) {
|
---|
52 | ymin = -90.0;
|
---|
53 | }
|
---|
54 | if (ymax > 90.0) {
|
---|
55 | ymax = 90.0;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void add(LatLon c) {
|
---|
60 | add(c.lon(), c.lat());
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void add(double x, double y) {
|
---|
64 | xmin = Math.min(xmin, x);
|
---|
65 | xmax = Math.max(xmax, x);
|
---|
66 | ymin = Math.min(ymin, y);
|
---|
67 | ymax = Math.max(ymax, y);
|
---|
68 | sanity();
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void addPrimitive(OsmPrimitive primitive, double extraSpace) {
|
---|
72 | BBox primBbox = primitive.getBBox();
|
---|
73 | add(primBbox.xmin - extraSpace, primBbox.ymin - extraSpace);
|
---|
74 | add(primBbox.xmax + extraSpace, primBbox.ymax + extraSpace);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public double height() {
|
---|
78 | return ymax-ymin;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public double width() {
|
---|
82 | return xmax-xmin;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public boolean bounds(BBox b) {
|
---|
86 | if (!(xmin <= b.xmin) ||
|
---|
87 | !(xmax >= b.xmax) ||
|
---|
88 | !(ymin <= b.ymin) ||
|
---|
89 | !(ymax >= b.ymax))
|
---|
90 | return false;
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public boolean bounds(LatLon c) {
|
---|
95 | if ((xmin <= c.lon()) &&
|
---|
96 | (xmax >= c.lon()) &&
|
---|
97 | (ymin <= c.lat()) &&
|
---|
98 | (ymax >= c.lat()))
|
---|
99 | return true;
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public boolean inside(BBox b) {
|
---|
104 | if (xmin >= b.xmax)
|
---|
105 | return false;
|
---|
106 | if (xmax <= b.xmin)
|
---|
107 | return false;
|
---|
108 | if (ymin >= b.ymax)
|
---|
109 | return false;
|
---|
110 | if (ymax <= b.ymin)
|
---|
111 | return false;
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public boolean intersects(BBox b) {
|
---|
116 | return this.inside(b) || b.inside(this);
|
---|
117 | }
|
---|
118 |
|
---|
119 | public List<LatLon> points() {
|
---|
120 | LatLon p1 = new LatLon(ymin, xmin);
|
---|
121 | LatLon p2 = new LatLon(ymin, xmax);
|
---|
122 | LatLon p3 = new LatLon(ymax, xmin);
|
---|
123 | LatLon p4 = new LatLon(ymax, xmax);
|
---|
124 | List<LatLon> ret = new ArrayList<LatLon>(4);
|
---|
125 | ret.add(p1);
|
---|
126 | ret.add(p2);
|
---|
127 | ret.add(p3);
|
---|
128 | ret.add(p4);
|
---|
129 | return ret;
|
---|
130 | }
|
---|
131 |
|
---|
132 | public LatLon getTopLeft() {
|
---|
133 | return new LatLon(ymax, xmin);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public LatLon getBottomRight() {
|
---|
137 | return new LatLon(ymin, xmax);
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public String toString() {
|
---|
142 | return "[ x: " + xmin + " -> " + xmax +
|
---|
143 | ", y: " + ymin + " -> " + ymax + " ]";
|
---|
144 | }
|
---|
145 | }
|
---|