[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 | */
|
---|
[6623] | 23 | Set<Node> outerNodes;
|
---|
[6544] | 24 | /**
|
---|
| 25 | * endpoints of selected ways
|
---|
| 26 | */
|
---|
[6623] | 27 | 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 |
|
---|
| 43 | for (Way way : ways)
|
---|
| 44 | addNodes(way);
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Add a way endpoint to nodes, outerNodes
|
---|
| 50 | *
|
---|
| 51 | * @param node a way endpoint
|
---|
| 52 | */
|
---|
| 53 | private void addNodes(Node node) {
|
---|
[6603] | 54 | if (node == null) return;
|
---|
[6544] | 55 | else if (!nodes.add(node))
|
---|
| 56 | outerNodes.remove(node);
|
---|
| 57 | else
|
---|
| 58 | outerNodes.add(node);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Add the endpoints of the way to nodes, outerNodes
|
---|
| 63 | *
|
---|
| 64 | * @param way a way whose endpoints are added
|
---|
| 65 | */
|
---|
| 66 | private void addNodes(Way way) {
|
---|
| 67 | addNodes(way.firstNode());
|
---|
| 68 | addNodes(way.lastNode());
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Find out if the selection can be extended
|
---|
| 73 | *
|
---|
| 74 | * @return true if the selection can be extended
|
---|
| 75 | */
|
---|
| 76 | public boolean canExtend() {
|
---|
| 77 | return outerNodes != null && !outerNodes.isEmpty();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * Finds out if the current selection can be extended.
|
---|
| 82 | *
|
---|
| 83 | * @param selection current selection (ways and others)
|
---|
| 84 | * @param node perimeter node from which to extend the selection
|
---|
| 85 | * @return a way by which to extend the selection, or null
|
---|
| 86 | */
|
---|
| 87 | private static Way findWay(Collection<OsmPrimitive> selection, Node node) {
|
---|
| 88 | Way foundWay = null;
|
---|
| 89 |
|
---|
| 90 | for (Way way : OsmPrimitive.getFilteredList(node.getReferrers(),
|
---|
| 91 | Way.class)) {
|
---|
| 92 | if (way.getNodesCount() < 2 || !way.isFirstLastNode(node)
|
---|
| 93 | || selection.contains(way))
|
---|
| 94 | continue;
|
---|
| 95 |
|
---|
| 96 | /* A previously unselected way was found that is connected
|
---|
| 97 | to the node. */
|
---|
| 98 | if (foundWay != null)
|
---|
| 99 | /* This is not the only qualifying way. There is a
|
---|
| 100 | branch at the node, and we cannot extend the selection. */
|
---|
| 101 | return null;
|
---|
| 102 |
|
---|
| 103 | /* Remember the first found qualifying way. */
|
---|
| 104 | foundWay = way;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /* Return the only way found, or null if none was found. */
|
---|
| 108 | return foundWay;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Finds out if the current selection can be extended.
|
---|
[6830] | 113 | * <p>
|
---|
[6544] | 114 | * The members outerNodes, nodes must have been initialized.
|
---|
| 115 | * How to update these members when extending the selection, @see extend().
|
---|
[6830] | 116 | * </p>
|
---|
[6544] | 117 | * @param selection current selection
|
---|
| 118 | * @return a way by which to extend the selection, or null
|
---|
| 119 | */
|
---|
| 120 | private Way findWay(Collection<OsmPrimitive> selection) {
|
---|
| 121 | for (Node node : outerNodes) {
|
---|
| 122 | Way way = findWay(selection, node);
|
---|
| 123 | if (way != null)
|
---|
| 124 | return way;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | return null;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * Extend the current selection
|
---|
| 132 | *
|
---|
| 133 | * @param data the data set in which to extend the selection
|
---|
| 134 | */
|
---|
| 135 | public void extend(DataSet data) {
|
---|
| 136 | Collection<OsmPrimitive> currentSelection;
|
---|
| 137 | LinkedList<OsmPrimitive> selection;
|
---|
| 138 | boolean selectionChanged = false;
|
---|
| 139 | Way way;
|
---|
| 140 |
|
---|
| 141 | if (!canExtend())
|
---|
| 142 | return;
|
---|
| 143 |
|
---|
| 144 | currentSelection = data.getSelected();
|
---|
| 145 |
|
---|
| 146 | way = findWay(currentSelection);
|
---|
| 147 |
|
---|
| 148 | if (way == null)
|
---|
| 149 | return;
|
---|
| 150 |
|
---|
[7005] | 151 | selection = new LinkedList<>();
|
---|
[6544] | 152 | for (OsmPrimitive primitive : currentSelection)
|
---|
| 153 | selection.add(primitive);
|
---|
| 154 |
|
---|
| 155 | do {
|
---|
| 156 | if (!selection.add(way))
|
---|
| 157 | break;
|
---|
| 158 |
|
---|
| 159 | selectionChanged = true;
|
---|
| 160 | addNodes(way);
|
---|
| 161 |
|
---|
| 162 | way = findWay(selection);
|
---|
| 163 | } while (way != null);
|
---|
| 164 |
|
---|
| 165 | if (selectionChanged)
|
---|
| 166 | data.setSelected(selection, true);
|
---|
| 167 | }
|
---|
| 168 | }
|
---|