Convert to implicitly shared object.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-03-26 19:02:12 +02:00
parent 81cda14c06
commit 1f462586cd
5 changed files with 187 additions and 99 deletions

View File

@ -27,13 +27,14 @@
*************************************************************************/ *************************************************************************/
#include "vbestsquare.h" #include "vbestsquare.h"
#include "vbestsquare_p.h"
#include <QMatrix> #include <QMatrix>
namespace namespace
{ {
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
qint64 Square(const QSizeF &size) Q_DECL_CONSTEXPR inline qint64 Square(const QSizeF &size)
{ {
return static_cast<qint64>(size.width()*size.height()); return static_cast<qint64>(size.width()*size.height());
} }
@ -41,16 +42,16 @@ qint64 Square(const QSizeF &size)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VBestSquare::VBestSquare(const QSizeF &sheetSize, bool saveLength) VBestSquare::VBestSquare(const QSizeF &sheetSize, bool saveLength)
: resI(0), : d(new VBestSquareData(sheetSize, saveLength))
resJ(0), {}
resMatrix(),
bestSize(QSizeF(sheetSize.width()+10, sheetSize.height()+10)), //---------------------------------------------------------------------------------------------------------------------
sheetSize(sheetSize), VBestSquare::VBestSquare(const VBestSquare &res)
valideResult(false), : d(res.d)
resMirror(false), {}
type(BestFrom::Rotation),
saveLength(saveLength), //---------------------------------------------------------------------------------------------------------------------
position(INT_MAX) VBestSquare::~VBestSquare()
{} {}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -59,36 +60,36 @@ void VBestSquare::NewResult(const QSizeF &candidate, int i, int j, const QTransf
{ {
auto SaveResult = [this, candidate, i, j, matrix, mirror, type, position]() auto SaveResult = [this, candidate, i, j, matrix, mirror, type, position]()
{ {
bestSize = candidate; d->bestSize = candidate;
resI = i; d->resI = i;
resJ = j; d->resJ = j;
resMatrix = matrix; d->resMatrix = matrix;
valideResult = true; d->valideResult = true;
resMirror = mirror; d->resMirror = mirror;
this->type = type; d->type = type;
this->position = position; d->position = position;
}; };
if (saveLength) if (d->saveLength)
{ {
const bool isPortrait = sheetSize.height() >= sheetSize.width(); const bool isPortrait = d->sheetSize.height() >= d->sheetSize.width();
const QSizeF saveSpaceSize = isPortrait ? QSizeF(sheetSize.width(), candidate.height()) : const QSizeF saveSpaceSize = isPortrait ? QSizeF(d->sheetSize.width(), candidate.height()) :
QSizeF(candidate.width(), sheetSize.height()); QSizeF(candidate.width(), d->sheetSize.height());
const QSizeF saveSpaceBestSize = isPortrait ? QSizeF(sheetSize.width(), bestSize.height()) : const QSizeF saveSpaceBestSize = isPortrait ? QSizeF(d->sheetSize.width(), d->bestSize.height()) :
QSizeF(bestSize.width(), sheetSize.height()); QSizeF(d->bestSize.width(), d->sheetSize.height());
if (Square(saveSpaceSize) <= Square(saveSpaceBestSize) && Square(candidate) <= Square(bestSize) if (Square(saveSpaceSize) <= Square(saveSpaceBestSize) && Square(candidate) <= Square(d->bestSize)
&& position <= this->position && Square(saveSpaceSize) > 0 && Square(saveSpaceBestSize) > 0 && position <= d->position && Square(saveSpaceSize) > 0 && Square(saveSpaceBestSize) > 0
&& type >= this->type) && type >= d->type)
{ {
SaveResult(); SaveResult();
} }
} }
else else
{ {
if (Square(candidate) <= Square(bestSize) && Square(candidate) > 0 && position <= this->position if (Square(candidate) <= Square(d->bestSize) && Square(candidate) > 0 && position <= d->position
&& type >= this->type) && type >= d->type)
{ {
SaveResult(); SaveResult();
} }
@ -98,9 +99,63 @@ void VBestSquare::NewResult(const QSizeF &candidate, int i, int j, const QTransf
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VBestSquare::NewResult(const VBestSquare &best) void VBestSquare::NewResult(const VBestSquare &best)
{ {
if (best.ValidResult() && saveLength == best.IsSaveLength()) if (best.IsValidResult() && d->saveLength == best.IsSaveLength())
{ {
NewResult(best.BestSize(), best.GContourEdge(), best.DetailEdge(), best.Matrix(), best.Mirror(), NewResult(best.BestSize(), best.GContourEdge(), best.DetailEdge(), best.Matrix(), best.Mirror(),
best.Position(), best.Type()); best.Position(), best.Type());
} }
} }
//---------------------------------------------------------------------------------------------------------------------
inline QSizeF VBestSquare::BestSize() const
{
return d->bestSize;
}
//---------------------------------------------------------------------------------------------------------------------
inline int VBestSquare::GContourEdge() const
{
return d->resI;
}
//---------------------------------------------------------------------------------------------------------------------
int VBestSquare::DetailEdge() const
{
return d->resJ;
}
//---------------------------------------------------------------------------------------------------------------------
QTransform VBestSquare::Matrix() const
{
return d->resMatrix;
}
//---------------------------------------------------------------------------------------------------------------------
bool VBestSquare::IsValidResult() const
{
return d->valideResult;
}
//---------------------------------------------------------------------------------------------------------------------
bool VBestSquare::Mirror() const
{
return d->resMirror;
}
//---------------------------------------------------------------------------------------------------------------------
BestFrom VBestSquare::Type() const
{
return d->type;
}
//---------------------------------------------------------------------------------------------------------------------
qreal VBestSquare::Position() const
{
return d->position;
}
//---------------------------------------------------------------------------------------------------------------------
bool VBestSquare::IsSaveLength() const
{
return d->saveLength;
}

View File

@ -32,13 +32,26 @@
#include <QSizeF> #include <QSizeF>
#include <QTransform> #include <QTransform>
#include <QtGlobal> #include <QtGlobal>
#include <QSharedDataPointer>
#include <QTypeInfo>
#include "vlayoutdef.h" #include "vlayoutdef.h"
class VBestSquareData;
class VBestSquare class VBestSquare
{ {
public: public:
VBestSquare(const QSizeF &sheetSize, bool saveLength); VBestSquare(const QSizeF &sheetSize, bool saveLength);
VBestSquare(const VBestSquare &res);
virtual ~VBestSquare();
#ifdef Q_COMPILER_RVALUE_REFS
VBestSquare &operator=(VBestSquare &&res) Q_DECL_NOTHROW { Swap(res); return *this; }
#endif
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, void NewResult(const QSizeF &candidate, int i, int j, const QTransform &matrix, bool mirror, qreal position,
BestFrom type); BestFrom type);
@ -48,7 +61,7 @@ public:
int GContourEdge() const; int GContourEdge() const;
int DetailEdge() const; int DetailEdge() const;
QTransform Matrix() const; QTransform Matrix() const;
bool ValidResult() const; bool IsValidResult() const;
bool Mirror() const; bool Mirror() const;
BestFrom Type() const; BestFrom Type() const;
qreal Position() const; qreal Position() const;
@ -56,71 +69,9 @@ public:
bool IsSaveLength() const; bool IsSaveLength() const;
private: private:
// All nedded information about best result QSharedDataPointer<VBestSquareData> d;
int resI; // Edge of global contour
int resJ; // Edge of detail
QTransform resMatrix; // Matrix for rotation and translation detail
QSizeF bestSize;
QSizeF sheetSize;
bool valideResult;
bool resMirror;
BestFrom type;
bool saveLength;
qreal position;
}; };
//--------------------------------------------------------------------------------------------------------------------- Q_DECLARE_TYPEINFO(VBestSquare, Q_MOVABLE_TYPE);
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

@ -0,0 +1,81 @@
/************************************************************************
**
** @file vbestsquare_p.h
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 26 3, 2019
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2019 Valentina project
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef VBESTSQUARE_P_H
#define VBESTSQUARE_P_H
#include <QSharedData>
#include <QTransform>
#include "vlayoutdef.h"
#include "../vmisc/diagnostic.h"
QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Weffc++")
QT_WARNING_DISABLE_GCC("-Wnon-virtual-dtor")
class VBestSquareData : public QSharedData
{
public:
VBestSquareData(const QSizeF &sheetSize, bool saveLength)
: bestSize(QSizeF(sheetSize.width()+10, sheetSize.height()+10)),
sheetSize(sheetSize),
saveLength(saveLength)
{}
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)
{}
~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};
private:
VBestSquareData &operator=(const VBestSquareData &) Q_DECL_EQ_DELETE;
};
#endif // VBESTSQUARE_P_H

View File

@ -20,7 +20,8 @@ HEADERS += \
$$PWD/vlayoutpiece.h \ $$PWD/vlayoutpiece.h \
$$PWD/vlayoutpiece_p.h \ $$PWD/vlayoutpiece_p.h \
$$PWD/vlayoutpiecepath.h \ $$PWD/vlayoutpiecepath.h \
$$PWD/vlayoutpiecepath_p.h $$PWD/vlayoutpiecepath_p.h \
$$PWD/vbestsquare_p.h
SOURCES += \ SOURCES += \
$$PWD/vlayoutgenerator.cpp \ $$PWD/vlayoutgenerator.cpp \

View File

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