From 1a6a8301195e1f2d5336018b1ebb9121f02fd7be Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 8 Apr 2024 11:55:59 +0300 Subject: [PATCH] Using QtConcurrent::blockingMappedReduced to parallelize the computation of finding closest distance between two polygons. --- src/app/puzzle/layout/vppiece.cpp | 43 ++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/app/puzzle/layout/vppiece.cpp b/src/app/puzzle/layout/vppiece.cpp index a31e0ce8f..8adbecf73 100644 --- a/src/app/puzzle/layout/vppiece.cpp +++ b/src/app/puzzle/layout/vppiece.cpp @@ -33,6 +33,7 @@ #include "../vlayout/vlayoutpiecepath.h" #include "../vlayout/vtextmanager.h" #include "../vwidgets/vpiecegrainline.h" +#include "QtConcurrent/qtconcurrentmap.h" #include "vplayout.h" #include "vpsheet.h" @@ -116,23 +117,35 @@ auto PrepareStickyPath(const QVector &path) -> QVector //--------------------------------------------------------------------------------------------------------------------- auto ClosestDistance(const QVector &path1, const QVector &path2) -> QLineF { - qreal distance = INT_MAX; - QLineF closestDistance; - - for (auto p1 : path1) - { - for (auto p2 : path2) + return QtConcurrent::blockingMappedReduced( + path1, + [path2](const QPointF &p1) { - QLineF const d(p1, p2); - if (d.length() <= distance) - { - distance = d.length(); - closestDistance = d; - } - } - } + qreal minLocalDistance = std::numeric_limits::max(); + QLineF localClosestDistance; - return closestDistance; + for (const auto &p2 : path2) + { + QLineF const d(p1, p2); + qreal const length = d.length(); + if (length < minLocalDistance) + { + minLocalDistance = length; + localClosestDistance = d; + } + } + + return localClosestDistance; + }, + [](QLineF &result, const QLineF &next) + { + qreal const dist1 = result.length(); + qreal const dist2 = next.length(); + if (result.isNull() || dist2 < dist1) + { + result = next; + } + }); } } // namespace