00001
00002 #include "stdafx.h"
00003 #include "Direction.h"
00004
00006 Direction::Direction()
00007 {
00008 m_pOutputDetector = NULL;
00009 }
00010
00012 Direction::~Direction()
00013 {
00014 int i;
00015
00016
00017 for(i = 0; i < m_vDetectors.size(); i++)
00018 delete m_vDetectors.at(i);
00019
00020
00021
00022 for(i = 0; i < m_vRoadSegments.size(); i++)
00023 delete m_vRoadSegments.at(i);
00024
00025 }
00026
00037 bool Direction::update(const double step, const double curTime)
00038 {
00039 CheckDetectorTimes(step);
00040 bool DirectionEmpty = false;
00041 int NoEmptyLanes = 0;
00042 for(int i = 0; i < m_NoLanes; i++)
00043 {
00044 bool laneEmpty = m_vLanes[i].update(step, curTime);
00045 if(laneEmpty)
00046 NoEmptyLanes++;
00047 }
00048
00049
00050 m_pOutputDetector->WriteVehiclesToFile();
00051
00052 if(NoEmptyLanes == m_NoLanes)
00053 DirectionEmpty = true;
00054
00055 return DirectionEmpty;
00056 }
00057
00066 void Direction::init(bool DirPos, OutputDetector* pOutDet, bool AllowLaneChange, int RoadLength, bool DriveOnRight)
00067 {
00068 m_DirPos = DirPos;
00069 m_pOutputDetector = pOutDet;
00070 m_AllowLaneChanging = AllowLaneChange;
00071 m_RoadLength = RoadLength;
00072 m_DriveOnRight = DriveOnRight;
00073 }
00074
00086 void Direction::createLanes(int iFirstLane, std::vector<int> vLaneLengths, std::vector<Detector*> detectors,
00087 std::vector<RoadSegment*> segments)
00088 {
00089 m_NoLanes = vLaneLengths.size();
00090 m_vDetectors = detectors;
00091 m_vRoadSegments = segments;
00092
00093 for(int i = 0; i < m_NoLanes; i++)
00094 {
00095 Lane lane(iFirstLane + i, vLaneLengths[i], m_vDetectors, m_DirPos, m_AllowLaneChanging);
00096 lane.setOutputDetector(m_pOutputDetector);
00097 lane.setRoadSegments(m_vRoadSegments);
00098 m_vLanes.push_back(lane);
00099 }
00100
00101
00102 if(m_NoLanes > 1)
00103 {
00104
00105
00106
00107 if(m_DriveOnRight)
00108 {
00109 for(i = 0; i < m_NoLanes - 1; i++)
00110 m_vLanes.at(i).setLeftLane(&m_vLanes.at(i+1));
00111 for(i = 1; i < m_NoLanes; i++)
00112 m_vLanes.at(i).setRightLane(&m_vLanes.at(i-1));
00113 }
00114 else
00115 {
00116 for(i = 0; i < m_NoLanes - 1; i++)
00117 m_vLanes.at(i).setRightLane(&m_vLanes.at(i+1));
00118 for(i = 1; i < m_NoLanes; i++)
00119 m_vLanes.at(i).setLeftLane(&m_vLanes.at(i-1));
00120 }
00121 }
00122 }
00123
00130 M2D Direction::getGlobalPos()
00131 {
00132
00133 M2D vVeh2D = getVehiclesM2D();
00134
00135 if(!m_DirPos)
00136 {
00137 for(int i = 0; i < m_NoLanes; i++)
00138 {
00139 int d2 = vVeh2D[i].size();
00140 for(int j = 0; j < d2; j++)
00141 vVeh2D[i][j]->setRoadPos( m_vLanes[i].getLength() - vVeh2D[i][j]->getPos() );
00142 }
00143 }
00144 return vVeh2D;
00145 }
00146
00151 M2D Direction::getVehiclesM2D()
00152 {
00153
00154 M2D vVeh2D;
00155
00156 for(int i = 0; i < m_NoLanes; i++)
00157 {
00158 std::vector<Vehicle*> temp = m_vLanes[i].getPos();
00159 vVeh2D.push_back(temp);
00160 }
00161 return vVeh2D;
00162 }
00163
00168 int Direction::getNoLanes()
00169 {
00170 return m_NoLanes;
00171 }
00172
00178 Lane& Direction::getLane(int i)
00179 {
00180 return m_vLanes.at(i);
00181 }
00182
00187 OutputDetector* Direction::getOutputDetector()
00188 {
00189 return m_pOutputDetector;
00190 }
00191
00196 void Direction::setOutputDetector(OutputDetector *OutputDet)
00197 {
00198 m_pOutputDetector = OutputDet;
00199 }
00200
00208 void Direction::CheckDetectorTimes(double step)
00209 {
00210 for(int i = 0; i < m_vDetectors.size(); i++)
00211 {
00212 m_vDetectors.at(i)->CheckForOutputTime(step);
00213 }
00214 }
00220 void Direction::clear()
00221 {
00222 int i;
00223
00224 for(i = 0; i < m_vLanes.size(); i++)
00225 m_vLanes.at(i).clear();
00226
00227 m_vLanes.clear();
00228
00229 delete m_pOutputDetector;
00230
00231
00232 for(i = 0; i < m_vDetectors.size(); i++)
00233 delete m_vDetectors.at(i);
00234
00235 m_vDetectors.clear();
00236
00237
00238 for(i = 0; i < m_vRoadSegments.size(); i++)
00239 delete m_vRoadSegments.at(i);
00240
00241 m_vRoadSegments.clear();
00242 }