Changeset 19703 in josm
- Timestamp:
- 2026-11-23T06:35:25+01:00 (88 minutes ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Geometry.java
r19446 r19703 87 87 public static final double INTERSECTION_EPS_EAST_NORTH = 1e-4; 88 88 89 private static final String INVALID_COORDINATE_MSG = " invalid"; 90 89 91 /** 90 92 * Will find all intersection and add nodes there for list of given ways. … … 103 105 public static Set<Node> addIntersections(List<Way> ways, boolean test, List<Command> cmds) { 104 106 105 int n = ways.size(); 107 final int n = ways.size(); 106 108 @SuppressWarnings("unchecked") 109 final 107 110 List<Node>[] newNodes = new ArrayList[n]; 108 BBox[] wayBounds = new BBox[n]; 109 boolean[] changedWays = new boolean[n]; 110 111 Set<Node> intersectionNodes = new LinkedHashSet<>(); 111 final BBox[] wayBounds = new BBox[n]; 112 final boolean[] changedWays = new boolean[n]; 113 114 final Set<Node> intersectionNodes = new LinkedHashSet<>(); 112 115 113 116 //copy node arrays for local usage. … … 118 121 } 119 122 120 DataSet dataset = ways.get(0).getDataSet(); 123 final DataSet dataset = ways.get(0).getDataSet(); 121 124 122 125 //iterate over all way pairs and introduce the intersections 123 Comparator<Node> coordsComparator = new NodePositionComparator(); 126 final Comparator<Node> coordsComparator = new NodePositionComparator(); 124 127 for (int seg1Way = 0; seg1Way < n; seg1Way++) { 125 128 for (int seg2Way = seg1Way; seg2Way < n; seg2Way++) { … … 130 133 } 131 134 132 List<Node> way1Nodes = newNodes[seg1Way]; 133 List<Node> way2Nodes = newNodes[seg2Way]; 135 final List<Node> way1Nodes = newNodes[seg1Way]; 136 final List<Node> way2Nodes = newNodes[seg2Way]; 134 137 135 138 //iterate over primary segment … … 137 140 138 141 //iterate over secondary segment 139 int seg2Start = seg1Way != seg2Way ? 0 : seg1Pos + 2; //skip the adjacent segment 142 final int seg2Start = seg1Way != seg2Way ? 0 : seg1Pos + 2; //skip the adjacent segment 140 143 141 144 for (int seg2Pos = seg2Start; seg2Pos + 1 < way2Nodes.size(); seg2Pos++) { 142 145 143 146 //need to get them again every time, because other segments may be changed 144 Node seg1Node1 = way1Nodes.get(seg1Pos); 145 Node seg1Node2 = way1Nodes.get(seg1Pos + 1); 146 Node seg2Node1 = way2Nodes.get(seg2Pos); 147 Node seg2Node2 = way2Nodes.get(seg2Pos + 1); 147 final Node seg1Node1 = way1Nodes.get(seg1Pos); 148 final Node seg1Node2 = way1Nodes.get(seg1Pos + 1); 149 final Node seg2Node1 = way2Nodes.get(seg2Pos); 150 final Node seg2Node2 = way2Nodes.get(seg2Pos + 1); 148 151 149 152 int commonCount = 0; … … 169 172 //no common nodes - find intersection 170 173 if (commonCount == 0) { 171 EastNorth intersection = getSegmentSegmentIntersection( 174 final EastNorth intersection = getSegmentSegmentIntersection( 172 175 seg1Node1.getEastNorth(), seg1Node2.getEastNorth(), 173 176 seg2Node1.getEastNorth(), seg2Node2.getEastNorth()); 174 177 175 178 if (intersection != null) { 176 Node newNode = new Node(ProjectionRegistry.getProjection().eastNorth2latlon(intersection)); 179 final Node newNode = new Node(ProjectionRegistry.getProjection().eastNorth2latlon(intersection)); 177 180 Node intNode = newNode; 178 181 boolean insertInSeg1 = false; … … 227 230 } 228 231 } 229 } else if (test && !intersectionNodes.isEmpty()) 232 } else if (test && !intersectionNodes.isEmpty()) { 230 233 return intersectionNodes; 234 } 231 235 } 232 236 } … … 261 265 */ 262 266 public static <N extends INode> boolean isToTheRightSideOfLine(N lineP1, N lineP2, N lineP3, N testPoint) { 263 boolean pathBendToRight = angleIsClockwise(lineP1, lineP2, lineP3); 264 boolean rightOfSeg1 = angleIsClockwise(lineP1, lineP2, testPoint); 265 boolean rightOfSeg2 = angleIsClockwise(lineP2, lineP3, testPoint); 266 267 if (pathBendToRight) 267 final boolean pathBendToRight = angleIsClockwise(lineP1, lineP2, lineP3); 268 final boolean rightOfSeg1 = angleIsClockwise(lineP1, lineP2, testPoint); 269 final boolean rightOfSeg2 = angleIsClockwise(lineP2, lineP3, testPoint); 270 271 if (pathBendToRight) { 268 272 return rightOfSeg1 && rightOfSeg2; 269 else273 } else { 270 274 return !(!rightOfSeg1 && !rightOfSeg2); 275 } 271 276 } 272 277 … … 295 300 // see the ILatLon version for an explanation why the checks are in the if statement 296 301 if (!(p1.isValid() && p2.isValid() && p3.isValid() && p4.isValid())) { 297 CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");298 CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");299 CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");300 CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + " invalid");301 } 302 303 double x1 = p1.getX(); 304 double y1 = p1.getY(); 305 double x2 = p2.getX(); 306 double y2 = p2.getY(); 307 double x3 = p3.getX(); 308 double y3 = p3.getY(); 309 double x4 = p4.getX(); 310 double y4 = p4.getY(); 311 double[] en = getSegmentSegmentIntersection(x1, y1, x2, y2, x3, y3, x4, y4); 302 CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + INVALID_COORDINATE_MSG); 303 CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + INVALID_COORDINATE_MSG); 304 CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + INVALID_COORDINATE_MSG); 305 CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + INVALID_COORDINATE_MSG); 306 } 307 308 final double x1 = p1.getX(); 309 final double y1 = p1.getY(); 310 final double x2 = p2.getX(); 311 final double y2 = p2.getY(); 312 final double x3 = p3.getX(); 313 final double y3 = p3.getY(); 314 final double x4 = p4.getX(); 315 final double y4 = p4.getY(); 316 final double[] en = getSegmentSegmentIntersection(x1, y1, x2, y2, x3, y3, x4, y4); 312 317 if (en != null && en.length == 2) { 313 318 return new EastNorth(en[0], en[1]); … … 331 336 // There was also a 2/3 decrease in CPU samples for the method. 332 337 if (!(p1.isLatLonKnown() && p2.isLatLonKnown() && p3.isLatLonKnown() && p4.isLatLonKnown())) { 333 CheckParameterUtil.ensureThat(p1.isLatLonKnown(), () -> p1 + " invalid");334 CheckParameterUtil.ensureThat(p2.isLatLonKnown(), () -> p2 + " invalid");335 CheckParameterUtil.ensureThat(p3.isLatLonKnown(), () -> p3 + " invalid");336 CheckParameterUtil.ensureThat(p4.isLatLonKnown(), () -> p4 + " invalid");337 } 338 339 double x1 = p1.lon(); 340 double y1 = p1.lat(); 341 double x2 = p2.lon(); 342 double y2 = p2.lat(); 343 double x3 = p3.lon(); 344 double y3 = p3.lat(); 345 double x4 = p4.lon(); 346 double y4 = p4.lat(); 347 double[] en = getSegmentSegmentIntersection(x1, y1, x2, y2, x3, y3, x4, y4); 338 CheckParameterUtil.ensureThat(p1.isLatLonKnown(), () -> p1 + INVALID_COORDINATE_MSG); 339 CheckParameterUtil.ensureThat(p2.isLatLonKnown(), () -> p2 + INVALID_COORDINATE_MSG); 340 CheckParameterUtil.ensureThat(p3.isLatLonKnown(), () -> p3 + INVALID_COORDINATE_MSG); 341 CheckParameterUtil.ensureThat(p4.isLatLonKnown(), () -> p4 + INVALID_COORDINATE_MSG); 342 } 343 344 final double x1 = p1.lon(); 345 final double y1 = p1.lat(); 346 final double x2 = p2.lon(); 347 final double y2 = p2.lat(); 348 final double x3 = p3.lon(); 349 final double y3 = p3.lat(); 350 final double x4 = p4.lon(); 351 final double y4 = p4.lat(); 352 final double[] en = getSegmentSegmentIntersection(x1, y1, x2, y2, x3, y3, x4, y4); 348 353 if (en != null && en.length == 2) { 349 354 return new LatLon(en[1], en[0]); … … 369 374 //TODO: do this locally. 370 375 //TODO: remove this check after careful testing 371 if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null; 376 if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) { 377 return null; 378 } 372 379 373 380 // solve line-line intersection in parametric form: … … 376 383 // if 0<= u,v <=1, intersection exists at ( x1+ (x2-x1)*u, y1 + (y2-y1)*u ) 377 384 378 double a1 = x2 - x1; 379 double b1 = x3 - x4; 380 double c1 = x3 - x1; 381 382 double a2 = y2 - y1; 383 double b2 = y3 - y4; 384 double c2 = y3 - y1; 385 final double a1 = x2 - x1; 386 final double b1 = x3 - x4; 387 final double c1 = x3 - x1; 388 389 final double a2 = y2 - y1; 390 final double b2 = y3 - y4; 391 final double c2 = y3 - y1; 385 392 386 393 // Solve the equations 387 double det = a1*b2 - a2*b1; 388 389 double uu = b2*c1 - b1*c2; 390 double vv = a1*c2 - a2*c1; 391 double mag = Math.abs(uu)+Math.abs(vv); 394 final double det = a1*b2 - a2*b1; 395 396 final double uu = b2*c1 - b1*c2; 397 final double vv = a1*c2 - a2*c1; 398 final double mag = Math.abs(uu)+Math.abs(vv); 392 399 393 400 if (Math.abs(det) > 1e-12 * mag) { 394 401 double u = uu/det, v = vv/det; 395 402 if (u > -1e-8 && u < 1+1e-8 && v > -1e-8 && v < 1+1e-8) { 396 if (u < 0) u = 0; 397 if (u > 1) u = 1.0; 403 if (u < 0) { 404 u = 0; 405 } 406 if (u > 1) { 407 u = 1.0; 408 } 398 409 return new double[] {x1+a1*u, y1+a2*u}; 399 410 } else { … … 418 429 public static EastNorth getLineLineIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) { 419 430 420 CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");421 CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");422 CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");423 CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + " invalid");431 CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + INVALID_COORDINATE_MSG); 432 CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + INVALID_COORDINATE_MSG); 433 CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + INVALID_COORDINATE_MSG); 434 CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + INVALID_COORDINATE_MSG); 424 435 425 436 // Basically, the formula from wikipedia is used: … … 435 446 436 447 // Convert line from (point, point) form to ax+by=c 437 double a1 = p2.getY() - p1.getY(); 438 double b1 = p1.getX() - p2.getX(); 439 440 double a2 = p4.getY() - p3.getY(); 441 double b2 = p3.getX() - p4.getX(); 448 final double a1 = p2.getY() - p1.getY(); 449 final double b1 = p1.getX() - p2.getX(); 450 451 final double a2 = p4.getY() - p3.getY(); 452 final double b2 = p3.getX() - p4.getX(); 442 453 443 454 // Solve the equations 444 double det = a1 * b2 - a2 * b1; 445 if (det == 0) 455 final double det = a1 * b2 - a2 * b1; 456 if (det == 0) { 446 457 return null; // Lines are parallel 447 448 double c2 = (p4.getX() - p1.getX()) * (p3.getY() - p1.getY()) - (p3.getX() - p1.getX()) * (p4.getY() - p1.getY()); 458 } 459 460 final double c2 = (p4.getX() - p1.getX()) * (p3.getY() - p1.getY()) - (p3.getX() - p1.getX()) * (p4.getY() - p1.getY()); 449 461 450 462 return new EastNorth(b1 * c2 / det + p1.getX(), -a1 * c2 / det + p1.getY()); … … 461 473 public static boolean segmentsParallel(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) { 462 474 463 CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");464 CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");465 CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");466 CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + " invalid");475 CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + INVALID_COORDINATE_MSG); 476 CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + INVALID_COORDINATE_MSG); 477 CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + INVALID_COORDINATE_MSG); 478 CheckParameterUtil.ensureThat(p4.isValid(), () -> p4 + INVALID_COORDINATE_MSG); 467 479 468 480 // Convert line from (point, point) form to ax+by=c 469 double a1 = p2.getY() - p1.getY(); 470 double b1 = p1.getX() - p2.getX(); 471 472 double a2 = p4.getY() - p3.getY(); 473 double b2 = p3.getX() - p4.getX(); 481 final double a1 = p2.getY() - p1.getY(); 482 final double b1 = p1.getX() - p2.getX(); 483 484 final double a2 = p4.getY() - p3.getY(); 485 final double b2 = p3.getX() - p4.getX(); 474 486 475 487 // Solve the equations … … 485 497 CheckParameterUtil.ensureParameterNotNull(point, "point"); 486 498 487 double ldx = p2.getX() - p1.getX(); 488 double ldy = p2.getY() - p1.getY(); 499 final double ldx = p2.getX() - p1.getX(); 500 final double ldy = p2.getY() - p1.getY(); 489 501 490 502 //segment zero length 491 if (ldx == 0 && ldy == 0) 503 if (ldx == 0 && ldy == 0) { 492 504 return p1; 493 494 double pdx = point.getX() - p1.getX(); 495 double pdy = point.getY() - p1.getY(); 496 497 double offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy); 498 499 if (segmentOnly && offset <= 0) 505 } 506 507 final double pdx = point.getX() - p1.getX(); 508 final double pdy = point.getY() - p1.getY(); 509 510 final double offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy); 511 512 if (segmentOnly && offset <= 0) { 500 513 return p1; 501 else if (segmentOnly && offset >= 1) 514 } else if (segmentOnly && offset >= 1) { 502 515 return p2; 503 else516 } else { 504 517 return p1.interpolate(p2, offset); 518 } 505 519 } 506 520 … … 547 561 public static boolean angleIsClockwise(EastNorth commonNode, EastNorth firstNode, EastNorth secondNode) { 548 562 549 CheckParameterUtil.ensureThat(commonNode.isValid(), () -> commonNode + " invalid");550 CheckParameterUtil.ensureThat(firstNode.isValid(), () -> firstNode + " invalid");551 CheckParameterUtil.ensureThat(secondNode.isValid(), () -> secondNode + " invalid");552 553 double dy1 = firstNode.getY() - commonNode.getY(); 554 double dy2 = secondNode.getY() - commonNode.getY(); 555 double dx1 = firstNode.getX() - commonNode.getX(); 556 double dx2 = secondNode.getX() - commonNode.getX(); 563 CheckParameterUtil.ensureThat(commonNode.isValid(), () -> commonNode + INVALID_COORDINATE_MSG); 564 CheckParameterUtil.ensureThat(firstNode.isValid(), () -> firstNode + INVALID_COORDINATE_MSG); 565 CheckParameterUtil.ensureThat(secondNode.isValid(), () -> secondNode + INVALID_COORDINATE_MSG); 566 567 final double dy1 = firstNode.getY() - commonNode.getY(); 568 final double dy2 = secondNode.getY() - commonNode.getY(); 569 final double dx1 = firstNode.getX() - commonNode.getX(); 570 final double dx2 = secondNode.getX() - commonNode.getX(); 557 571 558 572 return dy1 * dx2 - dx1 * dy2 > 0; … … 566 580 */ 567 581 public static Area getArea(List<? extends INode> polygon) { 568 Path2D path = new Path2D.Double(Path2D.WIND_NON_ZERO, polygon.size()); 582 final Path2D path = new Path2D.Double(Path2D.WIND_NON_ZERO, polygon.size()); 569 583 570 584 boolean begin = true; 571 585 for (INode n : polygon) { 572 EastNorth en = n.getEastNorth(); 586 final EastNorth en = n.getEastNorth(); 573 587 if (en != null) { 574 588 if (begin) { … … 595 609 */ 596 610 public static Path2D buildPath2DLatLon(List<? extends ILatLon> polygon, Path2D path2d) { 597 Path2D path = path2d != null ? path2d : new Path2D.Double(); 611 final Path2D path = path2d != null ? path2d : new Path2D.Double(); 598 612 boolean begin = true; 599 613 for (ILatLon n : polygon) { … … 622 636 } 623 637 if (p instanceof Relation && p.isMultipolygon() && !p.isIncomplete()) { 624 Multipolygon mp = MultipolygonCache.getInstance().get((Relation) p); 638 final Multipolygon mp = MultipolygonCache.getInstance().get((Relation) p); 625 639 if (mp.getOpenEnds().isEmpty()) { 626 Path2D path = new Path2D.Double(); 640 final Path2D path = new Path2D.Double(); 627 641 path.setWindingRule(Path2D.WIND_EVEN_ODD); 628 642 for (PolyData pd : mp.getCombinedPolygons()) { … … 642 656 public static Area getAreaLatLon(Relation multipolygon) { 643 657 final Multipolygon mp = MultipolygonCache.getInstance().get(multipolygon); 644 Path2D path = new Path2D.Double(); 658 final Path2D path = new Path2D.Double(); 645 659 path.setWindingRule(Path2D.WIND_EVEN_ODD); 646 660 for (Multipolygon.PolyData pd : mp.getCombinedPolygons()) { … … 660 674 */ 661 675 public static PolygonIntersection polygonIntersection(List<? extends INode> first, List<? extends INode> second) { 662 Area a1 = getArea(first); 663 Area a2 = getArea(second); 676 final Area a1 = getArea(first); 677 final Area a2 = getArea(second); 664 678 return polygonIntersection(a1, a2, INTERSECTION_EPS_EAST_NORTH); 665 679 } … … 704 718 return new Pair<>(PolygonIntersection.OUTSIDE, new Area()); 705 719 } 706 Area inter = new Area(a1); 720 final Area inter = new Area(a1); 707 721 inter.intersect(a2); 708 722 … … 727 741 */ 728 742 private static boolean checkIntersection(Area inter, double eps) { 729 PathIterator pit = inter.getPathIterator(null); 730 double[] res = new double[6]; 743 final PathIterator pit = inter.getPathIterator(null); 744 final double[] res = new double[6]; 731 745 Rectangle2D r = new Rectangle2D.Double(); 732 746 while (!pit.isDone()) { 733 int type = pit.currentSegment(res); 747 final int type = pit.currentSegment(res); 734 748 switch (type) { 735 749 case PathIterator.SEG_MOVETO: … … 740 754 break; 741 755 case PathIterator.SEG_CLOSE: 742 if (r.getWidth() > eps || r.getHeight() > eps) 756 if (r.getWidth() > eps || r.getHeight() > eps) { 743 757 return true; 758 } 744 759 break; 745 760 default: … … 758 773 */ 759 774 public static boolean nodeInsidePolygon(INode point, List<? extends INode> polygonNodes) { 760 if (polygonNodes.size() < 2) 775 if (polygonNodes.size() < 2) { 761 776 return false; 777 } 762 778 763 779 //iterate each side of the polygon, start with the last segment … … 790 806 } 791 807 792 EastNorth pEN = point.getEastNorth(); 793 EastNorth opEN = oldPoint.getEastNorth(); 794 EastNorth npEN = newPoint.getEastNorth(); 795 EastNorth p1EN = p1.getEastNorth(); 796 EastNorth p2EN = p2.getEastNorth(); 808 final EastNorth pEN = point.getEastNorth(); 809 final EastNorth opEN = oldPoint.getEastNorth(); 810 final EastNorth npEN = newPoint.getEastNorth(); 811 final EastNorth p1EN = p1.getEastNorth(); 812 final EastNorth p2EN = p2.getEastNorth(); 797 813 798 814 if (pEN != null && opEN != null && npEN != null && p1EN != null && p2EN != null) { … … 878 894 */ 879 895 public static boolean isClockwise(List<? extends INode> nodes) { 880 int nodesCount = nodes.size(); 896 final int nodesCount = nodes.size(); 881 897 if (nodesCount < 3 || nodes.get(0) != nodes.get(nodesCount - 1)) { 882 898 throw new IllegalArgumentException("Way must be closed to check orientation."); … … 885 901 886 902 for (int node = 1; node <= /*sic! consider last-first as well*/ nodesCount; node++) { 887 INode coorPrev = nodes.get(node - 1); 888 INode coorCurr = nodes.get(node % nodesCount); 903 final INode coorPrev = nodes.get(node - 1); 904 final INode coorCurr = nodes.get(node % nodesCount); 889 905 area2 += coorPrev.lon() * coorCurr.lat(); 890 906 area2 -= coorCurr.lon() * coorPrev.lat(); … … 902 918 public static double getSegmentAngle(EastNorth p1, EastNorth p2) { 903 919 904 CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");905 CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + " invalid");920 CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + INVALID_COORDINATE_MSG); 921 CheckParameterUtil.ensureThat(p2.isValid(), () -> p2 + INVALID_COORDINATE_MSG); 906 922 907 923 return Math.atan2(p2.north() - p1.north(), p2.east() - p1.east()); … … 918 934 public static double getCornerAngle(EastNorth p1, EastNorth common, EastNorth p3) { 919 935 920 CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + " invalid");921 CheckParameterUtil.ensureThat(common.isValid(), () -> common + " invalid");922 CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + " invalid");936 CheckParameterUtil.ensureThat(p1.isValid(), () -> p1 + INVALID_COORDINATE_MSG); 937 CheckParameterUtil.ensureThat(common.isValid(), () -> common + INVALID_COORDINATE_MSG); 938 CheckParameterUtil.ensureThat(p3.isValid(), () -> p3 + INVALID_COORDINATE_MSG); 923 939 924 940 double result = getSegmentAngle(common, p1) - getSegmentAngle(common, p3); … … 978 994 // See https://en.wikipedia.org/wiki/Centroid#Of_a_polygon for the equation used here 979 995 for (int i = 0; i < size; i++) { 980 EastNorth n0 = nodes.get(i); 981 EastNorth n1 = nodes.get((i+1) % size); 996 final EastNorth n0 = nodes.get(i); 997 final EastNorth n1 = nodes.get((i+1) % size); 982 998 983 999 if (n0 != null && n1 != null && n0.isValid() && n1.isValid()) { 984 BigDecimal x0 = BigDecimal.valueOf(n0.east()); 985 BigDecimal y0 = BigDecimal.valueOf(n0.north()); 986 BigDecimal x1 = BigDecimal.valueOf(n1.east()); 987 BigDecimal y1 = BigDecimal.valueOf(n1.north()); 988 989 BigDecimal k = x0.multiply(y1, MathContext.DECIMAL128).subtract(y0.multiply(x1, MathContext.DECIMAL128)); 1000 final BigDecimal x0 = BigDecimal.valueOf(n0.east()); 1001 final BigDecimal y0 = BigDecimal.valueOf(n0.north()); 1002 final BigDecimal x1 = BigDecimal.valueOf(n1.east()); 1003 final BigDecimal y1 = BigDecimal.valueOf(n1.north()); 1004 1005 final BigDecimal k = x0.multiply(y1, MathContext.DECIMAL128).subtract(y0.multiply(x1, MathContext.DECIMAL128)); 990 1006 991 1007 area = area.add(k, MathContext.DECIMAL128); … … 995 1011 } 996 1012 997 BigDecimal d = new BigDecimal(3, MathContext.DECIMAL128); // 1/2 * 6 = 3 1013 final BigDecimal d = new BigDecimal(3, MathContext.DECIMAL128); // 1/2 * 6 = 3 998 1014 area = area.multiply(d, MathContext.DECIMAL128); 999 1015 if (area.compareTo(BigDecimal.ZERO) != 0) { … … 1024 1040 */ 1025 1041 public static EastNorth getCenter(List<? extends INode> nodes) { 1026 int nc = nodes.size(); 1027 if (nc < 3) return null; 1042 final int nc = nodes.size(); 1043 if (nc < 3) { 1044 return null; 1045 } 1028 1046 /* 1029 1047 * Equation of each bisector ax + by + c = 0 1030 1048 */ 1031 double[] a = new double[nc]; 1032 double[] b = new double[nc]; 1033 double[] c = new double[nc]; 1049 final double[] a = new double[nc]; 1050 final double[] b = new double[nc]; 1051 final double[] c = new double[nc]; 1034 1052 // Compute equation of bisector 1035 1053 for (int i = 0; i < nc; i++) { 1036 EastNorth pt1 = nodes.get(i).getEastNorth(); 1037 EastNorth pt2 = nodes.get((i+1) % nc).getEastNorth(); 1054 final EastNorth pt1 = nodes.get(i).getEastNorth(); 1055 final EastNorth pt2 = nodes.get((i+1) % nc).getEastNorth(); 1038 1056 a[i] = pt1.east() - pt2.east(); 1039 1057 b[i] = pt1.north() - pt2.north(); 1040 double d = Math.sqrt(a[i]*a[i] + b[i]*b[i]); 1041 if (d == 0) return null; 1058 final double d = Math.sqrt(a[i]*a[i] + b[i]*b[i]); 1059 if (d == 0) { 1060 return null; 1061 } 1042 1062 a[i] /= d; 1043 1063 b[i] /= d; 1044 double xC = (pt1.east() + pt2.east()) / 2; 1045 double yC = (pt1.north() + pt2.north()) / 2; 1064 final double xC = (pt1.east() + pt2.east()) / 2; 1065 final double yC = (pt1.north() + pt2.north()) / 2; 1046 1066 c[i] = -(a[i]*xC + b[i]*yC); 1047 1067 } … … 1058 1078 } 1059 1079 // (At.A)^-1 = [invij] 1060 double det = a11*a22 - a12*a12; 1061 if (Math.abs(det) < 1e-5) return null; 1062 double inv11 = a22/det; 1063 double inv12 = -a12/det; 1064 double inv22 = a11/det; 1080 final double det = a11*a22 - a12*a12; 1081 if (Math.abs(det) < 1e-5) { 1082 return null; 1083 } 1084 final double inv11 = a22/det; 1085 final double inv12 = -a12/det; 1086 final double inv22 = a11/det; 1065 1087 // center (xC, yC) = (At.A)^-1.At.y 1066 double xC = inv11*b1 + inv12*b2; 1067 double yC = inv12*b1 + inv22*b2; 1088 final double xC = inv11*b1 + inv12*b2; 1089 final double yC = inv12*b1 + inv22*b2; 1068 1090 return new EastNorth(xC, yC); 1069 1091 } … … 1117 1139 public static boolean isPolygonInsideMultiPolygon(List<? extends INode> nodes, Pair<List<JoinedPolygon>, 1118 1140 List<JoinedPolygon>> outerInner, Predicate<Way> isOuterWayAMatch) { 1119 Area a1 = nodes.size() == 1 ? null : getArea(nodes); 1141 final Area a1 = nodes.size() == 1 ? null : getArea(nodes); 1120 1142 // Test if object is inside an outer member 1121 1143 for (JoinedPolygon out : outerInner.a) { … … 1124 1146 : PolygonIntersection.FIRST_INSIDE_SECOND == polygonIntersection(a1, out.area)) { 1125 1147 // If inside an outer, check it is not inside an inner 1126 boolean insideInner = outerInner.b.stream().anyMatch(in -> a1 == null 1148 final boolean insideInner = outerInner.b.stream().anyMatch(in -> a1 == null 1127 1149 ? nodeInsidePolygon(nodes.get(0), in.nodes) 1128 1150 : in.area.getBounds2D().contains(a1.getBounds2D()) … … 1170 1192 */ 1171 1193 public static List<IPrimitive> filterInsidePolygon(Collection<IPrimitive> primitives, IWay<?> polygon) { 1172 List<IPrimitive> res = new ArrayList<>(); 1173 if (!polygon.isClosed() || polygon.getNodesCount() <= 3) 1194 final List<IPrimitive> res = new ArrayList<>(); 1195 if (!polygon.isClosed() || polygon.getNodesCount() <= 3) { 1174 1196 return res; 1197 } 1175 1198 /* polygon area in east north space, calculated only when really needed */ 1176 1199 Area polygonArea = null; … … 1194 1217 polygonArea = getArea(polygon.getNodes()); 1195 1218 } 1196 Multipolygon mp = p.getDataSet() != null ? MultipolygonCache.getInstance().get((Relation) p) : new Multipolygon((Relation) p); 1219 final Multipolygon mp = p.getDataSet() != null ? MultipolygonCache.getInstance().get((Relation) p) 1220 : new Multipolygon((Relation) p); 1197 1221 boolean inside = true; 1198 1222 // a (valid) multipolygon is inside the polygon if all outer rings are inside … … 1237 1261 public static List<IPrimitive> filterInsideMultipolygon(Collection<IPrimitive> primitives, Relation multiPolygon, 1238 1262 Map<IRelation<?>, Pair<List<JoinedPolygon>, List<JoinedPolygon>>> cache) { 1239 List<IPrimitive> res = new ArrayList<>(); 1240 if (primitives.isEmpty()) 1263 final List<IPrimitive> res = new ArrayList<>(); 1264 if (primitives.isEmpty()) { 1241 1265 return res; 1266 } 1242 1267 1243 1268 final Pair<List<JoinedPolygon>, List<JoinedPolygon>> outerInner; … … 1250 1275 } 1251 1276 1252 Set<OsmPrimitive> members = multiPolygon.getMemberPrimitives(); 1277 final Set<OsmPrimitive> members = multiPolygon.getMemberPrimitives(); 1253 1278 for (IPrimitive p : primitives) { 1254 if (members.contains(p)) 1279 if (members.contains(p)) { 1255 1280 continue; 1281 } 1256 1282 if (p instanceof Node) { 1257 1283 if (isPolygonInsideMultiPolygon(Collections.singletonList((Node) p), outerInner, null)) { … … 1263 1289 } 1264 1290 } else if (p.isMultipolygon()) { 1265 Multipolygon mp = new Multipolygon((Relation) p); 1291 final Multipolygon mp = new Multipolygon((Relation) p); 1266 1292 // a (valid) multipolygon is inside multiPolygon if all outer rings are inside 1267 boolean inside = mp.getOuterPolygons().stream() 1293 final boolean inside = mp.getOuterPolygons().stream() 1268 1294 .allMatch(outer -> outer.isClosed() && isPolygonInsideMultiPolygon(outer.getNodes(), outerInner, null)); 1269 1295 if (inside) { … … 1333 1359 double area = 0; 1334 1360 double perimeter = 0; 1335 Projection useProjection = projection == null ? ProjectionRegistry.getProjection() : projection; 1361 final Projection useProjection = projection == null ? ProjectionRegistry.getProjection() : projection; 1336 1362 1337 1363 if (!nodes.isEmpty()) { 1338 boolean closed = nodes.get(0) == nodes.get(nodes.size() - 1); 1339 int numSegments = closed ? nodes.size() - 1 : nodes.size(); 1364 final boolean closed = nodes.get(0) == nodes.get(nodes.size() - 1); 1365 final int numSegments = closed ? nodes.size() - 1 : nodes.size(); 1340 1366 EastNorth p1 = nodes.get(0).getEastNorth(useProjection); 1341 1367 for (int i = 1; i <= numSegments; i++) { … … 1372 1398 */ 1373 1399 public static <T extends OsmPrimitive> T getClosestPrimitive(OsmPrimitive osm, Collection<T> primitives) { 1374 Collection<T> collection = getClosestPrimitives(osm, primitives); 1400 final Collection<T> collection = getClosestPrimitives(osm, primitives); 1375 1401 return collection.iterator().next(); 1376 1402 } … … 1395 1421 public static <T extends OsmPrimitive> Collection<T> getClosestPrimitives(OsmPrimitive osm, Collection<T> primitives) { 1396 1422 double lowestDistance = Double.MAX_VALUE; 1397 TreeSet<T> closest = new TreeSet<>(); 1423 final TreeSet<T> closest = new TreeSet<>(); 1398 1424 for (T primitive : primitives) { 1399 double distance = getDistance(osm, primitive); 1400 if (Double.isNaN(distance)) continue; 1401 int comp = Double.compare(distance, lowestDistance); 1425 final double distance = getDistance(osm, primitive); 1426 if (Double.isNaN(distance)) { 1427 continue; 1428 } 1429 final int comp = Double.compare(distance, lowestDistance); 1402 1430 if (comp < 0) { 1403 1431 closest.clear(); … … 1459 1487 public static <T extends OsmPrimitive> Collection<T> getFurthestPrimitives(OsmPrimitive osm, Collection<T> primitives) { 1460 1488 double furthestDistance = Double.NEGATIVE_INFINITY; 1461 TreeSet<T> furthest = new TreeSet<>(); 1489 final TreeSet<T> furthest = new TreeSet<>(); 1462 1490 for (T primitive : primitives) { 1463 double distance = getDistance(osm, primitive); 1464 if (Double.isNaN(distance)) continue; 1465 int comp = Double.compare(distance, furthestDistance); 1491 final double distance = getDistance(osm, primitive); 1492 if (Double.isNaN(distance)) { 1493 continue; 1494 } 1495 final int comp = Double.compare(distance, furthestDistance); 1466 1496 if (comp > 0) { 1467 1497 furthest.clear(); … … 1490 1520 double rValue = Double.MAX_VALUE; 1491 1521 if (one == null || two == null || one.isIncomplete() 1492 || two.isIncomplete()) return Double.NaN; 1522 || two.isIncomplete()) { 1523 return Double.NaN; 1524 } 1493 1525 if (one instanceof ILatLon && two instanceof ILatLon) { 1494 1526 rValue = ((ILatLon) one).greatCircleDistance(((ILatLon) two)); … … 1501 1533 } else if (one instanceof Relation && !(two instanceof Relation)) { 1502 1534 for (OsmPrimitive osmPrimitive: ((Relation) one).getMemberPrimitives()) { 1503 double currentDistance = getDistance(osmPrimitive, two); 1504 if (currentDistance < rValue) rValue = currentDistance; 1535 final double currentDistance = getDistance(osmPrimitive, two); 1536 if (currentDistance < rValue) { 1537 rValue = currentDistance; 1538 } 1505 1539 } 1506 1540 } else if (!(one instanceof Relation) && two instanceof Relation) { … … 1509 1543 for (OsmPrimitive osmPrimitive1 : ((Relation) one).getMemberPrimitives()) { 1510 1544 for (OsmPrimitive osmPrimitive2 : ((Relation) two).getMemberPrimitives()) { 1511 double currentDistance = getDistance(osmPrimitive1, osmPrimitive2); 1512 if (currentDistance < rValue) rValue = currentDistance; 1545 final double currentDistance = getDistance(osmPrimitive1, osmPrimitive2); 1546 if (currentDistance < rValue) { 1547 rValue = currentDistance; 1548 } 1513 1549 } 1514 1550 } … … 1527 1563 */ 1528 1564 public static double getDistanceWayNode(Way way, Node node) { 1529 if (way == null || node == null || way.getNodesCount() < 2 || !node.isLatLonKnown()) 1565 if (way == null || node == null || way.getNodesCount() < 2 || !node.isLatLonKnown()) { 1530 1566 return Double.NaN; 1567 } 1531 1568 1532 1569 double smallest = Double.MAX_VALUE; 1533 EastNorth en0 = node.getEastNorth(); 1570 final EastNorth en0 = node.getEastNorth(); 1534 1571 // go through the nodes as if they were paired 1535 Iterator<Node> iter = way.getNodes().iterator(); 1572 final Iterator<Node> iter = way.getNodes().iterator(); 1536 1573 EastNorth en1 = iter.next().getEastNorth(); 1537 1574 while (iter.hasNext()) { 1538 EastNorth en2 = iter.next().getEastNorth(); 1539 double distance = getSegmentNodeDistSq(en1, en2, en0); 1540 if (distance < smallest) 1575 final EastNorth en2 = iter.next().getEastNorth(); 1576 final double distance = getSegmentNodeDistSq(en1, en2, en0); 1577 if (distance < smallest) { 1541 1578 smallest = distance; 1579 } 1542 1580 en1 = en2; 1543 1581 } … … 1558 1596 public static WaySegment getClosestWaySegment(Way way, OsmPrimitive primitive) { 1559 1597 if (way == null || primitive == null || way.isIncomplete() 1560 || primitive.isIncomplete()) return null; 1598 || primitive.isIncomplete()) { 1599 return null; 1600 } 1561 1601 double lowestDistance = Double.MAX_VALUE; 1562 1602 Pair<Node, Node> closestNodes = null; 1563 1603 for (Pair<Node, Node> nodes : way.getNodePairs(false)) { 1564 Way tWay = new Way(); 1604 final Way tWay = new Way(); 1565 1605 tWay.addNode(new Node(nodes.a)); 1566 1606 tWay.addNode(new Node(nodes.b)); 1567 double distance = getDistance(tWay, primitive); 1607 final double distance = getDistance(tWay, primitive); 1568 1608 if (distance < lowestDistance) { 1569 1609 lowestDistance = distance; … … 1571 1611 } 1572 1612 } 1573 if (closestNodes == null) return null; 1613 if (closestNodes == null) { 1614 return null; 1615 } 1574 1616 return lowestDistance != Double.MAX_VALUE ? WaySegment.forNodePair(way, closestNodes.a, closestNodes.b) : null; 1575 1617 } … … 1586 1628 */ 1587 1629 public static double getDistanceWayWay(Way w1, Way w2) { 1588 if (w1 == null || w2 == null || w1.getNodesCount() < 2 || w2.getNodesCount() < 2) 1630 if (w1 == null || w2 == null || w1.getNodesCount() < 2 || w2.getNodesCount() < 2) { 1589 1631 return Double.NaN; 1632 } 1590 1633 double rValue = Double.MAX_VALUE; 1591 Iterator<Node> iter1 = w1.getNodes().iterator(); 1592 List<Node> w2Nodes = w2.getNodes(); 1634 final Iterator<Node> iter1 = w1.getNodes().iterator(); 1635 final List<Node> w2Nodes = w2.getNodes(); 1593 1636 Node w1N1 = iter1.next(); 1594 1637 while (iter1.hasNext()) { 1595 Node w1N2 = iter1.next(); 1596 Iterator<Node> iter2 = w2Nodes.iterator(); 1638 final Node w1N2 = iter1.next(); 1639 final Iterator<Node> iter2 = w2Nodes.iterator(); 1597 1640 Node w2N1 = iter2.next(); 1598 1641 while (iter2.hasNext()) { 1599 Node w2N2 = iter2.next(); 1600 double distance = getDistanceSegmentSegment(w1N1, w1N2, w2N1, w2N2); 1601 if (distance < rValue) 1642 final Node w2N2 = iter2.next(); 1643 final double distance = getDistanceSegmentSegment(w1N1, w1N2, w2N1, w2N2); 1644 if (distance < rValue) { 1602 1645 rValue = distance; 1646 } 1603 1647 w2N1 = w2N2; 1604 1648 } … … 1636 1680 return Double.NaN; 1637 1681 } 1638 EastNorth enWs1Node1 = ws1Node1.getEastNorth(); 1639 EastNorth enWs1Node2 = ws1Node2.getEastNorth(); 1640 EastNorth enWs2Node1 = ws2Node1.getEastNorth(); 1641 EastNorth enWs2Node2 = ws2Node2.getEastNorth(); 1642 if (getSegmentSegmentIntersection(enWs1Node1, enWs1Node2, enWs2Node1, enWs2Node2) != null) 1682 final EastNorth enWs1Node1 = ws1Node1.getEastNorth(); 1683 final EastNorth enWs1Node2 = ws1Node2.getEastNorth(); 1684 final EastNorth enWs2Node1 = ws2Node1.getEastNorth(); 1685 final EastNorth enWs2Node2 = ws2Node2.getEastNorth(); 1686 if (getSegmentSegmentIntersection(enWs1Node1, enWs1Node2, enWs2Node1, enWs2Node2) != null) { 1643 1687 return 0; 1644 1645 double dist1sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node1); 1646 double dist2sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node2); 1647 double dist3sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node1); 1648 double dist4sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node2); 1649 double smallest = Math.min(Math.min(dist1sq, dist2sq), Math.min(dist3sq, dist4sq)); 1688 } 1689 1690 final double dist1sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node1); 1691 final double dist2sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node2); 1692 final double dist3sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node1); 1693 final double dist4sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node2); 1694 final double smallest = Math.min(Math.min(dist1sq, dist2sq), Math.min(dist3sq, dist4sq)); 1650 1695 return smallest != Double.MAX_VALUE ? Math.sqrt(smallest) : Double.NaN; 1651 1696 } … … 1682 1727 */ 1683 1728 private static double getSegmentNodeDistSq(EastNorth s1, EastNorth s2, EastNorth p) { 1684 EastNorth c1 = closestPointTo(s1, s2, p, true); 1729 final EastNorth c1 = closestPointTo(s1, s2, p, true); 1685 1730 return c1.distanceSq(p); 1686 1731 }
Note:
See TracChangeset
for help on using the changeset viewer.
