[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[2450] | 2 | package org.openstreetmap.josm.data.osm;
|
---|
| 3 |
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.List;
|
---|
| 6 |
|
---|
| 7 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 8 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
| 9 |
|
---|
| 10 | public 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 |
|
---|
[3153] | 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 |
|
---|
[2450] | 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 |
|
---|
[3163] | 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 |
|
---|
[2450] | 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 |
|
---|
[3209] | 81 | /**
|
---|
| 82 | * Extends this bbox to include the point (x, y)
|
---|
| 83 | */
|
---|
[2450] | 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 |
|
---|
[2982] | 92 | public void add(BBox box) {
|
---|
| 93 | add(box.getTopLeft());
|
---|
| 94 | add(box.getBottomRight());
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[2450] | 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 |
|
---|
[3209] | 111 | /**
|
---|
| 112 | * Tests, weather the bbox b lies completely inside
|
---|
| 113 | * this bbox.
|
---|
| 114 | */
|
---|
[2450] | 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 |
|
---|
[3209] | 124 | /**
|
---|
| 125 | * Tests, weather the Point c lies within the bbox.
|
---|
| 126 | */
|
---|
[2450] | 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 |
|
---|
[3209] | 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) {
|
---|
[2500] | 141 | if (xmin > b.xmax)
|
---|
[2450] | 142 | return false;
|
---|
[2500] | 143 | if (xmax < b.xmin)
|
---|
[2450] | 144 | return false;
|
---|
[2500] | 145 | if (ymin > b.ymax)
|
---|
[2450] | 146 | return false;
|
---|
[2500] | 147 | if (ymax < b.ymin)
|
---|
[2450] | 148 | return false;
|
---|
| 149 | return true;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[3209] | 152 | /**
|
---|
| 153 | * Returns a list of all 4 corners of the bbox rectangle.
|
---|
| 154 | */
|
---|
[2450] | 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
|
---|
[2982] | 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
|
---|
[2450] | 191 | public String toString() {
|
---|
| 192 | return "[ x: " + xmin + " -> " + xmax +
|
---|
| 193 | ", y: " + ymin + " -> " + ymax + " ]";
|
---|
| 194 | }
|
---|
| 195 | }
|
---|