35 #pragma warning(disable: 4127) // do not warn about constant conditional expression 37 #include <osgViewer/Viewer> 38 #include <osgViewer/ViewerEventHandlers> 39 #include <osgGA/NodeTrackerManipulator> 40 #include <osgDB/ReadFile> 41 #include <osg/PositionAttitudeTransform> 43 #include <osg/ShapeDrawable> 85 FXDEFMAP(GUIOSGView) GUIOSGView_Map[] = {
87 FXMAPFUNC(SEL_CHORE,
MID_CHORE, GUIOSGView::OnIdle)
93 operator<<(std::ostream& os,
const osg::Vec3d& v) {
94 return os << v.x() <<
"," << v.y() <<
"," << v.z();
101 GUIOSGView::Command_TLSChange::Command_TLSChange(
const MSLink*
const link, osg::Switch* switchNode)
107 GUIOSGView::Command_TLSChange::~Command_TLSChange() {}
111 GUIOSGView::Command_TLSChange::execute() {
112 switch (myLink->getState()) {
115 mySwitch->setSingleChildOn(0);
119 mySwitch->setSingleChildOn(1);
122 mySwitch->setSingleChildOn(2);
125 mySwitch->setSingleChildOn(3);
128 mySwitch->setAllChildrenOff();
130 myLastState = myLink->getState();
138 GUIOSGView::GUIOSGView(
142 GUINet& net, FXGLVisual* glVis,
145 myTracked(0), myCameraManipulator(new SUMOTerrainManipulator()), myLastUpdate(-1) {
153 myAdapter =
new FXOSGAdapter(
this,
new FXCursor(parent->getApp(), CURSOR_CROSS));
155 myViewer =
new osgViewer::Viewer();
156 myViewer->getCamera()->setGraphicsContext(myAdapter);
157 myViewer->getCamera()->setViewport(0, 0, w, h);
158 myViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
160 const char* sumoPath = getenv(
"SUMO_HOME");
162 std::string newPath = std::string(sumoPath) +
"/data/3D";
164 osgDB::FilePathList path = osgDB::Registry::instance()->getDataFilePathList();
165 path.push_back(newPath);
166 osgDB::Registry::instance()->setDataFilePathList(path);
170 myGreenLight = osgDB::readNodeFile(
"tlg.obj");
171 myYellowLight = osgDB::readNodeFile(
"tly.obj");
172 myRedLight = osgDB::readNodeFile(
"tlr.obj");
173 myRedYellowLight = osgDB::readNodeFile(
"tlu.obj");
174 if (myGreenLight == 0 || myYellowLight == 0 || myRedLight == 0 || myRedYellowLight == 0) {
175 WRITE_ERROR(
"Could not load traffic light files.");
177 myRoot = GUIOSGBuilder::buildOSGScene(myGreenLight, myYellowLight, myRedLight, myRedYellowLight);
179 myViewer->addEventHandler(
new osgViewer::StatsHandler());
180 myViewer->setSceneData(myRoot);
181 myViewer->setCameraManipulator(myCameraManipulator);
182 osg::Vec3d lookFrom, lookAt, up;
183 myCameraManipulator->getHomePosition(lookFrom, lookAt, up);
184 double z = lookFrom[2];
185 lookFrom[2] = -lookFrom.y();
187 myCameraManipulator->setHomePosition(lookFrom, lookAt, up);
193 GUIOSGView::~GUIOSGView() {
195 myViewer->setDone(
true);
207 for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
209 if ((*i) == myVisualizationSettings->name) {
217 "\tLocate Junction\tLocate a junction within the network.",
219 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
222 "\tLocate Street\tLocate a street within the network.",
224 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
227 "\tLocate Vehicle\tLocate a vehicle within the network.",
229 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
232 "\tLocate Vehicle\tLocate a person within the network.",
234 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
237 "\tLocate TLS\tLocate a tls within the network.",
239 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
242 "\tLocate Additional\tLocate an additional structure within the network.",
244 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
247 "\tLocate POI\tLocate a POI within the network.",
249 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
252 "\tLocate Polygon\tLocate a Polygon within the network.",
254 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
259 GUIOSGView::recenterView() {
262 osg::Vec3d lookFromOSG, lookAtOSG, up;
263 myViewer->getCameraManipulator()->getHomePosition(lookFromOSG, lookAtOSG, up);
264 lookFromOSG[0] = center.
x();
265 lookFromOSG[1] = center.
y();
266 lookFromOSG[2] = myChanger->zoom2ZPos(100);
267 lookAtOSG[0] = center.
x();
268 lookAtOSG[1] = center.
y();
270 myViewer->getCameraManipulator()->setHomePosition(lookFromOSG, lookAtOSG, up);
276 GUIOSGView::centerTo(
GUIGlID id,
bool ,
double ) {
282 GUIOSGView::setColorScheme(
const std::string& name) {
286 if (myVisualizationChanger != 0) {
287 if (myVisualizationChanger->getCurrentScheme() != name) {
288 myVisualizationChanger->setCurrentScheme(name);
292 myVisualizationSettings->
gaming = myApp->isGaming();
299 GUIOSGView::onPaint(FXObject*, FXSelector,
void*) {
304 for (std::vector<GUISUMOAbstractView::Decal>::iterator l = myDecals.begin(); l != myDecals.end(); ++l) {
308 GUIOSGBuilder::buildLight(d, *myRoot);
310 const int linkStringIdx = (int)d.
filename.find(
':', 3);
315 if (linkIdx < 0 || linkIdx >= static_cast<int>(vars.
getActive()->
getLinks().size())) {
319 osg::Switch* switchNode =
new osg::Switch();
320 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myGreenLight, osg::Vec4d(0., 1., 0., .3)),
false);
321 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myYellowLight, osg::Vec4d(1., 1., 0., .3)),
false);
322 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myRedLight, osg::Vec4d(1., 0., 0., .3)),
false);
323 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myRedYellowLight, osg::Vec4d(1., .5, 0., .3)),
false);
324 myRoot->addChild(switchNode);
332 GUIOSGBuilder::buildDecal(d, *myRoot);
337 myDecalsLock.unlock();
340 for (
auto& item : myVehicles) {
341 item.second.active =
false;
348 auto itVeh = myVehicles.find(veh);
349 if (itVeh == myVehicles.end()) {
350 myVehicles[veh] = GUIOSGBuilder::buildMovable(veh->
getVehicleType());
351 myRoot->addChild(myVehicles[veh].pos);
353 itVeh->second.active =
true;
355 osg::PositionAttitudeTransform* n = myVehicles[veh].pos;
358 const double slope = veh->
getSlope();
359 n->setAttitude(osg::Quat(dir, osg::Vec3d(0, 0, 1)) *
360 osg::Quat(osg::DegreesToRadians(slope), osg::Vec3d(0, 1, 0)));
372 const RGBColor& col = myVisualizationSettings->vehicleColorer.getScheme().getColor(veh->
getColorValue(myVisualizationSettings->vehicleColorer.getActive()));
373 myVehicles[veh].geom->setColor(osg::Vec4d(col.
red() / 255., col.
green() / 255., col.
blue() / 255., col.
alpha() / 255.));
379 for (
auto& item : myVehicles) {
380 if (!item.second.active) {
381 removeVeh(item.first);
386 if (now != myLastUpdate || (myVisualizationChanger != 0 && myVisualizationChanger->shown())) {
389 if (now != myLastUpdate && myTracked != 0) {
390 osg::Vec3d lookFrom, lookAt, up;
391 lookAt[0] = myTracked->getPosition().x();
392 lookAt[1] = myTracked->getPosition().y();
393 lookAt[2] = myTracked->getPosition().z();
394 const double angle = myTracked->getAngle();
395 lookFrom[0] = lookAt[0] + 50. * cos(angle);
396 lookFrom[1] = lookAt[1] + 50. * sin(angle);
397 lookFrom[2] = lookAt[2] + 10.;
399 m.makeLookAt(lookFrom, lookAt, osg::Z_AXIS);
400 myCameraManipulator->setByInverseMatrix(m);
404 for (
auto& item : myPersons) {
405 item.second.active =
false;
414 auto itPers = myPersons.find(person);
415 if (itPers == myPersons.end()) {
416 myPersons[person] = GUIOSGBuilder::buildMovable(person->
getVehicleType());
417 myRoot->addChild(myPersons[person].pos);
419 itPers->second.active =
true;
421 osg::PositionAttitudeTransform* n = myPersons[person].pos;
423 n->setPosition(osg::Vec3d(pos.
x(), pos.
y(), pos.
z()));
425 n->setAttitude(osg::Quat(dir, osg::Vec3d(0, 0, 1)));
428 for (
auto& item : myPersons) {
429 if (!item.second.active) {
430 removeTransportable(item.first);
433 if (myAdapter->makeCurrent()) {
444 if (myTracked == veh) {
447 std::map<MSVehicle*, OSGMovable>::iterator i = myVehicles.find(veh);
448 if (i != myVehicles.end()) {
449 myRoot->removeChild(i->second.pos);
457 std::map<MSTransportable*, OSGMovable>::iterator i = myPersons.find(t);
458 if (i != myPersons.end()) {
459 myRoot->removeChild(i->second.pos);
466 GUIOSGView::showViewportEditor() {
468 osg::Vec3d lookFromOSG, lookAtOSG, up;
469 myViewer->getCameraManipulator()->getInverseMatrix().getLookAt(lookFromOSG, lookAtOSG, up);
470 Position from(lookFromOSG[0], lookFromOSG[1], lookFromOSG[2]), at(lookAtOSG[0], lookAtOSG[1], lookAtOSG[2]);
471 myViewportChooser->setOldValues(from, at, 0);
472 myViewportChooser->show();
477 GUIOSGView::setViewportFromToRot(
const Position& lookFrom,
const Position& lookAt,
double ) {
478 osg::Vec3d lookFromOSG, lookAtOSG, up;
479 myViewer->getCameraManipulator()->getHomePosition(lookFromOSG, lookAtOSG, up);
480 lookFromOSG[0] = lookFrom.
x();
481 lookFromOSG[1] = lookFrom.
y();
482 lookFromOSG[2] = lookFrom.
z();
483 lookAtOSG[0] = lookAt.
x();
484 lookAtOSG[1] = lookAt.
y();
485 lookAtOSG[2] = lookAt.
z();
486 myViewer->getCameraManipulator()->setHomePosition(lookFromOSG, lookAtOSG, up);
494 osg::Vec3d lookFrom, lookAt, up;
495 myCameraManipulator->getHomePosition(lookFrom, lookAt, up);
497 Position(lookAt[0], lookAt[1], lookAt[2]), 0);
503 GUIOSGView::startTrack(
int id) {
504 if (myTracked == 0 || (
int)myTracked->getGlID() != id) {
509 if ((
int)veh->
getGlID() == id) {
510 if (!veh->
isOnRoad() || myVehicles.find(veh) == myVehicles.end()) {
517 if (myTracked != 0) {
518 osg::Vec3d lookFrom, lookAt, up;
520 lookAt[1] = myTracked->getPosition().y();
521 lookAt[2] = myTracked->getPosition().z();
522 lookFrom[0] = lookAt[0] + 50.;
523 lookFrom[1] = lookAt[1] + 50.;
524 lookFrom[2] = lookAt[2] + 10.;
526 m.makeLookAt(lookFrom, lookAt, osg::Z_AXIS);
527 myCameraManipulator->setByInverseMatrix(m);
534 GUIOSGView::stopTrack() {
540 GUIOSGView::getTrackedID()
const {
546 GUIOSGView::onGamingClick(
Position pos) {
548 const std::vector<MSTrafficLightLogic*>& logics = tlsControl.
getAllLogics();
550 double minDist = std::numeric_limits<double>::infinity();
551 for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
557 if (lanes.size() > 0) {
558 const Position& endPos = lanes[0]->getShape().back();
568 const std::vector<MSTrafficLightLogic*> logics = vars.
getAllLogics();
569 if (logics.size() > 1) {
571 for (
int i = 0; i < (int)logics.size() - 1; i++) {
572 if (minTll->
getProgramID() == logics[i]->getProgramID()) {
577 if (l == logics[0]) {
588 GUIOSGView::getCurrentTimeStep()
const {
593 long GUIOSGView::onConfigure(FXObject* sender, FXSelector sel,
void* ptr) {
595 myAdapter->getEventQueue()->windowResize(0, 0, getWidth(), getHeight());
596 myAdapter->resized(0, 0, getWidth(), getHeight());
598 return FXGLCanvas::onConfigure(sender, sel, ptr);
601 long GUIOSGView::onKeyPress(FXObject* sender, FXSelector sel,
void* ptr) {
602 int key = ((FXEvent*)ptr)->code;
603 myAdapter->getEventQueue()->keyPress(key);
605 return FXGLCanvas::onKeyPress(sender, sel, ptr);
608 long GUIOSGView::onKeyRelease(FXObject* sender, FXSelector sel,
void* ptr) {
609 int key = ((FXEvent*)ptr)->code;
610 myAdapter->getEventQueue()->keyRelease(key);
612 return FXGLCanvas::onKeyRelease(sender, sel, ptr);
615 long GUIOSGView::onLeftBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
616 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
618 FXEvent*
event = (FXEvent*)ptr;
619 myAdapter->getEventQueue()->mouseButtonPress((
float)event->click_x, (
float)event->click_y, 1);
620 if (myApp->isGaming()) {
621 onGamingClick(getPositionInformation());
624 return FXGLCanvas::onLeftBtnPress(sender, sel, ptr);
627 long GUIOSGView::onLeftBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
628 FXEvent*
event = (FXEvent*)ptr;
629 myAdapter->getEventQueue()->mouseButtonRelease((
float)event->click_x, (
float)event->click_y, 1);
631 return FXGLCanvas::onLeftBtnRelease(sender, sel, ptr);
634 long GUIOSGView::onMiddleBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
635 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
637 FXEvent*
event = (FXEvent*)ptr;
638 myAdapter->getEventQueue()->mouseButtonPress((
float)event->click_x, (
float)event->click_y, 2);
640 return FXGLCanvas::onMiddleBtnPress(sender, sel, ptr);
643 long GUIOSGView::onMiddleBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
644 FXEvent*
event = (FXEvent*)ptr;
645 myAdapter->getEventQueue()->mouseButtonRelease((
float)event->click_x, (
float)event->click_y, 2);
647 return FXGLCanvas::onMiddleBtnRelease(sender, sel, ptr);
650 long GUIOSGView::onRightBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
651 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
653 FXEvent*
event = (FXEvent*)ptr;
654 myAdapter->getEventQueue()->mouseButtonPress((
float)event->click_x, (
float)event->click_y, 3);
656 return FXGLCanvas::onRightBtnPress(sender, sel, ptr);
659 long GUIOSGView::onRightBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
660 FXEvent*
event = (FXEvent*)ptr;
661 myAdapter->getEventQueue()->mouseButtonRelease((
float)event->click_x, (
float)event->click_y, 3);
663 return FXGLCanvas::onRightBtnRelease(sender, sel, ptr);
667 GUIOSGView::onMouseMove(FXObject* sender, FXSelector sel,
void* ptr) {
668 FXEvent*
event = (FXEvent*)ptr;
669 myAdapter->getEventQueue()->mouseMotion((
float)event->win_x, (
float)event->win_y);
671 return FXGLCanvas::onMotion(sender, sel, ptr);
675 GUIOSGView::OnIdle(FXObject* , FXSelector ,
void*) {
685 : myParent(parent), myOldCursor(cursor) {
686 _traits =
new GraphicsContext::Traits();
689 _traits->width = parent->getWidth();
690 _traits->height = parent->getHeight();
691 _traits->windowDecoration =
false;
692 _traits->doubleBuffer =
true;
693 _traits->sharedContext = 0;
695 setState(
new osg::State());
696 getState()->setGraphicsContext(
this);
697 if (_traits.valid() && _traits->sharedContext != 0) {
698 getState()->setContextID(_traits->sharedContext->getState()->getContextID());
699 incrementContextIDUsageCount(getState()->getContextID());
701 getState()->setContextID(createNewContextID());
707 GUIOSGView::FXOSGAdapter::~FXOSGAdapter() {
712 void GUIOSGView::FXOSGAdapter::grabFocus() {
714 myParent->setFocus();
717 void GUIOSGView::FXOSGAdapter::useCursor(
bool cursorOn) {
719 myParent->setDefaultCursor(myOldCursor);
721 myParent->setDefaultCursor(NULL);
725 bool GUIOSGView::FXOSGAdapter::makeCurrentImplementation() {
726 myParent->makeCurrent();
730 bool GUIOSGView::FXOSGAdapter::releaseContext() {
731 myParent->makeNonCurrent();
735 void GUIOSGView::FXOSGAdapter::swapBuffersImplementation() {
736 myParent->swapBuffers();
Locate junction - button.
A decal (an image) that can be shown.
The link has green light, may pass.
GUICompleteSchemeStorage gSchemeStorage
Representation of a vehicle in the micro simulation.
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
Storage for all programs of a single tls.
Position getPosition(const double offset=0) const
Return current position (x/y, cartesian)
double z() const
Returns the z-position.
static bool isReadable(std::string path)
Checks whether the given file is readable.
void switchTo(const std::string &id, const std::string &programID)
Switches the named (id) tls to the named (programID) program.
const std::vector< std::string > & getNames() const
Returns a list of stored settings names.
bool gaming
whether the application is in gaming mode or not
bool hasArrived() const
return whether the person has reached the end of its plan
Locate addtional structure - button.
unsigned char alpha() const
Returns the alpha-amount of the color.
The link has green light, has to brake.
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
LayeredRTree myGrid
The visualization speed-up.
double y() const
Returns the y-position.
double x() const
Returns the x-position.
double getColorValue(int activeScheme) const
gets the color value according to the current scheme index
bool isActive(const MSTrafficLightLogic *tl) const
Returns whether the given tls program is the currently active for his tls.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
unsigned char blue() const
Returns the blue-amount of the color.
const std::string & getID() const
Returns the id.
The link is controlled by a tls which is off, not blinking, may pass.
SUMORTree & getVisualisationSpeedUp()
Returns the RTree used for visualisation speed-up.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
FXDEFMAP(GUIDialog_AppSettings) GUIDialog_AppSettingsMap[]
A fixed traffic light logic.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
double layer
The layer of the image.
Right blinker lights are switched on.
A class that stores and controls tls and switching of their programs.
bool signalSet(int which) const
Returns whether the given signal is on.
bool isOnRoad() const
Returns the information whether the vehicle is on a road (is simulated)
SUMOTime duration
The duration of the phase.
std::vector< MSTrafficLightLogic * > getAllLogics() const
void addSwitchCommand(OnSwitchAction *c)
Left blinker lights are switched on.
Locate polygons - button.
const std::string & getProgramID() const
Returns this tl-logic's id.
static GUINet * getGUIInstance()
Returns the pointer to the unique instance of GUINet (singleton).
virtual MSTransportableControl & getPersonControl()
Returns the person control.
A point in 2D or 3D with translation and scaling methods.
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
virtual double getAngle() const
return the current angle of the transportable
FXComboBox & getColoringSchemesCombo()
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
GUIVisualizationSettings & get(const std::string &name)
Returns the named scheme.
Blinker lights on both sides are switched on.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
bool initialised
Whether this image was initialised (inserted as a texture)
std::ostream & operator<<(std::ostream &out, MSDevice_SSM::EncounterType type)
Nicer output for EncounterType enum.
std::string filename
The path to the file the image is located at.
virtual void setViewportFromToRot(const Position &lookFrom, const Position &lookAt, double rotation)
applies the given viewport settings
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
MSTransportableControl & getPersonControl()
Returns the person control.
A single child window which contains a view of the simulation area.
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
A MSNet extended by some values for usage within the gui.
The link has yellow light, may pass.
virtual Position getPosition() const
Return the Network coordinate of the transportable.
bool contains(const std::string &name) const
Returns the information whether a setting with the given name is stored.
unsigned char green() const
Returns the green-amount of the color.
The link has red light (must brake)
static const GUIGlID INVALID_ID
constVehIt loadedEnd() const
Returns the end of the internal transportables map.
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
GUIGlID getGlID() const
Returns the numerical id of the object.
Position getCenter() const
Returns the center of the boundary.
unsigned char red() const
Returns the red-amount of the color.
const MSVehicleType & getVehicleType() const
The parent class for traffic light logics.
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
double getSlope() const
Returns the slope of the road at vehicle's position.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
The link has yellow light, has to brake anyway.
MSTrafficLightLogic * getActive() const
std::vector< MSTrafficLightLogic * > getAllLogics() const
Returns a vector which contains all logics.
static FXIcon * getIcon(GUIIcon which)
returns a icon previously defined in the enum GUIIcon
The link has red light (must brake) but indicates upcoming green.
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
double getAngle() const
Return current angle.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
FXPopup * getLocatorPopup()
A MSVehicle extended by some values for usage within the gui.