Refactoring.
--HG-- branch : develop
This commit is contained in:
parent
d16b0d5501
commit
80d55659ab
|
@ -207,12 +207,6 @@ void VLayoutPaper::SetOriginPaperPortrait(bool portrait)
|
||||||
//---------------------------------------------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
bool VLayoutPaper::ArrangeDetail(const VLayoutPiece &detail, std::atomic_bool &stop)
|
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)
|
if (detail.LayoutEdgesCount() < 3 || detail.DetailEdgesCount() < 3)
|
||||||
{
|
{
|
||||||
return false;//Not enough edges
|
return false;//Not enough edges
|
||||||
|
@ -229,73 +223,23 @@ bool VLayoutPaper::ArrangeDetail(const VLayoutPiece &detail, std::atomic_bool &s
|
||||||
d->localRotationNumber = d->globalRotationNumber;
|
d->localRotationNumber = d->globalRotationNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
return AddToSheet(detail, stop);
|
|
||||||
}
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------------------------
|
|
||||||
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;
|
VPositionData data;
|
||||||
data.gContour = d->globalContour;
|
data.gContour = d->globalContour;
|
||||||
data.detail = detail;
|
data.detail = detail;
|
||||||
data.i = i;
|
|
||||||
data.j = j;
|
|
||||||
data.rotate = d->localRotate;
|
data.rotate = d->localRotate;
|
||||||
data.rotationNumber = d->localRotationNumber;
|
data.rotationNumber = d->localRotationNumber;
|
||||||
data.followGrainline = d->followGrainline;
|
data.followGrainline = d->followGrainline;
|
||||||
data.positionsCache = d->positionsCache;
|
data.positionsCache = d->positionsCache;
|
||||||
data.isOriginPaperOrientationPortrait = d->originPaperOrientation;
|
data.isOriginPaperOrientationPortrait = d->originPaperOrientation;
|
||||||
|
|
||||||
auto *thread = new VPosition(data, &stop, d->saveLength);
|
const VBestSquare result = VPosition::ArrangeDetail(data, &stop, d->saveLength);
|
||||||
thread->setAutoDelete(false);
|
return SaveResult(result, detail);
|
||||||
threads.append(thread);
|
}
|
||||||
thread_pool->start(thread);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for done
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
do
|
int VLayoutPaper::Count() const
|
||||||
{
|
{
|
||||||
QCoreApplication::processEvents();
|
return d->details.count();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
|
@ -109,8 +109,6 @@ public:
|
||||||
private:
|
private:
|
||||||
QSharedDataPointer<VLayoutPaperData> d;
|
QSharedDataPointer<VLayoutPaperData> d;
|
||||||
|
|
||||||
bool AddToSheet(const VLayoutPiece &detail, std::atomic_bool &stop);
|
|
||||||
|
|
||||||
bool SaveResult(const VBestSquare &bestResult, const VLayoutPiece &detail);
|
bool SaveResult(const VBestSquare &bestResult, const VLayoutPiece &detail);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -44,11 +44,18 @@
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringData>
|
#include <QStringData>
|
||||||
#include <QStringDataPtr>
|
#include <QStringDataPtr>
|
||||||
|
#include <QThreadPool>
|
||||||
#include <Qt>
|
#include <Qt>
|
||||||
|
|
||||||
#include "../vmisc/def.h"
|
#include "../vmisc/def.h"
|
||||||
#include "../vmisc/vmath.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)
|
VPosition::VPosition(const VPositionData &data, std::atomic_bool *stop, bool saveLength)
|
||||||
: QRunnable(),
|
: QRunnable(),
|
||||||
|
@ -80,6 +87,72 @@ VBestSquare VPosition::getBestResult() const
|
||||||
return m_bestResult;
|
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,
|
void VPosition::SaveCandidate(VBestSquare &bestResult, const VLayoutPiece &detail, int globalI, int detJ,
|
||||||
BestFrom type)
|
BestFrom type)
|
||||||
|
|
|
@ -63,6 +63,8 @@ public:
|
||||||
|
|
||||||
VBestSquare getBestResult() const;
|
VBestSquare getBestResult() const;
|
||||||
|
|
||||||
|
static VBestSquare ArrangeDetail(const VPositionData &data, std::atomic_bool *stop, bool saveLength);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Q_DISABLE_COPY(VPosition)
|
Q_DISABLE_COPY(VPosition)
|
||||||
VBestSquare m_bestResult;
|
VBestSquare m_bestResult;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user