/************************************************************************************ * 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.vehicular.simulator; import java.util.*; import vnsim.map.object.*; public class RouteSegment implements java.io.Serializable { //a segment, identified by a road index and two point indexes //(the order of the point indexes does matter - there are two possible ways //to look at the same segment, depending on the direction) /** serialVersionUID */ private static final long serialVersionUID = -5883719321862303634L; public short roadIndex; public short pt1; public short pt2; //pt1 and pt2 are point indexes on the road 'roadIndex'; they can be in any order //(pt1>pt2 or pt1=pt2) { for(int i=pt2;i<=pt1;i++) { Location test=new Location(roadIndex,i); if(test.equals(loc)) return true; } } else { for(int i=pt1;i<=pt2;i++) { Location test=new Location(roadIndex,i); if(test.equals(loc)) return true; } } return false; } public boolean checkOrder(Location l1, Location l2) { //checks whether l1 and l2 appear in the current route segment, in this order int state=0; //0=none found; 1=l1 found if(l1.roadIdx==l2.roadIdx && l1.roadIdx==this.roadIndex) { if(pt1>=pt2) { if(pt1>=l1.ptIdx && l1.ptIdx>=l2.ptIdx && l2.ptIdx>=pt2) return true; return false; } else { if(pt1<=l1.ptIdx && l1.ptIdx<=l2.ptIdx && l2.ptIdx<=pt2) return true; return false; } } if(l1.equals(l2)) return this.contains(l1); // else check each point if(pt1>=pt2) { for(int i=pt1;i>=pt2;i--) { Location test=new Location(roadIndex,i); if(state==0) { if(test.equals(l2)) return false; if(test.equals(l1)) state=1; } else { if(test.equals(l2)) return true; } } } else { for(int i=pt1;i<=pt2;i++) { Location test=new Location(roadIndex,i); if(state==0) { if(test.equals(l2)) return false; if(test.equals(l1)) state=1; } else { if(test.equals(l2)) return true; } } } return false; } public double distanceBetween(Location loc) { //assume loc is inside the current route segment; get the distance from loc //to the end of the current route segment; //return -1.0 on error; Road r=(Road) Globals.map.roads.get(roadIndex); if(loc.roadIdx==this.roadIndex) { if(pt1>=pt2) { return ((r.points.get(loc.ptIdx))).getDistance() - ((r.points.get(pt2))).getDistance(); } else { return ((Point)(r.points.get(pt2))).getDistance() - ((Point)(r.points.get(loc.ptIdx))).getDistance(); } } //else check each point if(pt1>=pt2) { for(int i=pt2;i<=pt1;i++) { Location test=new Location(roadIndex,i); if(test.equals(loc)) { return ((Point)(r.points.get(i))).getDistance() - ((Point)(r.points.get(pt2))).getDistance(); } } } else { for(int i=pt1;i<=pt2;i++) { Location test=new Location(roadIndex,i); if(test.equals(loc)) { return ((Point)(r.points.get(pt2))).getDistance() - ((Point)(r.points.get(i))).getDistance(); } } } return -1.0; } public double distanceFrom(Location loc) { //assume loc is inside the current segment! //calculates the distance between the start of the current route segment and the location loc; //return -1.0 on error; Road r=(Road) Globals.map.roads.get(roadIndex); if(loc.roadIdx==this.roadIndex) { if(pt1>=pt2) { return ((r.points.get(pt1))).getDistance() - ((r.points.get(loc.ptIdx))).getDistance(); } else { return ((r.points.get(loc.ptIdx))).getDistance() - ((r.points.get(pt1))).getDistance(); } } //else check each point if(pt1>=pt2) { for(int i=pt2;i<=pt1;i++) { Location test=new Location(roadIndex,i); if(test.equals(loc)) { return ((Point)(r.points.get(pt1))).getDistance() - ((Point)(r.points.get(i))).getDistance(); } } } else { for(int i=pt1;i<=pt2;i++) { Location test=new Location(roadIndex,i); if(test.equals(loc)) { return ((Point)(r.points.get(i))).getDistance() - ((Point)(r.points.get(pt1))).getDistance(); } } } return -1.0; } public boolean equals(RouteSegment other) { if(this.roadIndex==other.roadIndex) { if(this.pt1==other.pt1 && this.pt2==other.pt2) return true; if(this.pt2==other.pt1 && this.pt1==other.pt2) return true; return false; } return false; } public String toString() { String ret=""; //ret=ret+"[Rd "+rdIdx1+" Pt "+ptIdx1+"--Rd "+rdIdx2+" Pt "+ptIdx2; try{ ret=ret+"[Rd:"+roadIndex+";From="+pt1+";To="+pt2+"]"; return ret; } catch (Exception ex) { ret="EXCEPTION! RouteSegment.toString()!"; return ret; } } }