Skip to content

Commit

Permalink
Added a qgis 3d test executable to quickly open a 3D scene
Browse files Browse the repository at this point in the history
The 3d sandbox needs a qgis project file and will open 3D map canvas
  • Loading branch information
wonder-sk authored and nyalldawson committed Oct 26, 2020
1 parent a86149b commit 8c059eb
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/src/3d/CMakeLists.txt
Expand Up @@ -76,6 +76,8 @@ MACRO (ADD_QGIS_TEST testname testsrc)
# INSTALL_RPATH_USE_LINK_PATH true )
ENDMACRO (ADD_QGIS_TEST)

ADD_SUBDIRECTORY(sandbox)

ADD_QGIS_TEST(3dutilstest testqgs3dutils.cpp)
ADD_QGIS_TEST(3drenderingtest testqgs3drendering.cpp)
ADD_QGIS_TEST(layout3dmaptest testqgslayout3dmap.cpp)
Expand Down
66 changes: 66 additions & 0 deletions tests/src/3d/sandbox/CMakeLists.txt
@@ -0,0 +1,66 @@

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/external
${CMAKE_SOURCE_DIR}/external/nlohmann
${CMAKE_SOURCE_DIR}/tests/core #for render checker class
${CMAKE_SOURCE_DIR}/src/3d
${CMAKE_SOURCE_DIR}/src/3d/chunks
${CMAKE_SOURCE_DIR}/src/3d/materials
${CMAKE_SOURCE_DIR}/src/3d/mesh
${CMAKE_SOURCE_DIR}/src/3d/symbols
${CMAKE_SOURCE_DIR}/src/3d/terrain
${CMAKE_SOURCE_DIR}/src/core
${CMAKE_SOURCE_DIR}/src/core/3d
${CMAKE_SOURCE_DIR}/src/core/expression
${CMAKE_SOURCE_DIR}/src/core/auth
${CMAKE_SOURCE_DIR}/src/core/geometry
${CMAKE_SOURCE_DIR}/src/core/labeling
${CMAKE_SOURCE_DIR}/src/core/layertree
${CMAKE_SOURCE_DIR}/src/core/layout
${CMAKE_SOURCE_DIR}/src/core/metadata
${CMAKE_SOURCE_DIR}/src/core/mesh
${CMAKE_SOURCE_DIR}/src/core/pointcloud
${CMAKE_SOURCE_DIR}/src/core/raster
${CMAKE_SOURCE_DIR}/src/core/symbology
${CMAKE_SOURCE_DIR}/src/core/effects
${CMAKE_SOURCE_DIR}/src/core/fieldformatter
${CMAKE_SOURCE_DIR}/src/test
${CMAKE_SOURCE_DIR}/src/native

${CMAKE_BINARY_DIR}/src/core
${CMAKE_BINARY_DIR}/src/3d
${CMAKE_BINARY_DIR}/src/ui
${CMAKE_BINARY_DIR}/src/native
${CMAKE_CURRENT_BINARY_DIR}

${CMAKE_SOURCE_DIR}/src/app/3d
${CMAKE_BINARY_DIR}/src/app
${QWT_INCLUDE_DIR}
#${CMAKE_SOURCE_DIR}/external/qwtpolar-1.1.1
)
INCLUDE_DIRECTORIES(SYSTEM
${QT_INCLUDE_DIR}
${GDAL_INCLUDE_DIR}
${QT5_3DEXTRA_INCLUDE_DIR}
)


ADD_EXECUTABLE(qgis_3d_sandbox
qgis_3d_sandbox.cpp
${CMAKE_SOURCE_DIR}/src/app/3d/qgs3dmapcanvas.cpp
${CMAKE_SOURCE_DIR}/src/app/3d/qgs3dnavigationwidget.cpp
)
SET_TARGET_PROPERTIES(qgis_3d_sandbox PROPERTIES AUTORCC TRUE)
TARGET_LINK_LIBRARIES(qgis_3d_sandbox
${Qt5Xml_LIBRARIES}
${Qt5Core_LIBRARIES}
${Qt5Svg_LIBRARIES}
${Qt5Network_LIBRARIES}
${Qt5Test_LIBRARIES}
${PROJ_LIBRARY}
${GEOS_LIBRARY}
${GDAL_LIBRARY}
${QWT_LIBRARY}
qgis_core
qgis_3d
qgis_native)
126 changes: 126 additions & 0 deletions tests/src/3d/sandbox/qgis_3d_sandbox.cpp
@@ -0,0 +1,126 @@
/***************************************************************************
qgis_3d_sandbox.cpp
--------------------
begin : October 2020
copyright : (C) 2020 by Martin Dobias
email : wonder dot sk at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QApplication>
#include <QDesktopWidget>

#include "qgsapplication.h"
#include "qgs3d.h"
#include "qgslayertree.h"
#include "qgsmapsettings.h"
#include "qgspointcloudlayer.h"
#include "qgspointcloudlayer3drenderer.h"
#include "qgsproject.h"
#include "qgsflatterraingenerator.h"
#include "qgs3dmapscene.h"
#include "qgs3dmapsettings.h"
#include "qgs3dmapcanvas.h"


void initCanvas3D( Qgs3DMapCanvas *canvas )
{
QgsLayerTree *root = QgsProject::instance()->layerTreeRoot();
const QList< QgsMapLayer * > visibleLayers = root->checkedLayers();

QgsMapSettings ms;
ms.setDestinationCrs( QgsProject::instance()->crs() );
ms.setLayers( visibleLayers );
QgsRectangle fullExtent = ms.fullExtent();

QgsRectangle extent = fullExtent;
extent.scale( 1.3 );

Qgs3DMapSettings *map = new Qgs3DMapSettings;
map->setCrs( QgsProject::instance()->crs() );
map->setOrigin( QgsVector3D( fullExtent.center().x(), fullExtent.center().y(), 0 ) );
map->setLayers( visibleLayers );
map->setTerrainLayers( visibleLayers );

map->setTransformContext( QgsProject::instance()->transformContext() );
map->setPathResolver( QgsProject::instance()->pathResolver() );
map->setMapThemeCollection( QgsProject::instance()->mapThemeCollection() );
QObject::connect( QgsProject::instance(), &QgsProject::transformContextChanged, map, [map]
{
map->setTransformContext( QgsProject::instance()->transformContext() );
} );

QgsFlatTerrainGenerator *flatTerrain = new QgsFlatTerrainGenerator;
flatTerrain->setCrs( map->crs() );
flatTerrain->setExtent( fullExtent );
map->setTerrainGenerator( flatTerrain );

QgsPointLightSettings defaultPointLight;
defaultPointLight.setPosition( QgsVector3D( 0, 1000, 0 ) );
defaultPointLight.setConstantAttenuation( 0 );
map->setPointLights( QList<QgsPointLightSettings>() << defaultPointLight );
map->setOutputDpi( QgsApplication::desktop()->logicalDpiX() );

canvas->setMap( map );

float dist = static_cast< float >( std::max( extent.width(), extent.height() ) );
canvas->setViewFromTop( extent.center(), dist, 0 );

QObject::connect( canvas->scene(), &Qgs3DMapScene::totalPendingJobsCountChanged, [canvas]
{
qDebug() << "pending jobs:" << canvas->scene()->totalPendingJobsCount();
} );

qDebug() << "pending jobs:" << canvas->scene()->totalPendingJobsCount();
}

int main( int argc, char *argv[] )
{
QApplication app( argc, argv );

// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
Qgs3D::initialize();

if ( argc < 2 )
{
qDebug() << "need QGIS project file";
return 1;
}

QString projectFile = argv[1];
bool res = QgsProject::instance()->read( projectFile );
if ( !res )
{
qDebug() << "can't open project file" << projectFile;
return 1;
}

// a hack to assign 3D renderer
for ( QgsMapLayer *layer : QgsProject::instance()->layerTreeRoot()->checkedLayers() )
{
if ( QgsPointCloudLayer *pcLayer = qobject_cast<QgsPointCloudLayer *>( layer ) )
{
QgsPointCloudLayer3DRenderer *r = new QgsPointCloudLayer3DRenderer();
r->setLayer( pcLayer );
r->resolveReferences( *QgsProject::instance() );
pcLayer->setRenderer3D( r );
}
}

Qgs3DMapCanvas *canvas = new Qgs3DMapCanvas;
initCanvas3D( canvas );
canvas->resize( 800, 600 );
canvas->show();

return app.exec();
}

0 comments on commit 8c059eb

Please sign in to comment.