Changeset 8303 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/CreateCircleAction.java
r8285 r8303 10 10 import java.util.Arrays; 11 11 import java.util.Collection; 12 import java.util.Collections; 12 13 import java.util.Comparator; 13 14 import java.util.LinkedList; … … 28 29 import org.openstreetmap.josm.gui.Notification; 29 30 import org.openstreetmap.josm.tools.Geometry; 31 import org.openstreetmap.josm.tools.RightAndLefthandTraffic; 30 32 import org.openstreetmap.josm.tools.Shortcut; 31 33 … … 115 117 @Override 116 118 public int compare(PolarNode pc1, PolarNode pc2) { 117 if(pc1.a < pc2.a) 118 return -1; 119 else if(pc1.a == pc2.a) 120 return 0; 121 else 122 return 1; 119 return Double.compare(pc1.a, pc2.a); 123 120 } 124 121 } … … 137 134 138 135 Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected(); 139 List<Node> nodes = new LinkedList<>(); 136 List<Node> nodes = OsmPrimitive.getFilteredList(sel, Node.class); 137 List<Way> ways = OsmPrimitive.getFilteredList(sel, Way.class); 138 140 139 Way existingWay = null; 141 142 for (OsmPrimitive osm : sel)143 if (osm instanceof Node) {144 nodes.add((Node)osm);145 }146 140 147 141 // special case if no single nodes are selected and exactly one way is: 148 142 // then use the way's nodes 149 if (nodes.isEmpty() && (sel.size() == 1)) { 150 for (OsmPrimitive osm : sel) 151 if (osm instanceof Way) { 152 existingWay = ((Way)osm); 153 for (Node n : ((Way)osm).getNodes()) 154 { 155 if(!nodes.contains(n)) { 156 nodes.add(n); 157 } 158 } 143 if (nodes.isEmpty() && (ways.size() == 1)) { 144 existingWay = ways.get(0); 145 for (Node n : existingWay.getNodes()) { 146 if(!nodes.contains(n)) { 147 nodes.add(n); 159 148 } 149 } 160 150 } 161 151 … … 210 200 211 201 // build a way for the circle 212 List<Node> wayToAdd = new ArrayList<>();202 List<Node> nodesToAdd = new ArrayList<>(); 213 203 for(int i = 0; i < nodes.size(); i++) { 214 wayToAdd.add(angles[i].node);204 nodesToAdd.add(angles[i].node); 215 205 double delta = angles[(i+1) % nodes.size()].a - angles[i].a; 216 206 if(delta < 0) … … 226 216 } 227 217 Node n = new Node(ll); 228 wayToAdd.add(n);218 nodesToAdd.add(n); 229 219 cmds.add(new AddCommand(n)); 230 220 } 231 221 } 232 wayToAdd.add(wayToAdd.get(0)); // close the circle 222 nodesToAdd.add(nodesToAdd.get(0)); // close the circle 223 if (existingWay != null && existingWay.getNodesCount() >= 3) { 224 nodesToAdd = orderNodesByWay(nodesToAdd, existingWay); 225 } else { 226 nodesToAdd = orderNodesByTrafficHand(nodesToAdd); 227 } 233 228 if (existingWay == null) { 234 229 Way newWay = new Way(); 235 newWay.setNodes( wayToAdd);230 newWay.setNodes(nodesToAdd); 236 231 cmds.add(new AddCommand(newWay)); 237 232 } else { 238 233 Way newWay = new Way(existingWay); 239 newWay.setNodes( wayToAdd);234 newWay.setNodes(nodesToAdd); 240 235 cmds.add(new ChangeCommand(existingWay, newWay)); 241 236 } … … 243 238 Main.main.undoRedo.add(new SequenceCommand(tr("Create Circle"), cmds)); 244 239 Main.map.repaint(); 240 } 241 242 /** 243 * Order nodes according to left/right hand traffic. 244 * @param nodes Nodes list to be ordered. 245 * @return Modified nodes list ordered according hand traffic. 246 */ 247 private List<Node> orderNodesByTrafficHand(List<Node> nodes) { 248 boolean rightHandTraffic = true; 249 for (Node n: nodes) { 250 if (!RightAndLefthandTraffic.isRightHandTraffic(n.getCoor())) { 251 rightHandTraffic = false; 252 break; 253 } 254 } 255 if (rightHandTraffic == Geometry.isClockwise(nodes)) { 256 Collections.reverse(nodes); 257 } 258 return nodes; 259 } 260 261 /** 262 * Order nodes according to way direction. 263 * @param nodes Nodes list to be ordered. 264 * @param way Way used to determine direction. 265 * @return Modified nodes list with same direction as way. 266 */ 267 private List<Node> orderNodesByWay(List<Node> nodes, Way way) { 268 List<Node> wayNodes = way.getNodes(); 269 if (!way.isClosed()) { 270 wayNodes.add(wayNodes.get(0)); 271 } 272 if (Geometry.isClockwise(wayNodes) != Geometry.isClockwise(nodes)) { 273 Collections.reverse(nodes); 274 } 275 return nodes; 245 276 } 246 277 -
trunk/src/org/openstreetmap/josm/tools/Geometry.java
r8127 r8303 688 688 */ 689 689 public static boolean isClockwise(Way w) { 690 if (!w.isClosed()) { 690 return isClockwise(w.getNodes()); 691 } 692 693 /** 694 * Determines whether path from nodes list is oriented clockwise. 695 * @see #isClockwise(Way) 696 * @param nodes Nodes list to be checked. 697 * @return true if and only if way is oriented clockwise. 698 * @throws IllegalArgumentException if way is not closed (see {@link Way#isClosed}). 699 */ 700 public static boolean isClockwise(List<Node> nodes) { 701 double area2 = 0.; 702 int nodesCount = nodes.size(); 703 if (nodesCount < 3 || nodes.get(0) != nodes.get(nodesCount - 1)) { 691 704 throw new IllegalArgumentException("Way must be closed to check orientation."); 692 705 } 693 706 694 double area2 = 0.;695 int nodesCount = w.getNodesCount();696 697 707 for (int node = 1; node <= /*sic! consider last-first as well*/ nodesCount; node++) { 698 LatLon coorPrev = w.getNode(node - 1).getCoor();699 LatLon coorCurr = w.getNode(node % nodesCount).getCoor();708 LatLon coorPrev = nodes.get(node - 1).getCoor(); 709 LatLon coorCurr = nodes.get(node % nodesCount).getCoor(); 700 710 area2 += coorPrev.lon() * coorCurr.lat(); 701 711 area2 -= coorCurr.lon() * coorPrev.lat();
Note:
See TracChangeset
for help on using the changeset viewer.