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
PeterPetrik authored and nyalldawson committed Oct 9, 2020
1 parent 1258afe commit 9d06186
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 @@ -27,6 +27,7 @@
#include "qgsterraingenerator.h"

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

///@cond PRIVATE

Expand Down Expand Up @@ -268,21 +269,27 @@ void QgsDemHeightMapGenerator::waitForFinished()
}
}

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 @@ -32,6 +32,7 @@
#include <QtConcurrent/QtConcurrentRun>
#include <QFutureWatcher>
#include <QElapsedTimer>
#include <QMutex>

#include "qgschunknode_p.h"
#include "qgsrectangle.h"
Expand Down Expand Up @@ -133,6 +134,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 9d06186

Please sign in to comment.