@@ -240,7 +240,7 @@ void QgsCameraController::readXml( const QDomElement &elem )
240
240
setLookingAtPoint ( QgsVector3D ( x, elev, y ), dist, pitch, yaw );
241
241
}
242
242
243
- double QgsCameraController::sampleDepthBuffer ( const QImage &buffer, int px, int py )
243
+ double QgsCameraController::sampleDepthBuffer ( int px, int py )
244
244
{
245
245
double depth = 1 ;
246
246
@@ -249,24 +249,28 @@ double QgsCameraController::sampleDepthBuffer( const QImage &buffer, int px, int
249
249
{
250
250
for ( int y = py - 3 ; y <= py + 3 ; ++y )
251
251
{
252
- if ( buffer .valid ( x, y ) )
252
+ if ( mDepthBufferImage .valid ( x, y ) )
253
253
{
254
- depth = std::min ( depth, Qgs3DUtils::decodeDepth ( buffer .pixel ( x, y ) ) );
254
+ depth = std::min ( depth, Qgs3DUtils::decodeDepth ( mDepthBufferImage .pixel ( x, y ) ) );
255
255
}
256
256
}
257
257
}
258
258
259
259
if ( depth < 1 )
260
260
return depth;
261
261
262
+ // Cache the computed depth, since averaging over all pixels can be expensive
263
+ if ( mDepthBufferNonVoidAverage != -1 )
264
+ return mDepthBufferNonVoidAverage ;
265
+
262
266
// Returns the average of depth values that are not 1 (void area)
263
267
depth = 0 ;
264
268
int samplesCount = 0 ;
265
- for ( int x = 0 ; x < buffer .width (); ++x )
269
+ for ( int x = 0 ; x < mDepthBufferImage .width (); ++x )
266
270
{
267
- for ( int y = 0 ; y < buffer .height (); ++y )
271
+ for ( int y = 0 ; y < mDepthBufferImage .height (); ++y )
268
272
{
269
- double d = Qgs3DUtils::decodeDepth ( buffer .pixel ( x, y ) );
273
+ double d = Qgs3DUtils::decodeDepth ( mDepthBufferImage .pixel ( x, y ) );
270
274
if ( d < 1 )
271
275
{
272
276
depth += d;
@@ -281,6 +285,8 @@ double QgsCameraController::sampleDepthBuffer( const QImage &buffer, int px, int
281
285
else
282
286
depth /= samplesCount;
283
287
288
+ mDepthBufferNonVoidAverage = depth;
289
+
284
290
return depth;
285
291
}
286
292
@@ -319,7 +325,7 @@ void QgsCameraController::onPositionChanged( Qt3DInput::QMouseEvent *mouse )
319
325
320
326
bool QgsCameraController::screenPointToWorldPos ( QPoint position, Qt3DRender::QCamera *mCameraBefore , double &depth, QVector3D &worldPosition )
321
327
{
322
- depth = sampleDepthBuffer ( mDepthBufferImage , position.x (), position.y () );
328
+ depth = sampleDepthBuffer ( position.x (), position.y () );
323
329
if ( !std::isfinite ( depth ) )
324
330
{
325
331
QgsDebugMsgLevel ( QStringLiteral ( " screenPointToWorldPos: depth is NaN or Inf. This should not happen." ), 2 );
@@ -1026,6 +1032,7 @@ void QgsCameraController::depthBufferCaptured( const QImage &depthImage )
1026
1032
{
1027
1033
mDepthBufferImage = depthImage;
1028
1034
mDepthBufferIsReady = true ;
1035
+ mDepthBufferNonVoidAverage = -1 ;
1029
1036
1030
1037
if ( mCurrentOperation == MouseOperation::ZoomWheel )
1031
1038
{
0 commit comments