42 #include <osgViewer/Viewer> 43 #include <osgViewer/ViewerEventHandlers> 44 #include <osgGA/NodeTrackerManipulator> 45 #include <osgDB/ReadFile> 46 #include <osg/PositionAttitudeTransform> 48 #include <osg/ShapeDrawable> 89 #ifdef CHECK_MEMORY_LEAKS 91 #endif // CHECK_MEMORY_LEAKS 94 FXDEFMAP(GUIOSGView) GUIOSGView_Map[] = {
96 FXMAPFUNC(SEL_CHORE,
MID_CHORE, GUIOSGView::OnIdle)
102 operator<<(std::ostream& os, const osg::Vec3d& v) {
103 return os << v.x() <<
"," << v.y() <<
"," << v.z();
110 GUIOSGView::Command_TLSChange::Command_TLSChange(
const MSLink*
const link, osg::Switch* switchNode)
116 GUIOSGView::Command_TLSChange::~Command_TLSChange() {}
120 GUIOSGView::Command_TLSChange::execute() {
121 switch (myLink->getState()) {
124 mySwitch->setSingleChildOn(0);
128 mySwitch->setSingleChildOn(1);
131 mySwitch->setSingleChildOn(2);
134 mySwitch->setSingleChildOn(3);
137 mySwitch->setAllChildrenOff();
139 myLastState = myLink->getState();
147 GUIOSGView::GUIOSGView(
151 GUINet& net, FXGLVisual* glVis,
154 myTracked(0), myCameraManipulator(
new SUMOTerrainManipulator()), myLastUpdate(-1) {
162 myAdapter =
new FXOSGAdapter(
this,
new FXCursor(parent->getApp(), CURSOR_CROSS));
164 myViewer =
new osgViewer::Viewer();
165 myViewer->getCamera()->setGraphicsContext(myAdapter);
166 myViewer->getCamera()->setViewport(0, 0, w, h);
167 myViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
169 const char* sumoPath = getenv(
"SUMO_HOME");
171 std::string newPath = std::string(sumoPath) +
"/data/3D";
173 osgDB::FilePathList path = osgDB::Registry::instance()->getDataFilePathList();
174 path.push_back(newPath);
175 osgDB::Registry::instance()->setDataFilePathList(path);
179 myGreenLight = osgDB::readNodeFile(
"tlg.obj");
180 myYellowLight = osgDB::readNodeFile(
"tly.obj");
181 myRedLight = osgDB::readNodeFile(
"tlr.obj");
182 myRedYellowLight = osgDB::readNodeFile(
"tlu.obj");
183 if (myGreenLight == 0 || myYellowLight == 0 || myRedLight == 0 || myRedYellowLight == 0) {
184 WRITE_ERROR(
"Could not load traffic light files.");
186 myRoot = GUIOSGBuilder::buildOSGScene(myGreenLight, myYellowLight, myRedLight, myRedYellowLight);
188 myViewer->addEventHandler(
new osgViewer::StatsHandler());
189 myViewer->setSceneData(myRoot);
190 myViewer->setCameraManipulator(myCameraManipulator);
191 osg::Vec3d lookFrom, lookAt, up;
192 myCameraManipulator->getHomePosition(lookFrom, lookAt, up);
193 double z = lookFrom[2];
194 lookFrom[2] = -lookFrom.y();
196 myCameraManipulator->setHomePosition(lookFrom, lookAt, up);
202 GUIOSGView::~GUIOSGView() {
204 myViewer->setDone(
true);
216 for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
218 if ((*i) == myVisualizationSettings->name) {
226 "\tLocate Junction\tLocate a junction within the network.",
228 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
231 "\tLocate Street\tLocate a street within the network.",
233 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
236 "\tLocate Vehicle\tLocate a vehicle within the network.",
238 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
241 "\tLocate Vehicle\tLocate a person within the network.",
243 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
246 "\tLocate TLS\tLocate a tls within the network.",
248 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
251 "\tLocate Additional\tLocate an additional structure within the network.",
253 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
256 "\tLocate POI\tLocate a POI within the network.",
258 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
261 "\tLocate Polygon\tLocate a Polygon within the network.",
263 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
268 GUIOSGView::recenterView() {
281 GUIOSGView::setColorScheme(
const std::string& name) {
285 if (myVisualizationChanger != 0) {
286 if (myVisualizationChanger->getCurrentScheme() != name) {
287 myVisualizationChanger->setCurrentScheme(name);
291 myVisualizationSettings->
gaming = myApp->isGaming();
298 GUIOSGView::onPaint(FXObject*, FXSelector,
void*) {
303 for (std::vector<GUISUMOAbstractView::Decal>::iterator l = myDecals.begin(); l != myDecals.end(); ++l) {
307 GUIOSGBuilder::buildLight(d, *myRoot);
309 const int linkStringIdx = d.
filename.find(
':', 3);
314 if (linkIdx < 0 || linkIdx >= static_cast<int>(vars.
getActive()->
getLinks().size())) {
318 osg::Switch* switchNode =
new osg::Switch();
319 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myGreenLight, osg::Vec4(0., 1., 0., .3)),
false);
320 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myYellowLight, osg::Vec4(1., 1., 0., .3)),
false);
321 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myRedLight, osg::Vec4(1., 0., 0., .3)),
false);
322 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myRedYellowLight, osg::Vec4(1., .5, 0., .3)),
false);
323 myRoot->addChild(switchNode);
331 GUIOSGBuilder::buildDecal(d, *myRoot);
336 myDecalsLock.unlock();
343 if (myVehicles.find(veh) == myVehicles.end()) {
344 myVehicles[veh] = GUIOSGBuilder::buildMovable(veh->
getVehicleType());
345 myRoot->addChild(myVehicles[veh].pos);
347 osg::PositionAttitudeTransform* n = myVehicles[veh].pos;
351 n->setAttitude(osg::Quat(dir, osg::Vec3(0, 0, 1)) *
352 osg::Quat(osg::DegreesToRadians(slope), osg::Vec3(0, 1, 0)));
364 const RGBColor& col = myVisualizationSettings->vehicleColorer.getScheme().getColor(veh->
getColorValue(myVisualizationSettings->vehicleColorer.getActive()));
365 myVehicles[veh].geom->setColor(osg::Vec4(col.
red() / 255., col.
green() / 255., col.
blue() / 255., col.
alpha() / 255.));
372 if (now != myLastUpdate || (myVisualizationChanger != 0 && myVisualizationChanger->shown())) {
375 if (now != myLastUpdate && myTracked != 0) {
376 osg::Vec3d lookFrom, lookAt, up;
377 lookAt[0] = myTracked->getPosition().x();
378 lookAt[1] = myTracked->getPosition().y();
379 lookAt[2] = myTracked->getPosition().z();
380 const SUMOReal angle = myTracked->getAngle();
381 lookFrom[0] = lookAt[0] + 50. * cos(angle);
382 lookFrom[1] = lookAt[1] + 50. * sin(angle);
383 lookFrom[2] = lookAt[2] + 10.;
385 m.makeLookAt(lookFrom, lookAt, osg::Z_AXIS);
386 myCameraManipulator->setByInverseMatrix(m);
392 if (myPersons.find(person) == myPersons.end()) {
393 myPersons[person] = GUIOSGBuilder::buildMovable(person->
getVehicleType());
394 myRoot->addChild(myPersons[person].pos);
396 osg::PositionAttitudeTransform* n = myPersons[person].pos;
398 n->setPosition(osg::Vec3(pos.
x(), pos.
y(), pos.
z()));
400 n->setAttitude(osg::Quat(dir, osg::Vec3(0, 0, 1)));
402 if (myAdapter->makeCurrent()) {
413 if (myTracked == veh) {
416 std::map<MSVehicle*, OSGMovable>::iterator i = myVehicles.find(veh);
417 if (i != myVehicles.end()) {
418 myRoot->removeChild(i->second.pos);
425 GUIOSGView::showViewportEditor() {
427 osg::Vec3d lookFromOSG, lookAtOSG, up;
428 myViewer->getCameraManipulator()->getInverseMatrix().getLookAt(lookFromOSG, lookAtOSG, up);
429 Position from(lookFromOSG[0], lookFromOSG[1], lookFromOSG[2]), at(lookAtOSG[0], lookAtOSG[1], lookAtOSG[2]);
430 myViewportChooser->setOldValues(from, at);
431 myViewportChooser->show();
437 osg::Vec3d lookFromOSG, lookAtOSG, up;
438 myViewer->getCameraManipulator()->getHomePosition(lookFromOSG, lookAtOSG, up);
439 lookFromOSG[0] = lookFrom.
x();
440 lookFromOSG[1] = lookFrom.
y();
441 lookFromOSG[2] = lookFrom.
z();
442 lookAtOSG[0] = lookAt.
x();
443 lookAtOSG[1] = lookAt.
y();
444 lookAtOSG[2] = lookAt.
z();
445 myViewer->getCameraManipulator()->setHomePosition(lookFromOSG, lookAtOSG, up);
451 GUIOSGView::startTrack(
int id) {
452 if (myTracked == 0 || (
int)myTracked->getGlID() != id) {
457 if ((
int)veh->
getGlID() == id) {
458 if (!veh->
isOnRoad() || myVehicles.find(veh) == myVehicles.end()) {
465 if (myTracked != 0) {
466 osg::Vec3d lookFrom, lookAt, up;
467 lookAt[0] = myTracked->getPosition().
x();
468 lookAt[1] = myTracked->getPosition().y();
469 lookAt[2] = myTracked->getPosition().z();
470 lookFrom[0] = lookAt[0] + 50.;
471 lookFrom[1] = lookAt[1] + 50.;
472 lookFrom[2] = lookAt[2] + 10.;
474 m.makeLookAt(lookFrom, lookAt, osg::Z_AXIS);
475 myCameraManipulator->setByInverseMatrix(m);
482 GUIOSGView::stopTrack() {
488 GUIOSGView::getTrackedID()
const {
489 return myTracked == 0 ? -1 : myTracked->getGlID();
494 GUIOSGView::onGamingClick(
Position pos) {
496 const std::vector<MSTrafficLightLogic*>& logics = tlsControl.
getAllLogics();
498 SUMOReal minDist = std::numeric_limits<SUMOReal>::infinity();
499 for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
505 if (lanes.size() > 0) {
506 const Position& endPos = lanes[0]->getShape().back();
516 const std::vector<MSTrafficLightLogic*> logics = vars.
getAllLogics();
517 if (logics.size() > 1) {
519 for (
unsigned int i = 0; i < logics.size() - 1; i++) {
520 if (minTll->
getProgramID() == logics[i]->getProgramID()) {
525 if (l == logics[0]) {
536 GUIOSGView::getCurrentTimeStep()
const {
541 long GUIOSGView::onConfigure(FXObject* sender, FXSelector sel,
void* ptr) {
543 myAdapter->getEventQueue()->windowResize(0, 0, getWidth(), getHeight());
544 myAdapter->resized(0, 0, getWidth(), getHeight());
546 return FXGLCanvas::onConfigure(sender, sel, ptr);
549 long GUIOSGView::onKeyPress(FXObject* sender, FXSelector sel,
void* ptr) {
550 int key = ((FXEvent*)ptr)->code;
551 myAdapter->getEventQueue()->keyPress(key);
553 return FXGLCanvas::onKeyPress(sender, sel, ptr);
556 long GUIOSGView::onKeyRelease(FXObject* sender, FXSelector sel,
void* ptr) {
557 int key = ((FXEvent*)ptr)->code;
558 myAdapter->getEventQueue()->keyRelease(key);
560 return FXGLCanvas::onKeyRelease(sender, sel, ptr);
563 long GUIOSGView::onLeftBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
564 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
566 FXEvent*
event = (FXEvent*)ptr;
567 myAdapter->getEventQueue()->mouseButtonPress(event->click_x, event->click_y, 1);
568 if (myApp->isGaming()) {
569 onGamingClick(getPositionInformation());
572 return FXGLCanvas::onLeftBtnPress(sender, sel, ptr);
575 long GUIOSGView::onLeftBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
576 FXEvent*
event = (FXEvent*)ptr;
577 myAdapter->getEventQueue()->mouseButtonRelease(event->click_x, event->click_y, 1);
579 return FXGLCanvas::onLeftBtnRelease(sender, sel, ptr);
582 long GUIOSGView::onMiddleBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
583 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
585 FXEvent*
event = (FXEvent*)ptr;
586 myAdapter->getEventQueue()->mouseButtonPress(event->click_x, event->click_y, 2);
588 return FXGLCanvas::onMiddleBtnPress(sender, sel, ptr);
591 long GUIOSGView::onMiddleBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
592 FXEvent*
event = (FXEvent*)ptr;
593 myAdapter->getEventQueue()->mouseButtonRelease(event->click_x, event->click_y, 2);
595 return FXGLCanvas::onMiddleBtnRelease(sender, sel, ptr);
598 long GUIOSGView::onRightBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
599 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
601 FXEvent*
event = (FXEvent*)ptr;
602 myAdapter->getEventQueue()->mouseButtonPress(event->click_x, event->click_y, 3);
604 return FXGLCanvas::onRightBtnPress(sender, sel, ptr);
607 long GUIOSGView::onRightBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
608 FXEvent*
event = (FXEvent*)ptr;
609 myAdapter->getEventQueue()->mouseButtonRelease(event->click_x, event->click_y, 3);
611 return FXGLCanvas::onRightBtnRelease(sender, sel, ptr);
615 GUIOSGView::onMouseMove(FXObject* sender, FXSelector sel,
void* ptr) {
616 FXEvent*
event = (FXEvent*)ptr;
617 myAdapter->getEventQueue()->mouseMotion(event->win_x, event->win_y);
619 return FXGLCanvas::onMotion(sender, sel, ptr);
623 GUIOSGView::OnIdle(FXObject* , FXSelector ,
void*) {
633 : myParent(parent), myOldCursor(cursor) {
634 _traits =
new GraphicsContext::Traits();
637 _traits->width = parent->getWidth();
638 _traits->height = parent->getHeight();
639 _traits->windowDecoration =
false;
640 _traits->doubleBuffer =
true;
641 _traits->sharedContext = 0;
643 setState(
new osg::State());
644 getState()->setGraphicsContext(
this);
645 if (_traits.valid() && _traits->sharedContext != 0) {
646 getState()->setContextID(_traits->sharedContext->getState()->getContextID());
647 incrementContextIDUsageCount(getState()->getContextID());
649 getState()->setContextID(createNewContextID());
655 GUIOSGView::FXOSGAdapter::~FXOSGAdapter() {
660 void GUIOSGView::FXOSGAdapter::grabFocus() {
662 myParent->setFocus();
665 void GUIOSGView::FXOSGAdapter::useCursor(
bool cursorOn) {
667 myParent->setDefaultCursor(myOldCursor);
669 myParent->setDefaultCursor(NULL);
673 bool GUIOSGView::FXOSGAdapter::makeCurrentImplementation() {
674 myParent->makeCurrent();
678 bool GUIOSGView::FXOSGAdapter::releaseContext() {
679 myParent->makeNonCurrent();
683 void GUIOSGView::FXOSGAdapter::swapBuffersImplementation() {
684 myParent->swapBuffers();
bool signalSet(int which) const
Returns whether the given signal is on.
A decal (an image) that can be shown.
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, unsigned int step, SUMOTime stepDuration)
Changes the current phase and her duration.
The link has green light, may pass.
bool isActive(const MSTrafficLightLogic *tl) const
Returns whether the given tls program is the currently active for his tls.
SUMOReal getColorValue(size_t activeScheme) const
gets the color value according to the current scheme index
GUICompleteSchemeStorage gSchemeStorage
Storage for all programs of a single tls.
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.
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
bool gaming
whether the application is in gaming mode or not
The link has green light, has to brake.
constVehIt loadedPersonsEnd() const
Returns the end of the internal persons map.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
SUMOReal distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
The link is controlled by a tls which is off, not blinking, may pass.
virtual SUMOReal getAngle() const
return the current angle of the transportable
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
SUMORTree & getVisualisationSpeedUp()
Returns the RTree used for visualisation speed-up.
SUMOReal x() const
Returns the x-position.
GUIGlID getGlID() const
Returns the numerical id of the object.
FXDEFMAP(GUIDialog_AppSettings) GUIDialog_AppSettingsMap[]
A fixed traffic light logic.
Right blinker lights are switched on.
unsigned char blue() const
Returns the blue-amount of the color.
A class that stores and controls tls and switching of their programs.
std::vector< MSTrafficLightLogic * > getAllLogics() const
Locate addtional structure - button.
const std::string & getID() const
Returns the id.
SUMOTime duration
The duration of the phase.
const std::vector< std::string > & getNames() const
Returns a list of stored settings names.
const MSVehicleType & getVehicleType() const
void addSwitchCommand(OnSwitchAction *c)
Left blinker lights are switched on.
MSTrafficLightLogic * getActive() const
static GUINet * getGUIInstance()
Returns the pointer to the unique instance of GUINet (singleton).
A point in 2D or 3D with translation and scaling methods.
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
FXComboBox & getColoringSchemesCombo()
const LaneVector & getLanesAt(unsigned int i) const
Returns the list of lanes that are controlled by the signals at the given position.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
unsigned char alpha() const
Returns the alpha-amount of the color.
bool contains(const std::string &name) const
Returns the information whether a setting with the given name is stored.
const LinkVector & getLinksAt(unsigned int i) const
Returns the list of links that are controlled by the signals at the given position.
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
SUMOReal z() const
Returns the z-position.
GUIVisualizationSettings & get(const std::string &name)
Returns the named scheme.
Blinker lights on both sides are switched on.
bool initialised
Whether this image was initialised (inserted as a texture)
Locate junction - button.
std::string filename
The path to the file the image is located at.
A single child window which contains a view of the simulation area.
static int _2int(const E *const data)
std::vector< MSLane * > LaneVector
Definition of the list of links that participate in this tl-light.
A MSNet extended by some values for usage within the gui.
SUMOReal getSlope() const
Returns the slope of the road at vehicle's position.
virtual MSPersonControl & getPersonControl()
Returns the person control.
The link has yellow light, may pass.
SUMOReal y() const
Returns the y-position.
The link has red light (must brake)
Locate polygons - button.
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
virtual Position getPosition() const
Return the Network coordinate of the transportable.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
Position getPosition(const SUMOReal offset=0) const
Return current position (x/y, cartesian)
SUMOReal layer
The layer of the image.
The parent class for traffic light logics.
unsigned char green() const
Returns the green-amount of the color.
const std::string & getProgramID() const
Returns this tl-logic's id.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
std::vector< MSTrafficLightLogic * > getAllLogics() const
Returns a vector which contains all logics.
The link has yellow light, has to brake anyway.
bool isOnRoad() const
Returns the information whether the vehicle is on a road (is simulated)
unsigned char red() const
Returns the red-amount of the color.
SUMOReal getAngle() const
Return current angle.
static FXIcon * getIcon(GUIIcon which)
The link has red light (must brake) but indicates upcoming green.
FXPopup * getLocatorPopup()
MSPersonControl & getPersonControl()
Returns the person control.
A MSVehicle extended by some values for usage within the gui.