Ignore:
Timestamp:
2013-08-28T03:03:40+02:00 (11 years ago)
Author:
Don-vip
Message:

fix #9024 - bbox/bounds memory optimizations (modified patch by shinigami) + javadoc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Bounds.java

    r6162 r6203  
    99
    1010import org.openstreetmap.josm.data.coor.LatLon;
     11import org.openstreetmap.josm.data.osm.BBox;
    1112import org.openstreetmap.josm.tools.CheckParameterUtil;
    1213
     
    2728    }
    2829
     30    /**
     31     * Returns min latitude of bounds. Efficient shortcut for {@code getMin().lat()}.
     32     *
     33     * @return min latitude of bounds.
     34     * @since 6203
     35     */
     36    public double getMinLat() {
     37        return minLat;
     38    }
     39
     40    /**
     41     * Returns min longitude of bounds. Efficient shortcut for {@code getMin().lon()}.
     42     *
     43     * @return min longitude of bounds.
     44     * @since 6203
     45     */
     46    public double getMinLon() {
     47        return minLon;
     48    }
     49
    2950    public LatLon getMax() {
    3051        return new LatLon(maxLat, maxLon);
     52    }
     53
     54    /**
     55     * Returns max latitude of bounds. Efficient shortcut for {@code getMax().lat()}.
     56     *
     57     * @return max latitude of bounds.
     58     * @since 6203
     59     */
     60    public double getMaxLat() {
     61        return maxLat;
     62    }
     63
     64    /**
     65     * Returns max longitude of bounds. Efficient shortcut for {@code getMax().lon()}.
     66     *
     67     * @return max longitude of bounds.
     68     * @since 6203
     69     */
     70    public double getMaxLon() {
     71        return maxLon;
    3172    }
    3273
     
    3778
    3879    /**
    39      * Construct bounds out of two points
     80     * Construct bounds out of two points. Coords will be rounded.
    4081     */
    4182    public Bounds(LatLon min, LatLon max) {
     
    5192    }
    5293
     94    /**
     95     * Single point Bounds defined by lat/lon {@code b}.
     96     * Coordinates will be rounded to osm precision if {@code roundToOsmPrecision} is true.
     97     *
     98     * @param b lat/lon of given point.
     99     * @param roundToOsmPrecision defines if lat/lon will be rounded.
     100     */
    53101    public Bounds(LatLon b, boolean roundToOsmPrecision) {
     102        this(b.lat(), b.lon(), roundToOsmPrecision);
     103    }
     104   
     105    /**
     106     * Single point Bounds defined by point [lat,lon].
     107     * Coordinates will be rounded to osm precision if {@code roundToOsmPrecision} is true.
     108     *
     109     * @param lat latitude of given point.
     110     * @param lon longitude of given point.
     111     * @param roundToOsmPrecision defines if lat/lon will be rounded.
     112     * @since 6203
     113     */
     114    public Bounds(double lat, double lon, boolean roundToOsmPrecision) {
    54115        // Do not call this(b, b) to avoid GPX performance issue (see #7028) until roundToOsmPrecision() is improved
    55116        if (roundToOsmPrecision) {
    56             this.minLat = LatLon.roundToOsmPrecision(b.lat());
    57             this.minLon = LatLon.roundToOsmPrecision(b.lon());
    58         } else {
    59             this.minLat = b.lat();
    60             this.minLon = b.lon();
     117            this.minLat = LatLon.roundToOsmPrecision(lat);
     118            this.minLon = LatLon.roundToOsmPrecision(lon);
     119        } else {
     120            this.minLat = lat;
     121            this.minLon = lon;
    61122        }
    62123        this.maxLat = this.minLat;
     
    153214    }
    154215
    155     public Bounds(Bounds other) {
    156         this(other.getMin(), other.getMax());
     216    /**
     217     * Creates new {@code Bounds} from an existing one.
     218     * @param other The bounds to copy
     219     */
     220    public Bounds(final Bounds other) {
     221        this(other.minLat, other.minLon, other.maxLat, other.maxLon);
    157222    }
    158223
     
    186251    }
    187252
     253    /**
     254     * Creates BBox with same coordinates.
     255     *
     256     * @return BBox with same coordinates.
     257     * @since 6203
     258     */
     259    public BBox toBBox() {
     260        return new BBox(minLon, minLat, maxLon, maxLat);
     261    }
     262   
    188263    @Override public String toString() {
    189264        return "Bounds["+minLat+","+minLon+","+maxLat+","+maxLon+"]";
     
    203278    public LatLon getCenter()
    204279    {
     280        if (crosses180thMeridian()) {           
     281            double lat = (minLat + maxLat) / 2;
     282            double lon = (minLon + maxLon - 360.0) / 2;
     283            if (lon < -180.0){
     284                lon += 360.0;
     285            }
     286            return new LatLon(lat, lon);
     287        } else {
     288            return new LatLon((minLat + maxLat) / 2, (minLon + maxLon) / 2);
     289        }
     290    }
     291
     292    /**
     293     * Extend the bounds if necessary to include the given point.
     294     * @param ll The point to include into these bounds
     295     */
     296    public void extend(LatLon ll) {
     297        extend(ll.lat(), ll.lon());
     298    }
     299   
     300    /**
     301     * Extend the bounds if necessary to include the given point [lat,lon].
     302     * Good to use if you know coordinates to avoid creation of LatLon object.
     303     * @param lat Latitude of point to include into these bounds
     304     * @param lon Longitude of point to include into these bounds
     305     * @since 6203
     306     */
     307    public void extend(final double lat, final double lon) {
     308        if (lat < minLat) {
     309            minLat = LatLon.roundToOsmPrecision(lat);
     310        }
     311        if (lat > maxLat) {
     312            maxLat = LatLon.roundToOsmPrecision(lat);
     313        }
    205314        if (crosses180thMeridian()) {
    206             LatLon result = new LatLon(minLat, minLon-360.0).getCenter(getMax());
    207             if (result.lon() < -180.0) {
    208                 result = new LatLon(result.lat(), result.lon() + 360.0);
    209             }
    210             return result;
    211         } else {
    212             return getMin().getCenter(getMax());
    213         }
    214     }
    215 
    216     /**
    217      * Extend the bounds if necessary to include the given point.
    218      */
    219     public void extend(LatLon ll) {
    220         if (ll.lat() < minLat) {
    221             minLat = LatLon.roundToOsmPrecision(ll.lat());
    222         }
    223         if (ll.lat() > maxLat) {
    224             maxLat = LatLon.roundToOsmPrecision(ll.lat());
    225         }
    226         if (crosses180thMeridian()) {
    227             if (ll.lon() > maxLon && ll.lon() < minLon) {
    228                 if (Math.abs(ll.lon() - minLon) <= Math.abs(ll.lon() - maxLon)) {
    229                     minLon = LatLon.roundToOsmPrecision(ll.lon());
     315            if (lon > maxLon && lon < minLon) {
     316                if (Math.abs(lon - minLon) <= Math.abs(lon - maxLon)) {
     317                    minLon = LatLon.roundToOsmPrecision(lon);
    230318                } else {
    231                     maxLon = LatLon.roundToOsmPrecision(ll.lon());
     319                    maxLon = LatLon.roundToOsmPrecision(lon);
    232320                }
    233321            }
    234322        } else {
    235             if (ll.lon() < minLon) {
    236                 minLon = LatLon.roundToOsmPrecision(ll.lon());
     323            if (lon < minLon) {
     324                minLon = LatLon.roundToOsmPrecision(lon);
    237325            }
    238             if (ll.lon() > maxLon) {
    239                 maxLon = LatLon.roundToOsmPrecision(ll.lon());
     326            if (lon > maxLon) {
     327                maxLon = LatLon.roundToOsmPrecision(lon);
    240328            }
    241329        }
     
    243331
    244332    public void extend(Bounds b) {
    245         extend(b.getMin());
    246         extend(b.getMax());
    247     }
    248 
    249     /**
    250      * Is the given point within this bounds?
     333        extend(b.minLat, b.minLon);
     334        extend(b.maxLat, b.maxLon);
     335    }
     336
     337    /**
     338     * Determines if the given point {@code ll} is within these bounds.
     339     * @param ll The lat/lon to check
     340     * @return {@code true} if {@code ll} is within these bounds, {@code false} otherwise
    251341     */
    252342    public boolean contains(LatLon ll) {
     
    324414     */
    325415    public boolean isCollapsed() {
    326         return getMin().equals(getMax());
     416        return (minLat == maxLat) && (minLon == maxLon);
    327417    }
    328418
Note: See TracChangeset for help on using the changeset viewer.