From 80d55659ab91aff9b3f7e2891f486feb5b2980f8 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 18 Jun 2019 15:10:08 +0300 Subject: [PATCH] Refactoring. --HG-- branch : develop --- src/libs/vlayout/vlayoutpaper.cpp | 78 +++++-------------------------- src/libs/vlayout/vlayoutpaper.h | 2 - src/libs/vlayout/vposition.cpp | 73 +++++++++++++++++++++++++++++ src/libs/vlayout/vposition.h | 2 + 4 files changed, 86 insertions(+), 69 deletions(-) diff --git a/src/libs/vlayout/vlayoutpaper.cpp b/src/libs/vlayout/vlayoutpaper.cpp index 7d02f462f..120ef8d3c 100644 --- a/src/libs/vlayout/vlayoutpaper.cpp +++ b/src/libs/vlayout/vlayoutpaper.cpp @@ -207,12 +207,6 @@ void VLayoutPaper::SetOriginPaperPortrait(bool portrait) //--------------------------------------------------------------------------------------------------------------------- bool VLayoutPaper::ArrangeDetail(const VLayoutPiece &detail, std::atomic_bool &stop) { - // First need set size of paper - if (d->globalContour.GetHeight() <= 0 || d->globalContour.GetWidth() <= 0) - { - return false; - } - if (detail.LayoutEdgesCount() < 3 || detail.DetailEdgesCount() < 3) { return false;//Not enough edges @@ -229,7 +223,17 @@ bool VLayoutPaper::ArrangeDetail(const VLayoutPiece &detail, std::atomic_bool &s d->localRotationNumber = d->globalRotationNumber; } - return AddToSheet(detail, stop); + VPositionData data; + data.gContour = d->globalContour; + data.detail = detail; + data.rotate = d->localRotate; + data.rotationNumber = d->localRotationNumber; + data.followGrainline = d->followGrainline; + data.positionsCache = d->positionsCache; + data.isOriginPaperOrientationPortrait = d->originPaperOrientation; + + const VBestSquare result = VPosition::ArrangeDetail(data, &stop, d->saveLength); + return SaveResult(result, detail); } //--------------------------------------------------------------------------------------------------------------------- @@ -238,66 +242,6 @@ int VLayoutPaper::Count() const return d->details.count(); } -//--------------------------------------------------------------------------------------------------------------------- -bool VLayoutPaper::AddToSheet(const VLayoutPiece &detail, std::atomic_bool &stop) -{ - VBestSquare bestResult(d->globalContour.GetSize(), d->saveLength, d->originPaperOrientation); - QThreadPool *thread_pool = QThreadPool::globalInstance(); - thread_pool->setExpiryTimeout(1000); - QVector threads; - - int detailEdgesCount = detail.LayoutEdgesCount(); - - for (int j=1; j <= d->globalContour.GlobalEdgesCount(); ++j) - { - QCoreApplication::processEvents(); - - for (int i=1; i<= detailEdgesCount; ++i) - { - VPositionData data; - data.gContour = d->globalContour; - data.detail = detail; - data.i = i; - data.j = j; - data.rotate = d->localRotate; - data.rotationNumber = d->localRotationNumber; - data.followGrainline = d->followGrainline; - data.positionsCache = d->positionsCache; - data.isOriginPaperOrientationPortrait = d->originPaperOrientation; - - auto *thread = new VPosition(data, &stop, d->saveLength); - thread->setAutoDelete(false); - threads.append(thread); - thread_pool->start(thread); - } - } - - // Wait for done - do - { - QCoreApplication::processEvents(); - QThread::msleep(250); - } - while(thread_pool->activeThreadCount() > 0 && not stop.load()); - - if (stop.load()) - { - qDeleteAll(threads.begin(), threads.end()); - threads.clear(); - return false; - } - - for (auto thread : threads) - { - bestResult.NewResult(thread->getBestResult()); - } - - qDeleteAll(threads.begin(), threads.end()); - threads.clear(); - - return SaveResult(bestResult, detail); -} - //--------------------------------------------------------------------------------------------------------------------- bool VLayoutPaper::SaveResult(const VBestSquare &bestResult, const VLayoutPiece &detail) { diff --git a/src/libs/vlayout/vlayoutpaper.h b/src/libs/vlayout/vlayoutpaper.h index 071b9dc56..dc0cb6751 100644 --- a/src/libs/vlayout/vlayoutpaper.h +++ b/src/libs/vlayout/vlayoutpaper.h @@ -109,8 +109,6 @@ public: private: QSharedDataPointer d; - bool AddToSheet(const VLayoutPiece &detail, std::atomic_bool &stop); - bool SaveResult(const VBestSquare &bestResult, const VLayoutPiece &detail); }; diff --git a/src/libs/vlayout/vposition.cpp b/src/libs/vlayout/vposition.cpp index 6312636d3..487b25931 100644 --- a/src/libs/vlayout/vposition.cpp +++ b/src/libs/vlayout/vposition.cpp @@ -44,11 +44,18 @@ #include #include #include +#include #include #include "../vmisc/def.h" #include "../vmisc/vmath.h" +#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0) +#include "../vmisc/backport/qscopeguard.h" +#else +#include +#endif + //--------------------------------------------------------------------------------------------------------------------- VPosition::VPosition(const VPositionData &data, std::atomic_bool *stop, bool saveLength) : QRunnable(), @@ -80,6 +87,72 @@ VBestSquare VPosition::getBestResult() const return m_bestResult; } +//--------------------------------------------------------------------------------------------------------------------- +VBestSquare VPosition::ArrangeDetail(const VPositionData &data, std::atomic_bool *stop, bool saveLength) +{ + VBestSquare bestResult(data.gContour.GetSize(), saveLength, data.isOriginPaperOrientationPortrait); + + if (stop->load()) + { + return bestResult; + } + + // First need set size of paper + if (data.gContour.GetHeight() <= 0 || data.gContour.GetWidth() <= 0) + { + return bestResult; + } + + const VLayoutPiece detail = data.detail; + const int detailEdgesCount = detail.LayoutEdgesCount(); + if (detailEdgesCount < 3 || detail.DetailEdgesCount() < 3) + { + return bestResult;//Not enough edges + } + + QScopedPointer thread_pool(new QThreadPool()); + QVector threads; + + auto Cleanup = qScopeGuard([threads] {qDeleteAll(threads.begin(), threads.end());}); + + for (int j=1; j <= data.gContour.GlobalEdgesCount(); ++j) + { + QCoreApplication::processEvents(); + + for (int i=1; i<= detailEdgesCount; ++i) + { + VPositionData linkedData = data; + linkedData.i = i; + linkedData.j = j; + + auto *thread = new VPosition(linkedData, stop, saveLength); + thread->setAutoDelete(false); + threads.append(thread); + thread_pool->start(thread); + } + } + + // Wait for done + do + { + QCoreApplication::processEvents(); + QThread::msleep(250); + } + while(thread_pool->activeThreadCount() > 0 && not stop->load()); + + if (stop->load()) + { + return bestResult; + } + + for (auto &thread : threads) + { + bestResult.NewResult(thread->getBestResult()); + } + + return bestResult; +} + //--------------------------------------------------------------------------------------------------------------------- void VPosition::SaveCandidate(VBestSquare &bestResult, const VLayoutPiece &detail, int globalI, int detJ, BestFrom type) diff --git a/src/libs/vlayout/vposition.h b/src/libs/vlayout/vposition.h index d476ecd1f..0daa7442e 100644 --- a/src/libs/vlayout/vposition.h +++ b/src/libs/vlayout/vposition.h @@ -63,6 +63,8 @@ public: VBestSquare getBestResult() const; + static VBestSquare ArrangeDetail(const VPositionData &data, std::atomic_bool *stop, bool saveLength); + private: Q_DISABLE_COPY(VPosition) VBestSquare m_bestResult;