129 | | } else if (nodes.size() >= 2) { |
130 | | center = nodes.get(way.containsNode(nodes.get(0)) ? 1 : 0).getEastNorth(); |
131 | | if (nodes.size() == 2) { |
| 144 | } |
| 145 | if (nodes.size() >= 1) { |
| 146 | boolean[] isContained = new boolean[nodes.size()]; |
| 147 | for(int i = 0; i < nodes.size(); i++) { |
| 148 | Node n = nodes.get(i); |
| 149 | isContained[i] = false; |
| 150 | for(Way way: ways) |
| 151 | if(way.containsNode(n)) { |
| 152 | isContained[i] = true; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | if(nodes.size() == 1) { |
| 157 | if(!isContained[0]) |
| 158 | center = nodes.get(0).getEastNorth(); |
| 159 | } else { |
| 160 | if(!isContained[0] && !isContained[1]) { |
| 161 | // 2 nodes outside of way, can't choose one as center |
| 162 | new Notification( |
| 163 | tr("Please select only one node as center.")) |
| 164 | .setIcon(JOptionPane.INFORMATION_MESSAGE) |
| 165 | .setDuration(Notification.TIME_SHORT) |
| 166 | .show(); |
| 167 | return; |
| 168 | } else if (!isContained[0] || !isContained[1]) { |
| 169 | // 1 node inside and 1 outside, outside is center, inside node define radius |
| 170 | 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())); |
| 272 | } else { // Move each node to that distance from the center. |
| 273 | int nodeCount = nodes.size(); |
| 274 | // Search first fixed node |
| 275 | int startPosition = 0; |
| 276 | for(startPosition = 0; startPosition < nodeCount; startPosition++) |
| 277 | if(isFixNode(nodes.get(startPosition % nodeCount), sel)) break; |
| 278 | int i = startPosition; // Start position for current arc |
| 279 | int j; // End position for current arc |
| 280 | while(i < startPosition + nodeCount) { |
| 281 | for(j = i + 1; j < startPosition + nodeCount; j++) |
| 282 | if(isFixNode(nodes.get(j % nodeCount), sel)) break; |
| 283 | Node first = nodes.get(i % nodeCount); |
| 284 | PolarCoor pcFirst = new PolarCoor(first.getEastNorth(), center, 0); |
| 285 | pcFirst.radius = radius; |
| 286 | cmds.add(pcFirst.createMoveCommand(first)); |
| 287 | if(j > i + 1) { |
| 288 | double delta; |
| 289 | if(j == i + nodeCount) { |
| 290 | delta = 2 * Math.PI / nodeCount; |
| 291 | } else { |
| 292 | PolarCoor pcLast = new PolarCoor(nodes.get(j % nodeCount).getEastNorth(), center, 0); |
| 293 | delta = pcLast.angle - pcFirst.angle; |
| 294 | if(delta < 0) // Assume each PolarCoor.angle is in range ]-pi; pi] |
| 295 | delta += 2*Math.PI; |
| 296 | delta /= j - i; |
| 297 | } |
| 298 | for(int k = i+1; k < j; k++) { |
| 299 | PolarCoor p = new PolarCoor(radius, pcFirst.angle + (k-i)*delta, center, 0); |
| 300 | cmds.add(p.createMoveCommand(nodes.get(k % nodeCount))); |
| 301 | } |
| 302 | } |
| 303 | i = j; // Update start point for next iteration |
| 313 | /** |
| 314 | * Check if one or more nodes are outside of download area |
| 315 | * @param nodes Nodes to check |
| 316 | * @return true if action can be done |
| 317 | */ |
| 318 | private boolean actionAllowed(Collection<Node> nodes) { |
| 319 | boolean outside = false; |
| 320 | for(Node n: nodes) |
| 321 | if(n.isOutsideDownloadArea()) { |
| 322 | outside = true; |
| 323 | break; |
| 324 | } |
| 325 | if(outside) { |
| 326 | JPanel msg = new JPanel(new GridBagLayout()); |
| 327 | msg.add(new JLabel("<html>" + |
| 328 | tr("One or more nodes involved in this action are outside of the downloaded area.") + |
| 329 | "<br/>" + |
| 330 | tr("Are you really sure to continue?") + |
| 331 | "</html>")); |
| 332 | return ConditionalOptionPaneUtil.showConfirmationDialog( |
| 333 | "alignincircle_outside_nodes", |
| 334 | Main.parent, |
| 335 | msg, |
| 336 | tr("The selected area is incomplete. Continue?"), |
| 337 | JOptionPane.YES_NO_OPTION, |
| 338 | JOptionPane.QUESTION_MESSAGE, |
| 339 | JOptionPane.YES_OPTION); |
| 340 | } |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Test if angle of a node can be change. |
| 346 | * @param n Node |
| 347 | * @param sel Selection which action is apply |
| 348 | * @return true is this node does't have a fix angle |
| 349 | */ |
| 350 | private boolean isFixNode(Node n, Collection<OsmPrimitive> sel) { |
| 351 | List<OsmPrimitive> referrers = n.getReferrers(); |
| 352 | if(referrers.isEmpty()) return false; |
| 353 | if(sel.contains(n) || referrers.size() > 1 || !sel.contains(referrers.get(0))) return true; |
| 354 | return false; |
| 355 | } |
| 356 | |
| 382 | |
| 383 | /** |
| 384 | * Determines if ways can be joined into a polygon. |
| 385 | * @param ways The ways collection to check |
| 386 | * @return true if all ways can be joined into a polygon |
| 387 | */ |
| 388 | protected static boolean checkWaysArePolygon(Collection<Way> ways) { |
| 389 | // For each way, nodes strictly between first and last should't be reference by an other way |
| 390 | for(Way way: ways) { |
| 391 | for(Node node: way.getNodes()) { |
| 392 | if(node == way.firstNode() || node == way.lastNode()) continue; |
| 393 | for(Way wayOther: ways) { |
| 394 | if(way == wayOther) continue; |
| 395 | if(node.getReferrers().contains(wayOther)) return false; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | // Test if ways can be joined |
| 400 | Way currentWay = null; |
| 401 | Node startNode = null, endNode = null; |
| 402 | int used = 0; |
| 403 | while(true) { |
| 404 | Way nextWay = null; |
| 405 | for(Way w: ways) { |
| 406 | if(w.firstNode() == w.lastNode()) return ways.size() == 1; |
| 407 | if(w == currentWay) continue; |
| 408 | if(currentWay == null) { |
| 409 | nextWay = w; |
| 410 | startNode = w.firstNode(); |
| 411 | endNode = w.lastNode(); |
| 412 | break; |
| 413 | } |
| 414 | if(w.firstNode() == endNode) { |
| 415 | nextWay = w; |
| 416 | endNode = w.lastNode(); |
| 417 | break; |
| 418 | } |
| 419 | if(w.lastNode() == endNode) { |
| 420 | nextWay = w; |
| 421 | endNode = w.firstNode(); |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | if(nextWay == null) return false; |
| 426 | used += 1; |
| 427 | currentWay = nextWay; |
| 428 | if(endNode == startNode) return used == ways.size(); |
| 429 | } |
| 430 | } |