Replaced Vector3D with integer as key to map.

Chunks of voxel data are stored in a map, and it is quite common to need to search the map for a particular chunk. The key type used to be a Vector3D (i.e. the position of the chunk in 3D space) which makes conceptual sense but is relatively slow. Using a Vector3D as a key seems to have overhead, probably in terms of copying and performing comparisons. It seems to be significantly faster to use an integer as a key, so we now take the 3D position and pack it into a single integer by bitshifting.

Naturally this reduces the range of positions we can store - a 32-bit int can only encode 3 x 10-bit values, which means our volume can only be 1024 chunks in each direction (with a chunk often being 32x32x32 voxels). This should still be large enough for most uses, but an upcoming change will allow 64-bit keys to be used (at least on 64-bit builds) which then allows 21 bits of precision  per component. This is so large that it's almost infinite for any practical purposes.
This commit is contained in:
David Williams
2015-03-15 09:32:42 +01:00
parent d305038c27
commit e82d6beca1
5 changed files with 86 additions and 57 deletions

View File

@ -97,20 +97,18 @@ namespace PolyVox
// Base version updates position and validity flags.
BaseVolume<VoxelType>::template Sampler< PagedVolume<VoxelType> >::setPosition(xPos, yPos, zPos);
// Then we update the voxel pointer
const int32_t uXChunk = this->mXPosInVolume >> this->mVolume->m_uChunkSideLengthPower;
const int32_t uYChunk = this->mYPosInVolume >> this->mVolume->m_uChunkSideLengthPower;
const int32_t uZChunk = this->mZPosInVolume >> this->mVolume->m_uChunkSideLengthPower;
const int32_t key = this->mVolume->posToChunkKey(xPos, yPos, zPos);
const uint16_t uXPosInChunk = static_cast<uint16_t>(this->mXPosInVolume - (uXChunk << this->mVolume->m_uChunkSideLengthPower));
const uint16_t uYPosInChunk = static_cast<uint16_t>(this->mYPosInVolume - (uYChunk << this->mVolume->m_uChunkSideLengthPower));
const uint16_t uZPosInChunk = static_cast<uint16_t>(this->mZPosInVolume - (uZChunk << this->mVolume->m_uChunkSideLengthPower));
// Then we update the voxel pointer.
auto pCurrentChunk = (key == this->mVolume->m_v3dLastAccessedChunkKey) ? this->mVolume->m_pLastAccessedChunk : this->mVolume->getChunk(key);
const uint32_t uVoxelIndexInChunk = uXPosInChunk +
uYPosInChunk * this->mVolume->m_uChunkSideLength +
uZPosInChunk * this->mVolume->m_uChunkSideLength * this->mVolume->m_uChunkSideLength;
const uint16_t xOffset = static_cast<uint16_t>(xPos & this->mVolume->m_iChunkMask);
const uint16_t yOffset = static_cast<uint16_t>(yPos & this->mVolume->m_iChunkMask);
const uint16_t zOffset = static_cast<uint16_t>(zPos & this->mVolume->m_iChunkMask);
auto pCurrentChunk = this->mVolume->getChunk(uXChunk, uYChunk, uZChunk);
const uint32_t uVoxelIndexInChunk = xOffset +
yOffset * this->mVolume->m_uChunkSideLength +
zOffset * this->mVolume->m_uChunkSideLength * this->mVolume->m_uChunkSideLength;
mCurrentVoxel = pCurrentChunk->m_tData + uVoxelIndexInChunk;
}