QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgs3daxis.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgs3daxis.h
3  --------------------------------------
4  Date : March 2022
5  Copyright : (C) 2022 by Jean Felder
6  Email : jean dot felder at oslandia 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 QGS3DAXIS_H
17 #define QGS3DAXIS_H
18 
19 #include "qgis_3d.h"
20 
22 #include <Qt3DCore/QEntity>
23 #include <Qt3DExtras/Qt3DWindow>
24 #include <Qt3DExtras/QText2DEntity>
25 #include <Qt3DRender/QCamera>
26 #include <Qt3DRender/QViewport>
27 #include <Qt3DRender/QPickEvent>
28 #include <Qt3DRender/QScreenRayCaster>
29 #include <QVector3D>
30 #include <QVector2D>
31 
32 #include <Qt3DRender/QLayer>
33 #include <Qt3DRender/QBuffer>
34 #include <Qt3DRender/QGeometryRenderer>
35 
36 #include <QtWidgets/QMenu>
37 
38 #define SIP_NO_FILE
39 
41 class Qgs3DMapSettings;
42 class Qgs3DMapScene;
43 
56 class _3D_EXPORT Qgs3DAxis : public QObject
57 {
58  Q_OBJECT
59  public:
60 
69  Qgs3DAxis( Qt3DExtras::Qt3DWindow *parentWindow, Qt3DCore::QEntity *parent3DScene,
70  Qgs3DMapScene *mapScene, QgsCameraController *camera, Qgs3DMapSettings *map );
71  ~Qgs3DAxis() override;
72 
76  enum class Mode
77  {
78  Off = 1,
79  Crs = 2,
80  Cube = 3,
81  };
82  Q_ENUM( Mode )
83 
84 
87  Qgs3DAxis::Mode mode() { return mMode; }
88 
93  void setMode( Qgs3DAxis::Mode axisMode );
94 
101  void setAxisViewportPosition( int axisViewportSize, Qt::AnchorPoint axisViewportVertPos, Qt::AnchorPoint axisViewportHorizPos );
102 
106  int axisViewportSize() const { return mAxisViewportSize;}
107 
111  Qt::AnchorPoint axisViewportHorizontalPosition() const { return mAxisViewportHorizPos;}
112 
116  Qt::AnchorPoint axisViewportVerticalPosition() const { return mAxisViewportVertPos;}
117 
129  QVector3D from3dTo2dLabelPosition( const QVector3D &sourcePos,
130  Qt3DRender::QCamera *sourceCamera, Qt3DRender::QViewport *sourceViewport,
131  Qt3DRender::QCamera *destCamera, Qt3DRender::QViewport *destViewport,
132  const QSize &destSize );
133 
134  private slots:
135 
136  void onCameraUpdate( );
137  void onAxisViewportSizeUpdate( int val = 0 );
138 
139  // axis picking and menu
140  void onTouchedByRay( const Qt3DRender::QAbstractRayCaster::Hits &hits );
141  void onAxisModeChanged( Qgs3DAxis::Mode mode );
142  void onAxisHorizPositionChanged( Qt::AnchorPoint pos );
143  void onAxisVertPositionChanged( Qt::AnchorPoint pos );
144  void onCameraViewChange( float pitch, float yaw );
145 
146  void onCameraViewChangeHome( ) { onCameraViewChange( 45.0, 45.0 ); }
147  void onCameraViewChangeTop( ) {onCameraViewChange( 0.0, 90.0 );}
148  void onCameraViewChangeNorth( ) {onCameraViewChange( 90.0, 180.0 );}
149  void onCameraViewChangeEast( ) {onCameraViewChange( 90.0, 90.0 );}
150  void onCameraViewChangeSouth( ) {onCameraViewChange( 90.0, 0.0 );}
151  void onCameraViewChangeWest( ) {onCameraViewChange( 90.0, -90.0 );}
152  void onCameraViewChangeBottom() {onCameraViewChange( 180.0, 0.0 );}
153  void populateMenu();
154 
155  private:
156 
157  void createAxisScene();
158  void createAxis( Qt::Axis axis );
159  void createCube( );
160  void setEnableCube( bool show );
161  void setEnableAxis( bool show );
162  void updateAxisLabelPosition();
163 
164  Qt3DRender::QViewport *constructAxisViewport( Qt3DCore::QEntity *parent3DScene );
165  Qt3DRender::QViewport *constructLabelViewport( Qt3DCore::QEntity *parent3DScene, const QRectF &parentViewportSize );
166 
167  Qt3DExtras::QText2DEntity *addCubeText( const QString &text, float textHeight, float textWidth, const QFont &f, const QMatrix4x4 &rotation, const QVector3D &translation );
168 
169  // axis picking and menu
170  void init3DObjectPicking( );
171  bool eventFilter( QObject *watched, QEvent *event ) override;
172 
173  void hideMenu();
174  void displayMenuAt( const QPoint &position );
175 
176  Qgs3DMapSettings *mMapSettings = nullptr;
177  Qt3DExtras::Qt3DWindow *mParentWindow = nullptr;
178  Qgs3DMapScene *mMapScene = nullptr;
179  QgsCameraController *mCameraController = nullptr;
180 
181  float mCylinderLength = 40.0f;
182  int mAxisViewportSize = 4.0 * mCylinderLength;
183  Qt::AnchorPoint mAxisViewportVertPos = Qt::AnchorPoint::AnchorTop;
184  Qt::AnchorPoint mAxisViewportHorizPos = Qt::AnchorPoint::AnchorRight;
185  int mFontSize = 10;
186 
187  Qt3DCore::QEntity *mAxisSceneEntity = nullptr;
188  Qt3DRender::QLayer *mAxisSceneLayer = nullptr;
189  Qt3DRender::QCamera *mAxisCamera = nullptr;
190  Qt3DRender::QViewport *mAxisViewport = nullptr;
191 
192  Qgs3DAxis::Mode mMode = Mode::Crs;
193  Qt3DCore::QEntity *mAxisRoot = nullptr;
194  Qt3DCore::QEntity *mCubeRoot = nullptr;
195  QList<Qt3DExtras::QText2DEntity *> mCubeLabels;
196 
197  Qt3DExtras::QText2DEntity *mTextX = nullptr;
198  Qt3DExtras::QText2DEntity *mTextY = nullptr;
199  Qt3DExtras::QText2DEntity *mTextZ = nullptr;
200  QVector3D mTextCoordX;
201  QVector3D mTextCoordY;
202  QVector3D mTextCoordZ;
203  Qt3DCore::QTransform *mTextTransformX = nullptr;
204  Qt3DCore::QTransform *mTextTransformY = nullptr;
205  Qt3DCore::QTransform *mTextTransformZ = nullptr;
207  QVector3D mPreviousVector;
208 
209  Qt3DRender::QCamera *mTwoDLabelCamera = nullptr;
210  Qt3DCore::QEntity *mTwoDLabelSceneEntity = nullptr;
211  Qt3DRender::QViewport *mTwoDLabelViewport = nullptr;
212 
213  // axis picking and menu
214  Qt3DRender::QScreenRayCaster *mScreenRayCaster = nullptr;
215  QPoint mLastClickedPos;
216  Qt::MouseButton mLastClickedButton;
217  QCursor mPreviousCursor = Qt::ArrowCursor;
218  QMenu *mMenu = nullptr;
219 
220 };
221 
228 class Qgs3DWiredMesh : public Qt3DRender::QGeometryRenderer
229 {
230  Q_OBJECT
231 
232  public:
233 
237  Qgs3DWiredMesh( Qt3DCore::QNode *parent = nullptr );
238  ~Qgs3DWiredMesh() override;
239 
243  void setVertices( const QList<QVector3D> &vertices );
244 
245  private:
246  Qt3DRender::QGeometry *mGeom = nullptr;
247  Qt3DRender::QAttribute *mPositionAttribute = nullptr;
248  Qt3DRender::QBuffer *mVertexBuffer = nullptr;
249 };
250 
251 #endif // QGS3DAXIS_H
Qgs3DAxis::axisViewportHorizontalPosition
Qt::AnchorPoint axisViewportHorizontalPosition() const
Returns axis viewport horizontal position.
Definition: qgs3daxis.h:111
Qgs3DWiredMesh::setVertices
void setVertices(const QList< QVector3D > &vertices)
add or replace mesh vertices coordinates
Definition: qgs3daxis.cpp:1047
Qgs3DWiredMesh
Geometry renderer for lines, draws a wired mesh.
Definition: qgs3daxis.h:228
Qgs3DWiredMesh::Qgs3DWiredMesh
Qgs3DWiredMesh(Qt3DCore::QNode *parent=nullptr)
Defaul Qgs3DWiredMesh constructor.
Definition: qgs3daxis.cpp:1024
Qgs3DWiredMesh::~Qgs3DWiredMesh
~Qgs3DWiredMesh() override
Qgs3DAxis::axisViewportVerticalPosition
Qt::AnchorPoint axisViewportVerticalPosition() const
Returns axis viewport vertical position.
Definition: qgs3daxis.h:116
Qgs3DAxis
Display 3D ortho axis in the main 3D view.
Definition: qgs3daxis.h:56
Qgs3DMapSettings
Definition of the world.
Definition: qgs3dmapsettings.h:57
Qgs3DAxis::axisViewportSize
int axisViewportSize() const
Returns axis viewport size.
Definition: qgs3daxis.h:106
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
Qgs3DMapScene
Entity that encapsulates our 3D scene - contains all other entities (such as terrain) as children.
Definition: qgs3dmapscene.h:70
Qgs3DAxis::Mode
Mode
The Mode enum.
Definition: qgs3daxis.h:76
QgsCameraController
Object that controls camera movement based on user input.
Definition: qgscameracontroller.h:61
qgscoordinatereferencesystem.h