From c10d2f545dc00640b94ab3e89721186da98ed0ca Mon Sep 17 00:00:00 2001 From: Desour Date: Wed, 17 Apr 2024 16:33:27 +0200 Subject: [PATCH 1/3] Fix inconsistent rounding in VoxelLineIterator::VoxelLineIterator floatToInt rounds 0.5 differently depending on sign. --- src/voxelalgorithms.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/voxelalgorithms.cpp b/src/voxelalgorithms.cpp index cfbd36243..bd155a7e7 100644 --- a/src/voxelalgorithms.cpp +++ b/src/voxelalgorithms.cpp @@ -1246,34 +1246,34 @@ VoxelLineIterator::VoxelLineIterator(const v3f &start_position, const v3f &line_ m_last_index = getIndex(floatToInt(start_position + line_vector, 1)); if (m_line_vector.X > 0) { - m_next_intersection_multi.X = (floorf(m_start_position.X - 0.5) + 1.5 + m_next_intersection_multi.X = (m_current_node_pos.X + 0.5f - m_start_position.X) / m_line_vector.X; m_intersection_multi_inc.X = 1 / m_line_vector.X; } else if (m_line_vector.X < 0) { - m_next_intersection_multi.X = (floorf(m_start_position.X - 0.5) - - m_start_position.X + 0.5) / m_line_vector.X; + m_next_intersection_multi.X = (m_current_node_pos.X - 0.5f + - m_start_position.X) / m_line_vector.X; m_intersection_multi_inc.X = -1 / m_line_vector.X; m_step_directions.X = -1; } if (m_line_vector.Y > 0) { - m_next_intersection_multi.Y = (floorf(m_start_position.Y - 0.5) + 1.5 + m_next_intersection_multi.Y = (m_current_node_pos.Y + 0.5f - m_start_position.Y) / m_line_vector.Y; m_intersection_multi_inc.Y = 1 / m_line_vector.Y; } else if (m_line_vector.Y < 0) { - m_next_intersection_multi.Y = (floorf(m_start_position.Y - 0.5) - - m_start_position.Y + 0.5) / m_line_vector.Y; + m_next_intersection_multi.Y = (m_current_node_pos.Y - 0.5f + - m_start_position.Y) / m_line_vector.Y; m_intersection_multi_inc.Y = -1 / m_line_vector.Y; m_step_directions.Y = -1; } if (m_line_vector.Z > 0) { - m_next_intersection_multi.Z = (floorf(m_start_position.Z - 0.5) + 1.5 + m_next_intersection_multi.Z = (m_current_node_pos.Z + 0.5f - m_start_position.Z) / m_line_vector.Z; m_intersection_multi_inc.Z = 1 / m_line_vector.Z; } else if (m_line_vector.Z < 0) { - m_next_intersection_multi.Z = (floorf(m_start_position.Z - 0.5) - - m_start_position.Z + 0.5) / m_line_vector.Z; + m_next_intersection_multi.Z = (m_current_node_pos.Z - 0.5f + - m_start_position.Z) / m_line_vector.Z; m_intersection_multi_inc.Z = -1 / m_line_vector.Z; m_step_directions.Z = -1; } From 89b80295f341e13cf2f8a768780fec0c94dc74f8 Mon Sep 17 00:00:00 2001 From: Desour Date: Thu, 18 Apr 2024 17:25:31 +0200 Subject: [PATCH 2/3] update testVoxelLineIterator unit test --- src/unittest/test_voxelalgorithms.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/unittest/test_voxelalgorithms.cpp b/src/unittest/test_voxelalgorithms.cpp index 8caa0fbf4..706a6bcce 100644 --- a/src/unittest/test_voxelalgorithms.cpp +++ b/src/unittest/test_voxelalgorithms.cpp @@ -49,22 +49,20 @@ void TestVoxelAlgorithms::runTests(IGameDef *gamedef) void TestVoxelAlgorithms::testVoxelLineIterator() { // Test some lines - // Do not test lines that start or end on the border of - // two voxels as rounding errors can make the test fail! - std::vector > lines; + std::vector> lines; for (f32 x = -9.1; x < 9; x += 3.124) { for (f32 y = -9.2; y < 9; y += 3.123) { for (f32 z = -9.3; z < 9; z += 3.122) { lines.emplace_back(-x, -y, -z, x, y, z); - } - } - } + }}} + for (f32 x = -3.0; x < 3.1; x += 0.5) { + for (f32 y = -3.0; y < 3.1; y += 0.5) { + for (f32 z = -3.0; z < 3.1; z += 0.5) { + lines.emplace_back(-x, -y, -z, x, y, z); + }}} lines.emplace_back(0, 0, 0, 0, 0, 0); // Test every line - std::vector >::iterator it = lines.begin(); - for (; it < lines.end(); it++) { - core::line3d l = *it; - + for (auto l : lines) { // Initialize test voxalgo::VoxelLineIterator iterator(l.start, l.getVector()); @@ -88,8 +86,9 @@ void TestVoxelAlgorithms::testVoxelLineIterator() UASSERTEQ(f32, (new_voxel - old_voxel).getLengthSQ(), 1); // The line must intersect with the voxel v3f voxel_center = intToFloat(iterator.m_current_node_pos, 1); - aabb3f box(voxel_center - v3f(0.5, 0.5, 0.5), - voxel_center + v3f(0.5, 0.5, 0.5)); + constexpr f32 eps = 1.0e-5; + aabb3f box(voxel_center - v3f(0.5 + eps), + voxel_center + v3f(0.5 + eps)); UASSERT(box.intersectsWithLine(l)); // Update old voxel old_voxel = new_voxel; From d6b19ac9a1ef6ffd9f754a0c6d38c11dbc052df7 Mon Sep 17 00:00:00 2001 From: Desour Date: Fri, 19 Apr 2024 19:24:52 +0200 Subject: [PATCH 3/3] code style --- src/unittest/test_voxelalgorithms.cpp | 28 +++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/unittest/test_voxelalgorithms.cpp b/src/unittest/test_voxelalgorithms.cpp index 706a6bcce..366755da5 100644 --- a/src/unittest/test_voxelalgorithms.cpp +++ b/src/unittest/test_voxelalgorithms.cpp @@ -50,28 +50,28 @@ void TestVoxelAlgorithms::testVoxelLineIterator() { // Test some lines std::vector> lines; - for (f32 x = -9.1; x < 9; x += 3.124) { - for (f32 y = -9.2; y < 9; y += 3.123) { - for (f32 z = -9.3; z < 9; z += 3.122) { + for (f32 x = -9.1f; x < 9.0f; x += 3.124f) + for (f32 y = -9.2f; y < 9.0f; y += 3.123f) + for (f32 z = -9.3f; z < 9.0f; z += 3.122f) { lines.emplace_back(-x, -y, -z, x, y, z); - }}} - for (f32 x = -3.0; x < 3.1; x += 0.5) { - for (f32 y = -3.0; y < 3.1; y += 0.5) { - for (f32 z = -3.0; z < 3.1; z += 0.5) { + } + for (f32 x = -3.0f; x < 3.1f; x += 0.5f) + for (f32 y = -3.0f; y < 3.1f; y += 0.5f) + for (f32 z = -3.0f; z < 3.1f; z += 0.5f) { lines.emplace_back(-x, -y, -z, x, y, z); - }}} - lines.emplace_back(0, 0, 0, 0, 0, 0); + } + lines.emplace_back(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); // Test every line for (auto l : lines) { // Initialize test voxalgo::VoxelLineIterator iterator(l.start, l.getVector()); //Test the first voxel - v3s16 start_voxel = floatToInt(l.start, 1); + v3s16 start_voxel = floatToInt(l.start, 1.0f); UASSERT(iterator.m_current_node_pos == start_voxel); // Values for testing - v3s16 end_voxel = floatToInt(l.end, 1); + v3s16 end_voxel = floatToInt(l.end, 1.0f); v3s16 voxel_vector = end_voxel - start_voxel; int nodecount = abs(voxel_vector.X) + abs(voxel_vector.Y) + abs(voxel_vector.Z); @@ -86,9 +86,9 @@ void TestVoxelAlgorithms::testVoxelLineIterator() UASSERTEQ(f32, (new_voxel - old_voxel).getLengthSQ(), 1); // The line must intersect with the voxel v3f voxel_center = intToFloat(iterator.m_current_node_pos, 1); - constexpr f32 eps = 1.0e-5; - aabb3f box(voxel_center - v3f(0.5 + eps), - voxel_center + v3f(0.5 + eps)); + constexpr f32 eps = 1.0e-5f; + aabb3f box(voxel_center - v3f(0.5f + eps), + voxel_center + v3f(0.5f + eps)); UASSERT(box.intersectsWithLine(l)); // Update old voxel old_voxel = new_voxel;