Eclipse SUMO - Simulation of Urban MObility
GUIOSGView.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // An OSG-based 3D view on the simulation
19 /****************************************************************************/
20 #pragma once
21 #include <config.h>
22 
23 #ifdef HAVE_OSG
24 
25 #include <string>
26 #ifdef _MSC_VER
27 #pragma warning(push)
28 #pragma warning(disable: 4275) // do not warn about the DLL interface for OSG
29 #endif
30 #include <osgGA/TerrainManipulator>
31 #include <osgViewer/Viewer>
32 #include <osg/PositionAttitudeTransform>
33 #include <osg/ShapeDrawable>
34 #include <osg/ref_ptr>
35 #ifdef _MSC_VER
36 #pragma warning(pop)
37 #endif
39 #include <utils/geom/Boundary.h>
40 #include <utils/geom/Position.h>
41 #include <utils/common/RGBColor.h>
43 #include <gui/GUISUMOViewParent.h>
45 
46 
47 // ===========================================================================
48 // class declarations
49 // ===========================================================================
50 class GUINet;
51 class GUISUMOViewParent;
52 class GUIVehicle;
53 class GUILaneWrapper;
54 class MSRoute;
55 namespace osgGA {
56 class CameraManipulator;
57 class NodeTrackerManipulator;
58 }
59 
60 
61 // ===========================================================================
62 // class definitions
63 // ===========================================================================
68 class GUIOSGView : public GUISUMOAbstractView {
69  FXDECLARE(GUIOSGView)
70 public:
75  class Command_TLSChange : public MSTLLogicControl::OnSwitchAction {
76  public:
85  Command_TLSChange(const MSLink* const link, osg::Switch* switchNode);
86 
87 
89  virtual ~Command_TLSChange();
90 
91 
98  void execute();
99 
100 
101  private:
103  const MSLink* const myLink;
104 
106  osg::ref_ptr<osg::Switch> mySwitch;
107 
109  LinkState myLastState;
110 
111 
112  private:
114  Command_TLSChange(const Command_TLSChange&);
115 
117  Command_TLSChange& operator=(const Command_TLSChange&);
118 
119  };
120 
122  struct OSGMovable {
123  osg::ref_ptr<osg::PositionAttitudeTransform> pos;
124  osg::ref_ptr<osg::ShapeDrawable> geom;
125  osg::ref_ptr<osg::Switch> lights;
126  bool active;
127  };
128 
130  GUIOSGView(FXComposite* p, GUIMainWindow& app,
131  GUISUMOViewParent* parent, GUINet& net, FXGLVisual* glVis,
132  FXGLCanvas* share);
133 
135  virtual ~GUIOSGView();
136 
138  virtual void buildViewToolBars(GUIGlChildWindow*);
139 
141  void recenterView();
142 
149  void centerTo(GUIGlID id, bool applyZoom, double zoomDist = 20);
150 
152  void showViewportEditor();
153 
155  void setViewportFromToRot(const Position& lookFrom, const Position& lookAt, double rotation);
156 
159 
163  void startTrack(int id);
164 
165 
168  void stopTrack();
169 
170 
174  GUIGlID getTrackedID() const;
175 
176  bool setColorScheme(const std::string& name);
177 
179  void onGamingClick(Position pos);
180 
183 
184  void removeVeh(MSVehicle* veh);
185  void removeTransportable(MSTransportable* t);
186 
187  // callback
188  long onConfigure(FXObject*, FXSelector, void*);
189  long onKeyPress(FXObject*, FXSelector, void*);
190  long onKeyRelease(FXObject*, FXSelector, void*);
191  long onLeftBtnPress(FXObject*, FXSelector, void*);
192  long onLeftBtnRelease(FXObject*, FXSelector, void*);
193  long onMiddleBtnPress(FXObject*, FXSelector, void*);
194  long onMiddleBtnRelease(FXObject*, FXSelector, void*);
195  long onRightBtnPress(FXObject*, FXSelector, void*);
196  long onRightBtnRelease(FXObject*, FXSelector, void*);
197  //long onMotion(FXObject*, FXSelector, void*);
198  long onMouseMove(FXObject*, FXSelector, void*);
199  long onPaint(FXObject*, FXSelector, void*);
200  long OnIdle(FXObject* sender, FXSelector sel, void* ptr);
201 
202 private:
203  class SUMOTerrainManipulator : public osgGA::TerrainManipulator {
204  public:
205  SUMOTerrainManipulator() {
206  setAllowThrow(false);
207  setRotationMode(ELEVATION_AZIM_ROLL); // default is ELEVATION_AZIM and this prevents rotating the view around the z-axis
208  }
209  bool performMovementLeftMouseButton(const double eventTimeDelta, const double dx, const double dy) {
210  return osgGA::TerrainManipulator::performMovementMiddleMouseButton(eventTimeDelta, dx, dy);
211  }
212  bool performMovementMiddleMouseButton(const double eventTimeDelta, const double dx, const double dy) {
213  return osgGA::TerrainManipulator::performMovementLeftMouseButton(eventTimeDelta, dx, dy);
214  }
215  bool performMovementRightMouseButton(const double eventTimeDelta, const double dx, const double dy) {
216  return osgGA::TerrainManipulator::performMovementRightMouseButton(eventTimeDelta, dx, -dy);
217  }
218  };
219 
220  class FXOSGAdapter : public osgViewer::GraphicsWindow {
221  public:
222  FXOSGAdapter(GUISUMOAbstractView* parent, FXCursor* cursor);
223  void grabFocus();
224  void grabFocusIfPointerInWindow() {}
225  void useCursor(bool cursorOn);
226 
227  bool makeCurrentImplementation();
228  bool releaseContext();
229  void swapBuffersImplementation();
230 
231  // not implemented yet...just use dummy implementation to get working.
232  bool valid() const {
233  return true;
234  }
235  bool realizeImplementation() {
236  return true;
237  }
238  bool isRealizedImplementation() const {
239  return true;
240  }
241  void closeImplementation() {}
242  bool releaseContextImplementation() {
243  return true;
244  }
245 
246  protected:
247  ~FXOSGAdapter();
248  private:
249  GUISUMOAbstractView* const myParent;
250  FXCursor* const myOldCursor;
251  };
252 
253 protected:
254 
255  osg::ref_ptr<FXOSGAdapter> myAdapter;
256  osg::ref_ptr<osgViewer::Viewer> myViewer;
257  osg::ref_ptr<osg::Group> myRoot;
258 
259 private:
260  GUIVehicle* myTracked;
261  osg::ref_ptr<osgGA::CameraManipulator> myCameraManipulator;
262 
263  SUMOTime myLastUpdate;
264 
265  std::map<MSVehicle*, OSGMovable > myVehicles;
266  std::map<MSTransportable*, OSGMovable > myPersons;
267 
268  osg::ref_ptr<osg::Node> myGreenLight;
269  osg::ref_ptr<osg::Node> myYellowLight;
270  osg::ref_ptr<osg::Node> myRedLight;
271  osg::ref_ptr<osg::Node> myRedYellowLight;
272 
273 protected:
274  GUIOSGView() { }
275 
276 };
277 
278 #endif
unsigned int GUIGlID
Definition: GUIGlObject.h:40
long long int SUMOTime
Definition: SUMOTime.h:31
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:81
virtual long onLeftBtnRelease(FXObject *, FXSelector, void *)
virtual void recenterView()
recenters the view
virtual SUMOTime getCurrentTimeStep() const
get the current simulation time
virtual long onMiddleBtnRelease(FXObject *, FXSelector, void *)
virtual long onMouseMove(FXObject *, FXSelector, void *)
virtual void buildViewToolBars(GUIGlChildWindow *)
builds the view toolbars
virtual void showViewportEditor()
show viewport editor
virtual void centerTo(GUIGlID id, bool applyZoom, double zoomDist=20)
centers to the chosen artifact
virtual long onLeftBtnPress(FXObject *, FXSelector, void *)
virtual void setViewportFromToRot(const Position &lookFrom, const Position &lookAt, double rotation)
applies the given viewport settings
virtual void onGamingClick(Position)
on gaming click
virtual long onKeyPress(FXObject *o, FXSelector sel, void *data)
keyboard functions
virtual long onMiddleBtnPress(FXObject *, FXSelector, void *)
virtual void stopTrack()
stop track
virtual long onKeyRelease(FXObject *o, FXSelector sel, void *data)
virtual void startTrack(int)
star track
virtual long onRightBtnRelease(FXObject *, FXSelector, void *)
virtual void copyViewportTo(GUISUMOAbstractView *view)
copy the viewport to the given view
virtual bool setColorScheme(const std::string &)
set color scheme
virtual long onPaint(FXObject *, FXSelector, void *)
virtual long onRightBtnPress(FXObject *, FXSelector, void *)
virtual long onConfigure(FXObject *, FXSelector, void *)
mouse functions
virtual GUIGlID getTrackedID() const
get tracked id
A single child window which contains a view of the simulation area.
A MSVehicle extended by some values for usage within the gui.
Definition: GUIVehicle.h:51
Base class for things to execute if a tls switches to a new phase.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36