From 593a26ebd2d87ba81a8ae8e93e04912d597e24c5 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 20 Apr 2008 19:23:41 +0000 Subject: [PATCH] Templatized Volume class. --- CMakeLists.txt | 2 +- include/PolyVoxForwardDeclarations.h | 2 +- include/PolyVoxSceneManager.h | 2 +- include/Volume.h | 9 ++-- source/Volume.cpp => include/Volume.inl | 57 ++++++++++++++----------- include/VolumeIterator.h | 7 ++- source/PolyVoxSceneManager.cpp | 2 +- source/VolumeIterator.cpp | 2 +- 8 files changed, 47 insertions(+), 36 deletions(-) rename source/Volume.cpp => include/Volume.inl (83%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 32772e35..8bae641d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) diff --git a/include/PolyVoxForwardDeclarations.h b/include/PolyVoxForwardDeclarations.h index 75d6a71b..52b1e25c 100644 --- a/include/PolyVoxForwardDeclarations.h +++ b/include/PolyVoxForwardDeclarations.h @@ -31,7 +31,7 @@ namespace PolyVox class Vector3DDouble; class Vector3DInt32; class Vector3DUint32;*/ - class Volume; + template class Volume; class VolumeIterator; } diff --git a/include/PolyVoxSceneManager.h b/include/PolyVoxSceneManager.h index 605eb6e3..4a894cf8 100644 --- a/include/PolyVoxSceneManager.h +++ b/include/PolyVoxSceneManager.h @@ -89,7 +89,7 @@ namespace PolyVox NormalGenerationMethod m_normalGenerationMethod; - Volume* volumeData; + Volume* volumeData; bool m_bHaveGeneratedMeshes; diff --git a/include/Volume.h b/include/Volume.h index 5fd087ce..9e343b48 100644 --- a/include/Volume.h +++ b/include/Volume.h @@ -28,7 +28,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. namespace PolyVox { - class POLYVOX_API Volume + template + class Volume { //Make VolumeIterator a friend friend class VolumeIterator; @@ -43,7 +44,7 @@ namespace PolyVox Volume& operator=(const Volume& rhs); public: - Block* getBlock(boost::uint16_t index); + Block* 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* mBlocks[POLYVOX_NO_OF_BLOCKS_IN_VOLUME]; + Block* mBlocks[POLYVOX_NO_OF_BLOCKS_IN_VOLUME]; }; } +#include "Volume.inl" + #endif diff --git a/source/Volume.cpp b/include/Volume.inl similarity index 83% rename from source/Volume.cpp rename to include/Volume.inl index 7673563e..2427f26b 100644 --- a/source/Volume.cpp +++ b/include/Volume.inl @@ -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 + Volume::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; + mBlocks[i] = new Block; } } - Volume::Volume(const Volume& rhs) + template + Volume::Volume(const Volume& rhs) { std::cout << "Warning - Copying Volume" << std::endl; *this = rhs; } - Volume::~Volume() + template + Volume::~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 + Volume& Volume::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* Volume::getBlock(uint16_t index) + template + Block* Volume::getBlock(boost::uint16_t index) { return mBlocks[index]; } - bool Volume::containsPoint(Vector3DFloat pos, float boundary) + template + bool Volume::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 + bool Volume::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 + bool Volume::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 + bool Volume::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(&volumeWidth), sizeof(volumeWidth)); file.write(reinterpret_cast(&volumeHeight), sizeof(volumeHeight)); file.write(reinterpret_cast(&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(&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 + void Volume::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 + void Volume::tidy(void) { //Check for homogeneous blocks /*for(uint32_t ct = 0; ct < POLYVOX_NO_OF_BLOCKS_IN_VOLUME; ++ct) diff --git a/include/VolumeIterator.h b/include/VolumeIterator.h index 468daa28..a0f7b915 100644 --- a/include/VolumeIterator.h +++ b/include/VolumeIterator.h @@ -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& volume); ~VolumeIterator(); void setVoxel(boost::uint8_t value); @@ -89,7 +88,7 @@ namespace PolyVox private: //The current volume - Volume& mVolume; + Volume& mVolume; //The current position in the volume boost::uint16_t mXPosInVolume; diff --git a/source/PolyVoxSceneManager.cpp b/source/PolyVoxSceneManager.cpp index 69d4df4d..dab1a082 100644 --- a/source/PolyVoxSceneManager.cpp +++ b/source/PolyVoxSceneManager.cpp @@ -151,7 +151,7 @@ namespace PolyVox void PolyVoxSceneManager::generateLevelVolume(void) { //volumeData = VolumePtr(new Volume); - volumeData = new Volume(); + volumeData = new Volume(); VolumeIterator volIter(*volumeData); for(uint16_t z = 0; z < POLYVOX_VOLUME_SIDE_LENGTH; ++z) { diff --git a/source/VolumeIterator.cpp b/source/VolumeIterator.cpp index 50feba6b..8a61345f 100644 --- a/source/VolumeIterator.cpp +++ b/source/VolumeIterator.cpp @@ -25,7 +25,7 @@ using namespace boost; namespace PolyVox { - VolumeIterator::VolumeIterator(Volume& volume) + VolumeIterator::VolumeIterator(Volume& volume) :mVolume(volume) ,mXRegionFirst(0) ,mYRegionFirst(0)