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() {
270 Position center = myGrid->getCenter();
271 osg::Vec3d lookFromOSG, lookAtOSG, up;
272 myViewer->getCameraManipulator()->getHomePosition(lookFromOSG, lookAtOSG, up);
273 lookFromOSG[0] = center.
x();
274 lookFromOSG[1] = center.
y();
275 lookFromOSG[2] = myChanger->zoom2ZPos(100);
276 lookAtOSG[0] = center.
x();
277 lookAtOSG[1] = center.
y();
279 myViewer->getCameraManipulator()->setHomePosition(lookFromOSG, lookAtOSG, up);
291 GUIOSGView::setColorScheme(
const std::string& name) {
295 if (myVisualizationChanger != 0) {
296 if (myVisualizationChanger->getCurrentScheme() != name) {
297 myVisualizationChanger->setCurrentScheme(name);
301 myVisualizationSettings->
gaming = myApp->isGaming();
308 GUIOSGView::onPaint(FXObject*, FXSelector,
void*) {
313 for (std::vector<GUISUMOAbstractView::Decal>::iterator l = myDecals.begin(); l != myDecals.end(); ++l) {
317 GUIOSGBuilder::buildLight(d, *myRoot);
319 const int linkStringIdx = d.
filename.find(
':', 3);
324 if (linkIdx < 0 || linkIdx >= static_cast<int>(vars.
getActive()->
getLinks().size())) {
328 osg::Switch* switchNode =
new osg::Switch();
329 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myGreenLight, osg::Vec4(0., 1., 0., .3)),
false);
330 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myYellowLight, osg::Vec4(1., 1., 0., .3)),
false);
331 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myRedLight, osg::Vec4(1., 0., 0., .3)),
false);
332 switchNode->addChild(GUIOSGBuilder::getTrafficLight(d, d.
layer < 0 ? 0 : myRedYellowLight, osg::Vec4(1., .5, 0., .3)),
false);
333 myRoot->addChild(switchNode);
341 GUIOSGBuilder::buildDecal(d, *myRoot);
346 myDecalsLock.unlock();
353 if (myVehicles.find(veh) == myVehicles.end()) {
354 myVehicles[veh] = GUIOSGBuilder::buildMovable(veh->
getVehicleType());
355 myRoot->addChild(myVehicles[veh].pos);
357 osg::PositionAttitudeTransform* n = myVehicles[veh].pos;
361 n->setAttitude(osg::Quat(dir, osg::Vec3(0, 0, 1)) *
362 osg::Quat(osg::DegreesToRadians(slope), osg::Vec3(0, 1, 0)));
374 const RGBColor& col = myVisualizationSettings->vehicleColorer.getScheme().getColor(veh->
getColorValue(myVisualizationSettings->vehicleColorer.getActive()));
375 myVehicles[veh].geom->setColor(osg::Vec4(col.
red() / 255., col.
green() / 255., col.
blue() / 255., col.
alpha() / 255.));
382 if (now != myLastUpdate || (myVisualizationChanger != 0 && myVisualizationChanger->shown())) {
385 if (now != myLastUpdate && myTracked != 0) {
386 osg::Vec3d lookFrom, lookAt, up;
387 lookAt[0] = myTracked->getPosition().x();
388 lookAt[1] = myTracked->getPosition().y();
389 lookAt[2] = myTracked->getPosition().z();
390 const SUMOReal angle = myTracked->getAngle();
391 lookFrom[0] = lookAt[0] + 50. * cos(angle);
392 lookFrom[1] = lookAt[1] + 50. * sin(angle);
393 lookFrom[2] = lookAt[2] + 10.;
395 m.makeLookAt(lookFrom, lookAt, osg::Z_AXIS);
396 myCameraManipulator->setByInverseMatrix(m);
402 if (myPersons.find(person) == myPersons.end()) {
403 myPersons[person] = GUIOSGBuilder::buildMovable(person->
getVehicleType());
404 myRoot->addChild(myPersons[person].pos);
406 osg::PositionAttitudeTransform* n = myPersons[person].pos;
408 n->setPosition(osg::Vec3(pos.
x(), pos.
y(), pos.
z()));
410 n->setAttitude(osg::Quat(dir, osg::Vec3(0, 0, 1)));
412 if (myAdapter->makeCurrent()) {
423 if (myTracked == veh) {
426 std::map<MSVehicle*, OSGMovable>::iterator i = myVehicles.find(veh);
427 if (i != myVehicles.end()) {
428 myRoot->removeChild(i->second.pos);
435 GUIOSGView::showViewportEditor() {
437 osg::Vec3d lookFromOSG, lookAtOSG, up;
438 myViewer->getCameraManipulator()->getInverseMatrix().getLookAt(lookFromOSG, lookAtOSG, up);
439 Position from(lookFromOSG[0], lookFromOSG[1], lookFromOSG[2]), at(lookAtOSG[0], lookAtOSG[1], lookAtOSG[2]);
440 myViewportChooser->setOldValues(from, at);
441 myViewportChooser->show();
446 GUIOSGView::setViewportFromTo(
const Position& lookFrom,
const Position& lookAt) {
447 osg::Vec3d lookFromOSG, lookAtOSG, up;
448 myViewer->getCameraManipulator()->getHomePosition(lookFromOSG, lookAtOSG, up);
449 lookFromOSG[0] = lookFrom.
x();
450 lookFromOSG[1] = lookFrom.
y();
451 lookFromOSG[2] = lookFrom.
z();
452 lookAtOSG[0] = lookAt.
x();
453 lookAtOSG[1] = lookAt.
y();
454 lookAtOSG[2] = lookAt.
z();
455 myViewer->getCameraManipulator()->setHomePosition(lookFromOSG, lookAtOSG, up);
463 osg::Vec3d lookFrom, lookAt, up;
464 myCameraManipulator->getHomePosition(lookFrom, lookAt, up);
466 Position(lookAt[0], lookAt[1], lookAt[2]));
472 GUIOSGView::startTrack(
int id) {
473 if (myTracked == 0 || (
int)myTracked->getGlID() != id) {
478 if ((
int)veh->
getGlID() == id) {
479 if (!veh->
isOnRoad() || myVehicles.find(veh) == myVehicles.end()) {
486 if (myTracked != 0) {
487 osg::Vec3d lookFrom, lookAt, up;
489 lookAt[1] = myTracked->getPosition().y();
490 lookAt[2] = myTracked->getPosition().z();
491 lookFrom[0] = lookAt[0] + 50.;
492 lookFrom[1] = lookAt[1] + 50.;
493 lookFrom[2] = lookAt[2] + 10.;
495 m.makeLookAt(lookFrom, lookAt, osg::Z_AXIS);
496 myCameraManipulator->setByInverseMatrix(m);
503 GUIOSGView::stopTrack() {
509 GUIOSGView::getTrackedID()
const {
515 GUIOSGView::onGamingClick(
Position pos) {
517 const std::vector<MSTrafficLightLogic*>& logics = tlsControl.
getAllLogics();
519 SUMOReal minDist = std::numeric_limits<SUMOReal>::infinity();
520 for (std::vector<MSTrafficLightLogic*>::const_iterator i = logics.begin(); i != logics.end(); ++i) {
526 if (lanes.size() > 0) {
527 const Position& endPos = lanes[0]->getShape().back();
537 const std::vector<MSTrafficLightLogic*> logics = vars.
getAllLogics();
538 if (logics.size() > 1) {
540 for (
int i = 0; i < (int)logics.size() - 1; i++) {
541 if (minTll->
getProgramID() == logics[i]->getProgramID()) {
546 if (l == logics[0]) {
557 GUIOSGView::getCurrentTimeStep()
const {
562 long GUIOSGView::onConfigure(FXObject* sender, FXSelector sel,
void* ptr) {
564 myAdapter->getEventQueue()->windowResize(0, 0, getWidth(), getHeight());
565 myAdapter->resized(0, 0, getWidth(), getHeight());
567 return FXGLCanvas::onConfigure(sender, sel, ptr);
570 long GUIOSGView::onKeyPress(FXObject* sender, FXSelector sel,
void* ptr) {
571 int key = ((FXEvent*)ptr)->code;
572 myAdapter->getEventQueue()->keyPress(key);
574 return FXGLCanvas::onKeyPress(sender, sel, ptr);
577 long GUIOSGView::onKeyRelease(FXObject* sender, FXSelector sel,
void* ptr) {
578 int key = ((FXEvent*)ptr)->code;
579 myAdapter->getEventQueue()->keyRelease(key);
581 return FXGLCanvas::onKeyRelease(sender, sel, ptr);
584 long GUIOSGView::onLeftBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
585 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
587 FXEvent*
event = (FXEvent*)ptr;
588 myAdapter->getEventQueue()->mouseButtonPress(event->click_x, event->click_y, 1);
589 if (myApp->isGaming()) {
590 onGamingClick(getPositionInformation());
593 return FXGLCanvas::onLeftBtnPress(sender, sel, ptr);
596 long GUIOSGView::onLeftBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
597 FXEvent*
event = (FXEvent*)ptr;
598 myAdapter->getEventQueue()->mouseButtonRelease(event->click_x, event->click_y, 1);
600 return FXGLCanvas::onLeftBtnRelease(sender, sel, ptr);
603 long GUIOSGView::onMiddleBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
604 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
606 FXEvent*
event = (FXEvent*)ptr;
607 myAdapter->getEventQueue()->mouseButtonPress(event->click_x, event->click_y, 2);
609 return FXGLCanvas::onMiddleBtnPress(sender, sel, ptr);
612 long GUIOSGView::onMiddleBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
613 FXEvent*
event = (FXEvent*)ptr;
614 myAdapter->getEventQueue()->mouseButtonRelease(event->click_x, event->click_y, 2);
616 return FXGLCanvas::onMiddleBtnRelease(sender, sel, ptr);
619 long GUIOSGView::onRightBtnPress(FXObject* sender, FXSelector sel,
void* ptr) {
620 handle(
this, FXSEL(SEL_FOCUS_SELF, 0), ptr);
622 FXEvent*
event = (FXEvent*)ptr;
623 myAdapter->getEventQueue()->mouseButtonPress(event->click_x, event->click_y, 3);
625 return FXGLCanvas::onRightBtnPress(sender, sel, ptr);
628 long GUIOSGView::onRightBtnRelease(FXObject* sender, FXSelector sel,
void* ptr) {
629 FXEvent*
event = (FXEvent*)ptr;
630 myAdapter->getEventQueue()->mouseButtonRelease(event->click_x, event->click_y, 3);
632 return FXGLCanvas::onRightBtnRelease(sender, sel, ptr);
636 GUIOSGView::onMouseMove(FXObject* sender, FXSelector sel,
void* ptr) {
637 FXEvent*
event = (FXEvent*)ptr;
638 myAdapter->getEventQueue()->mouseMotion(event->win_x, event->win_y);
640 return FXGLCanvas::onMotion(sender, sel, ptr);
644 GUIOSGView::OnIdle(FXObject* , FXSelector ,
void*) {
654 : myParent(parent), myOldCursor(cursor) {
655 _traits =
new GraphicsContext::Traits();
658 _traits->width = parent->getWidth();
659 _traits->height = parent->getHeight();
660 _traits->windowDecoration =
false;
661 _traits->doubleBuffer =
true;
662 _traits->sharedContext = 0;
664 setState(
new osg::State());
665 getState()->setGraphicsContext(
this);
666 if (_traits.valid() && _traits->sharedContext != 0) {
667 getState()->setContextID(_traits->sharedContext->getState()->getContextID());
668 incrementContextIDUsageCount(getState()->getContextID());
670 getState()->setContextID(createNewContextID());
676 GUIOSGView::FXOSGAdapter::~FXOSGAdapter() {
681 void GUIOSGView::FXOSGAdapter::grabFocus() {
683 myParent->setFocus();
686 void GUIOSGView::FXOSGAdapter::useCursor(
bool cursorOn) {
688 myParent->setDefaultCursor(myOldCursor);
690 myParent->setDefaultCursor(NULL);
694 bool GUIOSGView::FXOSGAdapter::makeCurrentImplementation() {
695 myParent->makeCurrent();
699 bool GUIOSGView::FXOSGAdapter::releaseContext() {
700 myParent->makeNonCurrent();
704 void GUIOSGView::FXOSGAdapter::swapBuffersImplementation() {
705 myParent->swapBuffers();
bool signalSet(int which) const
Returns whether the given signal is on.
A decal (an image) that can be shown.
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.
GUICompleteSchemeStorage gSchemeStorage
Storage for all programs of a single tls.
const LaneVector & getLanesAt(int i) const
Returns the list of lanes that are controlled by the signals at the given position.
virtual void setViewportFromTo(const Position &lookFrom, const Position &lookAt)
applies the given viewport settings
SUMOReal getColorValue(int activeScheme) const
gets the color value according to the current scheme index
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.
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
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.
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.
const LinkVector & getLinksAt(int i) const
Returns the list of links that are controlled by the signals at the given position.
MSTrafficLightLogic * getActive() const
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.
FXComboBox & getColoringSchemesCombo()
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 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.
constVehIt loadedEnd() const
Returns the end of the internal transportables map.
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 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.
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.
static const GUIGlID INVALID_ID
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)
returns a icon previously defined in the enum GUIIcon
The link has red light (must brake) but indicates upcoming green.
FXPopup * getLocatorPopup()
A MSVehicle extended by some values for usage within the gui.