00001
00002
00004
00005 #include "stdafx.h"
00006 #include "LaneChangeDetector.h"
00007
00008 #ifdef _DEBUG
00009 #undef THIS_FILE
00010 static char THIS_FILE[]=__FILE__;
00011 #define new DEBUG_NEW
00012 #endif
00013
00015
00017
00027 LaneChangeDetector::LaneChangeDetector(std::string dir, double dt, double dx, int roadLen,
00028 bool DirPos, WORD VehType)
00029 {
00030 m_MetricsDir = dir;
00031 m_TimeInterval = dt;
00032 m_DistanceInterval = dx;
00033 m_RoadLength = roadLen;
00034 m_DirPos = DirPos;
00035 m_VehicleType = VehType;
00036
00037 m_NoLaneChangeEvents = 0;
00038 m_DetectorType = METRICS_TYPE_LANE_CHANGE;
00039
00040 int curPos = 0;
00041 while(curPos < m_RoadLength)
00042 {
00043 m_vDistanceIntervals.push_back(curPos);
00044 curPos += m_DistanceInterval;
00045 }
00046 m_vDistanceIntervals.push_back(m_RoadLength);
00047 m_NoDistIntervals = m_vDistanceIntervals.size();
00048 m_vDistInt_LCECount.assign(m_NoDistIntervals, 0);
00049
00050 InitOutputFiles();
00051 }
00052
00054 LaneChangeDetector::~LaneChangeDetector()
00055 {
00056
00057 }
00058
00060 void LaneChangeDetector::InitOutputFiles()
00061 {
00062 if(m_VehicleType == METRICS_VEH_ALL)
00063 InitCompositionFile();
00064
00065 InitVerboseFile();
00066 InitSpaceTimeFile();
00067 InitRateFile();
00068 }
00069
00077 void LaneChangeDetector::addEvent(LaneChangeEvent* pLCE)
00078 {
00079 WORD vehID = pLCE->getVehicleType();
00080
00081 switch(m_VehicleType)
00082 {
00083 case METRICS_VEH_ALL:
00084 AddCurrentEvent(pLCE);
00085 break;
00086 case METRICS_VEH_CAR:
00087 if(vehID == VEH_ID_CAR)
00088 AddCurrentEvent(pLCE);
00089 break;
00090 case METRICS_VEH_SMALLTRUCK:
00091 if(vehID == VEH_ID_SMALLTRUCK)
00092 AddCurrentEvent(pLCE);
00093 break;
00094 case METRICS_VEH_LARGETRUCK:
00095 if(vehID == VEH_ID_LARGETRUCK)
00096 AddCurrentEvent(pLCE);
00097 break;
00098 case METRICS_VEH_CRANE:
00099 if(vehID == VEH_ID_CRANE)
00100 AddCurrentEvent(pLCE);
00101 break;
00102 case METRICS_VEH_LOWLOADER:
00103 if(vehID == VEH_ID_LOWLOADER)
00104 AddCurrentEvent(pLCE);
00105 break;
00106 default:
00107 AddCurrentEvent(pLCE);
00108 }
00109 }
00110
00111 void LaneChangeDetector::AddCurrentEvent(LaneChangeEvent* lce)
00112 {
00113 m_vChangeEvents.push_back(lce);
00114 m_NoLaneChangeEvents++;
00115 }
00116
00118 void LaneChangeDetector::doIntervalOutput()
00119 {
00120 doVerboseOutput();
00121 doCompositionOutput();
00122 doSpaceTimeOutput();
00123 doRateOutput();
00124
00125 ReInit();
00126 }
00127
00129 void LaneChangeDetector::EndOutput()
00130 {
00131 if(m_VehicleType == METRICS_VEH_ALL)
00132 m_OutFile_Composition.close();
00133
00134 m_OutFile_Verbose.close();
00135 m_OutFile_SpaceTime.close();
00136 m_OutFile_Rate.close();
00137 }
00138
00140 void LaneChangeDetector::ReInit()
00141 {
00142 m_NoLaneChangeEvents = 0;
00143
00144 m_vDistInt_LCECount.clear();
00145 m_vDistInt_LCECount.assign(m_NoDistIntervals, 0);
00146
00147 for(int i = 0; i < m_vChangeEvents.size(); i++)
00148 delete m_vChangeEvents.at(i);
00149 m_vChangeEvents.clear();
00150
00151 }
00152
00154 void LaneChangeDetector::InitVerboseFile()
00155 {
00156 std::string file;
00157 file = m_MetricsDir;
00158 file += "LC_All_" + MapDetVehTypeToString(m_VehicleType) + "_"
00159 + MapDirToString(m_DirPos) + ".csv";
00160
00161 m_OutFile_Verbose.open(file.c_str(), std::ios::out);
00162
00163 m_OutFile_Verbose << "LANE CHANGE - VERBOSE OUTPUT OF EVENTS" << '\n';
00164 m_OutFile_Verbose << "Time,";
00165 m_OutFile_Verbose << "Position,";
00166 m_OutFile_Verbose << "From Lane,";
00167 m_OutFile_Verbose << "To Lane,";
00168 m_OutFile_Verbose << "Right/Left,";
00169 m_OutFile_Verbose << "Type" << '\n';
00170 m_OutFile_Verbose << std::endl;
00171 }
00172
00174 void LaneChangeDetector::InitCompositionFile()
00175 {
00176 std::string file;
00177 file = m_MetricsDir;
00178 file += "LC_Comp_" + MapDetVehTypeToString(m_VehicleType) + "_"
00179 + MapDirToString(m_DirPos) + ".csv";
00180
00181 m_OutFile_Composition.open(file.c_str(), std::ios::out);
00182
00183 m_OutFile_Composition << "LANE CHANGE - COMPOSITION OF EVENTS" << "\n";
00184 m_OutFile_Composition << "Time Interval" << ",";
00185 m_OutFile_Composition << "Car Changes" << ",";
00186 m_OutFile_Composition << "SmallTr Changes" << ",";
00187 m_OutFile_Composition << "LargeTr Changes" << ",";
00188 m_OutFile_Composition << "Crane Changes" << ",";
00189 m_OutFile_Composition << "LowLoad Changes" << '\n';
00190
00191 }
00192
00194 void LaneChangeDetector::InitSpaceTimeFile()
00195 {
00196 std::string file;
00197 file = m_MetricsDir;
00198 file += "LC_ST_" + MapDetVehTypeToString(m_VehicleType) + "_"
00199 + MapDirToString(m_DirPos) + ".csv";
00200
00201 m_OutFile_SpaceTime.open(file.c_str(), std::ios::out);
00202
00203 m_OutFile_SpaceTime << "LANE CHANGE - SPACE TIME OF EVENTS" << '\n';
00204 m_OutFile_SpaceTime << "Left To Right," << ',' << "Right To Left" << '\n';
00205 m_OutFile_SpaceTime << "Time (s),Location (m),Time (s),Location (m)" << '\n';
00206 }
00207
00209 void LaneChangeDetector::InitRateFile()
00210 {
00211 std::string file;
00212 file = m_MetricsDir;
00213 file += "LC_Rate_" + MapDetVehTypeToString(m_VehicleType) + "_"
00214 + MapDirToString(m_DirPos) + ".csv";
00215
00216 m_OutFile_Rate.open(file.c_str(), std::ios::out);
00217
00218 m_OutFile_Rate << "LANE CHANGE - RATES BY LOCATION AND TIME" << '\n';
00219 m_OutFile_Rate << "Time (s),Location (m)" << '\n';
00220 m_OutFile_Rate << ",";
00221 for(int i = 0; i < m_NoDistIntervals; i++)
00222 m_OutFile_Rate << m_vDistanceIntervals[i] << ",";
00223 m_OutFile_Rate << '\n';
00224 }
00225
00227 void LaneChangeDetector::doVerboseOutput()
00228 {
00229 for(int i = 0; i < m_NoLaneChangeEvents; i++)
00230 {
00231 LaneChangeEvent* curEvent = m_vChangeEvents.at(i);
00232
00233 std::string change = (curEvent->getChangeLeft()) ? "Left" : "Right";
00234 std::string dir = (curEvent->getDirPos()) ? "Pos" : "Neg";
00235
00236 std::string type;
00237
00238 switch(curEvent->getVehicleType())
00239 {
00240 case VEH_ID_CAR: type = "Car"; break;
00241 case VEH_ID_SMALLTRUCK: type = "SmallTr"; break;
00242 case VEH_ID_LARGETRUCK: type = "LargeTr"; break;
00243 case VEH_ID_CRANE: type = "Crane"; break;
00244 case VEH_ID_LOWLOADER: type = "Lowload"; break;
00245 }
00246
00247 m_OutFile_Verbose << curEvent->getTime() << " ,"
00248 << curEvent->getPosition() << " ,"
00249 << curEvent->getOriginLane() << " ,"
00250 << curEvent->getDestinationLane() << " ,"
00251 << change << " ,"
00252 << type << '\n';
00253 }
00254 }
00255
00257 void LaneChangeDetector::doCompositionOutput()
00258 {
00259 int nCar = 0;
00260 int nSmallTruck = 0;
00261 int nLargeTruck = 0;
00262 int nCrane = 0;
00263 int nLowLoader = 0;
00264
00265 for(int i = 0; i < m_NoLaneChangeEvents; i++)
00266 {
00267 WORD VehType = m_vChangeEvents.at(i)->getVehicleType();
00268
00269 switch(VehType)
00270 {
00271 case VEH_ID_CAR: nCar++; break;
00272 case VEH_ID_SMALLTRUCK: nSmallTruck++; break;
00273 case VEH_ID_LARGETRUCK: nLargeTruck++; break;
00274 case VEH_ID_CRANE: nCrane++; break;
00275 case VEH_ID_LOWLOADER: nLowLoader++; break;
00276 default: nCar++;
00277 }
00278 }
00279
00280 m_OutFile_Composition << m_TotalTime << ",";
00281 m_OutFile_Composition << nCar << ",";
00282 m_OutFile_Composition << nSmallTruck << ",";
00283 m_OutFile_Composition << nLargeTruck << ",";
00284 m_OutFile_Composition << nCrane << ",";
00285 m_OutFile_Composition << nLowLoader << '\n';
00286 }
00287
00289 void LaneChangeDetector::doSpaceTimeOutput()
00290 {
00291 for(int i = 0; i < m_NoLaneChangeEvents; i++)
00292 {
00293 LaneChangeEvent* pLCE = m_vChangeEvents.at(i);
00294 double time = pLCE->getTime();
00295 double position = pLCE->getPosition();
00296 bool bChangeToLeft = pLCE->getChangeLeft();
00297
00298 if(bChangeToLeft)
00299 m_OutFile_SpaceTime << ",,";
00300
00301 m_OutFile_SpaceTime << time << ',' << position << '\n';
00302 }
00303 }
00304
00306 void LaneChangeDetector::doRateOutput()
00307 {
00308 for(int i = 0; i < m_NoLaneChangeEvents; i++)
00309 {
00310 double position = m_vChangeEvents.at(i)->getPosition();
00311 int iDistInterval = 0;
00312
00313 while( !(position < m_vDistanceIntervals[iDistInterval]) )
00314 iDistInterval++;
00315
00316 m_vDistInt_LCECount[iDistInterval]++;
00317 }
00318
00319 double deltaX = (double)m_DistanceInterval/1000;
00320 double deltaT = (double)m_TimeInterval/SECS_PER_HOUR;
00321
00322 m_OutFile_Rate << m_TotalTime;
00323 for(i = 0; i < m_NoDistIntervals; i++)
00324 {
00325 double n = m_vDistInt_LCECount[i];
00326 double rate = n/(deltaX*deltaT);
00327 m_OutFile_Rate << ',' << rate;
00328 }
00329 m_OutFile_Rate << '\n';
00330 }