Bugfix - indexOfNode should return int not uint
[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) {
15             super(id, version, tags, loaded);
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):void {
22                         var node:Node;
23                         for each (node in this.nodes) { node.removeParent(this); }
24                         updateEntityProperties(version,tags,loaded); 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 function intersectsBbox(left:Number,right:Number,top:Number,bottom:Number):Boolean {
47                         if ((edge_l<left   && edge_r<left  ) ||
48                             (edge_l>right  && edge_r>right ) ||
49                             (edge_b<bottom && edge_t<bottom) ||
50                             (edge_b>top    && edge_b>top   )) { return false; }
51                         return true;
52                 }
53         
54         public function getNode(index:uint):Node {
55             return nodes[index];
56         }
57
58                 public function getLastNode():Node {
59                         return nodes[nodes.length-1];
60                 }
61
62         public function insertNode(index:uint, node:Node, performAction:Function):void {
63                         performAction(new AddNodeToWayAction(this, node, nodes, index));
64         }
65
66         public function appendNode(node:Node, performAction:Function):uint {
67                         performAction(new AddNodeToWayAction(this, node, nodes, -1));
68             return nodes.length + 1;
69         }
70         
71         public function prependNode(node:Node, performAction:Function):uint {
72                         performAction(new AddNodeToWayAction(this, node, nodes, 0));
73             return nodes.length + 1;
74         }
75         
76         // return the index of the Node, or -1 if not found
77         public function indexOfNode(node:Node):int {
78             return nodes.indexOf(node);
79         }
80
81                 public function removeNode(node:Node, performAction:Function):void {
82                         performAction(new RemoveNodeFromWayAction(this, node, nodes));
83                 }
84
85         public function removeNodeByIndex(index:uint, performAction:Function, fireEvent:Boolean=true):void {
86             performAction(new RemoveNodeByIndexAction(this, nodes, index, fireEvent));
87         }
88
89                 public function sliceNodes(start:int,end:int):Array {
90                         return nodes.slice(start,end);
91                 }
92
93         public function deleteNodesFrom(start:int, performAction:Function):void {
94             for (var i:int=nodes.length-1; i>=start; i--) {
95               performAction(new RemoveNodeByIndexAction(this, nodes, i));
96             }
97             markDirty();
98         }
99
100                 public function mergeWith(way:Way,topos:int,frompos:int, performAction:Function):void {
101                         performAction(new MergeWaysAction(this, way, topos, frompos));
102                 }
103                 
104                 public function addToEnd(topos:int,node:Node, performAction:Function):void {
105                         if (topos==0) {
106                                 if (nodes[0]==node) { return; }
107                                 prependNode(node, performAction);
108                         } else {
109                                 if (nodes[nodes.length-1]==node) { return; }
110                                 appendNode(node, performAction);
111                         }
112                 }
113
114         public function reverseNodes(performAction:Function):void {
115             performAction(new ReverseNodesAction(this, nodes));
116         }
117
118         /**
119          * Finds the 1st way segment which intersects the projected
120          * coordinate and adds the node to that segment. If snap is
121          * specified then the node is moved to exactly bisect the
122          * segment.
123          */
124         public function insertNodeAtClosestPosition(newNode:Node, isSnap:Boolean, performAction:Function):int {
125             var closestProportion:Number = 1;
126             var newIndex:uint = 0;
127             var nP:Point = new Point(newNode.lon, newNode.latp);
128             var snapped:Point = null;
129             
130             for ( var i:uint; i < length - 1; i++ ) {
131                 var node1:Node = getNode(i);
132                 var node2:Node = getNode(i+1);
133                 var p1:Point = new Point(node1.lon, node1.latp);
134                 var p2:Point = new Point(node2.lon, node2.latp);
135                 
136                 var directDist:Number = Point.distance(p1, p2);
137                 var viaNewDist:Number = Point.distance(p1, nP) + Point.distance(nP, p2);
138                         
139                 var proportion:Number = Math.abs(viaNewDist/directDist - 1);
140                 if ( proportion < closestProportion ) {
141                     newIndex = i+1;
142                     closestProportion = proportion;
143                     snapped = calculateSnappedPoint(p1, p2, nP);
144                 }
145             }
146             
147             // splice in new node
148             if ( isSnap ) {
149                 newNode.latp = snapped.y;
150                 newNode.lon = snapped.x;
151             }
152             insertNode(newIndex, newNode, performAction);
153             return newIndex;
154         }
155         
156         private function calculateSnappedPoint(p1:Point, p2:Point, nP:Point):Point {
157             var w:Number = p2.x - p1.x;
158             var h:Number = p2.y - p1.y;
159             var u:Number = ((nP.x-p1.x) * w + (nP.y-p1.y) * h) / (w*w + h*h);
160             return new Point(p1.x + u*w, p1.y+u*h);
161         }
162         
163         public override function toString():String {
164             return "Way("+id+"@"+version+"): "+getTagList()+
165                      " "+nodes.map(function(item:Node,index:int, arr:Array):String {return item.id.toString();}).join(",");
166         }
167
168                 public function isArea():Boolean {
169                         if (nodes.length==0) { return false; }
170                         return (nodes[0].id==nodes[nodes.length-1].id && nodes.length>2);
171                 }
172                 
173                 public override function remove(performAction:Function):void {
174                         performAction(new DeleteWayAction(this, setDeletedState, nodes));
175                 }
176
177                 public function get clockwise():Boolean {
178                         var lowest:uint=0;
179                         var xmin:Number=-999999; var ymin:Number=-999999;
180                         for (var i:uint=0; i<nodes.length; i++) {
181                                 if      (nodes[i].latp> ymin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
182                                 else if (nodes[i].latp==ymin
183                                           && nodes[i].lon > xmin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
184                         }
185                         return (this.onLeft(lowest)>0);
186                 }
187                 
188                 private function onLeft(j:uint):Number {
189                         var left:Number=0;
190                         var i:int, k:int;
191                         if (nodes.length>=3) {
192                                 i=j-1; if (i==-1) { i=nodes.length-2; }
193                                 k=j+1; if (k==nodes.length) { k=1; }
194                                 left=((nodes[j].lon-nodes[i].lon) * (nodes[k].latp-nodes[i].latp) -
195                                           (nodes[k].lon-nodes[i].lon) * (nodes[j].latp-nodes[i].latp));
196                         }
197                         return left;
198                 }
199
200                 internal override function isEmpty():Boolean {
201                         return (deleted || (nodes.length==0));
202                 }
203
204                 public override function getType():String {
205                         return 'way';
206                 }
207     }
208
209 }