[6544] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import java.util.Collection;
|
---|
| 5 | import java.util.LinkedList;
|
---|
[6623] | 6 | import java.util.Set;
|
---|
[6544] | 7 | import java.util.TreeSet;
|
---|
| 8 |
|
---|
| 9 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Auxiliary class for the {@link SelectNonBranchingWaySequencesAction}.
|
---|
| 16 | *
|
---|
| 17 | * @author Marko Mäkelä
|
---|
| 18 | */
|
---|
| 19 | public class SelectNonBranchingWaySequences {
|
---|
| 20 | /**
|
---|
| 21 | * outer endpoints of selected ways
|
---|
| 22 | */
|
---|
[8285] | 23 | private Set<Node> outerNodes;
|
---|
[6544] | 24 | /**
|
---|
| 25 | * endpoints of selected ways
|
---|
| 26 | */
|
---|
[8285] | 27 | private Set<Node> nodes;
|
---|
[6544] | 28 |
|
---|
| 29 | /**
|
---|
| 30 | * Creates a way selection
|
---|
| 31 | *
|
---|
| 32 | * @param ways selection a selection of ways
|
---|
| 33 | */
|
---|
| 34 | public SelectNonBranchingWaySequences(final Collection<Way> ways) {
|
---|
| 35 | if (ways.isEmpty()) {
|
---|
| 36 | // The selection cannot be extended.
|
---|
| 37 | outerNodes = null;
|
---|
| 38 | nodes = null;
|
---|
| 39 | } else {
|
---|
[7005] | 40 | nodes = new TreeSet<>();
|
---|
| 41 | outerNodes = new TreeSet<>();
|
---|
[6544] | 42 |
|
---|
[8513] | 43 | for (Way way : ways) {
|
---|
[6544] | 44 | addNodes(way);
|
---|
[8513] | 45 | }
|
---|
[6544] | 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Add a way endpoint to nodes, outerNodes
|
---|
| 51 | *
|
---|
| 52 | * @param node a way endpoint
|
---|
| 53 | */
|
---|
| 54 | private void addNodes(Node node) {
|
---|
[6603] | 55 | if (node == null) return;
|
---|
[6544] | 56 | else if (!nodes.add(node))
|
---|
| 57 | outerNodes.remove(node);
|
---|
| 58 | else
|
---|
| 59 | outerNodes.add(node);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * Add the endpoints of the way to nodes, outerNodes
|
---|
| 64 | *
|
---|
| 65 | * @param way a way whose endpoints are added
|
---|
| 66 | */
|
---|
| 67 | private void addNodes(Way way) {
|
---|
| 68 | addNodes(way.firstNode());
|
---|
| 69 | addNodes(way.lastNode());
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Find out if the selection can be extended
|
---|
| 74 | *
|
---|
| 75 | * @return true if the selection can be extended
|
---|
| 76 | */
|
---|
| 77 | public boolean canExtend() {
|
---|
| 78 | return outerNodes != null && !outerNodes.isEmpty();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Finds out if the current selection can be extended.
|
---|
| 83 | *
|
---|
| 84 | * @param selection current selection (ways and others)
|
---|
| 85 | * @param node perimeter node from which to extend the selection
|
---|
| 86 | * @return a way by which to extend the selection, or null
|
---|
| 87 | */
|
---|
| 88 | private static Way findWay(Collection<OsmPrimitive> selection, Node node) {
|
---|
| 89 | Way foundWay = null;
|
---|
| 90 |
|
---|
| 91 | for (Way way : OsmPrimitive.getFilteredList(node.getReferrers(),
|
---|
| 92 | Way.class)) {
|
---|
| 93 | if (way.getNodesCount() < 2 || !way.isFirstLastNode(node)
|
---|
[8780] | 94 | || !way.isSelectable()
|
---|
[6544] | 95 | || selection.contains(way))
|
---|
| 96 | continue;
|
---|
| 97 |
|
---|
| 98 | /* A previously unselected way was found that is connected
|
---|
| 99 | to the node. */
|
---|
| 100 | if (foundWay != null)
|
---|
| 101 | /* This is not the only qualifying way. There is a
|
---|
| 102 | branch at the node, and we cannot extend the selection. */
|
---|
| 103 | return null;
|
---|
| 104 |
|
---|
| 105 | /* Remember the first found qualifying way. */
|
---|
| 106 | foundWay = way;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /* Return the only way found, or null if none was found. */
|
---|
| 110 | return foundWay;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
| 114 | * Finds out if the current selection can be extended.
|
---|
[6830] | 115 | * <p>
|
---|
[6544] | 116 | * The members outerNodes, nodes must have been initialized.
|
---|
| 117 | * How to update these members when extending the selection, @see extend().
|
---|
[6830] | 118 | * </p>
|
---|
[6544] | 119 | * @param selection current selection
|
---|
| 120 | * @return a way by which to extend the selection, or null
|
---|
| 121 | */
|
---|
| 122 | private Way findWay(Collection<OsmPrimitive> selection) {
|
---|
| 123 | for (Node node : outerNodes) {
|
---|
| 124 | Way way = findWay(selection, node);
|
---|
| 125 | if (way != null)
|
---|
| 126 | return way;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | return null;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * Extend the current selection
|
---|
| 134 | *
|
---|
| 135 | * @param data the data set in which to extend the selection
|
---|
| 136 | */
|
---|
| 137 | public void extend(DataSet data) {
|
---|
| 138 | if (!canExtend())
|
---|
| 139 | return;
|
---|
| 140 |
|
---|
[9062] | 141 | Collection<OsmPrimitive> currentSelection = data.getSelected();
|
---|
[6544] | 142 |
|
---|
[9062] | 143 | Way way = findWay(currentSelection);
|
---|
[6544] | 144 |
|
---|
| 145 | if (way == null)
|
---|
| 146 | return;
|
---|
| 147 |
|
---|
[9062] | 148 | boolean selectionChanged = false;
|
---|
[9999] | 149 | Collection<OsmPrimitive> selection = new LinkedList<>(currentSelection);
|
---|
[6544] | 150 |
|
---|
| 151 | do {
|
---|
| 152 | if (!selection.add(way))
|
---|
| 153 | break;
|
---|
| 154 |
|
---|
| 155 | selectionChanged = true;
|
---|
| 156 | addNodes(way);
|
---|
| 157 |
|
---|
| 158 | way = findWay(selection);
|
---|
| 159 | } while (way != null);
|
---|
| 160 |
|
---|
| 161 | if (selectionChanged)
|
---|
| 162 | data.setSelected(selection, true);
|
---|
| 163 | }
|
---|
| 164 | }
|
---|