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

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 8.2 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 java.util.ArrayList;
16import javax.media.opengl.GL;
17import javax.media.opengl.GLAutoDrawable;
18import javax.media.opengl.GLDrawable;
19import javax.media.opengl.glu.GLU;
20import com.sun.opengl.util.Animator;
21import com.sun.opengl.util.GLUT;
22
23public class MapPanel extends MapViewPanel {
24
25        /**
26         *
27         */
28        private static final long serialVersionUID = -5883719321862303634L;
29
30        public Map myMap;
31
32        public Animator anim;
33
34        Route currentRoute = null;
35
36        EntryExitConfig eec;
37
38        public Location start, stop;
39
40        public ArrayList<Location> routePoints = null;
41
42        public MapPanel(int w, int h, Map map) {
43                super(null, w, h);
44                this.myMap = map;
45                Globals.minPoint = myMap.minPoint;
46                Globals.maxPoint = myMap.maxPoint;
47        }
48
49        public void display(GLAutoDrawable glDrawable) {
50                Road currentRoad;
51                Point currentPoint = null;
52                float x = 0.0f, y = 0.0f, x2, y2;
53                int i;
54                GL myGL = glDrawable.getGL();
55                GLU myGLU = new GLU();
56                myGL.glClear(GL.GL_COLOR_BUFFER_BIT);
57                myGL.glMatrixMode(GL.GL_MODELVIEW);
58                myGL.glLoadIdentity();
59                myGLU.gluLookAt(xCamera, yCamera, zCamera, xCamera, yCamera, 0.0, 0.0, 1.0, 0.0);
60                myGL.glCallList(mapList); // displays the map
61                // depending on the camera position displays one level of text
62                if (zCamera < 800) {
63                        myGL.glCallList(mapLevel[4]);
64                }
65                if ((zCamera >= 800) && (zCamera < 1600)) {
66                        myGL.glCallList(mapLevel[3]);
67                }
68                if ((zCamera >= 1600) && (zCamera < 2800)) {
69                        myGL.glCallList(mapLevel[2]);
70                }
71                if ((zCamera >= 2800) && (zCamera < 3700)) {
72                        myGL.glCallList(mapLevel[1]);
73                }
74                if (zCamera > 3700) {
75                        myGL.glCallList(mapLevel[0]);
76                }
77                currentRoad = null;
78                if (currentRoute != null) {
79                        RouteSegment rs;
80                        int pt1, pt2;
81                        for (i = 0; i < currentRoute.route.size(); i++) {
82                                rs = currentRoute.route.get(i);
83                                myGL.glPushMatrix();
84                                myGL.glLineWidth(3.0f);
85                                myGL.glColor3f(1.0f, 0.0f, 0.0f);
86                                if (rs.pt1 < rs.pt2) {
87                                        pt1 = rs.pt1;
88                                        pt2 = rs.pt2;
89                                } else {
90                                        pt2 = rs.pt1;
91                                        pt1 = rs.pt2;
92                                }
93                                currentRoad = myMap.roads.get(rs.roadIndex);
94                                currentPoint = currentRoad.points.get(pt1);
95                                y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
96                                x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
97                                for (int j = pt1 + 1; j <= pt2; j++) {
98                                        currentPoint = currentRoad.points.get(j);
99                                        y2 = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
100                                        x2 = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
101                                        myGL.glBegin(GL.GL_LINES);
102                                        myGL.glVertex3f(x, y, 1.0f);
103                                        myGL.glVertex3f(x2, y2, 1.0f);
104                                        myGL.glEnd();
105                                        y = y2;
106                                        x = x2;
107                                }
108                                myGL.glPopMatrix();
109                        }
110                }
111                if (start != null) {
112                        currentRoad = this.myMap.roads.get(start.roadIdx);
113                        currentPoint = currentRoad.points.get(start.ptIdx);
114                        y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
115                        x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
116                        myGL.glPushMatrix();
117                        {
118                                myGL.glLineWidth(3.0f);
119                                myGL.glTranslatef(x, y, 0.0f);
120                                myGL.glColor3f(1.0f, 1.0f, 1.0f);
121                                myGL.glBegin(GL.GL_POLYGON);
122                                {
123                                        myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
124                                        myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
125                                        myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
126                                        myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
127                                }
128                                myGL.glEnd();
129                        }
130                        myGL.glPopMatrix();
131                }
132                if (stop != null) {
133                        currentRoad = this.myMap.roads.get(stop.roadIdx);
134                        currentPoint = currentRoad.points.get(stop.ptIdx);
135                        y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint);
136                        x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint);
137                        myGL.glPushMatrix();
138                        {
139                                myGL.glLineWidth(3.0f);
140                                myGL.glTranslatef(x, y, 0.0f);
141                                myGL.glColor3f(0.0f, 0.0f, 0.0f);
142                                myGL.glBegin(GL.GL_POLYGON);
143                                {
144                                        myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
145                                        myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
146                                        myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
147                                        myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f);
148                       
149                                }
150                                myGL.glEnd();
151                        }
152                }
153                myGL.glPopMatrix();
154                display_Arrows(myGL);
155                display_Legend(myGL);
156                synchronized (Globals.mutex) {
157                        Globals.flag = 0;
158                        Globals.mutex.notify();
159                }
160
161        }
162
163        public void reshape(GLDrawable glDrawable, int i, int i1, int i2, int i3) {
164        }
165
166        public void buildList(GLAutoDrawable glDrawable) {
167                GL myGL = glDrawable.getGL();
168                GLUT glut = new GLUT();
169                GLU myGLU = new GLU();
170                prioritySign = myGL.glGenLists(12);
171                this.mapLevel = new int[5];
172                ListBuilder.buildList_PrioritySign(myGL, myGLU, glut, prioritySign);
173                noPrioritySign = prioritySign + 1;
174                ListBuilder.buildList_NoPrioritySign(myGL, myGLU, glut, noPrioritySign);
175                stopSign = noPrioritySign + 1;
176                ListBuilder.buildList_StopSign(myGL, myGLU, glut, stopSign);
177                mapList = stopSign + 1;
178                ListBuilder.buildList_MapList(myGL, myGLU, glut, mapList, prioritySign, stopSign, noPrioritySign,
179                                myMap.minPoint, myMap.maxPoint);
180                float y2 = (float) GPSutil.getMetersLatitude(myMap.minPoint, myMap.maxPoint);
181                float x2 = (float) GPSutil.getMetersLongitude(myMap.minPoint, myMap.maxPoint);
182                xCamera = x2 / 2;
183                yCamera = y2 / 2;
184                zCamera = (float) ((x2 * high) / this.getWidth());
185                carList = mapList + 1;
186                ListBuilder.buildList_CarList(myGL, myGLU, glut, carList);
187                carListOut = carList + 1;
188                ListBuilder.buildList_CarListOut(myGL, myGLU, glut, carListOut);
189                neighbor = carListOut + 1;
190                ListBuilder.buildList_Neighbor(myGL, myGLU, glut, neighbor);
191                arrows = neighbor + 1;
192                ListBuilder.buildList_Arrows(myGL, myGLU, glut, arrows, this.getWidth(), this.getHeight(), 60);
193                mapLevel[0] = arrows + 1;
194                ListBuilder.buildList_MapLevels(myGL, myGLU, glut, mapLevel, myMap.minPoint);
195        }
196
197        public void selection(float x, float y) {
198                // Gets the world coordinates of the mouse click
199                float winX = x;
200                float winY = (float) this.getSize().height - (float) y;
201                float w = (float) (this.getSize().width * zCamera) / high;
202                float h = (float) (this.getSize().height * zCamera) / high;
203                float worldX = xCamera - (float) (w / 2) + (winX * zCamera) / high;
204                float worldY = yCamera - (float) (h / 2) + (winY * zCamera) / high;
205                recoverRoad((float) worldX, (float) worldY);
206                Point recovered = recoverRoad((float) worldX, (float) worldY);
207                System.out.println("Selectie " + recovered.getLatitude());
208                if (recovered != null) {
209                        PeanoKey pk = Globals.map.findClosestPeanoKey(recovered);
210                        if (pk != null) {
211                                Road r = (Road) Globals.map.roads.get(pk.getRoadIndex());
212                                Point px = r.points.get(pk.getPointIndex());
213                                System.out.println("Click on road id: " + pk.getRoadIndex() + ";"
214                                                + ((Road) Globals.map.roads.get(pk.getRoadIndex())).getName() + " la punctul "
215                                                + pk.getPointIndex() + ": " + px);
216                        }
217                } else {
218                        // no road found
219                }
220        }
221
222        public void setRoute(Route r) {
223                this.currentRoute = r;
224        }
225
226        public void setStart(Location start) {
227                this.start = start;
228        }
229
230        public void setStop(Location stop) {
231                this.stop = stop;
232        }
233
234}
Note: See TracBrowser for help on using the repository browser.