fix bug with ways not being redrawn on panning back; and be a bit less silly about...
[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                 private var edge_l:Number;
9                 private var edge_r:Number;
10                 private var edge_t:Number;
11                 private var edge_b:Number;
12                 public static var entity_type:String = 'way';
13
14         public function Way(id:Number, version:uint, tags:Object, loaded:Boolean, nodes:Array, uid:Number = NaN, timestamp:String = null) {
15             super(id, version, tags, loaded, uid, timestamp);
16             this.nodes = nodes;
17                         for each (var node:Node in nodes) { node.addParent(this); }
18                         calculateBbox();
19         }
20
21                 public function update(version:uint, tags:Object, loaded:Boolean, nodes:Array, uid:Number = NaN, timestamp:String = null):void {
22                         var node:Node;
23                         for each (node in this.nodes) { node.removeParent(this); }
24                         updateEntityProperties(version,tags,loaded,uid,timestamp); this.nodes=nodes;
25                         for each (node in nodes) { node.addParent(this); }
26                         calculateBbox();
27                 }
28                 
29         public function get length():uint {
30             return nodes.length;
31         }
32
33                 private function calculateBbox():void {
34                         edge_l=999999; edge_r=-999999;
35                         edge_b=999999; edge_t=-999999;
36                         for each (var node:Node in nodes) { expandBbox(node); }
37                 }
38
39                 public function expandBbox(node:Node):void {
40                         edge_l=Math.min(edge_l,node.lon);
41                         edge_r=Math.max(edge_r,node.lon);
42                         edge_b=Math.min(edge_b,node.lat);
43                         edge_t=Math.max(edge_t,node.lat);
44                 }
45                 
46                 public override function within(left:Number,right:Number,top:Number,bottom:Number):Boolean {
47                         if (!edge_l ||
48                                 (edge_l<left   && edge_r<left  ) ||
49                             (edge_l>right  && edge_r>right ) ||
50                             (edge_b<bottom && edge_t<bottom) ||
51                             (edge_b>top    && edge_b>top   ) || deleted) { return false; }
52                         return true;
53                 }
54         
55         public function getNode(index:uint):Node {
56             return nodes[index];
57         }
58
59                 public function getLastNode():Node {
60                         return nodes[nodes.length-1];
61                 }
62
63         public function insertNode(index:uint, node:Node, performAction:Function):void {
64                         performAction(new AddNodeToWayAction(this, node, nodes, index));
65         }
66
67         public function appendNode(node:Node, performAction:Function):uint {
68                         performAction(new AddNodeToWayAction(this, node, nodes, -1));
69             return nodes.length + 1;
70         }
71         
72         public function prependNode(node:Node, performAction:Function):uint {
73                         performAction(new AddNodeToWayAction(this, node, nodes, 0));
74             return nodes.length + 1;
75         }
76         
77         // return the index of the Node, or -1 if not found
78         public function indexOfNode(node:Node):int {
79             return nodes.indexOf(node);
80         }
81
82                 public function hasOnceOnly(node:Node):Boolean {
83                         return nodes.indexOf(node)==nodes.lastIndexOf(node);
84                 }
85                 
86                 public function hasLockedNodes():Boolean {
87                         for each (var node:Node in nodes) {
88                                 if (node.locked) { return true; }
89                         }
90                         return false;
91                 }
92
93                 public function removeNode(node:Node, performAction:Function):void {
94                         performAction(new RemoveNodeFromWayAction(this, node, nodes));
95                 }
96
97         public function removeNodeByIndex(index:uint, performAction:Function, fireEvent:Boolean=true):void {
98             performAction(new RemoveNodeByIndexAction(this, nodes, index, fireEvent));
99         }
100
101                 public function sliceNodes(start:int,end:int):Array {
102                         return nodes.slice(start,end);
103                 }
104
105         public function deleteNodesFrom(start:int, performAction:Function):void {
106             for (var i:int=nodes.length-1; i>=start; i--) {
107               performAction(new RemoveNodeByIndexAction(this, nodes, i));
108             }
109             markDirty();
110         }
111
112                 public function mergeWith(way:Way,topos:int,frompos:int, performAction:Function):void {
113                         performAction(new MergeWaysAction(this, way, topos, frompos));
114                 }
115                 
116                 public function addToEnd(topos:int,node:Node, performAction:Function):void {
117                         if (topos==0) {
118                                 if (nodes[0]==node) { return; }
119                                 prependNode(node, performAction);
120                         } else {
121                                 if (nodes[nodes.length-1]==node) { return; }
122                                 appendNode(node, performAction);
123                         }
124                 }
125
126         public function reverseNodes(performAction:Function):void {
127             performAction(new ReverseNodesAction(this, nodes));
128         }
129
130         /**
131          * Finds the 1st way segment which intersects the projected
132          * coordinate and adds the node to that segment. If snap is
133          * specified then the node is moved to exactly bisect the
134          * segment.
135          */
136         public function insertNodeAtClosestPosition(newNode:Node, isSnap:Boolean, performAction:Function):int {
137             var closestProportion:Number = 1;
138             var newIndex:uint = 0;
139             var nP:Point = new Point(newNode.lon, newNode.latp);
140             var snapped:Point = null;
141             
142             for ( var i:uint; i < length - 1; i++ ) {
143                 var node1:Node = getNode(i);
144                 var node2:Node = getNode(i+1);
145                 var p1:Point = new Point(node1.lon, node1.latp);
146                 var p2:Point = new Point(node2.lon, node2.latp);
147                 
148                 var directDist:Number = Point.distance(p1, p2);
149                 var viaNewDist:Number = Point.distance(p1, nP) + Point.distance(nP, p2);
150                         
151                 var proportion:Number = Math.abs(viaNewDist/directDist - 1);
152                 if ( proportion < closestProportion ) {
153                     newIndex = i+1;
154                     closestProportion = proportion;
155                     snapped = calculateSnappedPoint(p1, p2, nP);
156                 }
157             }
158             
159             // splice in new node
160             if ( isSnap ) {
161                 newNode.setLonLatp(snapped.x, snapped.y, performAction);
162             }
163             insertNode(newIndex, newNode, performAction);
164             return newIndex;
165         }
166         
167         private function calculateSnappedPoint(p1:Point, p2:Point, nP:Point):Point {
168             var w:Number = p2.x - p1.x;
169             var h:Number = p2.y - p1.y;
170             var u:Number = ((nP.x-p1.x) * w + (nP.y-p1.y) * h) / (w*w + h*h);
171             return new Point(p1.x + u*w, p1.y+u*h);
172         }
173         
174         public override function toString():String {
175             return "Way("+id+"@"+version+"): "+getTagList()+
176                      " "+nodes.map(function(item:Node,index:int, arr:Array):String {return item.id.toString();}).join(",");
177         }
178
179                 public function isArea():Boolean {
180                         if (nodes.length==0) { return false; }
181                         return (nodes[0].id==nodes[nodes.length-1].id && nodes.length>2);
182                 }
183                 
184                 public function endsWith(node:Node):Boolean {
185                         return (nodes[0]==node || nodes[nodes.length-1]==node);
186                 }
187                 
188                 public override function remove(performAction:Function):void {
189                         performAction(new DeleteWayAction(this, setDeletedState, nodes));
190                 }
191
192                 public override function nullify():void {
193                         nullifyEntity();
194                         nodes=[];
195                         edge_l=edge_r=edge_t=edge_b=NaN;
196                 }
197                 
198                 public function get clockwise():Boolean {
199                         var lowest:uint=0;
200                         var xmin:Number=-999999; var ymin:Number=-999999;
201                         for (var i:uint=0; i<nodes.length; i++) {
202                                 if      (nodes[i].latp> ymin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
203                                 else if (nodes[i].latp==ymin
204                                           && nodes[i].lon > xmin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
205                         }
206                         return (this.onLeft(lowest)>0);
207                 }
208                 
209                 private function onLeft(j:uint):Number {
210                         var left:Number=0;
211                         var i:int, k:int;
212                         if (nodes.length>=3) {
213                                 i=j-1; if (i==-1) { i=nodes.length-2; }
214                                 k=j+1; if (k==nodes.length) { k=1; }
215                                 left=((nodes[j].lon-nodes[i].lon) * (nodes[k].latp-nodes[i].latp) -
216                                           (nodes[k].lon-nodes[i].lon) * (nodes[j].latp-nodes[i].latp));
217                         }
218                         return left;
219                 }
220
221                 internal override function isEmpty():Boolean {
222                         return (deleted || (nodes.length==0));
223                 }
224
225                 public override function getType():String {
226                         return 'way';
227                 }
228     }
229
230 }