Changeset 11272 in josm for trunk/src/org/openstreetmap/josm/data/osm/BBox.java
- Timestamp:
- 2016-11-17T21:00:49+01:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/BBox.java
r11269 r11272 6 6 import java.util.Objects; 7 7 8 import org.openstreetmap.josm.data.Bounds; 8 9 import org.openstreetmap.josm.data.coor.LatLon; 9 10 import org.openstreetmap.josm.data.coor.QuadTiling; 10 11 import org.openstreetmap.josm.tools.Utils; 11 12 13 /** 14 * A BBox represents an area in lat/lon space. It is used for the quad tree. 15 * 16 * In contrast to a {@link Bounds} object, a BBox can represent an invalid (empty) area. 17 */ 12 18 public class BBox { 13 19 … … 30 36 */ 31 37 public BBox(final double x, final double y) { 32 if (!Double.isNaN(x) && !Double.isNaN(y)) { 33 xmin = x; 34 ymin = y; 35 xmax = x; 36 ymax = y; 37 } 38 add(x, y); 38 39 } 39 40 … … 69 70 */ 70 71 public BBox(double ax, double ay, double bx, double by) { 71 if (Double.isNaN(ax) || Double.isNaN(ay) || Double.isNaN(bx) || Double.isNaN(by)) { 72 return; // use default which is an invalid BBox 73 } 74 75 if (ax > bx) { 76 xmax = ax; 77 xmin = bx; 78 } else { 79 xmax = bx; 80 xmin = ax; 81 } 82 83 if (ay > by) { 84 ymax = ay; 85 ymin = by; 86 } else { 87 ymax = by; 88 ymin = ay; 89 } 72 if (!(Double.isNaN(ax) || Double.isNaN(ay) || Double.isNaN(bx) || Double.isNaN(by))) { 73 add(ax, ay); 74 add(bx, by); 75 } 76 // otherwise use default which is an invalid BBox 90 77 } 91 78 … … 96 83 */ 97 84 public BBox(Way w) { 98 w.getNodes().forEach( (n)-> add(n.getCoor()));85 w.getNodes().forEach(n -> add(n.getCoor())); 99 86 } 100 87 … … 104 91 */ 105 92 public BBox(Node n) { 106 if (n.isLatLonKnown()) 93 if (n.isLatLonKnown()) { 107 94 add(n.getCoor()); 95 } 108 96 } 109 97 … … 114 102 */ 115 103 public final void add(LatLon c) { 116 if (c != null && c.isValid()) 104 if (c != null && c.isValid()) { 117 105 add(c.lon(), c.lat()); 106 } 118 107 } 119 108 … … 124 113 */ 125 114 public final void add(double x, double y) { 126 if ( Double.isNaN(x) || Double.isNaN(y))127 return;128 xmin = Math.min(xmin, x);129 xmax = Math.max(xmax, x);130 ymin = Math.min(ymin, y);131 ymax = Math.max(ymax, y);115 if (!Double.isNaN(x) && !Double.isNaN(y)) { 116 xmin = Math.min(xmin, x); 117 xmax = Math.max(xmax, x); 118 ymin = Math.min(ymin, y); 119 ymax = Math.max(ymax, y); 120 } 132 121 } 133 122 … … 156 145 } 157 146 147 /** 148 * Gets the height of the bbox. 149 * @return The difference between ymax and ymin. 0 for invalid bboxes. 150 */ 158 151 public double height() { 159 return ymax-ymin; 160 } 161 152 if (isValid()) { 153 return ymax - ymin; 154 } else { 155 return 0; 156 } 157 } 158 159 /** 160 * Gets the width of the bbox. 161 * @return The difference between xmax and xmin. 0 for invalid bboxes. 162 */ 162 163 public double width() { 163 return xmax-xmin; 164 if (isValid()) { 165 return xmax - xmin; 166 } else { 167 return 0; 168 } 164 169 } 165 170 … … 191 196 */ 192 197 public boolean intersects(BBox b) { 193 if (xmin > b.xmax) 194 return false; 195 if (xmax < b.xmin) 196 return false; 197 if (ymin > b.ymax) 198 return false; 199 if (ymax < b.ymin) 200 return false; 201 return true; 198 return xmin <= b.xmax && xmax >= b.xmin 199 && ymin <= b.ymax && ymax >= b.ymin; 202 200 } 203 201 … … 254 252 } 255 253 254 /** 255 * Gets the center of this BBox. 256 * @return The center. 257 */ 256 258 public LatLon getCenter() { 257 259 return new LatLon(ymin + (ymax-ymin)/2.0, xmin + (xmax-xmin)/2.0); … … 298 300 * @return true if the bbox covers a part of the planets surface 299 301 * Height and width must be non-negative, but may (both) be 0. 302 * @since 11269 300 303 */ 301 304 public boolean isValid() { 302 return (xmin <= xmax && ymin <= ymax); 303 } 304 305 /** 306 * @return true if the bbox covers a part of the planets surface 305 return xmin <= xmax && ymin <= ymax; 306 } 307 308 /** 309 * @return true if the bbox is avalid and covers a part of the planets surface 310 * @since 11269 307 311 */ 308 312 public boolean isInWorld() { 309 return !(xmin < -180.0 || xmax > 180.0 || ymin < -90.0 || ymax > 90.0);313 return isValid() && xmin >= -180.0 && xmax <= 180.0 && ymin >= -90.0 && ymax <= 90.0; 310 314 } 311 315
Note:
See TracChangeset
for help on using the changeset viewer.