Implement "pull left" behavior.

Between valid best results select most top or most left for portrait and
landscape orientation.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2018-12-31 13:35:42 +02:00
parent da0ee5ce6f
commit 3dfa269224
3 changed files with 104 additions and 82 deletions

View File

@ -30,16 +30,45 @@
#include <QMatrix> #include <QMatrix>
namespace
{
//---------------------------------------------------------------------------------------------------------------------
qint64 Square(const QSizeF &size)
{
return static_cast<qint64>(size.width()*size.height());
}
} // anonymous namespace
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VBestSquare::VBestSquare(const QSizeF &sheetSize, bool saveLength) VBestSquare::VBestSquare(const QSizeF &sheetSize, bool saveLength)
:resI(0), resJ(0), resMatrix(QMatrix()), bestSize(QSizeF(sheetSize.width()+10, sheetSize.height()+10)), : resI(0),
sheetSize(sheetSize), valideResult(false), resMirror(false), type (BestFrom::Rotation), resJ(0),
saveLength(saveLength) 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) if (saveLength)
{ {
const bool isPortrait = sheetSize.height() >= sheetSize.width(); 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()); QSizeF(bestSize.width(), sheetSize.height());
if (Square(saveSpaceSize) <= Square(saveSpaceBestSize) && Square(candidate) <= Square(bestSize) 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; SaveResult();
}
else
{
return;
} }
} }
else 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; SaveResult();
}
else
{
return;
} }
} }
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()) 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<qint64>(size.width()*size.height());
}

View File

@ -40,7 +40,8 @@ class VBestSquare
public: public:
VBestSquare(const QSizeF &sheetSize, bool saveLength); 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); void NewResult(const VBestSquare &best);
QSizeF BestSize() const; QSizeF BestSize() const;
@ -50,6 +51,7 @@ public:
bool ValidResult() const; bool ValidResult() const;
bool Mirror() const; bool Mirror() const;
BestFrom Type() const; BestFrom Type() const;
qreal Position() const;
bool IsSaveLength() const; bool IsSaveLength() const;
@ -64,8 +66,61 @@ private:
bool resMirror; bool resMirror;
BestFrom type; BestFrom type;
bool saveLength; bool saveLength;
qreal position;
static qint64 Square(const QSizeF &size);
}; };
//---------------------------------------------------------------------------------------------------------------------
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 #endif // VBESTSQUARE_H

View File

@ -342,7 +342,11 @@ void VPosition::SaveCandidate(VBestSquare &bestResult, const VLayoutPiece &detai
QVector<QPointF> newGContour = gContour.UniteWithContour(detail, globalI, detJ, type); QVector<QPointF> newGContour = gContour.UniteWithContour(detail, globalI, detJ, type);
newGContour.append(newGContour.first()); newGContour.append(newGContour.first());
const QSizeF size = QPolygonF(newGContour).boundingRect().size(); 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);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------