• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

PolyVoxCore/include/PolyVoxCore/SimpleVolumeBlock.inl

Go to the documentation of this file.
00001 /*******************************************************************************
00002 Copyright (c) 2005-2009 David Williams
00003 
00004 This software is provided 'as-is', without any express or implied
00005 warranty. In no event will the authors be held liable for any damages
00006 arising from the use of this software.
00007 
00008 Permission is granted to anyone to use this software for any purpose,
00009 including commercial applications, and to alter it and redistribute it
00010 freely, subject to the following restrictions:
00011 
00012     1. The origin of this software must not be misrepresented; you must not
00013     claim that you wrote the original software. If you use this software
00014     in a product, an acknowledgment in the product documentation would be
00015     appreciated but is not required.
00016 
00017     2. Altered source versions must be plainly marked as such, and must not be
00018     misrepresented as being the original software.
00019 
00020     3. This notice may not be removed or altered from any source
00021     distribution.   
00022 *******************************************************************************/
00023 
00024 namespace PolyVox
00025 {
00026     template <typename VoxelType>
00027     SimpleVolume<VoxelType>::Block::Block(uint16_t uSideLength)
00028         :m_tUncompressedData(0)
00029         ,m_uSideLength(0)
00030         ,m_uSideLengthPower(0)
00031     {
00032         if(uSideLength != 0)
00033         {
00034             initialise(uSideLength);
00035         }
00036     }
00037 
00038     template <typename VoxelType>
00039     SimpleVolume<VoxelType>::Block::~Block()
00040     {
00041         delete[] m_tUncompressedData;
00042     }
00043 
00044     template <typename VoxelType>
00045     uint16_t SimpleVolume<VoxelType>::Block::getSideLength(void) const
00046     {
00047         return m_uSideLength;
00048     }
00049 
00050     template <typename VoxelType>
00051     VoxelType SimpleVolume<VoxelType>::Block::getVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const
00052     {
00053         assert(uXPos < m_uSideLength);
00054         assert(uYPos < m_uSideLength);
00055         assert(uZPos < m_uSideLength);
00056 
00057         assert(m_tUncompressedData);
00058 
00059         return m_tUncompressedData
00060             [
00061                 uXPos + 
00062                 uYPos * m_uSideLength + 
00063                 uZPos * m_uSideLength * m_uSideLength
00064             ];
00065     }
00066 
00067     template <typename VoxelType>
00068     VoxelType SimpleVolume<VoxelType>::Block::getVoxelAt(const Vector3DUint16& v3dPos) const
00069     {
00070         return getVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ());
00071     }
00072 
00073     template <typename VoxelType>
00074     void SimpleVolume<VoxelType>::Block::setVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos, VoxelType tValue)
00075     {
00076         assert(uXPos < m_uSideLength);
00077         assert(uYPos < m_uSideLength);
00078         assert(uZPos < m_uSideLength);
00079 
00080         assert(m_tUncompressedData);
00081 
00082         m_tUncompressedData
00083         [
00084             uXPos + 
00085             uYPos * m_uSideLength + 
00086             uZPos * m_uSideLength * m_uSideLength
00087         ] = tValue;
00088     }
00089 
00090     template <typename VoxelType>
00091     void SimpleVolume<VoxelType>::Block::setVoxelAt(const Vector3DUint16& v3dPos, VoxelType tValue)
00092     {
00093         setVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), tValue);
00094     }
00095 
00096     template <typename VoxelType>
00097     void SimpleVolume<VoxelType>::Block::fill(VoxelType tValue)
00098     {
00099         const uint32_t uNoOfVoxels = m_uSideLength * m_uSideLength * m_uSideLength;
00100         std::fill(m_tUncompressedData, m_tUncompressedData + uNoOfVoxels, tValue);
00101     }
00102 
00103     template <typename VoxelType>
00104     void SimpleVolume<VoxelType>::Block::initialise(uint16_t uSideLength)
00105     {
00106         //Debug mode validation
00107         assert(isPowerOf2(uSideLength));
00108 
00109         //Release mode validation
00110         if(!isPowerOf2(uSideLength))
00111         {
00112             throw std::invalid_argument("Block side length must be a power of two.");
00113         }
00114 
00115         //Compute the side length       
00116         m_uSideLength = uSideLength;
00117         m_uSideLengthPower = logBase2(uSideLength);
00118 
00119         m_tUncompressedData = new VoxelType[m_uSideLength * m_uSideLength * m_uSideLength];
00120 
00121         SimpleVolume<VoxelType>::Block::fill(VoxelType());
00122     }
00123 
00124     template <typename VoxelType>
00125     uint32_t SimpleVolume<VoxelType>::Block::calculateSizeInBytes(void)
00126     {
00127         uint32_t uSizeInBytes = sizeof(Block);
00128         uSizeInBytes += sizeof(VoxelType) * m_uSideLength * m_uSideLength * m_uSideLength;
00129         return  uSizeInBytes;
00130     }
00131 }

Generated on Sat Nov 19 2011 00:27:31 for PolyVox by  doxygen 1.7.1