source: proiecte/ptvs/src/vnsim/map/object/PeanoKey.java @ 31

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 5.0 KB
Line 
1/************************************************************************************
2 * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University
3 * All rights reserved.
4 * Refer to LICENSE for terms and conditions of use.
5 ***********************************************************************************/
6package vnsim.map.object;
7
8/**
9 * @author Victor-Radu
10*/
11
12import java.util.ArrayList;
13
14import vnsim.applications.trafficview.SimulatedCarInfo;
15import vnsim.core.Engine;
16import vnsim.map.utils.GPSutil;
17
18
19
20
21
22public class PeanoKey implements Comparable<PeanoKey>, java.io.Serializable {
23       
24        /** <code>serialVersionUID</code> */
25        private static final long serialVersionUID = -5883719321862303634L;
26       
27        protected byte[] value = new byte[19];
28        protected int roadIndex;
29        protected int pointIndex;
30       
31        //added to optimize the search of nearby cars
32        protected transient ArrayList<SimulatedCarInfo> cars = null;
33        /**
34         * @param value
35         * @param roadIndex
36         * @param pointIndex
37         */
38        public PeanoKey(Point p){
39                int i;
40                byte[] latitude = parseCoord(p.latitude, 9);
41                byte[] longitude = parseCoord(p.longitude, 10);
42                for (i = 0; i < 9; i++) {
43                    this.value[2 * i] = longitude[i];
44                    this.value[2 * i + 1] = latitude[i];
45                }
46                this.value[2 * i] = longitude[i];
47               
48        }
49       
50        public PeanoKey(Point p, int roadIndex, int pointIndex) {
51        //computes a peano key from a point, the value array is obtained
52        //interleaving the point's latitude and longitude digits
53        //19, 10 (longitude), 9 (latitude)
54                byte[] latitude = parseCoord(p.latitude, 9);
55                byte[] longitude = parseCoord(p.longitude, 10);
56                //System.out.println(new String(longitude));
57                int i;
58                this.roadIndex = roadIndex;
59                this.pointIndex = pointIndex;
60                for (i = 0; i < 9; i++) {
61                    this.value[2 * i] = longitude[i];
62                    this.value[2 * i + 1] = latitude[i];
63                }
64                this.value[2 * i] = longitude[i];
65                 
66        }
67       
68        public int compareTo(PeanoKey pk) {
69                int i = 0;
70                while (i < value.length && this.value[i] == pk.value[i])
71                        i++;
72                if (i < value.length)
73                                return this.value[i] - pk.value[i];
74                else
75                        return 0;
76        }
77
78        // Used for searching the sorted peano keys vector, for cars in a certain range
79        // See the way the vector is sorted by interleaving digits
80        public void setToUpperCorner(PeanoKey reference)
81        {
82                boolean trig = false;
83                for (int i = 0; i < value.length; i++){
84                        if (trig){
85                                value[i] = 9;
86                        }else
87                                if (value[i] != reference.value[i]){
88                                        trig = true;
89                                }
90                }
91        }
92        public void setToLowerCorner(PeanoKey reference)
93        {
94                boolean trig = false;
95                for (int i = 0; i < value.length; i++)
96                {
97                       
98                        if (trig)
99                        {
100                                value[i] = -9;
101                        }
102                        else
103                                if (value[i] != reference.value[i])
104                                {
105                                        trig = true;
106                                }
107                }
108        }
109       
110        public String toString() 
111        {
112                StringBuffer sb = new StringBuffer();
113                sb.append("[");
114                sb.append((char)this.value[0]);
115                sb.append((char)this.value[1]);
116                for (int i = 2; i < value.length; i++)
117                        sb.append((char)(Math.abs(this.value[i])+'0'));
118//              sb.append(" - ");
119//              sb.append(this.roadIndex);
120//              sb.append(", ");
121//              sb.append(this.pointIndex);
122                sb.append("]");
123               
124                return sb.toString();
125        }
126       
127        private byte[] parseCoord(double coord, int digitno)
128        {
129        //digitno can be 10 for longitude or 9 for latitude     
130                byte[] result = new byte[digitno];
131                int coef = 0;
132                if (coord>0){
133                        result[0] = '+';
134                        coef = 1;
135                }else{
136                        result[0] = '-';
137                        coord = - coord;
138                        coef = -1;
139                }
140                int c = (int)(coord * 1e6);
141                for (int i=digitno-1; i>0; i--){
142                        result[i] = (byte)((c%10)*coef);
143                        c /= 10;
144                }
145                       
146                return result;
147        }
148       
149       
150        public int getPointIndex() 
151        {
152                return pointIndex;
153        }
154        public void setPointIndex(int pointIndex) 
155        {
156                this.pointIndex = pointIndex;
157        }
158        public int getRoadIndex() {
159                return roadIndex;
160        }
161        public void setRoadIndex(int roadIndex) {
162                this.roadIndex = roadIndex;
163        }
164        public byte[] getValue() {
165                return value;
166        }
167        public void setValue(byte[] value) {
168                this.value = value;
169        }
170
171       
172        PeanoKey maxpk = null; 
173        PeanoKey minpk = null; 
174       
175        public void addCar(SimulatedCarInfo car) 
176        {
177                if (cars == null)
178                {
179                        cars = new ArrayList<SimulatedCarInfo>();
180                }
181                if (maxpk == null || minpk == null)
182                {
183                        Road r = Globals.map.roads.get(roadIndex);
184                        Point p = r.points.get(pointIndex);
185                        maxpk = GPSutil.getMaxSearchBoundPK(p, 2 * Engine.WIRELESS_RANGE);
186                        minpk = GPSutil.getMaxSearchBoundPK(p,- 2 * Engine.WIRELESS_RANGE);
187                }
188                cars.add(car);
189        }
190
191        public void removeCar(SimulatedCarInfo car) 
192        {
193                if (cars == null)
194                        return;
195                cars.remove(car);
196                if (cars.size() == 0)
197                        cars = null;
198        }
199
200        public ArrayList<SimulatedCarInfo> getCars() {
201                return cars;
202        }
203
204        public PeanoKey getMaxpk() {
205                return maxpk;
206        }
207
208        public PeanoKey getMinpk() {
209                return minpk;
210        }
211
212        public void setMaxpk(PeanoKey maxpk) {
213                this.maxpk = maxpk;
214        }
215
216        public void setMinpk(PeanoKey minpk) {
217                this.minpk = minpk;
218        }
219       
220       
221       
222}       
223       
Note: See TracBrowser for help on using the repository browser.