From 528873bcd3c100270cb68ba5ddb4a5265913d648 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 24 Aug 2014 21:54:59 +0200 Subject: [PATCH] Extended 'Array2D class to be multidimensional (will rename it shortly). --- .../PolyVoxCore/include/PolyVoxCore/Array.h | 8 +- .../include/PolyVoxCore/Impl/Array2D.h | 103 +++++++++++++++--- .../PolyVoxCore/PolyVoxForwardDeclarations.h | 8 +- tests/TestArray.cpp | 60 ++++++---- 4 files changed, 131 insertions(+), 48 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/Array.h b/library/PolyVoxCore/include/PolyVoxCore/Array.h index ae59524a..9ee935ba 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Array.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Array.h @@ -154,7 +154,7 @@ namespace PolyVox //Some handy typedefs ///A 1D Array of floats. - typedef Array<1,float> Array1DFloat; + /*typedef Array<1,float> Array1DFloat; ///A 1D Array of doubles. typedef Array<1,double> Array1DDouble; ///A 1D Array of signed 8-bit values. @@ -168,7 +168,7 @@ namespace PolyVox ///A 1D Array of signed 32-bit values. typedef Array<1,int32_t> Array1DInt32; ///A 1D Array of unsigned 32-bit values. - typedef Array<1,uint32_t> Array1DUint32; + typedef Array<1,uint32_t> Array1DUint32;*/ ///A 2D Array of floats. /*typedef Array<2,float> Array2DFloat; @@ -188,7 +188,7 @@ namespace PolyVox typedef Array<2,uint32_t> Array2DUint32;*/ ///A 3D Array of floats. - typedef Array<3,float> Array3DFloat; + /*typedef Array<3,float> Array3DFloat; ///A 3D Array of doubles. typedef Array<3,double> Array3DDouble; ///A 3D Array of signed 8-bit values. @@ -202,7 +202,7 @@ namespace PolyVox ///A 3D Array of signed 32-bit values. typedef Array<3,int32_t> Array3DInt32; ///A 3D Array of unsigned 32-bit values. - typedef Array<3,uint32_t> Array3DUint32; + typedef Array<3,uint32_t> Array3DUint32;*/ }//namespace PolyVox #include "PolyVoxCore/Array.inl" diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Array2D.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Array2D.h index be383e98..6eaeace4 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Array2D.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Array2D.h @@ -24,6 +24,10 @@ distribution. #ifndef __PolyVox_Array2D_H__ #define __PolyVox_Array2D_H__ +#include + +#include + namespace PolyVox { template @@ -31,51 +35,84 @@ namespace PolyVox { public: - Array2D(uint32_t width, uint32_t height) - :m_pData(0) + Array2D(uint32_t width) + :m_pElements(0) { + static_assert(noOfDims == 1, "This constructor can only be used with a one-dimensional array"); + + m_uDimensions[0] = width; + + initialize(); + } + + Array2D(uint32_t width, uint32_t height) + :m_pElements(0) + { + static_assert(noOfDims == 2, "This constructor can only be used with a two-dimensional array"); + m_uDimensions[0] = width; m_uDimensions[1] = height; initialize(); } + Array2D(uint32_t width, uint32_t height, uint32_t depth) + :m_pElements(0) + { + static_assert(noOfDims == 3, "This constructor can only be used with a three-dimensional array"); + + m_uDimensions[0] = width; + m_uDimensions[1] = height; + m_uDimensions[2] = depth; + + initialize(); + } + // These are deleted to avoid accidental copying. Array2D(const Array2D&) = delete; Array2D& operator=(const Array2D&) = delete; ~Array2D() { - delete[] m_pData; + delete[] m_pElements; } - ElementType operator()(uint32_t x, uint32_t y) const + ElementType& operator()(uint32_t x) const { - POLYVOX_ASSERT((x < m_uDimensions[0] && y < m_uDimensions[1]), "Array access is out-of-range."); - return m_pData[y * m_uDimensions[0] + x]; + static_assert(noOfDims == 1, "This accessor can only be used with a one-dimensional array"); + POLYVOX_ASSERT(x < m_uDimensions[0], "Array access is out-of-range."); + return m_pElements[x]; } - ElementType& operator()(uint32_t x, uint32_t y) + ElementType& operator()(uint32_t x, uint32_t y) const { - POLYVOX_ASSERT((x < m_uDimensions[0] && y < m_uDimensions[1]), "Array access is out-of-range."); - return m_pData[y * m_uDimensions[0] + x]; + static_assert(noOfDims == 2, "This accessor can only be used with a two-dimensional array"); + POLYVOX_ASSERT(x < m_uDimensions[0] && y < m_uDimensions[1], "Array access is out-of-range."); + return m_pElements[y * m_uDimensions[0] + x]; + } + + ElementType& operator()(uint32_t x, uint32_t y, uint32_t z) const + { + static_assert(noOfDims == 3, "This accessor can only be used with a three-dimensional array"); + POLYVOX_ASSERT(x < m_uDimensions[0] && y < m_uDimensions[1] && z < m_uDimensions[2], "Array access is out-of-range."); + return m_pElements[z * m_uDimensions[1] * m_uDimensions[1] + y * m_uDimensions[0] + x]; } ElementType* getRawData() { - return m_pData; + return m_pElements; } - size_t getNoOfElements() + uint32_t getNoOfElements() { return m_uNoOfElements; } void swap(Array2D& other) { - ElementType* temp = other.m_pData; - other.m_pData = m_pData; - m_pData = temp; + ElementType* temp = other.m_pElements; + other.m_pElements = m_pElements; + m_pElements = temp; } private: @@ -88,14 +125,31 @@ namespace PolyVox { m_uNoOfElements *= m_uDimensions[i]; } - m_pData = new ElementType[m_uNoOfElements]; + m_pElements = new ElementType[m_uNoOfElements]; } uint32_t m_uDimensions[noOfDims]; uint32_t m_uNoOfElements; - ElementType* m_pData; + ElementType* m_pElements; }; + ///A 1D Array of floats. + typedef Array2D<1, float> Array1DFloat; + ///A 1D Array of doubles. + typedef Array2D<1, double> Array1DDouble; + ///A 1D Array of signed 8-bit values. + typedef Array2D<1, int8_t> Array1DInt8; + ///A 1D Array of unsigned 8-bit values. + typedef Array2D<1, uint8_t> Array1DUint8; + ///A 1D Array of signed 16-bit values. + typedef Array2D<1, int16_t> Array1DInt16; + ///A 1D Array of unsigned 16-bit values. + typedef Array2D<1, uint16_t> Array1DUint16; + ///A 1D Array of signed 32-bit values. + typedef Array2D<1, int32_t> Array1DInt32; + ///A 1D Array of unsigned 32-bit values. + typedef Array2D<1, uint32_t> Array1DUint32; + ///A 2D Array of floats. typedef Array2D<2, float> Array2DFloat; ///A 2D Array of doubles. @@ -112,6 +166,23 @@ namespace PolyVox typedef Array2D<2, int32_t> Array2DInt32; ///A 2D Array of unsigned 32-bit values. typedef Array2D<2, uint32_t> Array2DUint32; + + ///A 3D Array of floats. + typedef Array2D<3, float> Array3DFloat; + ///A 3D Array of doubles. + typedef Array2D<3, double> Array3DDouble; + ///A 3D Array of signed 8-bit values. + typedef Array2D<3, int8_t> Array3DInt8; + ///A 3D Array of unsigned 8-bit values. + typedef Array2D<3, uint8_t> Array3DUint8; + ///A 3D Array of signed 16-bit values. + typedef Array2D<3, int16_t> Array3DInt16; + ///A 3D Array of unsigned 16-bit values. + typedef Array2D<3, uint16_t> Array3DUint16; + ///A 3D Array of signed 32-bit values. + typedef Array2D<3, int32_t> Array3DInt32; + ///A 3D Array of unsigned 32-bit values. + typedef Array2D<3, uint32_t> Array3DUint32; } #endif //__PolyVox_Array2D_H__ \ No newline at end of file diff --git a/library/PolyVoxCore/include/PolyVoxCore/PolyVoxForwardDeclarations.h b/library/PolyVoxCore/include/PolyVoxCore/PolyVoxForwardDeclarations.h index 75ddf905..4c63906d 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/PolyVoxForwardDeclarations.h +++ b/library/PolyVoxCore/include/PolyVoxCore/PolyVoxForwardDeclarations.h @@ -33,7 +33,7 @@ namespace PolyVox //////////////////////////////////////////////////////////////////////////////// // Array //////////////////////////////////////////////////////////////////////////////// - template class Array; + /*template class Array; typedef Array<1,float> Array1DFloat; typedef Array<1,double> Array1DDouble; @@ -42,7 +42,7 @@ namespace PolyVox typedef Array<1,int16_t> Array1DInt16; typedef Array<1,uint16_t> Array1DUint16; typedef Array<1,int32_t> Array1DInt32; - typedef Array<1,uint32_t> Array1DUint32; + typedef Array<1,uint32_t> Array1DUint32;*/ /*typedef Array<2,float> Array2DFloat; typedef Array<2,double> Array2DDouble; @@ -53,14 +53,14 @@ namespace PolyVox typedef Array<2,int32_t> Array2DInt32; typedef Array<2,uint32_t> Array2DUint32;*/ - typedef Array<3,float> Array3DFloat; + /*typedef Array<3,float> Array3DFloat; typedef Array<3,double> Array3DDouble; typedef Array<3,int8_t> Array3DInt8; typedef Array<3,uint8_t> Array3DUint8; typedef Array<3,int16_t> Array3DInt16; typedef Array<3,uint16_t> Array3DUint16; typedef Array<3,int32_t> Array3DInt32; - typedef Array<3,uint32_t> Array3DUint32; + typedef Array<3,uint32_t> Array3DUint32;*/ //////////////////////////////////////////////////////////////////////////////// // BlockCompressor diff --git a/tests/TestArray.cpp b/tests/TestArray.cpp index 6d41aa6e..84d4bf14 100644 --- a/tests/TestArray.cpp +++ b/tests/TestArray.cpp @@ -33,22 +33,26 @@ using namespace PolyVox; void TestArray::testCArraySpeed() { - const int width = 128; - const int height = 128; + const int width = 32; + const int height = 32; + const int depth = 32; - int cArray[width][height]; + int cArray[width][height][depth]; QBENCHMARK { int ct = 1; int expectedTotal = 0; - for (int y = 0; y < height; y++) + for (int z = 0; z < depth; z++) { - for (int x = 0; x < width; x++) + for (int y = 0; y < height; y++) { - cArray[x][y] = ct; - expectedTotal += cArray[x][y]; - ct++; + for (int x = 0; x < width; x++) + { + cArray[x][y][z] = ct; + expectedTotal += cArray[x][y][z]; + ct++; + } } } } @@ -56,22 +60,26 @@ void TestArray::testCArraySpeed() void TestArray::testPolyVoxArraySpeed() { - const int width = 128; - const int height = 128; + const int width = 32; + const int height = 32; + const int depth = 32; - Array<2, int> polyvoxArray(ArraySizes(width)(height)); + Array<3, int> polyvoxArray(ArraySizes(width)(height)(depth)); QBENCHMARK { int ct = 1; int expectedTotal = 0; - for (int y = 0; y < height; y++) + for (int z = 0; z < depth; z++) { - for (int x = 0; x < width; x++) + for (int y = 0; y < height; y++) { - polyvoxArray[x][y] = ct; - expectedTotal += polyvoxArray[x][y]; - ct++; + for (int x = 0; x < width; x++) + { + polyvoxArray[x][y][z] = ct; + expectedTotal += polyvoxArray[x][y][z]; + ct++; + } } } } @@ -79,22 +87,26 @@ void TestArray::testPolyVoxArraySpeed() void TestArray::testPolyVoxArray2DSpeed() { - const int width = 128; - const int height = 128; + const int width = 32; + const int height = 32; + const int depth = 32; - Array2D polyvoxArray(width,height); + Array2D<3, int> polyvoxArray(width, height, depth); QBENCHMARK { int ct = 1; int expectedTotal = 0; - for (int y = 0; y < height; y++) + for (int z = 0; z < depth; z++) { - for (int x = 0; x < width; x++) + for (int y = 0; y < height; y++) { - polyvoxArray(x,y) = ct; - expectedTotal += polyvoxArray(x,y); - ct++; + for (int x = 0; x < width; x++) + { + polyvoxArray(x, y, z) = ct; + expectedTotal += polyvoxArray(x, y, z); + ct++; + } } } }