Using QtConcurrent::blockingMappedReduced to parallelize the computation of finding closest distance between two polygons.

This commit is contained in:
Roman Telezhynskyi 2024-04-08 11:55:59 +03:00
parent 0eef58f2cf
commit 1a6a830119

View File

@ -33,6 +33,7 @@
#include "../vlayout/vlayoutpiecepath.h" #include "../vlayout/vlayoutpiecepath.h"
#include "../vlayout/vtextmanager.h" #include "../vlayout/vtextmanager.h"
#include "../vwidgets/vpiecegrainline.h" #include "../vwidgets/vpiecegrainline.h"
#include "QtConcurrent/qtconcurrentmap.h"
#include "vplayout.h" #include "vplayout.h"
#include "vpsheet.h" #include "vpsheet.h"
@ -116,23 +117,35 @@ auto PrepareStickyPath(const QVector<QPointF> &path) -> QVector<QPointF>
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
auto ClosestDistance(const QVector<QPointF> &path1, const QVector<QPointF> &path2) -> QLineF auto ClosestDistance(const QVector<QPointF> &path1, const QVector<QPointF> &path2) -> QLineF
{ {
qreal distance = INT_MAX; return QtConcurrent::blockingMappedReduced(
QLineF closestDistance; path1,
[path2](const QPointF &p1)
for (auto p1 : path1)
{ {
for (auto p2 : path2) qreal minLocalDistance = std::numeric_limits<qreal>::max();
QLineF localClosestDistance;
for (const auto &p2 : path2)
{ {
QLineF const d(p1, p2); QLineF const d(p1, p2);
if (d.length() <= distance) qreal const length = d.length();
if (length < minLocalDistance)
{ {
distance = d.length(); minLocalDistance = length;
closestDistance = d; localClosestDistance = d;
}
} }
} }
return closestDistance; 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 } // namespace