38 template <
typename VoxelType>
41 ,m_uCompressedDataLength(0)
42 ,m_tUncompressedData(0)
44 ,m_uSideLengthPower(0)
45 ,m_bIsCompressed(false)
46 ,m_bIsUncompressedDataModified(true)
54 template <
typename VoxelType>
60 template <
typename VoxelType>
63 POLYVOX_ASSERT(uXPos < m_uSideLength,
"Supplied position is outside of the block");
64 POLYVOX_ASSERT(uYPos < m_uSideLength,
"Supplied position is outside of the block");
65 POLYVOX_ASSERT(uZPos < m_uSideLength,
"Supplied position is outside of the block");
67 POLYVOX_ASSERT(m_tUncompressedData,
"No uncompressed data - block must be decompressed before accessing voxels.");
69 return m_tUncompressedData
72 uYPos * m_uSideLength +
73 uZPos * m_uSideLength * m_uSideLength
77 template <
typename VoxelType>
80 return getVoxelAt(v3dPos.
getX(), v3dPos.
getY(), v3dPos.
getZ());
83 template <
typename VoxelType>
86 POLYVOX_ASSERT(uXPos < m_uSideLength,
"Supplied position is outside of the block");
87 POLYVOX_ASSERT(uYPos < m_uSideLength,
"Supplied position is outside of the block");
88 POLYVOX_ASSERT(uZPos < m_uSideLength,
"Supplied position is outside of the block");
90 POLYVOX_ASSERT(m_tUncompressedData,
"No uncompressed data - block must be decompressed before accessing voxels.");
95 uYPos * m_uSideLength +
96 uZPos * m_uSideLength * m_uSideLength
99 m_bIsUncompressedDataModified =
true;
102 template <
typename VoxelType>
105 setVoxelAt(v3dPos.
getX(), v3dPos.
getY(), v3dPos.
getZ(), tValue);
108 template <
typename VoxelType>
117 POLYVOX_THROW(std::invalid_argument,
"Block side length must be a power of two.");
121 m_uSideLength = uSideLength;
122 m_uSideLengthPower =
logBase2(uSideLength);
125 m_tUncompressedData =
new VoxelType[m_uSideLength * m_uSideLength * m_uSideLength];
128 const uint32_t uNoOfVoxels = m_uSideLength * m_uSideLength * m_uSideLength;
129 std::fill(m_tUncompressedData, m_tUncompressedData + uNoOfVoxels,
VoxelType());
130 m_bIsUncompressedDataModified =
true;
133 template <
typename VoxelType>
141 template <
typename VoxelType>
145 POLYVOX_ASSERT(m_bIsCompressed ==
false,
"Attempted to compress block which is already flagged as compressed.");
146 POLYVOX_ASSERT(m_tUncompressedData != 0,
"No uncompressed data is present.");
150 if(m_bIsUncompressedDataModified)
153 delete[] m_pCompressedData;
154 m_pCompressedData = 0;
156 void* pSrcData =
reinterpret_cast<void*
>(m_tUncompressedData);
157 uint32_t uSrcLength = m_uSideLength * m_uSideLength * m_uSideLength *
sizeof(
VoxelType);
160 void* pDstData =
reinterpret_cast<void*
>( tempBuffer );
167 uCompressedLength = pCompressor->
compress(pSrcData, uSrcLength, pDstData, uDstLength);
170 m_pCompressedData =
new uint8_t[uCompressedLength];
171 memcpy(m_pCompressedData, pDstData, uCompressedLength);
172 m_uCompressedDataLength = uCompressedLength;
174 catch(std::exception&)
182 pDstData =
reinterpret_cast<void*
>( buffer );
183 uDstLength = uMaxCompressedSize;
187 uCompressedLength = pCompressor->
compress(pSrcData, uSrcLength, pDstData, uDstLength);
190 m_pCompressedData =
new uint8_t[uCompressedLength];
191 memcpy(m_pCompressedData, pDstData, uCompressedLength);
192 m_uCompressedDataLength = uCompressedLength;
194 catch(std::exception&)
199 POLYVOX_THROW(std::runtime_error,
"Failed to compress block data");
207 delete[] m_tUncompressedData;
208 m_tUncompressedData = 0;
209 m_bIsCompressed =
true;
212 template <
typename VoxelType>
216 POLYVOX_ASSERT(m_bIsCompressed ==
true,
"Attempted to uncompress block which is not flagged as compressed.");
217 POLYVOX_ASSERT(m_tUncompressedData == 0,
"Uncompressed data already exists.");
219 m_tUncompressedData =
new VoxelType[m_uSideLength * m_uSideLength * m_uSideLength];
221 void* pSrcData =
reinterpret_cast<void*
>(m_pCompressedData);
222 void* pDstData =
reinterpret_cast<void*
>(m_tUncompressedData);
223 uint32_t uSrcLength = m_uCompressedDataLength;
224 uint32_t uDstLength = m_uSideLength * m_uSideLength * m_uSideLength *
sizeof(
VoxelType);
228 uint32_t uUncompressedLength = pCompressor->
decompress(pSrcData, uSrcLength, pDstData, uDstLength);
230 POLYVOX_ASSERT(uUncompressedLength == m_uSideLength * m_uSideLength * m_uSideLength *
sizeof(
VoxelType),
"Destination length has changed.");
234 m_bIsCompressed =
false;
235 m_bIsUncompressedDataModified =
false;