Merge https://github.com/openstreetmap/potlatch2
[potlatch2.git] / net / systemeD / halcyon / connection / Way.as
1 package net.systemeD.halcyon.connection {
2     import flash.geom.Point;
3     
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(connection:Connection, id:Number, version:uint, tags:Object, loaded:Boolean, nodes:Array, uid:Number = NaN, timestamp:String = null) {
15             super(connection, 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, parentsLoaded: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,parentsLoaded,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 getFirstNode():Node {
60             return nodes[0];
61         }
62
63                 public function getLastNode():Node {
64                         return nodes[nodes.length-1];
65                 }
66                 
67                 /** Given one node, return the next in sequence, cycling around a loop if necessary. */
68                 // TODO make behave correctly for P-shaped topologies?
69                 public function getNextNode(node:Node):Node {
70                         // If the last node in a loop is selected, this behaves correctly.
71                     var i:uint = indexOfNode(node);
72                     if(i < length-1)
73                     return nodes[i+1];
74                 return null;
75                 // What should happen for very short lengths?      
76                 }
77         
78         // TODO make behave correctly for P-shaped topologies?
79         /** Given one node, return the previous, cycling around a loop if necessary. */
80         public function getPrevNode(node:Node):Node {
81             var i:uint = indexOfNode(node);
82             if(i > 0)
83                 return nodes[i-1];
84             if(i == 0 && isArea() )
85                 return nodes[nodes.length - 2]
86             return null;
87             // What should happen for very short lengths?      
88         }
89
90         public function insertNode(index:uint, node:Node, performAction:Function):void {
91                         if (index>0 && getNode(index-1)==node) return;
92                         if (index<nodes.length-1 && getNode(index)==node) return;
93                         performAction(new AddNodeToWayAction(this, node, nodes, index));
94         }
95
96         public function appendNode(node:Node, performAction:Function):uint {
97                         if (node!=getLastNode()) performAction(new AddNodeToWayAction(this, node, nodes, -1));
98             return nodes.length + 1;
99         }
100         
101         public function prependNode(node:Node, performAction:Function):uint {
102                         if (node!=getFirstNode()) performAction(new AddNodeToWayAction(this, node, nodes, 0));
103             return nodes.length + 1;
104         }
105         
106         // return the index of the Node, or -1 if not found
107         public function indexOfNode(node:Node):int {
108             return nodes.indexOf(node);
109         }
110
111                 public function hasOnceOnly(node:Node):Boolean {
112                         return nodes.indexOf(node)==nodes.lastIndexOf(node);
113                 }
114                 
115                 public function hasLockedNodes():Boolean {
116                         for each (var node:Node in nodes) {
117                                 if (node.locked) { return true; }
118                         }
119                         return false;
120                 }
121
122                 public function removeNode(node:Node, performAction:Function):void {
123                         performAction(new RemoveNodeFromWayAction(this, node, nodes));
124                 }
125
126         public function removeNodeByIndex(index:uint, performAction:Function, fireEvent:Boolean=true):void {
127             performAction(new RemoveNodeByIndexAction(this, nodes, index, fireEvent));
128         }
129
130                 public function sliceNodes(start:int,end:int):Array {
131                         return nodes.slice(start,end);
132                 }
133
134         public function deleteNodesFrom(start:int, performAction:Function):void {
135             for (var i:int=nodes.length-1; i>=start; i--) {
136               performAction(new RemoveNodeByIndexAction(this, nodes, i));
137             }
138             markDirty();
139         }
140
141                 /** Merges another way into this one, removing the other one. */
142                 public function mergeWith(way:Way,topos:int,frompos:int, performAction:Function):void {
143                         performAction(new MergeWaysAction(this, way, topos, frompos));
144                 }
145                 
146                 public function addToEnd(topos:int,node:Node, performAction:Function):void {
147                         if (topos==0) {
148                                 if (nodes[0]==node) { return; }
149                                 prependNode(node, performAction);
150                         } else {
151                                 if (nodes[nodes.length-1]==node) { return; }
152                                 appendNode(node, performAction);
153                         }
154                 }
155
156         public function reverseNodes(performAction:Function):void {
157             performAction(new ReverseNodesAction(this, nodes));
158         }
159
160         /**
161          * Finds the 1st way segment which intersects the projected
162          * coordinate and adds the node to that segment. If snap is
163          * specified then the node is moved to exactly bisect the
164          * segment.
165          */
166         public function insertNodeAtClosestPosition(newNode:Node, isSnap:Boolean, performAction:Function):int {
167             var closestProportion:Number = 1;
168             var newIndex:uint = 0;
169             var nP:Point = new Point(newNode.lon, newNode.latp);
170             var snapped:Point = null;
171             
172             for ( var i:uint; i < length - 1; i++ ) {
173                 var node1:Node = getNode(i);
174                 var node2:Node = getNode(i+1);
175                 var p1:Point = new Point(node1.lon, node1.latp);
176                 var p2:Point = new Point(node2.lon, node2.latp);
177                 
178                 var directDist:Number = Point.distance(p1, p2);
179                 var viaNewDist:Number = Point.distance(p1, nP) + Point.distance(nP, p2);
180                         
181                 var proportion:Number = Math.abs(viaNewDist/directDist - 1);
182                 if ( proportion < closestProportion ) {
183                     newIndex = i+1;
184                     closestProportion = proportion;
185                     snapped = calculateSnappedPoint(p1, p2, nP);
186                 }
187             }
188             
189             // splice in new node
190             if ( isSnap ) {
191                 newNode.setLonLatp(snapped.x, snapped.y, performAction);
192             }
193             insertNode(newIndex, newNode, performAction);
194             return newIndex;
195         }
196         
197         private function calculateSnappedPoint(p1:Point, p2:Point, nP:Point):Point {
198             var w:Number = p2.x - p1.x;
199             var h:Number = p2.y - p1.y;
200             var u:Number = ((nP.x-p1.x) * w + (nP.y-p1.y) * h) / (w*w + h*h);
201             return new Point(p1.x + u*w, p1.y+u*h);
202         }
203         
204         public override function toString():String {
205             return "Way("+id+"@"+version+"): "+getTagList()+
206                      " "+nodes.map(function(item:Node,index:int, arr:Array):String {return item.id.toString();}).join(",");
207         }
208
209                 public function isArea():Boolean {
210                         if (nodes.length==0) { return false; }
211                         return (nodes[0].id==nodes[nodes.length-1].id && nodes.length>2);
212                 }
213                 
214                 public function endsWith(node:Node):Boolean {
215                         return (nodes[0]==node || nodes[nodes.length-1]==node);
216                 }
217                 
218                 public override function remove(performAction:Function):void {
219                         performAction(new DeleteWayAction(this, setDeletedState, nodes));
220                 }
221
222                 public override function nullify():void {
223                         nullifyEntity();
224                         nodes=[];
225                         edge_l=edge_r=edge_t=edge_b=NaN;
226                 }
227                 
228                 public function get clockwise():Boolean {
229                         var lowest:uint=0;
230                         var xmin:Number=-999999; var ymin:Number=-999999;
231                         for (var i:uint=0; i<nodes.length; i++) {
232                                 if      (nodes[i].latp> ymin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
233                                 else if (nodes[i].latp==ymin
234                                           && nodes[i].lon > xmin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
235                         }
236                         return (this.onLeft(lowest)>0);
237                 }
238                 
239                 private function onLeft(j:uint):Number {
240                         var left:Number=0;
241                         var i:int, k:int;
242                         if (nodes.length>=3) {
243                                 i=j-1; if (i==-1) { i=nodes.length-2; }
244                                 k=j+1; if (k==nodes.length) { k=1; }
245                                 left=((nodes[j].lon-nodes[i].lon) * (nodes[k].latp-nodes[i].latp) -
246                                           (nodes[k].lon-nodes[i].lon) * (nodes[j].latp-nodes[i].latp));
247                         }
248                         return left;
249                 }
250
251         public function get angle():Number {
252             var dx:Number = nodes[nodes.length-1].lon - nodes[0].lon;
253             var dy:Number = nodes[nodes.length-1].latp - nodes[0].latp;
254             if (dx != 0 || dy != 0) {
255                 return Math.atan2(dx,dy)*(180/Math.PI);
256             } else {
257                 return 0;
258             }
259         }
260
261                 internal override function isEmpty():Boolean {
262                         return (deleted || (nodes.length==0));
263                 }
264
265                 public override function getType():String {
266                         return 'way';
267                 }
268                 
269                 public override function isType(str:String):Boolean {
270                         if (str=='way') return true;
271                         if (str=='line' && !isArea()) return true;
272                         if (str=='area' &&  isArea()) return true;
273                         return false;
274                 }
275                 
276                 /** Whether the way has a loop that joins back midway along its length */
277                 public function isPShape():Boolean {
278                         return getFirstNode() != getLastNode() && (!hasOnceOnly(getFirstNode()) || !hasOnceOnly(getLastNode()) );
279                 }
280                 
281                 /** Given a P-shaped way, return the index of midway node that one end connects back to. */
282                 public function getPJunctionNodeIndex():uint {
283                         if (isPShape()) {
284                             if (hasOnceOnly(getFirstNode())) {
285                                 // nodes[0] is the free end
286                                 return nodes.indexOf(getLastNode());
287                             } else {
288                                 // nodes[0] is in the loop
289                                 return nodes.lastIndexOf(getFirstNode());
290                             }
291                         }
292                         return null;
293                 }
294
295                 public function intersects(left:Number,right:Number,top:Number,bottom:Number):Boolean {
296                         // simple test first: are any nodes contained?
297                         for (var i:uint=0; i<nodes.length; i++) {
298                                 if (nodes[i].within(left,right,top,bottom)) return true;
299                         }
300                         // more complex test: do any segments cross?
301                         for (i=0; i<nodes.length-1; i++) {
302                                 if (lineIntersectsRectangle(
303                                         nodes[i  ].lon, nodes[i  ].lat,
304                                         nodes[i+1].lon, nodes[i+1].lat,
305                                         left,right,top,bottom)) return true;
306                         }
307                         return false;
308                 }
309                 
310                 private function lineIntersectsRectangle(x0:Number, y0:Number, x1:Number, y1:Number, l:Number, r:Number, b:Number, t:Number):Boolean {
311                         // from http://sebleedelisle.com/2009/05/super-fast-trianglerectangle-intersection-test/
312                         // note that t and b are transposed above because we're dealing with lat (top=90), not AS3 pixels (top=0)
313                         var m:Number = (y1-y0) / (x1-x0);
314                         var c:Number = y0 -(m*x0);
315                         var top_intersection:Number, bottom_intersection:Number;
316                         var toptrianglepoint:Number, bottomtrianglepoint:Number;
317
318                         if (m>0) {
319                                 top_intersection = (m*l  + c);
320                                 bottom_intersection = (m*r  + c);
321                         } else {
322                                 top_intersection = (m*r  + c);
323                                 bottom_intersection = (m*l  + c);
324                         }
325
326                         if (y0<y1) {
327                                 toptrianglepoint = y0;
328                                 bottomtrianglepoint = y1;
329                         } else {
330                                 toptrianglepoint = y1;
331                                 bottomtrianglepoint = y0;
332                         }
333
334                         var topoverlap:Number = top_intersection>toptrianglepoint ? top_intersection : toptrianglepoint;
335                         var botoverlap:Number = bottom_intersection<bottomtrianglepoint ? bottom_intersection : bottomtrianglepoint;
336                         return (topoverlap<botoverlap) && (!((botoverlap<t) || (topoverlap>b)));
337                 }
338
339
340     }
341 }