Changeset 6049 in josm for trunk


Ignore:
Timestamp:
2013-07-06T10:28:01+02:00 (6 years ago)
Author:
akks
Message:

see #8851: fix some bugs in joining areas and adding intersections ("random nodes added")
(changed segment-segment intersection to more stable procedure)

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java

    r5947 r6049  
    315315     * Checks whether the selected objects are suitable to join and joins them if so
    316316     */
     317    @Override
    317318    public void actionPerformed(ActionEvent e) {
    318319        LinkedList<Way> ways = new LinkedList<Way>(Main.main.getCurrentDataSet().getSelectedWays());
     
    12951296     */
    12961297    private RelationRole addOwnMultigonRelation(Collection<Way> inner, Way outer) {
    1297         if (inner.size() == 0) return null;
     1298        if (inner.isEmpty()) return null;
    12981299        // Create new multipolygon relation and add all inner ways to it
    12991300        Relation newRel = new Relation();
     
    13711372        }
    13721373
    1373         Relation newRel = null;
     1374        Relation newRel;
    13741375        switch (multiouters.size()) {
    13751376        case 0:
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r6007 r6049  
    5151        //stupid java, cannot instantiate array of generic classes..
    5252        @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];
    5657
    5758        Set<Node> intersectionNodes = new LinkedHashSet<Node>();
    5859
    5960        //copy node arrays for local usage.
    60         for (int pos = 0; pos < ways.size(); pos ++) {
     61        for (int pos = 0; pos < n; pos ++) {
    6162            newNodes[pos] = new ArrayList<Node>(ways.get(pos).getNodes());
    6263            wayBounds[pos] = getNodesBounds(newNodes[pos]);
     
    6667        //iterate over all way pairs and introduce the intersections
    6768        Comparator<Node> coordsComparator = new NodePositionComparator();
    68 
    69         WayLoop: for (int seg1Way = 0; seg1Way < ways.size(); seg1Way ++) {
    70             for (int seg2Way = seg1Way; seg2Way < ways.size(); seg2Way ++) {
     69        WayLoop: for (int seg1Way = 0; seg1Way < n; seg1Way ++) {
     70            for (int seg2Way = seg1Way; seg2Way < n; seg2Way ++) {
    7171
    7272                //do not waste time on bounds that do not intersect
     
    128128                                boolean insertInSeg1 = false;
    129129                                boolean insertInSeg2 = false;
    130 
    131130                                //find if the intersection point is at end point of one of the segments, if so use that point
    132131
     
    264263
    265264        //TODO: do this locally.
     265        //TODO: remove this check after careful testing
    266266        if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null;
    267267
    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;
    272 
    273         double a2 = y4 - y3;
    274         double b2 = x3 - x4;
    275         double c2 = x4*y3 - x3*y4;
     268        // solve line-line intersection in parametric form:
     269        // (x1,y1) + (x2-x1,y2-y1)* u  = (x3,y3) + (x4-x3,y4-y3)* v
     270        // (x2-x1,y2-y1)*u - (x4-x3,y4-y3)*v = (x3-x1,y3-y1)
     271        // if 0<= u,v <=1, intersection exists at ( x1+ (x2-x1)*u, y1 + (y2-y1)*u )
     272       
     273        double a1 = x2 - x1;
     274        double b1 = x3 - x4;
     275        double c1 = x3 - x1;
     276
     277        double a2 = y2 - y1;
     278        double b2 = y3 - y4;
     279        double c2 = y3 - y1;
    276280
    277281        // Solve the equations
    278282        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);
     283       
     284        double uu = b2*c1 - b1*c2 ;
     285        double vv = a1*c2 - a2*c1;
     286        double mag = Math.abs(uu)+Math.abs(vv);
     287               
     288        if (Math.abs(det) > 1e-12 * mag) {
     289            double u = uu/det, v = vv/det;
     290            if (u>-1e-8 && u < 1+1e-8 && v>-1e-8 && v < 1+1e-8 ) {
     291                if (u<0) u=0;
     292                if (u>1) u=1.0;
     293                return new EastNorth(x1+a1*u, y1+a2*u);
     294            } else {
     295                return null;
     296            }
     297        } else {
     298            // parallel lines
     299            return null;
     300        }
    285301    }
    286302
Note: See TracChangeset for help on using the changeset viewer.