Extended 'Array2D class to be multidimensional (will rename it shortly).

This commit is contained in:
David Williams 2014-08-24 21:54:59 +02:00
parent 9a1c6784df
commit 528873bcd3
4 changed files with 131 additions and 48 deletions

View File

@ -154,7 +154,7 @@ namespace PolyVox
//Some handy typedefs //Some handy typedefs
///A 1D Array of floats. ///A 1D Array of floats.
typedef Array<1,float> Array1DFloat; /*typedef Array<1,float> Array1DFloat;
///A 1D Array of doubles. ///A 1D Array of doubles.
typedef Array<1,double> Array1DDouble; typedef Array<1,double> Array1DDouble;
///A 1D Array of signed 8-bit values. ///A 1D Array of signed 8-bit values.
@ -168,7 +168,7 @@ namespace PolyVox
///A 1D Array of signed 32-bit values. ///A 1D Array of signed 32-bit values.
typedef Array<1,int32_t> Array1DInt32; typedef Array<1,int32_t> Array1DInt32;
///A 1D Array of unsigned 32-bit values. ///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. ///A 2D Array of floats.
/*typedef Array<2,float> Array2DFloat; /*typedef Array<2,float> Array2DFloat;
@ -188,7 +188,7 @@ namespace PolyVox
typedef Array<2,uint32_t> Array2DUint32;*/ typedef Array<2,uint32_t> Array2DUint32;*/
///A 3D Array of floats. ///A 3D Array of floats.
typedef Array<3,float> Array3DFloat; /*typedef Array<3,float> Array3DFloat;
///A 3D Array of doubles. ///A 3D Array of doubles.
typedef Array<3,double> Array3DDouble; typedef Array<3,double> Array3DDouble;
///A 3D Array of signed 8-bit values. ///A 3D Array of signed 8-bit values.
@ -202,7 +202,7 @@ namespace PolyVox
///A 3D Array of signed 32-bit values. ///A 3D Array of signed 32-bit values.
typedef Array<3,int32_t> Array3DInt32; typedef Array<3,int32_t> Array3DInt32;
///A 3D Array of unsigned 32-bit values. ///A 3D Array of unsigned 32-bit values.
typedef Array<3,uint32_t> Array3DUint32; typedef Array<3,uint32_t> Array3DUint32;*/
}//namespace PolyVox }//namespace PolyVox
#include "PolyVoxCore/Array.inl" #include "PolyVoxCore/Array.inl"

View File

@ -24,6 +24,10 @@ distribution.
#ifndef __PolyVox_Array2D_H__ #ifndef __PolyVox_Array2D_H__
#define __PolyVox_Array2D_H__ #define __PolyVox_Array2D_H__
#include <PolyVoxCore/Impl/ErrorHandling.h>
#include <cstdint>
namespace PolyVox namespace PolyVox
{ {
template <uint32_t noOfDims, typename ElementType> template <uint32_t noOfDims, typename ElementType>
@ -31,51 +35,84 @@ namespace PolyVox
{ {
public: public:
Array2D(uint32_t width, uint32_t height) Array2D(uint32_t width)
:m_pData(0) :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[0] = width;
m_uDimensions[1] = height; m_uDimensions[1] = height;
initialize(); 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. // These are deleted to avoid accidental copying.
Array2D<noOfDims, ElementType>(const Array2D<noOfDims, ElementType>&) = delete; Array2D<noOfDims, ElementType>(const Array2D<noOfDims, ElementType>&) = delete;
Array2D<noOfDims, ElementType>& operator=(const Array2D<noOfDims, ElementType>&) = delete; Array2D<noOfDims, ElementType>& operator=(const Array2D<noOfDims, ElementType>&) = delete;
~Array2D() ~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."); static_assert(noOfDims == 1, "This accessor can only be used with a one-dimensional array");
return m_pData[y * m_uDimensions[0] + x]; 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."); static_assert(noOfDims == 2, "This accessor can only be used with a two-dimensional array");
return m_pData[y * m_uDimensions[0] + x]; 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() ElementType* getRawData()
{ {
return m_pData; return m_pElements;
} }
size_t getNoOfElements() uint32_t getNoOfElements()
{ {
return m_uNoOfElements; return m_uNoOfElements;
} }
void swap(Array2D& other) void swap(Array2D& other)
{ {
ElementType* temp = other.m_pData; ElementType* temp = other.m_pElements;
other.m_pData = m_pData; other.m_pElements = m_pElements;
m_pData = temp; m_pElements = temp;
} }
private: private:
@ -88,14 +125,31 @@ namespace PolyVox
{ {
m_uNoOfElements *= m_uDimensions[i]; 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_uDimensions[noOfDims];
uint32_t m_uNoOfElements; 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. ///A 2D Array of floats.
typedef Array2D<2, float> Array2DFloat; typedef Array2D<2, float> Array2DFloat;
///A 2D Array of doubles. ///A 2D Array of doubles.
@ -112,6 +166,23 @@ namespace PolyVox
typedef Array2D<2, int32_t> Array2DInt32; typedef Array2D<2, int32_t> Array2DInt32;
///A 2D Array of unsigned 32-bit values. ///A 2D Array of unsigned 32-bit values.
typedef Array2D<2, uint32_t> Array2DUint32; 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__ #endif //__PolyVox_Array2D_H__

View File

@ -33,7 +33,7 @@ namespace PolyVox
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Array // Array
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template<uint32_t dimensions, typename ElementType> class Array; /*template<uint32_t dimensions, typename ElementType> class Array;
typedef Array<1,float> Array1DFloat; typedef Array<1,float> Array1DFloat;
typedef Array<1,double> Array1DDouble; typedef Array<1,double> Array1DDouble;
@ -42,7 +42,7 @@ namespace PolyVox
typedef Array<1,int16_t> Array1DInt16; typedef Array<1,int16_t> Array1DInt16;
typedef Array<1,uint16_t> Array1DUint16; typedef Array<1,uint16_t> Array1DUint16;
typedef Array<1,int32_t> Array1DInt32; 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,float> Array2DFloat;
typedef Array<2,double> Array2DDouble; typedef Array<2,double> Array2DDouble;
@ -53,14 +53,14 @@ namespace PolyVox
typedef Array<2,int32_t> Array2DInt32; typedef Array<2,int32_t> Array2DInt32;
typedef Array<2,uint32_t> Array2DUint32;*/ typedef Array<2,uint32_t> Array2DUint32;*/
typedef Array<3,float> Array3DFloat; /*typedef Array<3,float> Array3DFloat;
typedef Array<3,double> Array3DDouble; typedef Array<3,double> Array3DDouble;
typedef Array<3,int8_t> Array3DInt8; typedef Array<3,int8_t> Array3DInt8;
typedef Array<3,uint8_t> Array3DUint8; typedef Array<3,uint8_t> Array3DUint8;
typedef Array<3,int16_t> Array3DInt16; typedef Array<3,int16_t> Array3DInt16;
typedef Array<3,uint16_t> Array3DUint16; typedef Array<3,uint16_t> Array3DUint16;
typedef Array<3,int32_t> Array3DInt32; typedef Array<3,int32_t> Array3DInt32;
typedef Array<3,uint32_t> Array3DUint32; typedef Array<3,uint32_t> Array3DUint32;*/
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// BlockCompressor // BlockCompressor

View File

@ -33,71 +33,83 @@ using namespace PolyVox;
void TestArray::testCArraySpeed() void TestArray::testCArraySpeed()
{ {
const int width = 128; const int width = 32;
const int height = 128; const int height = 32;
const int depth = 32;
int cArray[width][height]; int cArray[width][height][depth];
QBENCHMARK QBENCHMARK
{ {
int ct = 1; int ct = 1;
int expectedTotal = 0; int expectedTotal = 0;
for (int z = 0; z < depth; z++)
{
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
cArray[x][y] = ct; cArray[x][y][z] = ct;
expectedTotal += cArray[x][y]; expectedTotal += cArray[x][y][z];
ct++; ct++;
} }
} }
} }
}
} }
void TestArray::testPolyVoxArraySpeed() void TestArray::testPolyVoxArraySpeed()
{ {
const int width = 128; const int width = 32;
const int height = 128; const int height = 32;
const int depth = 32;
Array<2, int> polyvoxArray(ArraySizes(width)(height)); Array<3, int> polyvoxArray(ArraySizes(width)(height)(depth));
QBENCHMARK QBENCHMARK
{ {
int ct = 1; int ct = 1;
int expectedTotal = 0; int expectedTotal = 0;
for (int z = 0; z < depth; z++)
{
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
polyvoxArray[x][y] = ct; polyvoxArray[x][y][z] = ct;
expectedTotal += polyvoxArray[x][y]; expectedTotal += polyvoxArray[x][y][z];
ct++; ct++;
} }
} }
} }
}
} }
void TestArray::testPolyVoxArray2DSpeed() void TestArray::testPolyVoxArray2DSpeed()
{ {
const int width = 128; const int width = 32;
const int height = 128; const int height = 32;
const int depth = 32;
Array2D<int> polyvoxArray(width,height); Array2D<3, int> polyvoxArray(width, height, depth);
QBENCHMARK QBENCHMARK
{ {
int ct = 1; int ct = 1;
int expectedTotal = 0; int expectedTotal = 0;
for (int z = 0; z < depth; z++)
{
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
polyvoxArray(x,y) = ct; polyvoxArray(x, y, z) = ct;
expectedTotal += polyvoxArray(x,y); expectedTotal += polyvoxArray(x, y, z);
ct++; ct++;
} }
} }
} }
}
} }
void TestArray::testReadWrite() void TestArray::testReadWrite()