Refactoring.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-06-18 15:10:08 +03:00
parent d16b0d5501
commit 80d55659ab
4 changed files with 86 additions and 69 deletions

View File

@ -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<VPosition *> 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)
{

View File

@ -109,8 +109,6 @@ public:
private:
QSharedDataPointer<VLayoutPaperData> d;
bool AddToSheet(const VLayoutPiece &detail, std::atomic_bool &stop);
bool SaveResult(const VBestSquare &bestResult, const VLayoutPiece &detail);
};

View File

@ -44,11 +44,18 @@
#include <QString>
#include <QStringData>
#include <QStringDataPtr>
#include <QThreadPool>
#include <Qt>
#include "../vmisc/def.h"
#include "../vmisc/vmath.h"
#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
#include "../vmisc/backport/qscopeguard.h"
#else
#include <QScopeGuard>
#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<QThreadPool> thread_pool(new QThreadPool());
QVector<VPosition *> 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)

View File

@ -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;