d251369d3d4479a0df93c4ebc89cc3ffb89197ab
[potlatch2.git] / net / systemeD / halcyon / connection / Way.as
1 package net.systemeD.halcyon.connection {
2     import flash.geom.Point;
3         import net.systemeD.halcyon.Globals;
4         import net.systemeD.halcyon.connection.actions.*;
5
6     public class Way extends Entity {
7         private var nodes:Array;
8                 public static var entity_type:String = 'way';
9
10         public function Way(id:Number, version:uint, tags:Object, loaded:Boolean, nodes:Array) {
11             super(id, version, tags, loaded);
12             this.nodes = nodes;
13                         for each (var node:Node in nodes) { node.addParent(this); }
14         }
15
16                 public function update(version:uint, tags:Object, loaded:Boolean, nodes:Array):void {
17                         var node:Node;
18                         for each (node in this.nodes) { node.removeParent(this); }
19                         updateEntityProperties(version,tags,loaded); this.nodes=nodes;
20                         for each (node in nodes) { node.addParent(this); }
21                 }
22                 
23         public function get length():uint {
24             return nodes.length;
25         }
26         
27         public function getNode(index:uint):Node {
28             return nodes[index];
29         }
30
31                 public function getLastNode():Node {
32                         return nodes[nodes.length-1];
33                 }
34
35         public function insertNode(index:uint, node:Node, performAction:Function):void {
36                         performAction(new AddNodeToWayAction(this, node, nodes, index));
37         }
38
39         public function appendNode(node:Node, performAction:Function):uint {
40                         performAction(new AddNodeToWayAction(this, node, nodes, -1));
41             return nodes.length + 1;
42         }
43         
44         public function prependNode(node:Node, performAction:Function):uint {
45                         performAction(new AddNodeToWayAction(this, node, nodes, 0));
46             return nodes.length + 1;
47         }
48         
49         public function indexOfNode(node:Node):uint {
50             return nodes.indexOf(node);
51         }
52
53                 public function removeNode(node:Node, performAction:Function):void {
54                         performAction(new RemoveNodeFromWayAction(this, node, nodes));
55                 }
56
57         public function removeNodeByIndex(index:uint):void {
58             var removed:Array=nodes.splice(index, 1);
59                         if (nodes.indexOf(removed[0])==-1) { removed[0].removeParent(this); }
60                         markDirty();
61             dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, removed[0], this, index));
62         }
63
64                 public function sliceNodes(start:int,end:int):Array {
65                         return nodes.slice(start,end);
66                 }
67
68                 public function deleteNodesFrom(start:int):void {
69                         for (var i:int=start; i<nodes.length; i++) {
70                                 nodes[i].removeParent(this);
71                         }
72                         nodes.splice(start);
73                         markDirty();
74                 }
75
76                 public function mergeWith(way:Way,topos:int,frompos:int, performAction:Function):void {
77                         performAction(new MergeWaysAction(this, way, topos, frompos));
78                 }
79                 
80                 public function addToEnd(topos:int,node:Node, performAction:Function):void {
81                         if (topos==0) {
82                                 if (nodes[0]==node) { return; }
83                                 prependNode(node, performAction);
84                         } else {
85                                 if (nodes[nodes.length-1]==node) { return; }
86                                 appendNode(node, performAction);
87                         }
88                 }
89
90         public function reverseNodes(performAction:Function):void {
91             performAction(new ReverseNodesAction(this, nodes));
92         }
93
94         /**
95          * Finds the 1st way segment which intersects the projected
96          * coordinate and adds the node to that segment. If snap is
97          * specified then the node is moved to exactly bisect the
98          * segment.
99          */
100         public function insertNodeAtClosestPosition(newNode:Node, isSnap:Boolean, performAction:Function):int {
101             var closestProportion:Number = 1;
102             var newIndex:uint = 0;
103             var nP:Point = new Point(newNode.lon, newNode.latp);
104             var snapped:Point = null;
105             
106             for ( var i:uint; i < length - 1; i++ ) {
107                 var node1:Node = getNode(i);
108                 var node2:Node = getNode(i+1);
109                 var p1:Point = new Point(node1.lon, node1.latp);
110                 var p2:Point = new Point(node2.lon, node2.latp);
111                 
112                 var directDist:Number = Point.distance(p1, p2);
113                 var viaNewDist:Number = Point.distance(p1, nP) + Point.distance(nP, p2);
114                         
115                 var proportion:Number = Math.abs(viaNewDist/directDist - 1);
116                 if ( proportion < closestProportion ) {
117                     newIndex = i+1;
118                     closestProportion = proportion;
119                     snapped = calculateSnappedPoint(p1, p2, nP);
120                 }
121             }
122             
123             // splice in new node
124             if ( isSnap ) {
125                 newNode.latp = snapped.y;
126                 newNode.lon = snapped.x;
127             }
128             insertNode(newIndex, newNode, performAction);
129             return newIndex;
130         }
131         
132         private function calculateSnappedPoint(p1:Point, p2:Point, nP:Point):Point {
133             var w:Number = p2.x - p1.x;
134             var h:Number = p2.y - p1.y;
135             var u:Number = ((nP.x-p1.x) * w + (nP.y-p1.y) * h) / (w*w + h*h);
136             return new Point(p1.x + u*w, p1.y+u*h);
137         }
138         
139         public override function toString():String {
140             return "Way("+id+"@"+version+"): "+getTagList()+
141                      " "+nodes.map(function(item:Node,index:int, arr:Array):String {return item.id.toString();}).join(",");
142         }
143
144                 public function isArea():Boolean {
145                         return (nodes[0].id==nodes[nodes.length-1].id && nodes.length>2);
146                 }
147                 
148                 public override function remove(performAction:Function):void {
149                         performAction(new DeleteWayAction(this, setDeletedState, nodes));
150                 }
151
152                 public function get clockwise():Boolean {
153                         var lowest:uint=0;
154                         var xmin:Number=-999999; var ymin:Number=-999999;
155                         for (var i:uint=0; i<nodes.length; i++) {
156                                 if      (nodes[i].latp> ymin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
157                                 else if (nodes[i].latp==ymin
158                                           && nodes[i].lon > xmin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
159                         }
160                         return (this.onLeft(lowest)>0);
161                 }
162                 
163                 private function onLeft(j:uint):Number {
164                         var left:Number=0;
165                         var i:int, k:int;
166                         if (nodes.length>=3) {
167                                 i=j-1; if (i==-1) { i=nodes.length-2; }
168                                 k=j+1; if (k==nodes.length) { k=1; }
169                                 left=((nodes[j].lon-nodes[i].lon) * (nodes[k].latp-nodes[i].latp) -
170                                           (nodes[k].lon-nodes[i].lon) * (nodes[j].latp-nodes[i].latp));
171                         }
172                         return left;
173                 }
174
175                 internal override function isEmpty():Boolean {
176                         return (deleted || (nodes.length==0));
177                 }
178
179                 public override function getType():String {
180                         return 'way';
181                 }
182     }
183
184 }