From 0608e2af1c7f4be39d40d13bce473e922c0ea5a9 Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Wed, 17 Oct 2018 11:19:17 +0800 Subject: [PATCH] Fix uv unwrapper UV unwrapper may run into infinite loop if the parametrization failed with some infinite value. --- thirdparty/simpleuv/simpleuv/chartpacker.cpp | 4 ++++ thirdparty/simpleuv/simpleuv/chartpacker.h | 1 + thirdparty/simpleuv/simpleuv/uvunwrapper.cpp | 12 +++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/thirdparty/simpleuv/simpleuv/chartpacker.cpp b/thirdparty/simpleuv/simpleuv/chartpacker.cpp index a4519e5c..b1731cf8 100644 --- a/thirdparty/simpleuv/simpleuv/chartpacker.cpp +++ b/thirdparty/simpleuv/simpleuv/chartpacker.cpp @@ -89,6 +89,10 @@ void ChartPacker::pack() if (tryPack(textureSize)) break; m_textureSizeFactor += m_textureSizeGrowFactor; + if (m_tryNum >= m_maxTryNum) { + qDebug() << "Tried too many times:" << m_tryNum; + break; + } } } diff --git a/thirdparty/simpleuv/simpleuv/chartpacker.h b/thirdparty/simpleuv/simpleuv/chartpacker.h index 4c005246..86fb95df 100644 --- a/thirdparty/simpleuv/simpleuv/chartpacker.h +++ b/thirdparty/simpleuv/simpleuv/chartpacker.h @@ -24,6 +24,7 @@ private: float m_floatToIntFactor = 10000; size_t m_tryNum = 0; float m_textureSizeFactor = 1.0; + size_t m_maxTryNum = 10000; }; } diff --git a/thirdparty/simpleuv/simpleuv/uvunwrapper.cpp b/thirdparty/simpleuv/simpleuv/uvunwrapper.cpp index 4c4ed99e..874b080a 100644 --- a/thirdparty/simpleuv/simpleuv/uvunwrapper.cpp +++ b/thirdparty/simpleuv/simpleuv/uvunwrapper.cpp @@ -299,7 +299,8 @@ void UvUnwrapper::calculateSizeAndRemoveInvalidCharts() left = top = right = bottom = 0; calculateFaceTextureBoundingBox(chart.second, left, top, right, bottom); std::pair size = {right - left, bottom - top}; - if (size.first <= 0 || std::isnan(size.first) || size.second <= 0 || std::isnan(size.second)) { + if (size.first <= 0 || std::isnan(size.first) || std::isinf(size.first) || + size.second <= 0 || std::isnan(size.second) || std::isinf(size.second)) { qDebug() << "Found invalid chart size:" << size.first << "x" << size.second; continue; } @@ -325,6 +326,15 @@ void UvUnwrapper::packCharts() for (decltype(m_charts.size()) i = 0; i < m_charts.size(); ++i) { const auto &chartSize = m_chartSizes[i]; auto &chart = m_charts[i]; + if (i >= packedResult.size()) { + for (auto &item: chart.second) { + for (int i = 0; i < 3; ++i) { + item.coords[i].uv[0] = 0; + item.coords[i].uv[1] = 0; + } + } + continue; + } const auto &result = packedResult[i]; auto &left = std::get<0>(result); auto &top = std::get<1>(result);