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 GUISUMOAbstractView.h 11 /// @author Daniel Krajzewicz 12 /// @author Jakob Erdmann 13 /// @author Michael Behrisch 14 /// @author Andreas Gaubatz 15 /// @date Sept 2002 16 /// @version $Id$ 17 /// 18 // The base class for a view 19 /****************************************************************************/ 20 #ifndef GUISUMOAbstractView_h 21 #define GUISUMOAbstractView_h 22 23 24 // =========================================================================== 25 // included modules 26 // =========================================================================== 27 #include <config.h> 28 29 #include <string> 30 #include <vector> 31 #include <map> 32 #include <fx.h> 33 // fx3d includes windows.h so we need to guard against macro pollution 34 #ifdef WIN32 35 #define NOMINMAX 36 #endif 37 #include <fx3d.h> 38 #ifdef WIN32 39 #undef NOMINMAX 40 #endif 41 42 #include <utils/geom/Boundary.h> 43 #include <utils/geom/Position.h> 44 #include <utils/common/RGBColor.h> 45 #include <utils/common/SUMOTime.h> 46 #include <utils/gui/globjects/GUIGlObjectTypes.h> 47 #include <foreign/rtree/SUMORTree.h> 48 49 50 // =========================================================================== 51 // class declarations 52 // =========================================================================== 53 class GUIGlChildWindow; 54 class GUIVehicle; 55 class GUIPerspectiveChanger; 56 class GUIMainWindow; 57 class GUIGLObjectPopupMenu; 58 class GUIGlObject; 59 class GUIDialog_EditViewport; 60 class GUIDialog_ViewSettings; 61 class GUIVisualizationSettings; 62 63 64 // =========================================================================== 65 // class definitions 66 // =========================================================================== 67 /** 68 * @class GUISUMOAbstractView 69 * This class is meant to be pure virtual later; 70 * It shall be the main class to inherit views of the simulation (micro- 71 * or macroscopic ones) from it. 72 */ 73 class GUISUMOAbstractView : public FXGLCanvas { 74 FXDECLARE(GUISUMOAbstractView) 75 76 public: 77 /// @brief constructor 78 GUISUMOAbstractView(FXComposite* p, GUIMainWindow& app, GUIGlChildWindow* parent, const SUMORTree& grid, FXGLVisual* glVis, FXGLCanvas* share); 79 80 /// @brief destructor 81 virtual ~GUISUMOAbstractView(); 82 83 /// @brief builds the view toolbars buildViewToolBars(GUIGlChildWindow &)84 virtual void buildViewToolBars(GUIGlChildWindow&) { } 85 86 /// @brief recenters the view 87 virtual void recenterView(); 88 89 /** @brief centers to the chosen artifact 90 * @param[in] id The id of the artifact to center to 91 * @param[in] applyZoom Whether to zoom in 92 * @param[in] zoomDist The distance in m to use for the zoom, values < 0 means: use the centeringBoundary 93 * @note caller is responsible for calling update 94 */ 95 virtual void centerTo(GUIGlID id, bool applyZoom, double zoomDist = 20); 96 97 /// @brief centers to the chosen artifact 98 void centerTo(const Boundary& bound); 99 100 /// @brief applies the given viewport settings 101 virtual void setViewportFromToRot(const Position& lookFrom, const Position& lookAt, double rotation); 102 103 /// @brief copy the viewport to the given view 104 virtual void copyViewportTo(GUISUMOAbstractView* view); 105 106 /// @brief meter-to-pixels conversion method 107 double m2p(double meter) const; 108 109 /// @brief pixels-to-meters conversion method 110 double p2m(double pixel) const; 111 112 /// @brief Returns the information whether rotation is allowd 113 ///@note disabled 114 //bool allowRotation() const; 115 116 /// @brief return windows cursor position 117 Position getWindowCursorPosition() const; 118 119 /// @brief Returns the gl-id of the object under the given coordinates 120 void setWindowCursorPosition(FXint x, FXint y); 121 122 /// @brief A reimplementation due to some internal reasons 123 FXbool makeCurrent(); 124 125 /// @brief returns true, if the edit button was pressed 126 bool isInEditMode(); 127 128 /// @brief get changer 129 GUIPerspectiveChanger& getChanger() const; 130 131 /// @brief get visible boundary 132 Boundary getVisibleBoundary() const; 133 134 /// @brief mouse functions 135 //@{ 136 virtual long onConfigure(FXObject*, FXSelector, void*); 137 virtual long onPaint(FXObject*, FXSelector, void*); 138 virtual long onLeftBtnPress(FXObject*, FXSelector, void*); 139 virtual long onLeftBtnRelease(FXObject*, FXSelector, void*); 140 virtual long onMiddleBtnPress(FXObject*, FXSelector, void*); 141 virtual long onMiddleBtnRelease(FXObject*, FXSelector, void*); 142 virtual long onRightBtnPress(FXObject*, FXSelector, void*); 143 virtual long onRightBtnRelease(FXObject*, FXSelector, void*); 144 virtual long onDoubleClicked(FXObject*, FXSelector, void*); 145 virtual long onMouseWheel(FXObject*, FXSelector, void*); 146 virtual long onMouseMove(FXObject*, FXSelector, void*); 147 virtual long onMouseLeft(FXObject*, FXSelector, void*); 148 //@} 149 150 /// @brief keyboard functions 151 //@{ 152 virtual long onKeyPress(FXObject* o, FXSelector sel, void* data); 153 virtual long onKeyRelease(FXObject* o, FXSelector sel, void* data); 154 //@} 155 156 //@brief open object dialog 157 virtual void openObjectDialog(); 158 159 /// @brief A method that updates the tooltip 160 void updateToolTip(); 161 162 /// @brief @name Dealing with snapshots 163 ///@{ 164 /** @brief Sets the snapshot time to file map 165 * @param[in] snaps The snapshots to take at certain times 166 */ 167 void addSnapshot(SUMOTime time, const std::string& file, const int width = -1, const int height = -1); 168 169 /** @brief Takes a snapshots and writes it into the given file 170 * 171 * The format to use is determined from the extension. 172 * If compiled with ffmpeg and a video format is requested it will instantiate a video encoder. 173 * @param[in] destFile The name of the file to write the snapshot into 174 * @return The error message, if an error occcured; "" otherwise 175 */ 176 std::string makeSnapshot(const std::string& destFile, const int width = -1, const int height = -1); 177 178 /// @brief Adds a frame to a video snapshot which will be initialized if neccessary 179 virtual void saveFrame(const std::string& destFile, FXColor* buf); 180 181 /// @brief Ends a video snapshot endSnapshot()182 virtual void endSnapshot() {} 183 184 /// @brief Checks whether it is time for a snapshot 185 virtual void checkSnapshots(); 186 187 void waitForSnapshots(const SUMOTime snapshotTime); 188 189 /// @brief get the current simulation time 190 virtual SUMOTime getCurrentTimeStep() const; 191 ///@} 192 193 /// @brief get the viewport and create it on first access 194 GUIDialog_EditViewport* getViewportEditor(); 195 196 /// @brief show viewport editor 197 virtual void showViewportEditor(); 198 199 /// @brief show viewsscheme editor 200 void showViewschemeEditor(); 201 202 /// @brief show tool tips 203 void showToolTips(bool val); 204 205 /// @brief set color scheme 206 virtual bool setColorScheme(const std::string&); 207 208 /// @brief get visualitation settings 209 GUIVisualizationSettings* getVisualisationSettings() const; 210 211 /// @brief recalibrate color scheme according to the current value range buildColorRainbow(const GUIVisualizationSettings &,GUIColorScheme &,int,GUIGlObjectType)212 virtual void buildColorRainbow(const GUIVisualizationSettings& /*s*/, GUIColorScheme& /*scheme*/, int /*active*/, GUIGlObjectType /*objectType*/) { } 213 214 /// @brief return list of loaded edgeData attributes getEdgeDataAttrs()215 virtual std::vector<std::string> getEdgeDataAttrs() const { 216 return std::vector<std::string>(); 217 } 218 219 /// @brief return list of available edge parameters getEdgeLaneParamKeys(bool)220 virtual std::vector<std::string> getEdgeLaneParamKeys(bool /*edgeKeys*/) const { 221 return std::vector<std::string>(); 222 } 223 224 /// @brief remove viewport 225 void remove(GUIDialog_EditViewport*); 226 227 /// @brief remove view settings 228 void remove(GUIDialog_ViewSettings*); 229 230 /// @brief get grid width 231 // @todo: check why this is here 232 double getGridWidth() const; 233 234 /// @brief get grid Height 235 // @todo: check why this is here 236 double getGridHeight() const; 237 238 /// @brief star track 239 virtual void startTrack(int /*id*/); 240 241 /// @brief stop track 242 virtual void stopTrack(); 243 244 /// @brief get tracked id 245 virtual GUIGlID getTrackedID() const; 246 247 /// @brief on gaming click 248 virtual void onGamingClick(Position /*pos*/); 249 virtual void onGamingRightClick(Position /*pos*/); 250 251 /// @brief @name Additional visualisations 252 ///@{ 253 /** @brief Adds an object to call its additional visualisation method 254 * @param[in] which The object to add 255 * @return Always true 256 * @see GUIGlObject::drawGLAdditional 257 */ 258 bool addAdditionalGLVisualisation(const GUIGlObject* const which); 259 260 /** @brief Removes an object from the list of objects that show additional things 261 * @param[in] which The object to remoe 262 * @return True if the object was known, false otherwise 263 * @see GUIGlObject::drawGLAdditional 264 */ 265 bool removeAdditionalGLVisualisation(const GUIGlObject* const which); 266 267 /** @brief Check if an object is added in the additional GL visualitation 268 * @param[in] which The object to check 269 * @see GUIGlObject::drawGLAdditional 270 */ 271 bool isAdditionalGLVisualisationEnabled(GUIGlObject* const which) const; 272 ///@} 273 274 /// @brief get position of current popup 275 const Position& getPopupPosition() const; 276 277 /// @brief destoys the popup 278 void destroyPopup(); 279 280 public: 281 ///@struct Decal 282 /// @brief A decal (an image) that can be shown 283 struct Decal { 284 /// @brief Constructor 285 Decal(); 286 287 /// @brief The path to the file the image is located at 288 std::string filename; 289 /// @brief The center of the image in x-direction (net coordinates, in m) 290 double centerX; 291 /// @brief The center of the image in y-direction (net coordinates, in m) 292 double centerY; 293 /// @brief The center of the image in z-direction (net coordinates, in m) 294 double centerZ; 295 /// @brief The width of the image (net coordinates in x-direction, in m) 296 double width; 297 /// @brief The height of the image (net coordinates in y-direction, in m) 298 double height; 299 /// @brief The altitude of the image (net coordinates in z-direction, in m) 300 double altitude; 301 /// @brief The rotation of the image in the ground plane (in degrees) 302 double rot; 303 /// @brief The tilt of the image to the ground plane (in degrees) 304 double tilt; 305 /// @brief The roll of the image to the ground plane (in degrees) 306 double roll; 307 /// @brief The layer of the image 308 double layer; 309 /// @brief Whether this image was initialised (inserted as a texture) 310 bool initialised; 311 /// @brief Whether this image should be skipped in 2D-views 312 bool skip2D; 313 /// @brief Whether this image should be skipped in 2D-views 314 bool screenRelative; 315 /// @brief whether the decal shall be drawn in screen coordinates, rather than network coordinates 316 int glID; 317 /// @brief The image pointer for later cleanup 318 FXImage* image; 319 }; 320 321 public: 322 /// @brief get coloring schemes combo 323 FXComboBox* getColoringSchemesCombo(); 324 325 /// @brief Returns the cursor's x/y position within the network 326 Position getPositionInformation() const; 327 328 /**@brief Returns a position that is mapped to the closest grid point if the grid is active 329 * @brief note: formats are pos(x,y,0) por pos(0,0,z) 330 */ 331 Position snapToActiveGrid(const Position& pos) const; 332 333 /// @brief Translate screen position to network position 334 Position screenPos2NetPos(int x, int y) const; 335 336 /// @brief add decals 337 void addDecals(const std::vector<Decal>& decals); 338 339 /// @brief get visualisation settings 340 GUIVisualizationSettings* getVisualisationSettings(); 341 342 /// @brief Returns the delay of the parent application 343 double getDelay() const; 344 345 /// @brief Sets the delay of the parent application 346 void setDelay(double delay); 347 348 /** @brief Sets the breakpoints of the parent application */ 349 void setBreakpoints(const std::vector<SUMOTime>& breakpoints); 350 351 /// @brief retrieve breakpoints if provided by the application retrieveBreakpoints()352 virtual const std::vector<SUMOTime> retrieveBreakpoints() const { 353 return std::vector<SUMOTime>(); 354 } 355 356 /// @brief retrieve FPS 357 double getFPS() const; 358 359 protected: 360 /// @brief performs the painting of the simulation 361 void paintGL(); 362 363 /// @brief update position information 364 void updatePositionInformation() const; 365 366 /// @brief paint GL 367 virtual int doPaintGL(int /*mode*/, const Boundary& /*boundary*/); 368 369 /// @brief doInit 370 virtual void doInit(); 371 372 /// @brief paints a grid 373 void paintGLGrid(); 374 375 /// @brief Draws a line with ticks, and the length information. 376 void displayLegend(); 377 378 /// @brief Draws frames-per-second indicator 379 void drawFPS(); 380 381 /// @brief returns the id of the front object under the cursor using GL_SELECT 382 GUIGlID getObjectUnderCursor(); 383 384 /// @brief returns the id of the objects under the cursor using GL_SELECT (including overlapped objects) 385 std::vector<GUIGlID> getObjectstUnderCursor(); 386 387 /// @brief returns the GUIGlObject under the cursor using GL_SELECT (including overlapped objects) 388 std::vector<GUIGlObject*> getGUIGlObjectsUnderCursor(); 389 390 /// @brief returns the id of the object at position using GL_SELECT 391 GUIGlID getObjectAtPosition(Position pos); 392 393 /// @brief returns the ids of the object at position within the given (rectangular) radius using GL_SELECT 394 std::vector<GUIGlID> getObjectsAtPosition(Position pos, double radius); 395 396 /// @brief returns the GUIGlObjects at position within the given (rectangular) radius using GL_SELECT 397 std::vector<GUIGlObject*> getGUIGlObjectsAtPosition(Position pos, double radius); 398 399 /// @brief returns the ids of all objects in the given boundary 400 std::vector<GUIGlID> getObjectsInBoundary(Boundary bound); 401 402 /// @brief invokes the tooltip for the given object 403 void showToolTipFor(const GUIGlID id); 404 405 protected: 406 /// @brief FOX need this GUISUMOAbstractView()407 GUISUMOAbstractView() {} 408 409 /// @brief check whether we can read image data or position with gdal 410 FXImage* checkGDALImage(Decal& d); 411 412 /// @brief Draws the stored decals 413 void drawDecals(); 414 415 /// @brief applies gl-transformations to fit the Boundary given by myChanger onto the canvas. 416 /// If fixRatio is true, this boundary will be enlarged to prevent anisotropic stretching. 417 /// (this should be set to false when doing selections) 418 Boundary applyGLTransform(bool fixRatio = true); 419 420 protected: 421 /// @brief The application 422 GUIMainWindow* myApp; 423 424 /// @brief The parent window 425 GUIGlChildWindow* myParent; 426 427 /// @brief The visualization speed-up 428 SUMORTree* myGrid; 429 430 /// @brief The perspective changer 431 GUIPerspectiveChanger* myChanger; 432 433 /// @brief Information whether too-tip informations shall be generated 434 bool myInEditMode; 435 436 /// @brief Offset to the mouse-hotspot from the mouse position 437 int myMouseHotspotX, myMouseHotspotY; 438 439 /// @brief The current popup-menu 440 GUIGLObjectPopupMenu* myPopup; 441 442 /// @brief The current popup-menu position 443 Position myPopupPosition; 444 445 /// @brief visualization settings 446 GUIVisualizationSettings* myVisualizationSettings; 447 448 /// @brief use tool tips 449 bool myUseToolTips; 450 451 /// @brief Internal information whether doInit() was called 452 bool myAmInitialised; 453 454 /// @brief viewport chooser 455 GUIDialog_EditViewport* myViewportChooser; 456 457 /// @brief Position of the cursor relative to the window 458 FXint myWindowCursorPositionX, myWindowCursorPositionY; 459 460 /// @brief Visualization changer 461 GUIDialog_ViewSettings* myVisualizationChanger; 462 463 /// @brief @name Optionally shown decals 464 ///@{ 465 /// @brief The list of decals to show 466 std::vector<Decal> myDecals; 467 468 /// @brief The mutex to use before accessing the decals list in order to avoid thread conflicts 469 FXMutex myDecalsLock; 470 ///@} 471 472 /// @brief Snapshots 473 std::map<SUMOTime, std::vector<std::tuple<std::string, int, int> > > mySnapshots; 474 475 /// @brief The mutex to use before accessing the decals list in order to avoid thread conflicts 476 FXMutex mySnapshotsMutex; 477 478 /// @brief the semaphore when waiting for snapshots to finish 479 FXCondition mySnapshotCondition; 480 481 /// @brief poly draw lock 482 mutable FXMutex myPolyDrawLock; 483 484 /// @brief List of objects for which GUIGlObject::drawGLAdditional is called 485 std::map<const GUIGlObject*, int> myAdditionallyDrawn; 486 487 /// @brief counter for measuring rendering time 488 long myFrameDrawTime; 489 490 private: 491 // @brief sensitivity for "<>AtPosition(...) functions 492 static const double SENSITIVITY; 493 }; 494 495 496 #endif 497 498 /****************************************************************************/ 499 500