/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.gui.selector; import vnsim.gui.ListBuilder; import vnsim.gui.MapViewPanel; import vnsim.map.object.*; import vnsim.map.utils.*; import vnsim.vehicular.scenarios.Route; import vnsim.vehicular.simulator.Location; import vnsim.vehicular.simulator.RouteSegment; import java.util.ArrayList; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLDrawable; import javax.media.opengl.glu.GLU; import com.sun.opengl.util.Animator; import com.sun.opengl.util.GLUT; public class MapPanel extends MapViewPanel { /** * */ private static final long serialVersionUID = -5883719321862303634L; public Map myMap; public Animator anim; Route currentRoute = null; EntryExitConfig eec; public Location start, stop; public ArrayList routePoints = null; public MapPanel(int w, int h, Map map) { super(null, w, h); this.myMap = map; Globals.minPoint = myMap.minPoint; Globals.maxPoint = myMap.maxPoint; } public void display(GLAutoDrawable glDrawable) { Road currentRoad; Point currentPoint = null; float x = 0.0f, y = 0.0f, x2, y2; int i; GL myGL = glDrawable.getGL(); GLU myGLU = new GLU(); myGL.glClear(GL.GL_COLOR_BUFFER_BIT); myGL.glMatrixMode(GL.GL_MODELVIEW); myGL.glLoadIdentity(); myGLU.gluLookAt(xCamera, yCamera, zCamera, xCamera, yCamera, 0.0, 0.0, 1.0, 0.0); myGL.glCallList(mapList); // displays the map // depending on the camera position displays one level of text if (zCamera < 800) { myGL.glCallList(mapLevel[4]); } if ((zCamera >= 800) && (zCamera < 1600)) { myGL.glCallList(mapLevel[3]); } if ((zCamera >= 1600) && (zCamera < 2800)) { myGL.glCallList(mapLevel[2]); } if ((zCamera >= 2800) && (zCamera < 3700)) { myGL.glCallList(mapLevel[1]); } if (zCamera > 3700) { myGL.glCallList(mapLevel[0]); } currentRoad = null; if (currentRoute != null) { RouteSegment rs; int pt1, pt2; for (i = 0; i < currentRoute.route.size(); i++) { rs = currentRoute.route.get(i); myGL.glPushMatrix(); myGL.glLineWidth(3.0f); myGL.glColor3f(1.0f, 0.0f, 0.0f); if (rs.pt1 < rs.pt2) { pt1 = rs.pt1; pt2 = rs.pt2; } else { pt2 = rs.pt1; pt1 = rs.pt2; } currentRoad = myMap.roads.get(rs.roadIndex); currentPoint = currentRoad.points.get(pt1); y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint); x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint); for (int j = pt1 + 1; j <= pt2; j++) { currentPoint = currentRoad.points.get(j); y2 = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint); x2 = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint); myGL.glBegin(GL.GL_LINES); myGL.glVertex3f(x, y, 1.0f); myGL.glVertex3f(x2, y2, 1.0f); myGL.glEnd(); y = y2; x = x2; } myGL.glPopMatrix(); } } if (start != null) { currentRoad = this.myMap.roads.get(start.roadIdx); currentPoint = currentRoad.points.get(start.ptIdx); y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint); x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint); myGL.glPushMatrix(); { myGL.glLineWidth(3.0f); myGL.glTranslatef(x, y, 0.0f); myGL.glColor3f(1.0f, 1.0f, 1.0f); myGL.glBegin(GL.GL_POLYGON); { myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); } myGL.glEnd(); } myGL.glPopMatrix(); } if (stop != null) { currentRoad = this.myMap.roads.get(stop.roadIdx); currentPoint = currentRoad.points.get(stop.ptIdx); y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint); x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint); myGL.glPushMatrix(); { myGL.glLineWidth(3.0f); myGL.glTranslatef(x, y, 0.0f); myGL.glColor3f(0.0f, 0.0f, 0.0f); myGL.glBegin(GL.GL_POLYGON); { myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), -20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); myGL.glVertex3f(20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); myGL.glVertex3f(-20.0f*zCamera/(100000.0f * (float) maxlonglat), 20.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); } myGL.glEnd(); } } myGL.glPopMatrix(); display_Arrows(myGL); display_Legend(myGL); synchronized (Globals.mutex) { Globals.flag = 0; Globals.mutex.notify(); } } public void reshape(GLDrawable glDrawable, int i, int i1, int i2, int i3) { } public void buildList(GLAutoDrawable glDrawable) { GL myGL = glDrawable.getGL(); GLUT glut = new GLUT(); GLU myGLU = new GLU(); prioritySign = myGL.glGenLists(12); this.mapLevel = new int[5]; ListBuilder.buildList_PrioritySign(myGL, myGLU, glut, prioritySign); noPrioritySign = prioritySign + 1; ListBuilder.buildList_NoPrioritySign(myGL, myGLU, glut, noPrioritySign); stopSign = noPrioritySign + 1; ListBuilder.buildList_StopSign(myGL, myGLU, glut, stopSign); mapList = stopSign + 1; ListBuilder.buildList_MapList(myGL, myGLU, glut, mapList, prioritySign, stopSign, noPrioritySign, myMap.minPoint, myMap.maxPoint); float y2 = (float) GPSutil.getMetersLatitude(myMap.minPoint, myMap.maxPoint); float x2 = (float) GPSutil.getMetersLongitude(myMap.minPoint, myMap.maxPoint); xCamera = x2 / 2; yCamera = y2 / 2; zCamera = (float) ((x2 * high) / this.getWidth()); carList = mapList + 1; ListBuilder.buildList_CarList(myGL, myGLU, glut, carList); carListOut = carList + 1; ListBuilder.buildList_CarListOut(myGL, myGLU, glut, carListOut); neighbor = carListOut + 1; ListBuilder.buildList_Neighbor(myGL, myGLU, glut, neighbor); arrows = neighbor + 1; ListBuilder.buildList_Arrows(myGL, myGLU, glut, arrows, this.getWidth(), this.getHeight(), 60); mapLevel[0] = arrows + 1; ListBuilder.buildList_MapLevels(myGL, myGLU, glut, mapLevel, myMap.minPoint); } public void selection(float x, float y) { // Gets the world coordinates of the mouse click float winX = x; float winY = (float) this.getSize().height - (float) y; float w = (float) (this.getSize().width * zCamera) / high; float h = (float) (this.getSize().height * zCamera) / high; float worldX = xCamera - (float) (w / 2) + (winX * zCamera) / high; float worldY = yCamera - (float) (h / 2) + (winY * zCamera) / high; recoverRoad((float) worldX, (float) worldY); Point recovered = recoverRoad((float) worldX, (float) worldY); System.out.println("Selectie " + recovered.getLatitude()); if (recovered != null) { PeanoKey pk = Globals.map.findClosestPeanoKey(recovered); if (pk != null) { Road r = (Road) Globals.map.roads.get(pk.getRoadIndex()); Point px = r.points.get(pk.getPointIndex()); System.out.println("Click on road id: " + pk.getRoadIndex() + ";" + ((Road) Globals.map.roads.get(pk.getRoadIndex())).getName() + " la punctul " + pk.getPointIndex() + ": " + px); } } else { // no road found } } public void setRoute(Route r) { this.currentRoute = r; } public void setStart(Location start) { this.start = start; } public void setStop(Location stop) { this.stop = stop; } }