VOXELUTIL: added unittest for fillHollow

master
Martin Gerhardy 2022-05-01 21:49:07 +02:00
parent 879b1143c4
commit 0422bbddcd
3 changed files with 71 additions and 9 deletions

View File

@ -28,6 +28,7 @@ set(TEST_SRCS
tests/VolumeRotatorTest.cpp
tests/VolumeSplitterTest.cpp
tests/VolumeCropperTest.cpp
tests/VoxelUtilTest.cpp
)
set(TEST_FILES

View File

@ -59,7 +59,7 @@ static void fillRegion(voxel::RawVolume &in, const voxel::Voxel &voxel, const vo
const int height = region.getHeightInVoxels();
const int depth = region.getDepthInVoxels();
const int size = width * height * depth;
const glm::ivec3 offset = -in.mins();
const glm::ivec3 mins = in.mins();
core::DynamicArray<glm::ivec3> positions;
positions.reserve(size);
core::Buffer<bool> visitedData(size);
@ -67,30 +67,30 @@ static void fillRegion(voxel::RawVolume &in, const voxel::Voxel &voxel, const vo
visitVolume(
in, region, 1, 1, 1,
[&](int x, int y, int z, const voxel::Voxel &) { visited.set(offset.x + x, offset.y + y, offset.z + z, true); },
[&](int x, int y, int z, const voxel::Voxel &) { visited.set(x - mins.x, y - mins.y, z - mins.z, true); },
SkipEmpty());
for (int x = 0; x < width; ++x) {
for (int z = 1; z < depth - 1; ++z) {
const glm::ivec3 v1(x, 0, z);
if (voxel::isAir(in.voxel(offset + v1).getMaterial())) {
if (voxel::isAir(in.voxel(v1 + mins).getMaterial())) {
positions.push_back(v1);
visited.set(v1, true);
}
const glm::ivec3 v2(x, height - 1, z);
if (voxel::isAir(in.voxel(offset + v2).getMaterial())) {
if (voxel::isAir(in.voxel(v2 + mins).getMaterial())) {
positions.push_back(v2);
visited.set(v2, true);
}
}
for (int y = 0; y < height; ++y) {
const glm::ivec3 v1(x, y, 0);
if (voxel::isAir(in.voxel(offset + v1).getMaterial())) {
if (voxel::isAir(in.voxel(v1 + mins).getMaterial())) {
positions.push_back(v1);
visited.set(v1, true);
}
const glm::ivec3 v2(x, y, depth - 1);
if (voxel::isAir(in.voxel(offset + v2).getMaterial())) {
if (voxel::isAir(in.voxel(v2 + mins).getMaterial())) {
positions.push_back(v2);
visited.set(v2, true);
}
@ -99,12 +99,12 @@ static void fillRegion(voxel::RawVolume &in, const voxel::Voxel &voxel, const vo
for (int y = 1; y < height - 1; ++y) {
for (int z = 1; z < depth - 1; ++z) {
const glm::ivec3 v1(0, y, z);
if (voxel::isAir(in.voxel(v1).getMaterial())) {
if (voxel::isAir(in.voxel(v1 + mins).getMaterial())) {
positions.push_back(v1);
visited.set(v1, true);
}
const glm::ivec3 v2(width - 1, y, z);
if (voxel::isAir(in.voxel(v2).getMaterial())) {
if (voxel::isAir(in.voxel(v2 + mins).getMaterial())) {
positions.push_back(v2);
visited.set(v2, true);
}
@ -150,7 +150,7 @@ static void fillRegion(voxel::RawVolume &in, const voxel::Voxel &voxel, const vo
visitVolume(
in, region, 1, 1, 1,
[&](int x, int y, int z, const voxel::Voxel &) {
if (!visited.get(offset.x + x, offset.y + y, offset.z + z)) {
if (!visited.get(x - mins.x, y - mins.y, z - mins.z)) {
in.setVoxel(x, y, z, voxel);
}
},

View File

@ -0,0 +1,61 @@
/**
* @file
*/
#include "voxelutil/VoxelUtil.h"
#include "app/tests/AbstractTest.h"
#include "voxel/RawVolume.h"
#include "voxel/RawVolumeWrapper.h"
#include "voxel/Region.h"
#include "voxel/Voxel.h"
#include "voxel/tests/TestHelper.h"
#include "voxelutil/VolumeVisitor.h"
namespace voxelutil {
class VoxelUtilTest : public app::AbstractTest {};
TEST_F(VoxelUtilTest, testFillHollow3x3Center) {
voxel::Region region(0, 2);
voxel::RawVolume v(region);
const voxel::Voxel borderVoxel = voxel::createVoxel(voxel::VoxelType::Generic, 1);
voxelutil::visitVolume(
v, [&](int x, int y, int z, const voxel::Voxel &) { EXPECT_TRUE(v.setVoxel(x, y, z, borderVoxel)); },
VisitAll());
EXPECT_TRUE(v.setVoxel(region.getCenter(), voxel::Voxel()));
const voxel::Voxel fillVoxel = voxel::createVoxel(voxel::VoxelType::Generic, 2);
voxelutil::fillHollow(v, fillVoxel);
EXPECT_EQ(2, v.voxel(region.getCenter()).getColor());
}
TEST_F(VoxelUtilTest, testFillHollow5x5CenterNegativeOrigin) {
voxel::Region region(-2, 2);
voxel::RawVolume v(region);
const voxel::Voxel borderVoxel = voxel::createVoxel(voxel::VoxelType::Generic, 1);
voxelutil::visitVolume(
v, [&](int x, int y, int z, const voxel::Voxel &) { EXPECT_TRUE(v.setVoxel(x, y, z, borderVoxel)); },
VisitAll());
EXPECT_TRUE(v.setVoxel(region.getCenter(), voxel::Voxel()));
const voxel::Voxel fillVoxel = voxel::createVoxel(voxel::VoxelType::Generic, 2);
voxelutil::fillHollow(v, fillVoxel);
EXPECT_EQ(2, v.voxel(region.getCenter()).getColor());
}
TEST_F(VoxelUtilTest, testFillHollowLeak) {
voxel::Region region(0, 2);
voxel::RawVolume v(region);
const voxel::Voxel borderVoxel = voxel::createVoxel(voxel::VoxelType::Generic, 1);
voxelutil::visitVolume(
v, [&](int x, int y, int z, const voxel::Voxel &) { EXPECT_TRUE(v.setVoxel(x, y, z, borderVoxel)); },
VisitAll());
EXPECT_TRUE(v.setVoxel(region.getCenter(), voxel::Voxel()));
EXPECT_TRUE(v.setVoxel(1, 1, 0, voxel::Voxel())); // produce leak
const voxel::Voxel fillVoxel = voxel::createVoxel(voxel::VoxelType::Generic, 2);
voxelutil::fillHollow(v, fillVoxel);
EXPECT_EQ(0, v.voxel(region.getCenter()).getColor());
}
} // namespace voxelutil