Templatized Volume class.

This commit is contained in:
David Williams 2008-04-20 19:23:41 +00:00
parent 7177db44ac
commit 593a26ebd2
8 changed files with 47 additions and 36 deletions

View File

@ -7,7 +7,6 @@ SET(SRC_FILES
source/PolyVoxSceneManager.cpp
source/RegionGeometry.cpp
source/SurfaceVertex.cpp
source/Volume.cpp
source/VolumeIterator.cpp
)
@ -26,6 +25,7 @@ SET(INC_FILES
include/Vector.h
include/Vector.inl
include/Volume.h
include/Volume.inl
include/VolumeIterator.h
)

View File

@ -31,7 +31,7 @@ namespace PolyVox
class Vector3DDouble;
class Vector3DInt32;
class Vector3DUint32;*/
class Volume;
template <typename VoxelType> class Volume;
class VolumeIterator;
}

View File

@ -89,7 +89,7 @@ namespace PolyVox
NormalGenerationMethod m_normalGenerationMethod;
Volume* volumeData;
Volume<boost::uint8_t>* volumeData;
bool m_bHaveGeneratedMeshes;

View File

@ -28,7 +28,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
namespace PolyVox
{
class POLYVOX_API Volume
template <typename VoxelType>
class Volume
{
//Make VolumeIterator a friend
friend class VolumeIterator;
@ -43,7 +44,7 @@ namespace PolyVox
Volume& operator=(const Volume& rhs);
public:
Block<boost::uint8_t>* getBlock(boost::uint16_t index);
Block<VoxelType>* getBlock(boost::uint16_t index);
bool containsPoint(Vector3DFloat pos, float boundary);
bool containsPoint(Vector3DInt32 pos, boost::uint16_t boundary);
@ -55,8 +56,10 @@ namespace PolyVox
void tidy(void);
private:
Block<boost::uint8_t>* mBlocks[POLYVOX_NO_OF_BLOCKS_IN_VOLUME];
Block<VoxelType>* mBlocks[POLYVOX_NO_OF_BLOCKS_IN_VOLUME];
};
}
#include "Volume.inl"
#endif

View File

@ -25,34 +25,36 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "Volume.h"
#include "VolumeIterator.h" //Maybe this shouldn't be here?
using namespace boost;
namespace PolyVox
{
Volume::Volume()
template <typename VoxelType>
Volume<VoxelType>::Volume()
{
for(uint16_t i = 0; i < POLYVOX_NO_OF_BLOCKS_IN_VOLUME; ++i)
for(boost::uint16_t i = 0; i < POLYVOX_NO_OF_BLOCKS_IN_VOLUME; ++i)
{
mBlocks[i] = new Block<boost::uint8_t>;
mBlocks[i] = new Block<VoxelType>;
}
}
Volume::Volume(const Volume& rhs)
template <typename VoxelType>
Volume<VoxelType>::Volume(const Volume<VoxelType>& rhs)
{
std::cout << "Warning - Copying Volume" << std::endl;
*this = rhs;
}
Volume::~Volume()
template <typename VoxelType>
Volume<VoxelType>::~Volume()
{
for(uint16_t i = 0; i < POLYVOX_NO_OF_BLOCKS_IN_VOLUME; ++i)
for(boost::uint16_t i = 0; i < POLYVOX_NO_OF_BLOCKS_IN_VOLUME; ++i)
{
delete mBlocks[i];
}
}
Volume& Volume::operator=(const Volume& rhs)
template <typename VoxelType>
Volume<VoxelType>& Volume<VoxelType>::operator=(const Volume& rhs)
{
std::cout << "Warning - Assigning Volume" << std::endl;
if (this == &rhs)
@ -124,12 +126,14 @@ namespace PolyVox
block->setVoxelAt(xOffset,yOffset,zOffset, value);
}*/
Block<boost::uint8_t>* Volume::getBlock(uint16_t index)
template <typename VoxelType>
Block<VoxelType>* Volume<VoxelType>::getBlock(boost::uint16_t index)
{
return mBlocks[index];
}
bool Volume::containsPoint(Vector3DFloat pos, float boundary)
template <typename VoxelType>
bool Volume<VoxelType>::containsPoint(Vector3DFloat pos, float boundary)
{
return (pos.x() < POLYVOX_VOLUME_SIDE_LENGTH - 1 - boundary)
&& (pos.y() < POLYVOX_VOLUME_SIDE_LENGTH - 1 - boundary)
@ -139,7 +143,8 @@ namespace PolyVox
&& (pos.z() > boundary);
}
bool Volume::containsPoint(Vector3DInt32 pos, uint16_t boundary)
template <typename VoxelType>
bool Volume<VoxelType>::containsPoint(Vector3DInt32 pos, boost::uint16_t boundary)
{
return (pos.x() < POLYVOX_VOLUME_SIDE_LENGTH - 1 - boundary)
&& (pos.y() < POLYVOX_VOLUME_SIDE_LENGTH - 1 - boundary)
@ -149,7 +154,8 @@ namespace PolyVox
&& (pos.z() > boundary);
}
bool Volume::loadFromFile(const std::string& sFilename)
template <typename VoxelType>
bool Volume<VoxelType>::loadFromFile(const std::string& sFilename)
{
//Open the file
std::ifstream file;
@ -196,7 +202,8 @@ namespace PolyVox
return true;
}
bool Volume::saveToFile(const std::string& sFilename)
template <typename VoxelType>
bool Volume<VoxelType>::saveToFile(const std::string& sFilename)
{
//Open the file
std::ofstream file;
@ -209,9 +216,9 @@ namespace PolyVox
}
//Read volume dimensions
uint8_t volumeWidth = 0;
uint8_t volumeHeight = 0;
uint8_t volumeDepth = 0;
boost::uint8_t volumeWidth = 0;
boost::uint8_t volumeHeight = 0;
boost::uint8_t volumeDepth = 0;
file.write(reinterpret_cast<char*>(&volumeWidth), sizeof(volumeWidth));
file.write(reinterpret_cast<char*>(&volumeHeight), sizeof(volumeHeight));
file.write(reinterpret_cast<char*>(&volumeDepth), sizeof(volumeDepth));
@ -223,13 +230,13 @@ namespace PolyVox
//Write data
VolumeIterator volIter(*this);
for(uint16_t z = 0; z < POLYVOX_VOLUME_SIDE_LENGTH; ++z)
for(boost::uint16_t z = 0; z < POLYVOX_VOLUME_SIDE_LENGTH; ++z)
{
for(uint16_t y = 0; y < POLYVOX_VOLUME_SIDE_LENGTH; ++y)
for(boost::uint16_t y = 0; y < POLYVOX_VOLUME_SIDE_LENGTH; ++y)
{
for(uint16_t x = 0; x < POLYVOX_VOLUME_SIDE_LENGTH; ++x)
for(boost::uint16_t x = 0; x < POLYVOX_VOLUME_SIDE_LENGTH; ++x)
{
uint8_t value = volIter.getVoxelAt(x,y,z);
boost::uint8_t value = volIter.getVoxelAt(x,y,z);
file.write(reinterpret_cast<char*>(&value), sizeof(value)); //FIXME - check for error here
}
}
@ -237,7 +244,8 @@ namespace PolyVox
return true;
}
void Volume::regionGrow(uint16_t xStart, uint16_t yStart, uint16_t zStart, uint8_t value)
template <typename VoxelType>
void Volume<VoxelType>::regionGrow(boost::uint16_t xStart, boost::uint16_t yStart, boost::uint16_t zStart, boost::uint8_t value)
{
//FIXME - introduce integrer 'isInVolume' function
if((xStart > POLYVOX_VOLUME_SIDE_LENGTH-1) || (yStart > POLYVOX_VOLUME_SIDE_LENGTH-1) || (zStart > POLYVOX_VOLUME_SIDE_LENGTH-1)
@ -248,7 +256,7 @@ namespace PolyVox
}
VolumeIterator volIter(*this);
const uint8_t uSeedValue = volIter.getVoxelAt(xStart,yStart,zStart);
const boost::uint8_t uSeedValue = volIter.getVoxelAt(xStart,yStart,zStart);
if(value == uSeedValue)
{
@ -310,7 +318,8 @@ namespace PolyVox
}
}
void Volume::tidy(void)
template <typename VoxelType>
void Volume<VoxelType>::tidy(void)
{
//Check for homogeneous blocks
/*for(uint32_t ct = 0; ct < POLYVOX_NO_OF_BLOCKS_IN_VOLUME; ++ct)

View File

@ -21,17 +21,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "boost/cstdint.hpp"
#include "PolyVoxForwardDeclarations.h"
#include "TypeDef.h"
#include "Vector.h"
namespace PolyVox
{
class Volume;
class POLYVOX_API VolumeIterator
{
public:
VolumeIterator(Volume& volume);
VolumeIterator(Volume<boost::uint8_t>& volume);
~VolumeIterator();
void setVoxel(boost::uint8_t value);
@ -89,7 +88,7 @@ namespace PolyVox
private:
//The current volume
Volume& mVolume;
Volume<boost::uint8_t>& mVolume;
//The current position in the volume
boost::uint16_t mXPosInVolume;

View File

@ -151,7 +151,7 @@ namespace PolyVox
void PolyVoxSceneManager::generateLevelVolume(void)
{
//volumeData = VolumePtr(new Volume);
volumeData = new Volume();
volumeData = new Volume<boost::uint8_t>();
VolumeIterator volIter(*volumeData);
for(uint16_t z = 0; z < POLYVOX_VOLUME_SIDE_LENGTH; ++z)
{

View File

@ -25,7 +25,7 @@ using namespace boost;
namespace PolyVox
{
VolumeIterator::VolumeIterator(Volume& volume)
VolumeIterator::VolumeIterator(Volume<boost::uint8_t>& volume)
:mVolume(volume)
,mXRegionFirst(0)
,mYRegionFirst(0)