42 #pragma warning(disable: 4127) // do not warn about constant conditional expression 44 #include <osgViewer/Viewer> 45 #include <osgViewer/ViewerEventHandlers> 46 #include <osgGA/NodeTrackerManipulator> 47 #include <osgDB/ReadFile> 48 #include <osg/PositionAttitudeTransform> 50 #include <osg/ShapeDrawable> 93 FXDEFMAP(GUIOSGView) GUIOSGView_Map[] = {
95 FXMAPFUNC(SEL_CHORE,
MID_CHORE, GUIOSGView::OnIdle)
101 operator<<(std::ostream& os,
const osg::Vec3d& v) {
102 return os << v.x() <<
"," << v.y() <<
"," << v.z();
109 GUIOSGView::Command_TLSChange::Command_TLSChange(
const MSLink*
const link, osg::Switch* switchNode)
115 GUIOSGView::Command_TLSChange::~Command_TLSChange() {}
119 GUIOSGView::Command_TLSChange::execute() {
120 switch (myLink->getState()) {
123 mySwitch->setSingleChildOn(0);
127 mySwitch->setSingleChildOn(1);
130 mySwitch->setSingleChildOn(2);
133 mySwitch->setSingleChildOn(3);
136 mySwitch->setAllChildrenOff();
138 myLastState = myLink->getState();
146 GUIOSGView::GUIOSGView(
150 GUINet& net, FXGLVisual* glVis,
153 myTracked(0), myCameraManipulator(new SUMOTerrainManipulator()), myLastUpdate(-1) {
161 myAdapter =
new FXOSGAdapter(
this,
new FXCursor(parent->getApp(), CURSOR_CROSS));
163 myViewer =
new osgViewer::Viewer();
164 myViewer->getCamera()->setGraphicsContext(myAdapter);
165 myViewer->getCamera()->setViewport(0, 0, w, h);
166 myViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
168 const char* sumoPath = getenv(
"SUMO_HOME");
170 std::string newPath = std::string(sumoPath) +
"/data/3D";
172 osgDB::FilePathList path = osgDB::Registry::instance()->getDataFilePathList();
173 path.push_back(newPath);
174 osgDB::Registry::instance()->setDataFilePathList(path);
178 myGreenLight = osgDB::readNodeFile(
"tlg.obj");
179 myYellowLight = osgDB::readNodeFile(
"tly.obj");
180 myRedLight = osgDB::readNodeFile(
"tlr.obj");
181 myRedYellowLight = osgDB::readNodeFile(
"tlu.obj");
182 if (myGreenLight == 0 || myYellowLight == 0 || myRedLight == 0 || myRedYellowLight == 0) {
183 WRITE_ERROR(
"Could not load traffic light files.");
185 myRoot = GUIOSGBuilder::buildOSGScene(myGreenLight, myYellowLight, myRedLight, myRedYellowLight);
187 myViewer->addEventHandler(
new osgViewer::StatsHandler());
188 myViewer->setSceneData(myRoot);
189 myViewer->setCameraManipulator(myCameraManipulator);
190 osg::Vec3d lookFrom, lookAt, up;
191 myCameraManipulator->getHomePosition(lookFrom, lookAt, up);
192 double z = lookFrom[2];
193 lookFrom[2] = -lookFrom.y();
195 myCameraManipulator->setHomePosition(lookFrom, lookAt, up);
201 GUIOSGView::~GUIOSGView() {
203 myViewer->setDone(
true);
215 for (std::vector<std::string>::const_iterator i = names.begin(); i != names.end(); ++i) {
217 if ((*i) == myVisualizationSettings->name) {
225 "\tLocate Junction\tLocate a junction within the network.",
227 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
230 "\tLocate Street\tLocate a street within the network.",
232 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
235 "\tLocate Vehicle\tLocate a vehicle within the network.",
237 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
240 "\tLocate Vehicle\tLocate a person within the network.",
242 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
245 "\tLocate TLS\tLocate a tls within the network.",
247 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
250 "\tLocate Additional\tLocate an additional structure within the network.",
252 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
255 "\tLocate POI\tLocate a POI within the network.",
257 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
260 "\tLocate Polygon\tLocate a Polygon within the network.",
262 ICON_ABOVE_TEXT | FRAME_THICK | FRAME_RAISED);
267 GUIOSGView::recenterView() {
270 osg::Vec3d lookFromOSG, lookAtOSG, up;
271 myViewer->getCameraManipulator()->getHomePosition(lookFromOSG, lookAtOSG, up);
272 lookFromOSG[0] = center.
x();
273 lookFromOSG[1] = center.
y();
274 lookFromOSG[2] = myChanger->zoom2ZPos(100);
275 lookAtOSG[0] = center.
x();
276 lookAtOSG[1] = center.
y();
278 myViewer->getCameraManipulator()->setHomePosition(lookFromOSG, lookAtOSG, up);
284 GUIOSGView::centerTo(
GUIGlID id,
bool ,
double ) {
290 GUIOSGView::setColorScheme(
const std::string& name) {
294 if (myVisualizationChanger != 0) {
295 if (myVisualizationChanger->getCurrentScheme() != name) {
296 myVisualizationChanger->setCurrentScheme(name);
300 myVisualizationSettings->
gaming = myApp->isGaming();
307 GUIOSGView::onPaint(FXObject*, FXSelector,
void*) {
312 for (std::vector<GUISUMOAbstractView::Decal>::iterator l = myDecals.begin(); l != myDecals.end(); ++l) {
316 GUIOSGBuilder::buildLight(d, *myRoot);
318 const int linkStringIdx = (int)d.
filename.find(
':', 3);
323 if (linkIdx < 0 || linkIdx >= static_cast<int>(vars.
getActive()->
getLinks().size())) {
327 osg::Switch* switchNode =
new osg::Switch();
328 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myGreenLight, osg::Vec4d(0., 1., 0., .3)),
false);
329 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myYellowLight, osg::Vec4d(1., 1., 0., .3)),
false);
330 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myRedLight, osg::Vec4d(1., 0., 0., .3)),
false);
331 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myRedYellowLight, osg::Vec4d(1., .5, 0., .3)),
false);
332 myRoot->addChild(switchNode);
340 GUIOSGBuilder::buildDecal(d, *myRoot);
345 myDecalsLock.unlock();
352 if (myVehicles.find(veh) == myVehicles.end()) {
353 myVehicles[veh] = GUIOSGBuilder::buildMovable(veh->
getVehicleType());
354 myRoot->addChild(myVehicles[veh].pos);
356 osg::PositionAttitudeTransform* n = myVehicles[veh].pos;
359 const double slope = veh->
getSlope();
360 n->setAttitude(osg::Quat(dir, osg::Vec3d(0, 0, 1)) *
361 osg::Quat(osg::DegreesToRadians(slope), osg::Vec3d(0, 1, 0)));
373 const RGBColor& col = myVisualizationSettings->vehicleColorer.getScheme().getColor(veh->
getColorValue(myVisualizationSettings->vehicleColorer.getActive()));
374 myVehicles[veh].geom->setColor(osg::Vec4d(col.
red() / 255., col.
green() / 255., col.
blue() / 255., col.
alpha() / 255.));
381 if (now != myLastUpdate || (myVisualizationChanger != 0 && myVisualizationChanger->shown())) {
384 if (now != myLastUpdate && myTracked != 0) {
385 osg::Vec3d lookFrom, lookAt, up;
386 lookAt[0] = myTracked->getPosition().x();
387 lookAt[1] = myTracked->getPosition().y();
388 lookAt[2] = myTracked->getPosition().z();
389 const double angle = myTracked->getAngle();
390 lookFrom[0] = lookAt[0] + 50. * cos(angle);
391 lookFrom[1] = lookAt[1] + 50. * sin(angle);
392 lookFrom[2] = lookAt[2] + 10.;
394 m.makeLookAt(lookFrom, lookAt, osg::Z_AXIS);
395 myCameraManipulator->setByInverseMatrix(m);
401 if (myPersons.find(person) == myPersons.end()) {
402 myPersons[person] = GUIOSGBuilder::buildMovable(person->
getVehicleType());
403 myRoot->addChild(myPersons[person].pos);
405 osg::PositionAttitudeTransform* n = myPersons[person].pos;
407 n->setPosition(osg::Vec3d(pos.
x(), pos.
y(), pos.
z()));
408 const double dir = person->
getAngle() +
PI / 2.;
409 n->setAttitude(osg::Quat(dir, osg::Vec3d(0, 0, 1)));
411 if (myAdapter->makeCurrent()) {
422 if (myTracked == veh) {
425 std::map<MSVehicle*, OSGMovable>::iterator i = myVehicles.find(veh);
426 if (i != myVehicles.end()) {
427 myRoot->removeChild(i->second.pos);
434 GUIOSGView::showViewportEditor() {
436 osg::Vec3d lookFromOSG, lookAtOSG, up;
437 myViewer->getCameraManipulator()->getInverseMatrix().getLookAt(lookFromOSG, lookAtOSG, up);
438 Position from(lookFromOSG[0], lookFromOSG[1], lookFromOSG[2]), at(lookAtOSG[0], lookAtOSG[1], lookAtOSG[2]);
439 myViewportChooser->setOldValues(from, at);
440 myViewportChooser->show();
445 GUIOSGView::setViewportFromTo(
const Position& lookFrom,
const Position& lookAt) {
446 osg::Vec3d lookFromOSG, lookAtOSG, up;
447 myViewer->getCameraManipulator()->getHomePosition(lookFromOSG, lookAtOSG, up);
448 lookFromOSG[0] = lookFrom.
x();
449 lookFromOSG[1] = lookFrom.
y();
450 lookFromOSG[2] = lookFrom.
z();
451 lookAtOSG[0] = lookAt.
x();
452 lookAtOSG[1] = lookAt.
y();
453 lookAtOSG[2] = lookAt.
z();
454 myViewer->getCameraManipulator()->setHomePosition(lookFromOSG, lookAtOSG, up);
462 osg::Vec3d lookFrom, lookAt, up;
463 myCameraManipulator->getHomePosition(lookFrom, lookAt, up);
465 Position(lookAt[0], lookAt[1], lookAt[2]));
471 GUIOSGView::startTrack(
int id) {
472 if (myTracked == 0 || (
int)myTracked->getGlID() != id) {
477 if ((
int)veh->
getGlID() == id) {
478 if (!veh->
isOnRoad() || myVehicles.find(veh) == myVehicles.end()) {
485 if (myTracked != 0) {
486 osg::Vec3d lookFrom, lookAt, up;
488 lookAt[1] = myTracked->getPosition().y();
489 lookAt[2] = myTracked->getPosition().z();
490 lookFrom[0] = lookAt[0] + 50.;
491 lookFrom[1] = lookAt[1] + 50.;
492 lookFrom[2] = lookAt[2] + 10.;
494 m.makeLookAt(lookFrom, lookAt, osg::Z_AXIS);
495 myCameraManipulator->setByInverseMatrix(m);
502 GUIOSGView::stopTrack() {
508 GUIOSGView::getTrackedID()
const {
514 GUIOSGView::onGamingClick(
Position pos) {
516 const std::vector<MSTrafficLightLogic*>& logics = tlsControl.
getAllLogics();
518 double minDist = std::numeric_limits<double>::infinity();
519 for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
525 if (lanes.size() > 0) {
526 const Position& endPos = lanes[0]->getShape().back();
536 const std::vector<MSTrafficLightLogic*> logics = vars.
getAllLogics();
537 if (logics.size() > 1) {
539 for (
int i = 0; i < (int)logics.size() - 1; i++) {
540 if (minTll->
getProgramID() == logics[i]->getProgramID()) {
545 if (l == logics[0]) {
556 GUIOSGView::getCurrentTimeStep()
const {
561 long GUIOSGView::onConfigure(FXObject* sender, FXSelector sel,
void* ptr) {
563 myAdapter->getEventQueue()->windowResize(0, 0, getWidth(), getHeight());
564 myAdapter->resized(0, 0, getWidth(), getHeight());
566 return FXGLCanvas::onConfigure(sender, sel, ptr);
569 long GUIOSGView::onKeyPress(FXObject* sender, FXSelector sel,
void* ptr) {
570 int key = ((FXEvent*)ptr)->code;
571 myAdapter->getEventQueue()->keyPress(key);
573 return FXGLCanvas::onKeyPress(sender, sel, ptr);
576 long GUIOSGView::onKeyRelease(FXObject* sender, FXSelector sel,
void* ptr) {
577 int key = ((FXEvent*)ptr)->code;
578 myAdapter->getEventQueue()->keyRelease(key);
580 return FXGLCanvas::onKeyRelease(sender, sel, ptr);
583 long GUIOSGView::onLeftBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
584 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
586 FXEvent*
event = (FXEvent*)ptr;
587 myAdapter->getEventQueue()->mouseButtonPress((
float)event->click_x, (
float)event->click_y, 1);
588 if (myApp->isGaming()) {
589 onGamingClick(getPositionInformation());
592 return FXGLCanvas::onLeftBtnPress(sender, sel, ptr);
595 long GUIOSGView::onLeftBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
596 FXEvent*
event = (FXEvent*)ptr;
597 myAdapter->getEventQueue()->mouseButtonRelease((
float)event->click_x, (
float)event->click_y, 1);
599 return FXGLCanvas::onLeftBtnRelease(sender, sel, ptr);
602 long GUIOSGView::onMiddleBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
603 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
605 FXEvent*
event = (FXEvent*)ptr;
606 myAdapter->getEventQueue()->mouseButtonPress((
float)event->click_x, (
float)event->click_y, 2);
608 return FXGLCanvas::onMiddleBtnPress(sender, sel, ptr);
611 long GUIOSGView::onMiddleBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
612 FXEvent*
event = (FXEvent*)ptr;
613 myAdapter->getEventQueue()->mouseButtonRelease((
float)event->click_x, (
float)event->click_y, 2);
615 return FXGLCanvas::onMiddleBtnRelease(sender, sel, ptr);
618 long GUIOSGView::onRightBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
619 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
621 FXEvent*
event = (FXEvent*)ptr;
622 myAdapter->getEventQueue()->mouseButtonPress((
float)event->click_x, (
float)event->click_y, 3);
624 return FXGLCanvas::onRightBtnPress(sender, sel, ptr);
627 long GUIOSGView::onRightBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
628 FXEvent*
event = (FXEvent*)ptr;
629 myAdapter->getEventQueue()->mouseButtonRelease((
float)event->click_x, (
float)event->click_y, 3);
631 return FXGLCanvas::onRightBtnRelease(sender, sel, ptr);
635 GUIOSGView::onMouseMove(FXObject* sender, FXSelector sel,
void* ptr) {
636 FXEvent*
event = (FXEvent*)ptr;
637 myAdapter->getEventQueue()->mouseMotion((
float)event->win_x, (
float)event->win_y);
639 return FXGLCanvas::onMotion(sender, sel, ptr);
643 GUIOSGView::OnIdle(FXObject* , FXSelector ,
void*) {
653 : myParent(parent), myOldCursor(cursor) {
654 _traits =
new GraphicsContext::Traits();
657 _traits->width = parent->getWidth();
658 _traits->height = parent->getHeight();
659 _traits->windowDecoration =
false;
660 _traits->doubleBuffer =
true;
661 _traits->sharedContext = 0;
663 setState(
new osg::State());
664 getState()->setGraphicsContext(
this);
665 if (_traits.valid() && _traits->sharedContext != 0) {
666 getState()->setContextID(_traits->sharedContext->getState()->getContextID());
667 incrementContextIDUsageCount(getState()->getContextID());
669 getState()->setContextID(createNewContextID());
675 GUIOSGView::FXOSGAdapter::~FXOSGAdapter() {
680 void GUIOSGView::FXOSGAdapter::grabFocus() {
682 myParent->setFocus();
685 void GUIOSGView::FXOSGAdapter::useCursor(
bool cursorOn) {
687 myParent->setDefaultCursor(myOldCursor);
689 myParent->setDefaultCursor(NULL);
693 bool GUIOSGView::FXOSGAdapter::makeCurrentImplementation() {
694 myParent->makeCurrent();
698 bool GUIOSGView::FXOSGAdapter::releaseContext() {
699 myParent->makeNonCurrent();
703 void GUIOSGView::FXOSGAdapter::swapBuffersImplementation() {
704 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.
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.
Locate polygons - button.
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::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.
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.
Locate addtional structure - button.
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.
Locate junction - button.
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::ostream & operator<<(std::ostream &os, const MTRand &mtrand)
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.