Ignore:
Timestamp:
2007-09-24T01:36:24+02:00 (17 years ago)
Author:
framm
Message:

This commit is a manual merge of all changes that have been made to
the intermediate "core_0.5" branch on the main OSM repository,
bevore JOSM was moved to openstreetmap.de.

Changes incorporated here:

r4464@svn.openstreetmap.org
r4466@svn.openstreetmap.org
r4468@svn.openstreetmap.org
r4469@svn.openstreetmap.org
r4479@svn.openstreetmap.org

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branch/0.5/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r306 r329  
    44import java.awt.Point;
    55import java.util.Collection;
    6 import java.util.Collections;
    76import java.util.HashSet;
    8 import java.util.List;
    97
    108import javax.swing.JComponent;
     
    1614import org.openstreetmap.josm.data.osm.Node;
    1715import org.openstreetmap.josm.data.osm.OsmPrimitive;
    18 import org.openstreetmap.josm.data.osm.Segment;
    1916import org.openstreetmap.josm.data.osm.Way;
     17import org.openstreetmap.josm.data.osm.WaySegment;
    2018import org.openstreetmap.josm.data.projection.Projection;
    2119
     
    147145
    148146        /**
    149          * @return the nearest way to the screen point given.
    150          */
    151         public final Way getNearestWay(Point p) {
     147         * @return the nearest way segment to the screen point given that is not
     148         * in ignore.
     149         *
     150         * @param p the point for which to search the nearest segment.
     151         * @param ignore a collection of segments which are not to be returned.
     152         * May be null.
     153         */
     154        public final WaySegment getNearestWaySegment(Point p, Collection<WaySegment> ignore) {
    152155                Way minPrimitive = null;
     156                int minI = 0;
    153157                double minDistanceSq = Double.MAX_VALUE;
    154158                for (Way w : Main.ds.ways) {
    155159                        if (w.deleted)
    156160                                continue;
    157                         for (Segment ls : w.segments) {
    158                                 if (ls.deleted || ls.incomplete)
     161                        Node lastN = null;
     162                        int i = -2;
     163                        for (Node n : w.nodes) {
     164                                i++;
     165                                if (n.deleted) continue;
     166                                if (lastN == null) {
     167                                        lastN = n;
    159168                                        continue;
    160                                 Point A = getPoint(ls.from.eastNorth);
    161                                 Point B = getPoint(ls.to.eastNorth);
     169                                }
     170                                if (ignore != null && ignore.contains(new WaySegment(w, i))) {
     171                                        continue;
     172                                }
     173                                Point A = getPoint(lastN.eastNorth);
     174                                Point B = getPoint(n.eastNorth);
    162175                                double c = A.distanceSq(B);
    163176                                double a = p.distanceSq(B);
     
    167180                                        minDistanceSq = perDist;
    168181                                        minPrimitive = w;
    169                                 }
     182                                        minI = i;
     183                                }
     184                                lastN = n;
    170185                        }
    171186                }
    172                 return minPrimitive;
    173         }
    174 
    175         /**
    176          * @return the nearest segment to the screen point given
    177          *
    178          * @param p the point for which to search the nearest segment.
    179          */
    180         public final Segment getNearestSegment(Point p) {
    181                 List<Segment> e = Collections.emptyList();
    182                 return getNearestSegment(p, e);
     187                return minPrimitive == null ? null : new WaySegment(minPrimitive, minI);
     188        }
     189
     190        /**
     191         * @return the nearest way segment to the screen point given.
     192         */
     193        public final WaySegment getNearestWaySegment(Point p) {
     194                return getNearestWaySegment(p, null);
    183195        }
    184196       
    185197        /**
    186          * @return the nearest segment to the screen point given that is not
    187          * in ignoreThis.
    188          *
    189          * @param p the point for which to search the nearest segment.
    190          * @param ignore a collection of segments which are not to be returned. Must not be null.
    191          */
    192         public final Segment getNearestSegment(Point p, Collection<Segment> ignore) {
    193                 Segment minPrimitive = null;
    194                 double minDistanceSq = Double.MAX_VALUE;
    195                 // segments
    196                 for (Segment ls : Main.ds.segments) {
    197                         if (ls.deleted || ls.incomplete || ignore.contains(ls))
    198                                 continue;
    199                         Point A = getPoint(ls.from.eastNorth);
    200                         Point B = getPoint(ls.to.eastNorth);
    201                         double c = A.distanceSq(B);
    202                         double a = p.distanceSq(B);
    203                         double b = p.distanceSq(A);
    204                         double perDist = a-(a-b+c)*(a-b+c)/4/c; // perpendicular distance squared
    205                         if (perDist < 100 && minDistanceSq > perDist && a < c+100 && b < c+100) {
    206                                 minDistanceSq = perDist;
    207                                 minPrimitive = ls;
    208                         }
    209                 }
    210                 return minPrimitive;
     198         * @return the nearest way to the screen point given.
     199         */
     200        public final Way getNearestWay(Point p) {
     201                WaySegment nearestWaySeg = getNearestWaySegment(p);
     202                return nearestWaySeg == null ? null : nearestWaySeg.way;
    211203    }
    212204
     
    217209         * nearest node is returned.
    218210         *
    219          * If no node is found, search for pending segments.
    220          *
    221          * If no such segment is found, and a non-pending segment is
    222          * within 10 pixel to p, this segment is returned, except when
    223          * <code>wholeWay</code> is <code>true</code>, in which case the
    224          * corresponding Way is returned.
    225          *
    226          * If no segment is found and the point is within an area, return that
    227          * area.
    228          *
    229          * If no area is found, return <code>null</code>.
     211         * If no node is found, search for near ways.
     212         *
     213         * If nothing is found, return <code>null</code>.
    230214         *
    231215         * @param p                              The point on screen.
    232          * @param segmentInsteadWay Whether the segment (true) or only the whole
    233          *                                               way should be returned.
    234216         * @return      The primitive, that is nearest to the point p.
    235217         */
     218        public OsmPrimitive getNearest(Point p) {
     219                OsmPrimitive osm = getNearestNode(p);
     220                if (osm == null)
     221                        osm = getNearestWay(p);
     222                return osm;
     223        }
     224
     225        @Deprecated
    236226        public OsmPrimitive getNearest(Point p, boolean segmentInsteadWay) {
    237                 OsmPrimitive osm = getNearestNode(p);
    238                 if (osm == null && !segmentInsteadWay)
    239                         osm = getNearestWay(p);
    240                 if (osm == null)
    241                         osm = getNearestSegment(p);
    242                 return osm;
     227                return getNearest(p);
    243228        }
    244229
    245230        /**
    246231         * @return A list of all objects that are nearest to
    247          * the mouse. To do this, first the nearest object is
    248          * determined.
    249          *
    250          * If its a node, return all segments and
    251          * streets the node is part of, as well as all nodes
    252          * (with their segments and ways) with the same
    253          * location.
    254          *
    255          * If its a segment, return all ways this segment
    256          * belongs to as well as all segments that are between
    257          * the same nodes (in both direction) with all their ways.
     232         * the mouse.  Does a simple sequential scan on all the data.
    258233         *
    259234         * @return A collection of all items or <code>null</code>
     
    262237         */
    263238        public Collection<OsmPrimitive> getAllNearest(Point p) {
    264                 OsmPrimitive osm = getNearest(p, true);
    265                 if (osm == null)
    266                         return null;
    267                 Collection<OsmPrimitive> c = new HashSet<OsmPrimitive>();
    268                 c.add(osm);
    269                 if (osm instanceof Node) {
    270                         Node node = (Node)osm;
    271                         for (Node n : Main.ds.nodes)
    272                                 if (!n.deleted && n.coor.equals(node.coor))
    273                                         c.add(n);
    274                         for (Segment ls : Main.ds.segments)
    275                                 // segments never match nodes, so they are skipped by contains
    276                                 if (!ls.deleted && !ls.incomplete && (c.contains(ls.from) || c.contains(ls.to)))
    277                                         c.add(ls);
    278                 }
    279                 if (osm instanceof Segment) {
    280                         Segment line = (Segment)osm;
    281                         for (Segment ls : Main.ds.segments)
    282                                 if (!ls.deleted && ls.equalPlace(line))
    283                                         c.add(ls);
    284                 }
    285                 if (osm instanceof Node || osm instanceof Segment) {
     239                Collection<OsmPrimitive> nearest = new HashSet<OsmPrimitive>();
    286240                        for (Way w : Main.ds.ways) {
    287                                 if (w.deleted)
     241                        if (w.deleted) continue;
     242                        Node lastN = null;
     243                        for (Node n : w.nodes) {
     244                                if (n.deleted) continue;
     245                                if (lastN == null) {
     246                                        lastN = n;
    288247                                        continue;
    289                                 for (Segment ls : w.segments) {
    290                                         if (!ls.deleted && !ls.incomplete && c.contains(ls)) {
    291                                                 c.add(w);
     248                                }
     249                                Point A = getPoint(lastN.eastNorth);
     250                                Point B = getPoint(n.eastNorth);
     251                                double c = A.distanceSq(B);
     252                                double a = p.distanceSq(B);
     253                                double b = p.distanceSq(A);
     254                                double perDist = a-(a-b+c)*(a-b+c)/4/c; // perpendicular distance squared
     255                                if (perDist < 100 && a < c+100 && b < c+100) {
     256                                        nearest.add(w);
    292257                                                break;
    293258                                        }
    294                                 }
     259                                lastN = n;
     260                                }
     261                        }
     262                for (Node n : Main.ds.nodes) {
     263                        if (!n.deleted && getPoint(n.eastNorth).distanceSq(p) < 100) {
     264                                nearest.add(n);
    295265                        }
    296266                }
    297                 return c;
     267                return nearest.isEmpty() ? null : nearest;
    298268        }
    299269
Note: See TracChangeset for help on using the changeset viewer.