Changeset 329 in josm for branch/0.5/src/org/openstreetmap/josm/gui/NavigatableComponent.java
- Timestamp:
- 2007-09-24T01:36:24+02:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branch/0.5/src/org/openstreetmap/josm/gui/NavigatableComponent.java
r306 r329 4 4 import java.awt.Point; 5 5 import java.util.Collection; 6 import java.util.Collections;7 6 import java.util.HashSet; 8 import java.util.List;9 7 10 8 import javax.swing.JComponent; … … 16 14 import org.openstreetmap.josm.data.osm.Node; 17 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; 18 import org.openstreetmap.josm.data.osm.Segment;19 16 import org.openstreetmap.josm.data.osm.Way; 17 import org.openstreetmap.josm.data.osm.WaySegment; 20 18 import org.openstreetmap.josm.data.projection.Projection; 21 19 … … 147 145 148 146 /** 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) { 152 155 Way minPrimitive = null; 156 int minI = 0; 153 157 double minDistanceSq = Double.MAX_VALUE; 154 158 for (Way w : Main.ds.ways) { 155 159 if (w.deleted) 156 160 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; 159 168 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); 162 175 double c = A.distanceSq(B); 163 176 double a = p.distanceSq(B); … … 167 180 minDistanceSq = perDist; 168 181 minPrimitive = w; 169 } 182 minI = i; 183 } 184 lastN = n; 170 185 } 171 186 } 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); 183 195 } 184 196 185 197 /** 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; 211 203 } 212 204 … … 217 209 * nearest node is returned. 218 210 * 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>. 230 214 * 231 215 * @param p The point on screen. 232 * @param segmentInsteadWay Whether the segment (true) or only the whole233 * way should be returned.234 216 * @return The primitive, that is nearest to the point p. 235 217 */ 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 236 226 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); 243 228 } 244 229 245 230 /** 246 231 * @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. 258 233 * 259 234 * @return A collection of all items or <code>null</code> … … 262 237 */ 263 238 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>(); 286 240 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; 288 247 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); 292 257 break; 293 258 } 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); 295 265 } 296 266 } 297 return c;267 return nearest.isEmpty() ? null : nearest; 298 268 } 299 269
Note:
See TracChangeset
for help on using the changeset viewer.