00001
00002 #include "stdafx.h"
00003 #include "Road.h"
00004
00005 #include "ConfigData.h"
00006 extern CConfigData g_ConfigData;
00007
00009 Road::Road()
00010 {
00011 MIN_SPACE_FOR_NEXT_VEHICLE = g_ConfigData.IDM.MIN_SPACE_FOR_NEXT_VEHICLE;
00012
00013 m_pIDMParams_Car = NULL;
00014 m_pIDMParams_SmallTruck = NULL;
00015 m_pIDMParams_LargeTruck = NULL;
00016 m_pIDMParams_Crane = NULL;
00017 m_pIDMParams_Lowloader = NULL;
00018
00019 m_pFileHandler = NULL;
00020
00021 m_bEndOfFile = false;
00022 }
00023
00025 Road::~Road()
00026 {
00027 m_pIDMParams_Car = NULL;
00028 m_pIDMParams_SmallTruck = NULL;
00029 m_pIDMParams_LargeTruck = NULL;
00030 m_pIDMParams_Crane = NULL;
00031 m_pIDMParams_Lowloader = NULL;
00032 }
00033
00043 bool Road::update(double step,const double curTime)
00044 {
00045 bool SimulationOver = false;
00046 bool RoadEmpty = false;
00047 bool DirPosEmpty = false;
00048 bool DirNegEmpty = false;
00049
00050
00051 DirPosEmpty = DirectionPos.update(step, curTime);
00052
00053 if(m_NoDirections == 2)
00054 DirNegEmpty = DirectionNeg.update(step, curTime);
00055
00056 if(DirPosEmpty && DirNegEmpty)
00057 RoadEmpty = true;
00058
00059 if(RoadEmpty && m_bEndOfFile)
00060 SimulationOver = true;
00061
00062 if(SimulationOver)
00063 {
00064 for(int i = 0; i < m_vDetectorsPos.size(); i++)
00065 m_vDetectorsPos[i]->EndOutput();
00066 for(i = 0; i < m_vDetectorsNeg.size(); i++)
00067 m_vDetectorsNeg[i]->EndOutput();
00068 }
00069
00070 return SimulationOver;
00071 }
00072
00085 double Road::init()
00086 {
00087 sort(m_vRoadSegmentsPos.begin(), m_vRoadSegmentsPos.end(), RoadSegment::CompareSegments);
00088 sort(m_vRoadSegmentsNeg.begin(), m_vRoadSegmentsNeg.end(), RoadSegment::CompareSegments);
00089
00090
00091 m_BufferVehicles = m_pFileHandler->loadTruckGroup(INPUT_FILE_BUFFER_SIZE);
00092 for(int i = 0; i < m_BufferVehicles.size(); i++)
00093 MapTrafLaneToSimLane(m_BufferVehicles.at(i));
00094
00096
00097 std::vector<int> vLaneLengthsPos(m_NoLanesDirPos,m_RoadLength);
00098
00099
00100 OutputDetector* pDetDirPos = new OutputDetector(m_LocOutputDetectorDirPos, true, m_pFileHandler);
00101 DirectionPos.init(true, pDetDirPos, m_AllowLaneChanging, m_RoadLength, m_DriveOnRight);
00102 DirectionPos.createLanes(0, vLaneLengthsPos, m_vDetectorsPos, m_vRoadSegmentsPos);
00103
00104
00105 for(i = 0; i < DirectionPos.getNoLanes(); i++)
00106 m_vLanes.push_back(&DirectionPos.getLane(i));
00107
00109 if(m_NoDirections == 2)
00110 {
00111
00112 std::vector<int> vLaneLengthsNeg(m_NoLanesDirNeg,m_RoadLength);
00113
00114
00115 OutputDetector* pDetDirNeg = new OutputDetector(m_LocOutputDetectorDirNeg, false, m_pFileHandler);
00116 DirectionNeg.init(false, pDetDirNeg, m_AllowLaneChanging, m_RoadLength, m_DriveOnRight);
00117 DirectionNeg.createLanes(m_NoLanesDirPos, vLaneLengthsNeg, m_vDetectorsNeg, m_vRoadSegmentsNeg);
00118
00119 for(i = 0; i < DirectionNeg.getNoLanes(); i++)
00120 m_vLanes.push_back(&DirectionNeg.getLane(i));
00121 }
00122
00123
00124 return m_BufferVehicles.front()->getTime();
00125 }
00126
00141 void Road::populate(double step, const double curTime)
00142 {
00143 bool frontOn = true;
00144 while(m_BufferVehicles.size() > 0 && frontOn)
00145 {
00146 frontOn = false;
00147 int lane = m_BufferVehicles.front()->getLane() - 1;
00148
00149 if(m_vLanes.at(lane)->getLastPos() >= MIN_SPACE_FOR_NEXT_VEHICLE)
00150 {
00151 if(m_BufferVehicles.front()->getTime() <= curTime)
00152 {
00153 int noVeh = m_vLanes.at(lane)->getNoVeh();
00154
00155
00156 setIDMDriverModel(m_BufferVehicles.front());
00157 m_BufferVehicles.front()->init(m_RoadLength, m_DriveOnRight);
00158 m_vLanes.at(lane)->insert(noVeh, m_BufferVehicles.front());
00159 m_BufferVehicles.erase(m_BufferVehicles.begin());
00160
00161 Vehicle* tempVeh = m_pFileHandler->readLine();
00162 m_PercentComplete = m_pFileHandler->getPercentComplete();
00163 if(tempVeh != NULL)
00164 {
00165 MapTrafLaneToSimLane(tempVeh);
00166 m_BufferVehicles.push_back(tempVeh);
00167 }
00168 else
00169 m_bEndOfFile = true;
00170 frontOn = true;
00171 }
00172 }
00173
00174 }
00175 }
00176
00185 void Road::MapTrafLaneToSimLane(Vehicle* pVeh)
00186 {
00187
00188 pVeh->setTotalNoLanesInRoad(m_NoLanes);
00189
00190
00191 if(!pVeh->getDirection())
00192 {
00193 int TrafFileNoLanes = m_TrafFileNoLanesDirPos + m_TrafFileNoLanesDirNeg;
00194 int lanesFromNegVerge = TrafFileNoLanes - pVeh->getLane();
00195 int newLane = m_NoLanes - lanesFromNegVerge;
00196 pVeh->setLane(newLane);
00197 }
00198 }
00199
00207 void Road::setIDMDriverModel(Vehicle* pVeh)
00208 {
00209 WORD VEH_ID = pVeh->getID();
00210
00211 switch(VEH_ID)
00212 {
00213 case VEH_ID_CAR:
00214 pVeh->setDriver( m_pIDMParams_Car->Generate() );
00215 break;
00216 case VEH_ID_SMALLTRUCK:
00217 pVeh->setDriver( m_pIDMParams_SmallTruck->Generate() );
00218 break;
00219 case VEH_ID_LARGETRUCK:
00220 pVeh->setDriver( m_pIDMParams_LargeTruck->Generate() );
00221 break;
00222 case VEH_ID_CRANE:
00223 pVeh->setDriver( m_pIDMParams_Crane->Generate() );
00224 break;
00225 case VEH_ID_LOWLOADER:
00226 pVeh->setDriver( m_pIDMParams_Lowloader->Generate() );
00227 break;
00228 default:
00229 pVeh->setDriver( m_pIDMParams_Car->Generate() );
00230 };
00231 }
00232
00238 M2D Road::getVehicles()
00239 {
00240 M2D pos;
00241
00242 M2D temp = DirectionPos.getGlobalPos();
00243 for(int i = 0; i < m_NoLanesDirPos; i++)
00244 pos.push_back(temp[i]);
00245
00246 if(m_NoDirections == 2)
00247 {
00248 temp.clear();
00249
00250 temp = DirectionNeg.getGlobalPos();
00251 for(i = 0; i < m_NoLanesDirNeg; i++)
00252 pos.push_back(temp[i]);
00253 }
00254
00255 return pos;
00256 }
00257
00262 double Road::getLength()
00263 {
00264 return m_RoadLength;
00265 }
00266
00271 void Road::setNoLanesDirPos(int nlpos)
00272 {
00273 m_NoLanesDirPos = nlpos;
00274 }
00275
00280 void Road::setNoLanesDirNeg(int nlneg)
00281 {
00282 m_NoLanesDirNeg = nlneg;
00283 }
00284
00289 void Road::setNoDirections(int nd)
00290 {
00291 m_NoDirections = nd;
00292 }
00293
00298 void Road::setNoLanes(int nl)
00299 {
00300 m_NoLanes = nl;
00301 }
00302
00307 void Road::setRoadLength(int L)
00308 {
00309 m_RoadLength = L;
00310 }
00311
00322 void Road::initTruckGroup(string inFile, string outFile, WORD fileType)
00323 {
00324 if(fileType == CASTOR)
00325 {
00326 m_pFileHandler = new CASTORFile(inFile, outFile);
00327
00328 }
00329 else if(fileType == SAFT)
00330 {
00331 m_pFileHandler = new SAFTFile(inFile, outFile);
00332 }
00333 }
00334
00339 void Road::setIDMParams_Car(CIDMParameterSet* Params)
00340 {
00341 m_pIDMParams_Car = Params;
00342 }
00343
00348 void Road::setIDMParams_SmallTruck(CIDMParameterSet* Params)
00349 {
00350 m_pIDMParams_SmallTruck = Params;
00351 }
00352
00357 void Road::setIDMParams_LargeTruck(CIDMParameterSet* Params)
00358 {
00359 m_pIDMParams_LargeTruck = Params;
00360 }
00361
00366 void Road::setIDMParams_Crane(CIDMParameterSet* Params)
00367 {
00368 m_pIDMParams_Crane = Params;
00369 }
00370
00375 void Road::setIDMParams_Lowloader(CIDMParameterSet* Params)
00376 {
00377 m_pIDMParams_Lowloader = Params;
00378 }
00379
00384 void Road::setTrafFileNoLanesDirPos(int nl)
00385 {
00386 m_TrafFileNoLanesDirPos = nl;
00387 }
00388
00393 void Road::setTrafFileNoLanesDirNeg(int nl)
00394 {
00395 m_TrafFileNoLanesDirNeg = nl;
00396 }
00397
00402 void Road::setLocOuputDetectorDirPos(int loc)
00403 {
00404 m_LocOutputDetectorDirPos = loc;
00405 }
00406
00412 void Road::setLocOuputDetectorDirNeg(int loc)
00413 {
00414
00415 m_LocOutputDetectorDirNeg = m_RoadLength - loc;
00416 }
00417
00418 int Road::getPercentComplete()
00419 {
00420 return m_PercentComplete;
00421 }
00422
00427 bool Road::getAllowLaneChanging()
00428 {
00429 return m_AllowLaneChanging;
00430 }
00431
00436 void Road::setAllowLaneChanging(bool status)
00437 {
00438 m_AllowLaneChanging = status;
00439 }
00440
00445 void Road::setDriveOnRight(bool OnRight)
00446 {
00447 m_DriveOnRight = OnRight;
00448 }
00449
00454 void Road::passIDMParameters(RoadSegment *segment)
00455 {
00456 segment->setIDMParams_Car(m_pIDMParams_Car);
00457 segment->setIDMParams_Crane(m_pIDMParams_Crane);
00458 segment->setIDMParams_LargeTruck(m_pIDMParams_LargeTruck);
00459 segment->setIDMParams_Lowloader(m_pIDMParams_Lowloader);
00460 segment->setIDMParams_SmallTruck(m_pIDMParams_SmallTruck);
00461 }
00462
00470 void Road::SetSegmentFromFeature(CRoadFeature *pFeat)
00471 {
00472 ASSERT(pFeat->getStart() < pFeat->getEnd());
00473
00474 switch( pFeat->getType() )
00475 {
00476 case FEAT_SPEEDLIMIT:
00477 SetSpeedLimitSegment(pFeat);
00478 break;
00479 case FEAT_GRADIENT:
00480 SetGradientSegment(pFeat);
00481 break;
00482 default:
00483 break;
00484 }
00485
00486 }
00487
00495 void Road::SetSpeedLimitSegment(CRoadFeature *pFeat)
00496 {
00497 bool bDirPos = pFeat->getDirPos();
00498 SpeedLimit* pSL;
00499 if( bDirPos )
00500 {
00501 pSL = new SpeedLimit(pFeat->getStart(), pFeat->getEnd(), pFeat->getValue(), bDirPos);
00502 m_vRoadSegmentsPos.push_back(pSL);
00503 }
00504 else
00505 {
00506 int start = pFeat->getStart();
00507 int end = pFeat->getEnd();
00508
00509 pSL = new SpeedLimit(m_RoadLength - end, m_RoadLength - start, pFeat->getValue(), bDirPos);
00510
00511 m_vRoadSegmentsNeg.push_back(pSL);
00512 }
00513 passIDMParameters(pSL);
00514 }
00515
00523 void Road::SetGradientSegment(CRoadFeature *pFeat)
00524 {
00525 bool bDirPos = pFeat->getDirPos();
00526 Gradient* pGr;
00527
00528 if( bDirPos )
00529 {
00530 pGr = new Gradient(pFeat->getStart(), pFeat->getEnd(), pFeat->getValue(), bDirPos);
00531 m_vRoadSegmentsPos.push_back(pGr);
00532 }
00533 else
00534 {
00535 int start = pFeat->getStart();
00536 int end = pFeat->getEnd();
00537
00538 pGr = new Gradient(m_RoadLength - end, m_RoadLength - start, pFeat->getValue(), bDirPos);
00539
00540 m_vRoadSegmentsNeg.push_back(pGr);
00541 }
00542 passIDMParameters(pGr);
00543 }
00544
00555 void Road::SetMetricDetFromStatDet(CStatDetector* pDet, int nLanesPos, int nLanesNeg)
00556 {
00557 bool bDirPos = pDet->getDirPos();
00558
00559 if(pDet->getDetectorType() == METRICS_TYPE_LANE_CHANGE)
00560 {
00561 LaneChangeDetector* pLCD;
00562 pLCD = new LaneChangeDetector( pDet->getStdStrMetricsDir(),
00563 pDet->getTimeInterval(),
00564 pDet->getLocation(),
00565 m_RoadLength,
00566 bDirPos,
00567 pDet->getVehicleType());
00568 if( bDirPos )
00569 m_vDetectorsPos.push_back(pLCD);
00570 else
00571 m_vDetectorsNeg.push_back(pLCD);
00572 }
00573 else
00574 {
00575 MetricsDetector* pMD;
00576 if( bDirPos )
00577 {
00578 pMD = new MetricsDetector( pDet->getStdStrMetricsDir(),
00579 pDet->getDetectorType(),
00580 bDirPos,
00581 nLanesPos,
00582 pDet->getVehicleType(),
00583 pDet->getTimeInterval(),
00584 pDet->getLocation(),
00585 m_RoadLength);
00586 m_vDetectorsPos.push_back(pMD);
00587 }
00588 else
00589 {
00590 pMD = new MetricsDetector( pDet->getStdStrMetricsDir(),
00591 pDet->getDetectorType(),
00592 bDirPos,
00593 nLanesNeg,
00594 pDet->getVehicleType(),
00595 pDet->getTimeInterval(),
00596 m_RoadLength - pDet->getLocation(),
00597 m_RoadLength);
00598 m_vDetectorsNeg.push_back(pMD);
00599 }
00600 }
00601 }
00602
00608 void Road::clear()
00609 {
00610 DirectionPos.clear();
00611 if(m_NoDirections == 2)
00612 DirectionNeg.clear();
00613
00614 delete m_pFileHandler;
00615
00616 m_vLanes.clear();
00617
00618 m_BufferVehicles.clear();
00619
00620 m_vDetectorsPos.clear();
00621 m_vDetectorsNeg.clear();
00622
00623 m_vRoadSegmentsPos.clear();
00624 m_vRoadSegmentsNeg.clear();
00625
00626
00627 m_pIDMParams_Car = NULL;
00628 m_pIDMParams_SmallTruck = NULL;
00629 m_pIDMParams_LargeTruck = NULL;
00630 m_pIDMParams_Crane = NULL;
00631 m_pIDMParams_Lowloader = NULL;
00632 }