1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.LinkedHashMap;
|
---|
8 | import java.util.LinkedHashSet;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Map;
|
---|
12 | import java.util.Optional;
|
---|
13 | import java.util.Set;
|
---|
14 | import java.util.Stack;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.tools.Pair;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * A directed or undirected graph of nodes.
|
---|
20 | * @since 12463 (extracted from CombineWayAction)
|
---|
21 | */
|
---|
22 | public class NodeGraph {
|
---|
23 | public static List<NodePair> buildNodePairs(Way way, boolean directed) {
|
---|
24 | List<NodePair> pairs = new ArrayList<>();
|
---|
25 | for (Pair<Node, Node> pair: way.getNodePairs(false /* don't sort */)) {
|
---|
26 | pairs.add(new NodePair(pair));
|
---|
27 | if (!directed) {
|
---|
28 | pairs.add(new NodePair(pair).swap());
|
---|
29 | }
|
---|
30 | }
|
---|
31 | return pairs;
|
---|
32 | }
|
---|
33 |
|
---|
34 | public static List<NodePair> buildNodePairs(List<Way> ways, boolean directed) {
|
---|
35 | List<NodePair> pairs = new ArrayList<>();
|
---|
36 | for (Way w: ways) {
|
---|
37 | pairs.addAll(buildNodePairs(w, directed));
|
---|
38 | }
|
---|
39 | return pairs;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static List<NodePair> eliminateDuplicateNodePairs(List<NodePair> pairs) {
|
---|
43 | List<NodePair> cleaned = new ArrayList<>();
|
---|
44 | for (NodePair p: pairs) {
|
---|
45 | if (!cleaned.contains(p) && !cleaned.contains(p.swap())) {
|
---|
46 | cleaned.add(p);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | return cleaned;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static NodeGraph createDirectedGraphFromNodePairs(List<NodePair> pairs) {
|
---|
53 | NodeGraph graph = new NodeGraph();
|
---|
54 | for (NodePair pair: pairs) {
|
---|
55 | graph.add(pair);
|
---|
56 | }
|
---|
57 | return graph;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static NodeGraph createDirectedGraphFromWays(Collection<Way> ways) {
|
---|
61 | NodeGraph graph = new NodeGraph();
|
---|
62 | for (Way w: ways) {
|
---|
63 | graph.add(buildNodePairs(w, true /* directed */));
|
---|
64 | }
|
---|
65 | return graph;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Create an undirected graph from the given node pairs.
|
---|
70 | * @param pairs Node pairs to build the graph from
|
---|
71 | * @return node graph structure
|
---|
72 | */
|
---|
73 | public static NodeGraph createUndirectedGraphFromNodeList(List<NodePair> pairs) {
|
---|
74 | NodeGraph graph = new NodeGraph();
|
---|
75 | for (NodePair pair: pairs) {
|
---|
76 | graph.add(pair);
|
---|
77 | graph.add(pair.swap());
|
---|
78 | }
|
---|
79 | return graph;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Create an undirected graph from the given ways, but prevent reversing of all
|
---|
84 | * non-new ways by fix one direction.
|
---|
85 | * @param ways Ways to build the graph from
|
---|
86 | * @return node graph structure
|
---|
87 | * @since 8181
|
---|
88 | */
|
---|
89 | public static NodeGraph createUndirectedGraphFromNodeWays(Collection<Way> ways) {
|
---|
90 | NodeGraph graph = new NodeGraph();
|
---|
91 | for (Way w: ways) {
|
---|
92 | graph.add(buildNodePairs(w, false /* undirected */));
|
---|
93 | }
|
---|
94 | return graph;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public static NodeGraph createNearlyUndirectedGraphFromNodeWays(Collection<Way> ways) {
|
---|
98 | boolean dir = true;
|
---|
99 | NodeGraph graph = new NodeGraph();
|
---|
100 | for (Way w: ways) {
|
---|
101 | if (!w.isNew()) {
|
---|
102 | /* let the first non-new way give the direction (see #5880) */
|
---|
103 | graph.add(buildNodePairs(w, dir));
|
---|
104 | dir = false;
|
---|
105 | } else {
|
---|
106 | graph.add(buildNodePairs(w, false /* undirected */));
|
---|
107 | }
|
---|
108 | }
|
---|
109 | return graph;
|
---|
110 | }
|
---|
111 |
|
---|
112 | private final Set<NodePair> edges;
|
---|
113 | private int numUndirectedEges;
|
---|
114 | private final Map<Node, List<NodePair>> successors = new LinkedHashMap<>();
|
---|
115 | private final Map<Node, List<NodePair>> predecessors = new LinkedHashMap<>();
|
---|
116 |
|
---|
117 | protected void rememberSuccessor(NodePair pair) {
|
---|
118 | if (successors.containsKey(pair.getA())) {
|
---|
119 | if (!successors.get(pair.getA()).contains(pair)) {
|
---|
120 | successors.get(pair.getA()).add(pair);
|
---|
121 | }
|
---|
122 | } else {
|
---|
123 | List<NodePair> l = new ArrayList<>();
|
---|
124 | l.add(pair);
|
---|
125 | successors.put(pair.getA(), l);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected void rememberPredecessors(NodePair pair) {
|
---|
130 | if (predecessors.containsKey(pair.getB())) {
|
---|
131 | if (!predecessors.get(pair.getB()).contains(pair)) {
|
---|
132 | predecessors.get(pair.getB()).add(pair);
|
---|
133 | }
|
---|
134 | } else {
|
---|
135 | List<NodePair> l = new ArrayList<>();
|
---|
136 | l.add(pair);
|
---|
137 | predecessors.put(pair.getB(), l);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected boolean isTerminalNode(Node n) {
|
---|
142 | if (successors.get(n) == null) return false;
|
---|
143 | if (successors.get(n).size() != 1) return false;
|
---|
144 | if (predecessors.get(n) == null) return true;
|
---|
145 | if (predecessors.get(n).size() == 1) {
|
---|
146 | NodePair p1 = successors.get(n).get(0);
|
---|
147 | NodePair p2 = predecessors.get(n).get(0);
|
---|
148 | return p1.equals(p2.swap());
|
---|
149 | }
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 |
|
---|
153 | protected void prepare() {
|
---|
154 | Set<NodePair> undirectedEdges = new LinkedHashSet<>();
|
---|
155 | successors.clear();
|
---|
156 | predecessors.clear();
|
---|
157 |
|
---|
158 | for (NodePair pair: edges) {
|
---|
159 | if (!undirectedEdges.contains(pair) && !undirectedEdges.contains(pair.swap())) {
|
---|
160 | undirectedEdges.add(pair);
|
---|
161 | }
|
---|
162 | rememberSuccessor(pair);
|
---|
163 | rememberPredecessors(pair);
|
---|
164 | }
|
---|
165 | numUndirectedEges = undirectedEdges.size();
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Constructs a new {@code NodeGraph}.
|
---|
170 | */
|
---|
171 | public NodeGraph() {
|
---|
172 | edges = new LinkedHashSet<>();
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Add a node pair.
|
---|
177 | * @param pair node pair
|
---|
178 | */
|
---|
179 | public void add(NodePair pair) {
|
---|
180 | if (!edges.contains(pair)) {
|
---|
181 | edges.add(pair);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Add a list of node pairs.
|
---|
187 | * @param pairs list of node pairs
|
---|
188 | */
|
---|
189 | public void add(Collection<NodePair> pairs) {
|
---|
190 | for (NodePair pair: pairs) {
|
---|
191 | add(pair);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | protected Set<Node> getTerminalNodes() {
|
---|
196 | Set<Node> ret = new LinkedHashSet<>();
|
---|
197 | for (Node n: getNodes()) {
|
---|
198 | if (isTerminalNode(n)) {
|
---|
199 | ret.add(n);
|
---|
200 | }
|
---|
201 | }
|
---|
202 | return ret;
|
---|
203 | }
|
---|
204 |
|
---|
205 | protected List<NodePair> getOutboundPairs(NodePair pair) {
|
---|
206 | return getOutboundPairs(pair.getB());
|
---|
207 | }
|
---|
208 |
|
---|
209 | protected List<NodePair> getOutboundPairs(Node node) {
|
---|
210 | return Optional.ofNullable(successors.get(node)).orElseGet(Collections::emptyList);
|
---|
211 | }
|
---|
212 |
|
---|
213 | protected Set<Node> getNodes() {
|
---|
214 | Set<Node> nodes = new LinkedHashSet<>(2 * edges.size());
|
---|
215 | for (NodePair pair: edges) {
|
---|
216 | nodes.add(pair.getA());
|
---|
217 | nodes.add(pair.getB());
|
---|
218 | }
|
---|
219 | return nodes;
|
---|
220 | }
|
---|
221 |
|
---|
222 | protected boolean isSpanningWay(Stack<NodePair> way) {
|
---|
223 | return numUndirectedEges == way.size();
|
---|
224 | }
|
---|
225 |
|
---|
226 | protected List<Node> buildPathFromNodePairs(Stack<NodePair> path) {
|
---|
227 | List<Node> ret = new LinkedList<>();
|
---|
228 | for (NodePair pair: path) {
|
---|
229 | ret.add(pair.getA());
|
---|
230 | }
|
---|
231 | ret.add(path.peek().getB());
|
---|
232 | return ret;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Tries to find a spanning path starting from node <code>startNode</code>.
|
---|
237 | *
|
---|
238 | * Traverses the path in depth-first order.
|
---|
239 | *
|
---|
240 | * @param startNode the start node
|
---|
241 | * @return the spanning path; null, if no path is found
|
---|
242 | */
|
---|
243 | protected List<Node> buildSpanningPath(Node startNode) {
|
---|
244 | if (startNode == null)
|
---|
245 | return null;
|
---|
246 | Stack<NodePair> path = new Stack<>();
|
---|
247 | Stack<NodePair> nextPairs = new Stack<>();
|
---|
248 | nextPairs.addAll(getOutboundPairs(startNode));
|
---|
249 | while (!nextPairs.isEmpty()) {
|
---|
250 | NodePair cur = nextPairs.pop();
|
---|
251 | if (!path.contains(cur) && !path.contains(cur.swap())) {
|
---|
252 | while (!path.isEmpty() && !path.peek().isPredecessorOf(cur)) {
|
---|
253 | path.pop();
|
---|
254 | }
|
---|
255 | path.push(cur);
|
---|
256 | if (isSpanningWay(path)) return buildPathFromNodePairs(path);
|
---|
257 | nextPairs.addAll(getOutboundPairs(path.peek()));
|
---|
258 | }
|
---|
259 | }
|
---|
260 | return null;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Tries to find a path through the graph which visits each edge (i.e.
|
---|
265 | * the segment of a way) exactly once.
|
---|
266 | *
|
---|
267 | * @return the path; null, if no path was found
|
---|
268 | */
|
---|
269 | public List<Node> buildSpanningPath() {
|
---|
270 | prepare();
|
---|
271 | // try to find a path from each "terminal node", i.e. from a
|
---|
272 | // node which is connected by exactly one undirected edges (or
|
---|
273 | // two directed edges in opposite direction) to the graph. A
|
---|
274 | // graph built up from way segments is likely to include such
|
---|
275 | // nodes, unless all ways are closed.
|
---|
276 | // In the worst case this loops over all nodes which is very slow for large ways.
|
---|
277 | //
|
---|
278 | Set<Node> nodes = getTerminalNodes();
|
---|
279 | nodes = nodes.isEmpty() ? getNodes() : nodes;
|
---|
280 | for (Node n: nodes) {
|
---|
281 | List<Node> path = buildSpanningPath(n);
|
---|
282 | if (path != null)
|
---|
283 | return path;
|
---|
284 | }
|
---|
285 | return null;
|
---|
286 | }
|
---|
287 | }
|
---|