17#include "moc_qgsdemterraintilegeometry_p.cpp"
21#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
22#include <Qt3DRender/QAttribute>
23#include <Qt3DRender/QBuffer>
24#include <Qt3DRender/QAbstractFunctor>
29#include <Qt3DCore/QAttribute>
30#include <Qt3DCore/QBuffer>
31#include <Qt3DCore/QAbstractFunctor>
45static QByteArray createPlaneVertexData(
int res,
float side,
float vertScale,
float skirtHeight,
const QByteArray &heights )
48 Q_ASSERT( heights.count() == res * res *
static_cast<int>(
sizeof(
float ) ) );
50 const float *zData = (
const float * ) heights.constData();
51 const float *zBits = zData;
53 const int nVerts = ( res + 2 ) * ( res + 2 );
57 const quint32 elementSize = 3 + 2 + 3;
58 const quint32 stride = elementSize *
sizeof( float );
59 QByteArray bufferBytes;
60 bufferBytes.resize( stride * nVerts );
61 float *fptr =
reinterpret_cast<float *
>( bufferBytes.data() );
63 QSize resolution( res, res );
65 const float y0 = side;
66 const float dx = side /
static_cast<float>( resolution.width() - 1 );
67 const float dy = side /
static_cast<float>( resolution.height() - 1 );
68 const float du = 1.0f /
static_cast<float>( resolution.width() - 1 );
69 const float dv = 1.0f /
static_cast<float>( resolution.height() - 1 );
73 const float noDataHeight = 0;
75 const int iMax = resolution.width() - 1;
76 const int jMax = resolution.height() - 1;
79 for (
int j = -1; j <= resolution.height(); ++j )
81 int jBound = std::clamp( j, 0, jMax );
82 const float y = y0 -
static_cast<float>( jBound ) * dy;
83 const float v =
static_cast<float>( jBound ) * dv;
86 for (
int i = -1; i <= resolution.width(); ++i )
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;
93 if ( i == iBound && j == jBound )
96 height = zData[jBound * resolution.width() + iBound] - skirtHeight;
98 if ( std::isnan( height ) )
99 height = noDataHeight;
104 *fptr++ = height * vertScale;
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 ) );
118 if ( std::isnan( zi0 ) || std::isnan( zi1 ) || std::isnan( zj0 ) || std::isnan( zj1 ) )
119 n = QVector3D( 0, 0, 1 );
123 float zij = height * vertScale;
126 di = 2 * ( zij - zi1 );
127 else if ( i == iMax )
128 di = 2 * ( zi0 - zij );
133 dj = 2 * ( zij - zj1 );
134 else if ( j == jMax )
135 dj = 2 * ( zj0 - zij );
139 n = QVector3D( di, -dj, 2 * side /
static_cast<float>( res ) );
152inline int ijToHeightMapIndex(
int i,
int j,
int resX,
int resZ )
154 i = std::clamp( i, 1, resX ) - 1;
155 j = std::clamp( j, 1, resZ ) - 1;
159static bool hasNoData(
int i,
int j,
const float *heightMap,
int resX,
int resZ )
161 return std::isnan( heightMap[ijToHeightMapIndex( i, j, resX, resZ )] ) || std::isnan( heightMap[ijToHeightMapIndex( i + 1, j, resX, resZ )] ) || std::isnan( heightMap[ijToHeightMapIndex( i, j + 1, resX, resZ )] ) || std::isnan( heightMap[ijToHeightMapIndex( i + 1, j + 1, resX, resZ )] );
164static QByteArray createPlaneIndexData(
int res,
const QByteArray &heightMap )
166 QSize resolution( res, res );
167 int numVerticesX = resolution.width() + 2;
168 int numVerticesZ = resolution.height() + 2;
171 const int faces = 2 * ( numVerticesX - 1 ) * ( numVerticesZ - 1 );
172 const quint32 indices = 3 * faces;
173 Q_ASSERT( indices < std::numeric_limits<quint32>::max() );
174 QByteArray indexBytes;
175 indexBytes.resize( indices *
sizeof( quint32 ) );
176 quint32 *indexPtr =
reinterpret_cast<quint32 *
>( indexBytes.data() );
178 const float *heightMapFloat =
reinterpret_cast<const float *
>( heightMap.constData() );
181 for (
int j = 0; j < numVerticesZ - 1; ++j )
183 const int rowStartIndex = j * numVerticesX;
184 const int nextRowStartIndex = ( j + 1 ) * numVerticesX;
187 for (
int i = 0; i < numVerticesX - 1; ++i )
189 if ( hasNoData( i, j, heightMapFloat, res, res ) )
193 *indexPtr++ = rowStartIndex + i;
194 *indexPtr++ = rowStartIndex + i;
195 *indexPtr++ = rowStartIndex + i;
197 *indexPtr++ = rowStartIndex + i;
198 *indexPtr++ = rowStartIndex + i;
199 *indexPtr++ = rowStartIndex + i;
204 *indexPtr++ = rowStartIndex + i;
205 *indexPtr++ = nextRowStartIndex + i;
206 *indexPtr++ = rowStartIndex + i + 1;
208 *indexPtr++ = nextRowStartIndex + i;
209 *indexPtr++ = nextRowStartIndex + i + 1;
210 *indexPtr++ = rowStartIndex + i + 1;
224 explicit PlaneVertexBufferFunctor(
int resolution,
float side,
float vertScale,
float skirtHeight,
const QByteArray &heightMap )
225 : mResolution( resolution )
227 , mVertScale( vertScale )
228 , mSkirtHeight( skirtHeight )
229 , mHeightMap( heightMap )
232 QByteArray operator()()
234 return createPlaneVertexData( mResolution, mSide, mVertScale, mSkirtHeight, mHeightMap );
239 const PlaneVertexBufferFunctor *otherFunctor = functor_cast<PlaneVertexBufferFunctor>( &other );
241 return ( otherFunctor->mResolution == mResolution && otherFunctor->mSide == mSide && otherFunctor->mVertScale == mVertScale && otherFunctor->mSkirtHeight == mSkirtHeight && otherFunctor->mHeightMap == mHeightMap );
245 QT3D_FUNCTOR( PlaneVertexBufferFunctor )
252 QByteArray mHeightMap;
260 explicit PlaneIndexBufferFunctor(
int resolution,
const QByteArray &heightMap )
261 : mResolution( resolution )
262 , mHeightMap( heightMap )
265 QByteArray operator()()
267 return createPlaneIndexData( mResolution, mHeightMap );
272 const PlaneIndexBufferFunctor *otherFunctor = functor_cast<PlaneIndexBufferFunctor>( &other );
274 return ( otherFunctor->mResolution == mResolution );
278 QT3D_FUNCTOR( PlaneIndexBufferFunctor )
282 QByteArray mHeightMap;
290DemTerrainTileGeometry::DemTerrainTileGeometry(
int resolution,
float side,
float vertScale,
float skirtHeight,
const QByteArray &heightMap, DemTerrainTileGeometry::QNode *parent )
291 : QGeometry( parent )
292 , mResolution( resolution )
294 , mVertScale( vertScale )
295 , mSkirtHeight( skirtHeight )
296 , mHeightMap( heightMap )
301static bool intersectionDemTriangles(
const QByteArray &vertexBuf,
const QByteArray &indexBuf,
const QgsRayCastingUtils::Ray3D &r,
const QMatrix4x4 &worldTransform, QVector3D &intPt )
306 const float *vertices =
reinterpret_cast<const float *
>( vertexBuf.constData() );
307 const uint *indices =
reinterpret_cast<const uint *
>( indexBuf.constData() );
309 int vertexCnt = vertexBuf.count() /
sizeof( float );
310 Q_ASSERT( vertexCnt % 8 == 0 );
312 int indexCnt = indexBuf.count() /
sizeof( uint );
313 Q_ASSERT( indexCnt % 3 == 0 );
314 int triangleCount = indexCnt / 3;
316 QVector3D intersectionPt, minIntersectionPt;
318 float minDistance = -1;
320 for (
int i = 0; i < triangleCount; ++i )
322 int v0 = indices[i * 3], v1 = indices[i * 3 + 1], v2 = indices[i * 3 + 2];
323 QVector3D a( vertices[v0 * 8], vertices[v0 * 8 + 1], vertices[v0 * 8 + 2] );
324 QVector3D b( vertices[v1 * 8], vertices[v1 * 8 + 1], vertices[v1 * 8 + 2] );
325 QVector3D
c( vertices[v2 * 8], vertices[v2 * 8 + 1], vertices[v2 * 8 + 2] );
327 const QVector3D tA = worldTransform * a;
328 const QVector3D tB = worldTransform * b;
329 const QVector3D tC = worldTransform *
c;
333 if ( QgsRayCastingUtils::rayTriangleIntersection( r, tA, tB, tC, uvw, t ) )
335 intersectionPt = r.point( t * r.distance() );
336 distance = r.projectedDistance( intersectionPt );
339 if ( minDistance == -1 || distance < minDistance )
341 minDistance = distance;
342 minIntersectionPt = intersectionPt;
347 if ( minDistance != -1 )
349 intPt = minIntersectionPt;
356bool DemTerrainTileGeometry::rayIntersection(
const QgsRayCastingUtils::Ray3D &ray,
const QMatrix4x4 &worldTransform, QVector3D &intersectionPoint )
358 return intersectionDemTriangles( mVertexBuffer->data(), mIndexBuffer->data(), ray, worldTransform, intersectionPoint );
361void DemTerrainTileGeometry::init()
370 int nVertsX = mResolution + 2;
371 int nVertsZ = mResolution + 2;
372 const int nVerts = nVertsX * nVertsZ;
373 const int stride = ( 3 + 2 + 3 ) *
sizeof(
float );
374 const int faces = 2 * ( nVertsX - 1 ) * ( nVertsZ - 1 );
376 mPositionAttribute->setName( Qt3DQAttribute::defaultPositionAttributeName() );
377 mPositionAttribute->setVertexBaseType( Qt3DQAttribute::Float );
378 mPositionAttribute->setVertexSize( 3 );
379 mPositionAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
380 mPositionAttribute->setBuffer( mVertexBuffer );
381 mPositionAttribute->setByteStride( stride );
382 mPositionAttribute->setCount( nVerts );
384 mTexCoordAttribute->setName( Qt3DQAttribute::defaultTextureCoordinateAttributeName() );
385 mTexCoordAttribute->setVertexBaseType( Qt3DQAttribute::Float );
386 mTexCoordAttribute->setVertexSize( 2 );
387 mTexCoordAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
388 mTexCoordAttribute->setBuffer( mVertexBuffer );
389 mTexCoordAttribute->setByteStride( stride );
390 mTexCoordAttribute->setByteOffset( 3 *
sizeof(
float ) );
391 mTexCoordAttribute->setCount( nVerts );
393 mNormalAttribute->setName( Qt3DQAttribute::defaultNormalAttributeName() );
394 mNormalAttribute->setVertexBaseType( Qt3DQAttribute::Float );
395 mNormalAttribute->setVertexSize( 3 );
396 mNormalAttribute->setAttributeType( Qt3DQAttribute::VertexAttribute );
397 mNormalAttribute->setBuffer( mVertexBuffer );
398 mNormalAttribute->setByteStride( stride );
399 mNormalAttribute->setByteOffset( 5 *
sizeof(
float ) );
400 mNormalAttribute->setCount( nVerts );
402 mIndexAttribute->setAttributeType( Qt3DQAttribute::IndexAttribute );
403 mIndexAttribute->setVertexBaseType( Qt3DQAttribute::UnsignedInt );
404 mIndexAttribute->setBuffer( mIndexBuffer );
407 mIndexAttribute->setCount( faces * 3 );
412 mVertexBuffer->setData( PlaneVertexBufferFunctor( mResolution, mSide, mVertScale, mSkirtHeight, mHeightMap )() );
413 mIndexBuffer->setData( PlaneIndexBufferFunctor( mResolution, mHeightMap )() );
415 addAttribute( mPositionAttribute );
416 addAttribute( mTexCoordAttribute );
417 addAttribute( mNormalAttribute );
418 addAttribute( mIndexAttribute );
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
#define Q_NOWARN_DEPRECATED_PUSH
Qt3DCore::QAbstractFunctor Qt3DQAbstractFunctor
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)