From 31a08cf573e37e519fc03deb536d11694ff10d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 31 Dec 2019 00:24:28 +0100 Subject: [PATCH] MeshTools: use a range-based minmax in removeDuplicates(). --- src/Magnum/MeshTools/RemoveDuplicates.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Magnum/MeshTools/RemoveDuplicates.h b/src/Magnum/MeshTools/RemoveDuplicates.h index 03bbcbcf8..2f5278c9e 100644 --- a/src/Magnum/MeshTools/RemoveDuplicates.h +++ b/src/Magnum/MeshTools/RemoveDuplicates.h @@ -33,10 +33,11 @@ #include #include #include +#include #include #include "Magnum/Magnum.h" -#include "Magnum/Math/Functions.h" +#include "Magnum/Math/FunctionsBatch.h" namespace Magnum { namespace MeshTools { @@ -76,16 +77,14 @@ data accordingly: @snippet MagnumMeshTools.cpp removeDuplicates2 */ template std::vector removeDuplicates(std::vector& data, typename Vector::Type epsilon = Math::TypeTraits::epsilon()) { - /* Get bounds */ - Vector min = data[0], max = data[0]; - for(const auto& v: data) { - min = Math::min(v, min); - max = Math::max(v, max); - } + /* Get bounds. When NaNs appear, those will get collapsed together when + you're lucky, or cause the whole data to disappear when you're not -- it + needs a much more specialized handling to be robust. */ + std::pair minmax = Math::minmax(data); /* Make epsilon so large that std::size_t can index all vectors inside the bounds. */ - epsilon = Math::max(epsilon, typename Vector::Type((max-min).max()/~std::size_t{})); + epsilon = Math::max(epsilon, typename Vector::Type((minmax.second-minmax.first).max()/~std::size_t{})); /* Resulting index array */ std::vector resultIndices(data.size()); @@ -107,7 +106,7 @@ template std::vector removeDuplicates(std::vector v((data[i] + moved - min)/epsilon); + const Math::Vector v((data[i] + moved - minmax.first)/epsilon); const auto result = table.emplace(v, table.size()); /* Add the (either new or already existing) index to index array */