Skip to content

Commit

Permalink
fix crashes in QGIS 3D, mostly directly at project load
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored and nyalldawson committed Oct 11, 2020
1 parent 3686e01 commit aa014d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/3d/terrain/qgsdemterraintileloader_p.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsterraintileentity_p.h"

#include <Qt3DRender/QGeometryRenderer>
#include <QMutexLocker>

///@cond PRIVATE

Expand Down Expand Up @@ -265,21 +266,27 @@ QByteArray QgsDemHeightMapGenerator::renderSynchronously( int x, int y, int z )
return data;
}

float QgsDemHeightMapGenerator::heightAt( double x, double y )
void QgsDemHeightMapGenerator::lazyLoadDtmCoarseData( int res, const QgsRectangle &rect )
{
if ( !mDtm )
return 0; // TODO: calculate heights for online DTM

// TODO: this is quite a primitive implementation: better to use heightmaps currently in use
int res = 1024;
QgsRectangle rect = mDtm->extent();
QMutexLocker locker( &mLazyLoadDtmCoarseDataMutex );
if ( mDtmCoarseData.isEmpty() )
{
std::unique_ptr< QgsRasterBlock > block( mDtm->dataProvider()->block( 1, rect, res, res ) );
block->convert( Qgis::Float32 );
mDtmCoarseData = block->data();
mDtmCoarseData.detach(); // make a deep copy
}
}

float QgsDemHeightMapGenerator::heightAt( double x, double y )
{
if ( !mDtm )
return 0; // TODO: calculate heights for online DTM

// TODO: this is quite a primitive implementation: better to use heightmaps currently in use
int res = 1024;
QgsRectangle rect = mDtm->extent();
lazyLoadDtmCoarseData( res, rect );

int cellX = ( int )( ( x - rect.xMinimum() ) / rect.width() * res + .5f );
int cellY = ( int )( ( rect.yMaximum() - y ) / rect.height() * res + .5f );
Expand Down
3 changes: 3 additions & 0 deletions src/3d/terrain/qgsdemterraintileloader_p.h
Expand Up @@ -30,6 +30,7 @@
#include <QtConcurrent/QtConcurrentRun>
#include <QFutureWatcher>
#include <QElapsedTimer>
#include <QMutex>

#include "qgsrectangle.h"
#include "qgsterraintileloader_p.h"
Expand Down Expand Up @@ -129,6 +130,8 @@ class QgsDemHeightMapGenerator : public QObject

QHash<QFutureWatcher<QByteArray>*, JobData> mJobs;

void lazyLoadDtmCoarseData( int res, const QgsRectangle &rect );
mutable QMutex mLazyLoadDtmCoarseDataMutex;
//! used for height queries
QByteArray mDtmCoarseData;
};
Expand Down

0 comments on commit aa014d9

Please sign in to comment.