Changeset 2427 in josm for trunk/src/org


Ignore:
Timestamp:
2009-11-10T08:15:02+01:00 (14 years ago)
Author:
jttt
Message:

Added getBBox method to OsmPrimitive. Used it in QuadBuckets instead of testing whether element is Node or Way. Temporary removed waybbox_cache, bbox will be cached by Way itself

Location:
trunk/src/org/openstreetmap/josm/data/osm
Files:
5 edited

Legend:

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

    r2419 r2427  
    55import org.openstreetmap.josm.data.coor.EastNorth;
    66import org.openstreetmap.josm.data.coor.LatLon;
     7import org.openstreetmap.josm.data.osm.QuadBuckets.BBox;
    78import org.openstreetmap.josm.data.osm.visitor.Visitor;
    89
     
    175176        return OsmPrimitiveType.NODE;
    176177    }
     178
     179    public BBox getBBox() {
     180        if (coor == null)
     181            return new BBox(0, 0, 0, 0);
     182        else
     183            return new BBox(coor, coor);
     184    }
    177185}
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r2417 r2427  
    1919
    2020import org.openstreetmap.josm.Main;
     21import org.openstreetmap.josm.data.osm.QuadBuckets.BBox;
    2122import org.openstreetmap.josm.data.osm.visitor.Visitor;
    2223import org.openstreetmap.josm.gui.mappaint.ElemStyle;
     
    10071008    }
    10081009
     1010    public abstract BBox getBBox();
     1011
    10091012}
    10101013
  • trunk/src/org/openstreetmap/josm/data/osm/QuadBuckets.java

    r2425 r2427  
    11package org.openstreetmap.josm.data.osm;
     2
    23import java.lang.reflect.Array;
    34import java.util.ArrayList;
     
    56import java.util.Collection;
    67import java.util.Collections;
    7 import java.util.HashMap;
    88import java.util.Iterator;
    99import java.util.LinkedList;
     
    190190        }
    191191    }
    192     /*
    193      * This is a quick hack.  The problem is that we need the
    194      * way's bounding box a *bunch* of times when it gets
    195      * inserted.  It gets expensive if we have to recreate
    196      * them each time.
    197      *
    198      * An alternative would be to calculate it at .add() time
    199      * and passing it down the call chain.
    200      */
    201     HashMap<Way,BBox> way_bbox_cache = new HashMap<Way, BBox>();
    202     BBox way_bbox(Way w)
    203     {
    204         if (way_bbox_cache.size() > 100) {
    205             way_bbox_cache.clear();
    206         }
    207         BBox b = way_bbox_cache.get(w);
    208         if (b == null) {
    209             b = new BBox(w);
    210             way_bbox_cache.put(w, b);
    211         }
    212         return b;
    213         //return new BBox(w);
    214     }
    215 
    216192    class QBLevel
    217193    {
     
    247223                this.content = null;
    248224            }
    249             if (this.canRemove())
     225            if (this.canRemove()) {
    250226                this.remove_from_parent();
     227            }
    251228            return ret;
    252229        }
     
    268245                out("getting index for " + o + " at level: " + level);
    269246            }
    270             if (o instanceof Node) {
    271                 LatLon coor = ((Node)o).getCoor();
    272                 if (coor == null)
     247            int index = -1;
     248            for (LatLon c : o.getBBox().points()) {
     249                if (debug) {
     250                    out("getting index for point: " + c);
     251                }
     252                if (index == -1) {
     253                    index = QuadTiling.index(c, level);
     254                    if (debug) {
     255                        out("set initial index to: " + index);
     256                    }
     257                    continue;
     258                }
     259                int another_index = QuadTiling.index(c, level);
     260                if (debug) {
     261                    out("other point index: " + another_index);
     262                }
     263                if (another_index != index) {
     264                    // This happens at level 0 sometimes when a way has no
     265                    // nodes present.
     266                    if (debug) {
     267                        out("primitive ("+o.getId()+") would have gone across two quads "
     268                                + another_index + "/" + index + " at level: " + level + "    ");
     269                    }
    273270                    return -1;
    274                 return QuadTiling.index(coor, level);
    275             }
    276             if (o instanceof Way) {
    277                 Way w = (Way)o;
    278                 int index = -1;
    279                 //for (Node n : w.getNodes()) {
    280                 for (LatLon c : way_bbox(w).points()) {
    281                     //    LatLon c = n.getCoor();
    282                     if (debug) {
    283                         out("getting index for point: " + c);
    284                     }
    285                     if (index == -1) {
    286                         index = QuadTiling.index(c, level);
    287                         if (debug) {
    288                             out("set initial way index to: " + index);
    289                         }
    290                         continue;
    291                     }
    292                     int node_index = QuadTiling.index(c, level);
    293                     if (debug) {
    294                         out("other node way index: " + node_index);
    295                     }
    296                     if (node_index != index) {
    297                         // This happens at level 0 sometimes when a way has no
    298                         // nodes present.
    299                         if (debug) {
    300                             out("way ("+w.getId()+") would have gone across two quads "
    301                                     + node_index + "/" + index + " at level: " + level + "    ");
    302                             out("node count: " + w.getNodes().size());
    303                             for (LatLon c2 : way_bbox(w).points()) {
    304                                 out("points: " + c2);
    305                             }
    306                         }
    307                         return -1;
    308                     }
    309                 }
    310                 return index;
    311             }
    312             abort("bad primitive: " + o);
    313             return -1;
     271                }
     272            }
     273            return index;
    314274        }
    315275        void split()
     
    367327                }
    368328                if (level >= NR_LEVELS-1) {
    369                     if (debug)
     329                    if (debug) {
    370330                        out("can not split, but too deep: " + level + " size: " + content.size());
     331                    }
    371332                    return;
    372333                }
     
    388349        boolean matches(T o, BBox search_bbox)
    389350        {
    390             if (o instanceof Node) {
    391                 LatLon coor = ((Node)o).getCoor();
    392                 if (coor == null)
    393                     return false;
    394                 return search_bbox.bounds(coor);
    395             }
    396             if (o instanceof Way) {
    397                 BBox bbox = way_bbox((Way)o);
    398                 return bbox.intersects(search_bbox);
    399             }
    400             abort("matches() bad primitive: " + o);
    401             return false;
     351            return o.getBBox().intersects(search_bbox);
    402352        }
    403353        private List<T> search_contents(BBox search_bbox)
     
    497447                if (debug) {
    498448                    out("no siblings at level["+next.level+"], moving to parent");
    499                     }
     449                }
    500450                next = next.parent;
    501451                if (next == null) {
    502452                    break;
    503                     }
     453                }
    504454                sibling = next.next_sibling();
    505455            }
     
    510460        {
    511461            QBLevel next = this;
    512             if (this.isLeaf())
     462            if (this.isLeaf()) {
    513463                next = this.nextSibling();
     464            }
    514465            if (next == null)
    515466                return null;
    516467            // Walk back down the tree
    517468            // and find the first leaf
    518             while (next != null && !next.isLeaf()) {
    519                 if (next.hasContent() && next != this)
     469            while (!next.isLeaf()) {
     470                if (next.hasContent() && next != this) {
    520471                    break;
    521                 if (debug)
     472                }
     473                if (debug) {
    522474                    out("["+next.level+"] next node ("+next+") is a branch (content: "+next.hasContent()+"), moving down...");
     475                }
    523476                boolean progress = false;
    524477                for (QBLevel child : next.children) {
    525                     if (child == null)
     478                    if (child == null) {
    526479                        continue;
     480                    }
    527481                    next = child;
    528482                    progress = true;
     
    817771            for (int i = 0; i < parent.children.length; i++) {
    818772                QBLevel sibling = parent.children[i];
    819                 if (sibling != null)
     773                if (sibling != null) {
    820774                    nr_siblings++;
    821                 if (sibling != this)
     775                }
     776                if (sibling != this) {
    822777                    continue;
    823                 if (!canRemove())
     778                }
     779                if (!canRemove()) {
    824780                    abort("attempt to remove non-empty child: " + this.content + " " + this.children);
     781                }
    825782                parent.children[i] = null;
    826783                nr_siblings--;
    827784            }
    828             if (parent.canRemove())
     785            if (parent.canRemove()) {
    829786                parent.remove_from_parent();
     787            }
    830788        }
    831789        boolean canRemove()
     
    911869        return true;
    912870    }
    913     boolean canStore(Object o)
    914     {
    915         if (o instanceof Way)
    916             return true;
    917         if (o instanceof Node)
    918             return true;
    919         return false;
    920     }
    921871    public boolean removeAll(Collection<?> objects)
    922872    {
     873        boolean changed = false;
    923874        for (Object o : objects) {
    924             if (!canStore(o))
    925                 return false;
    926             if (!this.remove(o))
     875            changed = changed | remove(o);
     876        }
     877        return changed;
     878    }
     879    public boolean addAll(Collection<? extends T> objects)
     880    {
     881        boolean changed = false;
     882        for (Object o : objects) {
     883            changed = changed | this.add(convert(o));
     884        }
     885        return changed;
     886    }
     887    public boolean containsAll(Collection<?> objects)
     888    {
     889        for (Object o : objects) {
     890            if (!this.contains(o))
    927891                return false;
    928892        }
    929893        return true;
    930     }
    931     public boolean addAll(Collection<? extends T> objects)
    932     {
    933         for (Object o : objects) {
    934             if (!canStore(o))
    935                 return false;
    936             if (!this.add(convert(o)))
    937                 return false;
    938         }
    939         return true;
    940     }
    941     public boolean containsAll(Collection<?> objects)
    942     {
    943         for (Object o : objects) {
    944             if (!canStore(o))
    945                 return false;
    946             if (!this.contains(o))
    947                 return false;
    948         }
    949         return true;
    950     }
    951     private void check_type(Object o)
    952     {
    953         if (canStore(o))
    954             return;
    955         unsupported();
    956894    }
    957895    // If anyone has suggestions for how to fix
     
    964902    public boolean remove(Object o)
    965903    {
    966         check_type(o);
    967904        return this.remove(convert(o));
    968905    }
     
    991928         */
    992929        QBLevel bucket = root.find_exact(o);
    993         if (o instanceof Way) {
    994             way_bbox_cache.remove(o);
    995         }
    996930        /*
    997931         * That may fail because the object was
     
    1009943    public boolean contains(Object o)
    1010944    {
    1011         if (!canStore(o))
    1012             return false;
    1013945        QBLevel bucket = root.find_exact(convert(o));
    1014946        if (bucket == null)
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r2410 r2427  
    77import java.util.Set;
    88
     9import org.openstreetmap.josm.data.osm.QuadBuckets.BBox;
    910import org.openstreetmap.josm.data.osm.visitor.Visitor;
    1011import org.openstreetmap.josm.tools.CopyList;
     
    324325        return OsmPrimitiveType.RELATION;
    325326    }
     327
     328    @Override
     329    public BBox getBBox() {
     330        return new BBox(0, 0, 0, 0);
     331    }
    326332}
  • trunk/src/org/openstreetmap/josm/data/osm/Way.java

    r2419 r2427  
    99import java.util.List;
    1010
     11import org.openstreetmap.josm.data.osm.QuadBuckets.BBox;
    1112import org.openstreetmap.josm.data.osm.visitor.Visitor;
    1213import org.openstreetmap.josm.tools.CopyList;
     
    362363        }
    363364    }
     365
     366    @Override
     367    public BBox getBBox() {
     368        // TODO Precalculate way bbox (and update it every time nodes are moved or edited)
     369        return new BBox(this);
     370    }
    364371}
Note: See TracChangeset for help on using the changeset viewer.