diff --git a/src/libs/vlayout/vbestsquare.cpp b/src/libs/vlayout/vbestsquare.cpp index 737d95ea8..1fedbaf26 100644 --- a/src/libs/vlayout/vbestsquare.cpp +++ b/src/libs/vlayout/vbestsquare.cpp @@ -30,16 +30,45 @@ #include +namespace +{ +//--------------------------------------------------------------------------------------------------------------------- +qint64 Square(const QSizeF &size) +{ + return static_cast(size.width()*size.height()); +} +} // anonymous namespace + //--------------------------------------------------------------------------------------------------------------------- VBestSquare::VBestSquare(const QSizeF &sheetSize, bool saveLength) - :resI(0), resJ(0), resMatrix(QMatrix()), bestSize(QSizeF(sheetSize.width()+10, sheetSize.height()+10)), - sheetSize(sheetSize), valideResult(false), resMirror(false), type (BestFrom::Rotation), - saveLength(saveLength) + : resI(0), + resJ(0), + resMatrix(), + bestSize(QSizeF(sheetSize.width()+10, sheetSize.height()+10)), + sheetSize(sheetSize), + valideResult(false), + resMirror(false), + type(BestFrom::Rotation), + saveLength(saveLength), + position(INT_MAX) {} //--------------------------------------------------------------------------------------------------------------------- -void VBestSquare::NewResult(const QSizeF &candidate, int i, int j, const QTransform &matrix, bool mirror, BestFrom type) +void VBestSquare::NewResult(const QSizeF &candidate, int i, int j, const QTransform &matrix, bool mirror, + qreal position, BestFrom type) { + auto SaveResult = [this, candidate, i, j, matrix, mirror, type, position]() + { + bestSize = candidate; + resI = i; + resJ = j; + resMatrix = matrix; + valideResult = true; + resMirror = mirror; + this->type = type; + this->position = position; + }; + if (saveLength) { const bool isPortrait = sheetSize.height() >= sheetSize.width(); @@ -50,33 +79,20 @@ void VBestSquare::NewResult(const QSizeF &candidate, int i, int j, const QTransf QSizeF(bestSize.width(), sheetSize.height()); if (Square(saveSpaceSize) <= Square(saveSpaceBestSize) && Square(candidate) <= Square(bestSize) - && Square(saveSpaceSize) > 0 && Square(saveSpaceBestSize) > 0 && type >= this->type) + && position <= this->position && Square(saveSpaceSize) > 0 && Square(saveSpaceBestSize) > 0 + && type >= this->type) { - bestSize = candidate; - } - else - { - return; + SaveResult(); } } else { - if (Square(candidate) <= Square(bestSize) && Square(candidate) > 0 && type >= this->type) + if (Square(candidate) <= Square(bestSize) && Square(candidate) > 0 && position <= this->position + && type >= this->type) { - bestSize = candidate; - } - else - { - return; + SaveResult(); } } - - resI = i; - resJ = j; - resMatrix = matrix; - valideResult = true; - resMirror = mirror; - this->type = type; } //--------------------------------------------------------------------------------------------------------------------- @@ -84,60 +100,7 @@ void VBestSquare::NewResult(const VBestSquare &best) { if (best.ValidResult() && saveLength == best.IsSaveLength()) { - NewResult(best.BestSize(), best.GContourEdge(), best.DetailEdge(), best.Matrix(), best.Mirror(), best.Type()); + NewResult(best.BestSize(), best.GContourEdge(), best.DetailEdge(), best.Matrix(), best.Mirror(), + best.Position(), best.Type()); } } - -//--------------------------------------------------------------------------------------------------------------------- -QSizeF VBestSquare::BestSize() const -{ - return bestSize; -} - -//--------------------------------------------------------------------------------------------------------------------- -int VBestSquare::GContourEdge() const -{ - return resI; -} - -//--------------------------------------------------------------------------------------------------------------------- -int VBestSquare::DetailEdge() const -{ - return resJ; -} - -//--------------------------------------------------------------------------------------------------------------------- -QTransform VBestSquare::Matrix() const -{ - return resMatrix; -} - -//--------------------------------------------------------------------------------------------------------------------- -bool VBestSquare::ValidResult() const -{ - return valideResult; -} - -//--------------------------------------------------------------------------------------------------------------------- -bool VBestSquare::Mirror() const -{ - return resMirror; -} - -//--------------------------------------------------------------------------------------------------------------------- -BestFrom VBestSquare::Type() const -{ - return type; -} - -//--------------------------------------------------------------------------------------------------------------------- -bool VBestSquare::IsSaveLength() const -{ - return saveLength; -} - -//--------------------------------------------------------------------------------------------------------------------- -qint64 VBestSquare::Square(const QSizeF &size) -{ - return static_cast(size.width()*size.height()); -} diff --git a/src/libs/vlayout/vbestsquare.h b/src/libs/vlayout/vbestsquare.h index dba2e848f..1cb3b06e7 100644 --- a/src/libs/vlayout/vbestsquare.h +++ b/src/libs/vlayout/vbestsquare.h @@ -40,7 +40,8 @@ class VBestSquare public: VBestSquare(const QSizeF &sheetSize, bool saveLength); - void NewResult(const QSizeF &candidate, int i, int j, const QTransform &matrix, bool mirror, BestFrom type); + void NewResult(const QSizeF &candidate, int i, int j, const QTransform &matrix, bool mirror, qreal position, + BestFrom type); void NewResult(const VBestSquare &best); QSizeF BestSize() const; @@ -50,6 +51,7 @@ public: bool ValidResult() const; bool Mirror() const; BestFrom Type() const; + qreal Position() const; bool IsSaveLength() const; @@ -64,8 +66,61 @@ private: bool resMirror; BestFrom type; bool saveLength; - - static qint64 Square(const QSizeF &size); + qreal position; }; +//--------------------------------------------------------------------------------------------------------------------- +inline QSizeF VBestSquare::BestSize() const +{ + return bestSize; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline int VBestSquare::GContourEdge() const +{ + return resI; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline int VBestSquare::DetailEdge() const +{ + return resJ; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline QTransform VBestSquare::Matrix() const +{ + return resMatrix; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline bool VBestSquare::ValidResult() const +{ + return valideResult; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline bool VBestSquare::Mirror() const +{ + return resMirror; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline BestFrom VBestSquare::Type() const +{ + return type; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline qreal VBestSquare::Position() const +{ + return position; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline bool VBestSquare::IsSaveLength() const +{ + return saveLength; +} + #endif // VBESTSQUARE_H diff --git a/src/libs/vlayout/vposition.cpp b/src/libs/vlayout/vposition.cpp index d7926e41c..0b0edc5cd 100644 --- a/src/libs/vlayout/vposition.cpp +++ b/src/libs/vlayout/vposition.cpp @@ -342,7 +342,11 @@ void VPosition::SaveCandidate(VBestSquare &bestResult, const VLayoutPiece &detai QVector newGContour = gContour.UniteWithContour(detail, globalI, detJ, type); newGContour.append(newGContour.first()); const QSizeF size = QPolygonF(newGContour).boundingRect().size(); - bestResult.NewResult(size, globalI, detJ, detail.GetMatrix(), detail.IsMirror(), type); + + const bool isPortrait = gContour.GetSize().height() >= gContour.GetSize().width(); + const qreal position = isPortrait ? detail.DetailBoundingRect().y() : detail.DetailBoundingRect().x(); + + bestResult.NewResult(size, globalI, detJ, detail.GetMatrix(), detail.IsMirror(), position, type); } //---------------------------------------------------------------------------------------------------------------------