Changeset 2805 in josm


Ignore:
Timestamp:
Jan 10, 2010 1:24:25 PM (3 years ago)
Author:
jttt
Message:

Use double instead of latlon (slightly faster)

File:
1 edited

Legend:

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

    r2717 r2805  
    1919     * The minimum and maximum coordinates. 
    2020     */ 
    21     private LatLon min, max; 
     21    private double minLat, minLon, maxLat, maxLon; 
    2222 
    2323    public LatLon getMin() { 
    24         return min; 
     24        return new LatLon(minLat, minLon); 
    2525    } 
    2626 
    2727    public LatLon getMax() { 
    28         return max; 
     28        return new LatLon(maxLat, maxLon); 
    2929    } 
    3030 
     
    3333     */ 
    3434    public Bounds(LatLon min, LatLon max) { 
    35         this.min = min; 
    36         this.max = max; 
     35        this(min.lat(), min.lon(), max.lat(), max.lon()); 
    3736    } 
    3837 
    3938    public Bounds(LatLon b) { 
    40         this.min = b; 
    41         this.max = b; 
     39        this(b, b); 
    4240    } 
    4341 
    4442    public Bounds(double minlat, double minlon, double maxlat, double maxlon) { 
    45         this.min = new LatLon(minlat, minlon); 
    46         this.max = new LatLon(maxlat, maxlon); 
     43        this.minLat = minlat; 
     44        this.minLon = minlon; 
     45        this.maxLat = maxlat; 
     46        this.maxLon = maxlon; 
    4747    } 
    4848 
     
    5252        if (coords.length != 4) 
    5353            throw new IllegalArgumentException(tr("Expected array of length 4, got {0}", coords.length)); 
    54         this.min = new LatLon(coords[0], coords[1]); 
    55         this.max = new LatLon(coords[2], coords[3]); 
     54        this.minLat = coords[0]; 
     55        this.minLon = coords[1]; 
     56        this.maxLat = coords[2]; 
     57        this.maxLon = coords[3]; 
    5658    } 
    5759 
     
    7981            throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", values[3])); 
    8082 
    81         this.min = new LatLon(values[0], values[1]); 
    82         this.max = new LatLon(values[2], values[3]); 
     83        this.minLat = values[0]; 
     84        this.minLon = values[1]; 
     85        this.maxLat = values[2]; 
     86        this.maxLon = values[3]; 
    8387    } 
    8488 
    8589    public Bounds(Bounds other) { 
    86         this.min = new LatLon(other.min); 
    87         this.max = new LatLon(other.max); 
     90        this(other.getMin(), other.getMax()); 
    8891    } 
    8992 
    9093    public Bounds(Rectangle2D rect) { 
    91         this.min = new LatLon(rect.getMinY(), rect.getMinX()); 
    92         this.max = new LatLon(rect.getMaxY(), rect.getMaxX()); 
     94        this(rect.getMinY(), rect.getMinX(), rect.getMaxY(), rect.getMaxX()); 
    9395    } 
    9496 
     
    113115            throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0.0 exptected, got {1}", "lonExtent", lonExtent)); 
    114116 
    115         this.min = new LatLon( 
    116                 center.lat() - latExtent / 2, 
    117                 center.lon() - lonExtent / 2 
    118         ); 
    119         this.max = new LatLon( 
    120                 center.lat() + latExtent / 2, 
    121                 center.lon() + lonExtent / 2 
    122         ); 
     117        this.minLat = center.lat() - latExtent / 2; 
     118        this.minLon = center.lon() - lonExtent / 2; 
     119        this.maxLat = center.lat() + latExtent / 2; 
     120        this.maxLon = center.lon() + lonExtent / 2; 
    123121    } 
    124122 
    125123    @Override public String toString() { 
    126         return "Bounds["+min.lat()+","+min.lon()+","+max.lat()+","+max.lon()+"]"; 
     124        return "Bounds["+minLat+","+minLon+","+maxLat+","+maxLon+"]"; 
    127125    } 
    128126 
    129127    public String toShortString(DecimalFormat format) { 
    130128        return 
    131             format.format(min.lat()) + " " 
    132             + format.format(min.lon()) + " / " 
    133             + format.format(max.lat()) + " " 
    134             + format.format(max.lon()); 
     129        format.format(minLat) + " " 
     130        + format.format(minLon) + " / " 
     131        + format.format(maxLat) + " " 
     132        + format.format(maxLon); 
    135133    } 
    136134 
     
    140138    public LatLon getCenter() 
    141139    { 
    142         return min.getCenter(max); 
     140        return getMin().getCenter(getMax()); 
    143141    } 
    144142 
     
    147145     */ 
    148146    public void extend(LatLon ll) { 
    149         if (ll.lat() < min.lat() || ll.lon() < min.lon()) { 
    150             min = new LatLon(Math.min(ll.lat(), min.lat()), Math.min(ll.lon(), min.lon())); 
    151         } 
    152         if (ll.lat() > max.lat() || ll.lon() > max.lon()) { 
    153             max = new LatLon(Math.max(ll.lat(), max.lat()), Math.max(ll.lon(), max.lon())); 
    154         } 
     147        if (ll.lat() < minLat) { 
     148            minLat = ll.lat(); 
     149        } 
     150        if (ll.lon() < minLon) { 
     151            minLon = ll.lon(); 
     152        } 
     153        if (ll.lat() > maxLat) { 
     154            maxLat = ll.lat(); 
     155        } 
     156        if (ll.lon() > maxLon) { 
     157            maxLon = ll.lon(); 
     158        } 
     159    } 
     160 
     161    public void extend(Bounds b) { 
     162        extend(b.getMin()); 
     163        extend(b.getMax()); 
    155164    } 
    156165    /** 
     
    158167     */ 
    159168    public boolean contains(LatLon ll) { 
    160         if (ll.lat() < min.lat() || ll.lon() < min.lon()) 
    161             return false; 
    162         if (ll.lat() > max.lat() || ll.lon() > max.lon()) 
     169        if (ll.lat() < minLat || ll.lon() < minLon) 
     170            return false; 
     171        if (ll.lat() > maxLat || ll.lon() > maxLon) 
    163172            return false; 
    164173        return true; 
     
    170179     */ 
    171180    public Rectangle2D.Double asRect() { 
    172         return new Rectangle2D.Double(min.lon(), min.lat(), max.lon()-min.lon(), max.lat()-min.lat()); 
     181        return new Rectangle2D.Double(minLon, minLat, maxLon-minLon, maxLat-minLat); 
    173182    } 
    174183 
    175184    public double getArea() { 
    176         return (max.lon() - min.lon()) * (max.lat() - min.lat()); 
     185        return (maxLon - minLon) * (maxLat - minLat); 
    177186    } 
    178187 
    179188    public String encodeAsString(String separator) { 
    180189        StringBuffer sb = new StringBuffer(); 
    181         sb.append(min.lat()).append(separator).append(min.lon()) 
    182         .append(separator).append(max.lat()).append(separator) 
    183         .append(max.lon()); 
     190        sb.append(minLat).append(separator).append(minLon) 
     191        .append(separator).append(maxLat).append(separator) 
     192        .append(maxLon); 
    184193        return sb.toString(); 
    185194    } 
     
    189198        final int prime = 31; 
    190199        int result = 1; 
    191         result = prime * result + ((max == null) ? 0 : max.hashCode()); 
    192         result = prime * result + ((min == null) ? 0 : min.hashCode()); 
     200        long temp; 
     201        temp = Double.doubleToLongBits(maxLat); 
     202        result = prime * result + (int) (temp ^ (temp >>> 32)); 
     203        temp = Double.doubleToLongBits(maxLon); 
     204        result = prime * result + (int) (temp ^ (temp >>> 32)); 
     205        temp = Double.doubleToLongBits(minLat); 
     206        result = prime * result + (int) (temp ^ (temp >>> 32)); 
     207        temp = Double.doubleToLongBits(minLon); 
     208        result = prime * result + (int) (temp ^ (temp >>> 32)); 
    193209        return result; 
    194210    } 
     
    203219            return false; 
    204220        Bounds other = (Bounds) obj; 
    205         if (max == null) { 
    206             if (other.max != null) 
    207                 return false; 
    208         } else if (!max.equals(other.max)) 
    209             return false; 
    210         if (min == null) { 
    211             if (other.min != null) 
    212                 return false; 
    213         } else if (!min.equals(other.min)) 
     221        if (Double.doubleToLongBits(maxLat) != Double.doubleToLongBits(other.maxLat)) 
     222            return false; 
     223        if (Double.doubleToLongBits(maxLon) != Double.doubleToLongBits(other.maxLon)) 
     224            return false; 
     225        if (Double.doubleToLongBits(minLat) != Double.doubleToLongBits(other.minLat)) 
     226            return false; 
     227        if (Double.doubleToLongBits(minLon) != Double.doubleToLongBits(other.minLon)) 
    214228            return false; 
    215229        return true; 
Note: See TracChangeset for help on using the changeset viewer.