Massive changes to the organisation of PolyVoxCore and PolyVoxYtil.

Also added start of logging capability.
This commit is contained in:
David Williams
2009-04-03 21:36:22 +00:00
parent dbf7257edd
commit df1bf690c9
222 changed files with 3353 additions and 186 deletions

View File

@ -0,0 +1,51 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(PolyVoxUtil)
#Projects source files
SET(UTIL_SRC_FILES
source/Serialization.cpp
source/VolumeChangeTracker.cpp
)
#Projects headers files
SET(UTIL_INC_FILES
include/Export.h
include/Serialization.h
include/VolumeChangeTracker.h
)
ADD_DEFINITIONS(-DPOLYVOXUTIL_EXPORT) #Export symbols in the .dll
#Appends "_d" to the generated library when in debug mode
SET(CMAKE_DEBUG_POSTFIX "_d")
#"Sources" and "Headers" are the group names in Visual Studio.
#They may have other uses too...
SOURCE_GROUP("Sources" FILES ${UTIL_SRC_FILES})
SOURCE_GROUP("Headers" FILES ${UTIL_INC_FILES})
#Tell CMake the paths
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include ../PolyVoxCore/include)
#There has to be a better way!
LINK_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../PolyVoxCore/debug ${CMAKE_CURRENT_BINARY_DIR}/../PolyVoxCore/release)
#Util
#Build
ADD_LIBRARY(PolyVoxUtil SHARED ${UTIL_SRC_FILES} ${UTIL_INC_FILES})
TARGET_LINK_LIBRARIES(PolyVoxUtil debug PolyVoxCore_d optimized PolyVoxCore)
SET_TARGET_PROPERTIES(PolyVoxUtil PROPERTIES VERSION ${POLYVOX_VERSION} SOVERSION ${POLYVOX_VERSION_MAJOR})
IF(WIN32)
SET_TARGET_PROPERTIES(PolyVoxUtil PROPERTIES COMPILE_FLAGS "/wd4251") #Disable warning on STL exports
ENDIF(WIN32)
#Install
INSTALL(TARGETS PolyVoxUtil
RUNTIME DESTINATION PolyVoxUtil/bin
LIBRARY DESTINATION PolyVoxUtil/lib
ARCHIVE DESTINATION PolyVoxUtil/lib
COMPONENT library
)
#Install the util header files.
INSTALL(DIRECTORY include DESTINATION PolyVoxUtil COMPONENT development PATTERN "*.svn*" EXCLUDE)

View File

@ -0,0 +1,35 @@
#pragma region License
/******************************************************************************
This file is part of the PolyVox library
Copyright (C) 2006 David Williams
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#pragma endregion
#ifndef __PolyVoxUtil_Export_H__
#define __PolyVoxUtil_Export_H__
#ifdef WIN32
#ifdef POLYVOXUTIL_EXPORT
#define POLYVOXUTIL_API __declspec(dllexport)
#else
#define POLYVOXUTIL_API __declspec(dllimport)
#endif
#else
#define POLYVOXUTIL_API __attribute__ ((visibility("default")))
#endif
#endif

View File

@ -0,0 +1,40 @@
#pragma region License
/******************************************************************************
This file is part of the PolyVox library
Copyright (C) 2006 David Williams
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#pragma endregion
#ifndef __PolyVox_Serialization_H__
#define __PolyVox_Serialization_H__
#include "PolyVoxForwardDeclarations.h"
#include "Region.h"
#include "Export.h"
#include <iostream>
namespace PolyVox
{
POLYVOXUTIL_API Volume<uint8_t>* loadVolumeRaw(std::istream& stream);
POLYVOXUTIL_API void saveVolumeRaw(std::ostream& stream, Volume<uint8_t>& volume);
POLYVOXUTIL_API Volume<uint8_t>* loadVolumeRle(std::istream& stream);
POLYVOXUTIL_API void saveVolumeRle(std::ostream& stream, Volume<uint8_t>& volume);
}
#endif

View File

@ -0,0 +1,78 @@
#pragma region License
/******************************************************************************
This file is part of the PolyVox library
Copyright (C) 2006 David Williams
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#pragma endregion
#ifndef __PolyVox_VolumeChangeTracker_H__
#define __PolyVox_VolumeChangeTracker_H__
#include <list>
#include "PolyVoxForwardDeclarations.h"
#include "Region.h"
#include "Export.h"
namespace PolyVox
{
/// Voxel scene manager
class POLYVOXUTIL_API VolumeChangeTracker
{
public:
//Constructors, etc
VolumeChangeTracker(Volume<uint8_t>* volumeDataToSet, uint16_t regionSideLength);
~VolumeChangeTracker();
//Getters
int32_t getCurrentTime(void) const;
Region getEnclosingRegion(void) const;
int32_t getLastModifiedTimeForRegion(uint16_t uX, uint16_t uY, uint16_t uZ);
uint16_t getSideLength(void);
Volume<uint8_t>* getVolumeData(void) const;
uint8_t getVoxelAt(const Vector3DUint16& pos);
uint8_t getVoxelAt(uint16_t uX, uint16_t uY, uint16_t uZ);
//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);
//Others
void lockRegion(const Region& regToLock);
void unlockRegion(void);
//void markRegionChanged(uint16_t firstX, uint16_t firstY, uint16_t firstZ, uint16_t lastX, uint16_t lastY, uint16_t lastZ);
private:
bool m_bIsLocked;
Region m_regLastLocked;
Volume<uint8_t>* volumeData;
uint16_t m_uRegionSideLength;
uint8_t m_uRegionSideLengthPower;
uint16_t m_uVolumeSideLengthInRegions;
//It's not what the block class was designed for, but it
//provides a handy way of storing a 3D grid of values.
BlockData<int32_t>* volRegionLastModified;
static int32_t m_iCurrentTime;
};
}
#endif

View File

@ -0,0 +1,184 @@
#include "Serialization.h"
#include "Volume.h"
#include "VolumeIterator.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
Volume<uint8_t>* loadVolumeRaw(istream& stream)
{
//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
Volume<uint8_t>* volume = new Volume<uint8_t>(volumeWidth);
//Read data
for(uint16_t z = 0; z < volumeDepth; ++z)
{
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);
}
}
}
return volume;
}
void saveVolumeRaw(std::ostream& stream, Volume<uint8_t>& volume)
{
//Write volume dimensions
uint16_t volumeWidth = volume.getSideLength();
uint16_t volumeHeight = volume.getSideLength();
uint16_t volumeDepth = volume.getSideLength();
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
VolumeIterator<uint8_t> volIter(volume);
for(uint16_t z = 0; z < volumeDepth; ++z)
{
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));
}
}
}
}
//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
Volume<uint8_t>* loadVolumeRle(istream& stream)
{
//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
Volume<uint8_t>* volume = new Volume<uint8_t>(volumeWidth);
//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)
{
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--;
}
}
}
}
return volume;
}
void saveVolumeRle(std::ostream& stream, Volume<uint8_t>& volume)
{
//Write volume dimensions
uint16_t volumeWidth = volume.getSideLength();
uint16_t volumeHeight = volume.getSideLength();
uint16_t volumeDepth = volume.getSideLength();
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
VolumeIterator<uint8_t> volIter(volume);
uint8_t current = 0;
uint32_t runLength = 0;
bool firstTime = true;
for(uint16_t z = 0; z < volumeDepth; ++z)
{
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*>(&current), sizeof(current));
stream.write(reinterpret_cast<char*>(&runLength), sizeof(runLength));
current = value;
runLength = 1;
}
}
}
}
}
stream.write(reinterpret_cast<char*>(&current), sizeof(current));
stream.write(reinterpret_cast<char*>(&runLength), sizeof(runLength));
}
}

View File

@ -0,0 +1,216 @@
#pragma region License
/******************************************************************************
This file is part of the PolyVox library
Copyright (C) 2006 David Williams
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#pragma endregion
#include "VolumeChangeTracker.h"
#include "GradientEstimators.h"
#include "IndexedSurfacePatch.h"
#include "PolyVoxImpl/MarchingCubesTables.h"
#include "SurfaceExtractors.h"
#include "SurfaceVertex.h"
#include "PolyVoxImpl/Utility.h"
#include "Vector.h"
#include "Volume.h"
#include "VolumeIterator.h"
using namespace std;
namespace PolyVox
{
int32_t VolumeChangeTracker::m_iCurrentTime = 0;
//////////////////////////////////////////////////////////////////////////
// VolumeChangeTracker
//////////////////////////////////////////////////////////////////////////
VolumeChangeTracker::VolumeChangeTracker(Volume<uint8_t>* volumeDataToSet, uint16_t regionSideLength)
:m_bIsLocked(false)
,volumeData(0)
,m_uRegionSideLength(regionSideLength)
{
volumeData = volumeDataToSet;
m_uVolumeSideLengthInRegions = volumeData->getSideLength() / m_uRegionSideLength;
m_uRegionSideLengthPower = PolyVox::logBase2(m_uRegionSideLength);
volRegionLastModified = new BlockData<int32_t>(m_uRegionSideLength);
}
VolumeChangeTracker::~VolumeChangeTracker()
{
}
void VolumeChangeTracker::setAllRegionsModified(void)
{
for(uint16_t blockZ = 0; blockZ < m_uVolumeSideLengthInRegions; ++blockZ)
{
for(uint16_t blockY = 0; blockY < m_uVolumeSideLengthInRegions; ++blockY)
{
for(uint16_t blockX = 0; blockX < m_uVolumeSideLengthInRegions; ++blockX)
{
volRegionLastModified->setVoxelAt(blockX, blockY, blockZ, m_iCurrentTime);
++m_iCurrentTime;
}
}
}
}
int32_t VolumeChangeTracker::getCurrentTime(void) const
{
return m_iCurrentTime;
}
uint16_t VolumeChangeTracker::getSideLength(void)
{
return volumeData->getSideLength();
}
Region VolumeChangeTracker::getEnclosingRegion(void) const
{
return volumeData->getEnclosingRegion();
}
int32_t VolumeChangeTracker::getLastModifiedTimeForRegion(uint16_t uX, uint16_t uY, uint16_t uZ)
{
return volRegionLastModified->getVoxelAt(uX, uY, uZ);
}
uint8_t VolumeChangeTracker::getVoxelAt(const Vector3DUint16& pos)
{
return getVoxelAt(pos.getX(), pos.getY(), pos.getZ());
}
uint8_t VolumeChangeTracker::getVoxelAt(uint16_t uX, uint16_t uY, uint16_t uZ)
{
assert(uX < volumeData->getSideLength());
assert(uY < volumeData->getSideLength());
assert(uZ < volumeData->getSideLength());
VolumeIterator<uint8_t> volIter(*volumeData);
volIter.setPosition(uX,uY,uZ);
return volIter.getVoxel();
}
Volume<uint8_t>* VolumeChangeTracker::getVolumeData(void) const
{
return volumeData;
}
//NOTE - Document the fact that the time stamp is incremented at the start, not the end.
void VolumeChangeTracker::setVoxelAt(uint16_t x, uint16_t y, uint16_t z, uint8_t value)
{
++m_iCurrentTime;
//FIXME - rather than creating a iterator each time we should have one stored
//VolumeIterator<uint8_t> iterVol(*volumeData);
/*iterVol.setPosition(x,y,z);
iterVol.setVoxel(value);*/
volumeData->setVoxelAt(x,y,z,value);
//If we are not on a boundary, just mark one region.
if((x % m_uRegionSideLength != 0) &&
(x % m_uRegionSideLength != m_uRegionSideLength-1) &&
(y % m_uRegionSideLength != 0) &&
(y % m_uRegionSideLength != m_uRegionSideLength-1) &&
(z % m_uRegionSideLength != 0) &&
(z % m_uRegionSideLength != m_uRegionSideLength-1))
{
volRegionLastModified->setVoxelAt(x >> m_uRegionSideLengthPower, y >> m_uRegionSideLengthPower, z >> m_uRegionSideLengthPower, m_iCurrentTime);
}
else //Mark surrounding regions as well
{
const uint16_t regionX = x >> m_uRegionSideLengthPower;
const uint16_t regionY = y >> m_uRegionSideLengthPower;
const uint16_t regionZ = z >> m_uRegionSideLengthPower;
const uint16_t minRegionX = (std::max)(uint16_t(0),uint16_t(regionX-1));
const uint16_t minRegionY = (std::max)(uint16_t(0),uint16_t(regionY-1));
const uint16_t minRegionZ = (std::max)(uint16_t(0),uint16_t(regionZ-1));
const uint16_t maxRegionX = (std::min)(uint16_t(m_uVolumeSideLengthInRegions-1),uint16_t(regionX+1));
const uint16_t maxRegionY = (std::min)(uint16_t(m_uVolumeSideLengthInRegions-1),uint16_t(regionY+1));
const uint16_t maxRegionZ = (std::min)(uint16_t(m_uVolumeSideLengthInRegions-1),uint16_t(regionZ+1));
for(uint16_t zCt = minRegionZ; zCt <= maxRegionZ; zCt++)
{
for(uint16_t yCt = minRegionY; yCt <= maxRegionY; yCt++)
{
for(uint16_t xCt = minRegionX; xCt <= maxRegionX; xCt++)
{
volRegionLastModified->setVoxelAt(xCt,yCt,zCt,m_iCurrentTime);
}
}
}
}
//++m_iCurrentTime;
}
void VolumeChangeTracker::setLockedVoxelAt(uint16_t x, uint16_t y, uint16_t z, uint8_t value)
{
assert(m_bIsLocked);
//FIXME - rather than creating a iterator each time we should have one stored
/*VolumeIterator<uint8_t> iterVol(*volumeData);
iterVol.setPosition(x,y,z);
iterVol.setVoxel(value);*/
volumeData->setVoxelAt(x,y,z,value);
}
void VolumeChangeTracker::lockRegion(const Region& regToLock)
{
if(m_bIsLocked)
{
throw std::logic_error("A region is already locked. Please unlock it before locking another.");
}
m_regLastLocked = regToLock;
m_bIsLocked = true;
}
void VolumeChangeTracker::unlockRegion(void)
{
++m_iCurrentTime;
if(!m_bIsLocked)
{
throw std::logic_error("No region is locked. You must lock a region before you can unlock it.");
}
const uint16_t firstRegionX = m_regLastLocked.getLowerCorner().getX() >> m_uRegionSideLengthPower;
const uint16_t firstRegionY = m_regLastLocked.getLowerCorner().getY() >> m_uRegionSideLengthPower;
const uint16_t firstRegionZ = m_regLastLocked.getLowerCorner().getZ() >> m_uRegionSideLengthPower;
const uint16_t lastRegionX = m_regLastLocked.getUpperCorner().getX() >> m_uRegionSideLengthPower;
const uint16_t lastRegionY = m_regLastLocked.getUpperCorner().getY() >> m_uRegionSideLengthPower;
const uint16_t lastRegionZ = m_regLastLocked.getUpperCorner().getZ() >> m_uRegionSideLengthPower;
for(uint16_t zCt = firstRegionZ; zCt <= lastRegionZ; zCt++)
{
for(uint16_t yCt = firstRegionY; yCt <= lastRegionY; yCt++)
{
for(uint16_t xCt = firstRegionX; xCt <= lastRegionX; xCt++)
{
volRegionLastModified->setVoxelAt(xCt,yCt,zCt,m_iCurrentTime);
}
}
}
//++m_iCurrentTime;
m_bIsLocked = false;
}
}