Ticket #8851: better_intersections.patch
| File better_intersections.patch, 5.1 KB (added by , 13 years ago) |
|---|
-
src/org/openstreetmap/josm/actions/JoinAreasAction.java
314 314 * Gets called whenever the shortcut is pressed or the menu entry is selected 315 315 * Checks whether the selected objects are suitable to join and joins them if so 316 316 */ 317 @Override 317 318 public void actionPerformed(ActionEvent e) { 318 319 LinkedList<Way> ways = new LinkedList<Way>(Main.main.getCurrentDataSet().getSelectedWays()); 319 320 … … 1294 1295 * @return The list of relation with roles to add own relation to 1295 1296 */ 1296 1297 private RelationRole addOwnMultigonRelation(Collection<Way> inner, Way outer) { 1297 if (inner. size() == 0) return null;1298 if (inner.isEmpty()) return null; 1298 1299 // Create new multipolygon relation and add all inner ways to it 1299 1300 Relation newRel = new Relation(); 1300 1301 newRel.put("type", "multipolygon"); … … 1370 1371 cmds.add(new ChangeCommand(r.rel, newRel)); 1371 1372 } 1372 1373 1373 Relation newRel = null;1374 Relation newRel; 1374 1375 switch (multiouters.size()) { 1375 1376 case 0: 1376 1377 return; -
src/org/openstreetmap/josm/tools/Geometry.java
50 50 51 51 //stupid java, cannot instantiate array of generic classes.. 52 52 @SuppressWarnings("unchecked") 53 ArrayList<Node>[] newNodes = new ArrayList[ways.size()]; 54 BBox[] wayBounds = new BBox[ways.size()]; 55 boolean[] changedWays = new boolean[ways.size()]; 53 int n = ways.size(); 54 ArrayList<Node>[] newNodes = new ArrayList[n]; 55 BBox[] wayBounds = new BBox[n]; 56 boolean[] changedWays = new boolean[n]; 56 57 57 58 Set<Node> intersectionNodes = new LinkedHashSet<Node>(); 58 59 59 60 //copy node arrays for local usage. 60 for (int pos = 0; pos < ways.size(); pos ++) {61 for (int pos = 0; pos < n; pos ++) { 61 62 newNodes[pos] = new ArrayList<Node>(ways.get(pos).getNodes()); 62 63 wayBounds[pos] = getNodesBounds(newNodes[pos]); 63 64 changedWays[pos] = false; … … 65 66 66 67 //iterate over all way pairs and introduce the intersections 67 68 Comparator<Node> coordsComparator = new NodePositionComparator(); 69 WayLoop: for (int seg1Way = 0; seg1Way < n; seg1Way ++) { 70 for (int seg2Way = seg1Way; seg2Way < n; seg2Way ++) { 68 71 69 WayLoop: for (int seg1Way = 0; seg1Way < ways.size(); seg1Way ++) {70 for (int seg2Way = seg1Way; seg2Way < ways.size(); seg2Way ++) {71 72 72 //do not waste time on bounds that do not intersect 73 73 if (!wayBounds[seg1Way].intersects(wayBounds[seg2Way])) { 74 74 continue; … … 127 127 Node intNode = newNode; 128 128 boolean insertInSeg1 = false; 129 129 boolean insertInSeg2 = false; 130 131 130 //find if the intersection point is at end point of one of the segments, if so use that point 132 131 133 132 //segment 1 … … 265 264 //TODO: do this locally. 266 265 if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null; 267 266 268 // Convert line from (point, point) form to ax+by=c 269 double a1 = y2 - y1; 270 double b1 = x1 - x2; 271 double c1 = x2*y1 - x1*y2; 267 // solve line-line intersection in parametric form: 268 // (x1,y1) + (x2-x1,y2-y1)* u = (x3,y3) + (x4-x3,y4-y3)* v 269 // (x2-x1,y2-y1)*u - (x4-x3,y4-y3)*v = (x3-x1,y3-y1) 270 // if 0<= u,v <=1, intersection exists at ( x1+ (x2-x1)*u, y1 + (y2-y1)*u ) 271 272 double a1 = x2 - x1; 273 double b1 = x3 - x4; 274 double c1 = x3 - x1; 272 275 273 double a2 = y 4 - y3;274 double b2 = x3 - x4;275 double c2 = x4*y3 - x3*y4;276 double a2 = y2 - y1; 277 double b2 = y3 - y4; 278 double c2 = y3 - y1; 276 279 277 280 // Solve the equations 278 281 double det = a1*b2 - a2*b1; 279 if (det == 0) return null; // Lines are parallel 280 281 double x = (b1*c2 - b2*c1)/det; 282 double y = (a2*c1 -a1*c2)/det; 283 284 return new EastNorth(x, y); 282 283 double uu = b2*c1 - b1*c2 ; 284 double vv = a1*c2 - a2*c1; 285 double mag = Math.abs(uu)+Math.abs(vv); 286 287 if (Math.abs(det) > 1e-12 * mag) { 288 double u = uu/det, v = vv/det; 289 if (u>-1e-8 && u < 1+1e-8 && v>-1e-8 && v < 1+1e-8 ) { 290 if (u<0) u=0; 291 if (u>1) u=1.0; 292 return new EastNorth(x1+a1*u, y1+a2*u); 293 } else { 294 return null; 295 } 296 } else { 297 // parallel lines 298 return null; 299 } 285 300 } 286 301 287 302 /**
