| 129 | | } else if (nodes.size() >= 2) { |
| 130 | | center = nodes.get(way.containsNode(nodes.get(0)) ? 1 : 0).getEastNorth(); |
| 131 | | if (nodes.size() == 2) { |
| | 140 | } |
| | 141 | if (nodes.size() >= 1) { |
| | 142 | boolean[] isContained = new boolean[nodes.size()]; |
| | 143 | for(int i = 0; i < nodes.size(); i++) { |
| | 144 | Node n = nodes.get(i); |
| | 145 | isContained[i] = false; |
| | 146 | for(Way way: ways) |
| | 147 | if(way.containsNode(n)) { |
| | 148 | isContained[i] = true; |
| | 149 | break; |
| | 150 | } |
| | 151 | } |
| | 152 | if(nodes.size() == 1) { |
| | 153 | if(!isContained[0]) |
| | 154 | center = nodes.get(0).getEastNorth(); |
| | 155 | } else { |
| | 156 | if(!isContained[0] && !isContained[1]) { |
| | 157 | // 2 nodes outside of way, can't choose one as center |
| | 158 | new Notification( |
| | 159 | tr("Please select only one node as center.")) |
| | 160 | .setIcon(JOptionPane.INFORMATION_MESSAGE) |
| | 161 | .setDuration(Notification.TIME_SHORT) |
| | 162 | .show(); |
| | 163 | return; |
| | 164 | } else if (!isContained[0] || !isContained[1]) { |
| | 165 | // 1 node inside and 1 outside, outside is center, inside node define radius |
| | 166 | center = nodes.get(isContained[0] ? 1 : 0).getEastNorth(); |
| 226 | | } else { // Move each node to that distance from the centre. |
| 227 | | for (Node n : nodes) { |
| 228 | | pc = new PolarCoor(n.getEastNorth(), center, 0); |
| 229 | | pc.radius = radius; |
| 230 | | EastNorth no = pc.toEastNorth(); |
| 231 | | cmds.add(new MoveCommand(n, no.east() - n.getEastNorth().east(), no.north() - n.getEastNorth().north())); |
| | 268 | } else { // Move each node to that distance from the center. |
| | 269 | int nodeCount = nodes.size(); |
| | 270 | // Search first fixed node |
| | 271 | int startPosition = 0; |
| | 272 | for(startPosition = 0; startPosition < nodeCount; startPosition++) |
| | 273 | if(isFixNode(nodes.get(startPosition % nodeCount), sel)) break; |
| | 274 | int i = startPosition; // Start position for current arc |
| | 275 | int j; // End position for current arc |
| | 276 | while(i < startPosition + nodeCount) { |
| | 277 | for(j = i + 1; j < startPosition + nodeCount; j++) |
| | 278 | if(isFixNode(nodes.get(j % nodeCount), sel)) break; |
| | 279 | Node first = nodes.get(i % nodeCount); |
| | 280 | PolarCoor pcFirst = new PolarCoor(first.getEastNorth(), center, 0); |
| | 281 | pcFirst.radius = radius; |
| | 282 | cmds.add(pcFirst.createMoveCommand(first)); |
| | 283 | if(j > i + 1) { |
| | 284 | double delta; |
| | 285 | if(j == i + nodeCount) { |
| | 286 | delta = 2 * Math.PI / nodeCount; |
| | 287 | } else { |
| | 288 | PolarCoor pcLast = new PolarCoor(nodes.get(j % nodeCount).getEastNorth(), center, 0); |
| | 289 | delta = pcLast.angle - pcFirst.angle; |
| | 290 | if(delta < 0) // Assume each PolarCoor.angle is in range ]-pi; pi] |
| | 291 | delta += 2*Math.PI; |
| | 292 | delta /= j - i; |
| | 293 | } |
| | 294 | for(int k = i+1; k < j; k++) { |
| | 295 | PolarCoor p = new PolarCoor(radius, pcFirst.angle + (k-i)*delta, center, 0); |
| | 296 | cmds.add(p.createMoveCommand(nodes.get(k % nodeCount))); |
| | 297 | } |
| | 298 | } |
| | 299 | i = j; // Update start point for next iteration |
| | 345 | |
| | 346 | /** |
| | 347 | * Determines if ways can be joined into a polygon. |
| | 348 | * @param ways The ways collection to check |
| | 349 | * @return true if all ways can be joined into a polygon |
| | 350 | */ |
| | 351 | protected static boolean checkWaysArePolygon(Collection<Way> ways) { |
| | 352 | // For each way, nodes strictly between first and last should't be reference by an other way |
| | 353 | for(Way way: ways) { |
| | 354 | for(Node node: way.getNodes()) { |
| | 355 | if(node == way.firstNode() || node == way.lastNode()) continue; |
| | 356 | for(Way wayOther: ways) { |
| | 357 | if(way == wayOther) continue; |
| | 358 | if(node.getReferrers().contains(wayOther)) return false; |
| | 359 | } |
| | 360 | } |
| | 361 | } |
| | 362 | // Test if ways can be joined |
| | 363 | Way currentWay = null; |
| | 364 | Node startNode = null, endNode = null; |
| | 365 | int used = 0; |
| | 366 | while(true) { |
| | 367 | Way nextWay = null; |
| | 368 | for(Way w: ways) { |
| | 369 | if(w.firstNode() == w.lastNode()) return ways.size() == 1; |
| | 370 | if(w == currentWay) continue; |
| | 371 | if(currentWay == null) { |
| | 372 | nextWay = w; |
| | 373 | startNode = w.firstNode(); |
| | 374 | endNode = w.lastNode(); |
| | 375 | break; |
| | 376 | } |
| | 377 | if(w.firstNode() == endNode) { |
| | 378 | nextWay = w; |
| | 379 | endNode = w.lastNode(); |
| | 380 | break; |
| | 381 | } |
| | 382 | if(w.lastNode() == endNode) { |
| | 383 | nextWay = w; |
| | 384 | endNode = w.firstNode(); |
| | 385 | break; |
| | 386 | } |
| | 387 | } |
| | 388 | if(nextWay == null) return false; |
| | 389 | used += 1; |
| | 390 | currentWay = nextWay; |
| | 391 | if(endNode == startNode) return used == ways.size(); |
| | 392 | } |
| | 393 | } |