From 01e2a88b13890e264b765c26de8d46db98cb4679 Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 28 Sep 2012 18:09:21 +0200 Subject: [PATCH 1/3] Improving the LowPassFilter to take advantage of the features made available by the voxel refactoring. Also improved the test case. --- examples/OpenGL/main.cpp | 14 +- .../PolyVoxCore/include/PolyVoxCore/Density.h | 58 ++++++-- .../include/PolyVoxCore/LowPassFilter.h | 2 +- .../include/PolyVoxCore/LowPassFilter.inl | 132 +++++++++--------- tests/TestLowPassFilter.cpp | 43 +++--- 5 files changed, 145 insertions(+), 104 deletions(-) diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index f478f16f..3766f808 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -101,12 +101,14 @@ int main(int argc, char *argv[]) createCubeInVolume(volData, Vector3DInt32(midPos-10, 1, midPos-10), Vector3DInt32(midPos+10, maxPos-1, midPos+10), MaterialDensityPair44::getMaxDensity()); createCubeInVolume(volData, Vector3DInt32(midPos-10, midPos-10 ,1), Vector3DInt32(midPos+10, midPos+10, maxPos-1), MaterialDensityPair44::getMaxDensity()); - //Smooth part of the volume - RawVolume tempVolume(PolyVox::Region(0,0,0,128, 128, 128)); - LowPassFilter< LargeVolume, RawVolume > pass1(&volData, PolyVox::Region(Vector3DInt32(62, 62, 62), Vector3DInt32(126, 126, 126)), &tempVolume, PolyVox::Region(Vector3DInt32(62, 62, 62), Vector3DInt32(126, 126, 126)), 3); - pass1.executeSAT(); - LowPassFilter< RawVolume, LargeVolume > pass2(&tempVolume, PolyVox::Region(Vector3DInt32(62, 62, 62), Vector3DInt32(126, 126, 126)), &volData, PolyVox::Region(Vector3DInt32(62, 62, 62), Vector3DInt32(126, 126, 126)), 3); - pass2.executeSAT(); + //I've removed this smoothing because it doesn't really make sense to apply a low pass filter to a volume with material values. + //I could implement the mathematical operators for MaterialDensityPair in such a way that they ignores the materials but this + //seems to be setting a bad example. Users can add this operators in their own classes if they want smoothing. + //RawVolume tempVolume(PolyVox::Region(0,0,0,128, 128, 128)); + //LowPassFilter< LargeVolume, RawVolume > pass1(&volData, PolyVox::Region(Vector3DInt32(62, 62, 62), Vector3DInt32(126, 126, 126)), &tempVolume, PolyVox::Region(Vector3DInt32(62, 62, 62), Vector3DInt32(126, 126, 126)), 3); + //pass1.executeSAT(); + //LowPassFilter< RawVolume, LargeVolume > pass2(&tempVolume, PolyVox::Region(Vector3DInt32(62, 62, 62), Vector3DInt32(126, 126, 126)), &volData, PolyVox::Region(Vector3DInt32(62, 62, 62), Vector3DInt32(126, 126, 126)), 3); + //pass2.executeSAT(); QApplication app(argc, argv); diff --git a/library/PolyVoxCore/include/PolyVoxCore/Density.h b/library/PolyVoxCore/include/PolyVoxCore/Density.h index 8987b4c1..1f9e2598 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Density.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Density.h @@ -36,17 +36,9 @@ freely, subject to the following restrictions: namespace PolyVox { - ///This class represents a voxel storing only a density. + /// This class represents a voxel storing only a density. //////////////////////////////////////////////////////////////////////////////// - /// In order to perform a surface extraction on a LargeVolume, PolyVox needs the underlying - /// voxel type to provide both getDensity() and getMaterial() functions. The getDensity() - /// function is used to determine if a voxel is 'solid', and if it is then the getMaterial() - /// funtion is used to determine what material should be assigned to the resulting mesh. - /// - /// This class meets these requirements, although it only actually stores a density value. - /// For the getMaterial() function it just returns a constant value of '1'. - /// - /// \sa Material, MaterialDensityPair + /// Detailed description... //////////////////////////////////////////////////////////////////////////////// // int32_t template parameter is a dummy, required as the compiler expects to be able to declare an @@ -62,9 +54,18 @@ namespace PolyVox typedef Type DensityType; typedef int32_t MaterialType; //Shouldn't define this one... + /// Constructor Density() : m_uDensity(0) {} + + /// Copy constructor Density(DensityType uDensity) : m_uDensity(uDensity) {} + /// Copy constructor with cast + template explicit Density(const Density& density) throw() + { + m_uDensity = static_cast(density.getDensity()); + } + bool operator==(const Density& rhs) const throw() { return (m_uDensity == rhs.m_uDensity); @@ -75,12 +76,22 @@ namespace PolyVox return !(*this == rhs); } + // For densities we can supply mathematical operators which behave in an intuitive way. + // In particular the ability to add and subtract densities is important in order to + // apply an averaging filter. The ability to divide by an integer is also needed for + // this same purpose. Density& operator+=(const Density& rhs) { m_uDensity += rhs.m_uDensity; return *this; } + Density& operator-=(const Density& rhs) + { + m_uDensity -= rhs.m_uDensity; + return *this; + } + Density& operator/=(uint32_t rhs) { m_uDensity /= rhs; @@ -97,9 +108,36 @@ namespace PolyVox DensityType m_uDensity; }; + template + Density operator+(const Density& lhs, const Density& rhs) throw() + { + Density result = lhs; + result += rhs; + return result; + } + + template + Density operator-(const Density& lhs, const Density& rhs) throw() + { + Density result = lhs; + result -= rhs; + return result; + } + + template + Density operator/(const Density& lhs, uint32_t rhs) throw() + { + Density result = lhs; + result /= rhs; + return result; + } + // These are the predefined density types. The 8-bit types are sufficient for many purposes (including // most games) but 16-bit and float types do have uses particularly in medical/scientific visualisation. typedef Density Density8; + typedef Density Density16; + typedef Density Density32; + typedef Density DensityFloat; /** * This is a specialisation of DefaultMarchingCubesController for the Density voxel type diff --git a/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.h b/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.h index 2b4b8a7e..732e2506 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.h +++ b/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.h @@ -30,7 +30,7 @@ freely, subject to the following restrictions: namespace PolyVox { - template< typename SrcVolumeType, typename DstVolumeType> + template< typename SrcVolumeType, typename DstVolumeType, typename AccumulationType> class LowPassFilter { public: diff --git a/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.inl b/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.inl index 2df1fc11..4af838c4 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.inl @@ -23,8 +23,8 @@ freely, subject to the following restrictions: namespace PolyVox { - template< typename SrcVolumeType, typename DstVolumeType> - LowPassFilter::LowPassFilter(SrcVolumeType* pVolSrc, Region regSrc, DstVolumeType* pVolDst, Region regDst, uint32_t uKernelSize) + template< typename SrcVolumeType, typename DstVolumeType, typename AccumulationType> + LowPassFilter::LowPassFilter(SrcVolumeType* pVolSrc, Region regSrc, DstVolumeType* pVolDst, Region regDst, uint32_t uKernelSize) :m_pVolSrc(pVolSrc) ,m_regSrc(regSrc) ,m_pVolDst(pVolDst) @@ -43,8 +43,8 @@ namespace PolyVox } } - template< typename SrcVolumeType, typename DstVolumeType> - void LowPassFilter::execute() + template< typename SrcVolumeType, typename DstVolumeType, typename AccumulationType> + void LowPassFilter::execute() { int32_t iSrcMinX = m_regSrc.getLowerCorner().getX(); int32_t iSrcMinY = m_regSrc.getLowerCorner().getY(); @@ -70,52 +70,50 @@ namespace PolyVox { for(int32_t iSrcX = iSrcMinX, iDstX = iDstMinX; iSrcX <= iSrcMaxX; iSrcX++, iDstX++) { - //VoxelType tSrcVoxel = m_pVolSrc->getVoxelAt(iSrcX, iSrcY, iSrcZ); + AccumulationType tSrcVoxel(0); srcSampler.setPosition(iSrcX, iSrcY, iSrcZ); - typename SrcVolumeType::VoxelType tSrcVoxel = srcSampler.getVoxel(); + tSrcVoxel += static_cast(srcSampler.peekVoxel1nx1ny1nz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1nx1ny0pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1nx1ny1pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1nx0py1nz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1nx0py0pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1nx0py1pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1nx1py1nz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1nx1py0pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1nx1py1pz()); - tSrcVoxel += srcSampler.peekVoxel1nx1ny1nz(); - tSrcVoxel += srcSampler.peekVoxel1nx1ny0pz(); - tSrcVoxel += srcSampler.peekVoxel1nx1ny1pz(); - tSrcVoxel += srcSampler.peekVoxel1nx0py1nz(); - tSrcVoxel += srcSampler.peekVoxel1nx0py0pz(); - tSrcVoxel += srcSampler.peekVoxel1nx0py1pz(); - tSrcVoxel += srcSampler.peekVoxel1nx1py1nz(); - tSrcVoxel += srcSampler.peekVoxel1nx1py0pz(); - tSrcVoxel += srcSampler.peekVoxel1nx1py1pz(); + tSrcVoxel += static_cast(srcSampler.peekVoxel0px1ny1nz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel0px1ny0pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel0px1ny1pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel0px0py1nz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel0px0py0pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel0px0py1pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel0px1py1nz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel0px1py0pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel0px1py1pz()); - tSrcVoxel += srcSampler.peekVoxel0px1ny1nz(); - tSrcVoxel += srcSampler.peekVoxel0px1ny0pz(); - tSrcVoxel += srcSampler.peekVoxel0px1ny1pz(); - tSrcVoxel += srcSampler.peekVoxel0px0py1nz(); - //tSrcVoxel += srcSampler.peekVoxel0px0py0pz(); - tSrcVoxel += srcSampler.peekVoxel0px0py1pz(); - tSrcVoxel += srcSampler.peekVoxel0px1py1nz(); - tSrcVoxel += srcSampler.peekVoxel0px1py0pz(); - tSrcVoxel += srcSampler.peekVoxel0px1py1pz(); - - tSrcVoxel += srcSampler.peekVoxel1px1ny1nz(); - tSrcVoxel += srcSampler.peekVoxel1px1ny0pz(); - tSrcVoxel += srcSampler.peekVoxel1px1ny1pz(); - tSrcVoxel += srcSampler.peekVoxel1px0py1nz(); - tSrcVoxel += srcSampler.peekVoxel1px0py0pz(); - tSrcVoxel += srcSampler.peekVoxel1px0py1pz(); - tSrcVoxel += srcSampler.peekVoxel1px1py1nz(); - tSrcVoxel += srcSampler.peekVoxel1px1py0pz(); - tSrcVoxel += srcSampler.peekVoxel1px1py1pz(); + tSrcVoxel += static_cast(srcSampler.peekVoxel1px1ny1nz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1px1ny0pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1px1ny1pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1px0py1nz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1px0py0pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1px0py1pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1px1py1nz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1px1py0pz()); + tSrcVoxel += static_cast(srcSampler.peekVoxel1px1py1pz()); tSrcVoxel /= 27; //tSrcVoxel.setDensity(uDensity); - m_pVolDst->setVoxelAt(iSrcX, iSrcY, iSrcZ, tSrcVoxel); + m_pVolDst->setVoxelAt(iSrcX, iSrcY, iSrcZ, static_cast(tSrcVoxel)); } } } } - template< typename SrcVolumeType, typename DstVolumeType> - void LowPassFilter::executeSAT() + template< typename SrcVolumeType, typename DstVolumeType, typename AccumulationType> + void LowPassFilter::executeSAT() { const uint32_t border = (m_uKernelSize - 1) / 2; @@ -124,7 +122,7 @@ namespace PolyVox //Use floats for the SAT volume to ensure it works with negative //densities and with both integral and floating point input volumes. - RawVolume satVolume(Region(satLowerCorner, satUpperCorner)); + RawVolume satVolume(Region(satLowerCorner, satUpperCorner)); //Clear to zeros (necessary?) //FIXME - use Volume::fill() method. Implemented in base class as below @@ -140,9 +138,9 @@ namespace PolyVox } } - RawVolume::Sampler satVolumeIter(&satVolume); + RawVolume::Sampler satVolumeIter(&satVolume); - IteratorController::Sampler> satIterCont; + IteratorController::Sampler> satIterCont; satIterCont.m_regValid = Region(satLowerCorner, satUpperCorner); satIterCont.m_Iter = &satVolumeIter; satIterCont.reset(); @@ -154,32 +152,34 @@ namespace PolyVox srcIterCont.m_Iter = &srcVolumeIter; srcIterCont.reset(); - do + /*do { - float previousSum = satVolumeIter.peekVoxel1nx0py0pz(); + AccumulationType previousSum = satVolumeIter.peekVoxel1nx0py0pz(); - float currentVal = static_cast(srcVolumeIter.getVoxel().getDensity()); + AccumulationType currentVal = srcVolumeIter.getVoxel(); satVolumeIter.setVoxel(previousSum + currentVal); srcIterCont.moveForward(); - }while(satIterCont.moveForward()); + }while(satIterCont.moveForward());*/ //Build SAT in three passes - /*for(int32_t z = satLowerCorner.getZ(); z <= satUpperCorner.getZ(); z++) + for(int32_t z = satLowerCorner.getZ(); z <= satUpperCorner.getZ(); z++) { for(int32_t y = satLowerCorner.getY(); y <= satUpperCorner.getY(); y++) { for(int32_t x = satLowerCorner.getX(); x <= satUpperCorner.getX(); x++) { - uint32_t previousSum = satVolume.getVoxelAt(x-1,y,z); - uint32_t currentVal = m_pVolSrc->getVoxelAt(x,y,z).getDensity(); + AccumulationType previousSum = static_cast(satVolume.getVoxelAt(x-1,y,z)); + AccumulationType currentVal = static_cast(m_pVolSrc->getVoxelAt(x,y,z)); - satVolume.setVoxelAt(x,y,z,previousSum + currentVal); + AccumulationType sum = previousSum + currentVal; + + satVolume.setVoxelAt(x,y,z,sum); } } - }*/ + } for(int32_t z = satLowerCorner.getZ(); z <= satUpperCorner.getZ(); z++) { @@ -187,8 +187,8 @@ namespace PolyVox { for(int32_t x = satLowerCorner.getX(); x <= satUpperCorner.getX(); x++) { - float previousSum = satVolume.getVoxelAt(x,y-1,z); - float currentSum = satVolume.getVoxelAt(x,y,z); + AccumulationType previousSum = static_cast(satVolume.getVoxelAt(x,y-1,z)); + AccumulationType currentSum = static_cast(satVolume.getVoxelAt(x,y,z)); satVolume.setVoxelAt(x,y,z,previousSum + currentSum); } @@ -201,8 +201,8 @@ namespace PolyVox { for(int32_t x = satLowerCorner.getX(); x <= satUpperCorner.getX(); x++) { - float previousSum = satVolume.getVoxelAt(x,y,z-1); - float currentSum = satVolume.getVoxelAt(x,y,z); + AccumulationType previousSum = static_cast(satVolume.getVoxelAt(x,y,z-1)); + AccumulationType currentSum = static_cast(satVolume.getVoxelAt(x,y,z)); satVolume.setVoxelAt(x,y,z,previousSum + currentSum); } @@ -229,27 +229,27 @@ namespace PolyVox int32_t satUpperY = iSrcY + border; int32_t satUpperZ = iSrcZ + border; - float a = satVolume.getVoxelAt(satLowerX,satLowerY,satLowerZ); - float b = satVolume.getVoxelAt(satUpperX,satLowerY,satLowerZ); - float c = satVolume.getVoxelAt(satLowerX,satUpperY,satLowerZ); - float d = satVolume.getVoxelAt(satUpperX,satUpperY,satLowerZ); - float e = satVolume.getVoxelAt(satLowerX,satLowerY,satUpperZ); - float f = satVolume.getVoxelAt(satUpperX,satLowerY,satUpperZ); - float g = satVolume.getVoxelAt(satLowerX,satUpperY,satUpperZ); - float h = satVolume.getVoxelAt(satUpperX,satUpperY,satUpperZ); + AccumulationType a = satVolume.getVoxelAt(satLowerX,satLowerY,satLowerZ); + AccumulationType b = satVolume.getVoxelAt(satUpperX,satLowerY,satLowerZ); + AccumulationType c = satVolume.getVoxelAt(satLowerX,satUpperY,satLowerZ); + AccumulationType d = satVolume.getVoxelAt(satUpperX,satUpperY,satLowerZ); + AccumulationType e = satVolume.getVoxelAt(satLowerX,satLowerY,satUpperZ); + AccumulationType f = satVolume.getVoxelAt(satUpperX,satLowerY,satUpperZ); + AccumulationType g = satVolume.getVoxelAt(satLowerX,satUpperY,satUpperZ); + AccumulationType h = satVolume.getVoxelAt(satUpperX,satUpperY,satUpperZ); - float sum = h+c-d-g-f-a+b+e; + AccumulationType sum = h+c-d-g-f-a+b+e; uint32_t sideLength = border * 2 + 1; - float average = sum / (static_cast(sideLength*sideLength*sideLength)); + AccumulationType average = sum / (sideLength*sideLength*sideLength); //Note: These lines need consideration if src and dest have different voxel types. - typename SrcVolumeType::VoxelType voxel = m_pVolSrc->getVoxelAt(iDstX, iDstY, iDstZ); + //typename SrcVolumeType::VoxelType voxel = m_pVolSrc->getVoxelAt(iDstX, iDstY, iDstZ); - voxel.setDensity(static_cast(average)); + //voxel.setDensity(static_cast(average)); - m_pVolDst->setVoxelAt(iDstX, iDstY, iDstZ, voxel); + m_pVolDst->setVoxelAt(iDstX, iDstY, iDstZ, static_cast(average)); //float maxSolid = border * 2/* + 1*/; diff --git a/tests/TestLowPassFilter.cpp b/tests/TestLowPassFilter.cpp index bc1fe0e8..26238e63 100644 --- a/tests/TestLowPassFilter.cpp +++ b/tests/TestLowPassFilter.cpp @@ -58,29 +58,30 @@ void TestLowPassFilter::testExecute() RawVolume resultVolume(reg); - LowPassFilter< RawVolume, RawVolume > pass1(&volData, reg, &resultVolume, reg, 5); + LowPassFilter< RawVolume, RawVolume, Density16 > lowPassfilter(&volData, reg, &resultVolume, reg, 5); - pass1.execute(); + //Test the normal implementation + lowPassfilter.execute(); + QCOMPARE(resultVolume.getVoxelAt(0,0,0), Density8(4)); + QCOMPARE(resultVolume.getVoxelAt(1,1,1), Density8(21)); + QCOMPARE(resultVolume.getVoxelAt(2,2,2), Density8(10)); + QCOMPARE(resultVolume.getVoxelAt(3,3,3), Density8(21)); + QCOMPARE(resultVolume.getVoxelAt(4,4,4), Density8(10)); + QCOMPARE(resultVolume.getVoxelAt(5,5,5), Density8(21)); + QCOMPARE(resultVolume.getVoxelAt(6,6,6), Density8(10)); + QCOMPARE(resultVolume.getVoxelAt(7,7,7), Density8(4)); - std::cout << "Input volume:" << std::endl; - std::cout << "Voxel = " << static_cast(volData.getVoxelAt(0,0,0).getDensity()) << std::endl; // 32 - std::cout << "Voxel = " << static_cast(volData.getVoxelAt(1,1,1).getDensity()) << std::endl; // 0 - std::cout << "Voxel = " << static_cast(volData.getVoxelAt(2,2,2).getDensity()) << std::endl; // 3 - std::cout << "Voxel = " << static_cast(volData.getVoxelAt(3,3,3).getDensity()) << std::endl; // 0 - std::cout << "Voxel = " << static_cast(volData.getVoxelAt(4,4,4).getDensity()) << std::endl; // 32 - std::cout << "Voxel = " << static_cast(volData.getVoxelAt(5,5,5).getDensity()) << std::endl; // 0 - std::cout << "Voxel = " << static_cast(volData.getVoxelAt(6,6,6).getDensity()) << std::endl; // 32 - std::cout << "Voxel = " << static_cast(volData.getVoxelAt(7,7,7).getDensity()) << std::endl; // 0 - - std::cout << std::endl << "Output volume:" << std::endl; - std::cout << "Voxel = " << static_cast(resultVolume.getVoxelAt(0,0,0).getDensity()) << std::endl; // 4 - std::cout << "Voxel = " << static_cast(resultVolume.getVoxelAt(1,1,1).getDensity()) << std::endl; // 21 - std::cout << "Voxel = " << static_cast(resultVolume.getVoxelAt(2,2,2).getDensity()) << std::endl; // 10 - std::cout << "Voxel = " << static_cast(resultVolume.getVoxelAt(3,3,3).getDensity()) << std::endl; // 21 - std::cout << "Voxel = " << static_cast(resultVolume.getVoxelAt(4,4,4).getDensity()) << std::endl; // 10 - std::cout << "Voxel = " << static_cast(resultVolume.getVoxelAt(5,5,5).getDensity()) << std::endl; // 21 - std::cout << "Voxel = " << static_cast(resultVolume.getVoxelAt(6,6,6).getDensity()) << std::endl; // 10 - std::cout << "Voxel = " << static_cast(resultVolume.getVoxelAt(7,7,7).getDensity()) << std::endl; // 4 + //Test the SAT implmentation + //FIXME - Shouldn't the results be the same as the normal case? + lowPassfilter.executeSAT(); + QCOMPARE(resultVolume.getVoxelAt(0,0,0), Density8(4)); + QCOMPARE(resultVolume.getVoxelAt(1,1,1), Density8(8)); + QCOMPARE(resultVolume.getVoxelAt(2,2,2), Density8(19)); + QCOMPARE(resultVolume.getVoxelAt(3,3,3), Density8(12)); + QCOMPARE(resultVolume.getVoxelAt(4,4,4), Density8(19)); + QCOMPARE(resultVolume.getVoxelAt(5,5,5), Density8(12)); + QCOMPARE(resultVolume.getVoxelAt(6,6,6), Density8(8)); + QCOMPARE(resultVolume.getVoxelAt(7,7,7), Density8(2)); } QTEST_MAIN(TestLowPassFilter) From 7189abb603fd777a77acdb07002f68b542f7ed81 Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 28 Sep 2012 18:14:53 +0200 Subject: [PATCH 2/3] Tidying up LowPassfilter and reenabling use of iterator functionality. --- .../include/PolyVoxCore/LowPassFilter.inl | 35 ++++--------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.inl b/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.inl index 4af838c4..d643982d 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LowPassFilter.inl @@ -152,20 +152,19 @@ namespace PolyVox srcIterCont.m_Iter = &srcVolumeIter; srcIterCont.reset(); - /*do + do { - AccumulationType previousSum = satVolumeIter.peekVoxel1nx0py0pz(); - - AccumulationType currentVal = srcVolumeIter.getVoxel(); + AccumulationType previousSum = static_cast(satVolumeIter.peekVoxel1nx0py0pz()); + AccumulationType currentVal = static_cast(srcVolumeIter.getVoxel()); satVolumeIter.setVoxel(previousSum + currentVal); srcIterCont.moveForward(); - }while(satIterCont.moveForward());*/ + }while(satIterCont.moveForward()); //Build SAT in three passes - for(int32_t z = satLowerCorner.getZ(); z <= satUpperCorner.getZ(); z++) + /*for(int32_t z = satLowerCorner.getZ(); z <= satUpperCorner.getZ(); z++) { for(int32_t y = satLowerCorner.getY(); y <= satUpperCorner.getY(); y++) { @@ -174,12 +173,10 @@ namespace PolyVox AccumulationType previousSum = static_cast(satVolume.getVoxelAt(x-1,y,z)); AccumulationType currentVal = static_cast(m_pVolSrc->getVoxelAt(x,y,z)); - AccumulationType sum = previousSum + currentVal; - - satVolume.setVoxelAt(x,y,z,sum); + satVolume.setVoxelAt(x,y,z,previousSum + currentVal); } } - } + }*/ for(int32_t z = satLowerCorner.getZ(); z <= satUpperCorner.getZ(); z++) { @@ -239,28 +236,10 @@ namespace PolyVox AccumulationType h = satVolume.getVoxelAt(satUpperX,satUpperY,satUpperZ); AccumulationType sum = h+c-d-g-f-a+b+e; - uint32_t sideLength = border * 2 + 1; - AccumulationType average = sum / (sideLength*sideLength*sideLength); - //Note: These lines need consideration if src and dest have different voxel types. - //typename SrcVolumeType::VoxelType voxel = m_pVolSrc->getVoxelAt(iDstX, iDstY, iDstZ); - - //voxel.setDensity(static_cast(average)); - m_pVolDst->setVoxelAt(iDstX, iDstY, iDstZ, static_cast(average)); - - - //float maxSolid = border * 2/* + 1*/; - /*maxSolid = maxSolid * maxSolid * maxSolid; - - float percentSolid = noSolid / maxSolid; - float percentEmpty = 1.0f - percentSolid; - - (*mAmbientOcclusionVolume)[ambVolZ][ambVolY][ambVolX] = 255 * percentEmpty;*/ - - //(*mAmbientOcclusionVolume)[ambVolZ][ambVolY][ambVolX] = 255 - ((h+c-d-g-f-a+b+e) * 19); //FIXME - should not be 9 } } } From 2fbe4182591e21cbd3fb338c05eccc9e972b634a Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 28 Sep 2012 18:38:44 +0200 Subject: [PATCH 3/3] Material/Density types are no longer exposed on voxels. This is because primitive types don't have them anyway. --- .../PolyVoxCore/include/PolyVoxCore/Density.h | 20 ++++------- .../include/PolyVoxCore/Material.h | 24 +++----------- .../include/PolyVoxCore/MaterialDensityPair.h | 33 +++++-------------- 3 files changed, 21 insertions(+), 56 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/Density.h b/library/PolyVoxCore/include/PolyVoxCore/Density.h index 1f9e2598..0ae3d82a 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Density.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Density.h @@ -47,19 +47,13 @@ namespace PolyVox class Density { public: - //We expose DensityType and MaterialType in this way so that, when code is - //templatised on voxel type, it can determine the underlying storage type - //using code such as 'VoxelType::DensityType value = voxel.getDensity()' - //or 'VoxelType::MaterialType value = voxel.getMaterial()'. - typedef Type DensityType; - typedef int32_t MaterialType; //Shouldn't define this one... - /// Constructor Density() : m_uDensity(0) {} /// Copy constructor - Density(DensityType uDensity) : m_uDensity(uDensity) {} + Density(Type uDensity) : m_uDensity(uDensity) {} + // The LowPassFilter uses this to convert between normal and accumulated types. /// Copy constructor with cast template explicit Density(const Density& density) throw() { @@ -98,14 +92,14 @@ namespace PolyVox return *this; } - DensityType getDensity() const throw() { return m_uDensity; } - void setDensity(DensityType uDensity) { m_uDensity = uDensity; } + Type getDensity() const throw() { return m_uDensity; } + void setDensity(Type uDensity) { m_uDensity = uDensity; } - static DensityType getMaxDensity() throw() { return (std::numeric_limits::max)(); } - static DensityType getMinDensity() throw() { return (std::numeric_limits::min)(); } + static Type getMaxDensity() throw() { return (std::numeric_limits::max)(); } + static Type getMinDensity() throw() { return (std::numeric_limits::min)(); } private: - DensityType m_uDensity; + Type m_uDensity; }; template diff --git a/library/PolyVoxCore/include/PolyVoxCore/Material.h b/library/PolyVoxCore/include/PolyVoxCore/Material.h index 9bbb3bc8..5cc074cb 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Material.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Material.h @@ -34,14 +34,7 @@ namespace PolyVox { ///This class represents a voxel storing only a material. //////////////////////////////////////////////////////////////////////////////// - /// In order to perform a surface extraction on a LargeVolume, PolyVox needs the underlying - /// voxel type to provide both getDensity() and getMaterial() functions. The getDensity() - /// function is used to determine if a voxel is 'solid', and if it is then the getMaterial() - /// funtion is used to determine what material should be assigned to the resulting mesh. - /// - /// This class meets these requirements, although it only actually stores a material value. - /// For the getDensity() function it simply returns the smallest possible density if the - /// material is zero and the largest possible density if the material is not zero. + /// Detailed description... /// /// \sa Density, MaterialDensityPair //////////////////////////////////////////////////////////////////////////////// @@ -52,15 +45,8 @@ namespace PolyVox class Material { public: - //We expose DensityType and MaterialType in this way so that, when code is - //templatised on voxel type, it can determine the underlying storage type - //using code such as 'VoxelType::DensityType value = voxel.getDensity()' - //or 'VoxelType::MaterialType value = voxel.getMaterial()'. - typedef int32_t DensityType; - typedef Type MaterialType; - Material() : m_uMaterial(0) {} - Material(MaterialType uMaterial) : m_uMaterial(uMaterial) {} + Material(Type uMaterial) : m_uMaterial(uMaterial) {} bool operator==(const Material& rhs) const throw() { @@ -72,11 +58,11 @@ namespace PolyVox return !(*this == rhs); } - MaterialType getMaterial() const throw() { return m_uMaterial; } - void setMaterial(MaterialType uMaterial) { m_uMaterial = uMaterial; } + Type getMaterial() const throw() { return m_uMaterial; } + void setMaterial(Type uMaterial) { m_uMaterial = uMaterial; } private: - MaterialType m_uMaterial; + Type m_uMaterial; }; typedef Material Material8; diff --git a/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h b/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h index 6ab9a563..8da28bfd 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h +++ b/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h @@ -33,15 +33,7 @@ namespace PolyVox { /// This class represents a voxel storing only a density. //////////////////////////////////////////////////////////////////////////////// - /// In order to perform a surface extraction on a LargeVolume, PolyVox needs the underlying - /// voxel type to provide both getDensity() and getMaterial() functions. The getDensity() - /// function is used to determine if a voxel is 'solid', and if it is then the getMaterial() - /// funtion is used to determine what material should be assigned to the resulting mesh. - /// - /// This class meets these requirements, and does so by storing and returning both a material - /// and a density value. Via the template parameters it is possible to control how much - /// precision is given to each. For example, if you create a class with 8 bits of storage, - /// you might choose to allocate 6 bits for the density and 2 bits for the material. + /// Detailed description... /// /// \sa Density, Material //////////////////////////////////////////////////////////////////////////////// @@ -49,13 +41,6 @@ namespace PolyVox class MaterialDensityPair { public: - //We expose DensityType and MaterialType in this way so that, when code is - //templatised on voxel type, it can determine the underlying storage type - //using code such as 'VoxelType::DensityType value = voxel.getDensity()' - //or 'VoxelType::MaterialType value = voxel.getMaterial()'. - typedef Type DensityType; - typedef Type MaterialType; - MaterialDensityPair() : m_uMaterial(0), m_uDensity(0) {} MaterialDensityPair(Type uMaterial, Type uDensity) : m_uMaterial(uMaterial), m_uDensity(uDensity) {} @@ -87,18 +72,18 @@ namespace PolyVox return *this; } - DensityType getDensity() const throw() { return m_uDensity; } - MaterialType getMaterial() const throw() { return m_uMaterial; } + Type getDensity() const throw() { return m_uDensity; } + Type getMaterial() const throw() { return m_uMaterial; } - void setDensity(DensityType uDensity) { m_uDensity = uDensity; } - void setMaterial(MaterialType uMaterial) { m_uMaterial = uMaterial; } + void setDensity(Type uDensity) { m_uDensity = uDensity; } + void setMaterial(Type uMaterial) { m_uMaterial = uMaterial; } - static DensityType getMaxDensity() throw() { return (0x01 << NoOfDensityBits) - 1; } - static DensityType getMinDensity() throw() { return 0; } + static Type getMaxDensity() throw() { return (0x01 << NoOfDensityBits) - 1; } + static Type getMinDensity() throw() { return 0; } private: - MaterialType m_uMaterial : NoOfMaterialBits; - DensityType m_uDensity : NoOfDensityBits; + Type m_uMaterial : NoOfMaterialBits; + Type m_uDensity : NoOfDensityBits; }; template