parallelise fixes
authorRichard Fairhurst <richard@systemed.net>
Mon, 1 Nov 2010 12:19:22 +0000 (12:19 +0000)
committerRichard Fairhurst <richard@systemed.net>
Mon, 1 Nov 2010 12:19:22 +0000 (12:19 +0000)
TODO.txt
net/systemeD/potlatch2/tools/Parallelise.as

index 9c2e6d8..642d0af 100644 (file)
--- a/TODO.txt
+++ b/TODO.txt
@@ -6,7 +6,6 @@ Potlatch 2: main outstanding issues
 * purgeOutside simply doesn't work - WayUIs stay around. To investigate
 * Some combination of zoom / pan / purge leaves a copy of the selected WayUI around
 * P1-style J (join) / shift-J (unjoin)
-* Make parallelise properly undoable
 
 == Vector background layers ==
 
index 51ea53d..ae3c35e 100644 (file)
@@ -2,9 +2,6 @@ package net.systemeD.potlatch2.tools {
 
     import net.systemeD.halcyon.connection.*;
 
-       // FIXME:
-       // ** needs to be properly undoable
-
        public class Parallelise {
                private var originalWay:Way;
                public var parallelWay:Way;
@@ -15,14 +12,15 @@ package net.systemeD.potlatch2.tools {
                private var nodes:Object={};
                
                public function Parallelise(way:Way) {
-                       var a:Number, b:Number, h:Number, i:uint;
+                       var a:Number, b:Number, h:Number, i:uint, j:uint, k:int;
                        connection  = Connection.getConnection();
                        originalWay = way;
                        parallelWay = connection.createWay({}, [], MainUndoStack.getGlobalStack().addAction);
 
-                       for (i=0; i<originalWay.length-1; i++) {
-                               a=originalWay.getNode(i  ).latp - originalWay.getNode(i+1).latp;
-                               b=originalWay.getNode(i+1).lon  - originalWay.getNode(i  ).lon;
+                       for (i=0; i<originalWay.length; i++) {
+                               j=(i+1) % originalWay.length;
+                               a=originalWay.getNode(i).latp - originalWay.getNode(j).latp;
+                               b=originalWay.getNode(j).lon  - originalWay.getNode(i).lon;
                                h=Math.sqrt(a*a+b*b);
                                if (h!=0) { a=a/h; b=b/h; }
                                         else { a=0; b=0; }
@@ -30,15 +28,17 @@ package net.systemeD.potlatch2.tools {
                                offsety[i]=b;
                        }
 
-                       for (i=1; i<originalWay.length-1; i++) {
-                               a=det(offsetx[i]-offsetx[i-1],
-                                         offsety[i]-offsety[i-1],
-                                         originalWay.getNode(i+1).lon  - originalWay.getNode(i  ).lon,
-                                         originalWay.getNode(i+1).latp - originalWay.getNode(i  ).latp);
-                               b=det(originalWay.getNode(i  ).lon  - originalWay.getNode(i-1).lon,
-                                         originalWay.getNode(i  ).latp - originalWay.getNode(i-1).latp,
-                                         originalWay.getNode(i+1).lon  - originalWay.getNode(i  ).lon,
-                                         originalWay.getNode(i+1).latp - originalWay.getNode(i  ).latp);
+                       for (i=0; i<originalWay.length; i++) {
+                               j=(i+1) % originalWay.length;
+                               k=i-1; if (k==-1) { k=originalWay.length-2; }   // ** it's -2 because if this is an area, node[length-1] is the same as node[0]
+                               a=det(offsetx[i]-offsetx[k],
+                                         offsety[i]-offsety[k],
+                                         originalWay.getNode(j).lon  - originalWay.getNode(i).lon,
+                                         originalWay.getNode(j).latp - originalWay.getNode(i).latp);
+                               b=det(originalWay.getNode(i).lon  - originalWay.getNode(k).lon,
+                                         originalWay.getNode(i).latp - originalWay.getNode(k).latp,
+                                         originalWay.getNode(j).lon  - originalWay.getNode(i).lon,
+                                         originalWay.getNode(j).latp - originalWay.getNode(i).latp);
                                if (b!=0) { df[i]=a/b; } else { df[i]=0; }
                        }
 
@@ -50,9 +50,15 @@ package net.systemeD.potlatch2.tools {
                        parallelWay.suspend();
                        for (var i:int=0; i<originalWay.length; i++) {
                                if (i==0) {
-                                       x=originalWay.getNode(0).lon + offset * offsetx[0];
-                                       y=originalWay.getNode(0).latp+ offset * offsety[0];
+                                       if (originalWay.isArea()) {
+                                               x=originalWay.getNode(i).lon + offset * (offsetx[originalWay.length-2] + df[i] * (originalWay.getNode(i).lon - originalWay.getNode(originalWay.length-2).lon ));
+                                               y=originalWay.getNode(i).latp+ offset * (offsety[originalWay.length-2] + df[i] * (originalWay.getNode(i).latp- originalWay.getNode(originalWay.length-2).latp));
+                                       } else {
+                                               x=originalWay.getNode(0).lon + offset * offsetx[0];
+                                               y=originalWay.getNode(0).latp+ offset * offsety[0];
+                                       }
                                } else if (i==originalWay.length-1) {
+                                       if (originalWay.isArea()) { continue; }         // node[length-1] is the same as node[0] if it's an area, so skip
                                        x=originalWay.getNode(i).lon + offset * offsetx[i-1];
                                        y=originalWay.getNode(i).latp+ offset * offsety[i-1];
                                } else {
@@ -66,9 +72,10 @@ package net.systemeD.potlatch2.tools {
                                        parallelWay.appendNode(nodes[i], undo.push);
                                }
                        }
+
                        if (originalWay.isArea()) { parallelWay.appendNode(nodes[0],undo.push); }
                        parallelWay.resume();
-                       MainUndoStack.getGlobalStack().addAction(undo);
+                       undo.doAction();                // don't actually add it to the undo stack, just do it!
                }
                
                private function det(a:Number,b:Number,c:Number,d:Number):Number { return a*d-b*c; }