/************************************************************************************ * 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 vnsim.vehicular.simulator.intersections.DirectedRoadSegment; import vnsim.vehicular.simulator.intersections.Intersection; import java.awt.event.MouseEvent; 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 RouteDraw extends MapViewPanel { private static final long serialVersionUID = -5883719321862303634L; public Map myMap; public Animator anim; Route currentRoute = null; int width; int height; public Location start = null, stop = null; public ArrayList routePoints = null; public ArrayList entrys = null; public ArrayList exits = null; public Location currentLocation = null; public boolean isRouteEditing = false; public boolean isRouteShowing = false; public Road selectedRoad = null; public Road lastRoad = null; public Intersection lastIntersection = null; public boolean isRoad = true; RouteConfiguration rc; public RouteDraw(RouteConfiguration rc, int w, int h, Map map) { super(null, w, h); this.rc = rc; this.myMap = map; this.width = w; this.height = h; Globals.map = myMap; Globals.minPoint = myMap.minPoint; Globals.maxPoint = myMap.maxPoint; entrys = new ArrayList(); exits = new ArrayList(); } public void display(GLAutoDrawable glDrawable) { Road currentRoad; Point currentPoint = null; float x = 0.0f, y = 0.0f; 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]); } if ((isRoad) && (lastRoad != null) && (isRouteEditing)) { float x2, y2; myGL.glPushMatrix(); myGL.glLineWidth(3.0f); myGL.glColor3f(0.0f, 0.0f, 1.0f); currentPoint = lastRoad.points.get(0); y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint); x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint); for (int j = 1; j < lastRoad.points.size(); j++) { currentPoint = lastRoad.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(); } else if ((lastIntersection != null) && (isRouteEditing)) { for (int i = 0; i < lastIntersection.segments.size(); i++) { Road myCurrentRoad = lastIntersection.segments.get(i).road; float x2, y2; myGL.glPushMatrix(); myGL.glLineWidth(3.0f); myGL.glColor3f(0.0f, 0.0f, 1.0f); currentPoint = myCurrentRoad.points.get(0); y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint); x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint); for (int j = 1; j < myCurrentRoad.points.size(); j++) { currentPoint = myCurrentRoad.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 (selectedRoad != null) { float x2, y2; myGL.glPushMatrix(); myGL.glLineWidth(3.0f); myGL.glColor3f(0.0f, 1.0f, 0.0f); currentPoint = selectedRoad.points.get(0); y = (float) GPSutil.getMetersLatitude(myMap.minPoint, currentPoint); x = (float) GPSutil.getMetersLongitude(myMap.minPoint, currentPoint); for (int j = 1; j < selectedRoad.points.size(); j++) { currentPoint = selectedRoad.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 (currentRoute != null) { RouteSegment rs; int pt1, pt2, i; float x2, y2; 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 (isRouteEditing || isRouteShowing) { if (routePoints != null) { for (int i = 1; i < routePoints.size(); i++) { currentRoad = this.myMap.roads.get(routePoints.get(i).roadIdx); currentPoint = currentRoad.points.get(routePoints.get(i).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, 0.0f, 0.0f); myGL.glBegin(GL.GL_POLYGON); { myGL.glVertex3f(-10.0f*zCamera/(100000.0f * (float) maxlonglat), -10.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); myGL.glVertex3f(10.0f*zCamera/(100000.0f * (float) maxlonglat), -10.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); myGL.glVertex3f(10.0f*zCamera/(100000.0f * (float) maxlonglat), 10.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); myGL.glVertex3f(-10.0f*zCamera/(100000.0f * (float) maxlonglat), 10.0f*zCamera/(100000.0f * (float) maxlonglat), 1.0f); } myGL.glEnd(); } 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) / width); arrows = mapList + 1; ListBuilder.buildList_Arrows(myGL, myGLU, glut, arrows, width, height, 18); mapLevel[0] = arrows + 1; ListBuilder.buildList_MapLevels(myGL, myGLU, glut, mapLevel, myMap.minPoint); } private Intersection toIntersectionPoint(Location l) { for (int i = 0; i < Globals.map.allIntersections.size(); i++) { for (int j = 0; j < Globals.map.allIntersections.get(i).segments.size(); j++) { DirectedRoadSegment drs = Globals.map.allIntersections.get(i).segments.get(j); if ((drs.roadIndex == l.roadIdx) && (Math.abs(drs.pointIndex - l.ptIdx) < 5)) { l.ptIdx = drs.pointIndex; return Globals.map.allIntersections.get(i); } } } return null; } public void selection(MouseEvent e) { // Gets the world coordinates of the mouse click float winX = e.getX(); float winY = (float) this.getSize().height - (float) e.getY(); 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; Point recovered = recoverRoad((float) worldX, (float) worldY); 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()); rc.selectedroad.setText(((Road) Globals.map.roads.get(pk.getRoadIndex())).getName().trim()); rc.selectedpoint.setText(String.valueOf(pk.getPointIndex()).trim()); System.out.println("Click on road id: " + pk.getRoadIndex() + ";" + ((Road) Globals.map.roads.get(pk.getRoadIndex())).getName() + " la punctul " + pk.getPointIndex() + ": " + px); currentLocation = new Location(pk.getRoadIndex(), pk.getPointIndex()); if (isRouteEditing) { if (routePoints.size() % 2 == 0) { Intersection intersectie = toIntersectionPoint(currentLocation); if ((intersectie != null)&&(intersectie==lastIntersection)&&(Globals.map.roads.get(currentLocation.roadIdx)!=lastRoad)) { lastRoad = Globals.map.roads.get(currentLocation.roadIdx); routePoints.add(currentLocation); isRoad = true; System.err.println("Adaug 1"); } else { System.err.println("Invalid point! Place the point near the last intersection"); } } else { if (routePoints.get(routePoints.size() - 1).roadIdx == currentLocation.roadIdx) { lastIntersection = toIntersectionPoint(currentLocation); if (lastIntersection != null) { routePoints.add(currentLocation); isRoad = false; System.err.println("Adaug 2"); currentRoute.route.add(new RouteSegment((short) currentLocation.roadIdx, (short) routePoints.get(routePoints.size() - 2).ptIdx, (short) currentLocation.ptIdx)); } else { System.err.println("Invalid point! Place the point near an intersection"); } } else { System.err.println("Invalid point! Place another point at the end of the last road."); } } } } else { rc.selectedroad.setText(""); rc.selectedPoint.setText(""); currentLocation = null; } } else { // no road found rc.selectedroad.setText(""); rc.selectedpoint.setText(""); currentLocation = null; } } public void mouseMoved(MouseEvent e) { float winX = e.getX(); float winY = (float) this.getSize().height - (float) e.getY(); 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); if (recovered != null) { PeanoKey pk = Globals.map.findClosestPeanoKey(recovered); if (pk != null) { Road r = (Road) Globals.map.roads.get(pk.getRoadIndex()); rc.currentroad.setText(((Road) Globals.map.roads.get(pk.getRoadIndex())).getName().trim()); rc.currentpoint.setText(String.valueOf(pk.getPointIndex()).trim()); selectedRoad = r; } else { selectedRoad = null; } } } public void setRoute(Route r) { this.currentRoute = r; } public void setStart(Location start) { this.start = start; isRoad = true; this.lastRoad = Globals.map.roads.get(start.roadIdx); } public void setStop(Location stop) { this.stop = stop; } public void setSize(int width, int height) { super.setSize(width, height); } }