diff --git a/library/PolyVoxCore/include/Array.h b/library/PolyVoxCore/include/Array.h index 24266b82..decc82c9 100644 --- a/library/PolyVoxCore/include/Array.h +++ b/library/PolyVoxCore/include/Array.h @@ -39,12 +39,58 @@ namespace PolyVox class Array { public: + Array() + :m_pElements(0) + ,m_pDimensions(0) + ,m_pOffsets(0) + ,m_uNoOfElements(0) + { + } + Array(const uint32_t (&pDimensions)[noOfDims]) :m_pElements(0) ,m_pDimensions(0) ,m_pOffsets(0) ,m_uNoOfElements(0) { + resize(pDimensions); + } + + ~Array() + { + deallocate(); + } + + SubArray operator[](uint32_t uIndex) + { + assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], + m_pDimensions+1, m_pOffsets+1); + } + + const SubArray operator[](uint32_t uIndex) const + { + assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], + m_pDimensions+1, m_pOffsets+1); + } + + uint32_t getNoOfElements(void) const + { + return m_uNoOfElements; + } + + ElementType* getRawData(void) const + { + return m_pElements; + } + + void resize(const uint32_t (&pDimensions)[noOfDims]) + { + deallocate(); + m_pDimensions = new uint32_t[noOfDims]; m_pOffsets = new uint32_t[noOfDims]; @@ -64,42 +110,59 @@ namespace PolyVox } // Allocate new elements, let exception propagate m_pElements = new ElementType[m_uNoOfElements]; - } - - ~Array() - { - delete[] m_pDimensions; - delete[] m_pOffsets; - delete[] m_pElements; } - SubArray operator [](uint32_t uIndex) + void swap(Array& rhs) { - assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], - m_pDimensions+1, m_pOffsets+1); - } + //Implement this function without temporary 'Array' + //objects, as the destructors will free the memory... + uint32_t* m_pTempDimensions = m_pDimensions; + uint32_t* m_pTempOffsets = m_pOffsets; + uint32_t m_uTempNoOfElements = m_uNoOfElements; + ElementType* m_pTempElements = m_pElements; - const SubArray operator [](uint32_t uIndex) const - { - assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], - m_pDimensions+1, m_pOffsets+1); - } + m_pDimensions = rhs.m_pDimensions; + m_pOffsets = rhs.m_pOffsets; + m_uNoOfElements = rhs.m_uNoOfElements; + m_pElements = rhs.m_pElements; - ElementType* getRawData(void) - { - return m_pElements; - } - - uint32_t getNoOfElements(void) - { - return m_uNoOfElements; + rhs.m_pDimensions = m_pTempDimensions; + rhs.m_pOffsets = m_pTempOffsets; + rhs.m_uNoOfElements = m_uTempNoOfElements; + rhs.m_pElements = m_pTempElements; } private: + Array(const Array& rhs) + :m_pElements(0) + ,m_pDimensions(0) + ,m_pOffsets(0) + ,m_uNoOfElements(0) + { + //Not implemented + assert(false); + } + + Array& Array::operator=(const Array& rhs) + { + //Not implemented + assert(false); + + return *this; + } + + void deallocate(void) + { + delete[] m_pDimensions; + m_pDimensions = 0; + delete[] m_pOffsets; + m_pOffsets = 0; + delete[] m_pElements; + m_pElements = 0; + + m_uNoOfElements = 0; + } + uint32_t * m_pDimensions; uint32_t * m_pOffsets; uint32_t m_uNoOfElements; @@ -110,40 +173,96 @@ namespace PolyVox class Array<1, ElementType> { public: + Array<1, ElementType>() + : m_pElements(0) + ,m_pDimensions(0) + { + } + Array<1, ElementType>(const uint32_t (&pDimensions)[1]) : m_pElements(0) ,m_pDimensions(0) { + resize(pDimensions); + } + + ~Array<1, ElementType>() + { + deallocate(); + } + + ElementType& operator[] (uint32_t uIndex) + { + assert(uIndex& rhs) { - assert(uIndex(const Array<1, ElementType>& rhs) + : m_pElements(0) + ,m_pDimensions(0) + { + //Not implemented + assert(false); + } + + Array<1, ElementType>& Array<1, ElementType>::operator=(const Array<1, ElementType>& rhs) + { + //Not implemented + assert(false); + + return *this; + } + + void deallocate(void) + { + delete[] m_pDimensions; + m_pDimensions = 0; + delete[] m_pElements; + m_pElements = 0; + } + uint32_t * m_pDimensions; ElementType * m_pElements; }; diff --git a/library/PolyVoxCore/include/PolyVoxImpl/SubArray.inl b/library/PolyVoxCore/include/PolyVoxImpl/SubArray.inl index 687773d7..8c88890e 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/SubArray.inl +++ b/library/PolyVoxCore/include/PolyVoxImpl/SubArray.inl @@ -27,50 +27,50 @@ namespace PolyVox { template SubArray SubArray::operator[](uint32_t uIndex) - { - assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], - m_pDimensions+1, m_pOffsets+1); - } + { + assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], + m_pDimensions+1, m_pOffsets+1); + } - template - const SubArray SubArray::operator[](uint32_t uIndex) const - { - assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], - m_pDimensions+1, m_pOffsets+1); - } + template + const SubArray SubArray::operator[](uint32_t uIndex) const + { + assert(uIndex(&m_pElements[uIndex*m_pOffsets[0]], + m_pDimensions+1, m_pOffsets+1); + } - template - SubArray::SubArray(ElementType * pElements, uint32_t * pDimensions, uint32_t * pOffsets) - :m_pElements(pElements) - ,m_pDimensions(pDimensions) - ,m_pOffsets(pOffsets) - ,m_uNoOfElements(0) - { - } + template + SubArray::SubArray(ElementType * pElements, uint32_t * pDimensions, uint32_t * pOffsets) + :m_pElements(pElements) + ,m_pDimensions(pDimensions) + ,m_pOffsets(pOffsets) + ,m_uNoOfElements(0) + { + } - template - ElementType& SubArray<1, ElementType>::operator[] (uint32_t uIndex) - { - assert(uIndex + ElementType& SubArray<1, ElementType>::operator[] (uint32_t uIndex) + { + assert(uIndex - const ElementType& SubArray<1, ElementType>::operator[] (uint32_t uIndex) const - { - assert(uIndex - SubArray<1, ElementType>::SubArray<1, ElementType>(ElementType * pElements, uint32_t * pDimensions, uint32_t * /*pOffsets*/) - :m_pDimensions(pDimensions) - ,m_pElements(pElements) - { - } + template + const ElementType& SubArray<1, ElementType>::operator[] (uint32_t uIndex) const + { + assert(uIndex + SubArray<1, ElementType>::SubArray<1, ElementType>(ElementType * pElements, uint32_t * pDimensions, uint32_t * /*pOffsets*/) + :m_pDimensions(pDimensions) + ,m_pElements(pElements) + { + } }//namespace PolyVox \ No newline at end of file diff --git a/library/PolyVoxCore/source/SurfaceExtractor.cpp b/library/PolyVoxCore/source/SurfaceExtractor.cpp index 29c6c43c..75999a1d 100644 --- a/library/PolyVoxCore/source/SurfaceExtractor.cpp +++ b/library/PolyVoxCore/source/SurfaceExtractor.cpp @@ -57,53 +57,18 @@ namespace PolyVox m_uScratchPadWidth = m_uRegionWidth+1; m_uScratchPadHeight = m_uRegionHeight+1; + uint32_t arraySizes[2]= {m_uScratchPadWidth, m_uScratchPadHeight}; // Array dimensions + //For edge indices - /*m_pPreviousVertexIndicesX = new int32_t[m_uScratchPadWidth * m_uScratchPadHeight]; - m_pPreviousVertexIndicesY = new int32_t[m_uScratchPadWidth * m_uScratchPadHeight]; - m_pPreviousVertexIndicesZ = new int32_t[m_uScratchPadWidth * m_uScratchPadHeight]; - m_pCurrentVertexIndicesX = new int32_t[m_uScratchPadWidth * m_uScratchPadHeight]; - m_pCurrentVertexIndicesY = new int32_t[m_uScratchPadWidth * m_uScratchPadHeight]; - m_pCurrentVertexIndicesZ = new int32_t[m_uScratchPadWidth * m_uScratchPadHeight];*/ + Array2DInt32 m_pPreviousVertexIndicesX(arraySizes); + Array2DInt32 m_pPreviousVertexIndicesY(arraySizes); + Array2DInt32 m_pPreviousVertexIndicesZ(arraySizes); + Array2DInt32 m_pCurrentVertexIndicesX(arraySizes); + Array2DInt32 m_pCurrentVertexIndicesY(arraySizes); + Array2DInt32 m_pCurrentVertexIndicesZ(arraySizes); - //uint32_t Size1 [1]= {m_uScratchPadWidth}; // Array dimensions - uint32_t Size2 [2]= {m_uScratchPadWidth, m_uScratchPadHeight}; // Array dimensions - - /*Array2DInt32 m_pPreviousVertexIndicesX(m_uScratchPadWidth, m_uScratchPadHeight); - Array2DInt32 m_pPreviousVertexIndicesY(m_uScratchPadWidth, m_uScratchPadHeight); - Array2DInt32 m_pPreviousVertexIndicesZ(m_uScratchPadWidth, m_uScratchPadHeight); - Array2DInt32 m_pCurrentVertexIndicesX(m_uScratchPadWidth, m_uScratchPadHeight); - Array2DInt32 m_pCurrentVertexIndicesY(m_uScratchPadWidth, m_uScratchPadHeight); - Array2DInt32 m_pCurrentVertexIndicesZ(m_uScratchPadWidth, m_uScratchPadHeight);*/ - - Array2DInt32 m_pPreviousVertexIndicesX(ArraySizes(m_uScratchPadWidth)(m_uScratchPadHeight)); - Array2DInt32 m_pPreviousVertexIndicesY(Size2); - Array2DInt32 m_pPreviousVertexIndicesZ(Size2); - Array2DInt32 m_pCurrentVertexIndicesX(Size2); - Array2DInt32 m_pCurrentVertexIndicesY(Size2); - Array2DInt32 m_pCurrentVertexIndicesZ(Size2); - - //Array1DFloat test1d(ArraySizes(10)); - //Array2DFloat test2d(ArraySizes(10)(20)); - - - - /*m_pPreviousVertexIndicesX.resize(Size2); - m_pPreviousVertexIndicesY.resize(Size2); - m_pPreviousVertexIndicesZ.resize(Size2); - m_pCurrentVertexIndicesX.resize(Size2); - m_pCurrentVertexIndicesY.resize(Size2); - m_pCurrentVertexIndicesZ.resize(Size2);*/ - - - //Cell bitmasks - /*Array2DUint8 pPreviousBitmask(m_uScratchPadWidth, m_uScratchPadHeight); - Array2DUint8 pCurrentBitmask(m_uScratchPadWidth, m_uScratchPadHeight);*/ - - Array2DUint8 pPreviousBitmask(Size2); - Array2DUint8 pCurrentBitmask(Size2); - - /*pPreviousBitmask.resize(Size2); - pCurrentBitmask.resize(Size2);*/ + Array2DUint8 pPreviousBitmask(arraySizes); + Array2DUint8 pCurrentBitmask(arraySizes); //Create a region corresponding to the first slice m_regSlicePrevious = m_regInputCropped; @@ -128,10 +93,10 @@ namespace PolyVox } std::swap(uNoOfNonEmptyCellsForSlice0, uNoOfNonEmptyCellsForSlice1); - std::swap(pPreviousBitmask, pCurrentBitmask); - std::swap(m_pPreviousVertexIndicesX, m_pCurrentVertexIndicesX); - std::swap(m_pPreviousVertexIndicesY, m_pCurrentVertexIndicesY); - std::swap(m_pPreviousVertexIndicesZ, m_pCurrentVertexIndicesZ); + pPreviousBitmask.swap(pCurrentBitmask); + m_pPreviousVertexIndicesX.swap(m_pCurrentVertexIndicesX); + m_pPreviousVertexIndicesY.swap(m_pCurrentVertexIndicesY); + m_pPreviousVertexIndicesZ.swap(m_pCurrentVertexIndicesZ); m_regSlicePrevious = m_regSliceCurrent; m_regSliceCurrent.shift(Vector3DInt16(0,0,1)); @@ -156,10 +121,10 @@ namespace PolyVox } std::swap(uNoOfNonEmptyCellsForSlice0, uNoOfNonEmptyCellsForSlice1); - std::swap(pPreviousBitmask, pCurrentBitmask); - std::swap(m_pPreviousVertexIndicesX, m_pCurrentVertexIndicesX); - std::swap(m_pPreviousVertexIndicesY, m_pCurrentVertexIndicesY); - std::swap(m_pPreviousVertexIndicesZ, m_pCurrentVertexIndicesZ); + pPreviousBitmask.swap(pCurrentBitmask); + m_pPreviousVertexIndicesX.swap(m_pCurrentVertexIndicesX); + m_pPreviousVertexIndicesY.swap(m_pCurrentVertexIndicesY); + m_pPreviousVertexIndicesZ.swap(m_pCurrentVertexIndicesZ); m_regSlicePrevious = m_regSliceCurrent; m_regSliceCurrent.shift(Vector3DInt16(0,0,1)); @@ -175,13 +140,6 @@ namespace PolyVox generateIndicesForSlice(pPreviousBitmask, m_pPreviousVertexIndicesX, m_pPreviousVertexIndicesY, m_pPreviousVertexIndicesZ, m_pCurrentVertexIndicesX, m_pCurrentVertexIndicesY, m_pCurrentVertexIndicesZ); } - /*delete[] m_pPreviousVertexIndicesX; - delete[] m_pCurrentVertexIndicesX; - delete[] m_pPreviousVertexIndicesY; - delete[] m_pCurrentVertexIndicesY; - delete[] m_pPreviousVertexIndicesZ; - delete[] m_pCurrentVertexIndicesZ; */ - m_meshCurrent->m_Region = m_regInputUncropped; m_meshCurrent->m_vecLodRecords.clear();