QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsdemterraintilegeometry_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdemterraintilegeometry_p.cpp
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include <QMatrix4x4>
18
19
20#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
21#include <Qt3DRender/QAttribute>
22#include <Qt3DRender/QBuffer>
23#include <Qt3DRender/QAbstractFunctor>
24typedef Qt3DRender::QAttribute Qt3DQAttribute;
25typedef Qt3DRender::QBuffer Qt3DQBuffer;
26typedef Qt3DRender::QAbstractFunctor Qt3DQAbstractFunctor;
27#else
28#include <Qt3DCore/QAttribute>
29#include <Qt3DCore/QBuffer>
30#include <Qt3DCore/QAbstractFunctor>
31typedef Qt3DCore::QAttribute Qt3DQAttribute;
32typedef Qt3DCore::QBuffer Qt3DQBuffer;
33typedef Qt3DCore::QAbstractFunctor Qt3DQAbstractFunctor;
34#endif
35#include <limits>
36#include <cmath>
38#include "qgis.h"
39
41
42using namespace Qt3DRender;
43
44static QByteArray createPlaneVertexData( int res, float side, float vertScale, float skirtHeight, const QByteArray &heights )
45{
46 Q_ASSERT( res >= 2 );
47 Q_ASSERT( heights.count() == res * res * static_cast<int>( sizeof( float ) ) );
48
49 const float *zData = ( const float * ) heights.constData();
50 const float *zBits = zData;
51
52 const int nVerts = ( res + 2 ) * ( res + 2 );
53
54 // Populate a buffer with the interleaved per-vertex data with
55 // vec3 pos, vec2 texCoord, vec3 normal, vec4 tangent
56 const quint32 elementSize = 3 + 2 + 3;
57 const quint32 stride = elementSize * sizeof( float );
58 QByteArray bufferBytes;
59 bufferBytes.resize( stride * nVerts );
60 float *fptr = reinterpret_cast<float *>( bufferBytes.data() );
61
62 float w = 1, h = 1;
63 QSize resolution( res, res );
64 const float x0 = -w / 2.0f;
65 const float z0 = -h / 2.0f;
66 const float dx = w / ( resolution.width() - 1 );
67 const float dz = h / ( resolution.height() - 1 );
68 const float du = 1.0 / ( resolution.width() - 1 );
69 const float dv = 1.0 / ( resolution.height() - 1 );
70
71 // the height of vertices with no-data value... the value should not really matter
72 // as we do not create valid triangles that would use such vertices
73 const float noDataHeight = 0;
74
75 const int iMax = resolution.width() - 1;
76 const int jMax = resolution.height() - 1;
77
78 // Iterate over z
79 for ( int j = -1; j <= resolution.height(); ++j )
80 {
81 int jBound = std::clamp( j, 0, jMax );
82 const float z = z0 + static_cast<float>( jBound ) * dz;
83 const float v = static_cast<float>( jBound ) * dv;
84
85 // Iterate over x
86 for ( int i = -1; i <= resolution.width(); ++i )
87 {
88 int iBound = std::clamp( i, 0, iMax );
89 const float x = x0 + static_cast<float>( iBound ) * dx;
90 const float u = static_cast<float>( iBound ) * du;
91
92 float height;
93 if ( i == iBound && j == jBound )
94 height = *zBits++;
95 else
96 height = zData[ jBound * resolution.width() + iBound ] - skirtHeight;
97
98 if ( std::isnan( height ) )
99 height = noDataHeight;
100
101 // position
102 *fptr++ = x;
103 *fptr++ = height / side * vertScale;
104 *fptr++ = z;
105
106 // texture coordinates
107 *fptr++ = u;
108 *fptr++ = v;
109
110 // calculate normal coordinates
111#define zAt( ii, jj ) zData[ jj * resolution.width() + ii ] * vertScale
112 float zi0 = zAt( std::clamp( i - 1, 0, iMax ), jBound );
113 float zi1 = zAt( std::clamp( i + 1, 0, iMax ), jBound );
114 float zj0 = zAt( iBound, std::clamp( j - 1, 0, jMax ) );
115 float zj1 = zAt( iBound, std::clamp( j + 1, 0, jMax ) );
116
117 QVector3D n;
118 if ( std::isnan( zi0 ) || std::isnan( zi1 ) || std::isnan( zj0 ) || std::isnan( zj1 ) )
119 n = QVector3D( 0, 1, 0 );
120 else
121 {
122 float di, dj;
123 float zij = height * vertScale;
124
125 if ( i == 0 )
126 di = 2 * ( zij - zi1 );
127 else if ( i == iMax )
128 di = 2 * ( zi0 - zij );
129 else
130 di = zi0 - zi1;
131
132 if ( j == 0 )
133 dj = 2 * ( zij - zj1 );
134 else if ( j == jMax )
135 dj = 2 * ( zj0 - zij );
136 else
137 dj = zj0 - zj1;
138
139 n = QVector3D( di, 2 * side / res, dj );
140 n.normalize();
141 }
142
143 *fptr++ = n.x();
144 *fptr++ = n.y();
145 *fptr++ = n.z();
146 }
147 }
148
149 return bufferBytes;
150}
151
152inline int ijToHeightMapIndex( int i, int j, int resX, int resZ )
153{
154 i = std::clamp( i, 1, resX ) - 1;
155 j = std::clamp( j, 1, resZ ) - 1;
156 return j * resX + i;
157}
158
159static bool hasNoData( int i, int j, const float *heightMap, int resX, int resZ )
160{
161 return std::isnan( heightMap[ ijToHeightMapIndex( i, j, resX, resZ ) ] ) ||
162 std::isnan( heightMap[ ijToHeightMapIndex( i + 1, j, resX, resZ ) ] ) ||
163 std::isnan( heightMap[ ijToHeightMapIndex( i, j + 1, resX, resZ ) ] ) ||
164 std::isnan( heightMap[ ijToHeightMapIndex( i + 1, j + 1, resX, resZ ) ] );
165}
166
167static QByteArray createPlaneIndexData( int res, const QByteArray &heightMap )
168{
169 QSize resolution( res, res );
170 int numVerticesX = resolution.width() + 2;
171 int numVerticesZ = resolution.height() + 2;
172
173 // Create the index data. 2 triangles per rectangular face
174 const int faces = 2 * ( numVerticesX - 1 ) * ( numVerticesZ - 1 );
175 const quint32 indices = 3 * faces;
176 Q_ASSERT( indices < std::numeric_limits<quint32>::max() );
177 QByteArray indexBytes;
178 indexBytes.resize( indices * sizeof( quint32 ) );
179 quint32 *indexPtr = reinterpret_cast<quint32 *>( indexBytes.data() );
180
181 const float *heightMapFloat = reinterpret_cast<const float *>( heightMap.constData() );
182
183 // Iterate over z
184 for ( int j = 0; j < numVerticesZ - 1; ++j )
185 {
186 const int rowStartIndex = j * numVerticesX;
187 const int nextRowStartIndex = ( j + 1 ) * numVerticesX;
188
189 // Iterate over x
190 for ( int i = 0; i < numVerticesX - 1; ++i )
191 {
192 if ( hasNoData( i, j, heightMapFloat, res, res ) )
193 {
194 // at least one corner of the quad has no-data value
195 // so let's make two invalid triangles
196 *indexPtr++ = rowStartIndex + i;
197 *indexPtr++ = rowStartIndex + i;
198 *indexPtr++ = rowStartIndex + i;
199
200 *indexPtr++ = rowStartIndex + i;
201 *indexPtr++ = rowStartIndex + i;
202 *indexPtr++ = rowStartIndex + i;
203 continue;
204 }
205
206 // Split quad into two triangles
207 *indexPtr++ = rowStartIndex + i;
208 *indexPtr++ = nextRowStartIndex + i;
209 *indexPtr++ = rowStartIndex + i + 1;
210
211 *indexPtr++ = nextRowStartIndex + i;
212 *indexPtr++ = nextRowStartIndex + i + 1;
213 *indexPtr++ = rowStartIndex + i + 1;
214 }
215 }
216
217 return indexBytes;
218}
219
220// QAbstractFunctor marked as deprecated in 5.15, but undeprecated for Qt 6.0. TODO -- remove when we require 6.0
222
224class PlaneVertexBufferFunctor : public Qt3DQAbstractFunctor
225{
226 public:
227 explicit PlaneVertexBufferFunctor( int resolution, float side, float vertScale, float skirtHeight, const QByteArray &heightMap )
228 : mResolution( resolution )
229 , mSide( side )
230 , mVertScale( vertScale )
231 , mSkirtHeight( skirtHeight )
232 , mHeightMap( heightMap )
233 {}
234
235 QByteArray operator()()
236 {
237 return createPlaneVertexData( mResolution, mSide, mVertScale, mSkirtHeight, mHeightMap );
238 }
239
240 bool operator ==( const Qt3DQAbstractFunctor &other ) const
241 {
242 const PlaneVertexBufferFunctor *otherFunctor = functor_cast<PlaneVertexBufferFunctor>( &other );
243 if ( otherFunctor != nullptr )
244 return ( otherFunctor->mResolution == mResolution &&
245 otherFunctor->mSide == mSide &&
246 otherFunctor->mVertScale == mVertScale &&
247 otherFunctor->mSkirtHeight == mSkirtHeight &&
248 otherFunctor->mHeightMap == mHeightMap );
249 return false;
250 }
251
252 QT3D_FUNCTOR( PlaneVertexBufferFunctor )
253
254 private:
255 int mResolution;
256 float mSide;
257 float mVertScale;
258 float mSkirtHeight;
259 QByteArray mHeightMap;
260};
261
262
264class PlaneIndexBufferFunctor: public Qt3DQAbstractFunctor
265{
266 public:
267 explicit PlaneIndexBufferFunctor( int resolution, const QByteArray &heightMap )
268 : mResolution( resolution )
269 , mHeightMap( heightMap )
270 {}
271
272 QByteArray operator()()
273 {
274 return createPlaneIndexData( mResolution, mHeightMap );
275 }
276
277 bool operator ==( const Qt3DQAbstractFunctor &other ) const
278 {
279 const PlaneIndexBufferFunctor *otherFunctor = functor_cast<PlaneIndexBufferFunctor>( &other );
280 if ( otherFunctor != nullptr )
281 return ( otherFunctor->mResolution == mResolution );
282 return false;
283 }
284
285 QT3D_FUNCTOR( PlaneIndexBufferFunctor )
286
287 private:
288 int mResolution;
289 QByteArray mHeightMap;
290};
291
293
294// ------------
295
296
297DemTerrainTileGeometry::DemTerrainTileGeometry( int resolution, float side, float vertScale, float skirtHeight, const QByteArray &heightMap, DemTerrainTileGeometry::QNode *parent )
298 : QGeometry( parent )
299 , mResolution( resolution )
300 , mSide( side )
301 , mVertScale( vertScale )
302 , mSkirtHeight( skirtHeight )
303 , mHeightMap( heightMap )
304{
305 init();
306}
307
308static bool intersectionDemTriangles( const QByteArray &vertexBuf, const QByteArray &indexBuf, const QgsRayCastingUtils::Ray3D &r, const QMatrix4x4 &worldTransform, QVector3D &intPt )
309{
310 // WARNING! this code is specific to how vertex buffers are built for DEM tiles,
311 // it is not usable for any mesh...
312
313 const float *vertices = reinterpret_cast<const float *>( vertexBuf.constData() );
314 const uint *indices = reinterpret_cast<const uint *>( indexBuf.constData() );
315#ifdef QGISDEBUG
316 int vertexCnt = vertexBuf.count() / sizeof( float );
317 Q_ASSERT( vertexCnt % 8 == 0 );
318#endif
319 int indexCnt = indexBuf.count() / sizeof( uint );
320 Q_ASSERT( indexCnt % 3 == 0 );
321 int triangleCount = indexCnt / 3;
322
323 QVector3D intersectionPt, minIntersectionPt;
324 float distance;
325 float minDistance = -1;
326
327 for ( int i = 0; i < triangleCount; ++i )
328 {
329 int v0 = indices[i * 3], v1 = indices[i * 3 + 1], v2 = indices[i * 3 + 2];
330 QVector3D a( vertices[v0 * 8], vertices[v0 * 8 + 1], vertices[v0 * 8 + 2] );
331 QVector3D b( vertices[v1 * 8], vertices[v1 * 8 + 1], vertices[v1 * 8 + 2] );
332 QVector3D c( vertices[v2 * 8], vertices[v2 * 8 + 1], vertices[v2 * 8 + 2] );
333
334 const QVector3D tA = worldTransform * a;
335 const QVector3D tB = worldTransform * b;
336 const QVector3D tC = worldTransform * c;
337
338 QVector3D uvw;
339 float t = 0;
340 if ( QgsRayCastingUtils::rayTriangleIntersection( r, tA, tB, tC, uvw, t ) )
341 {
342 intersectionPt = r.point( t * r.distance() );
343 distance = r.projectedDistance( intersectionPt );
344
345 // we only want the first intersection of the ray with the mesh (closest to the ray origin)
346 if ( minDistance == -1 || distance < minDistance )
347 {
348 minDistance = distance;
349 minIntersectionPt = intersectionPt;
350 }
351 }
352 }
353
354 if ( minDistance != -1 )
355 {
356 intPt = minIntersectionPt;
357 return true;
358 }
359 else
360 return false;
361}
362
363bool DemTerrainTileGeometry::rayIntersection( const QgsRayCastingUtils::Ray3D &ray, const QMatrix4x4 &worldTransform, QVector3D &intersectionPoint )
364{
365 return intersectionDemTriangles( mVertexBuffer->data(), mIndexBuffer->data(), ray, worldTransform, intersectionPoint );
366}
367
368void DemTerrainTileGeometry::init()
369{
370 mPositionAttribute = new Qt3DQAttribute( this );
371 mNormalAttribute = new Qt3DQAttribute( this );
372 mTexCoordAttribute = new Qt3DQAttribute( this );
373 mIndexAttribute = new Qt3DQAttribute( this );
374 mVertexBuffer = new Qt3DQBuffer( this );
375 mIndexBuffer = new Qt3DQBuffer( this );
376
377 int nVertsX = mResolution + 2;
378 int nVertsZ = mResolution + 2;
379 const int nVerts = nVertsX * nVertsZ;
380 const int stride = ( 3 + 2 + 3 ) * sizeof( float );
381 const int faces = 2 * ( nVertsX - 1 ) * ( nVertsZ - 1 );
382
383 mPositionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
384 mPositionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
385 mPositionAttribute->setVertexSize( 3 );
386 mPositionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
387 mPositionAttribute->setBuffer( mVertexBuffer );
388 mPositionAttribute->setByteStride( stride );
389 mPositionAttribute->setCount( nVerts );
390
391 mTexCoordAttribute->setName( Qt3DQAttribute::defaultTextureCoordinateAttributeName() );
392 mTexCoordAttribute->setVertexBaseType( Qt3DQAttribute::Float );
393 mTexCoordAttribute->setVertexSize( 2 );
394 mTexCoordAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
395 mTexCoordAttribute->setBuffer( mVertexBuffer );
396 mTexCoordAttribute->setByteStride( stride );
397 mTexCoordAttribute->setByteOffset( 3 * sizeof( float ) );
398 mTexCoordAttribute->setCount( nVerts );
399
400 mNormalAttribute->setName( Qt3DQAttribute::defaultNormalAttributeName() );
401 mNormalAttribute->setVertexBaseType( Qt3DQAttribute::Float );
402 mNormalAttribute->setVertexSize( 3 );
403 mNormalAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
404 mNormalAttribute->setBuffer( mVertexBuffer );
405 mNormalAttribute->setByteStride( stride );
406 mNormalAttribute->setByteOffset( 5 * sizeof( float ) );
407 mNormalAttribute->setCount( nVerts );
408
409 mIndexAttribute->setAttributeType( Qt3DQAttribute::IndexAttribute );
410 mIndexAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedInt );
411 mIndexAttribute->setBuffer( mIndexBuffer );
412
413 // Each primitive has 3 vertives
414 mIndexAttribute->setCount( faces * 3 );
415
416 // switched to setting data instead of just setting data generators because we also need the buffers
417 // available for ray-mesh intersections and we can't access the private copy of data in Qt (if there is any)
418
419 mVertexBuffer->setData( PlaneVertexBufferFunctor( mResolution, mSide, mVertScale, mSkirtHeight, mHeightMap )() );
420 mIndexBuffer->setData( PlaneIndexBufferFunctor( mResolution, mHeightMap )() );
421
422 addAttribute( mPositionAttribute );
423 addAttribute( mTexCoordAttribute );
424 addAttribute( mNormalAttribute );
425 addAttribute( mIndexAttribute );
426}
427
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:4572
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:4571
Qt3DCore::QAbstractFunctor Qt3DQAbstractFunctor
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)