source: josm/trunk/src/org/openstreetmap/josm/actions/mapmode/ParallelWays.java@ 11124

Last change on this file since 11124 was 10247, checked in by Don-vip, 8 years ago

findbugs - DLS_DEAD_LOCAL_STORE_OF_NULL

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.mapmode;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.HashMap;
8import java.util.HashSet;
9import java.util.List;
10import java.util.Map;
11import java.util.Set;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.CombineWayAction;
15import org.openstreetmap.josm.command.AddCommand;
16import org.openstreetmap.josm.command.Command;
17import org.openstreetmap.josm.command.SequenceCommand;
18import org.openstreetmap.josm.data.coor.EastNorth;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.Way;
21import org.openstreetmap.josm.tools.Geometry;
22
23/**
24 * Helper for ParallelWayAction
25 *
26 * @author Ole Jørgen Brønner (olejorgenb)
27 */
28public class ParallelWays {
29 private final List<Way> ways;
30 private final List<Node> sortedNodes;
31
32 private final int nodeCount;
33
34 private final EastNorth[] pts;
35 private final EastNorth[] normals;
36
37 // Need a reference way to determine the direction of the offset when we manage multiple ways
38 public ParallelWays(Collection<Way> sourceWays, boolean copyTags, int refWayIndex) {
39 // Possible/sensible to use PrimetiveDeepCopy here?
40
41 // Make a deep copy of the ways, keeping the copied ways connected
42 // TODO: This assumes the first/last nodes of the ways are the only possible shared nodes.
43 Map<Node, Node> splitNodeMap = new HashMap<>(sourceWays.size());
44 for (Way w : sourceWays) {
45 if (!splitNodeMap.containsKey(w.firstNode())) {
46 splitNodeMap.put(w.firstNode(), copyNode(w.firstNode(), copyTags));
47 }
48 if (!splitNodeMap.containsKey(w.lastNode())) {
49 splitNodeMap.put(w.lastNode(), copyNode(w.lastNode(), copyTags));
50 }
51 }
52 ways = new ArrayList<>(sourceWays.size());
53 for (Way w : sourceWays) {
54 Way wCopy = new Way();
55 wCopy.addNode(splitNodeMap.get(w.firstNode()));
56 for (int i = 1; i < w.getNodesCount() - 1; i++) {
57 wCopy.addNode(copyNode(w.getNode(i), copyTags));
58 }
59 wCopy.addNode(splitNodeMap.get(w.lastNode()));
60 if (copyTags) {
61 wCopy.setKeys(w.getKeys());
62 }
63 ways.add(wCopy);
64 }
65
66 // Find a linear ordering of the nodes. Fail if there isn't one.
67 CombineWayAction.NodeGraph nodeGraph = CombineWayAction.NodeGraph.createUndirectedGraphFromNodeWays(ways);
68 List<Node> sortedNodesPath = nodeGraph.buildSpanningPath();
69 if (sortedNodesPath == null)
70 throw new IllegalArgumentException("Ways must have spanning path"); // Create a dedicated exception?
71
72 // Fix #8631 - Remove duplicated nodes from graph to be robust with self-intersecting ways
73 Set<Node> removedNodes = new HashSet<>();
74 sortedNodes = new ArrayList<>();
75 for (int i = 0; i < sortedNodesPath.size(); i++) {
76 Node n = sortedNodesPath.get(i);
77 if (i < sortedNodesPath.size()-1) {
78 if (sortedNodesPath.get(i+1).getCoor().equals(n.getCoor())) {
79 removedNodes.add(n);
80 for (Way w : ways) {
81 w.removeNode(n);
82 }
83 continue;
84 }
85 }
86 if (!removedNodes.contains(n)) {
87 sortedNodes.add(n);
88 }
89 }
90
91 // Ugly method of ensuring that the offset isn't inverted. I'm sure there is a better and more elegant way
92 Way refWay = ways.get(refWayIndex);
93 boolean refWayReversed = true;
94 for (int i = 0; i < sortedNodes.size() - 1; i++) {
95 if (sortedNodes.get(i) == refWay.firstNode() && sortedNodes.get(i + 1) == refWay.getNode(1)) {
96 refWayReversed = false;
97 break;
98 }
99 }
100 if (refWayReversed) {
101 Collections.reverse(sortedNodes); // need to keep the orientation of the reference way.
102 }
103
104 // Initialize the required parameters. (segment normals, etc.)
105 nodeCount = sortedNodes.size();
106 pts = new EastNorth[nodeCount];
107 normals = new EastNorth[nodeCount - 1];
108 int i = 0;
109 for (Node n : sortedNodes) {
110 EastNorth t = n.getEastNorth();
111 pts[i] = t;
112 i++;
113 }
114 for (i = 0; i < nodeCount - 1; i++) {
115 double dx = pts[i + 1].getX() - pts[i].getX();
116 double dy = pts[i + 1].getY() - pts[i].getY();
117 double len = Math.sqrt(dx * dx + dy * dy);
118 normals[i] = new EastNorth(-dy / len, dx / len);
119 }
120 }
121
122 public boolean isClosedPath() {
123 return sortedNodes.get(0) == sortedNodes.get(sortedNodes.size() - 1);
124 }
125
126 /**
127 * Offsets the way(s) d units. Positive d means to the left (relative to the reference way)
128 * @param d offset
129 */
130 public void changeOffset(double d) {
131 // This is the core algorithm:
132 /* 1. Calculate a parallel line, offset by 'd', to each segment in the path
133 * 2. Find the intersection of lines belonging to neighboring segments. These become the new node positions
134 * 3. Do some special casing for closed paths
135 *
136 * Simple and probably not even close to optimal performance wise
137 */
138
139 EastNorth[] ppts = new EastNorth[nodeCount];
140
141 EastNorth prevA = pts[0].add(normals[0].scale(d));
142 EastNorth prevB = pts[1].add(normals[0].scale(d));
143 for (int i = 1; i < nodeCount - 1; i++) {
144 EastNorth a = pts[i].add(normals[i].scale(d));
145 EastNorth b = pts[i + 1].add(normals[i].scale(d));
146 if (Geometry.segmentsParallel(a, b, prevA, prevB)) {
147 ppts[i] = a;
148 } else {
149 ppts[i] = Geometry.getLineLineIntersection(a, b, prevA, prevB);
150 }
151 prevA = a;
152 prevB = b;
153 }
154 if (isClosedPath()) {
155 EastNorth a = pts[0].add(normals[0].scale(d));
156 EastNorth b = pts[1].add(normals[0].scale(d));
157 if (Geometry.segmentsParallel(a, b, prevA, prevB)) {
158 ppts[0] = a;
159 } else {
160 ppts[0] = Geometry.getLineLineIntersection(a, b, prevA, prevB);
161 }
162 ppts[nodeCount - 1] = ppts[0];
163 } else {
164 ppts[0] = pts[0].add(normals[0].scale(d));
165 ppts[nodeCount - 1] = pts[nodeCount - 1].add(normals[nodeCount - 2].scale(d));
166 }
167
168 for (int i = 0; i < nodeCount; i++) {
169 sortedNodes.get(i).setEastNorth(ppts[i]);
170 }
171 }
172
173 public void commit() {
174 SequenceCommand undoCommand = new SequenceCommand("Make parallel way(s)", makeAddWayAndNodesCommandList());
175 Main.main.undoRedo.add(undoCommand);
176 }
177
178 private List<Command> makeAddWayAndNodesCommandList() {
179 List<Command> commands = new ArrayList<>(sortedNodes.size() + ways.size());
180 for (int i = 0; i < sortedNodes.size() - (isClosedPath() ? 1 : 0); i++) {
181 commands.add(new AddCommand(sortedNodes.get(i)));
182 }
183 for (Way w : ways) {
184 commands.add(new AddCommand(w));
185 }
186 return commands;
187 }
188
189 private static Node copyNode(Node source, boolean copyTags) {
190 if (copyTags)
191 return new Node(source, true);
192 else {
193 Node n = new Node();
194 n.setCoor(source.getCoor());
195 return n;
196 }
197 }
198
199 public final List<Way> getWays() {
200 return ways;
201 }
202}
Note: See TracBrowser for help on using the repository browser.