Changeset 2422 in josm for trunk/src/org


Ignore:
Timestamp:
2009-11-09T21:32:12+01:00 (14 years ago)
Author:
jttt
Message:
 
Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

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

    r2263 r2422  
    22package org.openstreetmap.josm.data.coor;
    33
    4 import org.openstreetmap.josm.data.osm.Node;
    5 
    6 import org.openstreetmap.josm.Main;
    7 import org.openstreetmap.josm.data.projection.Projection;
    84
    95public class QuadTiling
     
    4137            //    out("shift: " + shift + " bits: " + bits);
    4238            // remember x is the MSB
    43             if ((bits & 0x2) != 0)
     39            if ((bits & 0x2) != 0) {
    4440                x += x_unit;
    45             if ((bits & 0x1) != 0)
     41            }
     42            if ((bits & 0x1) != 0) {
    4643                y += y_unit;
     44            }
    4745            x_unit /= 2;
    4846            y_unit /= 2;
     
    5553    static long xy2tile(long x, long y)
    5654    {
    57        long tile = 0;
    58        int i;
    59        for (i = NR_LEVELS-1; i >= 0; i--)
    60        {
     55        long tile = 0;
     56        int i;
     57        for (i = NR_LEVELS-1; i >= 0; i--)
     58        {
    6159            long xbit = ((x >> i) & 1);
    6260            long ybit = ((y >> i) & 1);
     
    6462            // Note that x is the MSB
    6563            tile |= (xbit<<1) | ybit;
    66        }
    67        return tile;
     64        }
     65        return tile;
    6866    }
    6967    static long coorToTile(LatLon coor)
     
    7371    static long lon2x(double lon)
    7472    {
    75        //return Math.round((lon + 180.0) * QuadBuckets.WORLD_PARTS / 360.0)-1;
    76        long ret = (long)Math.floor((lon + 180.0) * WORLD_PARTS / 360.0);
    77        if (ret == WORLD_PARTS)
    78            ret--;
    79        return ret;
     73        //return Math.round((lon + 180.0) * QuadBuckets.WORLD_PARTS / 360.0)-1;
     74        long ret = (long)Math.floor((lon + 180.0) * WORLD_PARTS / 360.0);
     75        if (ret == WORLD_PARTS) {
     76            ret--;
     77        }
     78        return ret;
    8079    }
    8180    static long lat2y(double lat)
    8281    {
    83        //return Math.round((lat + 90.0) * QuadBuckets.WORLD_PARTS / 180.0)-1;
    84        long ret = (long)Math.floor((lat + 90.0) * WORLD_PARTS / 180.0);
    85        if (ret == WORLD_PARTS)
    86            ret--;
    87        return ret;
     82        //return Math.round((lat + 90.0) * QuadBuckets.WORLD_PARTS / 180.0)-1;
     83        long ret = (long)Math.floor((lat + 90.0) * WORLD_PARTS / 180.0);
     84        if (ret == WORLD_PARTS) {
     85            ret--;
     86        }
     87        return ret;
    8888    }
    8989    static public long quadTile(LatLon coor)
    9090    {
    9191        return xy2tile(lon2x(coor.lon()),
    92                        lat2y(coor.lat()));
     92                lat2y(coor.lat()));
    9393    }
    9494    static public int index(int level, long quad)
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r2381 r2422  
    2525import org.openstreetmap.josm.data.osm.Way;
    2626import org.openstreetmap.josm.data.osm.WaySegment;
     27import org.openstreetmap.josm.data.osm.QuadBuckets.BBox;
    2728import org.openstreetmap.josm.data.projection.Projection;
    2829import org.openstreetmap.josm.gui.help.Helpful;
     
    3637public class NavigatableComponent extends JComponent implements Helpful {
    3738
    38     public static final int snapDistance = sqr(Main.pref.getInteger("node.snap-distance", 10));
     39    public static final int snapDistance = Main.pref.getInteger("node.snap-distance", 10);
     40    public static final int snapDistanceSq = sqr(snapDistance);
    3941
    4042    private static int sqr(int a) { return a*a;}
     
    296298    }
    297299
     300    private BBox getSnapDistanceBBox(Point p) {
     301        return new BBox(getLatLon(p.x - snapDistance / 2, p.y - snapDistance / 2),
     302                getLatLon(p.x + snapDistance / 2, p.y + snapDistance / 2));
     303    }
     304
    298305    /**
    299306     * Return the nearest point to the screen point given.
     
    301308     */
    302309    public final Node getNearestNode(Point p) {
    303         double minDistanceSq = snapDistance;
    304         Node minPrimitive = null;
    305310        DataSet ds = getCurrentDataSet();
    306311        if (ds == null)
    307312            return null;
    308         for (Node n : ds.getNodes()) {
     313
     314        double minDistanceSq = snapDistanceSq;
     315        Node minPrimitive = null;
     316        for (Node n : ds.searchNodes(getSnapDistanceBBox(p))) {
    309317            if (!n.isUsable()) {
    310318                continue;
     
    319327            else if (dist == minDistanceSq && minPrimitive != null
    320328                    && ((n.isNew() && ds.isSelected(n))
    321                     || (!ds.isSelected(minPrimitive) && (ds.isSelected(n) || n.isNew())))) {
     329                            || (!ds.isSelected(minPrimitive) && (ds.isSelected(n) || n.isNew())))) {
    322330                minPrimitive = n;
    323331            }
     
    337345        if (ds == null)
    338346            return null;
    339         for (Way w : ds.getWays()) {
     347
     348        for (Way w : ds.searchWays(getSnapDistanceBBox(p))) {
    340349            if (!w.isUsable()) {
    341350                continue;
     
    359368                double b = p.distanceSq(A);
    360369                double perDist = a - (a - b + c) * (a - b + c) / 4 / c; // perpendicular distance squared
    361                 if (perDist < snapDistance && a < c + snapDistance && b < c + snapDistance) {
     370                if (perDist < snapDistanceSq && a < c + snapDistanceSq && b < c + snapDistanceSq) {
    362371                    if (ds.isSelected(w)) {
    363372                        perDist -= 0.00001;
     
    450459    /**
    451460     * @return A list of all objects that are nearest to
    452      * the mouse.  Does a simple sequential scan on all the data.
     461     * the mouse.
    453462     *
    454463     * @return A collection of all items or <code>null</code>
     
    461470        if (ds == null)
    462471            return null;
    463         for (Way w : ds.getWays()) {
     472        for (Way w : ds.searchWays(getSnapDistanceBBox(p))) {
    464473            if (!w.isUsable()) {
    465474                continue;
     
    480489                double b = p.distanceSq(A);
    481490                double perDist = a - (a - b + c) * (a - b + c) / 4 / c; // perpendicular distance squared
    482                 if (perDist < snapDistance && a < c + snapDistance && b < c + snapDistance) {
     491                if (perDist < snapDistanceSq && a < c + snapDistanceSq && b < c + snapDistanceSq) {
    483492                    nearest.add(w);
    484493                    break;
     
    487496            }
    488497        }
    489         for (Node n : ds.getNodes()) {
     498        for (Node n : ds.searchNodes(getSnapDistanceBBox(p))) {
    490499            if (n.isUsable()
    491                     && getPoint(n).distanceSq(p) < snapDistance) {
     500                    && getPoint(n).distanceSq(p) < snapDistanceSq) {
    492501                nearest.add(n);
    493502            }
     
    498507    /**
    499508     * @return A list of all nodes that are nearest to
    500      * the mouse.  Does a simple sequential scan on all the data.
     509     * the mouse.
    501510     *
    502511     * @return A collection of all nodes or <code>null</code>
     
    509518        if (ds == null)
    510519            return null;
    511         for (Node n : ds.getNodes()) {
     520
     521        for (Node n : ds.searchNodes(getSnapDistanceBBox(p))) {
    512522            if (n.isUsable()
    513                     && getPoint(n).distanceSq(p) < snapDistance) {
     523                    && getPoint(n).distanceSq(p) < snapDistanceSq) {
    514524                nearest.add(n);
    515525            }
Note: See TracChangeset for help on using the changeset viewer.