00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef _MSC_VER
00023 #include <windows_config.h>
00024 #else
00025 #include <config.h>
00026 #endif
00027
00028 #include <utility>
00029 #include <set>
00030 #include <microsim/MSNet.h>
00031 #include <microsim/MSJunction.h>
00032 #include <microsim/output/MSDetectorControl.h>
00033 #include <microsim/MSEdge.h>
00034 #include <microsim/MSVehicleTransfer.h>
00035 #include <microsim/MSVehicle.h>
00036 #include <microsim/MSEmitControl.h>
00037 #include <microsim/traffic_lights/MSTrafficLightLogic.h>
00038 #include <microsim/traffic_lights/MSTLLogicControl.h>
00039 #include <microsim/MSJunctionControl.h>
00040 #include <utils/gui/globjects/GUIGlObjectStorage.h>
00041 #include <utils/shapes/ShapeContainer.h>
00042 #include <utils/common/RGBColor.h>
00043 #include <guisim/GLObjectValuePassConnector.h>
00044 #include <guisim/GUIEdge.h>
00045 #include <guisim/GUILaneSpeedTrigger.h>
00046 #include <guisim/GUIDetectorWrapper.h>
00047 #include <guisim/GUIInductLoop.h>
00048 #include <guisim/GUI_E2_ZS_Collector.h>
00049 #include <guisim/GUI_E2_ZS_CollectorOverLanes.h>
00050 #include <guisim/GUIE3Collector.h>
00051 #include <guisim/GUITrafficLightLogicWrapper.h>
00052 #include <guisim/GUIJunctionWrapper.h>
00053 #include <guisim/GUIVehicleControl.h>
00054 #include <gui/GUIGlobals.h>
00055 #include <microsim/MSUpdateEachTimestepContainer.h>
00056 #include <microsim/MSRouteLoader.h>
00057 #include "GUIVehicle.h"
00058 #include "GUINet.h"
00059 #include "GUIShapeContainer.h"
00060 #include <utils/gui/globjects/GUIGlObjectStorage.h>
00061 #include <utils/gui/globjects/GUIPolygon2D.h>
00062 #include <utils/gui/globjects/GUIPointOfInterest.h>
00063 #include <utils/gui/globjects/GUIGLObjectPopupMenu.h>
00064 #include <utils/gui/div/GUIParameterTableWindow.h>
00065 #include <utils/common/StringUtils.h>
00066
00067 #ifdef CHECK_MEMORY_LEAKS
00068 #include <foreign/nvwa/debug_new.h>
00069 #endif // CHECK_MEMORY_LEAKS
00070
00071
00072
00073
00074
00075 GUINet::GUINet(MSVehicleControl *vc, MSEventControl *beginOfTimestepEvents,
00076 MSEventControl *endOfTimestepEvents, MSEventControl *emissionEvents) throw(ProcessError)
00077 : MSNet(vc, beginOfTimestepEvents, endOfTimestepEvents, emissionEvents, new GUIShapeContainer()),
00078 GUIGlObject(GUIGlObjectStorage::gIDStorage, "network"),
00079 myGrid(new SUMORTree(&GUIGlObject::drawGL)),
00080 myLastSimDuration(0), myLastIdleDuration(0),
00081 myLastVehicleMovementCount(0), myOverallVehicleCount(0), myOverallSimDuration(0) {
00082 GUIGlObjectStorage::gIDStorage.setNetObject(this);
00083
00084 MSCORN::setWished(MSCORN::CORN_VEH_SAVEREROUTING);
00085 }
00086
00087
00088 GUINet::~GUINet() throw() {
00089 GUIGlObjectStorage::gIDStorage.clear();
00090
00091
00092 for (std::vector<GUIJunctionWrapper*>::iterator i1=myJunctionWrapper.begin(); i1!=myJunctionWrapper.end(); i1++) {
00093 delete(*i1);
00094 }
00095
00096 GUIGlObject_AbstractAdd::clearDictionary();
00097
00098 for (Logics2WrapperMap::iterator i3=myLogics2Wrapper.begin(); i3!=myLogics2Wrapper.end(); i3++) {
00099 delete(*i3).second;
00100 }
00101
00102 for (std::map<std::string, GUIDetectorWrapper*>::iterator i=myDetectorDict.begin(); i!=myDetectorDict.end(); ++i) {
00103 delete(*i).second;
00104 }
00105
00106 delete myGrid;
00107 }
00108
00109
00110 const Boundary &
00111 GUINet::getBoundary() const {
00112 return myBoundary;
00113 }
00114
00115
00116 void
00117 GUINet::initDetectors() {
00118
00119 const std::map<std::string, MSE2Collector*> &e2 = myDetectorControl->getE2Detectors().getMyMap();
00120 for (std::map<std::string, MSE2Collector*>::const_iterator i2=e2.begin(); i2!=e2.end(); i2++) {
00121 MSE2Collector *const e2i = (*i2).second;
00122 const MSLane *lane = e2i->getLane();
00123 GUIEdge &edge = static_cast<GUIEdge&>(lane->getEdge());
00124
00125
00126
00127
00128
00129
00130
00131
00132 GUIDetectorWrapper *wrapper =
00133 static_cast<GUI_E2_ZS_Collector*>(e2i)->buildDetectorWrapper(GUIGlObjectStorage::gIDStorage, edge.getLaneGeometry(lane));
00134
00135 myDetectorDict[wrapper->getMicrosimID()] = wrapper;
00136 }
00137
00138 const std::map<std::string, MS_E2_ZS_CollectorOverLanes*> &e2ol = myDetectorControl->getE2OLDetectors().getMyMap();
00139 for (std::map<std::string, MS_E2_ZS_CollectorOverLanes*>::const_iterator i2=e2ol.begin(); i2!=e2ol.end(); i2++) {
00140 MS_E2_ZS_CollectorOverLanes * const e2oli = (*i2).second;
00141
00142 GUIDetectorWrapper *wrapper =
00143 static_cast<GUI_E2_ZS_CollectorOverLanes*>(e2oli)->buildDetectorWrapper(
00144 GUIGlObjectStorage::gIDStorage);
00145
00146 myDetectorDict[wrapper->getMicrosimID()] = wrapper;
00147 }
00148
00149 const std::map<std::string, MSInductLoop*> &e1 = myDetectorControl->getInductLoops().getMyMap();
00150 for (std::map<std::string, MSInductLoop*>::const_iterator i2=e1.begin(); i2!=e1.end(); i2++) {
00151 MSInductLoop *const e1i = (*i2).second;
00152 const MSLane *lane = e1i->getLane();
00153 GUIEdge &edge = static_cast<GUIEdge&>(lane->getEdge());
00154
00155 GUIDetectorWrapper *wrapper = static_cast<GUIInductLoop*>(e1i)->buildDetectorWrapper(GUIGlObjectStorage::gIDStorage, edge.getLaneGeometry(lane));
00156
00157 myDetectorDict[wrapper->getMicrosimID()] = wrapper;
00158 }
00159
00160 const std::map<std::string, MSE3Collector*> &e3 = myDetectorControl->getE3Detectors().getMyMap();
00161 for (std::map<std::string, MSE3Collector*>::const_iterator i2=e3.begin(); i2!=e3.end(); i2++) {
00162 MSE3Collector *const e3i = (*i2).second;
00163
00164 GUIDetectorWrapper *wrapper =
00165 static_cast<GUIE3Collector*>(e3i)->buildDetectorWrapper(GUIGlObjectStorage::gIDStorage);
00166
00167 myDetectorDict[wrapper->getMicrosimID()] = wrapper;
00168 }
00169 }
00170
00171
00172 void
00173 GUINet::initTLMap() {
00174
00175 const std::vector<MSTrafficLightLogic*> &logics = getTLSControl().getAllLogics();
00176
00177 myTLLogicWrappers.reserve(logics.size());
00178
00179 for (std::vector<MSTrafficLightLogic*>::const_iterator i=logics.begin(); i!=logics.end(); ++i) {
00180
00181 MSTrafficLightLogic *tll = (*i);
00182
00183 const MSTrafficLightLogic::LinkVectorVector &links = tll->getLinks();
00184 if (links.size()==0) {
00185 continue;
00186 }
00187
00188 GUITrafficLightLogicWrapper *tllw =
00189 new GUITrafficLightLogicWrapper(GUIGlObjectStorage::gIDStorage, *myLogics, *tll);
00190
00191 MSTrafficLightLogic::LinkVectorVector::const_iterator j;
00192 for (j=links.begin(); j!=links.end(); j++) {
00193 MSTrafficLightLogic::LinkVector::const_iterator j2;
00194 for (j2=(*j).begin(); j2!=(*j).end(); j2++) {
00195 myLinks2Logic[*j2] = tll->getID();
00196 }
00197 }
00198 myLogics2Wrapper[tll] = tllw;
00199 }
00200 }
00201
00202
00203 Position2D
00204 GUINet::getJunctionPosition(const std::string &name) const {
00205
00206 return myJunctions->get(name)->getPosition();
00207 }
00208
00209
00210 bool
00211 GUINet::vehicleExists(const std::string &name) const {
00212 return myVehicleControl->getVehicle(name)!=0;
00213 }
00214
00215
00216 Boundary
00217 GUINet::getEdgeBoundary(const std::string &name) const {
00218 GUIEdge *edge = static_cast<GUIEdge*>(MSEdge::dictionary(name));
00219 return edge->getBoundary();
00220 }
00221
00222
00223 unsigned int
00224 GUINet::getLinkTLID(MSLink *link) const {
00225 Links2LogicMap::const_iterator i = myLinks2Logic.find(link);
00226 if (i==myLinks2Logic.end()) {
00227 return -1;
00228 }
00229 if (myLogics2Wrapper.find(myLogics->getActive((*i).second))==myLogics2Wrapper.end()) {
00230 return -1;
00231 }
00232 return myLogics2Wrapper.find(myLogics->getActive((*i).second))->second->getGlID();
00233 }
00234
00235
00236 int
00237 GUINet::getLinkTLIndex(MSLink *link) const {
00238 Links2LogicMap::const_iterator i = myLinks2Logic.find(link);
00239 if (i==myLinks2Logic.end()) {
00240 return -1;
00241 }
00242 if (myLogics2Wrapper.find(myLogics->getActive((*i).second))==myLogics2Wrapper.end()) {
00243 return -1;
00244 }
00245 return myLogics2Wrapper.find(myLogics->getActive((*i).second))->second->getLinkIndex(link);
00246 }
00247
00248
00249 void
00250 GUINet::guiSimulationStep() {
00251 MSUpdateEachTimestepContainer<MSUpdateEachTimestep<GLObjectValuePassConnector<SUMOReal> > >::getInstance()->updateAll();
00252 MSUpdateEachTimestepContainer<MSUpdateEachTimestep<GLObjectValuePassConnector<std::pair<SUMOTime, MSPhaseDefinition> > > >::getInstance()->updateAll();
00253 }
00254
00255
00256 std::vector<GLuint>
00257 GUINet::getJunctionIDs() const {
00258 std::vector<GLuint> ret;
00259 for (std::vector<GUIJunctionWrapper*>::const_iterator i=myJunctionWrapper.begin(); i!=myJunctionWrapper.end(); ++i) {
00260 ret.push_back((*i)->getGlID());
00261 }
00262 return ret;
00263 }
00264
00265
00266 std::vector<GLuint>
00267 GUINet::getTLSIDs() const {
00268 std::vector<GLuint> ret;
00269 std::vector<std::string> ids;
00270 for (std::map<MSTrafficLightLogic*, GUITrafficLightLogicWrapper*>::const_iterator i=myLogics2Wrapper.begin(); i!=myLogics2Wrapper.end(); ++i) {
00271 size_t nid = (*i).second->getGlID();
00272 std::string sid = (*i).second->getMicrosimID();
00273 if (find(ids.begin(), ids.end(), sid)==ids.end()) {
00274 ret.push_back(nid);
00275 ids.push_back(sid);
00276 }
00277 }
00278 return ret;
00279 }
00280
00281
00282 std::vector<GLuint>
00283 GUINet::getShapeIDs() const {
00284 std::vector<GLuint> ret;
00285 if (myShapeContainer!=0) {
00286 int minLayer = myShapeContainer->getMinLayer();
00287 int maxLayer = myShapeContainer->getMaxLayer();
00288 for (int j=minLayer; j<=maxLayer; ++j) {
00289 const std::map<std::string, Polygon2D*> &pol = myShapeContainer->getPolygonCont(j).getMyMap();
00290 for (std::map<std::string, Polygon2D*>::const_iterator i=pol.begin(); i!=pol.end(); ++i) {
00291 ret.push_back(static_cast<GUIPolygon2D*>((*i).second)->getGlID());
00292 }
00293 const std::map<std::string, PointOfInterest*> &poi = myShapeContainer->getPOICont(j).getMyMap();
00294 for (std::map<std::string, PointOfInterest*>::const_iterator i=poi.begin(); i!=poi.end(); ++i) {
00295 ret.push_back(static_cast<GUIPointOfInterest*>((*i).second)->getGlID());
00296 }
00297 }
00298 }
00299 return ret;
00300 }
00301
00302
00303 void
00304 GUINet::initGUIStructures() {
00305
00306 initDetectors();
00307
00308 initTLMap();
00309
00310 GUIEdge::fill(myEdgeWrapper);
00311
00312 size_t size = myJunctions->size();
00313 myJunctionWrapper.reserve(size);
00314 const std::map<std::string, MSJunction*> &junctions = myJunctions->getMyMap();
00315 for (std::map<std::string, MSJunction*>::const_iterator i=junctions.begin(); i!=junctions.end(); ++i) {
00316 myJunctionWrapper.push_back(new GUIJunctionWrapper(GUIGlObjectStorage::gIDStorage, *(*i).second));
00317 }
00318
00319 float *cmin = new float[2];
00320 float *cmax = new float[2];
00321 for (std::vector<GUIEdge*>::iterator i=myEdgeWrapper.begin(); i!=myEdgeWrapper.end(); ++i) {
00322 GUIEdge *edge = *i;
00323 Boundary b;
00324 const std::vector<MSLane*> &lanes = edge->getLanes();
00325 for (std::vector<MSLane*>::const_iterator j=lanes.begin(); j!=lanes.end(); ++j) {
00326 b.add((*j)->getShape().getBoxBoundary());
00327 }
00328 b.grow(2.);
00329 cmin[0] = b.xmin();
00330 cmin[1] = b.ymin();
00331 cmax[0] = b.xmax();
00332 cmax[1] = b.ymax();
00333 myGrid->Insert(cmin, cmax, edge);
00334 myBoundary.add(b);
00335 }
00336 for (std::vector<GUIJunctionWrapper*>::iterator i=myJunctionWrapper.begin(); i!=myJunctionWrapper.end(); ++i) {
00337 GUIJunctionWrapper *junction = *i;
00338 Boundary b = junction->getBoundary();
00339 b.grow(2.);
00340 cmin[0] = b.xmin();
00341 cmin[1] = b.ymin();
00342 cmax[0] = b.xmax();
00343 cmax[1] = b.ymax();
00344 myGrid->Insert(cmin, cmax, junction);
00345 myBoundary.add(b);
00346 }
00347 const std::vector<GUIGlObject_AbstractAdd*> &a = GUIGlObject_AbstractAdd::getObjectList();
00348 for (std::vector<GUIGlObject_AbstractAdd*>::const_iterator i=a.begin(); i!=a.end(); ++i) {
00349 GUIGlObject_AbstractAdd *o = *i;
00350 Boundary b = o->getCenteringBoundary();
00351 cmin[0] = b.xmin();
00352 cmin[1] = b.ymin();
00353 cmax[0] = b.xmax();
00354 cmax[1] = b.ymax();
00355 myGrid->Insert(cmin, cmax, o);
00356 }
00357 delete[] cmin;
00358 delete[] cmax;
00359 myGrid->add(myBoundary);
00360 }
00361
00362
00363 unsigned int
00364 GUINet::getWholeDuration() const throw() {
00365 return myLastSimDuration+myLastIdleDuration;
00366 }
00367
00368
00369 unsigned int
00370 GUINet::getSimDuration() const throw() {
00371 return myLastSimDuration;
00372 }
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 SUMOReal
00384 GUINet::getRTFactor() const {
00385 if (myLastSimDuration==0) {
00386 return -1;
00387 }
00388 return (SUMOReal) 1000. / (SUMOReal) myLastSimDuration;
00389 }
00390
00391
00392 SUMOReal
00393 GUINet::getUPS() const {
00394 if (myLastSimDuration==0) {
00395 return -1;
00396 }
00397 return (SUMOReal) myLastVehicleMovementCount / (SUMOReal) myLastSimDuration *(SUMOReal) 1000.;
00398 }
00399
00400
00401 SUMOReal
00402 GUINet::getMeanRTFactor(int duration) const {
00403 if (myOverallSimDuration==0) {
00404 return -1;
00405 }
00406 return ((SUMOReal)(duration)*(SUMOReal) 1000./(SUMOReal)myOverallSimDuration);
00407 }
00408
00409
00410 SUMOReal
00411 GUINet::getMeanUPS() const {
00412 if (myOverallSimDuration==0) {
00413 return -1;
00414 }
00415 return ((SUMOReal)myVehiclesMoved / (SUMOReal)myOverallSimDuration *(SUMOReal) 1000.);
00416 }
00417
00418
00419 unsigned int
00420 GUINet::getIdleDuration() const throw() {
00421 return myLastIdleDuration;
00422 }
00423
00424
00425 void
00426 GUINet::setSimDuration(int val) {
00427 myLastSimDuration = val;
00428 myOverallSimDuration += val;
00429 myLastVehicleMovementCount = getVehicleControl().getRunningVehicleNo();
00430 myOverallVehicleCount += myLastVehicleMovementCount;
00431 }
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 void
00442 GUINet::setIdleDuration(int val) {
00443 myLastIdleDuration = val;
00444 }
00445
00446
00447 GUIGLObjectPopupMenu *
00448 GUINet::getPopUpMenu(GUIMainWindow &app,
00449 GUISUMOAbstractView &parent) throw() {
00450 GUIGLObjectPopupMenu *ret = new GUIGLObjectPopupMenu(app, parent, *this);
00451 buildPopupHeader(ret, app);
00452 buildCenterPopupEntry(ret);
00453 buildShowParamsPopupEntry(ret);
00454 buildPositionCopyEntry(ret, false);
00455 return ret;
00456 }
00457
00458
00459 GUIParameterTableWindow *
00460 GUINet::getParameterWindow(GUIMainWindow &app,
00461 GUISUMOAbstractView &) throw() {
00462 GUIParameterTableWindow *ret =
00463 new GUIParameterTableWindow(app, *this, 13);
00464
00465 ret->mkItem("vehicles running [#]", true,
00466 new FunctionBinding<MSVehicleControl, unsigned int>(&getVehicleControl(), &MSVehicleControl::getRunningVehicleNo));
00467 ret->mkItem("vehicles ended [#]", true,
00468 new FunctionBinding<MSVehicleControl, unsigned int>(&getVehicleControl(), &MSVehicleControl::getEndedVehicleNo));
00469 ret->mkItem("vehicles emitted [#]", true,
00470 new FunctionBinding<MSVehicleControl, unsigned int>(&getVehicleControl(), &MSVehicleControl::getEmittedVehicleNo));
00471 ret->mkItem("vehicles loaded [#]", true,
00472 new FunctionBinding<MSVehicleControl, unsigned int>(&getVehicleControl(), &MSVehicleControl::getLoadedVehicleNo));
00473 ret->mkItem("vehicles waiting [#]", true,
00474 new FunctionBinding<MSEmitControl, unsigned int>(&getEmitControl(), &MSEmitControl::getWaitingVehicleNo));
00475 ret->mkItem("end time [s]", false, OptionsCont::getOptions().getString("end"));
00476 ret->mkItem("begin time [s]", false, OptionsCont::getOptions().getString("begin"));
00477
00478 if (logSimulationDuration()) {
00479 ret->mkItem("step duration [ms]", true, new FunctionBinding<GUINet, unsigned int>(this, &GUINet::getWholeDuration));
00480 ret->mkItem("simulation duration [ms]", true, new FunctionBinding<GUINet, unsigned int>(this, &GUINet::getSimDuration));
00481
00482
00483
00484
00485
00486 ret->mkItem("idle duration [ms]", true, new FunctionBinding<GUINet, unsigned int>(this, &GUINet::getIdleDuration));
00487 ret->mkItem("duration factor []", true, new FunctionBinding<GUINet, SUMOReal>(this, &GUINet::getRTFactor));
00488
00489
00490
00491
00492
00493 ret->mkItem("ups [#]", true, new FunctionBinding<GUINet, SUMOReal>(this, &GUINet::getUPS));
00494 ret->mkItem("mean ups [#]", true, new FunctionBinding<GUINet, SUMOReal>(this, &GUINet::getMeanUPS));
00495 }
00496
00497 ret->closeBuilding();
00498 return ret;
00499 }
00500
00501
00502 const std::string &
00503 GUINet::getMicrosimID() const throw() {
00504 return StringUtils::emptyString;
00505 }
00506
00507
00508 void
00509 GUINet::drawGL(const GUIVisualizationSettings &s) const throw() {
00510 }
00511
00512 Boundary
00513 GUINet::getCenteringBoundary() const throw() {
00514 return getBoundary();
00515 }
00516
00517
00518
00519