From b3f3723a532ebd0b6f1b3f667f6f0377ba686ec3 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 27 Mar 2012 16:06:43 +0200 Subject: [PATCH] Added test for cubic surfce extractor. --- tests/CMakeLists.txt | 3 + tests/TestCubicSurfaceExtractor.cpp | 162 ++++++++++++++++++++++++++++ tests/TestCubicSurfaceExtractor.h | 37 +++++++ 3 files changed, 202 insertions(+) create mode 100644 tests/TestCubicSurfaceExtractor.cpp create mode 100644 tests/TestCubicSurfaceExtractor.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4f1f6179..2ab4e40e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -47,6 +47,9 @@ ADD_TEST(ArrayReadWriteTest ${LATEST_TEST} testReadWrite) CREATE_TEST(TestAStarPathfinder.h TestAStarPathfinder.cpp TestAStarPathfinder) ADD_TEST(AStarPathfinderExecuteTest ${LATEST_TEST} testExecute) +CREATE_TEST(TestCubicSurfaceExtractor.h TestCubicSurfaceExtractor.cpp TestCubicSurfaceExtractor) +ADD_TEST(CubicSurfaceExtractorExecuteTest ${LATEST_TEST} testExecute) + # Low pass filter tests CREATE_TEST(TestLowPassFilter.h TestLowPassFilter.cpp TestLowPassFilter) ADD_TEST(LowPassFilterExecuteTest ${LATEST_TEST} testExecute) diff --git a/tests/TestCubicSurfaceExtractor.cpp b/tests/TestCubicSurfaceExtractor.cpp new file mode 100644 index 00000000..8a48c897 --- /dev/null +++ b/tests/TestCubicSurfaceExtractor.cpp @@ -0,0 +1,162 @@ +/******************************************************************************* +Copyright (c) 2010 Matt 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. +*******************************************************************************/ + +#include "TestCubicSurfaceExtractor.h" + +#include "PolyVoxCore/Density.h" +#include "PolyVoxCore/Material.h" +#include "PolyVoxCore/MaterialDensityPair.h" +#include "PolyVoxCore/SimpleVolume.h" +#include "PolyVoxCore/CubicSurfaceExtractorWithNormals.h" + +#include + +using namespace PolyVox; + + +// These 'writeDensityValueToVoxel' functions provide a unified interface for writting densities to primative and class voxel types. +// They are conceptually the inverse of the 'convertToDensity' function used by the SurfaceExtractor. They probably shouldn't be part +// of PolyVox, but they might be useful to other tests so we cold move them into a 'Tests.h' or something in the future. +template +void writeDensityValueToVoxel(typename VoxelTypeTraits::DensityType valueToWrite, VoxelType& voxel) +{ + voxel = valueToWrite; +} + +template<> +void writeDensityValueToVoxel(typename VoxelTypeTraits::DensityType valueToWrite, Density8& voxel) +{ + voxel.setDensity(valueToWrite); +} + +template<> +void writeDensityValueToVoxel(typename VoxelTypeTraits::DensityType valueToWrite, MaterialDensityPair88& voxel) +{ + voxel.setDensity(valueToWrite); +} + +template +void writeMaterialValueToVoxel(typename VoxelTypeTraits::MaterialType valueToWrite, VoxelType& voxel) +{ + //Most types don't have a material + return; +} + +template<> +void writeMaterialValueToVoxel(typename VoxelTypeTraits::MaterialType valueToWrite, MaterialDensityPair88& voxel) +{ + voxel.setMaterial(valueToWrite); +} + +// Runs the surface extractor for a given type. +template +void testForType(SurfaceMesh& result) +{ + const int32_t uVolumeSideLength = 32; + + //Create empty volume + SimpleVolume volData(Region(Vector3DInt32(0,0,0), Vector3DInt32(uVolumeSideLength-1, uVolumeSideLength-1, uVolumeSideLength-1))); + + for (int32_t z = 0; z < uVolumeSideLength; z++) + { + for (int32_t y = 0; y < uVolumeSideLength; y++) + { + for (int32_t x = 0; x < uVolumeSideLength; x++) + { + if(x + y + z > uVolumeSideLength) + { + VoxelType voxelValue; + writeDensityValueToVoxel(100, voxelValue); + writeMaterialValueToVoxel(42, voxelValue); + volData.setVoxelAt(x, y, z, voxelValue); + } + } + } + } + + CubicSurfaceExtractorWithNormals extractor(&volData, volData.getEnclosingRegion(), &result, 50); + extractor.execute(); +} + +void TestCubicSurfaceExtractor::testExecute() +{ + const static uint32_t uExpectedVertices = 6624; + const static uint32_t uExpectedIndices = 9936; + const static uint32_t uMaterialToCheck = 3000; + const static float fExpectedMaterial = 42.0f; + const static float fNoMaterial = 1.0f; + + SurfaceMesh mesh; + + /*testForType(mesh); + QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); + QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial); + + testForType(mesh); + QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); + QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial); + + testForType(mesh); + QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); + QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial); + + testForType(mesh); + QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); + QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial); + + testForType(mesh); + QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); + QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial); + + testForType(mesh); + QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); + QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial); + + testForType(mesh); + QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); + QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial); + + testForType(mesh); + QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); + QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial);*/ + + /*testForType(mesh); + QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); + QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial);*/ + + testForType(mesh); + QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); + QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), fExpectedMaterial); +} + +QTEST_MAIN(TestCubicSurfaceExtractor) diff --git a/tests/TestCubicSurfaceExtractor.h b/tests/TestCubicSurfaceExtractor.h new file mode 100644 index 00000000..1ea815d8 --- /dev/null +++ b/tests/TestCubicSurfaceExtractor.h @@ -0,0 +1,37 @@ +/******************************************************************************* +Copyright (c) 2010 Matt 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. +*******************************************************************************/ + +#ifndef __PolyVox_TestCubicSurfaceExtractor_H__ +#define __PolyVox_TestCubicSurfaceExtractor_H__ + +#include + +class TestCubicSurfaceExtractor: public QObject +{ + Q_OBJECT + + private slots: + void testExecute(); +}; + +#endif