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.dev/sumo
3 // Copyright (C) 2001-2024 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 /****************************************************************************/
19 // An OSG-based 3D view on the simulation
20 /****************************************************************************/
21 #pragma once
22 #include <config.h>
23 
24 #ifdef HAVE_OSG
25 
26 #include "GUIOSGHeader.h"
27 #include "GUIOSGManipulator.h"
28 
29 #include <string>
31 #include <utils/geom/Boundary.h>
32 #include <utils/geom/Position.h>
33 #include <utils/common/RGBColor.h>
35 #include <gui/GUISUMOViewParent.h>
37 
38 
39 // ===========================================================================
40 // class declarations
41 // ===========================================================================
42 
43 class GUINet;
44 class GUISUMOViewParent;
45 class GUIVehicle;
46 class GUILaneWrapper;
47 class MSRoute;
48 class MSTransportable;
49 class MSVehicle;
50 
51 namespace osgGA {
52 class CameraManipulator;
53 }
54 
55 // ===========================================================================
56 // class definitions
57 // ===========================================================================
62 class GUIOSGView : public GUISUMOAbstractView {
63 #ifdef __clang__
64 #pragma clang diagnostic push
65 #pragma clang diagnostic ignored "-Winconsistent-missing-override"
66 #endif
67  FXDECLARE(GUIOSGView)
68 #ifdef __clang__
69 #pragma clang diagnostic pop
70 #endif
71 
72 public:
73  friend class GUIOSGPerspectiveChanger;
74 
76  enum NodeSetGroup {
78  NODESET_TLSDOMES,
80  NODESET_TLSLINKMARKERS,
82  NODESET_TLSMODELS,
83  };
84 
89  class Command_TLSChange : public MSTLLogicControl::OnSwitchAction {
90  public:
99  Command_TLSChange(const MSLink* const link, osg::Switch* switchNode);
100 
102  virtual ~Command_TLSChange();
103 
110  void execute();
111 
112  private:
114  const MSLink* const myLink;
115 
117  osg::ref_ptr<osg::Switch> mySwitch;
118 
120  LinkState myLastState;
121 
122  private:
124  Command_TLSChange(const Command_TLSChange&) = delete;
125 
127  Command_TLSChange& operator=(const Command_TLSChange&) = delete;
128  };
129 
131  struct OSGMovable {
132  osg::ref_ptr<osg::PositionAttitudeTransform> pos;
133  osg::ref_ptr<osg::ShapeDrawable> geom;
134  osg::ref_ptr<osg::Material> mat;
135  osg::ref_ptr<osg::Switch> lights;
136  bool active;
137  };
138 
140  GUIOSGView(FXComposite* p, GUIMainWindow& app,
141  GUISUMOViewParent* parent, GUINet& net, FXGLVisual* glVis,
142  FXGLCanvas* share);
143 
145  virtual ~GUIOSGView();
146 
148  Position getPositionInformation() const override;
149 
151  bool is3DView() const override;
152 
154  virtual void buildViewToolBars(GUIGlChildWindow*) override;
155 
157  void recenterView() override;
158 
165  //void centerTo(GUIGlID id, bool applyZoom, double zoomDist = 20);
166 
168  void updateViewportValues() override;
169 
171  void showViewportEditor() override;
172 
174  void setViewportFromToRot(const Position& lookFrom, const Position& lookAt, double rotation) override;
175 
177  void copyViewportTo(GUISUMOAbstractView* view) override;
178 
182  void startTrack(int id) override;
183 
186  void stopTrack() override;
187 
191  GUIGlID getTrackedID() const override;
192 
193  bool setColorScheme(const std::string& name) override;
194 
196  void onGamingClick(Position pos) override;
197 
199  SUMOTime getCurrentTimeStep() const override;
200 
201  void removeVeh(MSVehicle* veh);
202  void removeTransportable(MSTransportable* t);
203 
205  void position(int x, int y, int w, int h) override;
206  void resize(int w, int h) override;
207 
208  // callback
209  long onConfigure(FXObject*, FXSelector, void*) override;
210  long onKeyPress(FXObject*, FXSelector, void*) override;
211  long onKeyRelease(FXObject*, FXSelector, void*) override;
212  long onLeftBtnPress(FXObject*, FXSelector, void*) override;
213  long onLeftBtnRelease(FXObject*, FXSelector, void*) override;
214  long onMiddleBtnPress(FXObject*, FXSelector, void*) override;
215  long onMiddleBtnRelease(FXObject*, FXSelector, void*) override;
216  long onRightBtnPress(FXObject*, FXSelector, void*) override;
217  long onRightBtnRelease(FXObject*, FXSelector, void*) override;
218  //long onMotion(FXObject*, FXSelector, void*);
219  long onMouseMove(FXObject*, FXSelector, void*) override;
220  long onPaint(FXObject*, FXSelector, void*) override;
221  long onIdle(FXObject* sender, FXSelector sel, void* ptr);
222 
224  long onCmdCloseLane(FXObject*, FXSelector, void*) override;
225  long onCmdCloseEdge(FXObject*, FXSelector, void*) override;
226  long onCmdAddRerouter(FXObject*, FXSelector, void*) override;
227 
229  long onCmdShowReachability(FXObject*, FXSelector, void*) override;
230 
232  long onVisualizationChange(FXObject*, FXSelector, void*) override;
233 
234  // @brief get the new camera position given a zoom value
235  void zoom2Pos(Position& camera, Position& lookAt, double zoom) override;
236 
237  // @brief convert RGBColor 0..255 RGBA values to osg::Vec4 0..1 vector
238  static osg::Vec4d toOSGColorVector(RGBColor c, bool useAlpha = false);
239 
240  // @brief Overwrite the HUD text
241  void updateHUDText(const std::string text);
242 
243 protected:
245  void setWindowCursorPosition(float x, float y);
246 
247  void updatePositionInformation() const;
248 
250  bool getPositionAtCursor(float xNorm, float yNorm, Position& pos) const;
251 
253  std::vector<GUIGlObject*> getGUIGlObjectsUnderCursor();
254 
255  /* @brief Find GUILane which intersects with a ray from the camera to the stored cursor position
256  * @return The first found GUILane found or nullptr
257  */
258  GUILane* getLaneUnderCursor() override;
259 
261  void adoptViewSettings();
262 
263 private:
264  double calculateRotation(const osg::Vec3d& lookFrom, const osg::Vec3d& lookAt, const osg::Vec3d& up);
265 
267  void updateHUDPosition(int width, int height);
268 
269  class FXOSGAdapter : public osgViewer::GraphicsWindow {
270  public:
271  FXOSGAdapter(GUISUMOAbstractView* parent, FXCursor* cursor);
272  void grabFocus();
273  void grabFocusIfPointerInWindow() {}
274  void useCursor(bool cursorOn);
275 
276  bool makeCurrentImplementation();
277  bool releaseContext();
278  void swapBuffersImplementation();
279 
280  // not implemented yet...just use dummy implementation to get working.
281  bool valid() const {
282  return true;
283  }
284  bool realizeImplementation() {
285  return true;
286  }
287  bool isRealizedImplementation() const {
288  return true;
289  }
290  void closeImplementation() {}
291  bool releaseContextImplementation() {
292  return true;
293  }
294  void requestWarpPointer(float x, float y) {
295  int xRound = std::lround(x);
296  int yRound = std::lround(y);
297  int xPrev, yPrev;
298  unsigned int buttons;
299  myParent->getCursorPosition(xPrev, yPrev, buttons);
300  if (xRound - xPrev != 0 || yRound - yPrev != 0) {
301  myParent->setCursorPosition(xRound, yRound);
302  getEventQueue()->mouseWarped(x, y);
303  }
304  }
305 
306  protected:
307  ~FXOSGAdapter();
308  private:
309  GUISUMOAbstractView* const myParent;
310  FXCursor* const myOldCursor;
311  };
312 
313  class PlaneMoverCallback : public osg::Callback {
314  public:
315  PlaneMoverCallback(osg::Camera* camera) : myCamera(camera) {};
316  virtual bool run(osg::Object* object, osg::Object* /* data */) override {
317  osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(object);
318  osg::Vec3d lookFrom, lookAt, up;
319  myCamera->getViewMatrixAsLookAt(lookFrom, lookAt, up);
320  osg::Vec3d direction = lookAt - lookFrom;
321  direction.normalize();
322  osg::Vec3d lookAtGround = lookFrom - direction * (lookFrom.z() / direction.z());
323  osg::Matrixd translateMatrix;
324  translateMatrix.makeTranslate(lookAtGround.x(), lookAtGround.y(), 0.);
325  double angle = atan2(direction.y(), direction.x());
326  osg::Matrixd rotMatrix = osg::Matrixd::rotate(angle, osg::Z_AXIS);
327  mt->setMatrix(rotMatrix * translateMatrix);
328  return true;
329  }
330  protected:
331  ~PlaneMoverCallback() {};
332  private:
333  osg::Camera* myCamera;
334  };
335 
336  class PickHandler : public osgGA::GUIEventHandler {
337  public:
338  PickHandler(GUIOSGView* parent) : myParent(parent), myDrag(false) {};
339  bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
340  protected:
341  ~PickHandler() {};
342  private:
343  GUIOSGView* const myParent;
344  bool myDrag;
345  };
346 
347  class ExcludeFromNearFarComputationCallback : public osg::NodeCallback {
348  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
349  osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
350  // Default value
351  osg::CullSettings::ComputeNearFarMode oldMode = osg::CullSettings::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES;
352  if (cv) {
353  oldMode = cv->getComputeNearFarMode();
354  cv->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
355  }
356  traverse(node, nv);
357  if (cv) {
358  cv->setComputeNearFarMode(oldMode);
359  }
360  }
361  };
362 
363 
364 
365 protected:
366  GUIOSGView() {}
367 
368  osg::ref_ptr<FXOSGAdapter> myAdapter;
369  osg::ref_ptr<osgViewer::Viewer> myViewer;
370  osg::ref_ptr<osg::Group> myRoot;
371  osg::ref_ptr<osg::MatrixTransform> myPlane;
372  osg::ref_ptr<osg::Camera> myHUD;
373  osg::ref_ptr<osg::Geode> myTextNode;
374  osg::ref_ptr<osgText::Text> myText;
375 
376 private:
377  GUIVehicle* myTracked;
378  osg::ref_ptr<GUIOSGManipulator> myCameraManipulator;
379  SUMOTime myLastUpdate;
380 
381  float myOSGNormalizedCursorX, myOSGNormalizedCursorY;
382 
383  std::map<MSVehicle*, OSGMovable > myVehicles;
384  std::map<MSTransportable*, OSGMovable > myPersons;
385 
386  osg::ref_ptr<osg::Node> myGreenLight;
387  osg::ref_ptr<osg::Node> myYellowLight;
388  osg::ref_ptr<osg::Node> myRedLight;
389  osg::ref_ptr<osg::Node> myRedYellowLight;
390  osg::ref_ptr<osg::Node> myPoleBase;
391  osg::ref_ptr<osg::Node> myPlaneTransform;
392 };
393 
394 #endif
long long int SUMOTime
Definition: GUI.h:35
unsigned int GUIGlID
Definition: GUIGlObject.h:43
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
Representation of a lane in the micro simulation (gui-version)
Definition: GUILane.h:60
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:82
virtual long onLeftBtnRelease(FXObject *, FXSelector, void *)
virtual long onVisualizationChange(FXObject *, FXSelector, void *)
hook to react on change in visualization settings
std::vector< GUIGlObject * > getGUIGlObjectsUnderCursor()
returns the GUIGlObject under the cursor using GL_SELECT (including overlapped objects)
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 long onCmdCloseEdge(FXObject *, FXSelector, void *)
virtual void buildViewToolBars(GUIGlChildWindow *)
builds the view toolbars
virtual void showViewportEditor()
show viewport editor
virtual void zoom2Pos(Position &camera, Position &lookAt, double zoom)
zoom interface for 3D view
virtual long onCmdCloseLane(FXObject *, FXSelector, void *)
interaction with the simulation
virtual long onLeftBtnPress(FXObject *, FXSelector, void *)
virtual void setViewportFromToRot(const Position &lookFrom, const Position &lookAt, double rotation)
applies the given viewport settings
virtual GUILane * getLaneUnderCursor()
returns the GUILane at cursor position (implementation depends on view)
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 Position getPositionInformation() const
Returns the cursor's x/y position within the network.
virtual long onKeyRelease(FXObject *o, FXSelector sel, void *data)
void setWindowCursorPosition(FXint x, FXint y)
Returns the gl-id of the object under the given coordinates.
virtual long onCmdShowReachability(FXObject *, FXSelector, void *)
highlight edges according to reachability
virtual void updateViewportValues()
update the viewport chooser with the current view values
virtual void startTrack(int)
star track
virtual bool is3DView() const
return whether this is a 3D view
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 onCmdAddRerouter(FXObject *, FXSelector, void *)
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:37
@ object
object (unordered set of name/value pairs)