Skip to content

Commit

Permalink
Chunk node refactor: remove tileX(), tileY(), tileZ()
Browse files Browse the repository at this point in the history
Let's use tileId() everywhere for chunk coordinates
  • Loading branch information
wonder-sk authored and nyalldawson committed Oct 26, 2020
1 parent 8b15cfb commit 47ba499
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/3d/chunks/qgschunkedentity_p.cpp
Expand Up @@ -77,7 +77,7 @@ QgsChunkedEntity::QgsChunkedEntity( const QgsAABB &rootBbox, float rootError, fl
, mChunkLoaderFactory( loaderFactory )
, mOwnsFactory( ownsFactory )
{
mRootNode = new QgsChunkNode( 0, 0, 0, rootBbox, rootError );
mRootNode = new QgsChunkNode( QgsChunkNodeId( 0, 0, 0 ), rootBbox, rootError );
mChunkLoaderQueue = new QgsChunkList;
mReplacementQueue = new QgsChunkList;
}
Expand Down
14 changes: 6 additions & 8 deletions src/3d/chunks/qgschunknode_p.cpp
Expand Up @@ -22,12 +22,10 @@

///@cond PRIVATE

QgsChunkNode::QgsChunkNode( int x, int y, int z, const QgsAABB &bbox, float error, QgsChunkNode *parent )
QgsChunkNode::QgsChunkNode( const QgsChunkNodeId &nodeId, const QgsAABB &bbox, float error, QgsChunkNode *parent )
: mBbox( bbox )
, mError( error )
, mTileX( x )
, mTileY( y )
, mTileZ( z )
, mNodeId( nodeId )
, mParent( parent )
, mState( Skeleton )
, mLoaderQueueEntry( nullptr )
Expand Down Expand Up @@ -77,16 +75,16 @@ void QgsChunkNode::ensureAllChildrenExist()
float ymax = mBbox.yMax;

if ( !mChildren[0] )
mChildren[0] = new QgsChunkNode( mTileX * 2 + 0, mTileY * 2 + 1, mTileZ + 1, QgsAABB( mBbox.xMin, ymin, mBbox.zMin, xc, ymax, zc ), childError, this );
mChildren[0] = new QgsChunkNode( QgsChunkNodeId( mNodeId.x * 2 + 0, mNodeId.y * 2 + 1, mNodeId.z + 1 ), QgsAABB( mBbox.xMin, ymin, mBbox.zMin, xc, ymax, zc ), childError, this );

if ( !mChildren[1] )
mChildren[1] = new QgsChunkNode( mTileX * 2 + 0, mTileY * 2 + 0, mTileZ + 1, QgsAABB( mBbox.xMin, ymin, zc, xc, ymax, mBbox.zMax ), childError, this );
mChildren[1] = new QgsChunkNode( QgsChunkNodeId( mNodeId.x * 2 + 0, mNodeId.y * 2 + 0, mNodeId.z + 1 ), QgsAABB( mBbox.xMin, ymin, zc, xc, ymax, mBbox.zMax ), childError, this );

if ( !mChildren[2] )
mChildren[2] = new QgsChunkNode( mTileX * 2 + 1, mTileY * 2 + 1, mTileZ + 1, QgsAABB( xc, ymin, mBbox.zMin, mBbox.xMax, ymax, zc ), childError, this );
mChildren[2] = new QgsChunkNode( QgsChunkNodeId( mNodeId.x * 2 + 1, mNodeId.y * 2 + 1, mNodeId.z + 1 ), QgsAABB( xc, ymin, mBbox.zMin, mBbox.xMax, ymax, zc ), childError, this );

if ( !mChildren[3] )
mChildren[3] = new QgsChunkNode( mTileX * 2 + 1, mTileY * 2 + 0, mTileZ + 1, QgsAABB( xc, ymin, zc, mBbox.xMax, ymax, mBbox.zMax ), childError, this );
mChildren[3] = new QgsChunkNode( QgsChunkNodeId( mNodeId.x * 2 + 1, mNodeId.y * 2 + 0, mNodeId.z + 1 ), QgsAABB( xc, ymin, zc, mBbox.xMax, ymax, mBbox.zMax ), childError, this );
}

int QgsChunkNode::level() const
Expand Down
12 changes: 3 additions & 9 deletions src/3d/chunks/qgschunknode_p.h
Expand Up @@ -75,7 +75,7 @@ class QgsChunkNode
{
public:
//! constructs a skeleton chunk
QgsChunkNode( int tileX, int tileY, int tileZ, const QgsAABB &bbox, float error, QgsChunkNode *parent = nullptr );
QgsChunkNode( const QgsChunkNodeId &nodeId, const QgsAABB &bbox, float error, QgsChunkNode *parent = nullptr );

~QgsChunkNode();

Expand Down Expand Up @@ -111,14 +111,8 @@ class QgsChunkNode
QgsAABB bbox() const { return mBbox; }
//! Returns measure geometric/texture error of the chunk (in world coordinates)
float error() const { return mError; }
//! Returns chunk tile X coordinate of the tiling scheme
int tileX() const { return mTileX; }
//! Returns chunk tile Y coordinate of the tiling scheme
int tileY() const { return mTileY; }
//! Returns chunk tile Z coordinate of the tiling scheme
int tileZ() const { return mTileZ; }
//! Returns chunk tile coordinates of the tiling scheme
QgsChunkNodeId tileId() const { return QgsChunkNodeId( mTileX, mTileY, mTileZ ); }
QgsChunkNodeId tileId() const { return mNodeId; }
//! Returns pointer to the parent node. Parent is NULLPTR in the root node
QgsChunkNode *parent() const { return mParent; }
//! Returns array of the four children. Children may be NULLPTR if they were not created yet
Expand Down Expand Up @@ -198,7 +192,7 @@ class QgsChunkNode
QgsAABB mBbox; //!< Bounding box in world coordinates
float mError; //!< Error of the node in world coordinates (negative error means that chunk at this level has no data, but there may be children that do)

int mTileX, mTileY, mTileZ; //!< Chunk coordinates (for use with a tiling scheme)
QgsChunkNodeId mNodeId; //!< Chunk coordinates (for use with a tiling scheme)

QgsChunkNode *mParent; //!< TODO: should be shared pointer
QgsChunkNode *mChildren[4]; //!< TODO: should be weak pointers. May be nullptr if not created yet or removed already
Expand Down
17 changes: 8 additions & 9 deletions src/3d/terrain/qgsdemterraintileloader_p.cpp
Expand Up @@ -77,7 +77,7 @@ QgsDemTerrainTileLoader::QgsDemTerrainTileLoader( QgsTerrainEntity *terrain, Qgs

// get heightmap asynchronously
connect( heightMapGenerator, &QgsDemHeightMapGenerator::heightMapReady, this, &QgsDemTerrainTileLoader::onHeightMapReady );
mHeightMapJobId = heightMapGenerator->render( node->tileX(), node->tileY(), node->tileZ() );
mHeightMapJobId = heightMapGenerator->render( node->tileId() );
mResolution = heightMapGenerator->resolution();
}

Expand All @@ -93,14 +93,15 @@ Qt3DCore::QEntity *QgsDemTerrainTileLoader::createEntity( Qt3DCore::QEntity *par
}

const Qgs3DMapSettings &map = terrain()->map3D();
QgsRectangle extent = map.terrainGenerator()->tilingScheme().tileToExtent( mNode->tileX(), mNode->tileY(), mNode->tileZ() ); //node->extent;
QgsChunkNodeId nodeId = mNode->tileId();
QgsRectangle extent = map.terrainGenerator()->tilingScheme().tileToExtent( nodeId.x, nodeId.y, nodeId.z );
double x0 = extent.xMinimum() - map.origin().x();
double y0 = extent.yMinimum() - map.origin().y();
double side = extent.width();
double half = side / 2;


QgsTerrainTileEntity *entity = new QgsTerrainTileEntity( mNode->tileId() );
QgsTerrainTileEntity *entity = new QgsTerrainTileEntity( nodeId );

// create geometry renderer

Expand Down Expand Up @@ -209,14 +210,12 @@ static QByteArray _readOnlineDtm( QgsTerrainDownloader *downloader, const QgsRec
return downloader->getHeightMap( extent, res, destCrs );
}

int QgsDemHeightMapGenerator::render( int x, int y, int z )
int QgsDemHeightMapGenerator::render( const QgsChunkNodeId &nodeId )
{
QgsChunkNodeId tileId( x, y, z );

QgsEventTracing::addEvent( QgsEventTracing::AsyncBegin, QStringLiteral( "3D" ), QStringLiteral( "DEM" ), tileId.text() );
QgsEventTracing::addEvent( QgsEventTracing::AsyncBegin, QStringLiteral( "3D" ), QStringLiteral( "DEM" ), nodeId.text() );

// extend the rect by half-pixel on each side? to get the values in "corners"
QgsRectangle extent = mTilingScheme.tileToExtent( x, y, z );
QgsRectangle extent = mTilingScheme.tileToExtent( nodeId.x, nodeId.y, nodeId.z );
float mapUnitsPerPixel = extent.width() / mResolution;
extent.grow( mapUnitsPerPixel / 2 );
// but make sure not to go beyond the full extent (returns invalid values)
Expand All @@ -225,7 +224,7 @@ int QgsDemHeightMapGenerator::render( int x, int y, int z )

JobData jd;
jd.jobId = ++mLastJobId;
jd.tileId = tileId;
jd.tileId = nodeId;
jd.extent = extent;
jd.timer.start();
// make a clone of the data provider so it is safe to use in worker thread
Expand Down
2 changes: 1 addition & 1 deletion src/3d/terrain/qgsdemterraintileloader_p.h
Expand Up @@ -90,7 +90,7 @@ class QgsDemHeightMapGenerator : public QObject
~QgsDemHeightMapGenerator() override;

//! asynchronous terrain read for a tile (array of floats)
int render( int x, int y, int z );
int render( const QgsChunkNodeId &nodeId );

//! Waits for the tile to finish rendering
void waitForFinished();
Expand Down
7 changes: 4 additions & 3 deletions src/3d/terrain/qgsterraintileloader_p.cpp
Expand Up @@ -38,6 +38,7 @@ QgsTerrainTileLoader::QgsTerrainTileLoader( QgsTerrainEntity *terrain, QgsChunkN
, mTerrain( terrain )
{
const Qgs3DMapSettings &map = mTerrain->map3D();
QgsChunkNodeId nodeId = node->tileId();
int tx, ty, tz;
#if 0
if ( map.terrainGenerator->type() == TerrainGenerator::QuantizedMesh )
Expand All @@ -49,9 +50,9 @@ QgsTerrainTileLoader::QgsTerrainTileLoader( QgsTerrainEntity *terrain, QgsChunkN
else
#endif
{
tx = node->tileX();
ty = node->tileY();
tz = node->tileZ();
tx = nodeId.x;
ty = nodeId.y;
tz = nodeId.z;
}

QgsRectangle extentTerrainCrs = map.terrainGenerator()->tilingScheme().tileToExtent( tx, ty, tz );
Expand Down

0 comments on commit 47ba499

Please sign in to comment.