From 08415eb4ef23b7a93c9b5051805b4cb014924494 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 27 Mar 2019 09:31:02 +0200 Subject: [PATCH] Refactoring. Move best square result in separate struct. --HG-- branch : develop --- src/libs/vlayout/vbestsquare.cpp | 95 +++++++++++++++++-------------- src/libs/vlayout/vbestsquare.h | 10 ++-- src/libs/vlayout/vbestsquare_p.h | 25 +++----- src/libs/vlayout/vlayoutdef.h | 17 ++++++ src/libs/vlayout/vlayoutpaper.cpp | 4 +- src/libs/vlayout/vposition.cpp | 14 ++++- 6 files changed, 95 insertions(+), 70 deletions(-) diff --git a/src/libs/vlayout/vbestsquare.cpp b/src/libs/vlayout/vbestsquare.cpp index 50e2922ed..a5131e843 100644 --- a/src/libs/vlayout/vbestsquare.cpp +++ b/src/libs/vlayout/vbestsquare.cpp @@ -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(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(); +} diff --git a/src/libs/vlayout/vbestsquare.h b/src/libs/vlayout/vbestsquare.h index f400fecf0..45677efee 100644 --- a/src/libs/vlayout/vbestsquare.h +++ b/src/libs/vlayout/vbestsquare.h @@ -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 d; diff --git a/src/libs/vlayout/vbestsquare_p.h b/src/libs/vlayout/vbestsquare_p.h index 05842566c..90548f327 100644 --- a/src/libs/vlayout/vbestsquare_p.h +++ b/src/libs/vlayout/vbestsquare_p.h @@ -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; diff --git a/src/libs/vlayout/vlayoutdef.h b/src/libs/vlayout/vlayoutdef.h index 13d0030f2..b93e8063d 100644 --- a/src/libs/vlayout/vlayoutdef.h +++ b/src/libs/vlayout/vlayoutdef.h @@ -29,6 +29,8 @@ #ifndef VLAYOUTDEF_H #define VLAYOUTDEF_H +#include +#include #include #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 */ diff --git a/src/libs/vlayout/vlayoutpaper.cpp b/src/libs/vlayout/vlayoutpaper.cpp index a7bf0fb87..64792ca56 100644 --- a/src/libs/vlayout/vlayoutpaper.cpp +++ b/src/libs/vlayout/vlayoutpaper.cpp @@ -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? } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vlayout/vposition.cpp b/src/libs/vlayout/vposition.cpp index a749e2116..608348615 100644 --- a/src/libs/vlayout/vposition.cpp +++ b/src/libs/vlayout/vposition.cpp @@ -315,11 +315,19 @@ void VPosition::SaveCandidate(VBestSquare &bestResult, const VLayoutPiece &detai QVector 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); } //---------------------------------------------------------------------------------------------------------------------