Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2023 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>
37
38
39// ===========================================================================
40// class declarations
41// ===========================================================================
42
43class GUINet;
45class GUIVehicle;
46class GUILaneWrapper;
47class MSRoute;
48class MSTransportable;
49class MSVehicle;
50
51namespace osgGA {
52class CameraManipulator;
53}
54
55// ===========================================================================
56// class definitions
57// ===========================================================================
62class GUIOSGView : public GUISUMOAbstractView {
63 FXDECLARE(GUIOSGView)
64
65public:
66 friend class GUIOSGPerspectiveChanger;
67
69 enum NodeSetGroup {
71 NODESET_TLSDOMES,
73 NODESET_TLSLINKMARKERS,
75 NODESET_TLSMODELS,
76 };
77
82 class Command_TLSChange : public MSTLLogicControl::OnSwitchAction {
83 public:
92 Command_TLSChange(const MSLink* const link, osg::Switch* switchNode);
93
95 virtual ~Command_TLSChange();
96
103 void execute();
104
105 private:
107 const MSLink* const myLink;
108
110 osg::ref_ptr<osg::Switch> mySwitch;
111
113 LinkState myLastState;
114
115 private:
117 Command_TLSChange(const Command_TLSChange&) = delete;
118
120 Command_TLSChange& operator=(const Command_TLSChange&) = delete;
121 };
122
124 struct OSGMovable {
125 osg::ref_ptr<osg::PositionAttitudeTransform> pos;
126 osg::ref_ptr<osg::ShapeDrawable> geom;
127 osg::ref_ptr<osg::Material> mat;
128 osg::ref_ptr<osg::Switch> lights;
129 bool active;
130 };
131
133 GUIOSGView(FXComposite* p, GUIMainWindow& app,
134 GUISUMOViewParent* parent, GUINet& net, FXGLVisual* glVis,
135 FXGLCanvas* share);
136
138 virtual ~GUIOSGView();
139
142
145
147 bool is3DView() const;
148
150 virtual void buildViewToolBars(GUIGlChildWindow*);
151
153 void recenterView();
154
161 //void centerTo(GUIGlID id, bool applyZoom, double zoomDist = 20);
162
165
167 void showViewportEditor();
168
170 void setViewportFromToRot(const Position& lookFrom, const Position& lookAt, double rotation);
171
174
178 void startTrack(int id);
179
182 void stopTrack();
183
187 GUIGlID getTrackedID() const;
188
189 bool setColorScheme(const std::string& name);
190
192 void onGamingClick(Position pos);
193
196
197 void removeVeh(MSVehicle* veh);
198 void removeTransportable(MSTransportable* t);
199
201 void position(int x, int y, int w, int h);
202 void resize(int w, int h);
203
204 // callback
205 long onConfigure(FXObject*, FXSelector, void*);
206 long onKeyPress(FXObject*, FXSelector, void*);
207 long onKeyRelease(FXObject*, FXSelector, void*);
208 long onLeftBtnPress(FXObject*, FXSelector, void*);
209 long onLeftBtnRelease(FXObject*, FXSelector, void*);
210 long onMiddleBtnPress(FXObject*, FXSelector, void*);
211 long onMiddleBtnRelease(FXObject*, FXSelector, void*);
212 long onRightBtnPress(FXObject*, FXSelector, void*);
213 long onRightBtnRelease(FXObject*, FXSelector, void*);
214 //long onMotion(FXObject*, FXSelector, void*);
215 long onMouseMove(FXObject*, FXSelector, void*);
216 long onPaint(FXObject*, FXSelector, void*);
217 long OnIdle(FXObject* sender, FXSelector sel, void* ptr);
218
220 long onCmdCloseLane(FXObject*, FXSelector, void*);
221 long onCmdCloseEdge(FXObject*, FXSelector, void*);
222 long onCmdAddRerouter(FXObject*, FXSelector, void*);
223
225 long onCmdShowReachability(FXObject*, FXSelector, void*);
226
228 long onVisualizationChange(FXObject*, FXSelector, void*);
229
230 // @brief get the new camera position given a zoom value
231 void zoom2Pos(Position& camera, Position& lookAt, double zoom);
232
233 // @brief convert RGBColor 0..255 RGBA values to osg::Vec4 0..1 vector
234 static osg::Vec4d toOSGColorVector(RGBColor c, bool useAlpha = false);
235
236 // @brief Overwrite the HUD text
237 void updateHUDText(const std::string text);
238
239protected:
241 void setWindowCursorPosition(float x, float y);
242
243 void updatePositionInformation() const;
244
246 bool getPositionAtCursor(float xNorm, float yNorm, Position& pos) const;
247
249 std::vector<GUIGlObject*> getGUIGlObjectsUnderCursor();
250
251 /* @brief Find GUILane which intersects with a ray from the camera to the stored cursor position
252 * @return The first found GUILane found or nullptr
253 */
255
257 void adoptViewSettings();
258
259private:
260 double calculateRotation(const osg::Vec3d& lookFrom, const osg::Vec3d& lookAt, const osg::Vec3d& up);
261
263 void updateHUDPosition(int width, int height);
264
265 class FXOSGAdapter : public osgViewer::GraphicsWindow {
266 public:
267 FXOSGAdapter(GUISUMOAbstractView* parent, FXCursor* cursor);
268 void grabFocus();
269 void grabFocusIfPointerInWindow() {}
270 void useCursor(bool cursorOn);
271
272 bool makeCurrentImplementation();
273 bool releaseContext();
274 void swapBuffersImplementation();
275
276 // not implemented yet...just use dummy implementation to get working.
277 bool valid() const {
278 return true;
279 }
280 bool realizeImplementation() {
281 return true;
282 }
283 bool isRealizedImplementation() const {
284 return true;
285 }
286 void closeImplementation() {}
287 bool releaseContextImplementation() {
288 return true;
289 }
290 void requestWarpPointer(float x, float y) {
291 int xRound = std::lround(x);
292 int yRound = std::lround(y);
293 int xPrev, yPrev;
294 unsigned int buttons;
295 myParent->getCursorPosition(xPrev, yPrev, buttons);
296 if (xRound - xPrev != 0 || yRound - yPrev != 0) {
297 myParent->setCursorPosition(xRound, yRound);
298 getEventQueue()->mouseWarped(x, y);
299 }
300 }
301
302 protected:
303 ~FXOSGAdapter();
304 private:
305 GUISUMOAbstractView* const myParent;
306 FXCursor* const myOldCursor;
307 };
308
309 class PlaneMoverCallback : public osg::Callback {
310 public:
311 PlaneMoverCallback(osg::Camera* camera) : myCamera(camera) {};
312 virtual bool run(osg::Object* object, osg::Object* /* data */) override {
313 osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(object);
314 osg::Vec3d lookFrom, lookAt, up;
315 myCamera->getViewMatrixAsLookAt(lookFrom, lookAt, up);
316 osg::Vec3d direction = lookAt - lookFrom;
317 direction.normalize();
318 osg::Vec3d lookAtGround = lookFrom - direction * (lookFrom.z() / direction.z());
319 osg::Matrixd translateMatrix;
320 translateMatrix.makeTranslate(lookAtGround.x(), lookAtGround.y(), 0.);
321 double angle = atan2(direction.y(), direction.x());
322 osg::Matrixd rotMatrix = osg::Matrixd::rotate(angle, osg::Z_AXIS);
323 mt->setMatrix(rotMatrix * translateMatrix);
324 return true;
325 }
326 protected:
327 ~PlaneMoverCallback() {};
328 private:
329 osg::Camera* myCamera;
330 };
331
332 class PickHandler : public osgGA::GUIEventHandler {
333 public:
334 PickHandler(GUIOSGView* parent) : myParent(parent), myDrag(false) {};
335 bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
336 protected:
337 ~PickHandler() {};
338 private:
339 GUIOSGView* const myParent;
340 bool myDrag;
341 };
342
343 class ExcludeFromNearFarComputationCallback : public osg::NodeCallback {
344 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
345 osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
346 // Default value
347 osg::CullSettings::ComputeNearFarMode oldMode = osg::CullSettings::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES;
348 if (cv) {
349 oldMode = cv->getComputeNearFarMode();
350 cv->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
351 }
352 traverse(node, nv);
353 if (cv) {
354 cv->setComputeNearFarMode(oldMode);
355 }
356 }
357 };
358
359
360
361protected:
362 GUIOSGView() {}
363
364 osg::ref_ptr<FXOSGAdapter> myAdapter;
365 osg::ref_ptr<osgViewer::Viewer> myViewer;
366 osg::ref_ptr<osg::Group> myRoot;
367 osg::ref_ptr<osg::MatrixTransform> myPlane;
368 osg::ref_ptr<osg::Camera> myHUD;
369 osg::ref_ptr<osg::Geode> myTextNode;
370 osg::ref_ptr<osgText::Text> myText;
371
372private:
373 GUIVehicle* myTracked;
374 osg::ref_ptr<GUIOSGManipulator> myCameraManipulator;
375 SUMOTime myLastUpdate;
376
377 float myOSGNormalizedCursorX, myOSGNormalizedCursorY;
378
379 std::map<MSVehicle*, OSGMovable > myVehicles;
380 std::map<MSTransportable*, OSGMovable > myPersons;
381
382 osg::ref_ptr<osg::Node> myGreenLight;
383 osg::ref_ptr<osg::Node> myYellowLight;
384 osg::ref_ptr<osg::Node> myRedLight;
385 osg::ref_ptr<osg::Node> myRedYellowLight;
386 osg::ref_ptr<osg::Node> myPoleBase;
387 osg::ref_ptr<osg::Node> myPlaneTransform;
388};
389
390#endif
long long int SUMOTime
Definition GUI.h:36
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 recalculateBoundaries()=0
recalculate boundaries
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)