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