| 1 | // License: GPL. See LICENSE file for details.
|
|---|
| 2 | //
|
|---|
| 3 | package org.openstreetmap.josm.actions;
|
|---|
| 4 |
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.List;
|
|---|
| 8 | import java.awt.event.ActionEvent;
|
|---|
| 9 | import java.awt.event.KeyEvent;
|
|---|
| 10 | import java.util.ArrayList;
|
|---|
| 11 | import java.util.Collection;
|
|---|
| 12 | import java.util.LinkedList;
|
|---|
| 13 |
|
|---|
| 14 | import javax.swing.JOptionPane;
|
|---|
| 15 |
|
|---|
| 16 | import org.openstreetmap.josm.Main;
|
|---|
| 17 | import org.openstreetmap.josm.command.AddCommand;
|
|---|
| 18 | import org.openstreetmap.josm.command.Command;
|
|---|
| 19 | import org.openstreetmap.josm.command.MoveCommand;
|
|---|
| 20 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 21 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 22 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 24 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 25 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 26 | import org.openstreetmap.josm.tools.DontShowAgainInfo;
|
|---|
| 27 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Align edges of a way so all angles are right angles.
|
|---|
| 31 | *
|
|---|
| 32 | * 1. Find orientation of all edges
|
|---|
| 33 | * 2. Compute main orientation, weighted by length of edge, normalized to angles between 0 and pi/2
|
|---|
| 34 | * 3. Rotate every edge around its center to align with main orientation or perpendicular to it
|
|---|
| 35 | * 4. Compute new intersection points of two adjascent edges
|
|---|
| 36 | * 5. Move nodes to these points
|
|---|
| 37 | */
|
|---|
| 38 | public final class OrthogonalizeAction extends JosmAction {
|
|---|
| 39 |
|
|---|
| 40 | public OrthogonalizeAction() {
|
|---|
| 41 | super(tr("Orthogonalize Shape"),
|
|---|
| 42 | "ortho",
|
|---|
| 43 | tr("Move nodes so all angles are 90 or 270 degree"),
|
|---|
| 44 | Shortcut.registerShortcut("tools:orthogonalize", tr("Tool: {0}", tr("Orthogonalize Shape")),
|
|---|
| 45 | KeyEvent.VK_Q,
|
|---|
| 46 | Shortcut.GROUP_EDIT), true);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | public void actionPerformed(ActionEvent e) {
|
|---|
| 50 |
|
|---|
| 51 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
|---|
| 52 |
|
|---|
| 53 | ArrayList<Node> dirnodes = new ArrayList<Node>();
|
|---|
| 54 |
|
|---|
| 55 | // Check the selection if it is suitible for the orthogonalization
|
|---|
| 56 | for (OsmPrimitive osm : sel) {
|
|---|
| 57 | // Check if not more than two nodes in the selection
|
|---|
| 58 | if(osm instanceof Node) {
|
|---|
| 59 | if(dirnodes.size() == 2) {
|
|---|
| 60 | JOptionPane.showMessageDialog(Main.parent, tr("Only two nodes allowed"));
|
|---|
| 61 | return;
|
|---|
| 62 | }
|
|---|
| 63 | dirnodes.add((Node) osm);
|
|---|
| 64 | continue;
|
|---|
| 65 | }
|
|---|
| 66 | // Check if selection consists now only of ways
|
|---|
| 67 | if (!(osm instanceof Way)) {
|
|---|
| 68 | JOptionPane.showMessageDialog(Main.parent, tr("Selection must consist only of ways."));
|
|---|
| 69 | return;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | // Check if every way is made of at least four segments and closed
|
|---|
| 73 | Way way = (Way)osm;
|
|---|
| 74 | if ((way.nodes.size() < 5) || (!way.nodes.get(0).equals(way.nodes.get(way.nodes.size() - 1)))) {
|
|---|
| 75 | JOptionPane.showMessageDialog(Main.parent, tr("Please select one ore more closed ways of at least four nodes."));
|
|---|
| 76 | return;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | // Check if every edge in the way is a definite edge of at least 45 degrees of direction change
|
|---|
| 80 | // Otherwise, two segments could be turned into same direction and intersection would fail.
|
|---|
| 81 | // Or changes of shape would be too serious.
|
|---|
| 82 | for (int i1=0; i1 < way.nodes.size()-1; i1++) {
|
|---|
| 83 | int i2 = (i1+1) % (way.nodes.size()-1);
|
|---|
| 84 | int i3 = (i1+2) % (way.nodes.size()-1);
|
|---|
| 85 | double angle1 =Math.abs(way.nodes.get(i1).eastNorth.heading(way.nodes.get(i2).eastNorth));
|
|---|
| 86 | double angle2 = Math.abs(way.nodes.get(i2).eastNorth.heading(way.nodes.get(i3).eastNorth));
|
|---|
| 87 | double delta = Math.abs(angle2 - angle1);
|
|---|
| 88 | while(delta > Math.PI) delta -= Math.PI;
|
|---|
| 89 | if(delta < Math.PI/4) {
|
|---|
| 90 | JOptionPane.showMessageDialog(Main.parent, tr("Please select ways with almost right angles to orthogonalize."));
|
|---|
| 91 | return;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | if ("EPSG:4326".equals(Main.proj.toString())) {
|
|---|
| 97 | String msg = tr("<html>You are using the EPSG:4326 projection which might lead<br>" +
|
|---|
| 98 | "to undesirable results when doing rectangular alignments.<br>" +
|
|---|
| 99 | "Change your projection to get rid of this warning.<br>" +
|
|---|
| 100 | "Do you want to continue?");
|
|---|
| 101 |
|
|---|
| 102 | if (!DontShowAgainInfo.show("align_rectangular_4326", msg, false)) {
|
|---|
| 103 | return;
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | // Check, if selection held neither none nor two nodes
|
|---|
| 107 | if(dirnodes.size() == 1) {
|
|---|
| 108 | JOptionPane.showMessageDialog(Main.parent, tr("Only one node selected"));
|
|---|
| 109 | return;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | // Now all checks are done and we can now do the neccessary computations
|
|---|
| 113 | // From here it is assumed that the above checks hold
|
|---|
| 114 | Collection<Command> cmds = new LinkedList<Command>();
|
|---|
| 115 | double align_to_heading = 0.0;
|
|---|
| 116 | boolean use_dirnodes = false;
|
|---|
| 117 |
|
|---|
| 118 | if (dirnodes.size() == 2) {
|
|---|
| 119 | // When selection contains two nodes, use the nodes to compute a direction
|
|---|
| 120 | // to align all ways to
|
|---|
| 121 | align_to_heading = normalize_angle(dirnodes.get(0).eastNorth.heading(dirnodes.get(1).eastNorth));
|
|---|
| 122 | use_dirnodes = true;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | for (OsmPrimitive osm : sel) {
|
|---|
| 126 | if(!(osm instanceof Way))
|
|---|
| 127 | continue;
|
|---|
| 128 |
|
|---|
| 129 | Way way = (Way)osm;
|
|---|
| 130 | int nodes = way.nodes.size();
|
|---|
| 131 | int sides = nodes - 1;
|
|---|
| 132 | // Copy necessary data into a more suitable data structure
|
|---|
| 133 | EastNorth en[] = new EastNorth[sides];
|
|---|
| 134 | for (int i=0; i < sides; i++) {
|
|---|
| 135 | en[i] = new EastNorth(way.nodes.get(i).eastNorth.east(), way.nodes.get(i).eastNorth.north());
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if (! use_dirnodes) {
|
|---|
| 139 | // To find orientation of all segments, compute weighted average of all segment's headings
|
|---|
| 140 | // all headings are mapped into [-PI/4, PI/4] by PI/2 rotations so both main orientations are mapped into one
|
|---|
| 141 | // the headings are weighted by the length of the segment establishing it, so a longer segment, that is more
|
|---|
| 142 | // likely to have the correct orientation, has more influence in the computing than a short segment, that is easier to misalign.
|
|---|
| 143 | double headings[] = new double[sides];
|
|---|
| 144 | double weights[] = new double[sides];
|
|---|
| 145 | for (int i=0; i < sides; i++) {
|
|---|
| 146 | headings[i] = normalize_angle(way.nodes.get(i).eastNorth.heading(way.nodes.get(i+1).eastNorth));
|
|---|
| 147 | weights[i] = way.nodes.get(i).eastNorth.distance(way.nodes.get(i+1).eastNorth);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | // CAVEAT: for orientations near -PI/4 or PI/4 the mapping into ONE orientation fails
|
|---|
| 151 | // resulting in a heading-difference between adjacent sides of almost PI/2
|
|---|
| 152 | // and a totally wrong average
|
|---|
| 153 | // check for this (use PI/3 as arbitray limit) and rotate into ONE orientation
|
|---|
| 154 | double angle_diff_max = 0.0;
|
|---|
| 155 | for (int i=0; i < sides; i++) {
|
|---|
| 156 | double diff = 0.0;
|
|---|
| 157 | if (i == 0) {
|
|---|
| 158 | diff = heading_diff(headings[i], headings[sides - 1]);
|
|---|
| 159 | } else {
|
|---|
| 160 | diff = heading_diff(headings[i], headings[i - 1]);
|
|---|
| 161 | }
|
|---|
| 162 | if (diff > angle_diff_max) angle_diff_max = diff;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | if (angle_diff_max > Math.PI/3) {
|
|---|
| 166 | // rearrange headings: everything < 0 gets PI/2-rotated
|
|---|
| 167 | for (int i=0; i < sides; i++) {
|
|---|
| 168 | if (headings[i] < 0)
|
|---|
| 169 | headings[i] += Math.PI/2;
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | // TODO:
|
|---|
| 174 | // use angle_diff_max as an indicator that the way is already orthogonal
|
|---|
| 175 | // e.g. if angle_diff_max is less then Math.toRadians(0.5)
|
|---|
| 176 | // and do nothing in that case (?)
|
|---|
| 177 |
|
|---|
| 178 | // Compute the weighted average of the headings of all segments
|
|---|
| 179 | double sum_weighted_headings = 0.0;
|
|---|
| 180 | double sum_weights = 0.0;
|
|---|
| 181 | for (int i=0; i < sides; i++) {
|
|---|
| 182 | sum_weighted_headings += headings[i] * weights[i];
|
|---|
| 183 | sum_weights += weights[i];
|
|---|
| 184 | }
|
|---|
| 185 | align_to_heading = normalize_angle(sum_weighted_headings/sum_weights);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 | for (int i=0; i < sides; i++) {
|
|---|
| 190 | // Compute handy indices of three nodes to be used in one loop iteration.
|
|---|
| 191 | // We use segments (i1,i2) and (i2,i3), align them and compute the new
|
|---|
| 192 | // position of the i2-node as the intersection of the realigned (i1,i2), (i2,i3) segments
|
|---|
| 193 | // Not the most efficient algorithm, but we don't handle millions of nodes...
|
|---|
| 194 | int i1 = i;
|
|---|
| 195 | int i2 = (i+1)%sides;
|
|---|
| 196 | int i3 = (i+2)%sides;
|
|---|
| 197 | double heading1, heading2;
|
|---|
| 198 | double delta1, delta2;
|
|---|
| 199 | // Compute neccessary rotation of first segment to align it with main orientation
|
|---|
| 200 | heading1 = normalize_angle(en[i1].heading(en[i2]), align_to_heading);
|
|---|
| 201 | delta1 = align_to_heading - heading1;
|
|---|
| 202 | // Compute neccessary rotation of second segment to align it with main orientation
|
|---|
| 203 | heading2 = normalize_angle(en[i2].heading(en[i3]), align_to_heading);
|
|---|
| 204 | delta2 = align_to_heading - heading2;
|
|---|
| 205 | // To align a segment, rotate around its center
|
|---|
| 206 | EastNorth pivot1 = new EastNorth((en[i1].east()+en[i2].east())/2, (en[i1].north()+en[i2].north())/2);
|
|---|
| 207 | EastNorth A=en[i1].rotate(pivot1, delta1);
|
|---|
| 208 | EastNorth B=en[i2].rotate(pivot1, delta1);
|
|---|
| 209 | EastNorth pivot2 = new EastNorth((en[i2].east()+en[i3].east())/2, (en[i2].north()+en[i3].north())/2);
|
|---|
| 210 | EastNorth C=en[i2].rotate(pivot2, delta2);
|
|---|
| 211 | EastNorth D=en[i3].rotate(pivot2, delta2);
|
|---|
| 212 |
|
|---|
| 213 | // compute intersection of segments
|
|---|
| 214 | double u=det(B.east() - A.east(), B.north() - A.north(),
|
|---|
| 215 | C.east() - D.east(), C.north() - D.north());
|
|---|
| 216 |
|
|---|
| 217 | // Check for parallel segments and do nothing if they are
|
|---|
| 218 | // In practice this will probably only happen when a way has
|
|---|
| 219 | // been duplicated
|
|---|
| 220 |
|
|---|
| 221 | if (u == 0) continue;
|
|---|
| 222 |
|
|---|
| 223 | // q is a number between 0 and 1
|
|---|
| 224 | // It is the point in the segment where the intersection occurs
|
|---|
| 225 | // if the segment is scaled to length 1
|
|---|
| 226 |
|
|---|
| 227 | double q = det(B.north() - C.north(), B.east() - C.east(),
|
|---|
| 228 | D.north() - C.north(), D.east() - C.east()) / u;
|
|---|
| 229 | EastNorth intersection = new EastNorth(
|
|---|
| 230 | B.east() + q * (A.east() - B.east()),
|
|---|
| 231 | B.north() + q * (A.north() - B.north()));
|
|---|
| 232 |
|
|---|
| 233 | Node n = way.nodes.get(i2);
|
|---|
| 234 |
|
|---|
| 235 | LatLon ill = Main.proj.eastNorth2latlon(intersection);
|
|---|
| 236 | if (!ill.equalsEpsilon(n.coor)) {
|
|---|
| 237 | double dx = intersection.east()-n.eastNorth.east();
|
|---|
| 238 | double dy = intersection.north()-n.eastNorth.north();
|
|---|
| 239 | cmds.add(new MoveCommand(n, dx, dy));
|
|---|
| 240 | }
|
|---|
| 241 | }
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | if (cmds.size() > 0) {
|
|---|
| 245 | Main.main.undoRedo.add(new SequenceCommand(tr("Orthogonalize"), cmds));
|
|---|
| 246 | Main.map.repaint();
|
|---|
| 247 | }
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | static double det(double a, double b, double c, double d)
|
|---|
| 251 | {
|
|---|
| 252 | return a * d - b * c;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | static double normalize_angle(double h) {
|
|---|
| 256 | return normalize_angle(h, 0.0);
|
|---|
| 257 | }
|
|---|
| 258 | static double normalize_angle(double h, double align_to) {
|
|---|
| 259 | double llimit = -Math.PI/4;
|
|---|
| 260 | double ulimit = Math.PI/4;
|
|---|
| 261 | while (h - align_to > ulimit) h -= Math.PI/2;
|
|---|
| 262 | while (h - align_to < llimit) h += Math.PI/2;
|
|---|
| 263 |
|
|---|
| 264 | return h;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | static double heading_diff(double h1, double h2) {
|
|---|
| 268 | double heading_delta = h1 > h2 ? h1 - h2 : h2 - h1;
|
|---|
| 269 | return heading_delta;
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|