41 #pragma warning(disable: 4127) // do not warn about constant conditional expression 43 #include <osgViewer/Viewer> 44 #include <osgViewer/ViewerEventHandlers> 45 #include <osgGA/NodeTrackerManipulator> 46 #include <osgDB/ReadFile> 47 #include <osg/PositionAttitudeTransform> 49 #include <osg/ShapeDrawable> 91 FXDEFMAP(GUIOSGView) GUIOSGView_Map[] = {
93 FXMAPFUNC(SEL_CHORE,
MID_CHORE, GUIOSGView::OnIdle)
99 operator<<(std::ostream& os,
const osg::Vec3d& v) {
100 return os << v.x() <<
"," << v.y() <<
"," << v.z();
107 GUIOSGView::Command_TLSChange::Command_TLSChange(
const MSLink*
const link, osg::Switch* switchNode)
113 GUIOSGView::Command_TLSChange::~Command_TLSChange() {}
117 GUIOSGView::Command_TLSChange::execute() {
118 switch (myLink->getState()) {
121 mySwitch->setSingleChildOn(0);
125 mySwitch->setSingleChildOn(1);
128 mySwitch->setSingleChildOn(2);
131 mySwitch->setSingleChildOn(3);
134 mySwitch->setAllChildrenOff();
136 myLastState = myLink->getState();
144 GUIOSGView::GUIOSGView(
148 GUINet& net, FXGLVisual* glVis,
151 myTracked(0), myCameraManipulator(new SUMOTerrainManipulator()), myLastUpdate(-1) {
159 myAdapter =
new FXOSGAdapter(
this,
new FXCursor(parent->getApp(), CURSOR_CROSS));
161 myViewer =
new osgViewer::Viewer();
162 myViewer->getCamera()->setGraphicsContext(myAdapter);
163 myViewer->getCamera()->setViewport(0, 0, w, h);
164 myViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
166 const char* sumoPath = getenv(
"SUMO_HOME");
168 std::string newPath = std::string(sumoPath) +
"/data/3D";
170 osgDB::FilePathList path = osgDB::Registry::instance()->getDataFilePathList();
171 path.push_back(newPath);
172 osgDB::Registry::instance()->setDataFilePathList(path);
176 myGreenLight = osgDB::readNodeFile(
"tlg.obj");
177 myYellowLight = osgDB::readNodeFile(
"tly.obj");
178 myRedLight = osgDB::readNodeFile(
"tlr.obj");
179 myRedYellowLight = osgDB::readNodeFile(
"tlu.obj");
180 if (myGreenLight == 0 || myYellowLight == 0 || myRedLight == 0 || myRedYellowLight == 0) {
181 WRITE_ERROR(
"Could not load traffic light files.");
183 myRoot = GUIOSGBuilder::buildOSGScene(myGreenLight, myYellowLight, myRedLight, myRedYellowLight);
185 myViewer->addEventHandler(
new osgViewer::StatsHandler());
186 myViewer->setSceneData(myRoot);
187 myViewer->setCameraManipulator(myCameraManipulator);
188 osg::Vec3d lookFrom, lookAt, up;
189 myCameraManipulator->getHomePosition(lookFrom, lookAt, up);
190 double z = lookFrom[2];
191 lookFrom[2] = -lookFrom.y();
193 myCameraManipulator->setHomePosition(lookFrom, lookAt, up);
199 GUIOSGView::~GUIOSGView() {
201 myViewer->setDone(
true);
213 for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
215 if ((*i) == myVisualizationSettings->name) {
223 "\tLocate Junction\tLocate a junction within the network.",
225 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
228 "\tLocate Street\tLocate a street within the network.",
230 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
233 "\tLocate Vehicle\tLocate a vehicle within the network.",
235 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
238 "\tLocate Vehicle\tLocate a person within the network.",
240 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
243 "\tLocate TLS\tLocate a tls within the network.",
245 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
248 "\tLocate Additional\tLocate an additional structure within the network.",
250 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
253 "\tLocate POI\tLocate a POI within the network.",
255 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
258 "\tLocate Polygon\tLocate a Polygon within the network.",
260 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
265 GUIOSGView::recenterView() {
268 osg::Vec3d lookFromOSG, lookAtOSG, up;
269 myViewer->getCameraManipulator()->getHomePosition(lookFromOSG, lookAtOSG, up);
270 lookFromOSG[0] = center.
x();
271 lookFromOSG[1] = center.
y();
272 lookFromOSG[2] = myChanger->zoom2ZPos(100);
273 lookAtOSG[0] = center.
x();
274 lookAtOSG[1] = center.
y();
276 myViewer->getCameraManipulator()->setHomePosition(lookFromOSG, lookAtOSG, up);
282 GUIOSGView::centerTo(
GUIGlID id,
bool ,
double ) {
288 GUIOSGView::setColorScheme(
const std::string& name) {
292 if (myVisualizationChanger != 0) {
293 if (myVisualizationChanger->getCurrentScheme() != name) {
294 myVisualizationChanger->setCurrentScheme(name);
298 myVisualizationSettings->
gaming = myApp->isGaming();
305 GUIOSGView::onPaint(FXObject*, FXSelector,
void*) {
310 for (std::vector<GUISUMOAbstractView::Decal>::iterator l = myDecals.begin(); l != myDecals.end(); ++l) {
314 GUIOSGBuilder::buildLight(d, *myRoot);
316 const int linkStringIdx = (int)d.
filename.find(
':', 3);
321 if (linkIdx < 0 || linkIdx >= static_cast<int>(vars.
getActive()->
getLinks().size())) {
325 osg::Switch* switchNode =
new osg::Switch();
326 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myGreenLight, osg::Vec4d(0., 1., 0., .3)),
false);
327 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myYellowLight, osg::Vec4d(1., 1., 0., .3)),
false);
328 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myRedLight, osg::Vec4d(1., 0., 0., .3)),
false);
329 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myRedYellowLight, osg::Vec4d(1., .5, 0., .3)),
false);
330 myRoot->addChild(switchNode);
338 GUIOSGBuilder::buildDecal(d, *myRoot);
343 myDecalsLock.unlock();
350 if (myVehicles.find(veh) == myVehicles.end()) {
351 myVehicles[veh] = GUIOSGBuilder::buildMovable(veh->
getVehicleType());
352 myRoot->addChild(myVehicles[veh].pos);
354 osg::PositionAttitudeTransform* n = myVehicles[veh].pos;
357 const double slope = veh->
getSlope();
358 n->setAttitude(osg::Quat(dir, osg::Vec3d(0, 0, 1)) *
359 osg::Quat(osg::DegreesToRadians(slope), osg::Vec3d(0, 1, 0)));
371 const RGBColor& col = myVisualizationSettings->vehicleColorer.getScheme().getColor(veh->
getColorValue(myVisualizationSettings->vehicleColorer.getActive()));
372 myVehicles[veh].geom->setColor(osg::Vec4d(col.
red() / 255., col.
green() / 255., col.
blue() / 255., col.
alpha() / 255.));
379 if (now != myLastUpdate || (myVisualizationChanger != 0 && myVisualizationChanger->shown())) {
382 if (now != myLastUpdate && myTracked != 0) {
383 osg::Vec3d lookFrom, lookAt, up;
384 lookAt[0] = myTracked->getPosition().x();
385 lookAt[1] = myTracked->getPosition().y();
386 lookAt[2] = myTracked->getPosition().z();
387 const double angle = myTracked->getAngle();
388 lookFrom[0] = lookAt[0] + 50. * cos(angle);
389 lookFrom[1] = lookAt[1] + 50. * sin(angle);
390 lookFrom[2] = lookAt[2] + 10.;
392 m.makeLookAt(lookFrom, lookAt, osg::Z_AXIS);
393 myCameraManipulator->setByInverseMatrix(m);
399 if (myPersons.find(person) == myPersons.end()) {
400 myPersons[person] = GUIOSGBuilder::buildMovable(person->
getVehicleType());
401 myRoot->addChild(myPersons[person].pos);
403 osg::PositionAttitudeTransform* n = myPersons[person].pos;
405 n->setPosition(osg::Vec3d(pos.
x(), pos.
y(), pos.
z()));
407 n->setAttitude(osg::Quat(dir, osg::Vec3d(0, 0, 1)));
409 if (myAdapter->makeCurrent()) {
420 if (myTracked == veh) {
423 std::map<MSVehicle*, OSGMovable>::iterator i = myVehicles.find(veh);
424 if (i != myVehicles.end()) {
425 myRoot->removeChild(i->second.pos);
432 GUIOSGView::showViewportEditor() {
434 osg::Vec3d lookFromOSG, lookAtOSG, up;
435 myViewer->getCameraManipulator()->getInverseMatrix().getLookAt(lookFromOSG, lookAtOSG, up);
436 Position from(lookFromOSG[0], lookFromOSG[1], lookFromOSG[2]), at(lookAtOSG[0], lookAtOSG[1], lookAtOSG[2]);
437 myViewportChooser->setOldValues(from, at);
438 myViewportChooser->show();
443 GUIOSGView::setViewportFromTo(
const Position& lookFrom,
const Position& lookAt) {
444 osg::Vec3d lookFromOSG, lookAtOSG, up;
445 myViewer->getCameraManipulator()->getHomePosition(lookFromOSG, lookAtOSG, up);
446 lookFromOSG[0] = lookFrom.
x();
447 lookFromOSG[1] = lookFrom.
y();
448 lookFromOSG[2] = lookFrom.
z();
449 lookAtOSG[0] = lookAt.
x();
450 lookAtOSG[1] = lookAt.
y();
451 lookAtOSG[2] = lookAt.
z();
452 myViewer->getCameraManipulator()->setHomePosition(lookFromOSG, lookAtOSG, up);
460 osg::Vec3d lookFrom, lookAt, up;
461 myCameraManipulator->getHomePosition(lookFrom, lookAt, up);
463 Position(lookAt[0], lookAt[1], lookAt[2]));
469 GUIOSGView::startTrack(
int id) {
470 if (myTracked == 0 || (
int)myTracked->getGlID() != id) {
475 if ((
int)veh->
getGlID() == id) {
476 if (!veh->
isOnRoad() || myVehicles.find(veh) == myVehicles.end()) {
483 if (myTracked != 0) {
484 osg::Vec3d lookFrom, lookAt, up;
486 lookAt[1] = myTracked->getPosition().y();
487 lookAt[2] = myTracked->getPosition().z();
488 lookFrom[0] = lookAt[0] + 50.;
489 lookFrom[1] = lookAt[1] + 50.;
490 lookFrom[2] = lookAt[2] + 10.;
492 m.makeLookAt(lookFrom, lookAt, osg::Z_AXIS);
493 myCameraManipulator->setByInverseMatrix(m);
500 GUIOSGView::stopTrack() {
506 GUIOSGView::getTrackedID()
const {
512 GUIOSGView::onGamingClick(
Position pos) {
514 const std::vector<MSTrafficLightLogic*>& logics = tlsControl.
getAllLogics();
516 double minDist = std::numeric_limits<double>::infinity();
517 for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
523 if (lanes.size() > 0) {
524 const Position& endPos = lanes[0]->getShape().back();
534 const std::vector<MSTrafficLightLogic*> logics = vars.
getAllLogics();
535 if (logics.size() > 1) {
537 for (
int i = 0; i < (int)logics.size() - 1; i++) {
538 if (minTll->
getProgramID() == logics[i]->getProgramID()) {
543 if (l == logics[0]) {
554 GUIOSGView::getCurrentTimeStep()
const {
559 long GUIOSGView::onConfigure(FXObject* sender, FXSelector sel,
void* ptr) {
561 myAdapter->getEventQueue()->windowResize(0, 0, getWidth(), getHeight());
562 myAdapter->resized(0, 0, getWidth(), getHeight());
564 return FXGLCanvas::onConfigure(sender, sel, ptr);
567 long GUIOSGView::onKeyPress(FXObject* sender, FXSelector sel,
void* ptr) {
568 int key = ((FXEvent*)ptr)->code;
569 myAdapter->getEventQueue()->keyPress(key);
571 return FXGLCanvas::onKeyPress(sender, sel, ptr);
574 long GUIOSGView::onKeyRelease(FXObject* sender, FXSelector sel,
void* ptr) {
575 int key = ((FXEvent*)ptr)->code;
576 myAdapter->getEventQueue()->keyRelease(key);
578 return FXGLCanvas::onKeyRelease(sender, sel, ptr);
581 long GUIOSGView::onLeftBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
582 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
584 FXEvent*
event = (FXEvent*)ptr;
585 myAdapter->getEventQueue()->mouseButtonPress((
float)event->click_x, (
float)event->click_y, 1);
586 if (myApp->isGaming()) {
587 onGamingClick(getPositionInformation());
590 return FXGLCanvas::onLeftBtnPress(sender, sel, ptr);
593 long GUIOSGView::onLeftBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
594 FXEvent*
event = (FXEvent*)ptr;
595 myAdapter->getEventQueue()->mouseButtonRelease((
float)event->click_x, (
float)event->click_y, 1);
597 return FXGLCanvas::onLeftBtnRelease(sender, sel, ptr);
600 long GUIOSGView::onMiddleBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
601 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
603 FXEvent*
event = (FXEvent*)ptr;
604 myAdapter->getEventQueue()->mouseButtonPress((
float)event->click_x, (
float)event->click_y, 2);
606 return FXGLCanvas::onMiddleBtnPress(sender, sel, ptr);
609 long GUIOSGView::onMiddleBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
610 FXEvent*
event = (FXEvent*)ptr;
611 myAdapter->getEventQueue()->mouseButtonRelease((
float)event->click_x, (
float)event->click_y, 2);
613 return FXGLCanvas::onMiddleBtnRelease(sender, sel, ptr);
616 long GUIOSGView::onRightBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
617 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
619 FXEvent*
event = (FXEvent*)ptr;
620 myAdapter->getEventQueue()->mouseButtonPress((
float)event->click_x, (
float)event->click_y, 3);
622 return FXGLCanvas::onRightBtnPress(sender, sel, ptr);
625 long GUIOSGView::onRightBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
626 FXEvent*
event = (FXEvent*)ptr;
627 myAdapter->getEventQueue()->mouseButtonRelease((
float)event->click_x, (
float)event->click_y, 3);
629 return FXGLCanvas::onRightBtnRelease(sender, sel, ptr);
633 GUIOSGView::onMouseMove(FXObject* sender, FXSelector sel,
void* ptr) {
634 FXEvent*
event = (FXEvent*)ptr;
635 myAdapter->getEventQueue()->mouseMotion((
float)event->win_x, (
float)event->win_y);
637 return FXGLCanvas::onMotion(sender, sel, ptr);
641 GUIOSGView::OnIdle(FXObject* , FXSelector ,
void*) {
651 : myParent(parent), myOldCursor(cursor) {
652 _traits =
new GraphicsContext::Traits();
655 _traits->width = parent->getWidth();
656 _traits->height = parent->getHeight();
657 _traits->windowDecoration =
false;
658 _traits->doubleBuffer =
true;
659 _traits->sharedContext = 0;
661 setState(
new osg::State());
662 getState()->setGraphicsContext(
this);
663 if (_traits.valid() && _traits->sharedContext != 0) {
664 getState()->setContextID(_traits->sharedContext->getState()->getContextID());
665 incrementContextIDUsageCount(getState()->getContextID());
667 getState()->setContextID(createNewContextID());
673 GUIOSGView::FXOSGAdapter::~FXOSGAdapter() {
678 void GUIOSGView::FXOSGAdapter::grabFocus() {
680 myParent->setFocus();
683 void GUIOSGView::FXOSGAdapter::useCursor(
bool cursorOn) {
685 myParent->setDefaultCursor(myOldCursor);
687 myParent->setDefaultCursor(NULL);
691 bool GUIOSGView::FXOSGAdapter::makeCurrentImplementation() {
692 myParent->makeCurrent();
696 bool GUIOSGView::FXOSGAdapter::releaseContext() {
697 myParent->makeNonCurrent();
701 void GUIOSGView::FXOSGAdapter::swapBuffersImplementation() {
702 myParent->swapBuffers();
A decal (an image) that can be shown.
The link has green light, may pass.
GUICompleteSchemeStorage gSchemeStorage
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.
virtual void setViewportFromTo(const Position &lookFrom, const Position &lookAt)
applies the given viewport settings
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
unsigned char alpha() const
Returns the alpha-amount of the color.
The link has green light, has to brake.
Locate junction - button.
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.
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.
Locate polygons - button.
std::string filename
The path to the file the image is located at.
MSTransportableControl & getPersonControl()
Returns the person control.
A single child window which contains a view of the simulation area.
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
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.
Locate addtional structure - button.
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.