/
qgspointcloudlayerrenderer.cpp
365 lines (304 loc) · 12.5 KB
/
qgspointcloudlayerrenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/***************************************************************************
qgspointcloudlayerrenderer.cpp
--------------------
begin : October 2020
copyright : (C) 2020 by Peter Petrik
email : zilolv 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 <QElapsedTimer>
#include <QPointer>
#include <QTimer>
#include <QDebug>
#include "qgspointcloudlayerrenderer.h"
#include "qgspointcloudlayer.h"
#include "qgsrendercontext.h"
#include "qgspointcloudindex.h"
#include "qgsstyle.h"
#include "qgscolorramp.h"
#include "qgspointcloudrequest.h"
#include "qgspointcloudattribute.h"
#include "qgspointcloudrenderer.h"
#include "qgspointcloudextentrenderer.h"
#include "qgslogger.h"
#include "qgspointcloudlayerelevationproperties.h"
#include "qgsmessagelog.h"
#include "qgscircle.h"
#include "qgsmapclippingutils.h"
#include "qgspointcloudblockhandle.h"
QgsPointCloudLayerRenderer::QgsPointCloudLayerRenderer( QgsPointCloudLayer *layer, QgsRenderContext &context )
: QgsMapLayerRenderer( layer->id(), &context )
, mLayer( layer )
, mLayerAttributes( layer->attributes() )
{
// TODO: we must not keep pointer to mLayer (it's dangerous) - we must copy anything we need for rendering
// or use some locking to prevent read/write from multiple threads
if ( !mLayer || !mLayer->dataProvider() || !mLayer->renderer() )
return;
mRenderer.reset( mLayer->renderer()->clone() );
if ( mLayer->dataProvider()->index() )
{
mScale = mLayer->dataProvider()->index()->scale();
mOffset = mLayer->dataProvider()->index()->offset();
}
if ( const QgsPointCloudLayerElevationProperties *elevationProps = qobject_cast< const QgsPointCloudLayerElevationProperties * >( mLayer->elevationProperties() ) )
{
mZOffset = elevationProps->zOffset();
mZScale = elevationProps->zScale();
}
mCloudExtent = mLayer->dataProvider()->polygonBounds();
mClippingRegions = QgsMapClippingUtils::collectClippingRegionsForLayer( *renderContext(), layer );
mReadyToCompose = false;
}
bool QgsPointCloudLayerRenderer::render()
{
QgsPointCloudRenderContext context( *renderContext(), mScale, mOffset, mZScale, mZOffset );
// Set up the render configuration options
QPainter *painter = context.renderContext().painter();
QgsScopedQPainterState painterState( painter );
context.renderContext().setPainterFlagsUsingContext( painter );
if ( !mClippingRegions.empty() )
{
bool needsPainterClipPath = false;
const QPainterPath path = QgsMapClippingUtils::calculatePainterClipRegion( mClippingRegions, *renderContext(), QgsMapLayerType::VectorTileLayer, needsPainterClipPath );
if ( needsPainterClipPath )
renderContext()->painter()->setClipPath( path, Qt::IntersectClip );
}
if ( mRenderer->type() == QLatin1String( "extent" ) )
{
// special case for extent only renderer!
mRenderer->startRender( context );
static_cast< QgsPointCloudExtentRenderer * >( mRenderer.get() )->renderExtent( mCloudExtent, context );
mRenderer->stopRender( context );
mReadyToCompose = true;
return true;
}
// TODO cache!?
QgsPointCloudIndex *pc = mLayer->dataProvider()->index();
if ( !pc || !pc->isValid() )
{
mReadyToCompose = true;
return false;
}
// if the previous layer render was relatively quick (e.g. less than 3 seconds), the we show any previously
// cached version of the layer during rendering instead of the usual progressive updates
if ( mRenderTimeHint > 0 && mRenderTimeHint <= MAX_TIME_TO_USE_CACHED_PREVIEW_IMAGE )
{
mBlockRenderUpdates = true;
mElapsedTimer.start();
}
mRenderer->startRender( context );
mAttributes.push_back( QgsPointCloudAttribute( QStringLiteral( "X" ), QgsPointCloudAttribute::Int32 ) );
mAttributes.push_back( QgsPointCloudAttribute( QStringLiteral( "Y" ), QgsPointCloudAttribute::Int32 ) );
// collect attributes required by renderer
QSet< QString > rendererAttributes = mRenderer->usedAttributes( context );
if ( !context.renderContext().zRange().isInfinite() )
rendererAttributes.insert( QStringLiteral( "Z" ) );
for ( const QString &attribute : std::as_const( rendererAttributes ) )
{
if ( mAttributes.indexOf( attribute ) >= 0 )
continue; // don't re-add attributes we are already going to fetch
const int layerIndex = mLayerAttributes.indexOf( attribute );
if ( layerIndex < 0 )
{
QgsMessageLog::logMessage( QObject::tr( "Required attribute %1 not found in layer" ).arg( attribute ), QObject::tr( "Point Cloud" ) );
continue;
}
mAttributes.push_back( mLayerAttributes.at( layerIndex ) );
}
QgsPointCloudDataBounds db;
#ifdef QGISDEBUG
QElapsedTimer t;
t.start();
#endif
const IndexedPointCloudNode root = pc->root();
const double maximumError = context.renderContext().convertToPainterUnits( mRenderer->maximumScreenError(), mRenderer->maximumScreenErrorUnit() );// in pixels
const QgsRectangle rootNodeExtentLayerCoords = pc->nodeMapExtent( root );
QgsRectangle rootNodeExtentMapCoords;
try
{
rootNodeExtentMapCoords = context.renderContext().coordinateTransform().transformBoundingBox( rootNodeExtentLayerCoords );
}
catch ( QgsCsException & )
{
QgsDebugMsg( QStringLiteral( "Could not transform node extent to map CRS" ) );
rootNodeExtentMapCoords = rootNodeExtentLayerCoords;
}
const double rootErrorInMapCoordinates = rootNodeExtentMapCoords.width() / pc->span(); // in map coords
double mapUnitsPerPixel = context.renderContext().mapToPixel().mapUnitsPerPixel();
if ( ( rootErrorInMapCoordinates < 0.0 ) || ( mapUnitsPerPixel < 0.0 ) || ( maximumError < 0.0 ) )
{
QgsDebugMsg( QStringLiteral( "invalid screen error" ) );
mReadyToCompose = true;
return false;
}
double rootErrorPixels = rootErrorInMapCoordinates / mapUnitsPerPixel; // in pixels
const QVector<IndexedPointCloudNode> nodes = traverseTree( pc, context.renderContext(), pc->root(), maximumError, rootErrorPixels );
QgsPointCloudRequest request;
request.setAttributes( mAttributes );
// drawing
int nodesDrawn = 0;
bool canceled = false;
if ( pc->accessType() == QgsPointCloudIndex::AccessType::Local )
{
for ( const IndexedPointCloudNode &n : nodes )
{
if ( context.renderContext().renderingStopped() )
{
QgsDebugMsgLevel( "canceled", 2 );
canceled = true;
break;
}
std::unique_ptr<QgsPointCloudBlock> block( pc->nodeData( n, request ) );
if ( !block )
continue;
context.setAttributes( block->attributes() );
mRenderer->renderBlock( block.get(), context );
++nodesDrawn;
// as soon as first block is rendered, we can start showing layer updates.
// but if we are blocking render updates (so that a previously cached image is being shown), we wait
// at most e.g. 3 seconds before we start forcing progressive updates.
if ( !mBlockRenderUpdates || mElapsedTimer.elapsed() > MAX_TIME_TO_USE_CACHED_PREVIEW_IMAGE )
{
mReadyToCompose = true;
}
}
}
else
{
QElapsedTimer downloadTimer;
downloadTimer.start();
// Async loading of nodes
QVector<QgsPointCloudBlock *> blocks( nodes.size(), nullptr );
QVector<QgsPointCloudBlockHandle *> blockHandles( nodes.size(), nullptr );
QVector<bool> finishedLoadingBlock( nodes.size(), false );
QTimer timer;
QEventLoop loop;
QObject::connect( &timer, &QTimer::timeout, &loop, &QEventLoop::quit );
// Note: All capture by reference warnings here shouldn't be an issue since we have an event loop, so locals won't be deallocated
auto checkIfFinished = [&]()
{
// If all blocks are loaded, exit the event loop
if ( !finishedLoadingBlock.contains( false ) ) loop.exit();
};
for ( int i = 0; i < nodes.size(); ++i )
{
const IndexedPointCloudNode &n = nodes[i];
QgsPointCloudBlockHandle *blockHandle = pc->asyncNodeData( n, request );
blockHandles[ i ] = blockHandle;
QObject::connect( blockHandle, &QgsPointCloudBlockHandle::blockLoadingSucceeded, [ &, i ]( QgsPointCloudBlock * block )
{
if ( block )
{
blocks[ i ] = block;
}
else
{
qDebug() << "Unable to load node " << n.toString();
}
finishedLoadingBlock[ i ] = true;
checkIfFinished();
} );
QObject::connect( blockHandle, &QgsPointCloudBlockHandle::blockLoadingFailed, [ &, i ]( const QString & errorStr )
{
qDebug() << "Unable to load node " << n.toString() << ", error: " << errorStr;
finishedLoadingBlock[ i ] = true;
checkIfFinished();
} );
}
timer.start( 100000 );
// Wait for all point cloud nodes to finish loading
loop.exec();
qDebug() << "Downloaded in : " << downloadTimer.elapsed() << "ms";
// Render all the point cloud blocks sequentially
for ( int i = 0; i < nodes.size(); ++i )
{
if ( context.renderContext().renderingStopped() )
{
QgsDebugMsgLevel( "canceled", 2 );
canceled = true;
break;
}
if ( !blocks[ i ] )
continue;
context.setAttributes( blocks[ i ]->attributes() );
mRenderer->renderBlock( blocks[ i ], context );
++nodesDrawn;
// as soon as first block is rendered, we can start showing layer updates.
// but if we are blocking render updates (so that a previously cached image is being shown), we wait
// at most e.g. 3 seconds before we start forcing progressive updates.
if ( !mBlockRenderUpdates || mElapsedTimer.elapsed() > MAX_TIME_TO_USE_CACHED_PREVIEW_IMAGE )
{
mReadyToCompose = true;
}
}
for ( int i = 0; i < nodes.size(); ++i )
{
if ( blocks[ i ] )
delete blocks[ i ];
if ( blockHandles[ i ] )
{
blockHandles[ i ]->disconnect();
delete blockHandles[ i ];
}
}
}
#ifdef QGISDEBUG
qDebug() << QStringLiteral( "totals: %1 nodes | %2 points | %3ms" ).arg( nodesDrawn )
.arg( context.pointsRendered() )
.arg( t.elapsed() );
QgsDebugMsgLevel( QStringLiteral( "totals: %1 nodes | %2 points | %3ms" ).arg( nodesDrawn )
.arg( context.pointsRendered() )
.arg( t.elapsed() ), 2 );
#endif
mRenderer->stopRender( context );
mReadyToCompose = true;
return !canceled;
}
bool QgsPointCloudLayerRenderer::forceRasterRender() const
{
// unless we are using the extent only renderer, point cloud layers should always be rasterized -- we don't want to export points as vectors
// to formats like PDF!
return mRenderer ? mRenderer->type() != QLatin1String( "extent" ) : false;
}
void QgsPointCloudLayerRenderer::setLayerRenderingTimeHint( int time )
{
mRenderTimeHint = time;
}
QVector<IndexedPointCloudNode> QgsPointCloudLayerRenderer::traverseTree( const QgsPointCloudIndex *pc,
const QgsRenderContext &context,
IndexedPointCloudNode n,
double maxErrorPixels,
double nodeErrorPixels )
{
QVector<IndexedPointCloudNode> nodes;
if ( context.renderingStopped() )
{
QgsDebugMsgLevel( QStringLiteral( "canceled" ), 2 );
return nodes;
}
if ( !context.extent().intersects( pc->nodeMapExtent( n ) ) )
return nodes;
const QgsDoubleRange nodeZRange = pc->nodeZRange( n );
const QgsDoubleRange adjustedNodeZRange = QgsDoubleRange( nodeZRange.lower() + mZOffset, nodeZRange.upper() + mZOffset );
if ( !context.zRange().isInfinite() && !context.zRange().overlaps( adjustedNodeZRange ) )
return nodes;
nodes.append( n );
double childrenErrorPixels = nodeErrorPixels / 2.0;
if ( childrenErrorPixels < maxErrorPixels )
return nodes;
const QList<IndexedPointCloudNode> children = pc->nodeChildren( n );
for ( const IndexedPointCloudNode &nn : children )
{
nodes += traverseTree( pc, context, nn, maxErrorPixels, childrenErrorPixels );
}
return nodes;
}
QgsPointCloudLayerRenderer::~QgsPointCloudLayerRenderer() = default;