Ticket #9680: 9680_alpha.patch

File 9680_alpha.patch, 4.3 KB (added by Don-vip, 8 years ago)

alpha patch, reduces time from 1min 25s to 32s

  • src/org/openstreetmap/josm/data/osm/MultipolygonCreate.java

     
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.geom.Area;
    67import java.util.ArrayList;
    78import java.util.Collection;
    89import java.util.Collections;
     
    2526        public final List<Way> ways;
    2627        public final List<Boolean> reversed;
    2728        public final List<Node> nodes;
     29        public final Area area;
    2830
    2931        public JoinedPolygon(List<Way> ways, List<Boolean> reversed) {
    3032            this.ways = ways;
    3133            this.reversed = reversed;
    3234            this.nodes = this.getNodes();
     35            this.area = Geometry.getArea(nodes);
    3336        }
    3437
    3538        /**
     
    3740         * @param way the way to form the polygon
    3841         */
    3942        public JoinedPolygon(Way way) {
    40             this.ways = Collections.singletonList(way);
    41             this.reversed = Collections.singletonList(Boolean.FALSE);
    42             this.nodes = this.getNodes();
     43            this(Collections.singletonList(way), Collections.singletonList(Boolean.FALSE));
    4344        }
    4445
    45 
    4646        /**
    4747         * Builds a list of nodes for this polygon. First node is not duplicated as last node.
    4848         * @return list of nodes
     
    232232                    continue;
    233233                }
    234234
    235                 PolygonIntersection intersection = Geometry.polygonIntersection(outerWay.nodes, innerWay.nodes);
     235                //long start = System.currentTimeMillis();
     236                PolygonIntersection intersection = Geometry.polygonIntersection(outerWay.area, innerWay.area);
     237                //long time = System.currentTimeMillis() - start;
     238                //if (time > 2) {
     239                    //System.err.println(time + " ms for comparing "+outerWay+" and "+innerWay);
     240                //}
    236241
    237242                if (intersection == PolygonIntersection.FIRST_INSIDE_SECOND) {
    238243                    outerGood = false;  // outer is inside another polygon
  • src/org/openstreetmap/josm/tools/Geometry.java

     
    440440        return dy1 * dx2 - dx1 * dy2 > 0;
    441441    }
    442442
    443     private static Area getArea(List<Node> polygon) {
     443    /**
     444     * Returns the Area of a polygon, from its list of nodes.
     445     * @param polygon List of nodes forming polygon
     446     * @return Area for the given list of nodes
     447     */
     448    public static Area getArea(List<Node> polygon) {
    444449        Path2D path = new Path2D.Double();
    445450
    446451        boolean begin = true;
     
    461466
    462467    /**
    463468     * Tests if two polygons intersect.
    464      * @param first
    465      * @param second
     469     * @param first List of nodes forming first polygon
     470     * @param second List of nodes forming second polygon
    466471     * @return intersection kind
    467472     */
    468473    public static PolygonIntersection polygonIntersection(List<Node> first, List<Node> second) {
    469 
     474        //long start = System.currentTimeMillis();
    470475        Area a1 = getArea(first);
    471476        Area a2 = getArea(second);
     477        /*long time = System.currentTimeMillis() - start;
     478        if (time > 2) {
     479            System.err.println(time + " ms for computing areas");
     480        }*/
     481        return polygonIntersection(a1, a2);
     482    }
    472483
     484    /**
     485     * Tests if two polygons intersect.
     486     * @param a1 Area of first polygon
     487     * @param a2 Area of second polygon
     488     * @return intersection kind
     489     */
     490    public static PolygonIntersection polygonIntersection(Area a1, Area a2) {
     491
     492        //long start = System.currentTimeMillis();
    473493        Area inter = new Area(a1);
    474494        inter.intersect(a2);
    475495
    476496        Rectangle bounds = inter.getBounds();
     497        /*long time = System.currentTimeMillis() - start;
     498        if (time > 2) {
     499            System.err.println(time + " ms for computing intersection");
     500        }*/
    477501
    478502        if (inter.isEmpty() || bounds.getHeight()*bounds.getWidth() <= 1.0) {
    479503            return PolygonIntersection.OUTSIDE;