source: proiecte/ptvs/src/vnsim/vehicular/routePlan/selfRouted/RoadAreaUtils.java @ 31

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 16.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.vehicular.routePlan.selfRouted;
7
8
9import java.util.ArrayList;
10import java.io.Serializable;
11
12import vnsim.map.object.Cross;
13import vnsim.map.object.Globals;
14import vnsim.map.object.Point;
15import vnsim.map.object.Road;
16import vnsim.map.utils.GPSutil;
17
18public class RoadAreaUtils implements Serializable {
19
20        /** <code>serialVersionUID</code> */
21        private static final long serialVersionUID = -5883719321862303634L;
22       
23        public ArrayList<Integer> mainRoads; // indicii drumurilor principale
24
25        // din Globals.map.roads
26
27        public ArrayList<MajorRoadArea> distinctAreas;
28
29        public RoadAreaUtils() {
30                mainRoads = new ArrayList<Integer>();
31                distinctAreas = new ArrayList<MajorRoadArea>();
32        }
33
34        public void splitRoads() {
35                int i, j, k;
36                Road r;
37                Point p;
38                for (i = 0; i < Globals.map.roads.size(); i++) {
39                        r = Globals.map.roads.get(i);
40                        //if (((r.getRoadinfo())[1] == 49) || ((r.getRoadinfo())[1] == 50)) {
41                        //      if (((r.getRoadinfo())[1] >= 49) || ((r.getRoadinfo())[1] <= 51)) {
42                        if (((r.getRoadinfo())[1] == 49)) {
43                                mainRoads.add(new Integer(i));
44                        }
45                }
46                int dim = mainRoads.size();
47                System.out.println("I have: " + dim + " primary roads");
48                if (dim > 0) {
49                        for (i = 0; i < Globals.map.roads.size(); i++) {
50                                r = Globals.map.roads.get(i);
51                                for (j = 0; j < r.points.size(); j++) {
52                                        p = r.points.get(j);
53                                        p.areaCode = new AreaCode(dim);
54                                        for (k = 0; k < dim; k++) {
55                                                p.areaCode.code[k] = -1;
56                                        }
57
58                                }
59                        }
60
61                        for (i = 0; i < mainRoads.size(); i++) {
62                                r = Globals.map.roads.get(mainRoads.get(i).intValue());
63                                for (j = 0; j < r.points.size(); j++) {
64                                        p = r.points.get(j);
65                                        p.areaCode.code[i] = 2;
66
67                                }
68                        }
69
70                }
71
72        }
73
74        public boolean inDirection(float xM1, float yM1, float xM2, float yM2,
75                        float xm1, float ym1, float xm2, float ym2) {
76                float cosM, cosm, sinM, sinm;
77                float dxM, dxm, dyM, dym;
78
79                double angleM = 0.0, anglem = 0.0;
80
81                dxM = xM2 - xM1;
82                dxm = xm2 - xm1;
83                dyM = yM2 - yM1;
84                dym = ym2 - ym1;
85                cosM = (dxM / ((float) (Math.sqrt((double) (dxM * dxM + dyM * dyM)))));
86                cosm = (dxm / ((float) (Math.sqrt((double) (dxm * dxm + dym * dym)))));
87                sinM = (dyM / ((float) (Math.sqrt((double) (dxM * dxM + dyM * dyM)))));
88                sinm = (dym / ((float) (Math.sqrt((double) (dxm * dxm + dym * dym)))));
89
90                // System.out.println(" "+(Math.sqrt((double) (xM1 * xM2 + yM1 * yM2)))+
91                // "..."+(Math.sqrt((double) (xm1 * xm2 + ym1 * ym2))));
92
93                if (cosM > 1.0f) {
94                        cosM = 1.0f;
95                }
96                if (cosm > 1.0f) {
97                        cosm = 1.0f;
98                }
99                if (cosM < -1.0f) {
100                        cosM = -1.0f;
101                }
102                if (cosm < -1.0f) {
103                        cosm = -1.0f;
104                }
105                if (sinM > 1.0f) {
106                        sinM = 1.0f;
107                }
108                if (sinm > 1.0f) {
109                        sinm = 1.0f;
110                }
111                if (sinM < -1.0f) {
112                        sinM = -1.0f;
113                }
114                if (sinm < -1.0f) {
115                        sinm = -1.0f;
116                }
117
118                // //First angle
119                if (sinM == 0) {
120                        if (cosM > 0) {
121                                angleM = 0;
122                        } else {
123                                angleM = Math.PI;
124                        }
125
126                }
127                if (cosM == 0) {
128                        if (sinM > 0) {
129                                angleM = Math.PI / 2;
130                        } else {
131                                angleM = 0 - Math.PI / 2;
132                        }
133                }
134
135                if (sinM > 0 && cosM > 0) {
136                        angleM = Math.acos(cosM);
137
138                }
139                if (sinM < 0 && cosM < 0) {
140                        angleM = Math.acos(cosM);
141                        angleM = (Math.PI - angleM) + Math.PI;
142
143                }
144                if (sinM < 0 && cosM > 0) {
145                        angleM = Math.acos(cosM);
146
147                        angleM = Math.PI + Math.PI - angleM;
148
149                }
150
151                if (sinM > 0 && cosM < 0) {
152                        angleM = Math.acos(cosM);
153
154                }
155                // Second angle
156                if (sinm == 0) {
157                        if (cosm > 0) {
158                                anglem = 0;
159                        } else {
160                                anglem = Math.PI;
161                        }
162
163                }
164                if (cosm == 0) {
165                        if (sinm > 0) {
166                                anglem = Math.PI / 2;
167                        } else {
168                                anglem = 0 - Math.PI / 2;
169                        }
170                }
171
172                if (sinm > 0 && cosm > 0) {
173                        anglem = Math.acos(cosm);
174
175                }
176                if (sinm < 0 && cosm < 0) {
177                        anglem = Math.acos(cosm);
178                        anglem = (Math.PI - anglem) + Math.PI;
179
180                }
181                if (sinm < 0 && cosm > 0) {
182                        anglem = Math.acos(cosm);
183
184                        anglem = Math.PI + Math.PI - anglem;
185
186                }
187
188                if (sinm > 0 && cosm < 0) {
189                        anglem = Math.acos(cosm);
190
191                }
192
193                // System.out.println("Unghi MARE=" + angleM + " si in grade"
194                // + ((angleM * 180) / Math.PI));
195                // System.out.println("Unghi mic=" + anglem + " si in grade"
196                // + ((anglem * 180) / Math.PI));
197                if (Math.abs(angleM - anglem) >= Math.PI) {
198                        if (anglem > angleM) {
199                                // on the left side
200                                return false;
201                        } else {
202                                // on the right side
203                                return true;
204                        }
205                } else {
206
207                        if (anglem > angleM) {
208                                // on the right side
209                                return true;
210                        } else {
211                                // on the left side
212                                return false;
213                        }
214                }
215
216        }
217        ArrayList<Mark> currentMarks ;
218        ArrayList<Mark> nextMarks ;
219        public void splitMap() {
220                splitRoads();
221                currentMarks = new ArrayList<Mark>();
222                nextMarks = new ArrayList<Mark>();
223                int i, ii, j, k, l, mainRoadIdx;
224                Road r, crossedRoad;
225                Point p, cp;
226                Cross c;
227                java.awt.geom.Point2D.Float main1, main2, second1, second2;
228                int idxM1, idxM2, idxS1, idxS2;
229                Mark m;
230
231                for (ii = 0; ii < mainRoads.size(); ii++) {
232                        mainRoadIdx = mainRoads.get(ii).intValue();
233                        r = Globals.map.roads.get(mainRoadIdx);
234
235                        for (j = 0; j < r.crosses.size(); j++) {
236                                c = r.crosses.get(j);
237                                crossedRoad = Globals.map.roads.get(c.getCrossRoadIndex());
238                                main1 = new java.awt.geom.Point2D.Float();
239                                main2 = new java.awt.geom.Point2D.Float();
240                                second1 = new java.awt.geom.Point2D.Float();
241                                second2 = new java.awt.geom.Point2D.Float();
242
243                                if (c.getPointIndex() == 0) {
244                                        idxM1 = 0;
245                                        idxM2 = 1;
246
247                                } else {
248                                        if (c.getPointIndex() == r.points.size() - 1) {
249                                                idxM1 = r.points.size() - 2;
250                                                idxM2 = r.points.size() - 1;
251                                        } else {
252                                                idxM1 = c.getPointIndex() - 1;
253                                                idxM2 = c.getPointIndex() + 1;
254                                        }
255                                }
256                                if (c.getCrossPointIndex() == 0) {
257                                        idxS1 = 0;
258                                        idxS2 = 1;
259
260                                } else {
261                                        if (c.getCrossPointIndex() == crossedRoad.points.size() - 1) {
262                                                idxS1 = crossedRoad.points.size() - 2;
263                                                idxS2 = crossedRoad.points.size() - 1;
264
265                                        } else {
266
267                                                idxS1 = c.getCrossPointIndex() - 1;
268                                                idxS2 = c.getCrossPointIndex() + 1;
269
270                                        }
271                                }
272                                main1.y = (float) GPSutil.getMetersLatitude(
273                                                Globals.map.minPoint, r.points.get(idxM1));
274                                main1.x = (float) GPSutil.getMetersLongitude(
275                                                Globals.map.minPoint, r.points.get(idxM1));
276
277                                main2.y = (float) GPSutil.getMetersLatitude(
278                                                Globals.map.minPoint, r.points.get(idxM2));
279                                main2.x = (float) GPSutil.getMetersLongitude(
280                                                Globals.map.minPoint, r.points.get(idxM2));
281
282                                second1.y = (float) GPSutil.getMetersLatitude(
283                                                Globals.map.minPoint, crossedRoad.points.get(idxS1));
284                                second1.x = (float) GPSutil.getMetersLongitude(
285                                                Globals.map.minPoint, crossedRoad.points.get(idxS1));
286
287                                second2.y = (float) GPSutil.getMetersLatitude(
288                                                Globals.map.minPoint, crossedRoad.points.get(idxS2));
289                                second2.x = (float) GPSutil.getMetersLongitude(
290                                                Globals.map.minPoint, crossedRoad.points.get(idxS2));
291
292                                crossedRoad.points.get(c.getCrossPointIndex()).areaCode.code[ii] = 2;
293
294                                if (inDirection(main1.x, main1.y, main2.x, main2.y, second1.x,
295                                                second1.y, second2.x, second2.y)) {
296                                        nextMarks
297                                                        .add(new Mark(ii, 0, c.getCrossRoadIndex(), idxS1));
298                                        nextMarks
299                                                        .add(new Mark(ii, 1, c.getCrossRoadIndex(), idxS2));
300                                } else {
301                                        nextMarks
302                                                        .add(new Mark(ii, 1, c.getCrossRoadIndex(), idxS1));
303                                        nextMarks
304                                                        .add(new Mark(ii, 0, c.getCrossRoadIndex(), idxS2));
305
306                                }
307
308                        }
309                        // ///////////////////////////////////////////////////////////////////////////
310                        while (nextMarks.size() != 0) {
311                                currentMarks = nextMarks;
312                                nextMarks = new ArrayList<Mark>();
313
314                                for (i = 0; i < currentMarks.size(); i++) {
315                                        m = currentMarks.get(i);
316                                        if (m.pos >= mainRoads.size()) {
317                                                System.out
318                                                                .println("Incorect position for area code!To many major roads!");
319                                                continue;
320                                        }
321                                        if (m.pos < 0) {
322                                                System.out
323                                                                .println("Incorect position for area code!Negative value!");
324                                                continue;
325                                        }
326                                        r = Globals.map.roads.get(m.roadId);
327                                        p = r.points.get(m.pointId);
328                                        if (p.areaCode.code[m.pos] != -1) {
329                                                continue;
330                                        } else {
331                                                p.areaCode.code[m.pos] = (byte) m.val;
332                                                if (m.pointId == 0) {
333                                                        nextMarks.add(new Mark(m.pos, m.val, m.roadId, 1));
334                                                } else {
335                                                        if (m.pointId == r.points.size() - 1) {
336                                                                nextMarks.add(new Mark(m.pos, m.val, m.roadId,
337                                                                                m.pointId - 1));
338                                                        } else {
339                                                                nextMarks.add(new Mark(m.pos, m.val, m.roadId,
340                                                                                m.pointId - 1));
341                                                                nextMarks.add(new Mark(m.pos, m.val, m.roadId,
342                                                                                m.pointId + 1));
343
344                                                        }
345                                                }
346                                                for (j = 0; j < r.crosses.size(); j++) {
347                                                        c = r.crosses.get(j);
348                                                        cp = Globals.map.roads.get(c.getCrossRoadIndex()).points
349                                                                        .get(c.getCrossPointIndex());
350                                                        if (cp.equals(p)) {
351                                                                nextMarks.add(new Mark(m.pos, m.val, c
352                                                                                .getCrossRoadIndex(), c
353                                                                                .getCrossPointIndex()));
354                                                        }
355                                                }
356
357                                        }
358                                }
359                        }
360                        // ///////////////////////////////////////////////////////////////////////////
361                }
362
363        }
364
365        public void selectDistinctRoadAreas() {
366
367                int i, j, k;
368                Road r;
369                Point p;
370                boolean found = false;
371                for (i = 0; i < Globals.map.roads.size(); i++) {
372                        r = Globals.map.roads.get(i);
373                        for (j = 0; j < r.points.size(); j++) {
374
375                                p = r.points.get(j);
376                                //                              if(p.areaCode.code==null){
377                                //                                      continue;
378                                //                              }
379                                if (distinctAreas.size() != 0) {
380                                        // if (!isOnBorder(p.areaCode.code)) {
381                                        found = false;
382                                        for (k = 0; k < distinctAreas.size(); k++) {
383                                                if (distinctAreas.get(k).areaCodeString
384                                                                .equals(p.areaCode.areaCodeToString())) {
385                                                        found = true;
386                                                        p.areaCode.belongingTo = distinctAreas.get(k);
387                                                        p.areaCode.code = null;
388                                                        break;
389                                                }
390                                        }
391                                        if (found == false) {
392                                                p.areaCode.belongingTo = new MajorRoadArea(p.areaCode);
393                                                p.areaCode.code = null;
394                                                distinctAreas.add(p.areaCode.belongingTo);
395                                        }
396                                        // }
397                                } else {
398                                        // if (!isOnBorder(p.areaCode.code)) {
399                                        p.areaCode.belongingTo = new MajorRoadArea(p.areaCode);
400                                        p.areaCode.code = null;
401                                        distinctAreas.add(p.areaCode.belongingTo);
402                                        // }
403                                }
404
405                        }
406                }
407
408                for (i = 0; i < distinctAreas.size(); i++) {
409                        distinctAreas.get(i).constructArea();
410                }
411
412        }
413
414        public void mergeAreas() {
415                int i, j, k, u, v;
416                Road r;
417                Point p1, p2;
418                ArrayList<MajorRoadArea> changed = new ArrayList<MajorRoadArea>();
419                ArrayList<MajorRoadArea> changedWith = new ArrayList<MajorRoadArea>();
420                ArrayList<Integer> changedIdx = new ArrayList<Integer>();
421                Cross c;
422                for (i = 0; i < Globals.map.roads.size(); i++) {
423                        r = Globals.map.roads.get(i);
424                        if (i == 5360) {
425                                System.out.println("STACK");
426                        }
427
428                        for (j = 0; j < r.points.size() - 1; j++) {
429                                p1 = r.points.get(j);
430                                p2 = r.points.get(j + 1);
431                                if (!p1.areaCode.belongingTo.areaCodeString
432                                                .equals(p2.areaCode.belongingTo.areaCodeString)) {
433
434                                        if ((!isOnBorder(p1.areaCode.belongingTo.areaCodeByte))
435                                                        && (!isOnBorder(p2.areaCode.belongingTo.areaCodeByte))) {
436                                                changed.add(new MajorRoadArea(p2.areaCode.belongingTo));
437                                                changedWith.add(new MajorRoadArea(
438                                                                p1.areaCode.belongingTo));
439                                                changedIdx.add(new Integer(this.distinctAreas
440                                                                .indexOf(p2.areaCode.belongingTo)));
441                                                for (v = 0; v < changedWith.size() - 1; v++) {
442                                                        if (changedWith.get(v).areaCodeString
443                                                                        .equals(p2.areaCode.belongingTo.areaCodeString)) {
444                                                                this.distinctAreas.get(
445                                                                                changedIdx.get(v).intValue()).move(
446                                                                                p1.areaCode.belongingTo);
447                                                                changedWith.get(v)
448                                                                                .move(p1.areaCode.belongingTo);
449                                                        }
450                                                }
451                                                p2.areaCode.belongingTo.move(p1.areaCode.belongingTo);
452
453                                        }
454                                }
455
456                        }
457
458                        for (j = 0; j < r.crosses.size(); j++) {
459                                c = r.crosses.get(j);
460                                p1 = r.points.get(c.getPointIndex());
461                                p2 = Globals.map.roads.get(c.getCrossRoadIndex()).points.get(c
462                                                .getCrossPointIndex());
463                                if (!p1.areaCode.belongingTo.areaCodeString
464                                                .equals(p2.areaCode.belongingTo.areaCodeString)) {
465
466                                        if ((!isOnBorder(p1.areaCode.belongingTo.areaCodeByte))
467                                                        && (!isOnBorder(p2.areaCode.belongingTo.areaCodeByte))) {
468                                                changed.add(new MajorRoadArea(p2.areaCode.belongingTo));
469                                                changedWith.add(new MajorRoadArea(
470                                                                p1.areaCode.belongingTo));
471                                                changedIdx.add(new Integer(this.distinctAreas
472                                                                .indexOf(p2.areaCode.belongingTo)));
473                                                for (v = 0; v < changedWith.size() - 1; v++) {
474                                                        if (changedWith.get(v).areaCodeString
475                                                                        .equals(p2.areaCode.belongingTo.areaCodeString)) {
476                                                                this.distinctAreas.get(
477                                                                                changedIdx.get(v).intValue()).move(
478                                                                                p1.areaCode.belongingTo);
479                                                                changedWith.get(v)
480                                                                                .move(p1.areaCode.belongingTo);
481                                                        }
482                                                }
483                                                p2.areaCode.belongingTo.move(p1.areaCode.belongingTo);
484
485                                        }
486                                }
487                        }
488                }
489                //              for (i = 0; i < changed.size(); i++) {
490                //                      System.out.println(" ****   " + changed.get(i).areaCodeString
491                //                                      + "   cu   " + changedWith.get(i).areaCodeString);
492                //
493                //              }
494                ArrayList<Integer> borders;
495                for (i = 0; i < mainRoads.size(); i++) {
496                        r = Globals.map.roads.get(mainRoads.get(i).intValue());
497                        for (j = 0; j < r.points.size() - 1; j++) {
498                                p1 = r.points.get(j);
499                                p2 = r.points.get(j + 1);
500                                if (!p1.areaCode.belongingTo.areaCodeString
501                                                .equals(p2.areaCode.belongingTo.areaCodeString)) {
502                                        for (u = 0; u < changed.size(); u++) {
503                                                k = -1;
504                                                k = isOnBorderOf(p1.areaCode.belongingTo.areaCodeByte,
505                                                                changed.get(u).areaCodeByte);
506                                                if (k > -1) {
507                                                        borders = getBordersOf(p1.areaCode.belongingTo.areaCodeByte);
508
509                                                        p1.areaCode.belongingTo.move(changedWith.get(u));
510                                                        p1.areaCode.belongingTo.areaCodeByte[k] = 2;
511                                                        for (k = 0; k < borders.size(); k++) {
512                                                                p1.areaCode.belongingTo.areaCodeByte[borders
513                                                                                .get(k).intValue()] = 2;
514                                                        }
515                                                        p1.areaCode.belongingTo.setStringValue();
516
517                                                }
518                                                k = -1;
519                                                k = isOnBorderOf(p2.areaCode.belongingTo.areaCodeByte,
520                                                                changed.get(u).areaCodeByte);
521                                                if (k > -1) {
522                                                        borders = getBordersOf(p2.areaCode.belongingTo.areaCodeByte);
523
524                                                        p2.areaCode.belongingTo.move(changedWith.get(u));
525                                                        for (k = 0; k < borders.size(); k++) {
526                                                                p2.areaCode.belongingTo.areaCodeByte[borders
527                                                                                .get(k).intValue()] = 2;
528                                                        }
529                                                        p2.areaCode.belongingTo.setStringValue();
530
531                                                }
532                                        }
533                                }
534
535                        }
536
537                }
538
539        }
540
541        public boolean isOnBorder(byte[] area) {
542                int i;
543                for (i = 0; i < area.length; i++) {
544                        if (area[i] == 2) {
545                                return true;
546                        }
547                }
548                return false;
549        }
550
551        // /area1Code is the border of area2
552        public int isOnBorderOf(byte[] area1, byte[] area2) {
553                int i;
554
555                int j = -1;
556                for (i = 0; i < area1.length; i++) {
557                        if (area1[i] != area2[i]) {
558                                if (area1[i] != 2) {
559                                        return -1;
560                                } else {
561                                        continue;
562                                }
563                        }
564                }
565
566                return 0;
567        }
568
569        public ArrayList<Integer> getBordersOf(byte[] area1) {
570                int i;
571                ArrayList<Integer> rez = new ArrayList<Integer>();
572                int j = -1;
573                for (i = 0; i < area1.length; i++) {
574                        if (area1[i] == 2) {
575                                rez.add(new Integer(i));
576                        }
577                }
578
579                return rez;
580        }
581
582        public boolean inSameArea(byte[] area1, byte[] area2) {
583                int i;
584                if (area1.length != area2.length) {
585                        return false;
586                } else {
587                        for (i = 0; i < area1.length; i++) {
588                                if (area1[i] != area2[i]) {
589                                        if ((area1[i] != 2) && (area2[i] != 2)) {
590                                                return false;
591                                        }
592                                }
593                        }
594                        return true;
595                }
596        }
597
598        public int getAreaFor(Point p) {
599                int i;
600                for (i = 0; i < distinctAreas.size(); i++) {
601                        if (distinctAreas.get(i).areaCodeString
602                                        .equals(p.areaCode.belongingTo.areaCodeString)) {
603                                return i;
604                        }
605                }
606                return -1;
607
608        }
609
610}
Note: See TracBrowser for help on using the repository browser.