The 'Block' class is no longer considered just to be an implementation details of LargeVolume. Users may need to interact with them directly of they implement their own paging or compression systems.
This commit is contained in:
@ -1,131 +0,0 @@
|
||||
/*******************************************************************************
|
||||
Copyright (c) 2005-2009 David Williams
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source
|
||||
distribution.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __PolyVox_Block_H__
|
||||
#define __PolyVox_Block_H__
|
||||
|
||||
#include "PolyVoxCore/Impl/TypeDef.h"
|
||||
|
||||
#include "PolyVoxCore/PolyVoxForwardDeclarations.h"
|
||||
#include "PolyVoxCore/Vector.h"
|
||||
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
namespace PolyVox
|
||||
{
|
||||
template <typename VoxelType>
|
||||
class Block
|
||||
{
|
||||
friend class LargeVolume<VoxelType>;
|
||||
|
||||
public:
|
||||
Block()
|
||||
:m_uBlockLastAccessed(0)
|
||||
,m_bDataModified(true)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
// This is updated by the LargeVolume and used to discard the least recently used blocks.
|
||||
uint32_t m_uBlockLastAccessed;
|
||||
|
||||
// This is so we can tell whether a uncompressed block has to be recompressed and whether
|
||||
// a compressed block has to be paged back to disk, or whether they can just be discarded.
|
||||
bool m_bDataModified;
|
||||
|
||||
private:
|
||||
/// Private copy constructor to prevent accisdental copying
|
||||
Block(const Block& /*rhs*/) {};
|
||||
|
||||
/// Private assignment operator to prevent accisdental copying
|
||||
Block& operator=(const Block& /*rhs*/) {};
|
||||
};
|
||||
|
||||
template <typename VoxelType>
|
||||
class CompressedBlock : public Block<VoxelType>
|
||||
{
|
||||
friend class LargeVolume<VoxelType>;
|
||||
|
||||
public:
|
||||
CompressedBlock();
|
||||
~CompressedBlock();
|
||||
|
||||
const uint8_t* getData(void) const;
|
||||
uint32_t getDataSizeInBytes(void) const;
|
||||
|
||||
void setData(const uint8_t* const pData, uint32_t uDataSizeInBytes);
|
||||
|
||||
private:
|
||||
/// Private copy constructor to prevent accisdental copying
|
||||
CompressedBlock(const CompressedBlock& /*rhs*/) {};
|
||||
|
||||
/// Private assignment operator to prevent accisdental copying
|
||||
CompressedBlock& operator=(const CompressedBlock& /*rhs*/) {};
|
||||
|
||||
// Made this private to avoid any confusion with getDataSizeInBytes().
|
||||
// Users shouldn't really need this for CompressedBlock anyway.
|
||||
uint32_t calculateSizeInBytes(void);
|
||||
|
||||
uint8_t* m_pData;
|
||||
uint32_t m_uDataSizeInBytes;
|
||||
};
|
||||
|
||||
template <typename VoxelType>
|
||||
class UncompressedBlock : public Block<VoxelType>
|
||||
{
|
||||
friend class LargeVolume<VoxelType>;
|
||||
|
||||
public:
|
||||
UncompressedBlock(uint16_t uSideLength);
|
||||
~UncompressedBlock();
|
||||
|
||||
VoxelType* getData(void) const;
|
||||
uint32_t getDataSizeInBytes(void) const;
|
||||
|
||||
VoxelType getVoxel(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const;
|
||||
VoxelType getVoxel(const Vector3DUint16& v3dPos) const;
|
||||
|
||||
void setVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos, VoxelType tValue);
|
||||
void setVoxelAt(const Vector3DUint16& v3dPos, VoxelType tValue);
|
||||
|
||||
private:
|
||||
/// Private copy constructor to prevent accisdental copying
|
||||
UncompressedBlock(const UncompressedBlock& /*rhs*/) {};
|
||||
|
||||
/// Private assignment operator to prevent accisdental copying
|
||||
UncompressedBlock& operator=(const UncompressedBlock& /*rhs*/) {};
|
||||
|
||||
// Made this private for consistancy with CompressedBlock.
|
||||
// Users shouldn't really need this for UncompressedBlock anyway.
|
||||
uint32_t calculateSizeInBytes(void);
|
||||
|
||||
VoxelType* m_tData;
|
||||
uint16_t m_uSideLength;
|
||||
uint8_t m_uSideLengthPower;
|
||||
};
|
||||
}
|
||||
|
||||
#include "PolyVoxCore/Impl/Block.inl"
|
||||
|
||||
#endif
|
@ -1,187 +0,0 @@
|
||||
/*******************************************************************************
|
||||
Copyright (c) 2005-2009 David Williams
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source
|
||||
distribution.
|
||||
*******************************************************************************/
|
||||
|
||||
#include "PolyVoxCore/Impl/ErrorHandling.h"
|
||||
#include "PolyVoxCore/Impl/Utility.h"
|
||||
|
||||
#include "PolyVoxCore/Compressor.h"
|
||||
#include "PolyVoxCore/Vector.h"
|
||||
|
||||
#include "PolyVoxCore/Impl/ErrorHandling.h"
|
||||
|
||||
#include <cstring> //For memcpy
|
||||
#include <limits>
|
||||
#include <stdexcept> //for std::invalid_argument
|
||||
|
||||
namespace PolyVox
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <typename VoxelType>
|
||||
CompressedBlock<VoxelType>::CompressedBlock()
|
||||
:m_pData(0)
|
||||
,m_uDataSizeInBytes(0)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
CompressedBlock<VoxelType>::~CompressedBlock()
|
||||
{
|
||||
delete[] m_pData;
|
||||
m_pData = 0;
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
const uint8_t* CompressedBlock<VoxelType>::getData(void) const
|
||||
{
|
||||
return m_pData;
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
uint32_t CompressedBlock<VoxelType>::getDataSizeInBytes(void) const
|
||||
{
|
||||
return m_uDataSizeInBytes;
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
void CompressedBlock<VoxelType>::setData(const uint8_t* const pData, uint32_t uDataSizeInBytes)
|
||||
{
|
||||
POLYVOX_THROW_IF(pData == 0, std::invalid_argument, "Pointer to data cannot be null");
|
||||
POLYVOX_THROW_IF(m_pData == pData, std::invalid_argument, "Attempting to copy data onto itself");
|
||||
|
||||
// Delete any existing data
|
||||
delete[] m_pData;
|
||||
|
||||
// Allocate new data
|
||||
m_uDataSizeInBytes = uDataSizeInBytes;
|
||||
m_pData = new uint8_t[uDataSizeInBytes];
|
||||
|
||||
// Copy the data across
|
||||
memcpy(m_pData, pData, uDataSizeInBytes);
|
||||
|
||||
// Flag as modified
|
||||
this->m_bDataModified = true;
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
uint32_t CompressedBlock<VoxelType>::calculateSizeInBytes(void)
|
||||
{
|
||||
// Returns the size of this class plus the size of the compressed data.
|
||||
uint32_t uSizeInBytes = sizeof(CompressedBlock<VoxelType>) + m_uDataSizeInBytes;
|
||||
return uSizeInBytes;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <typename VoxelType>
|
||||
UncompressedBlock<VoxelType>::UncompressedBlock(uint16_t uSideLength)
|
||||
:m_tData(0)
|
||||
,m_uSideLength(0)
|
||||
,m_uSideLengthPower(0)
|
||||
{
|
||||
// Compute the side length
|
||||
m_uSideLength = uSideLength;
|
||||
m_uSideLengthPower = logBase2(uSideLength);
|
||||
|
||||
// Allocate the data
|
||||
const uint32_t uNoOfVoxels = m_uSideLength * m_uSideLength * m_uSideLength;
|
||||
m_tData = new VoxelType[uNoOfVoxels];
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
UncompressedBlock<VoxelType>::~UncompressedBlock()
|
||||
{
|
||||
delete[] m_tData;
|
||||
m_tData = 0;
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
VoxelType* UncompressedBlock<VoxelType>::getData(void) const
|
||||
{
|
||||
return m_tData;
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
uint32_t UncompressedBlock<VoxelType>::getDataSizeInBytes(void) const
|
||||
{
|
||||
return m_uSideLength * m_uSideLength * m_uSideLength * sizeof(VoxelType);
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
VoxelType UncompressedBlock<VoxelType>::getVoxel(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const
|
||||
{
|
||||
// This code is not usually expected to be called by the user, with the exception of when implementing paging
|
||||
// of uncompressed data. It's a performance critical code path so we use asserts rather than exceptions.
|
||||
POLYVOX_ASSERT(uXPos < m_uSideLength, "Supplied position is outside of the block");
|
||||
POLYVOX_ASSERT(uYPos < m_uSideLength, "Supplied position is outside of the block");
|
||||
POLYVOX_ASSERT(uZPos < m_uSideLength, "Supplied position is outside of the block");
|
||||
POLYVOX_ASSERT(m_tData, "No uncompressed data - block must be decompressed before accessing voxels.");
|
||||
|
||||
return m_tData
|
||||
[
|
||||
uXPos +
|
||||
uYPos * m_uSideLength +
|
||||
uZPos * m_uSideLength * m_uSideLength
|
||||
];
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
VoxelType UncompressedBlock<VoxelType>::getVoxel(const Vector3DUint16& v3dPos) const
|
||||
{
|
||||
return getVoxel(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ());
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
void UncompressedBlock<VoxelType>::setVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos, VoxelType tValue)
|
||||
{
|
||||
// This code is not usually expected to be called by the user, with the exception of when implementing paging
|
||||
// of uncompressed data. It's a performance critical code path so we use asserts rather than exceptions.
|
||||
POLYVOX_ASSERT(uXPos < m_uSideLength, "Supplied position is outside of the block");
|
||||
POLYVOX_ASSERT(uYPos < m_uSideLength, "Supplied position is outside of the block");
|
||||
POLYVOX_ASSERT(uZPos < m_uSideLength, "Supplied position is outside of the block");
|
||||
POLYVOX_ASSERT(m_tData, "No uncompressed data - block must be decompressed before accessing voxels.");
|
||||
|
||||
m_tData
|
||||
[
|
||||
uXPos +
|
||||
uYPos * m_uSideLength +
|
||||
uZPos * m_uSideLength * m_uSideLength
|
||||
] = tValue;
|
||||
|
||||
this->m_bDataModified = true;
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
void UncompressedBlock<VoxelType>::setVoxelAt(const Vector3DUint16& v3dPos, VoxelType tValue)
|
||||
{
|
||||
setVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), tValue);
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
uint32_t UncompressedBlock<VoxelType>::calculateSizeInBytes(void)
|
||||
{
|
||||
// Returns the size of this class plus the size of the uncompressed data.
|
||||
uint32_t uSizeInBytes = sizeof(UncompressedBlock<VoxelType>) + (m_uSideLength * m_uSideLength * m_uSideLength * sizeof(VoxelType));
|
||||
return uSizeInBytes;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user