QGIS API Documentation 4.3.0-Master (0cef2469b72)
Loading...
Searching...
No Matches
qgscameracontroller.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscameracontroller.h
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSCAMERACONTROLLER_H
17#define QGSCAMERACONTROLLER_H
18
19#include "qgis_3d.h"
20#include "qgscamerapose.h"
22
23#include <QImage>
24#include <Qt3DCore/QEntity>
25#include <Qt3DInput/QMouseEvent>
26
27#ifndef SIP_RUN
28namespace Qt3DInput
29{
30 class QKeyEvent;
31 class QKeyboardHandler;
32 class QMouseEvent;
33 class QMouseHandler;
34 class QWheelEvent;
35} // namespace Qt3DInput
36
37namespace Qt3DRender
38{
39 class QCamera;
40}
41
42#endif
43
44class QDomDocument;
45class QDomElement;
46
47class QgsCameraPose;
48class QgsVector3D;
50class Qgs3DMapScene;
51class QgsCrossSection;
52class QgsPointerLocker;
53
58#ifndef SIP_RUN
59class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
60{
61#else
62class _3D_EXPORT QgsCameraController : public QObject
63{
64#endif
65
66 Q_OBJECT
67 public:
71
72#ifndef SIP_RUN
73
78 Qt3DRender::QCamera *camera() const { return mCamera; }
79#endif
80
85 Qgis::NavigationMode cameraNavigationMode() const { return mCameraNavigationMode; }
86
91 double cameraMovementSpeed() const { return mCameraMovementSpeed; }
92
97 void setCameraMovementSpeed( double movementSpeed );
98
103 Qgis::VerticalAxisInversionFlags verticalAxisInversion() const { return mVerticalAxisInversion; }
104
109 void setVerticalAxisInversion( Qgis::VerticalAxisInversionFlags inversion );
110
112 void frameTriggered( float dt );
113
115 void resetView( float distance );
116
118 void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
119
121 QgsVector3D lookingAtPoint() const;
122
129 void setLookingAtPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
130
135 QgsVector3D lookingAtMapPoint() const;
136
141 void setLookingAtMapPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
142
147 void setCameraPose( const QgsCameraPose &camPose, bool force = false );
148
153 QgsCameraPose cameraPose() const { return mCameraPose; }
154
160 float distance() const { return mCameraPose.distanceFromCenterPoint(); }
161
167 float pitch() const { return mCameraPose.pitchAngle(); }
168
174 float yaw() const { return mCameraPose.headingAngle(); }
175
177 QDomElement writeXml( QDomDocument &doc ) const;
179 void readXml( const QDomElement &elem, QgsVector3D savedOrigin );
180
182 void zoom( float factor );
184 void tiltUpAroundViewCenter( float deltaPitch );
186 void rotateAroundViewCenter( float deltaYaw );
188 void setCameraHeadingAngle( float angle );
190 void moveView( float tx, float ty );
191
196 void walkView( double tx, double ty, double tz );
197
204 void rotateCamera( float diffPitch, float diffYaw );
205
211 void rotateCameraAroundPivot( float newPitch, float newHeading, const QVector3D &pivotPoint );
212
220 Q_DECL_DEPRECATED void zoomCameraAroundPivot( const QVector3D &oldCameraPosition, double zoomFactor, const QVector3D &pivotPoint ) SIP_DEPRECATED;
221
228 void zoomCameraAroundPivot( const QVector3D &oldCameraPosition, double oldDistanceFromCenterPoint, double zoomFactor, const QVector3D &pivotPoint );
229
234 bool keyboardEventFilter( QKeyEvent *event );
235
242 void setOrigin( const QgsVector3D &origin );
243
248 void setInputHandlersEnabled( bool enable ) { mInputHandlersEnabled = enable; }
249
254 bool hasInputHandlersEnabled() const { return mInputHandlersEnabled; }
255
262 void globeMoveCenterPoint( double latDiff, double lonDiff );
263
269 void globeZoom( float factor );
270
275 void globeUpdatePitchAngle( float angleDiff );
276
281 void globeUpdateHeadingAngle( float angleDiff );
282
288 void resetGlobe( float distance, double lat = 0, double lon = 0 );
289
294 const QgsVector3D origin() const { return mOrigin; }
295
301 void setCrossSectionSideView( const QgsCrossSection &crossSection );
302
303 // Convenience methods to set camera view to standard positions
305 void rotateCameraToHome() { rotateToRespectingTerrain( 45.0f, 45.0f ); }
307 void rotateCameraToTop() { rotateToRespectingTerrain( 0.0f, 0.0f ); }
309 void rotateCameraToNorth() { rotateToRespectingTerrain( 90.0f, 180.0f ); }
311 void rotateCameraToEast() { rotateToRespectingTerrain( 90.0f, 90.0f ); }
313 void rotateCameraToSouth() { rotateToRespectingTerrain( 90.0f, 0.0f ); }
315 void rotateCameraToWest() { rotateToRespectingTerrain( 90.0f, -90.0f ); }
317 void rotateCameraToBottom() { rotateToRespectingTerrain( 180.0f, 0.0f ); }
318
319 public slots:
320
325 void setCameraNavigationMode( Qgis::NavigationMode navigationMode );
326
331 void depthBufferCaptured( const QImage &depthImage );
332
337 void moveCenterPoint( const QVector3D &posDiff );
338
346 double sampleDepthBuffer( int px, int py );
347
348
349 private:
350#ifdef SIP_RUN
353#endif
354
359 void updateOrthographicProjectionPlane();
361 void updateCameraFromPose();
363 QWindow *window() const;
364
366 enum class MouseOperation
367 {
368 None = 0,
369 Translation,
370 RotationCamera,
371 RotationCenter,
372 Zoom,
373 ZoomWheel
374 };
375
376 // This list gathers all the rotation and translation operations.
377 // It is used to update the appropriate parameters when successive
378 // translation and rotation happen.
379 // clang-format off
380 const QList<MouseOperation> mTranslateOrRotate = {
381 MouseOperation::Translation,
382 MouseOperation::RotationCamera,
383 MouseOperation::RotationCenter
384 };
385 // clang-format on
386
387 // check that current sequence (current operation and new operation) is a rotation or translation
388 bool isATranslationRotationSequence( MouseOperation newOperation ) const;
389
390 void setMouseParameters( const MouseOperation &newOperation, const QPoint &clickPoint = QPoint() );
391
397 void rotateToRespectingTerrain( float pitch, float yaw );
398
399 signals:
402
405
409 void cameraMovementSpeedChanged( double speed );
410
415 void setCursorPosition( QPoint point );
416
422
427 void cameraRotationCenterChanged( QVector3D position );
428
434
435 private slots:
436 void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
437 void onWheel( Qt3DInput::QWheelEvent *wheel );
438 void onMousePressed( Qt3DInput::QMouseEvent *mouse );
439 void onMouseReleased( Qt3DInput::QMouseEvent *mouse );
440 void applyFlyModeKeyMovements();
441
442 private:
443 // All three methods return true if event is handled
444 bool onKeyPressedFlyNavigation( QKeyEvent *event );
445 bool onKeyPressedTerrainNavigation( QKeyEvent *event );
446 bool onKeyPressedGlobeTerrainNavigation( QKeyEvent *event );
447 void onPositionChangedFlyNavigation( Qt3DInput::QMouseEvent *mouse );
448 void onPositionChangedTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
449 void onPositionChangedGlobeTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
450
451 void handleTerrainNavigationWheelZoom();
452
453 // Returns the average depth of all non void pixels
454 double depthBufferNonVoidAverage();
455
456#ifndef SIP_RUN
458 bool screenPointToWorldPos( QPoint position, double &depth, QVector3D &worldPosition );
459#endif
460
461 // Moves given point (in ECEF) by specified lat/lon angle difference (in degrees) and returns new ECEF point
462 QgsVector3D moveGeocentricPoint( const QgsVector3D &point, double latDiff, double lonDiff );
463
464 void applyRelativePointerRotation( double dx, double dy );
465
467 Qgs3DMapScene *mScene = nullptr;
468
470 Qt3DRender::QCamera *mCamera = nullptr;
471
473 QgsCameraPose mCameraPose;
474
476 QPoint mMousePos;
477
479 QPoint mClickPoint;
480
481 // false when no depth buffer captured or new capture requested and not yet done.
482 bool mDepthBufferIsReady = false;
483 QImage mDepthBufferImage;
484 // -1 when unset
485 // TODO: Change to std::optional<double>
486 double mDepthBufferNonVoidAverage = -1;
487 // nullptr when !mDepthBufferIsReady
488 std::unique_ptr<Qt3DRender::QCamera> mDepthBufferCamera;
489
490 std::unique_ptr<Qt3DRender::QCamera> mCameraBefore;
491
492 bool mRotationCenterCalculated = false;
493 QVector3D mRotationCenter;
494 double mRotationDistanceFromCenter = 0;
495 float mRotationPitch = 0;
496 float mRotationYaw = 0;
497
498 bool mDragPointCalculated = false;
499 QVector3D mDragPoint;
500 double mDragDepth = 0;
501
502 bool mZoomPointCalculated = false;
503 QVector3D mZoomPoint;
504
505 // used for globe
506 QgsVector3D mMousePressViewCenter;
507 QgsCoordinateTransform mGlobeCrsToLatLon;
508
509 Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
510 Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
511 bool mInputHandlersEnabled = true;
514 double mCameraMovementSpeed = 5.0;
515
516 QSet<int> mDepressedKeys;
517 bool mCaptureFpsMouseMovements = false;
518 bool mIgnoreNextMouseMove = false;
519 QTimer *mFpsNavTimer = nullptr;
520
521 double mCumulatedWheelY = 0;
522
523 MouseOperation mCurrentOperation = MouseOperation::None;
524
525 // 3D world's origin in map coordinates
526 QgsVector3D mOrigin;
527
529 bool mCameraChanged = false;
530
531 QgsPointerLocker *mPointerLocker = nullptr;
532
533 // To test the cameracontroller
534 friend class TestQgs3DRendering;
536};
537
538#endif // QGSCAMERACONTROLLER_H
@ WhenPivoting
When pivoting camera around point in terrain.
Definition qgis.h:4619
@ WhenRotatingDragging
When rotating camera around self with mouse captured.
Definition qgis.h:4617
NavigationMode
The navigation mode used by 3D cameras.
Definition qgis.h:4591
@ TerrainBased
The default navigation based on the terrain.
Definition qgis.h:4592
QFlags< VerticalAxisInversion > VerticalAxisInversionFlags
Definition qgis.h:4628
Entity that encapsulates our 3D scene - contains all other entities (such as terrain) as children.
Object that controls camera movement based on user input.
void navigationModeChanged(Qgis::NavigationMode mode)
Emitted when the navigation mode is changed using the hotkey ctrl + ~.
bool hasInputHandlersEnabled() const
Returns whether the camera controller responds to mouse and keyboard events.
QgsCameraPose cameraPose() const
Returns camera pose.
void rotateCameraToBottom()
Rotate to bottom-up view.
float pitch() const
Returns pitch angle in degrees (0 = looking from the top, 90 = looking from the side).
Qt3DRender::QCamera * camera() const
Returns camera that is being controlled.
~QgsCameraController() override
float yaw() const
Returns yaw angle in degrees.
void requestDepthBufferCapture()
Emitted to ask for the depth buffer image.
void rotateCameraToTop()
Rotate to top-down view.
void rotateCameraToEast()
Rotate to view from the east.
const QgsVector3D origin() const
Returns the origin of the scene in map coordinates.
void rotateCameraToHome()
Rotate to diagonal view.
void rotateCameraToNorth()
Rotate to view from the north.
float distance() const
Returns distance of the camera from the point it is looking at.
friend class TestQgs3DCameraController
void rotateCameraToWest()
Rotate to view from the west.
double cameraMovementSpeed() const
Returns the camera movement speed.
void cameraChanged()
Emitted when camera has been updated.
Qgis::VerticalAxisInversionFlags verticalAxisInversion() const
Returns the vertical axis inversion behavior.
void cameraMovementSpeedChanged(double speed)
Emitted whenever the camera movement speed is changed by the controller.
QgsCameraController(Qgs3DMapScene *scene)
Constructs the camera controller with optional parent node that will take ownership.
void depthBufferReady()
Emitted after the depth buffer has been captured and is ready to sample.
void rotateCameraToSouth()
Rotate to view from the south.
Qgis::NavigationMode cameraNavigationMode() const
Returns the navigation mode used by the camera controller.
void cameraRotationCenterChanged(QVector3D position)
Emitted when the camera rotation center changes.
void setInputHandlersEnabled(bool enable)
Sets whether the camera controller responds to mouse and keyboard events.
void setCursorPosition(QPoint point)
Emitted when the mouse cursor position should be moved to the specified point on the map viewport.
Encapsulates camera pose in a 3D scene.
Handles coordinate transforms between two coordinate systems.
Encapsulates the definition of a cross section in 3D map coordinates.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:33
On-screen 3D engine: it creates an OpenGL window (QWindow) and displays rendered 3D scenes there.
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:138