Changeset 6894 in josm


Ignore:
Timestamp:
2014-03-02T14:51:43+01:00 (10 years ago)
Author:
bastiK
Message:

applied #9605 - Align Nodes in Line moves outer node instead of middle one (patch by Balaitous)

File:
1 edited

Legend:

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

    r6893 r6894  
    4343    }
    4444
    45     // the joy of single return values only...
     45    /**
     46     * Compute 2 anchor points to align a set of nodes.
     47     * If all nodes are part of a same way anchor points are choose farthest relative to this way,
     48     * else choose farthest nodes.
     49     * @param nodes Nodes to be aligned
     50     * @param resultOut Array of size >= 2
     51     */
    4652    private void nodePairFurthestApart(List<Node> nodes, Node[] resultOut) {
    4753        if(resultOut.length < 2)
    4854            throw new IllegalArgumentException();
    49         // Find from the selected nodes two that are the furthest apart.
    50         // Let's call them A and B.
    51         double distance = 0;
    52 
     55       
    5356        Node nodea = null;
    5457        Node nodeb = null;
    5558
    56         for (int i = 0; i < nodes.size()-1; i++) {
    57             Node n = nodes.get(i);
    58             for (int j = i+1; j < nodes.size(); j++) {
    59                 Node m = nodes.get(j);
    60                 double dist = Math.sqrt(n.getEastNorth().distance(m.getEastNorth()));
    61                 if (dist > distance) {
    62                     nodea = n;
    63                     nodeb = m;
    64                     distance = dist;
     59        // Intersection of all ways referred by each node
     60        HashSet<Way> waysRef = null;
     61        for(Node n: nodes) {
     62            Collection<Way> ref = OsmPrimitive.getFilteredList(n.getReferrers(), Way.class);
     63            if(waysRef == null)
     64                waysRef = new HashSet<Way>(ref);
     65            else
     66                waysRef.retainAll(ref);
     67        }
     68        if(waysRef.size() == 1) {
     69            // All nodes are part of the same way. See #9605
     70            HashSet<Node> remainNodes = new HashSet<Node>(nodes);
     71            Way way = waysRef.iterator().next();
     72            for(Node n: way.getNodes()) {
     73                if(!remainNodes.contains(n)) continue;
     74                if(nodea == null) nodea = n;
     75                if(remainNodes.size() == 1) {
     76                    nodeb = remainNodes.iterator().next();
     77                    break;
     78                }
     79                remainNodes.remove(n);
     80            }
     81        } else {
     82            // Find from the selected nodes two that are the furthest apart.
     83            // Let's call them A and B.
     84            double distance = 0;
     85            for (int i = 0; i < nodes.size()-1; i++) {
     86                Node n = nodes.get(i);
     87                for (int j = i+1; j < nodes.size(); j++) {
     88                    Node m = nodes.get(j);
     89                    double dist = Math.sqrt(n.getEastNorth().distance(m.getEastNorth()));
     90                    if (dist > distance) {
     91                        nodea = n;
     92                        nodeb = m;
     93                        distance = dist;
     94                    }
    6595                }
    6696            }
Note: See TracChangeset for help on using the changeset viewer.