Refactoring. Move best square result in separate struct.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-03-27 09:31:02 +02:00
parent 63ea3bf2f9
commit 08415eb4ef
6 changed files with 95 additions and 70 deletions

View File

@ -34,14 +34,14 @@
namespace
{
//---------------------------------------------------------------------------------------------------------------------
Q_DECL_CONSTEXPR inline qint64 Square(const QSizeF &size)
Q_DECL_CONSTEXPR inline qint64 Square(QSizeF size)
{
return static_cast<qint64>(size.width()*size.height());
}
} // anonymous namespace
//---------------------------------------------------------------------------------------------------------------------
VBestSquare::VBestSquare(const QSizeF &sheetSize, bool saveLength)
VBestSquare::VBestSquare(QSizeF sheetSize, bool saveLength)
: d(new VBestSquareData(sheetSize, saveLength))
{}
@ -55,43 +55,39 @@ VBestSquare::~VBestSquare()
{}
//---------------------------------------------------------------------------------------------------------------------
void VBestSquare::NewResult(const QSizeF &candidate, int i, int j, const QTransform &matrix, bool mirror,
qreal position, BestFrom type)
void VBestSquare::NewResult(const VBestSquareResData &data)
{
auto SaveResult = [this, candidate, i, j, matrix, mirror, type, position]()
auto SaveResult = [this, data]()
{
d->bestSize = candidate;
d->resI = i;
d->resJ = j;
d->resMatrix = matrix;
d->valideResult = true;
d->resMirror = mirror;
d->type = type;
d->position = position;
d->data = data;
};
if (d->saveLength)
{
const bool isPortrait = d->sheetSize.height() >= d->sheetSize.width();
const QSizeF saveSpaceSize = isPortrait ? QSizeF(d->sheetSize.width(), candidate.height()) :
QSizeF(candidate.width(), d->sheetSize.height());
const qint64 candidateSquare = Square(data.bestSize);
const QSizeF saveSpaceBestSize = isPortrait ? QSizeF(d->sheetSize.width(), d->bestSize.height()) :
QSizeF(d->bestSize.width(), d->sheetSize.height());
if (Square(saveSpaceSize) <= Square(saveSpaceBestSize) && Square(candidate) <= Square(d->bestSize)
&& position <= d->position && Square(saveSpaceSize) > 0 && Square(saveSpaceBestSize) > 0
&& type >= d->type)
{
SaveResult();
}
}
else
if (candidateSquare > 0)
{
if (Square(candidate) <= Square(d->bestSize) && Square(candidate) > 0 && position <= d->position
&& type >= d->type)
if (data.type >= d->data.type && candidateSquare <= Square(d->data.bestSize)
&& data.depthPosition <= d->data.depthPosition)
{
SaveResult();
if (d->saveLength)
{
const QSizeF saveSpaceSize = IsPortrait() ? QSizeF(d->sheetSize.width(), data.bestSize.height()) :
QSizeF(data.bestSize.width(), d->sheetSize.height());
const QSizeF saveSpaceBestSize = IsPortrait() ? QSizeF(d->sheetSize.width(), d->data.bestSize.height()):
QSizeF(d->data.bestSize.width(), d->sheetSize.height());
if (Square(saveSpaceBestSize) > 0 && Square(saveSpaceSize) > 0
&& Square(saveSpaceSize) <= Square(saveSpaceBestSize))
{
SaveResult();
}
}
else
{
SaveResult();
}
}
}
}
@ -99,39 +95,38 @@ void VBestSquare::NewResult(const QSizeF &candidate, int i, int j, const QTransf
//---------------------------------------------------------------------------------------------------------------------
void VBestSquare::NewResult(const VBestSquare &best)
{
if (best.IsValidResult() && d->saveLength == best.IsSaveLength())
if (best.HasValidResult() && d->saveLength == best.IsSaveLength())
{
NewResult(best.BestSize(), best.GContourEdge(), best.DetailEdge(), best.Matrix(), best.Mirror(),
best.Position(), best.Type());
NewResult(best.BestResultData());
}
}
//---------------------------------------------------------------------------------------------------------------------
inline QSizeF VBestSquare::BestSize() const
QSizeF VBestSquare::BestSize() const
{
return d->bestSize;
return d->data.bestSize;
}
//---------------------------------------------------------------------------------------------------------------------
inline int VBestSquare::GContourEdge() const
int VBestSquare::GContourEdge() const
{
return d->resI;
return d->data.globalI;
}
//---------------------------------------------------------------------------------------------------------------------
int VBestSquare::DetailEdge() const
{
return d->resJ;
return d->data.detJ;
}
//---------------------------------------------------------------------------------------------------------------------
QTransform VBestSquare::Matrix() const
{
return d->resMatrix;
return d->data.resMatrix;
}
//---------------------------------------------------------------------------------------------------------------------
bool VBestSquare::IsValidResult() const
bool VBestSquare::HasValidResult() const
{
return d->valideResult;
}
@ -139,19 +134,25 @@ bool VBestSquare::IsValidResult() const
//---------------------------------------------------------------------------------------------------------------------
bool VBestSquare::Mirror() const
{
return d->resMirror;
return d->data.resMirror;
}
//---------------------------------------------------------------------------------------------------------------------
BestFrom VBestSquare::Type() const
{
return d->type;
return d->data.type;
}
//---------------------------------------------------------------------------------------------------------------------
qreal VBestSquare::Position() const
{
return d->position;
return d->depthPosition;
}
//---------------------------------------------------------------------------------------------------------------------
VBestSquareResData VBestSquare::BestResultData() const
{
return d->data;
}
//---------------------------------------------------------------------------------------------------------------------
@ -159,3 +160,9 @@ bool VBestSquare::IsSaveLength() const
{
return d->saveLength;
}
//---------------------------------------------------------------------------------------------------------------------
bool VBestSquare::IsPortrait() const
{
return d->sheetSize.height() >= d->sheetSize.width();
}

View File

@ -42,7 +42,7 @@ class VBestSquareData;
class VBestSquare
{
public:
VBestSquare(const QSizeF &sheetSize, bool saveLength);
VBestSquare(QSizeF sheetSize, bool saveLength);
VBestSquare(const VBestSquare &res);
virtual ~VBestSquare();
@ -53,20 +53,22 @@ public:
inline void Swap(VBestSquare &res) Q_DECL_NOTHROW
{ std::swap(d, res.d); }
void NewResult(const QSizeF &candidate, int i, int j, const QTransform &matrix, bool mirror, qreal position,
BestFrom type);
void NewResult(const VBestSquareResData &data);
void NewResult(const VBestSquare &best);
QSizeF BestSize() const;
int GContourEdge() const;
int DetailEdge() const;
QTransform Matrix() const;
bool IsValidResult() const;
bool HasValidResult() const;
bool Mirror() const;
BestFrom Type() const;
qreal Position() const;
VBestSquareResData BestResultData() const;
bool IsSaveLength() const;
bool IsPortrait() const;
private:
QSharedDataPointer<VBestSquareData> d;

View File

@ -42,37 +42,28 @@ class VBestSquareData : public QSharedData
{
public:
VBestSquareData(const QSizeF &sheetSize, bool saveLength)
: bestSize(QSizeF(sheetSize.width()+10, sheetSize.height()+10)),
sheetSize(sheetSize),
: sheetSize(sheetSize),
saveLength(saveLength)
{}
{
data.bestSize = QSizeF(sheetSize.width()+10, sheetSize.height()+10);
}
VBestSquareData(const VBestSquareData &res)
: QSharedData(res),
resI(res.resI),
resJ(res.resJ),
resMatrix(res.resMatrix),
bestSize(res.bestSize),
sheetSize(res.sheetSize),
valideResult(res.valideResult),
resMirror(res.resMirror),
type(res.type),
saveLength(res.saveLength),
position(res.position)
depthPosition(res.depthPosition),
data(res.data)
{}
~VBestSquareData() {}
int resI{0}; // Edge of global contour
int resJ{0}; // Edge of detail
QTransform resMatrix{}; // Matrix for rotation and translation detail
QSizeF bestSize;
QSizeF sheetSize;
bool valideResult{false};
bool resMirror{false};
BestFrom type{BestFrom::Rotation};
bool saveLength;
qreal position{INT_MAX};
qreal depthPosition{INT_MAX};
VBestSquareResData data{};
private:
VBestSquareData &operator=(const VBestSquareData &) Q_DECL_EQ_DELETE;

View File

@ -29,6 +29,8 @@
#ifndef VLAYOUTDEF_H
#define VLAYOUTDEF_H
#include <QSize>
#include <QTransform>
#include <ciso646>
#include "../vmisc/typedef.h"
@ -47,6 +49,21 @@ enum class BestFrom : char
Combine = 1
};
struct VBestSquareResData
{
QSizeF bestSize{INT_MAX, INT_MAX};
// cppcheck-suppress unusedStructMember
int globalI{0}; // Edge of global contour
// cppcheck-suppress unusedStructMember
int detJ{0}; // Edge of detail
QTransform resMatrix{}; // Matrix for rotation and translation detail
// cppcheck-suppress unusedStructMember
bool resMirror{false};
BestFrom type{BestFrom::Rotation};
// cppcheck-suppress unusedStructMember
qreal depthPosition{INT_MAX};
};
/* Warning! Debugging doesn't work stable in debug mode. If you need big allocation use release mode. Or disable
* Address Sanitizer. See page https://bitbucket.org/dismine/valentina/wiki/developers/Address_Sanitizer
*/

View File

@ -301,7 +301,7 @@ bool VLayoutPaper::AddToSheet(const VLayoutPiece &detail, std::atomic_bool &stop
//---------------------------------------------------------------------------------------------------------------------
bool VLayoutPaper::SaveResult(const VBestSquare &bestResult, const VLayoutPiece &detail)
{
if (bestResult.IsValidResult())
if (bestResult.HasValidResult())
{
VLayoutPiece workDetail = detail;
workDetail.SetMatrix(bestResult.Matrix());// Don't forget set matrix
@ -329,7 +329,7 @@ bool VLayoutPaper::SaveResult(const VBestSquare &bestResult, const VLayoutPiece
#endif
}
return bestResult.IsValidResult(); // Do we have the best result?
return bestResult.HasValidResult(); // Do we have the best result?
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -315,11 +315,19 @@ void VPosition::SaveCandidate(VBestSquare &bestResult, const VLayoutPiece &detai
QVector<QPointF> newGContour = m_data.gContour.UniteWithContour(detail, globalI, detJ, type);
newGContour.append(newGContour.first());
const QSizeF size = QPolygonF(newGContour).boundingRect().size();
const qreal depthPosition = m_data.gContour.IsPortrait() ? detail.DetailBoundingRect().y() :
detail.DetailBoundingRect().x();
const qreal position = m_data.gContour.IsPortrait() ? detail.DetailBoundingRect().y() :
detail.DetailBoundingRect().x();
VBestSquareResData data;
data.bestSize = size;
data.globalI = globalI; // Edge of global contour
data.detJ = detJ; // Edge of detail
data.resMatrix = detail.GetMatrix(); // Matrix for rotation and translation detail
data.resMirror = detail.IsMirror();
data.type = type;
data.depthPosition = depthPosition;
bestResult.NewResult(size, globalI, detJ, detail.GetMatrix(), detail.IsMirror(), position, type);
bestResult.NewResult(data);
}
//---------------------------------------------------------------------------------------------------------------------