[31] | 1 | package vnsim.gui; |
---|
| 2 | |
---|
| 3 | import java.awt.event.KeyEvent; |
---|
| 4 | import java.awt.event.KeyListener; |
---|
| 5 | import java.awt.event.MouseEvent; |
---|
| 6 | import java.awt.event.MouseListener; |
---|
| 7 | import java.awt.event.MouseMotionListener; |
---|
| 8 | import java.awt.event.MouseWheelEvent; |
---|
| 9 | import java.awt.event.MouseWheelListener; |
---|
| 10 | import java.awt.geom.Point2D; |
---|
| 11 | import java.util.ArrayList; |
---|
| 12 | import java.util.HashMap; |
---|
| 13 | import java.util.Iterator; |
---|
| 14 | import java.util.Vector; |
---|
| 15 | |
---|
| 16 | import javax.media.opengl.GL; |
---|
| 17 | import javax.media.opengl.GLAutoDrawable; |
---|
| 18 | import javax.media.opengl.GLCanvas; |
---|
| 19 | import javax.media.opengl.GLCapabilities; |
---|
| 20 | import javax.media.opengl.GLEventListener; |
---|
| 21 | import javax.media.opengl.glu.GLU; |
---|
| 22 | import javax.swing.JPanel; |
---|
| 23 | |
---|
| 24 | import com.sun.opengl.util.Animator; |
---|
| 25 | import com.sun.opengl.util.GLUT; |
---|
| 26 | |
---|
| 27 | import vnsim.applications.adaptiveTL.IntersectionCarRecord; |
---|
| 28 | import vnsim.applications.adaptiveTL.WirelessTrafficLight; |
---|
| 29 | import vnsim.applications.trafficview.SimulatedCarInfo; |
---|
| 30 | import vnsim.core.CarInfo; |
---|
| 31 | import vnsim.map.object.Globals; |
---|
| 32 | import vnsim.map.object.Point; |
---|
| 33 | import vnsim.map.object.Road; |
---|
| 34 | import vnsim.map.utils.GPSutil; |
---|
| 35 | import vnsim.vehicular.simulator.CarInstance; |
---|
| 36 | import vnsim.vehicular.simulator.RouteSegment; |
---|
| 37 | import vnsim.vehicular.simulator.intersections.DirectedRoadSegment; |
---|
| 38 | import vnsim.vehicular.simulator.intersections.IntersectionWithTrafficLights; |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * @author Simion Liviu Mihai |
---|
| 42 | * |
---|
| 43 | */ |
---|
| 44 | public abstract class MapViewPanel extends JPanel implements GLEventListener, MouseListener, MouseMotionListener, |
---|
| 45 | KeyListener, MouseWheelListener { |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * |
---|
| 49 | */ |
---|
| 50 | private static final long serialVersionUID = 1L; |
---|
| 51 | |
---|
| 52 | public GLCanvas canvas; |
---|
| 53 | |
---|
| 54 | /** Camera position */ |
---|
| 55 | public float xCamera, yCamera, zCamera; |
---|
| 56 | |
---|
| 57 | /** display lists */ |
---|
| 58 | public int mapList, carList, accCar, hAccCar, hDecCar, signalCar, decCar, stoppedCar, whiteCar, pinkCar, traceCar, |
---|
| 59 | carListOut, neighbor, endOfQueue, arrows, prioritySign, noPrioritySign, stopSign, DSRCapp; |
---|
| 60 | |
---|
| 61 | public int[] mapLevel; |
---|
| 62 | |
---|
| 63 | /** Clicks on the map */ |
---|
| 64 | |
---|
| 65 | public Vector<Point2D.Float> zones; |
---|
| 66 | public HashMap<CarInfo, MyPoint> carPositions = new HashMap<CarInfo, MyPoint>(); |
---|
| 67 | |
---|
| 68 | public float high; |
---|
| 69 | |
---|
| 70 | /** Car currently selected */ |
---|
| 71 | public SimulatedCarInfo currentCar = null; |
---|
| 72 | |
---|
| 73 | public WirelessTrafficLight currentIntersection = null; |
---|
| 74 | |
---|
| 75 | public float reposCameraX = 0, reposCameraY = 0, reposCameraZ = 0; |
---|
| 76 | |
---|
| 77 | double maxX = 0, maxY = 0; |
---|
| 78 | |
---|
| 79 | int pressX, pressY, releaseX, releaseY; |
---|
| 80 | |
---|
| 81 | ArrayList<SimulatedCarInfo> ci; |
---|
| 82 | |
---|
| 83 | public Animator anim; |
---|
| 84 | |
---|
| 85 | public float maxlonglat = 0; |
---|
| 86 | |
---|
| 87 | public boolean printRoute = false; |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * |
---|
| 91 | * @param s |
---|
| 92 | * @param w |
---|
| 93 | * Latimea ferestrei. |
---|
| 94 | * @param h |
---|
| 95 | * Inaltimea ferestrei. |
---|
| 96 | */ |
---|
| 97 | public MapViewPanel(Statistics s, int w, int h) { |
---|
| 98 | super(); |
---|
| 99 | GLCapabilities capabilities = new GLCapabilities(); |
---|
| 100 | capabilities.setHardwareAccelerated(true); // and sets it's properties |
---|
| 101 | // Se creeaza un nou canvas |
---|
| 102 | canvas = new GLCanvas(); |
---|
| 103 | // Se adauga listeneri |
---|
| 104 | canvas.addGLEventListener(this); |
---|
| 105 | canvas.addMouseListener(this); |
---|
| 106 | canvas.addMouseMotionListener(this); |
---|
| 107 | canvas.addKeyListener(this); |
---|
| 108 | canvas.addMouseWheelListener(this); |
---|
| 109 | // Se seteaza dimensiunea |
---|
| 110 | canvas.setSize(w, h); |
---|
| 111 | // Se face vizibil |
---|
| 112 | canvas.setVisible(true); |
---|
| 113 | // Se face un Animator pentru animarea canvasului |
---|
| 114 | anim = new Animator(canvas); // needed for constant displying |
---|
| 115 | // Se adauga canvasul pe panel si se seteaza dimensiunea panel-ului. |
---|
| 116 | this.add(canvas); |
---|
| 117 | this.setSize(w, h); |
---|
| 118 | // Se ruleaza animatorul |
---|
| 119 | anim.setRunAsFastAsPossible(true); |
---|
| 120 | anim.start(); |
---|
| 121 | this.zones = new Vector<Point2D.Float>();// vector of mouse clicks |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | * Face initializarea OpenGL. |
---|
| 126 | */ |
---|
| 127 | public void init(GLAutoDrawable glDrawable) { |
---|
| 128 | GL myGL = glDrawable.getGL(); |
---|
| 129 | GLU myGLU = new GLU(); |
---|
| 130 | double width = this.getSize().getWidth(); |
---|
| 131 | double height = this.getSize().getHeight(); |
---|
| 132 | this.high = (float) Math.sqrt((double) ((((width * width) + (height * height))) / 4)); |
---|
| 133 | buildList(glDrawable); // builds all display lists |
---|
| 134 | // Culoarea atmosferei |
---|
| 135 | myGL.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
---|
| 136 | myGL.glShadeModel(GL.GL_FASTEST); |
---|
| 137 | myGL.glMatrixMode(GL.GL_PROJECTION); |
---|
| 138 | // Incarca matricea identitate. |
---|
| 139 | myGL.glLoadIdentity(); |
---|
| 140 | myGLU.gluPerspective(60.0f, (double) width / height, 0.1f, 50000.0f); |
---|
| 141 | myGL.glMatrixMode(GL.GL_MODELVIEW); |
---|
| 142 | myGL.glLoadIdentity(); |
---|
| 143 | // Regleaza camera. |
---|
| 144 | myGLU.gluLookAt(xCamera, yCamera, zCamera, xCamera, yCamera, 0.0, 0.0, 1.0, 0.0); |
---|
| 145 | float maxlong = (float) Math.abs(Globals.maxPoint.getLongitude() - Globals.minPoint.getLongitude()); |
---|
| 146 | float maxlat = (float) Math.abs(Globals.maxPoint.getLatitude() - Globals.minPoint.getLatitude()); |
---|
| 147 | maxlonglat = maxlong > maxlat ? maxlong : maxlat; |
---|
| 148 | |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Deseneaza ruta masinii curente. |
---|
| 153 | * |
---|
| 154 | * @param glDrawable |
---|
| 155 | * Obiectul pe care se deseneaza |
---|
| 156 | */ |
---|
| 157 | public void drawRoute(GL myGL) { |
---|
| 158 | RouteSegment[] tp = null; |
---|
| 159 | // Daca masina curenta este null se revine. |
---|
| 160 | if (currentCar == null) { |
---|
| 161 | return; |
---|
| 162 | } |
---|
| 163 | // Se ia ruta masinii. |
---|
| 164 | synchronized (currentCar) { |
---|
| 165 | tp = currentCar.getRealPos().route; |
---|
| 166 | } |
---|
| 167 | // Daca exista o ruta o vom afisa. |
---|
| 168 | if (tp != null) { |
---|
| 169 | // Primul punct din segment, al doilea punct din segment. |
---|
| 170 | int p1, p2, r; |
---|
| 171 | // Pentru fiecare segment din ruta. |
---|
| 172 | for (int i = 0; i < tp.length; i++) { |
---|
| 173 | // Se preiau datele segmentului. |
---|
| 174 | r = tp[i].roadIndex; |
---|
| 175 | p1 = tp[i].pt1; |
---|
| 176 | p2 = tp[i].pt2; |
---|
| 177 | float x, y, x2, y2; |
---|
| 178 | Point currentPoint; |
---|
| 179 | // Se calculeaza coordonatele pe ecran ale primului punct. |
---|
| 180 | currentPoint = Globals.map.roads.get(r).points.get(p1); |
---|
| 181 | y = (float) GPSutil.getMetersLatitude(Globals.minPoint, currentPoint); |
---|
| 182 | x = (float) GPSutil.getMetersLongitude(Globals.minPoint, currentPoint); |
---|
| 183 | // Se calculeaza cordonatele pe ecran ale celui de-al doilea |
---|
| 184 | // punct |
---|
| 185 | currentPoint = Globals.map.roads.get(r).points.get(p2); |
---|
| 186 | y2 = (float) GPSutil.getMetersLatitude(Globals.minPoint, currentPoint); |
---|
| 187 | x2 = (float) GPSutil.getMetersLongitude(Globals.minPoint, currentPoint); |
---|
| 188 | // Se salveaza configuratia OpenGL curenta. |
---|
| 189 | myGL.glPushMatrix(); |
---|
| 190 | // Se seteaza culoarea de desenare. |
---|
| 191 | myGL.glColor3f(1.0f, 0.5f, 0.0f); |
---|
| 192 | // Se seteaza dimensiunea liniei. |
---|
| 193 | myGL.glLineWidth(2.0f); |
---|
| 194 | // Se deseneaza o linie intre cele 2 puncte de pe ecrn. |
---|
| 195 | myGL.glBegin(GL.GL_LINES); |
---|
| 196 | { |
---|
| 197 | myGL.glVertex3f(x, y, 1.0f); |
---|
| 198 | myGL.glVertex3f(x2, y2, 1.0f); |
---|
| 199 | } |
---|
| 200 | myGL.glEnd(); |
---|
| 201 | // Se revine la configuratia matricii. |
---|
| 202 | myGL.glPopMatrix(); |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | public void display_Semaphores(GL myGL) { |
---|
| 208 | Road currentRoad; |
---|
| 209 | Point currentPoint = null; |
---|
| 210 | float x = 0.0f, y = 0.0f, x2, y2, sinAlfa, cosAlfa, dx, dy; |
---|
| 211 | int laneNr; |
---|
| 212 | int i; |
---|
| 213 | ci = Globals.engine.cars; |
---|
| 214 | currentRoad = null; |
---|
| 215 | // Afiseaza semafoarele |
---|
| 216 | Integer idx; |
---|
| 217 | IntersectionWithTrafficLights tpI; |
---|
| 218 | DirectedRoadSegment drS; |
---|
| 219 | int tempPointIdx; |
---|
| 220 | // Pentru fiecare intersectie cu semafor |
---|
| 221 | for (i = 0; i < Globals.map.lightsIndices.size(); i++) { |
---|
| 222 | // Se preia intersectia respectiva |
---|
| 223 | idx = Globals.map.lightsIndices.get(i); |
---|
| 224 | tpI = (IntersectionWithTrafficLights) Globals.map.allIntersections.get(idx.intValue()); |
---|
| 225 | // Pentru fiecare segment care intra in intersectie |
---|
| 226 | for (int j = 0; j < tpI.segments.size(); j++) { |
---|
| 227 | // drS - Segmentul curent |
---|
| 228 | drS = tpI.segments.get(j); |
---|
| 229 | // Daca este doar iesire se trece la urmatorul segment. |
---|
| 230 | if (drS.isExitOnly()) |
---|
| 231 | continue; |
---|
| 232 | currentRoad = (Road) Globals.map.roads.get(drS.roadIndex); |
---|
| 233 | laneNr = currentRoad.laneNo; |
---|
| 234 | if (drS.direction) { |
---|
| 235 | tempPointIdx = drS.pointIndex + 2; |
---|
| 236 | while (currentRoad.points.size() <= tempPointIdx) { |
---|
| 237 | tempPointIdx--; |
---|
| 238 | } |
---|
| 239 | currentPoint = (Point) currentRoad.points.get(tempPointIdx); |
---|
| 240 | } else { |
---|
| 241 | tempPointIdx = drS.pointIndex - 2; |
---|
| 242 | while (tempPointIdx < 0) { |
---|
| 243 | tempPointIdx++; |
---|
| 244 | } |
---|
| 245 | currentPoint = (Point) currentRoad.points.get(tempPointIdx); |
---|
| 246 | } |
---|
| 247 | // Se salveaza coordonatele punctelor intre care va fi desenat. |
---|
| 248 | // segmentul. |
---|
| 249 | y = (float) GPSutil.getMetersLatitude(Globals.minPoint, currentPoint); |
---|
| 250 | x = (float) GPSutil.getMetersLongitude(Globals.minPoint, currentPoint); |
---|
| 251 | currentPoint = (Point) currentRoad.points.get(drS.pointIndex); |
---|
| 252 | y2 = (float) GPSutil.getMetersLatitude(Globals.minPoint, currentPoint); |
---|
| 253 | x2 = (float) GPSutil.getMetersLongitude(Globals.minPoint, currentPoint); |
---|
| 254 | // Daca punctele nu se suprapun |
---|
| 255 | if ((x2 != x) || (y2 != y)) { |
---|
| 256 | // Semaforul va fi perpendicular pe drum. |
---|
| 257 | sinAlfa = (float) ((y2 - y) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x)))); |
---|
| 258 | cosAlfa = (float) ((x2 - x) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x)))); |
---|
| 259 | // Latimea depinde de numarul de benzi. |
---|
| 260 | dx = (float) (laneNr * 2 * sinAlfa); |
---|
| 261 | dy = (float) (laneNr * 2 * cosAlfa); |
---|
| 262 | x2 = dx + x; |
---|
| 263 | y2 = y - dy; |
---|
| 264 | x = x - dx; |
---|
| 265 | y = y + dy; |
---|
| 266 | // myGL.glPushMatrix(); |
---|
| 267 | // TODO trebuie introdusa tpI.isYellow(int) |
---|
| 268 | // Se regleaza culoarea de desenare. |
---|
| 269 | if (tpI.isGreen(j)) { |
---|
| 270 | myGL.glColor3f(MyColor.color_SemaphoreGreen.getRed(), MyColor.color_SemaphoreGreen.getGreen(), |
---|
| 271 | MyColor.color_SemaphoreGreen.getBlue()); |
---|
| 272 | } else if (tpI.isRed(j)) { |
---|
| 273 | myGL.glColor3f(MyColor.color_SemaphoreRed.getRed(), MyColor.color_SemaphoreRed.getGreen(), |
---|
| 274 | MyColor.color_SemaphoreRed.getBlue()); |
---|
| 275 | } else { |
---|
| 276 | myGL.glColor3f(MyColor.color_SemaphoreYellow.getRed(), |
---|
| 277 | MyColor.color_SemaphoreYellow.getGreen(), MyColor.color_SemaphoreYellow.getBlue()); |
---|
| 278 | } |
---|
| 279 | myGL.glPushMatrix(); |
---|
| 280 | myGL.glLineWidth(2.0f); |
---|
| 281 | // Se deseneaza o linie reprezentand semaforul. |
---|
| 282 | myGL.glBegin(GL.GL_LINES); |
---|
| 283 | { |
---|
| 284 | myGL.glVertex3f(x, y, 1.0f); |
---|
| 285 | myGL.glVertex3f(x2, y2, 1.0f); |
---|
| 286 | } |
---|
| 287 | myGL.glEnd(); |
---|
| 288 | myGL.glPopMatrix(); |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | public void display_Cars(GL myGL) { |
---|
| 295 | CarInfo elem; |
---|
| 296 | double offset; |
---|
| 297 | // Daca trebuie sa afseza rutele si este o masina selectata afiseaza |
---|
| 298 | // ruta. |
---|
| 299 | if (printRoute && currentCar != null) { |
---|
| 300 | drawRoute(myGL); |
---|
| 301 | } |
---|
| 302 | Road currentRoad; |
---|
| 303 | Point p, p2; |
---|
| 304 | float x = 0.0f, y = 0.0f, auxx, auxy, x2, y2, sinAlfa, cosAlfa, dx, dy; |
---|
| 305 | int dir, laneNr, ln; |
---|
| 306 | int i, pt; |
---|
| 307 | carPositions.clear(); |
---|
| 308 | synchronized (ci) { |
---|
| 309 | Iterator<SimulatedCarInfo> it = Globals.engine.cars.iterator(); |
---|
| 310 | // Se ia fiecare masina in parte. |
---|
| 311 | while (it.hasNext()) { |
---|
| 312 | elem = (SimulatedCarInfo) (it.next()); |
---|
| 313 | currentRoad = (Road) Globals.map.roads.get(elem.getRoadIdx()); |
---|
| 314 | pt = elem.getPointIdx(); |
---|
| 315 | offset = elem.getOffset(); |
---|
| 316 | p = (Point) currentRoad.points.get(pt); |
---|
| 317 | y = (float) GPSutil.getMetersLatitude(Globals.minPoint, p); |
---|
| 318 | x = (float) GPSutil.getMetersLongitude(Globals.minPoint, p); |
---|
| 319 | y2 = y; |
---|
| 320 | x2 = x; |
---|
| 321 | pt++; |
---|
| 322 | dir = (int) elem.getDirection(); |
---|
| 323 | try { |
---|
| 324 | p2 = (Point) currentRoad.points.get(pt); |
---|
| 325 | y2 = (float) GPSutil.getMetersLatitude(Globals.minPoint, p2); |
---|
| 326 | x2 = (float) GPSutil.getMetersLongitude(Globals.minPoint, p2); |
---|
| 327 | // Se inverseaza valorile |
---|
| 328 | if (dir == 0) { |
---|
| 329 | auxx = x; |
---|
| 330 | auxy = y; |
---|
| 331 | x = x2; |
---|
| 332 | y = y2; |
---|
| 333 | x2 = auxx; |
---|
| 334 | y2 = auxy; |
---|
| 335 | offset = p2.getDistance() - p.getDistance() - offset; |
---|
| 336 | } |
---|
| 337 | offset = offset * 1000; |
---|
| 338 | laneNr = currentRoad.laneNo; |
---|
| 339 | ln = elem.getLane(); |
---|
| 340 | if ((x2 != x) || (y2 != y)) { |
---|
| 341 | sinAlfa = (float) ((y2 - y) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x)))); |
---|
| 342 | cosAlfa = (float) ((x2 - x) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x)))); |
---|
| 343 | x = x + (float) (cosAlfa * offset); |
---|
| 344 | y = y + (float) (sinAlfa * offset); |
---|
| 345 | dx = (float) (((laneNr + 1 - ln) * 2 - 1) * sinAlfa); |
---|
| 346 | dy = (float) (((laneNr + 1 - ln) * 2 - 1) * cosAlfa); |
---|
| 347 | x = x + dx; |
---|
| 348 | y = y - dy; |
---|
| 349 | //System.out.println("1Car "+elem.getVehicleId()+":"+elem |
---|
| 350 | // +"road:"+currentRoad+"; point "+pt+ |
---|
| 351 | // " gets printed at: x="+x+"y="+y); |
---|
| 352 | // rd++; |
---|
| 353 | } |
---|
| 354 | } catch (Exception e) { |
---|
| 355 | } |
---|
| 356 | carPositions.put(elem, new MyPoint(x, y)); |
---|
| 357 | // Desenez masina in functie de ce tip este. |
---|
| 358 | // System.out.println(elem.getVehicleId()+" : "+x+" : "+y); |
---|
| 359 | myGL.glPushMatrix(); |
---|
| 360 | myGL.glTranslatef(x, y, 0.0f); |
---|
| 361 | myGL.glScalef(0.1f, 0.1f, 0.1f); |
---|
| 362 | if (currentCar != null && currentCar.equals(((SimulatedCarInfo) elem))) |
---|
| 363 | myGL.glCallList(pinkCar); |
---|
| 364 | else { |
---|
| 365 | if (Globals.monitoredCars.indexOf((SimulatedCarInfo) elem) != -1) |
---|
| 366 | myGL.glCallList(traceCar); |
---|
| 367 | else { |
---|
| 368 | CarInstance ci = ((SimulatedCarInfo) elem).getRealPos(); |
---|
| 369 | if (!Globals.comView) { |
---|
| 370 | if (ci.getSpeed() < 0.5) |
---|
| 371 | myGL.glCallList(stoppedCar); |
---|
| 372 | else { |
---|
| 373 | double acc = ci.accel / (3.6 * 3600); |
---|
| 374 | if (acc >= 2) |
---|
| 375 | myGL.glCallList(hAccCar); |
---|
| 376 | else if (acc >= 0) |
---|
| 377 | myGL.glCallList(accCar); |
---|
| 378 | else if (acc <= -3) |
---|
| 379 | myGL.glCallList(hDecCar); |
---|
| 380 | else |
---|
| 381 | myGL.glCallList(decCar); |
---|
| 382 | } |
---|
| 383 | int aux = Globals.engine.crtTime / Globals.SECOND; |
---|
| 384 | if (((SimulatedCarInfo) elem).getSignal() == 1 && ci.canSignal && aux % 2 == 0) { |
---|
| 385 | myGL.glCallList(signalCar); |
---|
| 386 | } |
---|
| 387 | } else { |
---|
| 388 | if (!((SimulatedCarInfo) elem).pulseMessage) |
---|
| 389 | myGL.glCallList(carList); |
---|
| 390 | else if (((SimulatedCarInfo) elem).isPromiscuousMode()) |
---|
| 391 | myGL.glCallList(pinkCar); |
---|
| 392 | else |
---|
| 393 | myGL.glCallList(whiteCar); |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | } |
---|
| 397 | myGL.glPopMatrix(); |
---|
| 398 | if (currentCar != null && currentCar.trafficDB != null |
---|
| 399 | && currentCar.trafficDB.contains((SimulatedCarInfo) elem)) { |
---|
| 400 | myGL.glPushMatrix(); |
---|
| 401 | myGL.glTranslatef(x, y, 0.0f); |
---|
| 402 | myGL.glScalef(0.1f, 0.1f, 0.1f); |
---|
| 403 | myGL.glCallList(neighbor); |
---|
| 404 | myGL.glPopMatrix(); |
---|
| 405 | } |
---|
| 406 | if (currentIntersection != null) { |
---|
| 407 | boolean end = false; |
---|
| 408 | for (int j = 0; j < currentIntersection.segments.size(); j++) { |
---|
| 409 | DirectedRoadSegment seg = currentIntersection.segments.get(j); |
---|
| 410 | synchronized (currentIntersection.DBmutex) { |
---|
| 411 | // Daca nu exista masini trec mai departe. |
---|
| 412 | if (seg.cars == null) { |
---|
| 413 | continue; |
---|
| 414 | } |
---|
| 415 | ArrayList<IntersectionCarRecord> neighbors = seg.cars; |
---|
| 416 | for (i = 0; i < neighbors.size(); i++) { |
---|
| 417 | IntersectionCarRecord icr = (IntersectionCarRecord) neighbors.get(i); |
---|
| 418 | SimulatedCarInfo elem1 = icr.getCar(); |
---|
| 419 | if (elem1.equals(elem)) { |
---|
| 420 | boolean endCar = false; |
---|
| 421 | if (seg.endOfQueue[elem.getLane()].getCar() != null |
---|
| 422 | && seg.endOfQueue[elem.getLane()].equals(icr)) { |
---|
| 423 | endCar = true; |
---|
| 424 | } |
---|
| 425 | myGL.glPushMatrix(); |
---|
| 426 | myGL.glTranslatef(x, y, 0.0f); |
---|
| 427 | myGL.glScalef(0.1f, 0.1f, 0.1f); |
---|
| 428 | if (endCar) { |
---|
| 429 | myGL.glCallList(endOfQueue); |
---|
| 430 | } else { |
---|
| 431 | myGL.glCallList(neighbor); |
---|
| 432 | } |
---|
| 433 | myGL.glPopMatrix(); |
---|
| 434 | end = true; |
---|
| 435 | break; |
---|
| 436 | } |
---|
| 437 | } |
---|
| 438 | } |
---|
| 439 | if (end) |
---|
| 440 | break; |
---|
| 441 | } |
---|
| 442 | } |
---|
| 443 | } |
---|
| 444 | // } |
---|
| 445 | // S-au terminat de desenat masinile |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | public void display_Arrows(GL myGL) { |
---|
| 450 | myGL.glPushMatrix(); |
---|
| 451 | myGL.glTranslatef(xCamera, yCamera, (float) (zCamera - this.high)); |
---|
| 452 | myGL.glCallList(arrows); |
---|
| 453 | myGL.glPopMatrix(); |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | public void display_Legend(GL myGL) { |
---|
| 457 | float raport, len, scale; |
---|
| 458 | String legend; |
---|
| 459 | raport = (float) (this.high / zCamera); |
---|
| 460 | myGL.glPushMatrix(); |
---|
| 461 | float x2 = (float) (xCamera - (((this.getSize().getWidth() * zCamera) / this.high) / 2)); |
---|
| 462 | x2 = x2 + (float) ((((this.getSize().getWidth() - 100) * zCamera) / this.high)); |
---|
| 463 | float y2 = (float) (yCamera - (((this.getSize().getHeight() * zCamera) / this.high) / 2)); |
---|
| 464 | y2 = y2 + (float) ((100 * zCamera) / this.high); |
---|
| 465 | myGL.glTranslatef(x2, y2, 2.0f); |
---|
| 466 | myGL.glColor3f(1.0f, 0.0f, 0.0f); |
---|
| 467 | myGL.glLineWidth(4.0f); |
---|
| 468 | len = 0.0f; |
---|
| 469 | legend = null; |
---|
| 470 | if ((10000 * raport) < (this.getSize().width / 3)) { |
---|
| 471 | len = 10000.0f; |
---|
| 472 | legend = new String("10 Km"); |
---|
| 473 | } else { |
---|
| 474 | if ((1000 * raport) < (this.getSize().width / 3)) { |
---|
| 475 | len = 1000.0f; |
---|
| 476 | legend = new String("1 Km"); |
---|
| 477 | } else { |
---|
| 478 | if ((100 * raport) < (this.getSize().width / 3)) { |
---|
| 479 | len = 100.0f; |
---|
| 480 | legend = new String("100 m"); |
---|
| 481 | } else { |
---|
| 482 | if ((10 * raport) < (this.getSize().width / 3)) { |
---|
| 483 | len = 10.0f; |
---|
| 484 | legend = new String("10 m"); |
---|
| 485 | } else { |
---|
| 486 | len = 1.0f; |
---|
| 487 | legend = new String("1 m"); |
---|
| 488 | } |
---|
| 489 | } |
---|
| 490 | } |
---|
| 491 | } |
---|
| 492 | // Afisez legenda |
---|
| 493 | GLUT myGLUT = new GLUT(); |
---|
| 494 | scale = myGLUT.glutStrokeLengthf(GLUT.STROKE_ROMAN, legend); |
---|
| 495 | scale = (float) ((50.0f) / (raport * scale)); |
---|
| 496 | myGL.glBegin(GL.GL_LINES); |
---|
| 497 | { |
---|
| 498 | myGL.glVertex3f(0.0f, 0.0f, 0.0f); |
---|
| 499 | myGL.glVertex3f(0.0f - len, 0.0f, 0.0f); |
---|
| 500 | } |
---|
| 501 | myGL.glEnd(); |
---|
| 502 | myGL.glPushMatrix(); |
---|
| 503 | myGL.glScalef(scale, scale, scale); |
---|
| 504 | myGLUT.glutStrokeString(GLUT.STROKE_ROMAN, legend); |
---|
| 505 | myGL.glPopMatrix(); |
---|
| 506 | myGL.glPopMatrix(); |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | /** |
---|
| 510 | * Metoda principala de desenare. |
---|
| 511 | */ |
---|
| 512 | public abstract void display(GLAutoDrawable glDrawable); |
---|
| 513 | |
---|
| 514 | public void reshape(GLAutoDrawable glDrawable, int i, int i1, int i2, int i3) { |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | /** |
---|
| 518 | * |
---|
| 519 | * @param glDrawable |
---|
| 520 | */ |
---|
| 521 | public abstract void buildList(GLAutoDrawable glDrawable); |
---|
| 522 | |
---|
| 523 | /** |
---|
| 524 | * |
---|
| 525 | */ |
---|
| 526 | public void displayChanged(GLAutoDrawable glDrawable, boolean b, boolean b1) { |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | /** |
---|
| 530 | * |
---|
| 531 | */ |
---|
| 532 | public void zoomIn() { |
---|
| 533 | if (zCamera > 50) { |
---|
| 534 | zCamera = (zCamera * 9) / 10; |
---|
| 535 | } |
---|
| 536 | canvas.repaint(); |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | /** |
---|
| 540 | * |
---|
| 541 | */ |
---|
| 542 | public void zoomOut() { |
---|
| 543 | if (zCamera < 100000.0f * maxlonglat) { |
---|
| 544 | zCamera = (zCamera * 12) / 10; |
---|
| 545 | }else{ |
---|
| 546 | zCamera = 100000.0f * (float) maxlonglat; |
---|
| 547 | } |
---|
| 548 | canvas.repaint(); |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | /** |
---|
| 552 | * |
---|
| 553 | */ |
---|
| 554 | public void goLeft() { |
---|
| 555 | xCamera = xCamera - zCamera / 10; |
---|
| 556 | canvas.repaint(); |
---|
| 557 | } |
---|
| 558 | |
---|
| 559 | /** |
---|
| 560 | * |
---|
| 561 | */ |
---|
| 562 | public void goRight() { |
---|
| 563 | xCamera = xCamera + zCamera / 10; |
---|
| 564 | canvas.repaint(); |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | /** |
---|
| 568 | * |
---|
| 569 | */ |
---|
| 570 | public void goUp() { |
---|
| 571 | yCamera = yCamera + zCamera / 10; |
---|
| 572 | canvas.repaint(); |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | /** |
---|
| 576 | * |
---|
| 577 | */ |
---|
| 578 | public void goDown() { |
---|
| 579 | yCamera = yCamera - zCamera / 10; |
---|
| 580 | canvas.repaint(); |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | /** |
---|
| 584 | * Se face zoom modificand coordonata z a camerei corespunzator. |
---|
| 585 | * |
---|
| 586 | * @param p1 |
---|
| 587 | * @param p2 |
---|
| 588 | */ |
---|
| 589 | void doZoom(Point2D.Float p1, Point2D.Float p2) { |
---|
| 590 | double worldX1, worldY1, worldX2, worldY2, temp; |
---|
| 591 | boolean zoomout = true; |
---|
| 592 | float w, h; |
---|
| 593 | float winX = p1.x; |
---|
| 594 | float winY = (float) this.getSize().height - (float) p1.y; |
---|
| 595 | w = (float) (this.getSize().width * zCamera) / high; |
---|
| 596 | h = (float) (this.getSize().height * zCamera) / high; |
---|
| 597 | worldX1 = xCamera - (float) (w / 2) + (winX * zCamera) / high; |
---|
| 598 | worldY1 = yCamera - (float) (h / 2) + (winY * zCamera) / high; |
---|
| 599 | winX = p2.x; |
---|
| 600 | winY = (float) this.getSize().height - (float) p2.y; |
---|
| 601 | worldX2 = xCamera - (float) (w / 2) + (winX * zCamera) / high; |
---|
| 602 | worldY2 = yCamera - (float) (h / 2) + (winY * zCamera) / high; |
---|
| 603 | if (worldX1 > worldX2) { |
---|
| 604 | temp = worldX1; |
---|
| 605 | worldX1 = worldX2; |
---|
| 606 | worldX2 = temp; |
---|
| 607 | } |
---|
| 608 | if (worldY1 > worldY2) { |
---|
| 609 | zoomout = false; |
---|
| 610 | temp = worldY1; |
---|
| 611 | worldY1 = worldY2; |
---|
| 612 | worldY2 = temp; |
---|
| 613 | } |
---|
| 614 | xCamera = (float) (((worldX2 - worldX1) / 2) + worldX1); |
---|
| 615 | yCamera = (float) (((worldY2 - worldY1) / 2) + worldY1); |
---|
| 616 | float wM, hM, diffW, diffH; |
---|
| 617 | wM = (float) (worldX2 - worldX1); |
---|
| 618 | hM = (float) (worldY2 - worldY1); |
---|
| 619 | diffW = ((this.getSize().width * zCamera) / high) - wM; |
---|
| 620 | diffH = ((this.getSize().height * zCamera) / high) - hM; |
---|
| 621 | if (!zoomout) { |
---|
| 622 | if (diffW < diffH) { |
---|
| 623 | zCamera = (high * wM) / this.getSize().width; |
---|
| 624 | } else { |
---|
| 625 | zCamera = (high * hM) / this.getSize().height; |
---|
| 626 | } |
---|
| 627 | } else { |
---|
| 628 | if (diffW < diffH) { |
---|
| 629 | zCamera = zCamera + 15 * (zCamera * (diffH)) / this.getSize().height; |
---|
| 630 | } else { |
---|
| 631 | zCamera = zCamera + 15 * (zCamera * (diffW)) / this.getSize().width; |
---|
| 632 | } |
---|
| 633 | } |
---|
| 634 | if (zCamera < 50.0f) { |
---|
| 635 | zCamera = 50.0f; |
---|
| 636 | } |
---|
| 637 | if (zCamera > 100000.0f * maxlonglat) { |
---|
| 638 | zCamera = 100000.0f * (float) maxlonglat; |
---|
| 639 | } |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | /** |
---|
| 643 | * |
---|
| 644 | */ |
---|
| 645 | public void mouseEntered(MouseEvent e) { |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | /** |
---|
| 649 | * |
---|
| 650 | */ |
---|
| 651 | public void mouseExited(MouseEvent e) { |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | /** |
---|
| 655 | * Cand este apasat un buton de mouse. |
---|
| 656 | */ |
---|
| 657 | public void mousePressed(MouseEvent e) { |
---|
| 658 | this.pressX = e.getX(); |
---|
| 659 | this.pressY = e.getY(); |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | /** |
---|
| 663 | * Cand este eliberat butonul de mouse. |
---|
| 664 | */ |
---|
| 665 | public void mouseReleased(MouseEvent e) { |
---|
| 666 | this.releaseX = e.getX(); |
---|
| 667 | this.releaseY = e.getY(); |
---|
| 668 | if ((this.pressX != this.releaseX) || (this.pressY != this.releaseY)) { |
---|
| 669 | if ((this.releaseX > 30) && (this.releaseX < (this.getSize().getWidth() - 30)) && (this.releaseY > 60) |
---|
| 670 | && (this.releaseY < (this.getSize().getHeight() - 60))) { |
---|
| 671 | doZoom(new Point2D.Float(pressX, pressY), new Point2D.Float(releaseX, releaseY)); |
---|
| 672 | } |
---|
| 673 | } |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | public void selection(MouseEvent e) { |
---|
| 677 | |
---|
| 678 | } |
---|
| 679 | |
---|
| 680 | /** |
---|
| 681 | * |
---|
| 682 | */ |
---|
| 683 | public void mouseClicked(MouseEvent e) { |
---|
| 684 | // isDragged = true; |
---|
| 685 | float x = e.getX(); |
---|
| 686 | float y = e.getY(); |
---|
| 687 | if ((x < 30) && (y < 60)) { |
---|
| 688 | goUp(); |
---|
| 689 | goLeft(); |
---|
| 690 | return; |
---|
| 691 | } |
---|
| 692 | if ((this.getSize().getWidth() - 30 < x) && (y < 60)) { |
---|
| 693 | goRight(); |
---|
| 694 | goUp(); |
---|
| 695 | return; |
---|
| 696 | } |
---|
| 697 | if ((x < 30) && (y > this.getSize().getHeight() - 60)) { |
---|
| 698 | goLeft(); |
---|
| 699 | goDown(); |
---|
| 700 | return; |
---|
| 701 | } |
---|
| 702 | if ((this.getSize().getWidth() - 30 < x) && (y > this.getSize().getHeight() - 60)) { |
---|
| 703 | goDown(); |
---|
| 704 | goRight(); |
---|
| 705 | return; |
---|
| 706 | } |
---|
| 707 | if (this.getSize().getWidth() - 30 < x) { |
---|
| 708 | goRight(); |
---|
| 709 | return; |
---|
| 710 | } |
---|
| 711 | if (x < 30) { |
---|
| 712 | goLeft(); |
---|
| 713 | return; |
---|
| 714 | } |
---|
| 715 | if (y < 60) { |
---|
| 716 | goUp(); |
---|
| 717 | return; |
---|
| 718 | } |
---|
| 719 | if (y > this.getSize().getHeight() - 60) { |
---|
| 720 | goDown(); |
---|
| 721 | return; |
---|
| 722 | } |
---|
| 723 | selection(e); |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | /** |
---|
| 727 | * |
---|
| 728 | * @param e |
---|
| 729 | */ |
---|
| 730 | public void mouseDragged(MouseEvent e) { |
---|
| 731 | } |
---|
| 732 | |
---|
| 733 | /** |
---|
| 734 | * |
---|
| 735 | * @param e |
---|
| 736 | */ |
---|
| 737 | public void mouseMoved(MouseEvent e) { |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | /** |
---|
| 741 | * Petroaca |
---|
| 742 | * |
---|
| 743 | * @param elem |
---|
| 744 | * @param myGL |
---|
| 745 | */ |
---|
| 746 | public void drawDSRCCar(CarInfo elem, GL myGL) { |
---|
| 747 | Road currentRoad; |
---|
| 748 | Point p, p2; |
---|
| 749 | float x = 0.0f, y = 0.0f, auxx, auxy, x2, y2, sinAlfa, cosAlfa, dx, dy; |
---|
| 750 | int dir, laneNr, ln; |
---|
| 751 | int pt; |
---|
| 752 | double offset; |
---|
| 753 | currentRoad = (Road) Globals.map.roads.get(elem.getRoadIdx()); |
---|
| 754 | pt = elem.getPointIdx(); |
---|
| 755 | offset = elem.getOffset(); |
---|
| 756 | p = (Point) currentRoad.points.get(pt); |
---|
| 757 | y = (float) GPSutil.getMetersLatitude(Globals.minPoint, p); |
---|
| 758 | x = (float) GPSutil.getMetersLongitude(Globals.minPoint, p); |
---|
| 759 | y2 = y; |
---|
| 760 | x2 = x; |
---|
| 761 | pt++; |
---|
| 762 | dir = (int) elem.getDirection(); |
---|
| 763 | try { |
---|
| 764 | p2 = (Point) currentRoad.points.get(pt); |
---|
| 765 | y2 = (float) GPSutil.getMetersLatitude(Globals.minPoint, p2); |
---|
| 766 | x2 = (float) GPSutil.getMetersLongitude(Globals.minPoint, p2); |
---|
| 767 | if (dir == 0) { |
---|
| 768 | auxx = x; |
---|
| 769 | auxy = y; |
---|
| 770 | x = x2; |
---|
| 771 | y = y2; |
---|
| 772 | x2 = auxx; |
---|
| 773 | y2 = auxy; |
---|
| 774 | offset = p2.getDistance() - p.getDistance() - offset; |
---|
| 775 | } |
---|
| 776 | offset = offset * 1000; |
---|
| 777 | laneNr = currentRoad.laneNo; |
---|
| 778 | ln = elem.getLane(); |
---|
| 779 | if ((x2 != x) || (y2 != y)) { |
---|
| 780 | sinAlfa = (float) ((y2 - y) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x)))); |
---|
| 781 | cosAlfa = (float) ((x2 - x) / (Math.sqrt((y2 - y) * (y2 - y) + (x2 - x) * (x2 - x)))); |
---|
| 782 | x = x + (float) (cosAlfa * offset); |
---|
| 783 | y = y + (float) (sinAlfa * offset); |
---|
| 784 | dx = (float) (((laneNr + 1 - ln) * 2 - 1) * sinAlfa); |
---|
| 785 | dy = (float) (((laneNr + 1 - ln) * 2 - 1) * cosAlfa); |
---|
| 786 | x = x + dx; |
---|
| 787 | y = y - dy; |
---|
| 788 | } |
---|
| 789 | } catch (Exception e) { |
---|
| 790 | |
---|
| 791 | } |
---|
| 792 | myGL.glPushMatrix(); |
---|
| 793 | { |
---|
| 794 | myGL.glTranslatef(x, y, 0.0f); |
---|
| 795 | myGL.glScalef(0.1f, 0.1f, 0.1f); |
---|
| 796 | myGL.glCallList(DSRCapp); |
---|
| 797 | } |
---|
| 798 | myGL.glPopMatrix(); |
---|
| 799 | } |
---|
| 800 | |
---|
| 801 | public Point recoverRoad(float x, float y) { |
---|
| 802 | float y2 = (float) GPSutil.getMetersLatitude(Globals.minPoint, Globals.maxPoint); |
---|
| 803 | float x2 = (float) GPSutil.getMetersLongitude(Globals.minPoint, Globals.maxPoint); |
---|
| 804 | maxX = x2; |
---|
| 805 | maxY = y2; |
---|
| 806 | Point currentPoint; |
---|
| 807 | double l, L; |
---|
| 808 | l = Globals.map.maxPoint.getLongitude() - Globals.map.minPoint.getLongitude(); |
---|
| 809 | l = (double) l / maxX; |
---|
| 810 | l = (double) (x * l + Globals.map.minPoint.getLongitude()); |
---|
| 811 | L = Globals.map.maxPoint.getLatitude() - Globals.map.minPoint.getLatitude(); |
---|
| 812 | L = (double) L / maxY; |
---|
| 813 | L = (double) (y * L + Globals.map.minPoint.getLatitude()); |
---|
| 814 | currentPoint = new Point(l, L); |
---|
| 815 | return currentPoint; |
---|
| 816 | } |
---|
| 817 | |
---|
| 818 | public void keyPressed(KeyEvent e) { |
---|
| 819 | // TODO Auto-generated method stub |
---|
| 820 | if (e.getKeyCode() == KeyEvent.VK_UP) { |
---|
| 821 | goUp(); |
---|
| 822 | } |
---|
| 823 | if (e.getKeyCode() == KeyEvent.VK_DOWN) { |
---|
| 824 | goDown(); |
---|
| 825 | } |
---|
| 826 | if (e.getKeyCode() == KeyEvent.VK_LEFT) { |
---|
| 827 | goLeft(); |
---|
| 828 | } |
---|
| 829 | if (e.getKeyCode() == KeyEvent.VK_RIGHT) { |
---|
| 830 | goRight(); |
---|
| 831 | } |
---|
| 832 | if (e.getKeyCode() == KeyEvent.VK_EQUALS) { |
---|
| 833 | zoomIn(); |
---|
| 834 | } |
---|
| 835 | if (e.getKeyCode() == KeyEvent.VK_MINUS) { |
---|
| 836 | zoomOut(); |
---|
| 837 | } |
---|
| 838 | } |
---|
| 839 | |
---|
| 840 | public void keyReleased(KeyEvent e) { |
---|
| 841 | // TODO Auto-generated method stub |
---|
| 842 | } |
---|
| 843 | |
---|
| 844 | public void keyTyped(KeyEvent e) { |
---|
| 845 | // TODO Auto-generated method stub |
---|
| 846 | } |
---|
| 847 | |
---|
| 848 | public void mouseWheelMoved(MouseWheelEvent e) { |
---|
| 849 | // TODO Auto-generated method stub |
---|
| 850 | if (e.getWheelRotation()>0){ |
---|
| 851 | zoomIn(); |
---|
| 852 | } |
---|
| 853 | if (e.getWheelRotation()<0){ |
---|
| 854 | zoomOut(); |
---|
| 855 | } |
---|
| 856 | } |
---|
| 857 | } |
---|