From a34eee754c26b543c53895e507eb34747308ddf0 Mon Sep 17 00:00:00 2001 From: codereader Date: Wed, 22 Sep 2021 20:59:01 +0200 Subject: [PATCH] #5746: Adjust unit test assertions --- test/TextureTool.cpp | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/test/TextureTool.cpp b/test/TextureTool.cpp index 864e946e36..ec887a4680 100644 --- a/test/TextureTool.cpp +++ b/test/TextureTool.cpp @@ -999,6 +999,27 @@ inline void assertAllCoordsMovedBySameAmount(IFace&, const std::vector& } } +inline std::size_t getFarthestIndex(const std::vector& texcoords, const Vector2& point, std::vector fixedIndices) +{ + std::size_t farthestIndex = 1; + double largestDistance = 0; + + for (std::size_t i = 0; i < texcoords.size(); ++i) + { + if (std::find(fixedIndices.begin(), fixedIndices.end(), i) != fixedIndices.end()) continue; + + auto candidateDistance = (texcoords[i] - point).getLengthSquared(); + + if (candidateDistance > largestDistance) + { + farthestIndex = i; + largestDistance = candidateDistance; + } + } + + return farthestIndex; +} + // When manipulating one vertex, the "opposite" vertex should remain the same as it is chosen as fixed point TEST_F(TextureToolTest, DragManipulateSingleFaceVertex) { @@ -1006,26 +1027,21 @@ TEST_F(TextureToolTest, DragManipulateSingleFaceVertex) const std::vector& oldTexcoords, const std::vector& changedTexcoords) { // Find out which face vertex was the farthest away from the modified one - int farthestIndex = 1; - double largestDistance = 0; - for (int i = 1; i < oldTexcoords.size(); ++i) - { - auto candidateDistance = (oldTexcoords[i] - oldTexcoords[0]).getLengthSquared(); - if (candidateDistance > largestDistance) - { - farthestIndex = i; - largestDistance = candidateDistance; - } - } + auto farthestIndex = getFarthestIndex(oldTexcoords, oldTexcoords[0], { 0 }); // The farthest vertex should remain unchanged EXPECT_NEAR(oldTexcoords[farthestIndex].x(), changedTexcoords[farthestIndex].x(), 0.01) << "Opposite vertex X should remain unchanged"; EXPECT_NEAR(oldTexcoords[farthestIndex].y(), changedTexcoords[farthestIndex].y(), 0.01) << "Opposite vertex Y should remain unchanged"; + // The algorithm will pick a third vertex that should remain unchanged + // it will be the farthest from the center of the first two vertices + auto center = (oldTexcoords[farthestIndex] + oldTexcoords[0]) * 0.5; + auto thirdIndex = getFarthestIndex(oldTexcoords, center, { 0, farthestIndex }); + // All the others will have changed in some way for (int i = 0; i < oldTexcoords.size(); ++i) { - if (i == farthestIndex) continue; + if (i == farthestIndex || i == thirdIndex) continue; EXPECT_FALSE(float_equal_epsilon(oldTexcoords[i].x(), changedTexcoords[i].x(), 0.05)) << "Vertex " << i << " x should have changed"; EXPECT_FALSE(float_equal_epsilon(oldTexcoords[i].y(), changedTexcoords[i].y(), 0.05)) << "Vertex " << i << " y should have changed";;