QGIS API Documentation 3.31.0-Master (d2b117a3c8)
qgs3daxis.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgs3daxis.cpp
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#include "qgs3daxis.h"
17
18#include <Qt3DCore/QTransform>
19#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
20#include <Qt3DRender/QAttribute>
21#include <Qt3DRender/QGeometry>
22typedef Qt3DRender::QAttribute Qt3DQAttribute;
23typedef Qt3DRender::QGeometry Qt3DQGeometry;
24typedef Qt3DRender::QBuffer Qt3DQBuffer;
25#else
26#include <Qt3DCore/QAttribute>
27#include <Qt3DCore/QGeometry>
28typedef Qt3DCore::QAttribute Qt3DQAttribute;
29typedef Qt3DCore::QGeometry Qt3DQGeometry;
30typedef Qt3DCore::QBuffer Qt3DQBuffer;
31#endif
32#include <Qt3DExtras/QCylinderMesh>
33#include <Qt3DExtras/QPhongMaterial>
34#include <Qt3DExtras/QConeMesh>
35#include <Qt3DRender/qcameralens.h>
36#include <Qt3DRender/QCameraSelector>
37#include <Qt3DRender/QClearBuffers>
38#include <Qt3DRender/QLayer>
39#include <Qt3DRender/QLayerFilter>
40#include <Qt3DRender/QPointLight>
41#include <QWidget>
42#include <QScreen>
43#include <QShortcut>
44#include <QFontDatabase>
45#include <ctime>
46#include <QApplication>
47#include <QActionGroup>
48
49#include "qgsmapsettings.h"
50#include "qgs3dmapscene.h"
51#include "qgsterrainentity_p.h"
54#include "qgswindow3dengine.h"
56
57Qgs3DAxis::Qgs3DAxis( Qt3DExtras::Qt3DWindow *parentWindow,
58 Qt3DCore::QEntity *parent3DScene,
59 Qgs3DMapScene *mapScene,
60 QgsCameraController *cameraCtrl,
61 Qgs3DMapSettings *map )
62 : QObject( parentWindow )
63 , mMapSettings( map )
64 , mParentWindow( parentWindow )
65 , mMapScene( mapScene )
66 , mCameraController( cameraCtrl )
67 , mCrs( map->crs() )
68{
69 mAxisViewport = constructAxisViewport( parent3DScene );
70 mAxisViewport->setParent( mParentWindow->activeFrameGraph() );
71
72 mTwoDLabelViewport = constructLabelViewport( parent3DScene, QRectF( 0.0f, 0.0f, 1.0f, 1.0f ) );
73 mTwoDLabelViewport->setParent( mParentWindow->activeFrameGraph() );
74
75 connect( cameraCtrl, &QgsCameraController::cameraChanged, this, &Qgs3DAxis::onCameraUpdate );
76 connect( mParentWindow, &Qt3DExtras::Qt3DWindow::widthChanged, this, &Qgs3DAxis::onAxisViewportSizeUpdate );
77 connect( mParentWindow, &Qt3DExtras::Qt3DWindow::heightChanged, this, &Qgs3DAxis::onAxisViewportSizeUpdate );
78
79 createAxisScene();
80 onAxisViewportSizeUpdate();
81
82 init3DObjectPicking();
83
84 createKeyboardShortCut();
85}
86
88{
89 delete mMenu;
90 mMenu = nullptr;
91}
92
93void Qgs3DAxis::init3DObjectPicking( )
94{
95 // Create screencaster to be used by EventFilter:
96 // 1- Perform ray casting tests by specifying "touch" coordinates in screen space
97 // 2- connect screencaster results to onTouchedByRay
98 // 3- screencaster will be triggered by EventFilter
99 mScreenRayCaster = new Qt3DRender::QScreenRayCaster( mAxisSceneEntity );
100 mScreenRayCaster->addLayer( mAxisSceneLayer ); // to only filter on axis objects
101 mScreenRayCaster->setFilterMode( Qt3DRender::QScreenRayCaster::AcceptAllMatchingLayers );
102 mScreenRayCaster->setRunMode( Qt3DRender::QAbstractRayCaster::SingleShot );
103
104 mAxisSceneEntity->addComponent( mScreenRayCaster );
105
106 QObject::connect( mScreenRayCaster, &Qt3DRender::QScreenRayCaster::hitsChanged, this, &Qgs3DAxis::onTouchedByRay );
107
108 // we need event filter (see Qgs3DAxis::eventFilter) to handle the mouse click event as this event is not catchable via the Qt3DRender::QObjectPicker
109 mParentWindow->installEventFilter( this );
110}
111
112bool Qgs3DAxis::eventFilter( QObject *watched, QEvent *event )
113{
114 if ( watched != mParentWindow )
115 return false;
116
117 if ( event->type() == QEvent::MouseButtonPress )
118 {
119 // register mouse click to detect dragging
120 mHasClicked = true;
121 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>( event );
122 mLastClickedPos = mouseEvent->pos();
123 }
124
125 // handle QEvent::MouseButtonRelease as it represents the end of click and QEvent::MouseMove.
126 else if ( event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::MouseMove )
127 {
128 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>( event );
129
130 // user has clicked and move ==> dragging start
131 if ( event->type() == QEvent::MouseMove &&
132 ( ( mHasClicked && ( mouseEvent->pos() - mLastClickedPos ).manhattanLength() < QApplication::startDragDistance() ) || mIsDragging ) )
133 {
134 mIsDragging = true;
135 }
136
137 // user has released ==> dragging ends
138 else if ( mIsDragging && event->type() == QEvent::MouseButtonRelease )
139 {
140 mIsDragging = false;
141 mHasClicked = false;
142 }
143
144 // user is moving or has released but not dragging
145 else if ( ! mIsDragging )
146 {
147 // limit ray caster usage to the axis viewport
148 QPointF normalizedPos( static_cast<float>( mouseEvent->pos().x() ) / mParentWindow->width(),
149 ( float )mouseEvent->pos().y() / mParentWindow->height() );
150
151 if ( 2 <= QgsLogger::debugLevel() && event->type() == QEvent::MouseButtonRelease )
152 {
153 std::ostringstream os;
154 os << "QGS3DAxis: normalized pos: " << normalizedPos << " / viewport: " << mAxisViewport->normalizedRect();
155 QgsDebugMsgLevel( os.str().c_str(), 2 );
156 }
157
158 if ( mAxisViewport->normalizedRect().contains( normalizedPos ) )
159 {
160 mLastClickedButton = mouseEvent->button();
161 mLastClickedPos = mouseEvent->pos();
162
163 // if casted ray from pos matches an entity, call onTouchedByRay
164 mScreenRayCaster->trigger( mLastClickedPos );
165 }
166
167 // when we exit the viewport, reset the mouse cursor if needed
168 else if ( mPreviousCursor != Qt::ArrowCursor && mParentWindow->cursor() == Qt::ArrowCursor )
169 {
170 mParentWindow->setCursor( mPreviousCursor );
171 mPreviousCursor = Qt::ArrowCursor;
172 }
173
174 mIsDragging = false; // drag ends
175 mHasClicked = false;
176 }
177 }
178
179 return false;
180}
181
182void Qgs3DAxis::onTouchedByRay( const Qt3DRender::QAbstractRayCaster::Hits &hits )
183{
184 int mHitsFound = -1;
185 if ( !hits.empty() )
186 {
187 if ( 2 <= QgsLogger::debugLevel() )
188 {
189 std::ostringstream os;
190 os << "Qgs3DAxis::onTouchedByRay " << hits.length() << " hits at pos " << mLastClickedPos << " with QButton: " << mLastClickedButton;
191 for ( int i = 0; i < hits.length(); ++i )
192 {
193 os << "\n";
194 os << "\tHit Type: " << hits.at( i ).type() << "\n";
195 os << "\tHit triangle id: " << hits.at( i ).primitiveIndex() << "\n";
196 os << "\tHit distance: " << hits.at( i ).distance() << "\n";
197 os << "\tHit entity name: " << hits.at( i ).entity()->objectName().toStdString();
198 }
199 QgsDebugMsgLevel( os.str().c_str(), 2 );
200 }
201
202 for ( int i = 0; i < hits.length() && mHitsFound == -1; ++i )
203 {
204 if ( hits.at( i ).distance() < 500.0f && ( hits.at( i ).entity() == mCubeRoot || hits.at( i ).entity() == mAxisRoot || hits.at( i ).entity()->parent() == mCubeRoot || hits.at( i ).entity()->parent() == mAxisRoot ) )
205 {
206 mHitsFound = i;
207 }
208 }
209 }
210
211 if ( mLastClickedButton == Qt::NoButton ) // hover
212 {
213 if ( mHitsFound != -1 )
214 {
215 if ( mParentWindow->cursor() != Qt::ArrowCursor )
216 {
217 mPreviousCursor = mParentWindow->cursor();
218 mParentWindow->setCursor( Qt::ArrowCursor );
219 QgsDebugMsgLevel( "Enabling arrow cursor", 2 );
220 }
221 }
222 }
223 else if ( mLastClickedButton == Qt::MouseButton::RightButton && mHitsFound != -1 ) // show menu
224 {
225 displayMenuAt( mLastClickedPos );
226 }
227 else if ( mLastClickedButton == Qt::MouseButton::LeftButton ) // handle cube face clicks
228 {
229 hideMenu();
230
231 if ( mHitsFound != -1 )
232 {
233 if ( hits.at( mHitsFound ).entity() == mCubeRoot || hits.at( mHitsFound ).entity()->parent() == mCubeRoot )
234 {
235 switch ( hits.at( mHitsFound ).primitiveIndex() / 2 )
236 {
237 case 0: // "East face";
238 QgsDebugMsgLevel( "Qgs3DAxis: East face clicked", 2 );
239 onCameraViewChangeEast();
240 break;
241
242 case 1: // "West face ";
243 QgsDebugMsgLevel( "Qgs3DAxis: West face clicked", 2 );
244 onCameraViewChangeWest();
245 break;
246
247 case 2: // "North face ";
248 QgsDebugMsgLevel( "Qgs3DAxis: North face clicked", 2 );
249 onCameraViewChangeNorth();
250 break;
251
252 case 3: // "South face";
253 QgsDebugMsgLevel( "Qgs3DAxis: South face clicked", 2 );
254 onCameraViewChangeSouth();
255 break;
256
257 case 4: // "Top face ";
258 QgsDebugMsgLevel( "Qgs3DAxis: Top face clicked", 2 );
259 onCameraViewChangeTop();
260 break;
261
262 case 5: // "Bottom face ";
263 QgsDebugMsgLevel( "Qgs3DAxis: Bottom face clicked", 2 );
264 onCameraViewChangeBottom();
265 break;
266
267 default:
268 break;
269 }
270 }
271 }
272 }
273}
274
275Qt3DRender::QViewport *Qgs3DAxis::constructAxisViewport( Qt3DCore::QEntity *parent3DScene )
276{
277 Qt3DRender::QViewport *axisViewport = new Qt3DRender::QViewport;
278 // parent will be set later
279 // size will be set later
280
281 mAxisSceneEntity = new Qt3DCore::QEntity;
282 mAxisSceneEntity->setParent( parent3DScene );
283 mAxisSceneEntity->setObjectName( "3DAxis_SceneEntity" );
284
285 mAxisSceneLayer = new Qt3DRender::QLayer;
286 mAxisSceneLayer->setObjectName( "3DAxis_SceneLayer" );
287 mAxisSceneLayer->setParent( mAxisSceneEntity );
288 mAxisSceneLayer->setRecursive( true );
289
290 mAxisCamera = new Qt3DRender::QCamera;
291 mAxisCamera->setParent( mAxisSceneEntity );
292 mAxisCamera->setProjectionType( mCameraController->camera()->projectionType() );
293 mAxisCamera->lens()->setFieldOfView( mCameraController->camera()->lens()->fieldOfView() * 0.5f );
294
295 mAxisCamera->setUpVector( QVector3D( 0.0f, 0.0f, 1.0f ) );
296 mAxisCamera->setViewCenter( QVector3D( 0.0f, 0.0f, 0.0f ) );
297 // position will be set later
298
299 Qt3DRender::QLayer *axisLayer = new Qt3DRender::QLayer;
300 axisLayer->setRecursive( true );
301 mAxisSceneEntity->addComponent( axisLayer );
302
303 Qt3DRender::QLayerFilter *axisLayerFilter = new Qt3DRender::QLayerFilter( axisViewport );
304 axisLayerFilter->addLayer( axisLayer );
305
306 Qt3DRender::QCameraSelector *axisCameraSelector = new Qt3DRender::QCameraSelector;
307 axisCameraSelector->setParent( axisLayerFilter );
308 axisCameraSelector->setCamera( mAxisCamera );
309
310 Qt3DRender::QClearBuffers *clearBuffers = new Qt3DRender::QClearBuffers( axisCameraSelector );
311 clearBuffers->setBuffers( Qt3DRender::QClearBuffers::DepthBuffer );
312
313 // cppcheck-suppress memleak
314 return axisViewport;
315}
316
317Qt3DRender::QViewport *Qgs3DAxis::constructLabelViewport( Qt3DCore::QEntity *parent3DScene, const QRectF &parentViewportSize )
318{
319 Qt3DRender::QViewport *twoDViewport = new Qt3DRender::QViewport;
320 // parent will be set later
321 twoDViewport->setNormalizedRect( parentViewportSize );
322
323 mTwoDLabelSceneEntity = new Qt3DCore::QEntity;
324 mTwoDLabelSceneEntity->setParent( parent3DScene );
325 mTwoDLabelSceneEntity->setEnabled( true );
326
327 mTwoDLabelCamera = new Qt3DRender::QCamera;
328 mTwoDLabelCamera->setParent( mTwoDLabelSceneEntity );
329 mTwoDLabelCamera->setProjectionType( Qt3DRender::QCameraLens::ProjectionType::OrthographicProjection );
330 mTwoDLabelCamera->lens()->setOrthographicProjection(
331 -mParentWindow->width() / 2.0f, mParentWindow->width() / 2.0f,
332 -mParentWindow->height() / 2.0f, mParentWindow->height() / 2.0f,
333 -10.0f, 100.0f );
334
335 mTwoDLabelCamera->setUpVector( QVector3D( 0.0f, 0.0f, 1.0f ) );
336 mTwoDLabelCamera->setViewCenter( QVector3D( 0.0f, 0.0f, 0.0f ) );
337
338 mTwoDLabelCamera->setPosition( QVector3D( 0.0f, 0.0f, 100.0f ) );
339
340 Qt3DRender::QLayer *twoDLayer = new Qt3DRender::QLayer;
341 twoDLayer->setRecursive( true );
342 mTwoDLabelSceneEntity->addComponent( twoDLayer );
343
344 Qt3DRender::QLayerFilter *twoDLayerFilter = new Qt3DRender::QLayerFilter( twoDViewport );
345 twoDLayerFilter->addLayer( twoDLayer );
346
347 Qt3DRender::QCameraSelector *twoDCameraSelector = new Qt3DRender::QCameraSelector;
348 twoDCameraSelector->setParent( twoDLayerFilter );
349 twoDCameraSelector->setCamera( mTwoDLabelCamera );
350
351 Qt3DRender::QClearBuffers *clearBuffers = new Qt3DRender::QClearBuffers( twoDCameraSelector );
352 clearBuffers->setBuffers( Qt3DRender::QClearBuffers::DepthBuffer );
353
354 // cppcheck-suppress memleak
355 return twoDViewport;
356}
357
358QVector3D Qgs3DAxis::from3DTo2DLabelPosition( const QVector3D &sourcePos,
359 Qt3DRender::QCamera *sourceCamera, Qt3DRender::QViewport *sourceViewport,
360 Qt3DRender::QCamera *destCamera, Qt3DRender::QViewport *destViewport,
361 const QSize &destSize )
362{
363 QVector3D destPos = sourcePos.project( sourceCamera->viewMatrix(),
364 destCamera->projectionMatrix(),
365 QRect( 0.0f, 0.0f,
366 destViewport->normalizedRect().width() * destSize.width(),
367 destViewport->normalizedRect().height() * destSize.height() ) );
368 QPointF axisCenter = sourceViewport->normalizedRect().center();
369 QPointF labelCenter = destViewport->normalizedRect().center();
370 QVector3D viewTranslation = QVector3D( ( axisCenter - labelCenter ).x() * destSize.width(),
371 ( axisCenter - labelCenter ).y() * destSize.height(),
372 0.0f );
373 destPos -= QVector3D( labelCenter.x() * destSize.width(),
374 labelCenter.y() * destSize.height(),
375 0.0f );
376 destPos.setX( destPos.x() + viewTranslation.x() );
377 destPos.setY( destPos.y() - viewTranslation.y() );
378 destPos.setZ( 0.0f );
379
380 if ( 2 <= QgsLogger::debugLevel() )
381 {
382 std::ostringstream os;
383 os << "Qgs3DAxis::from3DTo2DLabelPosition: sourcePos: " << sourcePos.toPoint()
384 << " with translation: " << viewTranslation.toPoint()
385 << " corrected to pos: " << destPos.toPoint();
386 QgsDebugMsgLevel( os.str().c_str(), 2 );
387 }
388 return destPos;
389}
390
391void Qgs3DAxis::setEnableCube( bool show )
392{
393 mCubeRoot->setEnabled( show );
394 if ( show )
395 {
396 mCubeRoot->setParent( mAxisSceneEntity );
397 }
398 else
399 {
400 mCubeRoot->setParent( static_cast<Qt3DCore::QEntity *>( nullptr ) );
401 }
402}
403
404void Qgs3DAxis::setEnableAxis( bool show )
405{
406 mAxisRoot->setEnabled( show );
407 if ( show )
408 {
409 mAxisRoot->setParent( mAxisSceneEntity );
410 }
411 else
412 {
413 mAxisRoot->setParent( static_cast<Qt3DCore::QEntity *>( nullptr ) );
414 }
415
416 mTextX->setEnabled( show );
417 mTextY->setEnabled( show );
418 mTextZ->setEnabled( show );
419}
420
421void Qgs3DAxis::createAxisScene()
422{
423 if ( mAxisRoot == nullptr || mCubeRoot == nullptr )
424 {
425 mAxisRoot = new Qt3DCore::QEntity;
426 mAxisRoot->setParent( mAxisSceneEntity );
427 mAxisRoot->setObjectName( "3DAxis_AxisRoot" );
428 mAxisRoot->addComponent( mAxisSceneLayer ); // raycaster will filter object containing this layer
429
430 createAxis( Qt::Axis::XAxis );
431 createAxis( Qt::Axis::YAxis );
432 createAxis( Qt::Axis::ZAxis );
433
434 mCubeRoot = new Qt3DCore::QEntity;
435 mCubeRoot->setParent( mAxisSceneEntity );
436 mCubeRoot->setObjectName( "3DAxis_CubeRoot" );
437 mCubeRoot->addComponent( mAxisSceneLayer ); // raycaster will filter object containing this layer
438
439 createCube( );
440 }
441
442 Qgs3DAxisSettings::Mode mode = mMapSettings->get3DAxisSettings().mode();
443
444 if ( mode == Qgs3DAxisSettings::Mode::Off )
445 {
446 mAxisSceneEntity->setEnabled( false );
447 setEnableAxis( false );
448 setEnableCube( false );
449 }
450 else
451 {
452 mAxisSceneEntity->setEnabled( true );
453 if ( mode == Qgs3DAxisSettings::Mode::Crs )
454 {
455 setEnableCube( false );
456 setEnableAxis( true );
457
458 const QList< Qgis::CrsAxisDirection > axisDirections = mCrs.axisOrdering();
459
460 if ( axisDirections.length() > 0 )
461 mTextX->setText( QgsCoordinateReferenceSystemUtils::axisDirectionToAbbreviatedString( axisDirections.at( 0 ) ) );
462 else
463 mTextY->setText( "X?" );
464
465 if ( axisDirections.length() > 1 )
466 mTextY->setText( QgsCoordinateReferenceSystemUtils::axisDirectionToAbbreviatedString( axisDirections.at( 1 ) ) );
467 else
468 mTextY->setText( "Y?" );
469
470 if ( axisDirections.length() > 2 )
471 mTextZ->setText( QgsCoordinateReferenceSystemUtils::axisDirectionToAbbreviatedString( axisDirections.at( 2 ) ) );
472 else
473 mTextZ->setText( QStringLiteral( "up" ) );
474 }
475 else if ( mode == Qgs3DAxisSettings::Mode::Cube )
476 {
477 setEnableCube( true );
478 setEnableAxis( false );
479 }
480 else
481 {
482 setEnableCube( false );
483 setEnableAxis( true );
484 mTextX->setText( "X?" );
485 mTextY->setText( "Y?" );
486 mTextZ->setText( "Z?" );
487 }
488
489 updateAxisLabelPosition();
490 }
491}
492
493void Qgs3DAxis::createKeyboardShortCut()
494{
495 QgsWindow3DEngine *eng = dynamic_cast<QgsWindow3DEngine *>( mMapScene->engine() );
496 if ( eng )
497 {
498 QWidget *mapCanvas = dynamic_cast<QWidget *>( eng->parent() );
499 if ( mapCanvas == nullptr )
500 {
501 QgsLogger::warning( "Qgs3DAxis: no canvas defined!" );
502 }
503 else
504 {
505 QShortcut *shortcutHome = new QShortcut( QKeySequence( Qt::CTRL + Qt::Key_1 ), mapCanvas );
506 connect( shortcutHome, &QShortcut::activated, this, [this]( ) {onCameraViewChangeHome();} );
507
508 QShortcut *shortcutTop = new QShortcut( QKeySequence( Qt::CTRL + Qt::Key_5 ), mapCanvas );
509 connect( shortcutTop, &QShortcut::activated, this, [this]( ) {onCameraViewChangeTop();} );
510
511 QShortcut *shortcutNorth = new QShortcut( QKeySequence( Qt::CTRL + Qt::Key_8 ), mapCanvas );
512 connect( shortcutNorth, &QShortcut::activated, this, [this]( ) {onCameraViewChangeNorth();} );
513
514 QShortcut *shortcutEast = new QShortcut( QKeySequence( Qt::CTRL + Qt::Key_6 ), mapCanvas );
515 connect( shortcutEast, &QShortcut::activated, this, [this]( ) {onCameraViewChangeEast();} );
516
517 QShortcut *shortcutSouth = new QShortcut( QKeySequence( Qt::CTRL + Qt::Key_2 ), mapCanvas );
518 connect( shortcutSouth, &QShortcut::activated, this, [this]( ) {onCameraViewChangeSouth();} );
519
520 QShortcut *shortcutWest = new QShortcut( QKeySequence( Qt::CTRL + Qt::Key_4 ), mapCanvas );
521 connect( shortcutWest, &QShortcut::activated, this, [this]( ) {onCameraViewChangeWest();} );
522 }
523 }
524}
525
526void Qgs3DAxis::createMenu()
527{
528 mMenu = new QMenu();
529
530 // axis type menu
531 QAction *typeOffAct = new QAction( tr( "&Off" ), mMenu );
532 typeOffAct->setCheckable( true );
533 typeOffAct->setStatusTip( tr( "Disable 3D axis" ) );
534 connect( mMapSettings, &Qgs3DMapSettings::axisSettingsChanged, this, [typeOffAct, this]()
535 {
536 if ( mMapSettings->get3DAxisSettings().mode() == Qgs3DAxisSettings::Mode::Off )
537 typeOffAct->setChecked( true );
538 } );
539
540 QAction *typeCrsAct = new QAction( tr( "Coordinate Reference &System" ), mMenu );
541 typeCrsAct->setCheckable( true );
542 typeCrsAct->setStatusTip( tr( "Coordinate Reference System 3D axis" ) );
543 connect( mMapSettings, &Qgs3DMapSettings::axisSettingsChanged, this, [typeCrsAct, this]()
544 {
545 if ( mMapSettings->get3DAxisSettings().mode() == Qgs3DAxisSettings::Mode::Crs )
546 typeCrsAct->setChecked( true );
547 } );
548
549 QAction *typeCubeAct = new QAction( tr( "&Cube" ), mMenu );
550 typeCubeAct->setCheckable( true );
551 typeCubeAct->setStatusTip( tr( "Cube 3D axis" ) );
552 connect( mMapSettings, &Qgs3DMapSettings::axisSettingsChanged, this, [typeCubeAct, this]()
553 {
554 if ( mMapSettings->get3DAxisSettings().mode() == Qgs3DAxisSettings::Mode::Cube )
555 typeCubeAct->setChecked( true );
556 } );
557
558 QActionGroup *typeGroup = new QActionGroup( mMenu );
559 typeGroup->addAction( typeOffAct );
560 typeGroup->addAction( typeCrsAct );
561 typeGroup->addAction( typeCubeAct );
562
563 connect( typeOffAct, &QAction::triggered, this, [this]( bool ) {onAxisModeChanged( Qgs3DAxisSettings::Mode::Off );} );
564 connect( typeCrsAct, &QAction::triggered, this, [this]( bool ) {onAxisModeChanged( Qgs3DAxisSettings::Mode::Crs );} );
565 connect( typeCubeAct, &QAction::triggered, this, [this]( bool ) {onAxisModeChanged( Qgs3DAxisSettings::Mode::Cube );} );
566
567 QMenu *typeMenu = new QMenu( QStringLiteral( "Axis Type" ), mMenu );
568 Q_ASSERT( typeMenu );
569 typeMenu->addAction( typeOffAct );
570 typeMenu->addAction( typeCrsAct );
571 typeMenu->addAction( typeCubeAct );
572 mMenu->addMenu( typeMenu );
573
574 // horizontal position menu
575 QAction *hPosLeftAct = new QAction( tr( "&Left" ), mMenu );
576 hPosLeftAct->setCheckable( true );
577 connect( mMapSettings, &Qgs3DMapSettings::axisSettingsChanged, this, [hPosLeftAct, this]()
578 {
579 if ( mMapSettings->get3DAxisSettings().horizontalPosition() == Qt::AnchorPoint::AnchorLeft )
580 hPosLeftAct->setChecked( true );
581 } );
582
583 QAction *hPosMiddleAct = new QAction( tr( "&Center" ), mMenu );
584 hPosMiddleAct->setCheckable( true );
585 connect( mMapSettings, &Qgs3DMapSettings::axisSettingsChanged, this, [hPosMiddleAct, this]()
586 {
587 if ( mMapSettings->get3DAxisSettings().horizontalPosition() == Qt::AnchorPoint::AnchorHorizontalCenter )
588 hPosMiddleAct->setChecked( true );
589 } );
590
591 QAction *hPosRightAct = new QAction( tr( "&Right" ), mMenu );
592 hPosRightAct->setCheckable( true );
593 connect( mMapSettings, &Qgs3DMapSettings::axisSettingsChanged, this, [hPosRightAct, this]()
594 {
595 if ( mMapSettings->get3DAxisSettings().horizontalPosition() == Qt::AnchorPoint::AnchorRight )
596 hPosRightAct->setChecked( true );
597 } );
598
599 QActionGroup *hPosGroup = new QActionGroup( mMenu );
600 hPosGroup->addAction( hPosLeftAct );
601 hPosGroup->addAction( hPosMiddleAct );
602 hPosGroup->addAction( hPosRightAct );
603
604 connect( hPosLeftAct, &QAction::triggered, this, [this]( bool ) {onAxisHorizPositionChanged( Qt::AnchorPoint::AnchorLeft );} );
605 connect( hPosMiddleAct, &QAction::triggered, this, [this]( bool ) {onAxisHorizPositionChanged( Qt::AnchorPoint::AnchorHorizontalCenter );} );
606 connect( hPosRightAct, &QAction::triggered, this, [this]( bool ) {onAxisHorizPositionChanged( Qt::AnchorPoint::AnchorRight );} );
607
608 QMenu *horizPosMenu = new QMenu( QStringLiteral( "Horizontal Position" ), mMenu );
609 horizPosMenu->addAction( hPosLeftAct );
610 horizPosMenu->addAction( hPosMiddleAct );
611 horizPosMenu->addAction( hPosRightAct );
612 mMenu->addMenu( horizPosMenu );
613
614 // vertical position menu
615 QAction *vPosTopAct = new QAction( tr( "&Top" ), mMenu );
616 vPosTopAct->setCheckable( true );
617 connect( mMapSettings, &Qgs3DMapSettings::axisSettingsChanged, this, [vPosTopAct, this]()
618 {
619 if ( mMapSettings->get3DAxisSettings().verticalPosition() == Qt::AnchorPoint::AnchorTop )
620 vPosTopAct->setChecked( true );
621 } );
622
623 QAction *vPosMiddleAct = new QAction( tr( "&Middle" ), mMenu );
624 vPosMiddleAct->setCheckable( true );
625 connect( mMapSettings, &Qgs3DMapSettings::axisSettingsChanged, this, [vPosMiddleAct, this]()
626 {
627 if ( mMapSettings->get3DAxisSettings().verticalPosition() == Qt::AnchorPoint::AnchorVerticalCenter )
628 vPosMiddleAct->setChecked( true );
629 } );
630
631 QAction *vPosBottomAct = new QAction( tr( "&Bottom" ), mMenu );
632 vPosBottomAct->setCheckable( true );
633 connect( mMapSettings, &Qgs3DMapSettings::axisSettingsChanged, this, [vPosBottomAct, this]()
634 {
635 if ( mMapSettings->get3DAxisSettings().verticalPosition() == Qt::AnchorPoint::AnchorBottom )
636 vPosBottomAct->setChecked( true );
637 } );
638
639 QActionGroup *vPosGroup = new QActionGroup( mMenu );
640 vPosGroup->addAction( vPosTopAct );
641 vPosGroup->addAction( vPosMiddleAct );
642 vPosGroup->addAction( vPosBottomAct );
643
644 connect( vPosTopAct, &QAction::triggered, this, [this]( bool ) {onAxisVertPositionChanged( Qt::AnchorPoint::AnchorTop );} );
645 connect( vPosMiddleAct, &QAction::triggered, this, [this]( bool ) {onAxisVertPositionChanged( Qt::AnchorPoint::AnchorVerticalCenter );} );
646 connect( vPosBottomAct, &QAction::triggered, this, [this]( bool ) {onAxisVertPositionChanged( Qt::AnchorPoint::AnchorBottom );} );
647
648 QMenu *vertPosMenu = new QMenu( QStringLiteral( "Vertical Position" ), mMenu );
649 vertPosMenu->addAction( vPosTopAct );
650 vertPosMenu->addAction( vPosMiddleAct );
651 vertPosMenu->addAction( vPosBottomAct );
652 mMenu->addMenu( vertPosMenu );
653
654 // axis view menu
655 QAction *viewHomeAct = new QAction( tr( "&Home" ) + "\t Ctrl+1", mMenu );
656 QAction *viewTopAct = new QAction( tr( "&Top" ) + "\t Ctrl+5", mMenu );
657 QAction *viewNorthAct = new QAction( tr( "&North" ) + "\t Ctrl+8", mMenu );
658 QAction *viewEastAct = new QAction( tr( "&East" ) + "\t Ctrl+6", mMenu );
659 QAction *viewSouthAct = new QAction( tr( "&South" ) + "\t Ctrl+2", mMenu );
660 QAction *viewWestAct = new QAction( tr( "&West" ) + "\t Ctrl+4", mMenu );
661 QAction *viewBottomAct = new QAction( tr( "&Bottom" ), mMenu );
662
663 connect( viewHomeAct, &QAction::triggered, this, &Qgs3DAxis::onCameraViewChangeHome );
664 connect( viewTopAct, &QAction::triggered, this, &Qgs3DAxis::onCameraViewChangeTop );
665 connect( viewNorthAct, &QAction::triggered, this, &Qgs3DAxis::onCameraViewChangeNorth );
666 connect( viewEastAct, &QAction::triggered, this, &Qgs3DAxis::onCameraViewChangeEast );
667 connect( viewSouthAct, &QAction::triggered, this, &Qgs3DAxis::onCameraViewChangeSouth );
668 connect( viewWestAct, &QAction::triggered, this, &Qgs3DAxis::onCameraViewChangeWest );
669 connect( viewBottomAct, &QAction::triggered, this, &Qgs3DAxis::onCameraViewChangeBottom );
670
671 QMenu *viewMenu = new QMenu( QStringLiteral( "Camera View" ), mMenu );
672 viewMenu->addAction( viewHomeAct );
673 viewMenu->addAction( viewTopAct );
674 viewMenu->addAction( viewNorthAct );
675 viewMenu->addAction( viewEastAct );
676 viewMenu->addAction( viewSouthAct );
677 viewMenu->addAction( viewWestAct );
678 viewMenu->addAction( viewBottomAct );
679 mMenu->addMenu( viewMenu );
680
681 // update checkable items
682 mMapSettings->set3DAxisSettings( mMapSettings->get3DAxisSettings(), true );
683}
684
685void Qgs3DAxis::hideMenu()
686{
687 if ( mMenu && mMenu->isVisible() )
688 mMenu->hide();
689}
690
691void Qgs3DAxis::displayMenuAt( const QPoint &sourcePos )
692{
693 if ( mMenu == nullptr )
694 {
695 createMenu();
696 }
697 QObject *threeDMapCanvasWidget = mMapScene->engine() // ie. 3DEngine
698 ->parent() // ie. Qgs3DMapCanvas
699 ->parent(); // ie. Qgs3DMapCanvasWidget
700
701 QWidget *container = dynamic_cast<QWidget * >( threeDMapCanvasWidget->parent() );
702 if ( container )
703 mMenu->popup( container->mapToGlobal( sourcePos ) );
704 else
705 mMenu->popup( mParentWindow->parent()->mapToGlobal( sourcePos ) );
706}
707
708void Qgs3DAxis::onAxisModeChanged( Qgs3DAxisSettings::Mode mode )
709{
710 Qgs3DAxisSettings s = mMapSettings->get3DAxisSettings();
711 s.setMode( mode );
712 mMapSettings->set3DAxisSettings( s );
713}
714
715void Qgs3DAxis::onAxisHorizPositionChanged( Qt::AnchorPoint pos )
716{
717 Qgs3DAxisSettings s = mMapSettings->get3DAxisSettings();
718 s.setHorizontalPosition( pos );
719 mMapSettings->set3DAxisSettings( s );
720}
721
722void Qgs3DAxis::onAxisVertPositionChanged( Qt::AnchorPoint pos )
723{
724 Qgs3DAxisSettings s = mMapSettings->get3DAxisSettings();
725 s.setVerticalPosition( pos );
726 mMapSettings->set3DAxisSettings( s );
727}
728
729void Qgs3DAxis::onCameraViewChange( float pitch, float yaw )
730{
731 QgsVector3D pos = mCameraController->lookingAtPoint();
732 double elevation = 0.0;
733 if ( mMapSettings->terrainRenderingEnabled() )
734 {
735 QgsDebugMsgLevel( "Checking elevation from terrain...", 2 );
736 QVector3D camPos = mCameraController->camera()->position();
737 QgsRayCastingUtils::Ray3D ray( camPos, pos.toVector3D() - camPos, mCameraController->camera()->farPlane() );
738 const QVector<QgsRayCastingUtils::RayHit> hits = mMapScene->terrainEntity()->rayIntersection( ray, QgsRayCastingUtils::RayCastContext() );
739 if ( !hits.isEmpty() )
740 {
741 elevation = hits.at( 0 ).pos.y();
742 QgsDebugMsgLevel( QString( "Computed elevation from terrain: %1" ).arg( elevation ), 2 );
743 }
744 else
745 {
746 QgsDebugMsgLevel( "Unable to obtain elevation from terrain", 2 );
747 }
748 }
749 pos.set( pos.x(), elevation + mMapSettings->terrainElevationOffset(), pos.z() );
750
751 mCameraController->setLookingAtPoint( pos, ( mCameraController->camera()->position() - pos.toVector3D() ).length(),
752 pitch, yaw );
753}
754
755
756void Qgs3DAxis::createCube( )
757{
758 QVector3D minPos = QVector3D( -mCylinderLength * 0.5f, -mCylinderLength * 0.5f, -mCylinderLength * 0.5f );
759
760 // cube outlines
761 Qt3DCore::QEntity *cubeLineEntity = new Qt3DCore::QEntity( mCubeRoot );
762 cubeLineEntity->setObjectName( "3DAxis_cubeline" );
763 Qgs3DWiredMesh *cubeLine = new Qgs3DWiredMesh;
764 QgsAABB box = QgsAABB( -mCylinderLength * 0.5f, -mCylinderLength * 0.5f, -mCylinderLength * 0.5f,
765 mCylinderLength * 0.5f, mCylinderLength * 0.5f, mCylinderLength * 0.5f );
766 cubeLine->setVertices( box.verticesForLines() );
767 cubeLineEntity->addComponent( cubeLine );
768
769 Qt3DExtras::QPhongMaterial *cubeLineMaterial = new Qt3DExtras::QPhongMaterial;
770 cubeLineMaterial->setAmbient( Qt::white );
771 cubeLineEntity->addComponent( cubeLineMaterial );
772
773 // cube mesh
774 Qt3DExtras::QCuboidMesh *cubeMesh = new Qt3DExtras::QCuboidMesh;
775 cubeMesh->setObjectName( "3DAxis_cubemesh" );
776 cubeMesh->setXExtent( mCylinderLength );
777 cubeMesh->setYExtent( mCylinderLength );
778 cubeMesh->setZExtent( mCylinderLength );
779 mCubeRoot->addComponent( cubeMesh );
780
781 Qt3DExtras::QPhongMaterial *cubeMaterial = new Qt3DExtras::QPhongMaterial( mCubeRoot );
782 cubeMaterial->setAmbient( QColor( 100, 100, 100, 50 ) );
783 cubeMaterial->setShininess( 100 );
784 mCubeRoot->addComponent( cubeMaterial );
785
786 Qt3DCore::QTransform *cubeTransform = new Qt3DCore::QTransform;
787 QMatrix4x4 transformMatrixcube;
788 //transformMatrixcube.rotate( rotation );
789 transformMatrixcube.translate( minPos + QVector3D( mCylinderLength * 0.5f, mCylinderLength * 0.5f, mCylinderLength * 0.5f ) );
790 cubeTransform->setMatrix( transformMatrixcube );
791 mCubeRoot->addComponent( cubeTransform );
792
793 // text
794 QString text;
795 int fontSize = 0.75 * mFontSize;
796 float textHeight = fontSize * 1.5f;
797 float textWidth;
798 QFont f = QFontDatabase::systemFont( QFontDatabase::FixedFont );
799 f.setPointSize( fontSize );
800 f.setWeight( QFont::Weight::Black );
801
802 {
803 text = QStringLiteral( "top" );
804 textWidth = text.length() * fontSize * 0.75f;
805 QVector3D translation = minPos + QVector3D(
806 mCylinderLength * 0.5f - textWidth / 2.0f,
807 mCylinderLength * 0.5f - textHeight / 2.0f,
808 mCylinderLength * 1.01f );
809 QMatrix4x4 rotation;
810 mCubeLabels << addCubeText( text, textHeight, textWidth, f, rotation, translation );
811 }
812
813 {
814 text = QStringLiteral( "btm" );
815 textWidth = text.length() * fontSize * 0.75f;
816 QVector3D translation = minPos + QVector3D(
817 mCylinderLength * 0.5f - textWidth / 2.0f,
818 mCylinderLength * 0.5f + textHeight / 2.0f,
819 -mCylinderLength * 0.01f );
820 QMatrix4x4 rotation;
821 rotation.rotate( 180.0f, QVector3D( 1.0f, 0.0f, 0.0f ).normalized() );
822 mCubeLabels << addCubeText( text, textHeight, textWidth, f, rotation, translation );
823 }
824
825 {
826 text = QStringLiteral( "west" );
827 textWidth = text.length() * fontSize * 0.75f;
828 QVector3D translation = minPos + QVector3D(
829 - mCylinderLength * 0.01f,
830 mCylinderLength * 0.5f + textWidth / 2.0f,
831 mCylinderLength * 0.5f - textHeight / 2.0f );
832 QMatrix4x4 rotation;
833 rotation.rotate( 90.0f, QVector3D( 0.0f, -1.0f, 0.0f ).normalized() );
834 rotation.rotate( 90.0f, QVector3D( 0.0f, 0.0f, -1.0f ).normalized() );
835 mCubeLabels << addCubeText( text, textHeight, textWidth, f, rotation, translation );
836 }
837
838 {
839 text = QStringLiteral( "east" );
840 textWidth = text.length() * fontSize * 0.75f;
841 QVector3D translation = minPos + QVector3D(
842 mCylinderLength * 1.01f,
843 mCylinderLength * 0.5f - textWidth / 2.0f,
844 mCylinderLength * 0.5f - textHeight / 2.0f );
845 QMatrix4x4 rotation;
846 rotation.rotate( 90.0f, QVector3D( 0.0f, 1.0f, 0.0f ).normalized() );
847 rotation.rotate( 90.0f, QVector3D( 0.0f, 0.0f, 1.0f ).normalized() );
848 mCubeLabels << addCubeText( text, textHeight, textWidth, f, rotation, translation );
849 }
850
851 {
852 text = QStringLiteral( "south" );
853 textWidth = text.length() * fontSize * 0.75f;
854 QVector3D translation = minPos + QVector3D(
855 mCylinderLength * 0.5f - textWidth / 2.0f,
856 - mCylinderLength * 0.01f,
857 mCylinderLength * 0.5f - textHeight / 2.0f );
858 QMatrix4x4 rotation;
859 rotation.rotate( 90.0f, QVector3D( 1.0f, 0.0f, 0.0f ).normalized() );
860 mCubeLabels << addCubeText( text, textHeight, textWidth, f, rotation, translation );
861 }
862
863 {
864 text = QStringLiteral( "north" );
865 textWidth = text.length() * fontSize * 0.75f;
866 QVector3D translation = minPos + QVector3D(
867 mCylinderLength * 0.5f + textWidth / 2.0f,
868 mCylinderLength * 1.01f,
869 mCylinderLength * 0.5f - textHeight / 2.0f );
870 QMatrix4x4 rotation;
871 rotation.rotate( 90.0f, QVector3D( -1.0f, 0.0f, 0.0f ).normalized() );
872 rotation.rotate( 180.0f, QVector3D( 0.0f, 0.0f, 1.0f ).normalized() );
873 mCubeLabels << addCubeText( text, textHeight, textWidth, f, rotation, translation );
874 }
875
876 for ( Qt3DExtras::QText2DEntity *l : std::as_const( mCubeLabels ) )
877 {
878 l->setParent( mCubeRoot );
879 }
880}
881
882Qt3DExtras::QText2DEntity *Qgs3DAxis::addCubeText( const QString &text, float textHeight, float textWidth, const QFont &f, const QMatrix4x4 &rotation, const QVector3D &translation )
883{
884 Qt3DExtras::QText2DEntity *textEntity = new Qt3DExtras::QText2DEntity;
885 textEntity->setObjectName( "3DAxis_cube_label_" + text );
886 textEntity->setFont( f );
887 textEntity->setHeight( textHeight );
888 textEntity->setWidth( textWidth );
889 textEntity->setColor( QColor( 192, 192, 192 ) );
890 textEntity->setText( text );
891
892 Qt3DCore::QTransform *textFrontTransform = new Qt3DCore::QTransform();
893 textFrontTransform->setMatrix( rotation );
894 textFrontTransform->setTranslation( translation );
895 textEntity->addComponent( textFrontTransform );
896
897 return textEntity;
898}
899
900void Qgs3DAxis::createAxis( Qt::Axis axisType )
901{
902 float cylinderRadius = 0.05f * mCylinderLength;
903 float coneLength = 0.3f * mCylinderLength;
904 float coneBottomRadius = 0.1f * mCylinderLength;
905
906 QQuaternion rotation;
907 QColor color;
908
909 Qt3DExtras::QText2DEntity *text = nullptr;
910 Qt3DCore::QTransform *textTransform = nullptr;
911 QString name;
912
913 switch ( axisType )
914 {
915 case Qt::Axis::XAxis:
916 mTextX = new Qt3DExtras::QText2DEntity( ); // object initialization in two step:
917 mTextX->setParent( mTwoDLabelSceneEntity ); // see https://bugreports.qt.io/browse/QTBUG-77139
918 connect( mTextX, &Qt3DExtras::QText2DEntity::textChanged, this, &Qgs3DAxis::onTextXChanged );
919 mTextTransformX = new Qt3DCore::QTransform();
920 mTextCoordX = QVector3D( mCylinderLength + coneLength / 2.0f, 0.0f, 0.0f );
921
922 rotation = QQuaternion::fromAxisAndAngle( QVector3D( 0.0f, 0.0f, 1.0f ), -90.0f );
923 color = Qt::red;
924 text = mTextX;
925 textTransform = mTextTransformX;
926 name = "3DAxis_axisX";
927 break;
928
929 case Qt::Axis::YAxis:
930 mTextY = new Qt3DExtras::QText2DEntity( ); // object initialization in two step:
931 mTextY->setParent( mTwoDLabelSceneEntity ); // see https://bugreports.qt.io/browse/QTBUG-77139
932 connect( mTextY, &Qt3DExtras::QText2DEntity::textChanged, this, &Qgs3DAxis::onTextYChanged );
933 mTextTransformY = new Qt3DCore::QTransform();
934 mTextCoordY = QVector3D( 0.0f, mCylinderLength + coneLength / 2.0f, 0.0f );
935
936 rotation = QQuaternion::fromAxisAndAngle( QVector3D( 0.0f, 0.0f, 0.0f ), 0.0f );
937 color = Qt::green;
938 text = mTextY;
939 textTransform = mTextTransformY;
940 name = "3DAxis_axisY";
941 break;
942
943 case Qt::Axis::ZAxis:
944 mTextZ = new Qt3DExtras::QText2DEntity( ); // object initialization in two step:
945 mTextZ->setParent( mTwoDLabelSceneEntity ); // see https://bugreports.qt.io/browse/QTBUG-77139
946 connect( mTextZ, &Qt3DExtras::QText2DEntity::textChanged, this, &Qgs3DAxis::onTextZChanged );
947 mTextTransformZ = new Qt3DCore::QTransform();
948 mTextCoordZ = QVector3D( 0.0f, 0.0f, mCylinderLength + coneLength / 2.0f );
949
950 rotation = QQuaternion::fromAxisAndAngle( QVector3D( 1.0f, 0.0f, 0.0f ), 90.0f );
951 color = Qt::blue;
952 text = mTextZ;
953 textTransform = mTextTransformZ;
954 name = "3DAxis_axisZ";
955 break;
956
957 default:
958 return;
959 }
960
961 // cylinder
962 Qt3DCore::QEntity *cylinder = new Qt3DCore::QEntity( mAxisRoot );
963 cylinder->setObjectName( name );
964
965 Qt3DExtras::QCylinderMesh *cylinderMesh = new Qt3DExtras::QCylinderMesh;
966 cylinderMesh->setRadius( cylinderRadius );
967 cylinderMesh->setLength( mCylinderLength );
968 cylinderMesh->setRings( 10 );
969 cylinderMesh->setSlices( 4 );
970 cylinder->addComponent( cylinderMesh );
971
972 Qt3DExtras::QPhongMaterial *cylinderMaterial = new Qt3DExtras::QPhongMaterial( cylinder );
973 cylinderMaterial->setAmbient( color );
974 cylinderMaterial->setShininess( 0 );
975 cylinder->addComponent( cylinderMaterial );
976
977 Qt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform;
978 QMatrix4x4 transformMatrixCylinder;
979 transformMatrixCylinder.rotate( rotation );
980 transformMatrixCylinder.translate( QVector3D( 0.0f, mCylinderLength / 2.0f, 0.0f ) );
981 cylinderTransform->setMatrix( transformMatrixCylinder );
982 cylinder->addComponent( cylinderTransform );
983
984 // cone
985 Qt3DCore::QEntity *coneEntity = new Qt3DCore::QEntity( mAxisRoot );
986 coneEntity->setObjectName( name );
987 Qt3DExtras::QConeMesh *coneMesh = new Qt3DExtras::QConeMesh;
988 coneMesh->setLength( coneLength );
989 coneMesh->setBottomRadius( coneBottomRadius );
990 coneMesh->setTopRadius( 0.0f );
991 coneMesh->setRings( 10 );
992 coneMesh->setSlices( 4 );
993 coneEntity->addComponent( coneMesh );
994
995 Qt3DExtras::QPhongMaterial *coneMaterial = new Qt3DExtras::QPhongMaterial( coneEntity );
996 coneMaterial->setAmbient( color );
997 coneMaterial->setShininess( 0 );
998 coneEntity->addComponent( coneMaterial );
999
1000 Qt3DCore::QTransform *coneTransform = new Qt3DCore::QTransform;
1001 QMatrix4x4 transformMatrixCone;
1002 transformMatrixCone.rotate( rotation );
1003 transformMatrixCone.translate( QVector3D( 0.0f, mCylinderLength, 0.0f ) );
1004 coneTransform->setMatrix( transformMatrixCone );
1005 coneEntity->addComponent( coneTransform );
1006
1007 // text font, height and width will be set later in onText?Changed
1008 text->setColor( QColor( 192, 192, 192, 192 ) );
1009 text->addComponent( textTransform );
1010}
1011
1013{
1014 createAxisScene();
1015 onAxisViewportSizeUpdate();
1016}
1017
1018void Qgs3DAxis::onAxisViewportSizeUpdate( int )
1019{
1020 Qgs3DAxisSettings settings = mMapSettings->get3DAxisSettings();
1021
1022 double windowWidth = ( double )mParentWindow->width();
1023 double windowHeight = ( double )mParentWindow->height();
1024
1025 QgsMapSettings set;
1026 if ( 2 <= QgsLogger::debugLevel() )
1027 {
1028 QgsDebugMsgLevel( QString( "onAxisViewportSizeUpdate window w/h: %1px / %2px" )
1029 .arg( windowWidth ).arg( windowHeight ), 2 );
1030 QgsDebugMsgLevel( QString( "onAxisViewportSizeUpdate window physicalDpi %1 (%2, %3)" )
1031 .arg( mParentWindow->screen()->physicalDotsPerInch() )
1032 .arg( mParentWindow->screen()->physicalDotsPerInchX() )
1033 .arg( mParentWindow->screen()->physicalDotsPerInchY() ), 2 );
1034 QgsDebugMsgLevel( QString( "onAxisViewportSizeUpdate window logicalDotsPerInch %1 (%2, %3)" )
1035 .arg( mParentWindow->screen()->logicalDotsPerInch() )
1036 .arg( mParentWindow->screen()->logicalDotsPerInchX() )
1037 .arg( mParentWindow->screen()->logicalDotsPerInchY() ), 2 );
1038
1039 QgsDebugMsgLevel( QString( "onAxisViewportSizeUpdate window pixel ratio %1" )
1040 .arg( mParentWindow->screen()->devicePixelRatio() ), 2 );
1041
1042 QgsDebugMsgLevel( QString( "onAxisViewportSizeUpdate set pixel ratio %1" )
1043 .arg( set.devicePixelRatio() ), 2 );
1044 QgsDebugMsgLevel( QString( "onAxisViewportSizeUpdate set outputDpi %1" )
1045 .arg( set.outputDpi() ), 2 );
1046 QgsDebugMsgLevel( QString( "onAxisViewportSizeUpdate set dpiTarget %1" )
1047 .arg( set.dpiTarget() ), 2 );
1048 }
1049
1050 // default viewport size in pixel according to 92 dpi
1051 double defaultViewportPixelSize = ( ( double )settings.defaultViewportSize() / 25.4 ) * 92.0;
1052
1053 // computes the viewport size according to screen dpi but as the viewport size growths too fast
1054 // then we limit the growth by using a factor on the dpi difference.
1055 double viewportPixelSize = defaultViewportPixelSize + ( ( double )settings.defaultViewportSize() / 25.4 )
1056 * ( mParentWindow->screen()->physicalDotsPerInch() - 92.0 ) * 0.7;
1057 QgsDebugMsgLevel( QString( "onAxisViewportSizeUpdate viewportPixelSize %1" ).arg( viewportPixelSize ), 2 );
1058 double widthRatio = viewportPixelSize / windowWidth;
1059 double heightRatio = widthRatio * windowWidth / windowHeight;
1060
1061 QgsDebugMsgLevel( QString( "3DAxis viewport ratios width: %1% / height: %2%" ).arg( widthRatio ).arg( heightRatio ), 2 );
1062
1063 if ( heightRatio * windowHeight < viewportPixelSize )
1064 {
1065 heightRatio = viewportPixelSize / windowHeight;
1066 widthRatio = heightRatio * windowHeight / windowWidth;
1067 QgsDebugMsgLevel( QString( "3DAxis viewport, height too small, ratios adjusted to width: %1% / height: %2%" ).arg( widthRatio ).arg( heightRatio ), 2 );
1068 }
1069
1070 if ( heightRatio > settings.maxViewportRatio() || widthRatio > settings.maxViewportRatio() )
1071 {
1072 QgsDebugMsgLevel( "viewport takes too much place into the 3d view, disabling it", 2 );
1073 // take too much place into the 3d view
1074 mAxisViewport->setEnabled( false );
1075 setEnableCube( false );
1076 setEnableAxis( false );
1077 }
1078 else
1079 {
1080 // will be used to adjust the axis label translations/sizes
1081 mAxisScaleFactor = viewportPixelSize / defaultViewportPixelSize;
1082 QgsDebugMsgLevel( QString( "3DAxis viewport mAxisScaleFactor %1" ).arg( mAxisScaleFactor ), 2 );
1083
1084 if ( ! mAxisViewport->isEnabled() )
1085 {
1086 if ( settings.mode() == Qgs3DAxisSettings::Mode::Crs )
1087 setEnableAxis( true );
1088 else if ( settings.mode() == Qgs3DAxisSettings::Mode::Cube )
1089 setEnableCube( true );
1090 }
1091 mAxisViewport->setEnabled( true );
1092
1093 float xRatio = 1.0f;
1094 float yRatio = 1.0f;
1095 if ( settings.horizontalPosition() == Qt::AnchorPoint::AnchorLeft )
1096 xRatio = 0.0f;
1097 else if ( settings.horizontalPosition() == Qt::AnchorPoint::AnchorHorizontalCenter )
1098 xRatio = 0.5f - widthRatio / 2.0f;
1099 else
1100 xRatio = 1.0f - widthRatio;
1101
1102 if ( settings.verticalPosition() == Qt::AnchorPoint::AnchorTop )
1103 yRatio = 0.0f;
1104 else if ( settings.verticalPosition() == Qt::AnchorPoint::AnchorVerticalCenter )
1105 yRatio = 0.5f - heightRatio / 2.0f;
1106 else
1107 yRatio = 1.0f - heightRatio;
1108
1109 QgsDebugMsgLevel( QString( "Qgs3DAxis: update viewport: %1 x %2 x %3 x %4" ).arg( xRatio ).arg( yRatio ).arg( widthRatio ).arg( heightRatio ), 2 );
1110 mAxisViewport->setNormalizedRect( QRectF( xRatio, yRatio, widthRatio, heightRatio ) );
1111
1112 if ( settings.mode() == Qgs3DAxisSettings::Mode::Crs )
1113 {
1114 mTwoDLabelCamera->lens()->setOrthographicProjection(
1115 -windowWidth / 2.0f, windowWidth / 2.0f,
1116 -windowHeight / 2.0f, windowHeight / 2.0f,
1117 mTwoDLabelCamera->lens()->nearPlane(), mTwoDLabelCamera->lens()->farPlane() );
1118
1119 updateAxisLabelPosition();
1120 }
1121 }
1122}
1123
1124void Qgs3DAxis::onCameraUpdate( )
1125{
1126 Qt3DRender::QCamera *parentCamera = mCameraController->camera();
1127
1128 if ( parentCamera->viewVector() != mPreviousVector
1129 && !std::isnan( parentCamera->viewVector().x() )
1130 && !std::isnan( parentCamera->viewVector().y() )
1131 && !std::isnan( parentCamera->viewVector().z() ) )
1132 {
1133 mPreviousVector = parentCamera->viewVector();
1134 QVector3D mainCameraShift = parentCamera->viewVector().normalized();
1135 float zy_swap = mainCameraShift.y();
1136 mainCameraShift.setY( mainCameraShift.z() );
1137 mainCameraShift.setZ( -zy_swap );
1138 mainCameraShift.setX( -mainCameraShift.x() );
1139
1140 if ( mAxisCamera->projectionType() == Qt3DRender::QCameraLens::ProjectionType::OrthographicProjection )
1141 {
1142 mAxisCamera->setPosition( mainCameraShift );
1143 }
1144 else
1145 {
1146 mAxisCamera->setPosition( mainCameraShift * mCylinderLength * 10.0 );
1147 }
1148
1149 if ( mAxisRoot->isEnabled() )
1150 {
1151 updateAxisLabelPosition();
1152 }
1153 }
1154}
1155
1156void Qgs3DAxis::updateAxisLabelPosition()
1157{
1158 if ( mTextTransformX && mTextTransformY && mTextTransformZ )
1159 {
1160 mTextTransformX->setTranslation( from3DTo2DLabelPosition( mTextCoordX * mAxisScaleFactor, mAxisCamera,
1161 mAxisViewport, mTwoDLabelCamera, mTwoDLabelViewport,
1162 mParentWindow->size() ) );
1163 onTextXChanged( mTextX->text() );
1164
1165 mTextTransformY->setTranslation( from3DTo2DLabelPosition( mTextCoordY * mAxisScaleFactor, mAxisCamera,
1166 mAxisViewport, mTwoDLabelCamera, mTwoDLabelViewport,
1167 mParentWindow->size() ) );
1168 onTextYChanged( mTextY->text() );
1169
1170 mTextTransformZ->setTranslation( from3DTo2DLabelPosition( mTextCoordZ * mAxisScaleFactor, mAxisCamera,
1171 mAxisViewport, mTwoDLabelCamera, mTwoDLabelViewport,
1172 mParentWindow->size() ) );
1173 onTextZChanged( mTextZ->text() );
1174 }
1175}
1176
1177void Qgs3DAxis::onTextXChanged( const QString &text )
1178{
1179 QFont f = QFont( "monospace", mAxisScaleFactor * mFontSize ); // TODO: should use outlined font
1180 f.setWeight( QFont::Weight::Black );
1181 f.setStyleStrategy( QFont::StyleStrategy::ForceOutline );
1182 mTextX->setFont( f );
1183 mTextX->setWidth( mAxisScaleFactor * mFontSize * text.length() );
1184 mTextX->setHeight( mAxisScaleFactor * mFontSize * 1.5f );
1185}
1186
1187void Qgs3DAxis::onTextYChanged( const QString &text )
1188{
1189 QFont f = QFont( "monospace", mAxisScaleFactor * mFontSize ); // TODO: should use outlined font
1190 f.setWeight( QFont::Weight::Black );
1191 f.setStyleStrategy( QFont::StyleStrategy::ForceOutline );
1192 mTextY->setFont( f );
1193 mTextY->setWidth( mAxisScaleFactor * mFontSize * text.length() );
1194 mTextY->setHeight( mAxisScaleFactor * mFontSize * 1.5f );
1195}
1196
1197void Qgs3DAxis::onTextZChanged( const QString &text )
1198{
1199 QFont f = QFont( "monospace", mAxisScaleFactor * mFontSize ); // TODO: should use outlined font
1200 f.setWeight( QFont::Weight::Black );
1201 f.setStyleStrategy( QFont::StyleStrategy::ForceOutline );
1202 mTextZ->setFont( f );
1203 mTextZ->setWidth( mAxisScaleFactor * mFontSize * text.length() );
1204 mTextZ->setHeight( mAxisScaleFactor * mFontSize * 1.5f );
1205}
1206
1207//
1208// Qgs3DWiredMesh
1209//
1210
1211Qgs3DWiredMesh::Qgs3DWiredMesh( Qt3DCore::QNode *parent )
1212 : Qt3DRender::QGeometryRenderer( parent )
1213 , mPositionAttribute( new Qt3DQAttribute( this ) )
1214 , mVertexBuffer( new Qt3DQBuffer( this ) )
1215{
1216 mPositionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
1217 mPositionAttribute->setBuffer( mVertexBuffer );
1218 mPositionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
1219 mPositionAttribute->setVertexSize( 3 );
1220 mPositionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
1221
1222 mGeom = new Qt3DQGeometry( this );
1223 mGeom->addAttribute( mPositionAttribute );
1224
1225 setInstanceCount( 1 );
1226 setIndexOffset( 0 );
1227 setFirstInstance( 0 );
1228 setPrimitiveType( Qt3DRender::QGeometryRenderer::Lines );
1229 setGeometry( mGeom );
1230}
1231
1233
1234void Qgs3DWiredMesh::setVertices( const QList<QVector3D> &vertices )
1235{
1236 QByteArray vertexBufferData;
1237 vertexBufferData.resize( vertices.size() * 3 * sizeof( float ) );
1238 float *rawVertexArray = reinterpret_cast<float *>( vertexBufferData.data() );
1239 int idx = 0;
1240 for ( const QVector3D &v : std::as_const( vertices ) )
1241 {
1242 rawVertexArray[idx++] = v.x();
1243 rawVertexArray[idx++] = v.y();
1244 rawVertexArray[idx++] = v.z();
1245 }
1246
1247 mVertexBuffer->setData( vertexBufferData );
1248 setVertexCount( vertices.count() );
1249}
Contains the configuration of a 3d axis.
void setMode(Qgs3DAxisSettings::Mode type)
Sets the type of the 3daxis.
double maxViewportRatio() const
Returns the maximal axis viewport ratio (see Qt3DRender::QViewport::normalizedRect())
Mode
Axis representation enum.
@ Crs
Respect CRS directions.
@ Cube
Abstract cube mode.
Qt::AnchorPoint verticalPosition() const
Returns the vertical position for the 3d axis.
void setHorizontalPosition(Qt::AnchorPoint position)
Sets the horizontal position for the 3d axis.
int defaultViewportSize() const
Returns the default axis viewport size in millimeters.
Qgs3DAxisSettings::Mode mode() const
Returns the type of the 3daxis.
Qt::AnchorPoint horizontalPosition() const
Returns the horizontal position for the 3d axis.
void setVerticalPosition(Qt::AnchorPoint position)
Sets the vertical position for the 3d axis.
Qgs3DAxis(Qt3DExtras::Qt3DWindow *parentWindow, Qt3DCore::QEntity *parent3DScene, Qgs3DMapScene *mapScene, QgsCameraController *camera, Qgs3DMapSettings *map)
Default Qgs3DAxis constructor.
Definition: qgs3daxis.cpp:57
QVector3D from3DTo2DLabelPosition(const QVector3D &sourcePos, Qt3DRender::QCamera *sourceCamera, Qt3DRender::QViewport *sourceViewport, Qt3DRender::QCamera *destCamera, Qt3DRender::QViewport *destViewport, const QSize &destSize)
project a 3D position from sourceCamera (in sourceViewport) to a 2D position for destCamera (in destV...
Definition: qgs3daxis.cpp:358
~Qgs3DAxis() override
Definition: qgs3daxis.cpp:87
void onAxisSettingsChanged()
Force update of the axis and the viewport when a setting has changed.
Definition: qgs3daxis.cpp:1012
QgsAbstract3DEngine * engine()
Returns the abstract 3D engine.
QgsTerrainEntity * terrainEntity()
Returns terrain entity (may be temporarily nullptr)
Definition: qgs3dmapscene.h:88
Qgs3DAxisSettings get3DAxisSettings() const
Returns the current configuration of 3d axis.
float terrainElevationOffset() const
Returns the elevation offset of the terrain (used to move the terrain up or down)
void set3DAxisSettings(const Qgs3DAxisSettings &axisSettings, bool force=false)
Sets the current configuration of 3d axis.
bool terrainRenderingEnabled() const
Returns whether the 2D terrain surface will be rendered.
void axisSettingsChanged()
Emitted when 3d axis rendering settings are changed.
~Qgs3DWiredMesh() override
Qgs3DWiredMesh(Qt3DCore::QNode *parent=nullptr)
Defaul Qgs3DWiredMesh constructor.
Definition: qgs3daxis.cpp:1211
void setVertices(const QList< QVector3D > &vertices)
add or replace mesh vertices coordinates
Definition: qgs3daxis.cpp:1234
3
Definition: qgsaabb.h:34
QList< QVector3D > verticesForLines() const
Returns a list of pairs of vertices (useful for display of bounding boxes)
Definition: qgsaabb.cpp:63
Qt3DRender::QCamera * camera() const
Returns camera that is being controlled.
void cameraChanged()
Emitted when camera has been updated.
void setLookingAtPoint(const QgsVector3D &point, float distance, float pitch, float yaw)
Sets the complete camera configuration: the point towards it is looking (in 3D world coordinates),...
QgsVector3D lookingAtPoint() const
Returns the point in the world coordinates towards which the camera is looking.
static QString axisDirectionToAbbreviatedString(Qgis::CrsAxisDirection axis)
Returns a translated abbreviation representing an axis direction.
QList< Qgis::CrsAxisDirection > axisOrdering() const
Returns an ordered list of the axis directions reflecting the native axis order for the CRS.
static int debugLevel()
Reads the environment variable QGIS_DEBUG and converts it to int.
Definition: qgslogger.h:108
static void warning(const QString &msg)
Goes to qWarning.
Definition: qgslogger.cpp:122
The QgsMapSettings class contains configuration for rendering of the map.
double dpiTarget() const
Returns the target DPI (dots per inch) to be taken into consideration when rendering.
float devicePixelRatio() const
Returns the device pixel ratio.
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:53
QVector3D toVector3D() const
Converts the current object to QVector3D.
Definition: qgsvector3d.h:169
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:49
void set(double x, double y, double z)
Sets vector coordinates.
Definition: qgsvector3d.h:56
Qt3DCore::QAttribute Qt3DQAttribute
Definition: qgs3daxis.cpp:28
Qt3DCore::QBuffer Qt3DQBuffer
Definition: qgs3daxis.cpp:30
Qt3DCore::QGeometry Qt3DQGeometry
Definition: qgs3daxis.cpp:29
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
const QgsCoordinateReferenceSystem & crs