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;
|
---|
6 | import java.util.Set;
|
---|
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 | */
|
---|
23 | private Set<Node> outerNodes;
|
---|
24 | /**
|
---|
25 | * endpoints of selected ways
|
---|
26 | */
|
---|
27 | private Set<Node> nodes;
|
---|
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 {
|
---|
40 | nodes = new TreeSet<>();
|
---|
41 | outerNodes = new TreeSet<>();
|
---|
42 |
|
---|
43 | for (Way way : ways) {
|
---|
44 | addNodes(way);
|
---|
45 | }
|
---|
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) {
|
---|
55 | if (node == null) return;
|
---|
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)
|
---|
94 | || selection.contains(way))
|
---|
95 | continue;
|
---|
96 |
|
---|
97 | /* A previously unselected way was found that is connected
|
---|
98 | to the node. */
|
---|
99 | if (foundWay != null)
|
---|
100 | /* This is not the only qualifying way. There is a
|
---|
101 | branch at the node, and we cannot extend the selection. */
|
---|
102 | return null;
|
---|
103 |
|
---|
104 | /* Remember the first found qualifying way. */
|
---|
105 | foundWay = way;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* Return the only way found, or null if none was found. */
|
---|
109 | return foundWay;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Finds out if the current selection can be extended.
|
---|
114 | * <p>
|
---|
115 | * The members outerNodes, nodes must have been initialized.
|
---|
116 | * How to update these members when extending the selection, @see extend().
|
---|
117 | * </p>
|
---|
118 | * @param selection current selection
|
---|
119 | * @return a way by which to extend the selection, or null
|
---|
120 | */
|
---|
121 | private Way findWay(Collection<OsmPrimitive> selection) {
|
---|
122 | for (Node node : outerNodes) {
|
---|
123 | Way way = findWay(selection, node);
|
---|
124 | if (way != null)
|
---|
125 | return way;
|
---|
126 | }
|
---|
127 |
|
---|
128 | return null;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Extend the current selection
|
---|
133 | *
|
---|
134 | * @param data the data set in which to extend the selection
|
---|
135 | */
|
---|
136 | public void extend(DataSet data) {
|
---|
137 | Collection<OsmPrimitive> currentSelection;
|
---|
138 | Collection<OsmPrimitive> selection;
|
---|
139 | boolean selectionChanged = false;
|
---|
140 | Way way;
|
---|
141 |
|
---|
142 | if (!canExtend())
|
---|
143 | return;
|
---|
144 |
|
---|
145 | currentSelection = data.getSelected();
|
---|
146 |
|
---|
147 | way = findWay(currentSelection);
|
---|
148 |
|
---|
149 | if (way == null)
|
---|
150 | return;
|
---|
151 |
|
---|
152 | selection = new LinkedList<>();
|
---|
153 | for (OsmPrimitive primitive : currentSelection) {
|
---|
154 | selection.add(primitive);
|
---|
155 | }
|
---|
156 |
|
---|
157 | do {
|
---|
158 | if (!selection.add(way))
|
---|
159 | break;
|
---|
160 |
|
---|
161 | selectionChanged = true;
|
---|
162 | addNodes(way);
|
---|
163 |
|
---|
164 | way = findWay(selection);
|
---|
165 | } while (way != null);
|
---|
166 |
|
---|
167 | if (selectionChanged)
|
---|
168 | data.setSelected(selection, true);
|
---|
169 | }
|
---|
170 | }
|
---|