1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    GUIOSGView.h
11 /// @author  Daniel Krajzewicz
12 /// @date    19.01.2012
13 /// @version $Id$
14 ///
15 // An OSG-based 3D view on the simulation
16 /****************************************************************************/
17 #ifndef GUIOSGView_h
18 #define GUIOSGView_h
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #ifdef HAVE_OSG
26 
27 #include <string>
28 #include <osgGA/TerrainManipulator>
29 #include <osgViewer/Viewer>
30 #include <osg/PositionAttitudeTransform>
31 #include <osg/ShapeDrawable>
32 #include <microsim/traffic_lights/MSTLLogicControl.h>
33 #include <utils/geom/Boundary.h>
34 #include <utils/geom/Position.h>
35 #include <utils/common/RGBColor.h>
36 #include <utils/geom/PositionVector.h>
37 #include <gui/GUISUMOViewParent.h>
38 #include <utils/gui/windows/GUISUMOAbstractView.h>
39 
40 
41 // ===========================================================================
42 // class declarations
43 // ===========================================================================
44 class GUINet;
45 class GUISUMOViewParent;
46 class GUIVehicle;
47 class GUILaneWrapper;
48 class MSRoute;
49 namespace osgGA {
50 class CameraManipulator;
51 class NodeTrackerManipulator;
52 }
53 
54 
55 // ===========================================================================
56 // class definitions
57 // ===========================================================================
58 /**
59  * @class GUIOSGView
60  * @brief An OSG-based 3D view on the simulation
61  */
62 class GUIOSGView : public GUISUMOAbstractView {
FXDECLARE(GUIOSGView)63     FXDECLARE(GUIOSGView)
64 public:
65     /**
66      * @class Command_TLSChange
67      * @brief Updates scene on each tls switch
68      */
69     class Command_TLSChange : public MSTLLogicControl::OnSwitchAction {
70     public:
71         /** @brief Constructor
72          *
73          * @param[in] link   The link to observe
74          * @param[in] root   the root of the scene
75          * @param[in] green  the green light
76          * @param[in] yellow the yellow light
77          * @param[in] red    the red light
78          */
79         Command_TLSChange(const MSLink* const link, osg::Switch* switchNode);
80 
81 
82         /// @brief Destructor
83         virtual ~Command_TLSChange();
84 
85 
86         /** @brief Executes the command
87          *
88          * Called when an active tls program switches.
89          *  If the state of the observed linkchanged, this method removes
90          *  the old traffic light and adds a new one.
91          */
92         void execute();
93 
94 
95     private:
96         /// @brief The link to observe
97         const MSLink* const myLink;
98 
99         /// @brief The switch for the traffic light models
100         osg::ref_ptr<osg::Switch> mySwitch;
101 
102         /// @brief The previous link state
103         LinkState myLastState;
104 
105 
106     private:
107         /// @brief Invalidated copy constructor.
108         Command_TLSChange(const Command_TLSChange&);
109 
110         /// @brief Invalidated assignment operator.
111         Command_TLSChange& operator=(const Command_TLSChange&);
112 
113     };
114 
115 
116     struct OSGMovable {
117         osg::ref_ptr<osg::PositionAttitudeTransform> pos;
118         osg::ref_ptr<osg::ShapeDrawable> geom;
119         osg::ref_ptr<osg::Switch> lights;
120         bool active;
121     };
122 
123 
124     /// constructor
125     GUIOSGView(FXComposite* p, GUIMainWindow& app,
126                GUISUMOViewParent* parent, GUINet& net, FXGLVisual* glVis,
127                FXGLCanvas* share);
128 
129     virtual ~GUIOSGView();
130 
131     /// builds the view toolbars
132     virtual void buildViewToolBars(GUIGlChildWindow&);
133 
134     /// recenters the view
135     void recenterView();
136 
137     /** @brief centers to the chosen artifact
138      * @param[in] id The id of the artifact to center to
139      * @param[in] applyZoom Whether to zoom in
140      * @param[in] zoomDist The distance in m to use for the zoom, values < 0 means: use the centeringBoundary
141      * @note caller is responsible for calling update
142      */
143     void centerTo(GUIGlID id, bool applyZoom, double zoomDist = 20);
144 
145     void showViewportEditor();
146 
147     /// applies the given viewport settings
148     void setViewportFromToRot(const Position& lookFrom, const Position& lookAt, double rotation);
149 
150     ///@brief copy the viewport to the given view
151     void copyViewportTo(GUISUMOAbstractView* view);
152 
153     /** @brief Starts vehicle tracking
154      * @param[in] id The glID of the vehicle to track
155      */
156     void startTrack(int id);
157 
158 
159     /** @brief Stops vehicle tracking
160      */
161     void stopTrack();
162 
163 
164     /** @brief Returns the id of the tracked vehicle (-1 if none)
165      * @return The glID of the vehicle to track
166      */
167     GUIGlID getTrackedID() const;
168 
169     bool setColorScheme(const std::string& name);
170 
171     /// @brief handle mouse click in gaming mode
172     void onGamingClick(Position pos);
173 
174     /// @brief get the current simulation time
175     SUMOTime getCurrentTimeStep() const;
176 
177     void removeVeh(MSVehicle* veh);
178     void removeTransportable(MSTransportable* t);
179 
180     // callback
181     long onConfigure(FXObject*, FXSelector, void*);
182     long onKeyPress(FXObject*, FXSelector, void*);
183     long onKeyRelease(FXObject*, FXSelector, void*);
184     long onLeftBtnPress(FXObject*, FXSelector, void*);
185     long onLeftBtnRelease(FXObject*, FXSelector, void*);
186     long onMiddleBtnPress(FXObject*, FXSelector, void*);
187     long onMiddleBtnRelease(FXObject*, FXSelector, void*);
188     long onRightBtnPress(FXObject*, FXSelector, void*);
189     long onRightBtnRelease(FXObject*, FXSelector, void*);
190     //long onMotion(FXObject*, FXSelector, void*);
191     long onMouseMove(FXObject*, FXSelector, void*);
192     long onPaint(FXObject*, FXSelector, void*);
193     long OnIdle(FXObject* sender, FXSelector sel, void* ptr);
194 
195 private:
196     class SUMOTerrainManipulator : public osgGA::TerrainManipulator {
197     public:
SUMOTerrainManipulator()198         SUMOTerrainManipulator() {
199             setAllowThrow(false);
200         }
performMovementLeftMouseButton(const double eventTimeDelta,const double dx,const double dy)201         bool performMovementLeftMouseButton(const double eventTimeDelta, const double dx, const double dy) {
202             return osgGA::TerrainManipulator::performMovementMiddleMouseButton(eventTimeDelta, dx, dy);
203         }
performMovementMiddleMouseButton(const double eventTimeDelta,const double dx,const double dy)204         bool performMovementMiddleMouseButton(const double eventTimeDelta, const double dx, const double dy) {
205             return osgGA::TerrainManipulator::performMovementLeftMouseButton(eventTimeDelta, dx, dy);
206         }
performMovementRightMouseButton(const double eventTimeDelta,const double dx,const double dy)207         bool performMovementRightMouseButton(const double eventTimeDelta, const double dx, const double dy) {
208             return osgGA::TerrainManipulator::performMovementRightMouseButton(eventTimeDelta, dx, -dy);
209         }
210     };
211 
212     class FXOSGAdapter : public osgViewer::GraphicsWindow {
213     public:
214         FXOSGAdapter(GUISUMOAbstractView* parent, FXCursor* cursor);
215         void grabFocus();
grabFocusIfPointerInWindow()216         void grabFocusIfPointerInWindow() {}
217         void useCursor(bool cursorOn);
218 
219         bool makeCurrentImplementation();
220         bool releaseContext();
221         void swapBuffersImplementation();
222 
223         // not implemented yet...just use dummy implementation to get working.
valid()224         bool valid() const {
225             return true;
226         }
realizeImplementation()227         bool realizeImplementation() {
228             return true;
229         }
isRealizedImplementation()230         bool isRealizedImplementation() const  {
231             return true;
232         }
closeImplementation()233         void closeImplementation() {}
releaseContextImplementation()234         bool releaseContextImplementation() {
235             return true;
236         }
237 
238     protected:
239         ~FXOSGAdapter();
240     private:
241         GUISUMOAbstractView* const myParent;
242         FXCursor* const myOldCursor;
243     };
244 
245 protected:
246 
247     osg::ref_ptr<FXOSGAdapter> myAdapter;
248     osg::ref_ptr<osgViewer::Viewer> myViewer;
249     osg::ref_ptr<osg::Group> myRoot;
250 
251 private:
252     GUIVehicle* myTracked;
253     osg::ref_ptr<osgGA::CameraManipulator> myCameraManipulator;
254 
255     SUMOTime myLastUpdate;
256 
257     std::map<MSVehicle*, OSGMovable > myVehicles;
258     std::map<MSTransportable*, OSGMovable > myPersons;
259 
260     osg::ref_ptr<osg::Node> myGreenLight;
261     osg::ref_ptr<osg::Node> myYellowLight;
262     osg::ref_ptr<osg::Node> myRedLight;
263     osg::ref_ptr<osg::Node> myRedYellowLight;
264 
265 protected:
GUIOSGView()266     GUIOSGView() { }
267 
268 };
269 
270 #endif
271 
272 #endif
273 
274 /****************************************************************************/
275 
276 
277