From 59505d47e9509dd8a8c25dede649294aa9782d26 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 23 Jun 2013 23:17:40 +0200 Subject: [PATCH] Tidying up Block and FilePager. --- examples/Basic/main.cpp | 24 +++++++++++++++---- .../include/PolyVoxCore/FilePager.h | 18 +++++++------- .../include/PolyVoxCore/Impl/Block.h | 4 +++- .../include/PolyVoxCore/Impl/Block.inl | 8 ++++++- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/examples/Basic/main.cpp b/examples/Basic/main.cpp index e7359f52..40bf0058 100644 --- a/examples/Basic/main.cpp +++ b/examples/Basic/main.cpp @@ -26,14 +26,17 @@ freely, subject to the following restrictions: #include "PolyVoxCore/CubicSurfaceExtractorWithNormals.h" #include "PolyVoxCore/MarchingCubesSurfaceExtractor.h" #include "PolyVoxCore/SurfaceMesh.h" -#include "PolyVoxCore/SimpleVolume.h" +//#include "PolyVoxCore/SimpleVolume.h" +#include "PolyVoxCore/LargeVolume.h" +#include "PolyVoxCore/RLECompressor.h" +#include "PolyVoxCore/FilePager.h" #include //Use the PolyVox namespace using namespace PolyVox; -void createSphereInVolume(SimpleVolume& volData, float fRadius) +void createSphereInVolume(LargeVolume& volData, float fRadius) { //This vector hold the position of the center of the volume Vector3DFloat v3dVolCenter(volData.getWidth() / 2, volData.getHeight() / 2, volData.getDepth() / 2); @@ -68,21 +71,32 @@ void createSphereInVolume(SimpleVolume& volData, float fRadius) int main(int argc, char *argv[]) { + setTraceStream(&(std::cout)); + setDebugStream(&(std::cout)); + setInfoStream(&(std::cout)); + //Create and show the Qt OpenGL window QApplication app(argc, argv); OpenGLWidget openGLWidget(0); openGLWidget.show(); //Create an empty volume and then place a sphere in it - SimpleVolume volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(63, 63, 63))); + RLECompressor* pCompressor = new RLECompressor(); + FilePager* pFilePager = new FilePager("D:/temp/voldata/"); + + LargeVolume volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(63, 63, 63)), pCompressor, pFilePager, 32); + //volData.setMaxNumberOfUncompressedBlocks(6); + //volData.setMaxNumberOfBlocksInMemory(7); + + createSphereInVolume(volData, 30); //A mesh object to hold the result of surface extraction SurfaceMesh mesh; //Create a surface extractor. Comment out one of the following two lines to decide which type gets created. - CubicSurfaceExtractorWithNormals< SimpleVolume > surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh); - //MarchingCubesSurfaceExtractor< SimpleVolume > surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh); + CubicSurfaceExtractorWithNormals< LargeVolume > surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh); + //MarchingCubesSurfaceExtractor< LargeVolume > surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh); //Execute the surface extractor. surfaceExtractor.execute(); diff --git a/library/PolyVoxCore/include/PolyVoxCore/FilePager.h b/library/PolyVoxCore/include/PolyVoxCore/FilePager.h index 5e28782d..97b9f2cc 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/FilePager.h +++ b/library/PolyVoxCore/include/PolyVoxCore/FilePager.h @@ -54,7 +54,7 @@ namespace PolyVox virtual void pageIn(const Region& region, Block* pBlockData) { POLYVOX_ASSERT(pBlockData, "Attempting to page in NULL block"); - POLYVOX_ASSERT(pBlockData->m_bIsCompressed, "Attempting to page in uncompressed block"); + POLYVOX_ASSERT(pBlockData->isCompressed(), "Attempting to page in uncompressed block"); std::stringstream ss; ss << region.getLowerX() << "_" << region.getLowerY() << "_" << region.getLowerZ() << "_" @@ -71,13 +71,13 @@ namespace PolyVox logTrace() << "Paging in data for " << region; fseek(pFile, 0L, SEEK_END); - pBlockData->m_uCompressedDataLength = ftell(pFile); + size_t fileSizeInBytes = ftell(pFile); fseek(pFile, 0L, SEEK_SET); - - delete[] pBlockData->m_pCompressedData; - pBlockData->m_pCompressedData = new uint8_t[pBlockData->m_uCompressedDataLength]; - - fread(pBlockData->m_pCompressedData, sizeof(uint8_t), pBlockData->m_uCompressedDataLength, pFile); + + uint8_t* buffer = new uint8_t[fileSizeInBytes]; + fread(buffer, sizeof(uint8_t), fileSizeInBytes, pFile); + pBlockData->setCompressedData(buffer, fileSizeInBytes); + delete[] buffer; if(ferror(pFile)) { @@ -95,7 +95,7 @@ namespace PolyVox virtual void pageOut(const Region& region, Block* pBlockData) { POLYVOX_ASSERT(pBlockData, "Attempting to page out NULL block"); - POLYVOX_ASSERT(pBlockData->m_bIsCompressed, "Attempting to page out uncompressed block"); + POLYVOX_ASSERT(pBlockData->isCompressed(), "Attempting to page out uncompressed block"); logTrace() << "Paging out data for " << region; @@ -114,7 +114,7 @@ namespace PolyVox POLYVOX_THROW(std::runtime_error, "Unable to open file to write out block data."); } - fwrite(pBlockData->m_pCompressedData, sizeof(uint8_t), pBlockData->m_uCompressedDataLength, pFile); + fwrite(pBlockData->getCompressedData(), sizeof(uint8_t), pBlockData->getCompressedDataLength(), pFile); if(ferror(pFile)) { diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h index 93a9c0f0..8c3b68bd 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h @@ -51,12 +51,14 @@ namespace PolyVox public: Block(uint16_t uSideLength = 0); - const uint8_t* getCompressedData(void) const; + const uint8_t* const getCompressedData(void) const; const uint32_t getCompressedDataLength(void) const; uint16_t getSideLength(void) const; VoxelType getVoxel(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const; VoxelType getVoxel(const Vector3DUint16& v3dPos) const; + bool isCompressed(void); + void setCompressedData(const uint8_t* const data, uint32_t dataLength); void setVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos, VoxelType tValue); void setVoxelAt(const Vector3DUint16& v3dPos, VoxelType tValue); diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl index 64fbe33b..c2f38883 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl @@ -52,7 +52,7 @@ namespace PolyVox } template - const uint8_t* Block::getCompressedData(void) const + const uint8_t* const Block::getCompressedData(void) const { POLYVOX_ASSERT(m_bIsCompressed, "You cannot call getCompressedData() when the block is not compressed"); POLYVOX_ASSERT(m_pCompressedData, "Compressed data is NULL"); @@ -99,6 +99,12 @@ namespace PolyVox return getVoxel(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ()); } + template + bool Block::isCompressed(void) + { + return m_bIsCompressed; + } + template void Block::setCompressedData(const uint8_t* const data, uint32_t dataLength) {