make parallelise even moar awesome
[potlatch2.git] / net / systemeD / potlatch2 / tools / Parallelise.as
1 package net.systemeD.potlatch2.tools {
2
3     import net.systemeD.halcyon.connection.*;
4
5         // FIXME:
6         // ** needs to be properly undoable
7
8         public class Parallelise {
9                 private var originalWay:Way;
10                 public var parallelWay:Way;
11                 private var connection:Connection;
12                 private var offsetx:Array=[];
13                 private var offsety:Array=[];
14                 private var df:Array=[];
15                 private var nodes:Object={};
16                 
17                 public function Parallelise(way:Way) {
18                         var a:Number, b:Number, h:Number, i:uint;
19                         connection  = Connection.getConnection();
20                         originalWay = way;
21                         parallelWay = connection.createWay({}, [], MainUndoStack.getGlobalStack().addAction);
22
23                         for (i=0; i<originalWay.length-1; i++) {
24                                 a=originalWay.getNode(i  ).latp - originalWay.getNode(i+1).latp;
25                                 b=originalWay.getNode(i+1).lon  - originalWay.getNode(i  ).lon;
26                                 h=Math.sqrt(a*a+b*b);
27                                 if (h!=0) { a=a/h; b=b/h; }
28                                          else { a=0; b=0; }
29                                 offsetx[i]=a;
30                                 offsety[i]=b;
31                         }
32
33                         for (i=1; i<originalWay.length-1; i++) {
34                                 a=det(offsetx[i]-offsetx[i-1],
35                                           offsety[i]-offsety[i-1],
36                                           originalWay.getNode(i+1).lon  - originalWay.getNode(i  ).lon,
37                                           originalWay.getNode(i+1).latp - originalWay.getNode(i  ).latp);
38                                 b=det(originalWay.getNode(i  ).lon  - originalWay.getNode(i-1).lon,
39                                           originalWay.getNode(i  ).latp - originalWay.getNode(i-1).latp,
40                                           originalWay.getNode(i+1).lon  - originalWay.getNode(i  ).lon,
41                                           originalWay.getNode(i+1).latp - originalWay.getNode(i  ).latp);
42                                 if (b!=0) { df[i]=a/b; } else { df[i]=0; }
43                         }
44
45                 }
46
47                 public function draw(offset:Number):void {
48                         var x:Number, y:Number;
49                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Draw parallel way");
50                         parallelWay.suspend();
51                         for (var i:int=0; i<originalWay.length; i++) {
52                                 if (i==0) {
53                                         x=originalWay.getNode(0).lon + offset * offsetx[0];
54                                         y=originalWay.getNode(0).latp+ offset * offsety[0];
55                                 } else if (i==originalWay.length-1) {
56                                         x=originalWay.getNode(i).lon + offset * offsetx[i-1];
57                                         y=originalWay.getNode(i).latp+ offset * offsety[i-1];
58                                 } else {
59                                         x=originalWay.getNode(i).lon + offset * (offsetx[i-1] + df[i] * (originalWay.getNode(i).lon - originalWay.getNode(i-1).lon ));
60                                         y=originalWay.getNode(i).latp+ offset * (offsety[i-1] + df[i] * (originalWay.getNode(i).latp- originalWay.getNode(i-1).latp));
61                                 }
62                                 if (nodes[i]) {
63                                         nodes[i].setLonLatp(x,y,undo.push);
64                                 } else {
65                                         nodes[i] = connection.createNode({},Node.latp2lat(y),x,undo.push);
66                                         parallelWay.appendNode(nodes[i], undo.push);
67                                 }
68                         }
69                         if (originalWay.isArea()) { parallelWay.appendNode(nodes[0],undo.push); }
70                         parallelWay.resume();
71                         MainUndoStack.getGlobalStack().addAction(undo);
72                 }
73                 
74                 private function det(a:Number,b:Number,c:Number,d:Number):Number { return a*d-b*c; }
75
76         }
77 }