source: proiecte/ptvs/src/vnsim/gui/selector/RouteDraw.java @ 31

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 15.5 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.gui.selector;
7
8import vnsim.gui.ListBuilder;
9import vnsim.gui.MapViewPanel;
10import vnsim.map.object.*;
11import vnsim.map.utils.*;
12import vnsim.vehicular.scenarios.Route;
13import vnsim.vehicular.simulator.Location;
14import vnsim.vehicular.simulator.RouteSegment;
15import vnsim.vehicular.simulator.intersections.DirectedRoadSegment;
16import vnsim.vehicular.simulator.intersections.Intersection;
17
18import java.awt.event.MouseEvent;
19import java.util.ArrayList;
20import javax.media.opengl.GL;
21import javax.media.opengl.GLAutoDrawable;
22import javax.media.opengl.GLDrawable;
23import javax.media.opengl.glu.GLU;
24import com.sun.opengl.util.Animator;
25import com.sun.opengl.util.GLUT;
26
27public class RouteDraw extends MapViewPanel {
28
29        private static final long serialVersionUID = -5883719321862303634L;
30
31        public Map myMap;
32
33        public Animator anim;
34
35        Route currentRoute = null;
36
37        int width;
38
39        int height;
40
41        public Location start = null, stop = null;
42
43        public ArrayList<Location> routePoints = null;
44
45        public ArrayList<Location> entrys = null;
46
47        public ArrayList<Location> exits = null;
48
49        public Location currentLocation = null;
50
51        public boolean isRouteEditing = false;
52
53        public boolean isRouteShowing = false;
54
55        public Road selectedRoad = null;
56
57        public Road lastRoad = null;
58
59        public Intersection lastIntersection = null;
60
61        public boolean isRoad = true;
62
63        RouteConfiguration rc;
64
65        public RouteDraw(RouteConfiguration rc, int w, int h, Map map) {
66                super(null, w, h);
67                this.rc = rc;
68                this.myMap = map;
69                this.width = w;
70                this.height = h;
71                Globals.map = myMap;
72                Globals.minPoint = myMap.minPoint;
73                Globals.maxPoint = myMap.maxPoint;
74                entrys = new ArrayList<Location>();
75                exits = new ArrayList<Location>();
76        }
77
78        public void display(GLAutoDrawable glDrawable) {
79                Road currentRoad;
80                Point currentPoint = null;
81                float x = 0.0f, y = 0.0f;
82                GL myGL = glDrawable.getGL();
83                GLU myGLU = new GLU();
84                myGL.glClear(GL.GL_COLOR_BUFFER_BIT);
85                myGL.glMatrixMode(GL.GL_MODELVIEW);
86                myGL.glLoadIdentity();
87                myGLU.gluLookAt(xCamera, yCamera, zCamera, xCamera, yCamera, 0.0, 0.0, 1.0, 0.0);
88                myGL.glCallList(mapList); // displays the map
89                // depending on the camera position displays one level of text
90                if (zCamera < 800) {
91                        myGL.glCallList(mapLevel[4]);
92                }
93                if ((zCamera >= 800) && (zCamera < 1600)) {
94                        myGL.glCallList(mapLevel[3]);
95                }
96                if ((zCamera >= 1600) && (zCamera < 2800)) {
97                        myGL.glCallList(mapLevel[2]);
98                }
99                if ((zCamera >= 2800) && (zCamera < 3700)) {
100                        myGL.glCallList(mapLevel[1]);
101                }
102                if (zCamera > 3700) {
103                        myGL.glCallList(mapLevel[0]);
104                }
105                if ((isRoad) && (lastRoad != null) && (isRouteEditing)) {
106                        float x2, y2;
107                        myGL.glPushMatrix();
108                        myGL.glLineWidth(3.0f);
109                        myGL.glColor3f(0.0f, 0.0f, 1.0f);
110                        currentPoint = lastRoad.points.get(0);
111                        y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
112                        x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
113                        for (int j = 1; j < lastRoad.points.size(); j++) {
114                                currentPoint = lastRoad.points.get(j);
115                                y2 = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
116                                x2 = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
117                                myGL.glBegin(GL.GL_LINES);
118                                {
119                                        myGL.glVertex3f(x, y, 1.0f);
120                                        myGL.glVertex3f(x2, y2, 1.0f);
121                                }
122                                myGL.glEnd();
123                                y = y2;
124                                x = x2;
125                        }
126                        myGL.glPopMatrix();
127                } else if ((lastIntersection != null) && (isRouteEditing)) {
128                        for (int i = 0; i < lastIntersection.segments.size(); i++) {
129                                Road myCurrentRoad = lastIntersection.segments.get(i).road;
130                                float x2, y2;
131                                myGL.glPushMatrix();
132                                myGL.glLineWidth(3.0f);
133                                myGL.glColor3f(0.0f, 0.0f, 1.0f);
134                                currentPoint = myCurrentRoad.points.get(0);
135                                y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
136                                x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
137                                for (int j = 1; j < myCurrentRoad.points.size(); j++) {
138                                        currentPoint = myCurrentRoad.points.get(j);
139                                        y2 = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
140                                        x2 = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
141                                        myGL.glBegin(GL.GL_LINES);
142                                        {
143                                                myGL.glVertex3f(x, y, 1.0f);
144                                                myGL.glVertex3f(x2, y2, 1.0f);
145                                        }
146                                        myGL.glEnd();
147                                        y = y2;
148                                        x = x2;
149                                }
150                                myGL.glPopMatrix();
151                        }
152                }
153                if (selectedRoad != null) {
154                        float x2, y2;
155                        myGL.glPushMatrix();
156                        myGL.glLineWidth(3.0f);
157                        myGL.glColor3f(0.0f, 1.0f, 0.0f);
158                        currentPoint = selectedRoad.points.get(0);
159                        y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
160                        x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
161                        for (int j = 1; j < selectedRoad.points.size(); j++) {
162                                currentPoint = selectedRoad.points.get(j);
163                                y2 = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
164                                x2 = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
165                                myGL.glBegin(GL.GL_LINES);
166                                {
167                                        myGL.glVertex3f(x, y, 1.0f);
168                                        myGL.glVertex3f(x2, y2, 1.0f);
169                                }
170                                myGL.glEnd();
171                                y = y2;
172                                x = x2;
173                        }
174                        myGL.glPopMatrix();
175                }
176                if (currentRoute != null) {
177                        RouteSegment rs;
178                        int pt1, pt2, i;
179                        float x2, y2;
180                        for (i = 0; i < currentRoute.route.size(); i++) {
181                                rs = currentRoute.route.get(i);
182                                myGL.glPushMatrix();
183                                myGL.glLineWidth(3.0f);
184                                myGL.glColor3f(1.0f, 0.0f, 0.0f);
185                                if (rs.pt1 < rs.pt2) {
186                                        pt1 = rs.pt1;
187                                        pt2 = rs.pt2;
188                                } else {
189                                        pt2 = rs.pt1;
190                                        pt1 = rs.pt2;
191                                }
192                                currentRoad = myMap.roads.get(rs.roadIndex);
193                                currentPoint = currentRoad.points.get(pt1);
194                                y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
195                                x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
196                                for (int j = pt1 + 1; j <= pt2; j++) {
197                                        currentPoint = currentRoad.points.get(j);
198                                        y2 = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
199                                        x2 = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
200                                        myGL.glBegin(GL.GL_LINES);
201                                        {
202                                                myGL.glVertex3f(x, y, 1.0f);
203                                                myGL.glVertex3f(x2, y2, 1.0f);
204                                        }
205                                        myGL.glEnd();
206                                        y = y2;
207                                        x = x2;
208                                }
209                                myGL.glPopMatrix();
210                        }
211                }
212                if (isRouteEditing || isRouteShowing) {
213                        if (routePoints != null) {
214                                for (int i = 1; i < routePoints.size(); i++) {
215                                        currentRoad = this.myMap.roads.get(routePoints.get(i).roadIdx);
216                                        currentPoint = currentRoad.points.get(routePoints.get(i).ptIdx);
217                                        y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
218                                        x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
219                                        myGL.glPushMatrix();
220                                        {
221                                                myGL.glLineWidth(3.0f);
222                                                myGL.glTranslatef(x, y, 0.0f);
223                                                myGL.glColor3f(1.0f, 0.0f, 0.0f);
224                                                myGL.glBegin(GL.GL_POLYGON);
225                                                {
226                                                        myGL.glVertex3f(-10.0f*zCamera/(100000.0f * (float) maxlonglat), -10.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
227                                                        myGL.glVertex3f(10.0f*zCamera/(100000.0f * (float) maxlonglat), -10.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
228                                                        myGL.glVertex3f(10.0f*zCamera/(100000.0f * (float) maxlonglat), 10.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
229                                                        myGL.glVertex3f(-10.0f*zCamera/(100000.0f * (float) maxlonglat), 10.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
230                                                }
231                                                myGL.glEnd();
232                                        }
233                                        myGL.glPopMatrix();
234                                }
235                        }
236                }
237                if (start != null) {
238                        currentRoad = this.myMap.roads.get(start.roadIdx);
239                        currentPoint = currentRoad.points.get(start.ptIdx);
240                        y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
241                        x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
242                        myGL.glPushMatrix();
243                        {
244                                myGL.glLineWidth(3.0f);
245                                myGL.glTranslatef(x, y, 0.0f);
246                                myGL.glColor3f(1.0f, 1.0f, 1.0f);
247                                myGL.glBegin(GL.GL_POLYGON);
248                                {
249                                        myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
250                                        myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
251                                        myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
252                                        myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
253                                }
254                                myGL.glEnd();
255                        }
256                        myGL.glPopMatrix();
257                }
258                if (stop != null) {
259                        currentRoad = this.myMap.roads.get(stop.roadIdx);
260                        currentPoint = currentRoad.points.get(stop.ptIdx);
261                        y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
262                        x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
263                        myGL.glPushMatrix();
264                        {
265                                myGL.glLineWidth(3.0f);
266                                myGL.glTranslatef(x, y, 0.0f);
267                                myGL.glColor3f(0.0f, 0.0f, 0.0f);
268                                myGL.glBegin(GL.GL_POLYGON);
269                                {
270                                        myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
271                                        myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
272                                        myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
273                                        myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
274                                }
275                                myGL.glEnd();
276                        }
277                        myGL.glPopMatrix();
278                }
279                display_Arrows(myGL);
280                display_Legend(myGL);
281                synchronized (Globals.mutex) {
282                        Globals.flag = 0;
283                        Globals.mutex.notify();
284                }
285        }
286
287        public void reshape(GLDrawable glDrawable, int i, int i1, int i2, int i3) {
288        }
289
290        public void buildList(GLAutoDrawable glDrawable) {
291                GL myGL = glDrawable.getGL();
292                GLUT glut = new GLUT();
293                GLU myGLU = new GLU();
294                prioritySign = myGL.glGenLists(12);
295                this.mapLevel = new int[5];
296                ListBuilder.buildList_PrioritySign(myGL, myGLU, glut, prioritySign);
297                noPrioritySign = prioritySign + 1;
298                ListBuilder.buildList_NoPrioritySign(myGL, myGLU, glut, noPrioritySign);
299                stopSign = noPrioritySign + 1;
300                ListBuilder.buildList_StopSign(myGL, myGLU, glut, stopSign);
301                mapList = stopSign + 1;
302                ListBuilder.buildList_MapList(myGL, myGLU, glut, mapList, prioritySign, stopSign, noPrioritySign,
303                                myMap.minPoint, myMap.maxPoint);
304                float y2 = (float) GPSutil.getMetersLatitude(myMap.minPoint, myMap.maxPoint);
305                float x2 = (float) GPSutil.getMetersLongitude(myMap.minPoint, myMap.maxPoint);
306                xCamera = x2 / 2;
307                yCamera = y2 / 2;
308                zCamera = (float) ((x2 * high) / width);
309                arrows = mapList + 1;
310                ListBuilder.buildList_Arrows(myGL, myGLU, glut, arrows, width, height, 18);
311                mapLevel[0] = arrows + 1;
312                ListBuilder.buildList_MapLevels(myGL, myGLU, glut, mapLevel, myMap.minPoint);
313        }
314
315        private Intersection toIntersectionPoint(Location l) {
316                for (int i = 0; i < Globals.map.allIntersections.size(); i++) {
317                        for (int j = 0; j < Globals.map.allIntersections.get(i).segments.size(); j++) {
318                                DirectedRoadSegment drs = Globals.map.allIntersections.get(i).segments.get(j);
319                                if ((drs.roadIndex == l.roadIdx) && (Math.abs(drs.pointIndex - l.ptIdx) < 5)) {
320                                        l.ptIdx = drs.pointIndex;
321                                        return Globals.map.allIntersections.get(i);
322                                }
323                        }
324                }
325                return null;
326        }
327
328        public void selection(MouseEvent e) {
329                // Gets the world coordinates of the mouse click
330                float winX = e.getX();
331                float winY = (float) this.getSize().height - (float) e.getY();
332                float w = (float) (this.getSize().width * zCamera) / high;
333                float h = (float) (this.getSize().height * zCamera) / high;
334                float worldX = xCamera - (float) (w / 2) + (winX * zCamera) / high;
335                float worldY = yCamera - (float) (h / 2) + (winY * zCamera) / high;
336                Point recovered = recoverRoad((float) worldX, (float) worldY);
337                if (recovered != null) {
338                        PeanoKey pk = Globals.map.findClosestPeanoKey(recovered);
339                        if (pk != null) {
340                                Road r = (Road) Globals.map.roads.get(pk.getRoadIndex());
341                                Point px = r.points.get(pk.getPointIndex());
342                                rc.selectedroad.setText(((Road) Globals.map.roads.get(pk.getRoadIndex())).getName().trim());
343                                rc.selectedpoint.setText(String.valueOf(pk.getPointIndex()).trim());
344                                System.out.println("Click on road id: " + pk.getRoadIndex() + ";"
345                                                + ((Road) Globals.map.roads.get(pk.getRoadIndex())).getName() + " la punctul "
346                                                + pk.getPointIndex() + ": " + px);
347                                currentLocation = new Location(pk.getRoadIndex(), pk.getPointIndex());
348                                if (isRouteEditing) {
349                                        if (routePoints.size() % 2 == 0) {
350                                                Intersection intersectie = toIntersectionPoint(currentLocation);
351                                                if ((intersectie != null)&&(intersectie==lastIntersection)&&(Globals.map.roads.get(currentLocation.roadIdx)!=lastRoad)) {
352                                                        lastRoad = Globals.map.roads.get(currentLocation.roadIdx);
353                                                        routePoints.add(currentLocation);
354                                                        isRoad = true;
355                                                        System.err.println("Adaug 1");
356                                                } else {
357                                                        System.err.println("Invalid point! Place the point near the last intersection");
358                                                }
359                                        } else {
360                                                if (routePoints.get(routePoints.size() - 1).roadIdx == currentLocation.roadIdx) {
361                                                        lastIntersection = toIntersectionPoint(currentLocation);
362                                                        if (lastIntersection != null) {
363                                                                routePoints.add(currentLocation);
364                                                                isRoad = false;
365                                                                System.err.println("Adaug 2");
366                                                                currentRoute.route.add(new RouteSegment((short) currentLocation.roadIdx,
367                                                                                (short) routePoints.get(routePoints.size() - 2).ptIdx,
368                                                                                (short) currentLocation.ptIdx));
369                                                        } else {
370                                                                System.err.println("Invalid point! Place the point near an intersection");
371                                                        }
372                                                } else {
373                                                        System.err.println("Invalid point! Place another point at the end of the last road.");
374                                                }
375                                        }
376                                }
377                        } else {
378                                rc.selectedroad.setText("");
379                                rc.selectedPoint.setText("");
380                                currentLocation = null;
381                        }
382                } else {
383                        // no road found
384                        rc.selectedroad.setText("");
385                        rc.selectedpoint.setText("");
386                        currentLocation = null;
387                }
388        }
389
390        public void mouseMoved(MouseEvent e) {
391                float winX = e.getX();
392                float winY = (float) this.getSize().height - (float) e.getY();
393                float w = (float) (this.getSize().width * zCamera) / high;
394                float h = (float) (this.getSize().height * zCamera) / high;
395                float worldX = xCamera - (float) (w / 2) + (winX * zCamera) / high;
396                float worldY = yCamera - (float) (h / 2) + (winY * zCamera) / high;
397                recoverRoad((float) worldX, (float) worldY);
398                Point recovered = recoverRoad((float) worldX, (float) worldY);
399                if (recovered != null) {
400                        PeanoKey pk = Globals.map.findClosestPeanoKey(recovered);
401                        if (pk != null) {
402                                Road r = (Road) Globals.map.roads.get(pk.getRoadIndex());
403                                rc.currentroad.setText(((Road) Globals.map.roads.get(pk.getRoadIndex())).getName().trim());
404                                rc.currentpoint.setText(String.valueOf(pk.getPointIndex()).trim());
405                                selectedRoad = r;
406                        } else {
407                                selectedRoad = null;
408                        }
409                }
410        }
411
412        public void setRoute(Route r) {
413                this.currentRoute = r;
414        }
415
416        public void setStart(Location start) {
417                this.start = start;
418                isRoad = true;
419                this.lastRoad = Globals.map.roads.get(start.roadIdx);
420        }
421
422        public void setStop(Location stop) {
423                this.stop = stop;
424        }
425
426        public void setSize(int width, int height) {
427                super.setSize(width, height);
428        }
429
430}
Note: See TracBrowser for help on using the repository browser.