From 87d97436ea77e9296a82f823fc1e5b45da9ebebe Mon Sep 17 00:00:00 2001 From: David Williams Date: Sat, 30 Apr 2011 12:34:13 +0100 Subject: [PATCH] More work on SimpleVolume. --- library/PolyVoxCore/include/SimpleVolume.h | 11 +++-- library/PolyVoxCore/include/SimpleVolume.inl | 52 ++++++++------------ 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/library/PolyVoxCore/include/SimpleVolume.h b/library/PolyVoxCore/include/SimpleVolume.h index eab6844e..ab34249b 100644 --- a/library/PolyVoxCore/include/SimpleVolume.h +++ b/library/PolyVoxCore/include/SimpleVolume.h @@ -28,10 +28,7 @@ freely, subject to the following restrictions: #include "PolyVoxForwardDeclarations.h" #include -#include -#include #include -#include namespace PolyVox { @@ -186,8 +183,6 @@ private: Block* getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const; //The block data - //mutable std::map m_pBlocks; - Block* m_pBlocks; //We don't store an actual Block for the border, just the uncompressed data. This is partly because the border @@ -200,6 +195,12 @@ private: Region m_regValidRegion; Region m_regValidRegionInBlocks; + //Volume size measured in blocks. + uint32_t m_uNoOfBlocksInVolume; + uint16_t m_uWidthInBlocks; + uint16_t m_uHeightInBlocks; + uint16_t m_uDepthInBlocks; + //The size of the blocks uint16_t m_uBlockSideLength; uint8_t m_uBlockSideLengthPower; diff --git a/library/PolyVoxCore/include/SimpleVolume.inl b/library/PolyVoxCore/include/SimpleVolume.inl index 610826c2..9efc847e 100644 --- a/library/PolyVoxCore/include/SimpleVolume.inl +++ b/library/PolyVoxCore/include/SimpleVolume.inl @@ -30,8 +30,6 @@ freely, subject to the following restrictions: #include #include #include //For abort() -#include //For memcpy -#include #include //For invalid_argument namespace PolyVox @@ -82,6 +80,8 @@ namespace PolyVox template SimpleVolume::~SimpleVolume() { + delete[] m_pBlocks; + m_pBlocks = 0; } //////////////////////////////////////////////////////////////////////////////// @@ -288,14 +288,15 @@ namespace PolyVox m_uBlockSideLength = uBlockSideLength; m_uBlockSideLengthPower = logBase2(m_uBlockSideLength); - //Clear the previous data - //m_pBlocks.clear(); - //delete[] m_pBlocks; - //Allocate the new data + //Compute the size of the volume in blocks (and note +1 at the end) + m_uWidthInBlocks = m_regValidRegionInBlocks.getUpperCorner().getX() - m_regValidRegionInBlocks.getLowerCorner().getX() + 1; + m_uHeightInBlocks = m_regValidRegionInBlocks.getUpperCorner().getY() - m_regValidRegionInBlocks.getLowerCorner().getY() + 1; + m_uDepthInBlocks = m_regValidRegionInBlocks.getUpperCorner().getZ() - m_regValidRegionInBlocks.getLowerCorner().getZ() + 1; + m_uNoOfBlocksInVolume = m_uWidthInBlocks * m_uHeightInBlocks * m_uDepthInBlocks; - Vector3DInt32 uDimensionsInBlocks = m_regValidRegionInBlocks.getUpperCorner() - m_regValidRegionInBlocks.getLowerCorner() + Vector3DInt32(1,1,1); - m_pBlocks = new Block[uDimensionsInBlocks.getX() * uDimensionsInBlocks.getY() * uDimensionsInBlocks.getZ()]; - for(uint32_t i = 0; i < uDimensionsInBlocks.getX() * uDimensionsInBlocks.getY() * uDimensionsInBlocks.getZ(); ++i) + //Allocate the data + m_pBlocks = new Block[m_uNoOfBlocksInVolume]; + for(uint32_t i = 0; i < m_uNoOfBlocksInVolume; ++i) { m_pBlocks[i].initialise(m_uBlockSideLength); } @@ -313,33 +314,20 @@ namespace PolyVox template typename SimpleVolume::Block* SimpleVolume::getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const { - Vector3DInt32 v3dBlockPos(uBlockX, uBlockY, uBlockZ); - - Vector3DInt32 uDimensionsInBlocks = m_regValidRegionInBlocks.getUpperCorner() - m_regValidRegionInBlocks.getLowerCorner() + Vector3DInt32(1,1,1); + //The lower left corner of the volume could be + //anywhere, but array indices need to start at zero. + uBlockX -= m_regValidRegionInBlocks.getLowerCorner().getX(); + uBlockY -= m_regValidRegionInBlocks.getLowerCorner().getY(); + uBlockZ -= m_regValidRegionInBlocks.getLowerCorner().getZ(); + //Compute the block index uint32_t uBlockIndex = uBlockX + - uBlockY * uDimensionsInBlocks.getX() + - uBlockZ * uDimensionsInBlocks.getX() * uDimensionsInBlocks.getY(); + uBlockY * m_uWidthInBlocks + + uBlockZ * m_uWidthInBlocks * m_uHeightInBlocks; - //Get the block - Block* block = &(m_pBlocks[uBlockIndex]); - - return block; - - //Block& block = m_pBlocks[] - - /*typename std::map::iterator itBlock = m_pBlocks.find(v3dBlockPos); - // check whether the block is already loaded - if(itBlock == m_pBlocks.end()) - { - // create the new block - Block newBlock(m_uBlockSideLength); - itBlock = m_pBlocks.insert(std::make_pair(v3dBlockPos, newBlock)).first; - } - - Block& block = itBlock->second; - return █*/ + //Return the block + return &(m_pBlocks[uBlockIndex]); } ////////////////////////////////////////////////////////////////////////////////