diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl index a4db69d1..aa228dc7 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl @@ -36,10 +36,8 @@ namespace PolyVox { template LargeVolume::Sampler::Sampler(LargeVolume* volume) - //:mVolume(volume) + :Volume::Sampler< LargeVolume >(volume) { - //Dodgy doing this - need to find how to call base constructor - this->mVolume = volume; } template diff --git a/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl index d1b9d736..cd18672e 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl @@ -31,15 +31,13 @@ namespace PolyVox { template RawVolume::Sampler::Sampler(RawVolume* volume) - //:mVolume(volume) - :mXPosInVolume(0) + :Volume::Sampler< RawVolume >(volume) + ,mXPosInVolume(0) ,mYPosInVolume(0) ,mZPosInVolume(0) ,mCurrentVoxel(0) ,m_bIsCurrentPositionValid(false) { - //Dodgy doing this - need to find how to call base constructor - this->mVolume = volume; } template diff --git a/library/PolyVoxCore/include/PolyVoxCore/Region.h b/library/PolyVoxCore/include/PolyVoxCore/Region.h index 60ce2ba6..5888df0d 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Region.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Region.h @@ -41,6 +41,7 @@ namespace PolyVox Region(); Region(const Vector3DInt32& v3dLowerCorner, const Vector3DInt32& v3dUpperCorner); + Region(int32_t iLowerX, int32_t iLowerY, int32_t iLowerZ, int32_t iUpperX, int32_t iUpperY, int32_t iUpperZ); const Vector3DInt32& getLowerCorner(void) const; const Vector3DInt32& getUpperCorner(void) const; diff --git a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl index 12d2366c..dc4c74a4 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl @@ -36,10 +36,8 @@ namespace PolyVox { template SimpleVolume::Sampler::Sampler(SimpleVolume* volume) - //:mVolume(volume) + :Volume::Sampler< SimpleVolume >(volume) { - //Dodgy doing this - need to find how to call base constructor - this->mVolume = volume; } template diff --git a/library/PolyVoxCore/include/PolyVoxCore/Volume.h b/library/PolyVoxCore/include/PolyVoxCore/Volume.h index 780db379..b9d367aa 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Volume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Volume.h @@ -41,6 +41,9 @@ namespace PolyVox class Sampler { public: + Sampler(DerivedVolumeType* volume); + ~Sampler(); + int32_t getPosX(void) const; int32_t getPosY(void) const; int32_t getPosZ(void) const; diff --git a/library/PolyVoxCore/include/PolyVoxCore/VolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/VolumeSampler.inl index 25d6014d..4b7394dd 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/VolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/VolumeSampler.inl @@ -29,6 +29,22 @@ freely, subject to the following restrictions: #include namespace PolyVox { + template + template + Volume::Sampler::Sampler(DerivedVolumeType* volume) + :mVolume(volume) + ,mXPos(0) + ,mYPos(0) + ,mZPos(0) + { + } + + template + template + Volume::Sampler::~Sampler() + { + } + template template int32_t Volume::Sampler::getPosX(void) const @@ -54,7 +70,7 @@ namespace PolyVox template VoxelType Volume::Sampler::getVoxel(void) const { - mVolume->getVoxelAt(mXPos, mYPos, mZPos); + return mVolume->getVoxelAt(mXPos, mYPos, mZPos); } template @@ -121,63 +137,63 @@ namespace PolyVox template VoxelType Volume::Sampler::peekVoxel1nx1ny1nz(void) const { - mVolume->getVoxelAt(mXPos - 1, mYPos - 1, mZPos - 1); + return mVolume->getVoxelAt(mXPos - 1, mYPos - 1, mZPos - 1); } template template VoxelType Volume::Sampler::peekVoxel1nx1ny0pz(void) const { - mVolume->getVoxelAt(mXPos - 1, mYPos - 1, mZPos ); + return mVolume->getVoxelAt(mXPos - 1, mYPos - 1, mZPos ); } template template VoxelType Volume::Sampler::peekVoxel1nx1ny1pz(void) const { - mVolume->getVoxelAt(mXPos - 1, mYPos - 1, mZPos + 1); + return mVolume->getVoxelAt(mXPos - 1, mYPos - 1, mZPos + 1); } template template VoxelType Volume::Sampler::peekVoxel1nx0py1nz(void) const { - mVolume->getVoxelAt(mXPos - 1, mYPos , mZPos - 1); + return mVolume->getVoxelAt(mXPos - 1, mYPos , mZPos - 1); } template template VoxelType Volume::Sampler::peekVoxel1nx0py0pz(void) const { - mVolume->getVoxelAt(mXPos - 1, mYPos , mZPos ); + return mVolume->getVoxelAt(mXPos - 1, mYPos , mZPos ); } template template VoxelType Volume::Sampler::peekVoxel1nx0py1pz(void) const { - mVolume->getVoxelAt(mXPos - 1, mYPos , mZPos + 1); + return mVolume->getVoxelAt(mXPos - 1, mYPos , mZPos + 1); } template template VoxelType Volume::Sampler::peekVoxel1nx1py1nz(void) const { - mVolume->getVoxelAt(mXPos - 1, mYPos + 1, mZPos - 1); + return mVolume->getVoxelAt(mXPos - 1, mYPos + 1, mZPos - 1); } template template VoxelType Volume::Sampler::peekVoxel1nx1py0pz(void) const { - mVolume->getVoxelAt(mXPos - 1, mYPos + 1, mZPos ); + return mVolume->getVoxelAt(mXPos - 1, mYPos + 1, mZPos ); } template template VoxelType Volume::Sampler::peekVoxel1nx1py1pz(void) const { - mVolume->getVoxelAt(mXPos - 1, mYPos + 1, mZPos + 1); + return mVolume->getVoxelAt(mXPos - 1, mYPos + 1, mZPos + 1); } ////////////////////////////////////////////////////////////////////////// @@ -186,63 +202,63 @@ namespace PolyVox template VoxelType Volume::Sampler::peekVoxel0px1ny1nz(void) const { - mVolume->getVoxelAt(mXPos , mYPos - 1, mZPos - 1); + return mVolume->getVoxelAt(mXPos , mYPos - 1, mZPos - 1); } template template VoxelType Volume::Sampler::peekVoxel0px1ny0pz(void) const { - mVolume->getVoxelAt(mXPos , mYPos - 1, mZPos ); + return mVolume->getVoxelAt(mXPos , mYPos - 1, mZPos ); } template template VoxelType Volume::Sampler::peekVoxel0px1ny1pz(void) const { - mVolume->getVoxelAt(mXPos , mYPos - 1, mZPos + 1); + return mVolume->getVoxelAt(mXPos , mYPos - 1, mZPos + 1); } template template VoxelType Volume::Sampler::peekVoxel0px0py1nz(void) const { - mVolume->getVoxelAt(mXPos , mYPos , mZPos - 1); + return mVolume->getVoxelAt(mXPos , mYPos , mZPos - 1); } template template VoxelType Volume::Sampler::peekVoxel0px0py0pz(void) const { - mVolume->getVoxelAt(mXPos , mYPos , mZPos ); + return mVolume->getVoxelAt(mXPos , mYPos , mZPos ); } template template VoxelType Volume::Sampler::peekVoxel0px0py1pz(void) const { - mVolume->getVoxelAt(mXPos , mYPos , mZPos + 1); + return mVolume->getVoxelAt(mXPos , mYPos , mZPos + 1); } template template VoxelType Volume::Sampler::peekVoxel0px1py1nz(void) const { - mVolume->getVoxelAt(mXPos , mYPos + 1, mZPos - 1); + return mVolume->getVoxelAt(mXPos , mYPos + 1, mZPos - 1); } template template VoxelType Volume::Sampler::peekVoxel0px1py0pz(void) const { - mVolume->getVoxelAt(mXPos , mYPos + 1, mZPos ); + return mVolume->getVoxelAt(mXPos , mYPos + 1, mZPos ); } template template VoxelType Volume::Sampler::peekVoxel0px1py1pz(void) const { - mVolume->getVoxelAt(mXPos , mYPos + 1, mZPos + 1); + return mVolume->getVoxelAt(mXPos , mYPos + 1, mZPos + 1); } ////////////////////////////////////////////////////////////////////////// @@ -251,62 +267,62 @@ namespace PolyVox template VoxelType Volume::Sampler::peekVoxel1px1ny1nz(void) const { - mVolume->getVoxelAt(mXPos + 1, mYPos - 1, mZPos - 1); + return mVolume->getVoxelAt(mXPos + 1, mYPos - 1, mZPos - 1); } template template VoxelType Volume::Sampler::peekVoxel1px1ny0pz(void) const { - mVolume->getVoxelAt(mXPos + 1, mYPos - 1, mZPos ); + return mVolume->getVoxelAt(mXPos + 1, mYPos - 1, mZPos ); } template template VoxelType Volume::Sampler::peekVoxel1px1ny1pz(void) const { - mVolume->getVoxelAt(mXPos + 1, mYPos - 1, mZPos + 1); + return mVolume->getVoxelAt(mXPos + 1, mYPos - 1, mZPos + 1); } template template VoxelType Volume::Sampler::peekVoxel1px0py1nz(void) const { - mVolume->getVoxelAt(mXPos + 1, mYPos , mZPos - 1); + return mVolume->getVoxelAt(mXPos + 1, mYPos , mZPos - 1); } template template VoxelType Volume::Sampler::peekVoxel1px0py0pz(void) const { - mVolume->getVoxelAt(mXPos + 1, mYPos , mZPos ); + return mVolume->getVoxelAt(mXPos + 1, mYPos , mZPos ); } template template VoxelType Volume::Sampler::peekVoxel1px0py1pz(void) const { - mVolume->getVoxelAt(mXPos + 1, mYPos , mZPos + 1); + return mVolume->getVoxelAt(mXPos + 1, mYPos , mZPos + 1); } template template VoxelType Volume::Sampler::peekVoxel1px1py1nz(void) const { - mVolume->getVoxelAt(mXPos + 1, mYPos + 1, mZPos - 1); + return mVolume->getVoxelAt(mXPos + 1, mYPos + 1, mZPos - 1); } template template VoxelType Volume::Sampler::peekVoxel1px1py0pz(void) const { - mVolume->getVoxelAt(mXPos + 1, mYPos + 1, mZPos ); + return mVolume->getVoxelAt(mXPos + 1, mYPos + 1, mZPos ); } template template VoxelType Volume::Sampler::peekVoxel1px1py1pz(void) const { - mVolume->getVoxelAt(mXPos + 1, mYPos + 1, mZPos + 1); + return mVolume->getVoxelAt(mXPos + 1, mYPos + 1, mZPos + 1); } } diff --git a/library/PolyVoxCore/source/Region.cpp b/library/PolyVoxCore/source/Region.cpp index e0eaa1c1..8b6491d5 100644 --- a/library/PolyVoxCore/source/Region.cpp +++ b/library/PolyVoxCore/source/Region.cpp @@ -50,6 +50,16 @@ namespace PolyVox assert(m_v3dUpperCorner.getZ() >= m_v3dLowerCorner.getZ()); } + Region::Region(int32_t iLowerX, int32_t iLowerY, int32_t iLowerZ, int32_t iUpperX, int32_t iUpperY, int32_t iUpperZ) + :m_v3dLowerCorner(iLowerX, iLowerY, iLowerZ) + ,m_v3dUpperCorner(iUpperX, iUpperY, iUpperZ) + { + //Check the region is valid. + assert(m_v3dUpperCorner.getX() >= m_v3dLowerCorner.getX()); + assert(m_v3dUpperCorner.getY() >= m_v3dLowerCorner.getY()); + assert(m_v3dUpperCorner.getZ() >= m_v3dLowerCorner.getZ()); + } + const Vector3DInt32& Region::getLowerCorner(void) const { return m_v3dLowerCorner; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 877e7fe5..39a68d7b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -59,3 +59,7 @@ ADD_TEST(MaterialTestCompile ${LATEST_TEST} testCompile) CREATE_TEST(testvector.h testvector.cpp testvector) ADD_TEST(VectorLengthTest ${LATEST_TEST} testLength) ADD_TEST(VectorDotProductTest ${LATEST_TEST} testDotProduct) + +# Volume subclass tests +CREATE_TEST(TestVolumeSubclass.h TestVolumeSubclass.cpp TestVolumeSubclass) +ADD_TEST(VolumeSubclassExtractSurfaceTest ${LATEST_TEST} testExtractSurface) diff --git a/tests/TestVolumeSubclass.cpp b/tests/TestVolumeSubclass.cpp new file mode 100644 index 00000000..2c42d954 --- /dev/null +++ b/tests/TestVolumeSubclass.cpp @@ -0,0 +1,133 @@ +/******************************************************************************* +Copyright (c) 2010 Matt 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 "TestVolumeSubclass.h" + +#include "PolyVoxCore/Array.h" +#include "PolyVoxCore/CubicSurfaceExtractor.h" +#include "PolyVoxCore/Material.h" +#include "PolyVoxCore/Vector.h" +#include "PolyVoxCore/Volume.h" + +#include + +using namespace PolyVox; + +template +class VolumeSubclass : public Volume +{ +public: + +#if defined(_MSC_VER) //DIRTY HACK!!! + class Sampler : public Volume::Sampler< VolumeSubclass > +#else + class Sampler : public Volume::template Sampler< VolumeSubclass > +#endif + { + public: + Sampler(VolumeSubclass* volume) + :Volume::Sampler< VolumeSubclass >(volume) + { + this->mVolume = volume; + } + //~Sampler(); + }; + + /// Constructor for creating a fixed size volume. + VolumeSubclass(const Region& regValid) + :Volume(regValid) + { + mVolumeData.resize(ArraySizes(this->getWidth())(this->getHeight())(this->getDepth())); + } + /// Destructor + ~VolumeSubclass() {}; + + /// Gets the value used for voxels which are outside the volume + VoxelType getBorderValue(void) const { return 0; } + /// Gets a voxel at the position given by x,y,z coordinates + VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const + { + if(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos))) + { + return mVolumeData[uXPos][uYPos][uZPos]; + } + else + { + return getBorderValue(); + } + } + /// Gets a voxel at the position given by a 3D vector + VoxelType getVoxelAt(const Vector3DInt32& v3dPos) const { return getVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ()); } + + /// Sets the value used for voxels which are outside the volume + void setBorderValue(const VoxelType& tBorder) { } + /// Sets the voxel at the position given by x,y,z coordinates + bool setVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos, VoxelType tValue) + { + if(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos))) + { + mVolumeData[uXPos][uYPos][uZPos] = tValue; + return true; + } + else + { + return false; + } + } + /// Sets the voxel at the position given by a 3D vector + bool setVoxelAt(const Vector3DInt32& v3dPos, VoxelType tValue) { return setVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), tValue); } + + /// Calculates approximatly how many bytes of memory the volume is currently using. + uint32_t calculateSizeInBytes(void) { return 0; } + + /// Deprecated - I don't think we should expose this function? Let us know if you disagree... + //void resize(const Region& regValidRegion); + +private: + Array<3, VoxelType> mVolumeData; +}; + +void TestVolumeSubclass::testExtractSurface() +{ + VolumeSubclass volumeSubclass(Region(0,0,0,16,16,16)); + + for(int32_t z = 0; z < volumeSubclass.getDepth() / 2; z++) + { + for(int32_t y = 0; y < volumeSubclass.getHeight(); y++) + { + for(int32_t x = 0; x < volumeSubclass.getWidth(); x++) + { + Material8 mat(1); + volumeSubclass.setVoxelAt(Vector3DInt32(x,y,z),mat); + } + } + } + + SurfaceMesh result; + CubicSurfaceExtractor cubicSurfaceExtractor(&volumeSubclass, volumeSubclass.getEnclosingRegion(), &result); + cubicSurfaceExtractor.execute(); + + QCOMPARE(result.getNoOfVertices(), static_cast(8)); +} + +QTEST_MAIN(TestVolumeSubclass) diff --git a/tests/TestVolumeSubclass.h b/tests/TestVolumeSubclass.h new file mode 100644 index 00000000..46e86bd8 --- /dev/null +++ b/tests/TestVolumeSubclass.h @@ -0,0 +1,37 @@ +/******************************************************************************* +Copyright (c) 2010 Matt 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. +*******************************************************************************/ + +#ifndef __PolyVox_TestVolumeSubclass_H__ +#define __PolyVox_TestVolumeSubclass_H__ + +#include + +class TestVolumeSubclass: public QObject +{ + Q_OBJECT + + private slots: + void testExtractSurface(); +}; + +#endif