Ticket #8902: quadbucketsindex.diff

File quadbucketsindex.diff, 5.1 KB (added by shinigami, 11 years ago)

index count optimization

  • src/org/openstreetmap/josm/data/coor/QuadTiling.java

     
    102102        int shift = NR_LEVELS-level-1;
    103103        return (int)((x >> shift & 1) * 2 + (y >> shift & 1));
    104104    }
     105
     106    static public int index(double lat, double lon, int level) {
     107        long x = lon2x(lon);
     108        long y = lat2y(lat);
     109        int shift = NR_LEVELS-level-1;
     110        return (int)((x >> shift & 1) * 2 + (y >> shift & 1));
     111    }
    105112}
  • src/org/openstreetmap/josm/data/osm/BBox.java

     
    77
    88import org.openstreetmap.josm.data.Bounds;
    99import org.openstreetmap.josm.data.coor.LatLon;
     10import org.openstreetmap.josm.data.coor.QuadTiling;
    1011import org.openstreetmap.josm.tools.Utils;
    1112
    1213public class BBox {
     
    151152        return true;
    152153    }
    153154
    154     /**
    155      * Returns a list of all 4 corners of the bbox rectangle.
    156      */
    157     public List<LatLon> points()  {
    158         LatLon p1 = new LatLon(ymin, xmin);
    159         LatLon p2 = new LatLon(ymin, xmax);
    160         LatLon p3 = new LatLon(ymax, xmin);
    161         LatLon p4 = new LatLon(ymax, xmax);
    162         List<LatLon> ret = new ArrayList<LatLon>(4);
    163         ret.add(p1);
    164         ret.add(p2);
    165         ret.add(p3);
    166         ret.add(p4);
    167         return ret;
    168     }
    169 
    170155    public LatLon getTopLeft() {
    171156        return new LatLon(ymax, xmin);
    172157    }
     
    179164        return new LatLon(ymin + (ymax-ymin)/2.0, xmin + (xmax-xmin)/2.0);
    180165    }
    181166
     167    int getIndex(final int level) {
     168
     169        int idx1 = QuadTiling.index(ymin, xmin, level);
     170
     171        final int idx2 = QuadTiling.index(ymin, xmax, level);
     172        if (idx1 == -1) idx1 = idx2;
     173        else if (idx1 != idx2) return -1;
     174
     175        final int idx3 = QuadTiling.index(ymax, xmin, level);
     176        if (idx1 == -1) idx1 = idx3;
     177        else if (idx1 != idx3) return -1;
     178
     179        final int idx4 = QuadTiling.index(ymax, xmax, level);
     180        if (idx1 == -1) idx1 = idx4;
     181        else if (idx1 != idx4) return -1;
     182
     183        return idx1;
     184    }
     185
    182186    @Override
    183187    public int hashCode() {
    184188        return (int)(ymin * xmin);
  • src/org/openstreetmap/josm/data/osm/QuadBuckets.java

     
    158158            if (!hasChildren())
    159159                return this;
    160160            else {
    161                 int index = get_index(bbox, level);
     161                int index = bbox.getIndex(level);
    162162                if (index == -1)
    163163                    return this;
    164164                return getChild(index).findBucket(bbox);
     
    183183            }
    184184            return ret;
    185185        }
    186         // Get the correct index for the given primitive
    187         // at the given level.  If the primitive can not
    188         // fit into a single quad at this level, return -1
    189         int get_index(BBox bbox, int level) {
    190             int index = -1;
    191             for (LatLon c : bbox.points()) {
    192                 /*if (debug) {
    193                     out("getting index for point: " + c);
    194                 }*/
    195                 if (index == -1) {
    196                     index = QuadTiling.index(c, level);
    197                     /*if (debug) {
    198                         out("set initial index to: " + index);
    199                     }*/
    200                     continue;
    201                 }
    202                 int another_index = QuadTiling.index(c, level);
    203                 /*if (debug) {
    204                     out("other point index: " + another_index);
    205                 }*/
    206                 if (another_index != index)
    207                     return -1;
    208             }
    209             return index;
    210         }
     186
    211187        /*
    212188         * There is a race between this and qb.nextContentNode().
    213189         * If nextContentNode() runs into this bucket, it may
     
    224200            content = null;
    225201
    226202            for (T o: tmpcontent) {
    227                 int index = get_index(o.getBBox(), level);
     203                int index = o.getBBox().getIndex(level);
    228204                if (index == -1) {
    229205                    __add_content(o);
    230206                } else {
     
    377353                if (!matches(o, this.bbox())) {
    378354                    /*out("-----------------------------");
    379355                    debug = true;*/
    380                     get_index(o.getBBox(), level);
    381                     get_index(o.getBBox(), level-1);
     356                    o.getBBox().getIndex(level);
     357                    o.getBBox().getIndex(level-1);
    382358                    int nr = 0;
    383359                    /*for (QBLevel sibling : parent.getChildren()) {
    384360                        out("sibling["+ (nr++) +"]: " + sibling.bbox() + " this: " + (this==sibling));