PolyVox and Thermite3D now support the use of a density component as well as a material component for each voxel.
This commit is contained in:
@ -4,15 +4,16 @@ PROJECT(PolyVoxUtil)
|
||||
|
||||
#Projects source files
|
||||
SET(UTIL_SRC_FILES
|
||||
source/Serialization.cpp
|
||||
source/VolumeChangeTracker.cpp
|
||||
source/Dummy.cpp
|
||||
)
|
||||
|
||||
#Projects headers files
|
||||
SET(UTIL_INC_FILES
|
||||
include/Export.h
|
||||
include/Serialization.h
|
||||
include/Serialization.inl
|
||||
include/VolumeChangeTracker.h
|
||||
include/VolumeChangeTracker.inl
|
||||
)
|
||||
|
||||
ADD_DEFINITIONS(-DPOLYVOXUTIL_EXPORT) #Export symbols in the .dll
|
||||
|
@ -41,11 +41,279 @@ namespace PolyVox
|
||||
virtual void onProgressUpdated(float fProgress) = 0;
|
||||
};
|
||||
|
||||
POLYVOXUTIL_API std::shared_ptr< Volume<uint8_t> > loadVolumeRaw(std::istream& stream, VolumeSerializationProgressListener* progressListener = 0);
|
||||
POLYVOXUTIL_API void saveVolumeRaw(std::ostream& stream, Volume<uint8_t>& volume, VolumeSerializationProgressListener* progressListener = 0);
|
||||
template <typename VoxelType>
|
||||
std::shared_ptr< Volume<VoxelType> > loadVolumeRaw(std::istream& stream, VolumeSerializationProgressListener* progressListener = 0);
|
||||
template <typename VoxelType>
|
||||
void saveVolumeRaw(std::ostream& stream, Volume<VoxelType>& volume, VolumeSerializationProgressListener* progressListener = 0);
|
||||
|
||||
POLYVOXUTIL_API std::shared_ptr< Volume<uint8_t> > loadVolumeRle(std::istream& stream, VolumeSerializationProgressListener* progressListener = 0);
|
||||
POLYVOXUTIL_API void saveVolumeRle(std::ostream& stream, Volume<uint8_t>& volume, VolumeSerializationProgressListener* progressListener = 0);
|
||||
template <typename VoxelType>
|
||||
std::shared_ptr< Volume<VoxelType> > loadVolumeRle(std::istream& stream, VolumeSerializationProgressListener* progressListener = 0);
|
||||
template <typename VoxelType>
|
||||
void saveVolumeRle(std::ostream& stream, Volume<VoxelType>& volume, VolumeSerializationProgressListener* progressListener = 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 "Serialization.h"
|
||||
|
||||
#include "Volume.h"
|
||||
#include "VolumeSampler.h"
|
||||
#include "PolyVoxImpl/Utility.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace PolyVox
|
||||
{
|
||||
//Note: we don't do much error handling in here - exceptions will simply be propergated up to the caller.
|
||||
//FIXME - think about pointer ownership issues. Or could return volume by value if the copy constructor is shallow
|
||||
template <typename VoxelType>
|
||||
shared_ptr< Volume<VoxelType> > loadVolumeRaw(istream& stream, VolumeSerializationProgressListener* progressListener)
|
||||
{
|
||||
//Read volume dimensions
|
||||
uint8_t volumeWidthPower = 0;
|
||||
uint8_t volumeHeightPower = 0;
|
||||
uint8_t volumeDepthPower = 0;
|
||||
stream.read(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
|
||||
stream.read(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
|
||||
stream.read(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
|
||||
|
||||
uint16_t volumeWidth = 0x0001 << volumeWidthPower;
|
||||
uint16_t volumeHeight = 0x0001 << volumeHeightPower;
|
||||
uint16_t volumeDepth = 0x0001 << volumeDepthPower;
|
||||
|
||||
//FIXME - need to support non cubic volumes
|
||||
shared_ptr< Volume<VoxelType> > volume(new Volume<VoxelType>(volumeWidth, volumeHeight, volumeDepth));
|
||||
|
||||
//Read data
|
||||
for(uint16_t z = 0; z < volumeDepth; ++z)
|
||||
{
|
||||
//Update progress once per slice.
|
||||
if(progressListener)
|
||||
{
|
||||
float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
|
||||
progressListener->onProgressUpdated(fProgress);
|
||||
}
|
||||
|
||||
for(uint16_t y = 0; y < volumeHeight; ++y)
|
||||
{
|
||||
for(uint16_t x = 0; x < volumeWidth; ++x)
|
||||
{
|
||||
VoxelType value;
|
||||
stream.read(reinterpret_cast<char*>(&value), sizeof(value));
|
||||
|
||||
volume->setVoxelAt(x,y,z,value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Finished
|
||||
if(progressListener)
|
||||
{
|
||||
progressListener->onProgressUpdated(1.0f);
|
||||
}
|
||||
|
||||
return volume;
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
void saveVolumeRaw(std::ostream& stream, Volume<VoxelType>& volume, VolumeSerializationProgressListener* progressListener)
|
||||
{
|
||||
//Write volume dimensions
|
||||
uint16_t volumeWidth = volume.getWidth();
|
||||
uint16_t volumeHeight = volume.getHeight();
|
||||
uint16_t volumeDepth = volume.getDepth();
|
||||
|
||||
uint8_t volumeWidthPower = logBase2(volumeWidth);
|
||||
uint8_t volumeHeightPower = logBase2(volumeHeight);
|
||||
uint8_t volumeDepthPower = logBase2(volumeDepth);
|
||||
|
||||
stream.write(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
|
||||
stream.write(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
|
||||
stream.write(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
|
||||
|
||||
//Write data
|
||||
VolumeSampler<VoxelType> volIter(&volume);
|
||||
for(uint16_t z = 0; z < volumeDepth; ++z)
|
||||
{
|
||||
//Update progress once per slice.
|
||||
if(progressListener)
|
||||
{
|
||||
float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
|
||||
progressListener->onProgressUpdated(fProgress);
|
||||
}
|
||||
|
||||
for(uint16_t y = 0; y < volumeHeight; ++y)
|
||||
{
|
||||
for(uint16_t x = 0; x < volumeWidth; ++x)
|
||||
{
|
||||
volIter.setPosition(x,y,z);
|
||||
VoxelType value = volIter.getVoxel();
|
||||
stream.write(reinterpret_cast<char*>(&value), sizeof(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Finished
|
||||
if(progressListener)
|
||||
{
|
||||
progressListener->onProgressUpdated(1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
//Note: we don't do much error handling in here - exceptions will simply be propergated up to the caller.
|
||||
//FIXME - think about pointer ownership issues. Or could return volume by value if the copy constructor is shallow
|
||||
template <typename VoxelType>
|
||||
shared_ptr< Volume<VoxelType> > loadVolumeRle(istream& stream, VolumeSerializationProgressListener* progressListener)
|
||||
{
|
||||
//Read volume dimensions
|
||||
uint8_t volumeWidthPower = 0;
|
||||
uint8_t volumeHeightPower = 0;
|
||||
uint8_t volumeDepthPower = 0;
|
||||
stream.read(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
|
||||
stream.read(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
|
||||
stream.read(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
|
||||
|
||||
uint16_t volumeWidth = 0x0001 << volumeWidthPower;
|
||||
uint16_t volumeHeight = 0x0001 << volumeHeightPower;
|
||||
uint16_t volumeDepth = 0x0001 << volumeDepthPower;
|
||||
|
||||
//FIXME - need to support non cubic volumes
|
||||
shared_ptr< Volume<VoxelType> > volume(new Volume<VoxelType>(volumeWidth, volumeHeight, volumeDepth));
|
||||
|
||||
//Read data
|
||||
bool firstTime = true;
|
||||
uint32_t runLength = 0;
|
||||
VoxelType value;
|
||||
stream.read(reinterpret_cast<char*>(&value), sizeof(value));
|
||||
stream.read(reinterpret_cast<char*>(&runLength), sizeof(runLength));
|
||||
for(uint16_t z = 0; z < volumeDepth; ++z)
|
||||
{
|
||||
//Update progress once per slice.
|
||||
if(progressListener)
|
||||
{
|
||||
float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
|
||||
progressListener->onProgressUpdated(fProgress);
|
||||
}
|
||||
|
||||
for(uint16_t y = 0; y < volumeHeight; ++y)
|
||||
{
|
||||
for(uint16_t x = 0; x < volumeWidth; ++x)
|
||||
{
|
||||
if(runLength != 0)
|
||||
{
|
||||
volume->setVoxelAt(x,y,z,value);
|
||||
runLength--;
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.read(reinterpret_cast<char*>(&value), sizeof(value));
|
||||
stream.read(reinterpret_cast<char*>(&runLength), sizeof(runLength));
|
||||
|
||||
volume->setVoxelAt(x,y,z,value);
|
||||
runLength--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Finished
|
||||
if(progressListener)
|
||||
{
|
||||
progressListener->onProgressUpdated(1.0f);
|
||||
}
|
||||
|
||||
return volume;
|
||||
}
|
||||
|
||||
template <typename VoxelType>
|
||||
void saveVolumeRle(std::ostream& stream, Volume<VoxelType>& volume, VolumeSerializationProgressListener* progressListener)
|
||||
{
|
||||
//Write volume dimensions
|
||||
uint16_t volumeWidth = volume.getWidth();
|
||||
uint16_t volumeHeight = volume.getHeight();
|
||||
uint16_t volumeDepth = volume.getDepth();
|
||||
|
||||
uint8_t volumeWidthPower = logBase2(volumeWidth);
|
||||
uint8_t volumeHeightPower = logBase2(volumeHeight);
|
||||
uint8_t volumeDepthPower = logBase2(volumeDepth);
|
||||
|
||||
stream.write(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
|
||||
stream.write(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
|
||||
stream.write(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
|
||||
|
||||
//Write data
|
||||
VolumeSampler<VoxelType> volIter(&volume);
|
||||
VoxelType current;
|
||||
uint32_t runLength = 0;
|
||||
bool firstTime = true;
|
||||
for(uint16_t z = 0; z < volumeDepth; ++z)
|
||||
{
|
||||
//Update progress once per slice.
|
||||
if(progressListener)
|
||||
{
|
||||
float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
|
||||
progressListener->onProgressUpdated(fProgress);
|
||||
}
|
||||
|
||||
for(uint16_t y = 0; y < volumeHeight; ++y)
|
||||
{
|
||||
for(uint16_t x = 0; x < volumeWidth; ++x)
|
||||
{
|
||||
volIter.setPosition(x,y,z);
|
||||
VoxelType value = volIter.getVoxel();
|
||||
if(firstTime)
|
||||
{
|
||||
current = value;
|
||||
runLength = 1;
|
||||
firstTime = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(value == current)
|
||||
{
|
||||
runLength++;
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.write(reinterpret_cast<char*>(¤t), sizeof(current));
|
||||
stream.write(reinterpret_cast<char*>(&runLength), sizeof(runLength));
|
||||
current = value;
|
||||
runLength = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stream.write(reinterpret_cast<char*>(¤t), sizeof(current));
|
||||
stream.write(reinterpret_cast<char*>(&runLength), sizeof(runLength));
|
||||
|
||||
//Finished
|
||||
if(progressListener)
|
||||
{
|
||||
progressListener->onProgressUpdated(1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
0
library/PolyVoxUtil/include/Serialization.inl
Normal file
0
library/PolyVoxUtil/include/Serialization.inl
Normal file
@ -35,22 +35,23 @@ freely, subject to the following restrictions:
|
||||
namespace PolyVox
|
||||
{
|
||||
/// Voxel scene manager
|
||||
class POLYVOXUTIL_API VolumeChangeTracker
|
||||
template <typename VoxelType>
|
||||
class VolumeChangeTracker
|
||||
{
|
||||
public:
|
||||
//Constructors, etc
|
||||
VolumeChangeTracker(Volume<uint8_t>* volumeDataToSet, uint16_t regionSideLength);
|
||||
VolumeChangeTracker(Volume<VoxelType>* volumeDataToSet, uint16_t regionSideLength);
|
||||
~VolumeChangeTracker();
|
||||
|
||||
//Getters
|
||||
int32_t getCurrentTime(void) const;
|
||||
int32_t getLastModifiedTimeForRegion(uint16_t uX, uint16_t uY, uint16_t uZ);
|
||||
Volume<uint8_t>* getWrappedVolume(void) const;
|
||||
Volume<VoxelType>* getWrappedVolume(void) const;
|
||||
|
||||
//Setters
|
||||
void setAllRegionsModified(void);
|
||||
void setLockedVoxelAt(uint16_t x, uint16_t y, uint16_t z, uint8_t value);
|
||||
void setVoxelAt(uint16_t x, uint16_t y, uint16_t z, uint8_t value);
|
||||
void setLockedVoxelAt(uint16_t x, uint16_t y, uint16_t z, VoxelType value);
|
||||
void setVoxelAt(uint16_t x, uint16_t y, uint16_t z, VoxelType value);
|
||||
|
||||
//Others
|
||||
void lockRegion(const Region& regToLock);
|
||||
@ -61,7 +62,7 @@ namespace PolyVox
|
||||
void incrementCurrentTime(void);
|
||||
bool m_bIsLocked;
|
||||
Region m_regLastLocked;
|
||||
Volume<uint8_t>* volumeData;
|
||||
Volume<VoxelType>* volumeData;
|
||||
|
||||
uint16_t m_uRegionSideLength;
|
||||
uint8_t m_uRegionSideLengthPower;
|
||||
@ -78,4 +79,6 @@ namespace PolyVox
|
||||
};
|
||||
}
|
||||
|
||||
#include "VolumeChangeTracker.inl"
|
||||
|
||||
#endif
|
||||
|
@ -38,12 +38,14 @@ using namespace std;
|
||||
|
||||
namespace PolyVox
|
||||
{
|
||||
uint32_t VolumeChangeTracker::m_uCurrentTime = 0;
|
||||
template <typename VoxelType>
|
||||
uint32_t VolumeChangeTracker<VoxelType>::m_uCurrentTime = 0;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// VolumeChangeTracker
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
VolumeChangeTracker::VolumeChangeTracker(Volume<uint8_t>* volumeDataToSet, uint16_t regionSideLength)
|
||||
template <typename VoxelType>
|
||||
VolumeChangeTracker<VoxelType>::VolumeChangeTracker(Volume<VoxelType>* volumeDataToSet, uint16_t regionSideLength)
|
||||
:m_bIsLocked(false)
|
||||
,volumeData(0)
|
||||
,m_uRegionSideLength(regionSideLength)
|
||||
@ -57,11 +59,13 @@ namespace PolyVox
|
||||
volRegionLastModified = new Volume<int32_t>(m_uVolumeWidthInRegions, m_uVolumeHeightInRegions, m_uVolumeDepthInRegions, 0);
|
||||
}
|
||||
|
||||
VolumeChangeTracker::~VolumeChangeTracker()
|
||||
template <typename VoxelType>
|
||||
VolumeChangeTracker<VoxelType>::~VolumeChangeTracker()
|
||||
{
|
||||
}
|
||||
|
||||
void VolumeChangeTracker::setAllRegionsModified(void)
|
||||
template <typename VoxelType>
|
||||
void VolumeChangeTracker<VoxelType>::setAllRegionsModified(void)
|
||||
{
|
||||
incrementCurrentTime();
|
||||
for(uint16_t blockZ = 0; blockZ < m_uVolumeDepthInRegions; ++blockZ)
|
||||
@ -76,22 +80,26 @@ namespace PolyVox
|
||||
}
|
||||
}
|
||||
|
||||
int32_t VolumeChangeTracker::getCurrentTime(void) const
|
||||
template <typename VoxelType>
|
||||
int32_t VolumeChangeTracker<VoxelType>::getCurrentTime(void) const
|
||||
{
|
||||
return m_uCurrentTime;
|
||||
}
|
||||
|
||||
int32_t VolumeChangeTracker::getLastModifiedTimeForRegion(uint16_t uX, uint16_t uY, uint16_t uZ)
|
||||
template <typename VoxelType>
|
||||
int32_t VolumeChangeTracker<VoxelType>::getLastModifiedTimeForRegion(uint16_t uX, uint16_t uY, uint16_t uZ)
|
||||
{
|
||||
return volRegionLastModified->getVoxelAt(uX, uY, uZ);
|
||||
}
|
||||
|
||||
Volume<uint8_t>* VolumeChangeTracker::getWrappedVolume(void) const
|
||||
template <typename VoxelType>
|
||||
Volume<VoxelType>* VolumeChangeTracker<VoxelType>::getWrappedVolume(void) const
|
||||
{
|
||||
return volumeData;
|
||||
}
|
||||
|
||||
void VolumeChangeTracker::setVoxelAt(uint16_t x, uint16_t y, uint16_t z, uint8_t value)
|
||||
template <typename VoxelType>
|
||||
void VolumeChangeTracker<VoxelType>::setVoxelAt(uint16_t x, uint16_t y, uint16_t z, VoxelType value)
|
||||
{
|
||||
//Note: We increase the time stamp both at the start and the end
|
||||
//to avoid ambiguity about whether the timestamp comparison should
|
||||
@ -140,18 +148,20 @@ namespace PolyVox
|
||||
incrementCurrentTime();
|
||||
}
|
||||
|
||||
void VolumeChangeTracker::setLockedVoxelAt(uint16_t x, uint16_t y, uint16_t z, uint8_t value)
|
||||
template <typename VoxelType>
|
||||
void VolumeChangeTracker<VoxelType>::setLockedVoxelAt(uint16_t x, uint16_t y, uint16_t z, VoxelType value)
|
||||
{
|
||||
assert(m_bIsLocked);
|
||||
|
||||
//FIXME - rather than creating a iterator each time we should have one stored
|
||||
/*VolumeSampler<uint8_t> iterVol(*volumeData);
|
||||
/*VolumeSampler<VoxelType> iterVol(*volumeData);
|
||||
iterVol.setPosition(x,y,z);
|
||||
iterVol.setVoxel(value);*/
|
||||
volumeData->setVoxelAt(x,y,z,value);
|
||||
}
|
||||
|
||||
void VolumeChangeTracker::lockRegion(const Region& regToLock)
|
||||
template <typename VoxelType>
|
||||
void VolumeChangeTracker<VoxelType>::lockRegion(const Region& regToLock)
|
||||
{
|
||||
if(m_bIsLocked)
|
||||
{
|
||||
@ -162,7 +172,8 @@ namespace PolyVox
|
||||
m_bIsLocked = true;
|
||||
}
|
||||
|
||||
void VolumeChangeTracker::unlockRegion(void)
|
||||
template <typename VoxelType>
|
||||
void VolumeChangeTracker<VoxelType>::unlockRegion(void)
|
||||
{
|
||||
if(!m_bIsLocked)
|
||||
{
|
||||
@ -199,7 +210,8 @@ namespace PolyVox
|
||||
incrementCurrentTime();
|
||||
}
|
||||
|
||||
void VolumeChangeTracker::incrementCurrentTime(void)
|
||||
template <typename VoxelType>
|
||||
void VolumeChangeTracker<VoxelType>::incrementCurrentTime(void)
|
||||
{
|
||||
//Increment the current time.
|
||||
uint32_t time = m_uCurrentTime++;
|
@ -1,259 +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 "Serialization.h"
|
||||
|
||||
#include "Volume.h"
|
||||
#include "VolumeSampler.h"
|
||||
#include "PolyVoxImpl/Utility.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace PolyVox
|
||||
{
|
||||
//Note: we don't do much error handling in here - exceptions will simply be propergated up to the caller.
|
||||
//FIXME - think about pointer ownership issues. Or could return volume by value if the copy constructor is shallow
|
||||
shared_ptr< Volume<uint8_t> > loadVolumeRaw(istream& stream, VolumeSerializationProgressListener* progressListener)
|
||||
{
|
||||
//Read volume dimensions
|
||||
uint8_t volumeWidthPower = 0;
|
||||
uint8_t volumeHeightPower = 0;
|
||||
uint8_t volumeDepthPower = 0;
|
||||
stream.read(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
|
||||
stream.read(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
|
||||
stream.read(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
|
||||
|
||||
uint16_t volumeWidth = 0x0001 << volumeWidthPower;
|
||||
uint16_t volumeHeight = 0x0001 << volumeHeightPower;
|
||||
uint16_t volumeDepth = 0x0001 << volumeDepthPower;
|
||||
|
||||
//FIXME - need to support non cubic volumes
|
||||
shared_ptr< Volume<uint8_t> > volume(new Volume<uint8_t>(volumeWidth, volumeHeight, volumeDepth));
|
||||
|
||||
//Read data
|
||||
for(uint16_t z = 0; z < volumeDepth; ++z)
|
||||
{
|
||||
//Update progress once per slice.
|
||||
if(progressListener)
|
||||
{
|
||||
float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
|
||||
progressListener->onProgressUpdated(fProgress);
|
||||
}
|
||||
|
||||
for(uint16_t y = 0; y < volumeHeight; ++y)
|
||||
{
|
||||
for(uint16_t x = 0; x < volumeWidth; ++x)
|
||||
{
|
||||
uint8_t value = 0;
|
||||
stream.read(reinterpret_cast<char*>(&value), sizeof(value));
|
||||
|
||||
volume->setVoxelAt(x,y,z,value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Finished
|
||||
if(progressListener)
|
||||
{
|
||||
progressListener->onProgressUpdated(1.0f);
|
||||
}
|
||||
|
||||
return volume;
|
||||
}
|
||||
|
||||
void saveVolumeRaw(std::ostream& stream, Volume<uint8_t>& volume, VolumeSerializationProgressListener* progressListener)
|
||||
{
|
||||
//Write volume dimensions
|
||||
uint16_t volumeWidth = volume.getWidth();
|
||||
uint16_t volumeHeight = volume.getHeight();
|
||||
uint16_t volumeDepth = volume.getDepth();
|
||||
|
||||
uint8_t volumeWidthPower = logBase2(volumeWidth);
|
||||
uint8_t volumeHeightPower = logBase2(volumeHeight);
|
||||
uint8_t volumeDepthPower = logBase2(volumeDepth);
|
||||
|
||||
stream.write(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
|
||||
stream.write(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
|
||||
stream.write(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
|
||||
|
||||
//Write data
|
||||
VolumeSampler<uint8_t> volIter(&volume);
|
||||
for(uint16_t z = 0; z < volumeDepth; ++z)
|
||||
{
|
||||
//Update progress once per slice.
|
||||
if(progressListener)
|
||||
{
|
||||
float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
|
||||
progressListener->onProgressUpdated(fProgress);
|
||||
}
|
||||
|
||||
for(uint16_t y = 0; y < volumeHeight; ++y)
|
||||
{
|
||||
for(uint16_t x = 0; x < volumeWidth; ++x)
|
||||
{
|
||||
volIter.setPosition(x,y,z);
|
||||
uint8_t value = volIter.getVoxel();
|
||||
stream.write(reinterpret_cast<char*>(&value), sizeof(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Finished
|
||||
if(progressListener)
|
||||
{
|
||||
progressListener->onProgressUpdated(1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
//Note: we don't do much error handling in here - exceptions will simply be propergated up to the caller.
|
||||
//FIXME - think about pointer ownership issues. Or could return volume by value if the copy constructor is shallow
|
||||
shared_ptr< Volume<uint8_t> > loadVolumeRle(istream& stream, VolumeSerializationProgressListener* progressListener)
|
||||
{
|
||||
//Read volume dimensions
|
||||
uint8_t volumeWidthPower = 0;
|
||||
uint8_t volumeHeightPower = 0;
|
||||
uint8_t volumeDepthPower = 0;
|
||||
stream.read(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
|
||||
stream.read(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
|
||||
stream.read(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
|
||||
|
||||
uint16_t volumeWidth = 0x0001 << volumeWidthPower;
|
||||
uint16_t volumeHeight = 0x0001 << volumeHeightPower;
|
||||
uint16_t volumeDepth = 0x0001 << volumeDepthPower;
|
||||
|
||||
//FIXME - need to support non cubic volumes
|
||||
shared_ptr< Volume<uint8_t> > volume(new Volume<uint8_t>(volumeWidth, volumeHeight, volumeDepth));
|
||||
|
||||
//Read data
|
||||
bool firstTime = true;
|
||||
uint32_t runLength = 0;
|
||||
uint8_t value = 0;
|
||||
stream.read(reinterpret_cast<char*>(&value), sizeof(value));
|
||||
stream.read(reinterpret_cast<char*>(&runLength), sizeof(runLength));
|
||||
for(uint16_t z = 0; z < volumeDepth; ++z)
|
||||
{
|
||||
//Update progress once per slice.
|
||||
if(progressListener)
|
||||
{
|
||||
float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
|
||||
progressListener->onProgressUpdated(fProgress);
|
||||
}
|
||||
|
||||
for(uint16_t y = 0; y < volumeHeight; ++y)
|
||||
{
|
||||
for(uint16_t x = 0; x < volumeWidth; ++x)
|
||||
{
|
||||
if(runLength != 0)
|
||||
{
|
||||
volume->setVoxelAt(x,y,z,value);
|
||||
runLength--;
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.read(reinterpret_cast<char*>(&value), sizeof(value));
|
||||
stream.read(reinterpret_cast<char*>(&runLength), sizeof(runLength));
|
||||
|
||||
volume->setVoxelAt(x,y,z,value);
|
||||
runLength--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Finished
|
||||
if(progressListener)
|
||||
{
|
||||
progressListener->onProgressUpdated(1.0f);
|
||||
}
|
||||
|
||||
return volume;
|
||||
}
|
||||
|
||||
void saveVolumeRle(std::ostream& stream, Volume<uint8_t>& volume, VolumeSerializationProgressListener* progressListener)
|
||||
{
|
||||
//Write volume dimensions
|
||||
uint16_t volumeWidth = volume.getWidth();
|
||||
uint16_t volumeHeight = volume.getHeight();
|
||||
uint16_t volumeDepth = volume.getDepth();
|
||||
|
||||
uint8_t volumeWidthPower = logBase2(volumeWidth);
|
||||
uint8_t volumeHeightPower = logBase2(volumeHeight);
|
||||
uint8_t volumeDepthPower = logBase2(volumeDepth);
|
||||
|
||||
stream.write(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
|
||||
stream.write(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
|
||||
stream.write(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
|
||||
|
||||
//Write data
|
||||
VolumeSampler<uint8_t> volIter(&volume);
|
||||
uint8_t current = 0;
|
||||
uint32_t runLength = 0;
|
||||
bool firstTime = true;
|
||||
for(uint16_t z = 0; z < volumeDepth; ++z)
|
||||
{
|
||||
//Update progress once per slice.
|
||||
if(progressListener)
|
||||
{
|
||||
float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
|
||||
progressListener->onProgressUpdated(fProgress);
|
||||
}
|
||||
|
||||
for(uint16_t y = 0; y < volumeHeight; ++y)
|
||||
{
|
||||
for(uint16_t x = 0; x < volumeWidth; ++x)
|
||||
{
|
||||
volIter.setPosition(x,y,z);
|
||||
uint8_t value = volIter.getVoxel();
|
||||
if(firstTime)
|
||||
{
|
||||
current = value;
|
||||
runLength = 1;
|
||||
firstTime = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(value == current)
|
||||
{
|
||||
runLength++;
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.write(reinterpret_cast<char*>(¤t), sizeof(current));
|
||||
stream.write(reinterpret_cast<char*>(&runLength), sizeof(runLength));
|
||||
current = value;
|
||||
runLength = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
stream.write(reinterpret_cast<char*>(¤t), sizeof(current));
|
||||
stream.write(reinterpret_cast<char*>(&runLength), sizeof(runLength));
|
||||
|
||||
//Finished
|
||||
if(progressListener)
|
||||
{
|
||||
progressListener->onProgressUpdated(1.0f);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user