From bc0829b4f968770ef4afb8c39cea49c4d12f4d7a Mon Sep 17 00:00:00 2001 From: David Williams Date: Mon, 28 Jun 2010 17:50:53 +0000 Subject: [PATCH] Added smoothRegion function to smooth part of a volume. --- examples/OpenGL/main.cpp | 81 ++---------------------- library/PolyVoxCore/CMakeLists.txt | 2 + library/PolyVoxCore/include/ArraySizes.h | 3 +- library/PolyVoxCore/include/Filters.h | 43 +++++++++++++ library/PolyVoxCore/include/Filters.inl | 69 ++++++++++++++++++++ 5 files changed, 121 insertions(+), 77 deletions(-) create mode 100644 library/PolyVoxCore/include/Filters.h create mode 100644 library/PolyVoxCore/include/Filters.inl diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index 4d79c8ae..22b804a9 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -21,6 +21,7 @@ freely, subject to the following restrictions: distribution. *******************************************************************************/ +#include "Filters.h" #include "Log.h" #include "MaterialDensityPair.h" #include "Volume.h" @@ -72,13 +73,11 @@ int main(int argc, char *argv[]) { logHandler = &exampleLog; Volume volData(g_uVolumeSideLength, g_uVolumeSideLength, g_uVolumeSideLength); - Volume volDataAveraged(g_uVolumeSideLength, g_uVolumeSideLength, g_uVolumeSideLength); //Make our volume contain a sphere in the center. uint16_t minPos = 0; uint16_t midPos = g_uVolumeSideLength / 2; uint16_t maxPos = g_uVolumeSideLength - 1; - //createCubeInVolume(volData, Vector3DUint16(minPos, minPos, minPos), Vector3DUint16(maxPos, maxPos, maxPos), 0); cout << "Creating sphere 1" << std::endl; createSphereInVolume(volData, 60.0f, 5); @@ -91,17 +90,6 @@ int main(int argc, char *argv[]) cout << "Creating sphere 5" << std::endl; createSphereInVolume(volData, 20.0f, 1); - /*cout << "Creating sphere 1" << std::endl; - createSphereInVolume(volData, 120.0f, 5); - cout << "Creating sphere 2" << std::endl; - createSphereInVolume(volData, 100.0f, 4); - cout << "Creating sphere 3" << std::endl; - createSphereInVolume(volData, 80.0f, 3); - cout << "Creating sphere 4" << std::endl; - createSphereInVolume(volData, 60.0f, 2); - cout << "Creating sphere 5" << std::endl; - createSphereInVolume(volData, 40.0f, 1);*/ - cout << "Creating cubes" << std::endl; createCubeInVolume(volData, Vector3DUint16(minPos, minPos, minPos), Vector3DUint16(midPos-1, midPos-1, midPos-1), 0); createCubeInVolume(volData, Vector3DUint16(midPos+1, midPos+1, minPos), Vector3DUint16(maxPos, maxPos, midPos-1), 0); @@ -112,71 +100,12 @@ int main(int argc, char *argv[]) createCubeInVolume(volData, Vector3DUint16(midPos-10, 1, midPos-10), Vector3DUint16(midPos+10, maxPos-1, midPos+10), MaterialDensityPair44::getMaxDensity()); createCubeInVolume(volData, Vector3DUint16(midPos-10, midPos-10 ,1), Vector3DUint16(midPos+10, midPos+10, maxPos-1), MaterialDensityPair44::getMaxDensity()); - //createCubeInVolume(volData, Vector3DUint16(1, 1, 1), Vector3DUint16(maxPos-1, maxPos-1, midPos/4), 255); - - for (int z = 1; z < volData.getWidth()-1; z++) - { - for (int y = 1; y < volData.getHeight()-1; y++) - { - for (int x = 1; x < volData.getDepth()-1; x++) - { - int uDensity = 0; - uDensity += volData.getVoxelAt(x-1,y-1,z-1).getDensity(); - uDensity += volData.getVoxelAt(x-1,y-1,z-0).getDensity(); - uDensity += volData.getVoxelAt(x-1,y-1,z+1).getDensity(); - uDensity += volData.getVoxelAt(x-1,y-0,z-1).getDensity(); - uDensity += volData.getVoxelAt(x-1,y-0,z-0).getDensity(); - uDensity += volData.getVoxelAt(x-1,y-0,z+1).getDensity(); - uDensity += volData.getVoxelAt(x-1,y+1,z-1).getDensity(); - uDensity += volData.getVoxelAt(x-1,y+1,z-0).getDensity(); - uDensity += volData.getVoxelAt(x-1,y+1,z+1).getDensity(); - - uDensity += volData.getVoxelAt(x-0,y-1,z-1).getDensity(); - uDensity += volData.getVoxelAt(x-0,y-1,z-0).getDensity(); - uDensity += volData.getVoxelAt(x-0,y-1,z+1).getDensity(); - uDensity += volData.getVoxelAt(x-0,y-0,z-1).getDensity(); - uDensity += volData.getVoxelAt(x-0,y-0,z-0).getDensity(); - uDensity += volData.getVoxelAt(x-0,y-0,z+1).getDensity(); - uDensity += volData.getVoxelAt(x-0,y+1,z-1).getDensity(); - uDensity += volData.getVoxelAt(x-0,y+1,z-0).getDensity(); - uDensity += volData.getVoxelAt(x-0,y+1,z+1).getDensity(); - - uDensity += volData.getVoxelAt(x+1,y-1,z-1).getDensity(); - uDensity += volData.getVoxelAt(x+1,y-1,z-0).getDensity(); - uDensity += volData.getVoxelAt(x+1,y-1,z+1).getDensity(); - uDensity += volData.getVoxelAt(x+1,y-0,z-1).getDensity(); - uDensity += volData.getVoxelAt(x+1,y-0,z-0).getDensity(); - uDensity += volData.getVoxelAt(x+1,y-0,z+1).getDensity(); - uDensity += volData.getVoxelAt(x+1,y+1,z-1).getDensity(); - uDensity += volData.getVoxelAt(x+1,y+1,z-0).getDensity(); - uDensity += volData.getVoxelAt(x+1,y+1,z+1).getDensity(); - uDensity /= 27; - - MaterialDensityPair44 val = volData.getVoxelAt(x,y,z); - val.setDensity(uDensity); - - volDataAveraged.setVoxelAt(x,y,z,val); - } - } - } - - /*std::vector counts(256); - fill(counts.begin(), counts.end(), 0); - - for (int z = 0; z < volData.getWidth(); z++) - { - for (int y = 0; y < volData.getHeight(); y++) - { - for (int x = 0; x < volData.getDepth(); x++) - { - counts[volData.getVoxelAt(x,y,z).getMaterial()]++; - } - } - }*/ + //Smooth part of the volume + smoothRegion(volData, Region(Vector3DInt16(62, 62, 62), Vector3DInt16(130, 130, 130))); + smoothRegion(volData, Region(Vector3DInt16(62, 62, 62), Vector3DInt16(130, 130, 130))); cout << "Tidying memory..."; volData.tidyUpMemory(0); - volDataAveraged.tidyUpMemory(0); cout << "done." << endl; QApplication app(argc, argv); @@ -188,7 +117,7 @@ int main(int argc, char *argv[]) QTime time; time.start(); - openGLWidget.setVolume(&volDataAveraged); + openGLWidget.setVolume(&volData); cout << endl << "Time taken = " << time.elapsed() / 1000.0f << "s" << endl << endl; //return 0; diff --git a/library/PolyVoxCore/CMakeLists.txt b/library/PolyVoxCore/CMakeLists.txt index 3d5ef2e8..ea0363ab 100644 --- a/library/PolyVoxCore/CMakeLists.txt +++ b/library/PolyVoxCore/CMakeLists.txt @@ -22,6 +22,8 @@ SET(CORE_INC_FILES include/Array.h include/Array.inl include/ArraySizes.h + include/Filters.h + include/Filters.inl include/GradientEstimators.inl include/Log.h include/MaterialDensityPair.h diff --git a/library/PolyVoxCore/include/ArraySizes.h b/library/PolyVoxCore/include/ArraySizes.h index 335fe7ae..79080354 100644 --- a/library/PolyVoxCore/include/ArraySizes.h +++ b/library/PolyVoxCore/include/ArraySizes.h @@ -28,6 +28,7 @@ distribution. #pragma region Headers #include "PolyVoxImpl/ArraySizesImpl.h" +#include "PolyVoxImpl/Typedef.h" #pragma endregion namespace PolyVox @@ -56,7 +57,7 @@ namespace PolyVox /// behind it may appear complex. For reference, it is based upon the article here: /// http://www.drdobbs.com/cpp/184401319/ //////////////////////////////////////////////////////////////////////////////// - class ArraySizes + class POLYVOXCORE_API ArraySizes { typedef const uint32_t (&UIntArray1)[1]; diff --git a/library/PolyVoxCore/include/Filters.h b/library/PolyVoxCore/include/Filters.h new file mode 100644 index 00000000..7104f81c --- /dev/null +++ b/library/PolyVoxCore/include/Filters.h @@ -0,0 +1,43 @@ +#pragma region License +/******************************************************************************* +Copyright (c) 2005-2009 David 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. +*******************************************************************************/ +#pragma endregion + +#ifndef __PolyVox_Filters_H__ +#define __PolyVox_Filters_H__ + +#pragma region Headers +#include "Array.h" +#include "Region.h" +#include "Volume.h" +#pragma endregion + +namespace PolyVox +{ + template + void smoothRegion(Volume& volData, const Region& regionToSmooth); +}//namespace PolyVox + +#include "Filters.inl" + +#endif \ No newline at end of file diff --git a/library/PolyVoxCore/include/Filters.inl b/library/PolyVoxCore/include/Filters.inl new file mode 100644 index 00000000..acd7a5ed --- /dev/null +++ b/library/PolyVoxCore/include/Filters.inl @@ -0,0 +1,69 @@ +namespace PolyVox +{ + template + void smoothRegion(Volume& volData, const Region& regionToSmooth) + { + Region croppedRegion = regionToSmooth; + croppedRegion.cropTo(volData.getEnclosingRegion()); + + Array<3, uint16_t> temp(ArraySizes(croppedRegion.width())(croppedRegion.height())(croppedRegion.depth())); + + for (int z = croppedRegion.getLowerCorner().getZ(); z < croppedRegion.getUpperCorner().getZ(); z++) + { + for (int y = croppedRegion.getLowerCorner().getY(); y < croppedRegion.getUpperCorner().getY(); y++) + { + for (int x = croppedRegion.getLowerCorner().getX(); x < croppedRegion.getUpperCorner().getX(); x++) + { + uint16_t& uDensity = temp[x-croppedRegion.getLowerCorner().getX()][y-croppedRegion.getLowerCorner().getY()][z-croppedRegion.getLowerCorner().getZ()]; + + uDensity=0; + uDensity += volData.getVoxelAt(x-1,y-1,z-1).getDensity(); + uDensity += volData.getVoxelAt(x-1,y-1,z-0).getDensity(); + uDensity += volData.getVoxelAt(x-1,y-1,z+1).getDensity(); + uDensity += volData.getVoxelAt(x-1,y-0,z-1).getDensity(); + uDensity += volData.getVoxelAt(x-1,y-0,z-0).getDensity(); + uDensity += volData.getVoxelAt(x-1,y-0,z+1).getDensity(); + uDensity += volData.getVoxelAt(x-1,y+1,z-1).getDensity(); + uDensity += volData.getVoxelAt(x-1,y+1,z-0).getDensity(); + uDensity += volData.getVoxelAt(x-1,y+1,z+1).getDensity(); + + uDensity += volData.getVoxelAt(x-0,y-1,z-1).getDensity(); + uDensity += volData.getVoxelAt(x-0,y-1,z-0).getDensity(); + uDensity += volData.getVoxelAt(x-0,y-1,z+1).getDensity(); + uDensity += volData.getVoxelAt(x-0,y-0,z-1).getDensity(); + uDensity += volData.getVoxelAt(x-0,y-0,z-0).getDensity(); + uDensity += volData.getVoxelAt(x-0,y-0,z+1).getDensity(); + uDensity += volData.getVoxelAt(x-0,y+1,z-1).getDensity(); + uDensity += volData.getVoxelAt(x-0,y+1,z-0).getDensity(); + uDensity += volData.getVoxelAt(x-0,y+1,z+1).getDensity(); + + uDensity += volData.getVoxelAt(x+1,y-1,z-1).getDensity(); + uDensity += volData.getVoxelAt(x+1,y-1,z-0).getDensity(); + uDensity += volData.getVoxelAt(x+1,y-1,z+1).getDensity(); + uDensity += volData.getVoxelAt(x+1,y-0,z-1).getDensity(); + uDensity += volData.getVoxelAt(x+1,y-0,z-0).getDensity(); + uDensity += volData.getVoxelAt(x+1,y-0,z+1).getDensity(); + uDensity += volData.getVoxelAt(x+1,y+1,z-1).getDensity(); + uDensity += volData.getVoxelAt(x+1,y+1,z-0).getDensity(); + uDensity += volData.getVoxelAt(x+1,y+1,z+1).getDensity(); + uDensity /= 27; + } + } + } + + for (int z = croppedRegion.getLowerCorner().getZ(); z < croppedRegion.getUpperCorner().getZ(); z++) + { + for (int y = croppedRegion.getLowerCorner().getY(); y < croppedRegion.getUpperCorner().getY(); y++) + { + for (int x = croppedRegion.getLowerCorner().getX(); x < croppedRegion.getUpperCorner().getX(); x++) + { + uint16_t& uDensity = temp[x-croppedRegion.getLowerCorner().getX()][y-croppedRegion.getLowerCorner().getY()][z-croppedRegion.getLowerCorner().getZ()]; + + MaterialDensityPair44 val = volData.getVoxelAt(x,y,z); + val.setDensity(uDensity); + volData.setVoxelAt(x,y,z,val); + } + } + } + } +} \ No newline at end of file