QGIS API Documentation 3.43.0-Master (3ee7834ace6)
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 <Qt3DCore/QEntity>
24#include <Qt3DInput/QMouseEvent>
25#include <QImage>
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;
51
56#ifndef SIP_RUN
57class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
58{
59#else
60class _3D_EXPORT QgsCameraController : public QObject
61{
62#endif
63
64 Q_OBJECT
65 public:
69
70#ifndef SIP_RUN
71
76 Qt3DRender::QCamera *camera() const { return mCamera; }
77#endif
78
83 Qgis::NavigationMode cameraNavigationMode() const { return mCameraNavigationMode; }
84
89 double cameraMovementSpeed() const { return mCameraMovementSpeed; }
90
95 void setCameraMovementSpeed( double movementSpeed );
96
101 Qgis::VerticalAxisInversion verticalAxisInversion() const { return mVerticalAxisInversion; }
102
107 void setVerticalAxisInversion( Qgis::VerticalAxisInversion inversion );
108
110 void frameTriggered( float dt );
111
113 void resetView( float distance );
114
116 void setViewFromTop( float worldX, float worldY, float distance, float yaw = 0 );
117
119 QgsVector3D lookingAtPoint() const;
120
127 void setLookingAtPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
128
133 QgsVector3D lookingAtMapPoint() const;
134
139 void setLookingAtMapPoint( const QgsVector3D &point, float distance, float pitch, float yaw );
140
145 void setCameraPose( const QgsCameraPose &camPose, bool force = false );
146
151 QgsCameraPose cameraPose() const { return mCameraPose; }
152
158 float distance() const { return mCameraPose.distanceFromCenterPoint(); }
159
165 float pitch() const { return mCameraPose.pitchAngle(); }
166
172 float yaw() const { return mCameraPose.headingAngle(); }
173
175 QDomElement writeXml( QDomDocument &doc ) const;
177 void readXml( const QDomElement &elem );
178
180 void zoom( float factor );
182 void tiltUpAroundViewCenter( float deltaPitch );
184 void rotateAroundViewCenter( float deltaYaw );
186 void setCameraHeadingAngle( float angle );
188 void moveView( float tx, float ty );
189
194 void walkView( double tx, double ty, double tz );
195
202 void rotateCamera( float diffPitch, float diffYaw );
203
209 void rotateCameraAroundPivot( float newPitch, float newHeading, const QVector3D &pivotPoint );
210
217 void zoomCameraAroundPivot( const QVector3D &oldCameraPosition, double zoomFactor, const QVector3D &pivotPoint );
218
224 bool willHandleKeyEvent( QKeyEvent *event );
225
232 void setOrigin( const QgsVector3D &origin );
233
238 void setInputHandlersEnabled( bool enable ) { mInputHandlersEnabled = enable; }
239
244 bool hasInputHandlersEnabled() const { return mInputHandlersEnabled; }
245
252 void globeMoveCenterPoint( double latDiff, double lonDiff );
253
259 void globeZoom( float factor );
260
265 void globeUpdatePitchAngle( float angleDiff );
266
271 void globeUpdateHeadingAngle( float angleDiff );
272
278 void resetGlobe( float distance, double lat = 0, double lon = 0 );
279
280 public slots:
281
286 void setCameraNavigationMode( Qgis::NavigationMode navigationMode );
287
292 void depthBufferCaptured( const QImage &depthImage );
293
294 private:
295#ifdef SIP_RUN
298#endif
299
300 void updateCameraFromPose();
301 void moveCameraPositionBy( const QVector3D &posDiff );
303 QWindow *window() const;
304
306 enum class MouseOperation
307 {
308 None = 0, // no operation
309 Translation, // left button pressed, no modifier
310 RotationCamera, // left button pressed + ctrl modifier
311 RotationCenter, // left button pressed + shift modifier
312 Zoom, // right button pressed
313 ZoomWheel // mouse wheel scroll
314 };
315
316 // This list gathers all the rotation and translation operations.
317 // It is used to update the appropriate parameters when successive
318 // translation and rotation happen.
319 const QList<MouseOperation> mTranslateOrRotate = {
320 MouseOperation::Translation,
321 MouseOperation::RotationCamera,
322 MouseOperation::RotationCenter
323 };
324
325 // check that current sequence (current operation and new operation) is a rotation or translation
326 bool isATranslationRotationSequence( MouseOperation newOperation ) const;
327
328 void setMouseParameters( const MouseOperation &newOperation, const QPoint &clickPoint = QPoint() );
329
330 signals:
333
336
340 void cameraMovementSpeedChanged( double speed );
341
346 void setCursorPosition( QPoint point );
347
353
358 void cameraRotationCenterChanged( QVector3D position );
359
360 private slots:
361 void onPositionChanged( Qt3DInput::QMouseEvent *mouse );
362 void onWheel( Qt3DInput::QWheelEvent *wheel );
363 void onMousePressed( Qt3DInput::QMouseEvent *mouse );
364 void onMouseReleased( Qt3DInput::QMouseEvent *mouse );
365 void onKeyPressed( Qt3DInput::QKeyEvent *event );
366 void onKeyReleased( Qt3DInput::QKeyEvent *event );
367 void applyFlyModeKeyMovements();
368
369 private:
370 void onKeyPressedFlyNavigation( Qt3DInput::QKeyEvent *event );
371 void onKeyPressedTerrainNavigation( Qt3DInput::QKeyEvent *event );
372 void onKeyPressedGlobeTerrainNavigation( Qt3DInput::QKeyEvent *event );
373 void onPositionChangedFlyNavigation( Qt3DInput::QMouseEvent *mouse );
374 void onPositionChangedTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
375 void onPositionChangedGlobeTerrainNavigation( Qt3DInput::QMouseEvent *mouse );
376
377 void handleTerrainNavigationWheelZoom();
378
385 double sampleDepthBuffer( int px, int py );
386
387 // Returns the average depth of all non void pixels
388 double depthBufferNonVoidAverage();
389
390#ifndef SIP_RUN
392 bool screenPointToWorldPos( QPoint position, Qt3DRender::QCamera *cameraBefore, double &depth, QVector3D &worldPosition );
393#endif
394
395 // Moves given point (in ECEF) by specified lat/lon angle difference (in degrees) and returns new ECEF point
396 QgsVector3D moveGeocentricPoint( const QgsVector3D &point, double latDiff, double lonDiff );
397
399 Qgs3DMapScene *mScene = nullptr;
400
402 Qt3DRender::QCamera *mCamera = nullptr;
403
405 QgsCameraPose mCameraPose;
406
408 QPoint mMousePos;
409
411 QPoint mClickPoint;
412
413 bool mDepthBufferIsReady = false;
414 QImage mDepthBufferImage;
415 // -1 when unset
416 // TODO: Change to std::optional<double>
417 double mDepthBufferNonVoidAverage = -1;
418
419 std::unique_ptr<Qt3DRender::QCamera> mCameraBefore;
420
421 bool mRotationCenterCalculated = false;
422 QVector3D mRotationCenter;
423 double mRotationDistanceFromCenter = 0;
424 double mRotationPitch = 0;
425 double mRotationYaw = 0;
426
427 bool mDragPointCalculated = false;
428 QVector3D mDragPoint;
429 double mDragDepth;
430
431 bool mZoomPointCalculated = false;
432 QVector3D mZoomPoint;
433
434 // used for globe
435 QgsVector3D mMousePressViewCenter;
436 QgsCoordinateTransform mGlobeCrsToLatLon;
437
438 Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
439 Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
440 bool mInputHandlersEnabled = true;
443 double mCameraMovementSpeed = 5.0;
444
445 QSet<int> mDepressedKeys;
446 bool mCaptureFpsMouseMovements = false;
447 bool mIgnoreNextMouseMove = false;
448 QTimer *mFpsNavTimer = nullptr;
449
450 double mCumulatedWheelY = 0;
451
452 MouseOperation mCurrentOperation = MouseOperation::None;
453
454 // 3D world's origin in map coordinates
455 QgsVector3D mOrigin;
456
458 bool mCameraChanged = false;
459
460 // To test the cameracontroller
461 friend class TestQgs3DRendering;
462 friend class TestQgs3DCameraController;
463};
464
465#endif // QGSCAMERACONTROLLER_H
VerticalAxisInversion
Vertical axis inversion options for 3D views.
Definition qgis.h:4026
@ WhenDragging
Invert vertical axis movements when dragging in first person modes.
NavigationMode
The navigation mode used by 3D cameras.
Definition qgis.h:4001
@ TerrainBased
The default navigation based on the terrain.
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.
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.
Qgis::VerticalAxisInversion verticalAxisInversion() const
Returns the vertical axis inversion behavior.
float distance() const
Returns distance of the camera from the point it is looking at.
double cameraMovementSpeed() const
Returns the camera movement speed.
void cameraChanged()
Emitted when camera has been updated.
void cameraMovementSpeedChanged(double speed)
Emitted whenever the camera movement speed is changed by the controller.
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.
Class for doing transforms between two map coordinate systems.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
#define SIP_SKIP
Definition qgis_sip.h:126