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

Last change on this file since 11885 was 11385, checked in by Don-vip, 7 years ago

sonar - squid:S1066 - Collapsible "if" statements should be merged

  • 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 && sortedNodesPath.get(i+1).getCoor().equals(n.getCoor())) {
78 removedNodes.add(n);
79 for (Way w : ways) {
80 w.removeNode(n);
81 }
82 continue;
83 }
84 if (!removedNodes.contains(n)) {
85 sortedNodes.add(n);
86 }
87 }
88
89 // Ugly method of ensuring that the offset isn't inverted. I'm sure there is a better and more elegant way
90 Way refWay = ways.get(refWayIndex);
91 boolean refWayReversed = true;
92 for (int i = 0; i < sortedNodes.size() - 1; i++) {
93 if (sortedNodes.get(i) == refWay.firstNode() && sortedNodes.get(i + 1) == refWay.getNode(1)) {
94 refWayReversed = false;
95 break;
96 }
97 }
98 if (refWayReversed) {
99 Collections.reverse(sortedNodes); // need to keep the orientation of the reference way.
100 }
101
102 // Initialize the required parameters. (segment normals, etc.)
103 nodeCount = sortedNodes.size();
104 pts = new EastNorth[nodeCount];
105 normals = new EastNorth[nodeCount - 1];
106 int i = 0;
107 for (Node n : sortedNodes) {
108 EastNorth t = n.getEastNorth();
109 pts[i] = t;
110 i++;
111 }
112 for (i = 0; i < nodeCount - 1; i++) {
113 double dx = pts[i + 1].getX() - pts[i].getX();
114 double dy = pts[i + 1].getY() - pts[i].getY();
115 double len = Math.sqrt(dx * dx + dy * dy);
116 normals[i] = new EastNorth(-dy / len, dx / len);
117 }
118 }
119
120 public boolean isClosedPath() {
121 return sortedNodes.get(0) == sortedNodes.get(sortedNodes.size() - 1);
122 }
123
124 /**
125 * Offsets the way(s) d units. Positive d means to the left (relative to the reference way)
126 * @param d offset
127 */
128 public void changeOffset(double d) {
129 // This is the core algorithm:
130 /* 1. Calculate a parallel line, offset by 'd', to each segment in the path
131 * 2. Find the intersection of lines belonging to neighboring segments. These become the new node positions
132 * 3. Do some special casing for closed paths
133 *
134 * Simple and probably not even close to optimal performance wise
135 */
136
137 EastNorth[] ppts = new EastNorth[nodeCount];
138
139 EastNorth prevA = pts[0].add(normals[0].scale(d));
140 EastNorth prevB = pts[1].add(normals[0].scale(d));
141 for (int i = 1; i < nodeCount - 1; i++) {
142 EastNorth a = pts[i].add(normals[i].scale(d));
143 EastNorth b = pts[i + 1].add(normals[i].scale(d));
144 if (Geometry.segmentsParallel(a, b, prevA, prevB)) {
145 ppts[i] = a;
146 } else {
147 ppts[i] = Geometry.getLineLineIntersection(a, b, prevA, prevB);
148 }
149 prevA = a;
150 prevB = b;
151 }
152 if (isClosedPath()) {
153 EastNorth a = pts[0].add(normals[0].scale(d));
154 EastNorth b = pts[1].add(normals[0].scale(d));
155 if (Geometry.segmentsParallel(a, b, prevA, prevB)) {
156 ppts[0] = a;
157 } else {
158 ppts[0] = Geometry.getLineLineIntersection(a, b, prevA, prevB);
159 }
160 ppts[nodeCount - 1] = ppts[0];
161 } else {
162 ppts[0] = pts[0].add(normals[0].scale(d));
163 ppts[nodeCount - 1] = pts[nodeCount - 1].add(normals[nodeCount - 2].scale(d));
164 }
165
166 for (int i = 0; i < nodeCount; i++) {
167 sortedNodes.get(i).setEastNorth(ppts[i]);
168 }
169 }
170
171 public void commit() {
172 SequenceCommand undoCommand = new SequenceCommand("Make parallel way(s)", makeAddWayAndNodesCommandList());
173 Main.main.undoRedo.add(undoCommand);
174 }
175
176 private List<Command> makeAddWayAndNodesCommandList() {
177 List<Command> commands = new ArrayList<>(sortedNodes.size() + ways.size());
178 for (int i = 0; i < sortedNodes.size() - (isClosedPath() ? 1 : 0); i++) {
179 commands.add(new AddCommand(sortedNodes.get(i)));
180 }
181 for (Way w : ways) {
182 commands.add(new AddCommand(w));
183 }
184 return commands;
185 }
186
187 private static Node copyNode(Node source, boolean copyTags) {
188 if (copyTags)
189 return new Node(source, true);
190 else {
191 Node n = new Node();
192 n.setCoor(source.getCoor());
193 return n;
194 }
195 }
196
197 public final List<Way> getWays() {
198 return ways;
199 }
200}
Note: See TracBrowser for help on using the repository browser.