Skip to content

Commit

Permalink
Adapt 3d for numeric ids
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 9, 2023
1 parent d1c66db commit 54410a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
33 changes: 28 additions & 5 deletions src/3d/chunks/qgschunknode_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,54 @@ class QgsChunkQueueJobFactory;
/**
* Helper class to store integer coordinates of a chunk node.
*
* IDs can be stored using quad or octree depth, x, y and z, where:
*
* - "d" is the depth of the tree
* - when used with a quadtree, "x" and "y" are the coordinates within the depth level of the tree ("z" coordinate is always -1)
* - when used with an octree, "x", "y" and "z" are the coordinates within the depth level of the tree
*
* Or alternatively, IDs can be associated with a direct integer ID if a unique
* ID is already available by the loader.
*/
struct QgsChunkNodeId
{
//! Constructs node ID

/**
* Constructs node ID from depth, x, y and z.
*/
QgsChunkNodeId( int _d = -1, int _x = -1, int _y = -1, int _z = -1 )
: d( _d ), x( _x ), y( _y ), z( _z ) {}

int d, x, y, z;
/**
* Constructs node ID from a unique integer ID.
*
* Useful for nodes which are not structured in a quadtree or octree arrangement.
*/
QgsChunkNodeId( long long id )
: uniqueId( id )
{}

int d = 0;
int x = 0;
int y = 0;
int z = 0;
long long uniqueId = -1;

//! Returns textual representation of the node ID in form of "Z/X/Y"
QString text() const
{
if ( z == -1 )
if ( uniqueId == -1 )
return QString::number( uniqueId );
else if ( z == -1 )
return QStringLiteral( "%1/%2/%3" ).arg( d ).arg( x ).arg( y ); // quadtree
else
return QStringLiteral( "%1/%2/%3/%4" ).arg( d ).arg( x ).arg( y ).arg( z ); // octree
}

// TODO c++20 - replace with = default
bool operator==( const QgsChunkNodeId &other ) const
{
return d == other.d && x == other.x && y == other.y && z == other.z;
return ( uniqueId == -1 && other.uniqueId == -1 && d == other.d && x == other.x && y == other.y && z == other.z )
|| ( uniqueId != -1 && uniqueId == other.uniqueId );
}

bool operator!=( const QgsChunkNodeId &other ) const
Expand Down
41 changes: 9 additions & 32 deletions src/3d/qgstiledscenechunkloader_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

size_t qHash( const QgsChunkNodeId &n )
{
return n.d ^ n.x ^ n.y ^ n.z;
return n.uniqueId;
}

static bool hasLargeBounds( const QgsTiledSceneTile &t )
Expand Down Expand Up @@ -88,24 +88,6 @@ static QString resolveUri( QString uri, const QString &baseUri )
return uri;
}

static QgsChunkNodeId nodeIdFromUuid( const QString &uuid )
{
QgsChunkNodeId nodeId;
QUuid nodeUuid = QUuid::fromString( uuid );
Q_ASSERT( !nodeUuid.isNull() );
Q_ASSERT( sizeof( QUuid ) == 16 && sizeof( QgsChunkNodeId ) == 16 );
memcpy( reinterpret_cast<char *>( &nodeId ), reinterpret_cast<const char *>( &nodeUuid ), 16 );
return nodeId;
}

static QString uuidFromNodeId( const QgsChunkNodeId &nodeId )
{
QUuid nodeUuid;
Q_ASSERT( sizeof( QUuid ) == 16 && sizeof( QgsChunkNodeId ) == 16 );
memcpy( reinterpret_cast<char *>( &nodeUuid ), reinterpret_cast<const char *>( &nodeId ), 16 );
return nodeUuid.toString();
}

///

QgsTiledSceneChunkLoader::QgsTiledSceneChunkLoader( QgsChunkNode *node, const QgsTiledSceneChunkLoaderFactory &factory, const QgsTiledSceneTile &t )
Expand Down Expand Up @@ -186,24 +168,20 @@ QgsTiledSceneChunkLoaderFactory::QgsTiledSceneChunkLoaderFactory( const Qgs3DMap
mRegionTransform = QgsCoordinateTransform( QgsCoordinateReferenceSystem( "EPSG:4979" ), mMap.crs(), mMap.transformContext() );
}


QgsChunkLoader *QgsTiledSceneChunkLoaderFactory::createChunkLoader( QgsChunkNode *node ) const
{
QString id = uuidFromNodeId( node->tileId() );
QgsTiledSceneTile t = mIndex.getTile( id );
const QgsTiledSceneTile t = mIndex.getTile( node->tileId().uniqueId );

return new QgsTiledSceneChunkLoader( node, *this, t );
}


// converts box from map coordinates to world coords (also flips [X,Y] to [X,-Z])
static QgsAABB aabbConvert( const QgsBox3D &b0, const QgsVector3D &sceneOriginTargetCrs )
{
QgsBox3D b = b0 - sceneOriginTargetCrs;
const QgsBox3D b = b0 - sceneOriginTargetCrs;
return QgsAABB( b.xMinimum(), b.zMinimum(), -b.yMaximum(), b.xMaximum(), b.zMaximum(), -b.yMinimum() );
}


QgsChunkNode *QgsTiledSceneChunkLoaderFactory::nodeForTile( const QgsTiledSceneTile &t, const QgsChunkNodeId &nodeId ) const
{
if ( hasLargeBounds( t ) )
Expand All @@ -227,16 +205,15 @@ QgsChunkNode *QgsTiledSceneChunkLoaderFactory::nodeForTile( const QgsTiledSceneT

QgsChunkNode *QgsTiledSceneChunkLoaderFactory::createRootNode() const
{
QgsTiledSceneTile t = mIndex.rootTile();
QgsChunkNodeId nodeId = nodeIdFromUuid( t.id() );
return nodeForTile( t, nodeId );
const QgsTiledSceneTile t = mIndex.rootTile();
return nodeForTile( t, QgsChunkNodeId( t.id() ) );
}


QVector<QgsChunkNode *> QgsTiledSceneChunkLoaderFactory::createChildren( QgsChunkNode *node ) const
{
QVector<QgsChunkNode *> children;
QString indexTileId = uuidFromNodeId( node->tileId() );
const long long indexTileId = node->tileId().uniqueId;

switch ( mIndex.childAvailability( indexTileId ) )
{
Expand All @@ -250,10 +227,10 @@ QVector<QgsChunkNode *> QgsTiledSceneChunkLoaderFactory::createChildren( QgsChun
break;
}

const QStringList childIds = mIndex.childTileIds( indexTileId );
for ( const QString &childId : childIds )
const QVector< long long > childIds = mIndex.childTileIds( indexTileId );
for ( long long childId : childIds )
{
QgsChunkNodeId chId = nodeIdFromUuid( childId );
const QgsChunkNodeId chId( childId );
QgsTiledSceneTile t = mIndex.getTile( childId );

// first check if this node should be even considered
Expand Down

0 comments on commit 54410a9

Please sign in to comment.