Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix a crash with terrain generation from DEM (fixes #17730)
A typical off by one error - reading data outside of heightmap array
  • Loading branch information
wonder-sk committed Jul 8, 2018
1 parent 031e15e commit d1e4c5e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/3d/terrain/qgsdemterraintilegeometry_p.cpp
Expand Up @@ -101,20 +101,20 @@ static QByteArray createPlaneVertexData( int res, float skirtHeight, const QByte
return bufferBytes;
}

inline int ijToHeightMapIndex( int i, int j, int numVerticesX, int numVerticesZ )
inline int ijToHeightMapIndex( int i, int j, int resX, int resZ )
{
i = qBound( 1, i, numVerticesX - 1 ) - 1;
j = qBound( 1, j, numVerticesZ - 1 ) - 1;
return j * ( numVerticesX - 2 ) + i;
i = qBound( 1, i, resX ) - 1;
j = qBound( 1, j, resZ ) - 1;
return j * resX + i;
}


static bool hasNoData( int i, int j, const float *heightMap, int numVerticesX, int numVerticesZ )
static bool hasNoData( int i, int j, const float *heightMap, int resX, int resZ )
{
return std::isnan( heightMap[ ijToHeightMapIndex( i, j, numVerticesX, numVerticesZ ) ] ) ||
std::isnan( heightMap[ ijToHeightMapIndex( i + 1, j, numVerticesX, numVerticesZ ) ] ) ||
std::isnan( heightMap[ ijToHeightMapIndex( i, j + 1, numVerticesX, numVerticesZ ) ] ) ||
std::isnan( heightMap[ ijToHeightMapIndex( i + 1, j + 1, numVerticesX, numVerticesZ ) ] );
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 ) ] );
}

static QByteArray createPlaneIndexData( int res, const QByteArray &heightMap )
Expand Down Expand Up @@ -142,7 +142,7 @@ static QByteArray createPlaneIndexData( int res, const QByteArray &heightMap )
// Iterate over x
for ( int i = 0; i < numVerticesX - 1; ++i )
{
if ( hasNoData( i, j, heightMapFloat, numVerticesX, numVerticesZ ) )
if ( hasNoData( i, j, heightMapFloat, res, res ) )
{
// at least one corner of the quad has no-data value
// so let's make two invalid triangles
Expand Down

0 comments on commit d1e4c5e

Please sign in to comment.