2015-01-11 14:11:56 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vbank.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 11 1, 2015
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2015-01-11 14:11:56 +01:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2015-01-11 14:11:56 +01:00
|
|
|
** <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/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vbank.h"
|
|
|
|
|
|
|
|
#include <climits>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include "../vmisc/diagnostic.h"
|
|
|
|
#include "../vmisc/logging.h"
|
2019-02-23 17:43:06 +01:00
|
|
|
#include "../vmisc/vabstractapplication.h"
|
2017-01-22 10:02:02 +01:00
|
|
|
#include "vlayoutpiece.h"
|
2015-01-11 14:11:56 +01:00
|
|
|
|
2016-08-06 20:42:40 +02:00
|
|
|
QT_WARNING_PUSH
|
|
|
|
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
|
|
|
|
QT_WARNING_DISABLE_INTEL(1418)
|
2016-07-26 12:33:58 +02:00
|
|
|
|
|
|
|
Q_LOGGING_CATEGORY(lBank, "layout.bank")
|
|
|
|
|
2016-08-06 20:42:40 +02:00
|
|
|
QT_WARNING_POP
|
2016-07-26 12:33:58 +02:00
|
|
|
|
2016-08-05 15:44:20 +02:00
|
|
|
// An annoying char define, from the Windows team in <rpcndr.h>
|
|
|
|
// #define small char
|
|
|
|
// http://stuartjames.info/Journal/c--visual-studio-2012-vs2012--win8--converting-projects-up-some-conflicts-i-found.aspx
|
|
|
|
#if defined (Q_OS_WIN) && defined (Q_CC_MSVC)
|
|
|
|
#pragma push_macro("small")
|
|
|
|
#undef small
|
|
|
|
#endif
|
|
|
|
|
2015-01-11 14:11:56 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VBank::VBank()
|
2017-01-22 10:02:02 +01:00
|
|
|
:details(QVector<VLayoutPiece>()), unsorted(QHash<int, qint64>()), big(QHash<int, qint64>()),
|
2015-01-11 14:11:56 +01:00
|
|
|
middle(QHash<int, qint64>()), small(QHash<int, qint64>()), layoutWidth(0), caseType(Cases::CaseDesc),
|
2016-02-23 13:13:10 +01:00
|
|
|
prepare(false), diagonal(0)
|
2015-01-11 14:11:56 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal VBank::GetLayoutWidth() const
|
|
|
|
{
|
|
|
|
return layoutWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VBank::SetLayoutWidth(const qreal &value)
|
|
|
|
{
|
|
|
|
layoutWidth = value;
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
void VBank::SetDetails(const QVector<VLayoutPiece> &details)
|
2015-01-11 14:11:56 +01:00
|
|
|
{
|
|
|
|
this->details = details;
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VBank::GetTiket()
|
|
|
|
{
|
|
|
|
if (prepare == false)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LeftArrange() == 0)
|
|
|
|
{
|
|
|
|
if (unsorted.isEmpty())
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PrepareGroup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:11:43 +01:00
|
|
|
switch (caseType)
|
2015-01-11 14:11:56 +01:00
|
|
|
{
|
|
|
|
case Cases::CaseThreeGroup:
|
|
|
|
return GetNextThreeGroups();
|
|
|
|
case Cases::CaseTwoGroup:
|
|
|
|
return GetNextTwoGroups();
|
|
|
|
case Cases::CaseDesc:
|
|
|
|
return GetNextDescGroup();
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
VLayoutPiece VBank::GetDetail(int i) const
|
2015-01-11 14:11:56 +01:00
|
|
|
{
|
|
|
|
if (i >= 0 && i < details.size())
|
|
|
|
{
|
|
|
|
return details.at(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-22 10:02:02 +01:00
|
|
|
return VLayoutPiece();
|
2015-01-11 14:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VBank::Arranged(int i)
|
|
|
|
{
|
|
|
|
if (big.contains(i))
|
|
|
|
{
|
|
|
|
big.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (middle.contains(i))
|
|
|
|
{
|
|
|
|
middle.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (small.contains(i))
|
|
|
|
{
|
|
|
|
small.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VBank::NotArranged(int i)
|
|
|
|
{
|
|
|
|
if (big.contains(i))
|
|
|
|
{
|
|
|
|
unsorted.insert(i, big.value(i));
|
|
|
|
big.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (middle.contains(i))
|
|
|
|
{
|
|
|
|
unsorted.insert(i, middle.value(i));
|
|
|
|
middle.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (small.contains(i))
|
|
|
|
{
|
|
|
|
unsorted.insert(i, small.value(i));
|
|
|
|
small.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VBank::Prepare()
|
|
|
|
{
|
|
|
|
if (layoutWidth <= 0)
|
|
|
|
{
|
2016-07-26 12:33:58 +02:00
|
|
|
qCDebug(lBank, "Preparing data for layout error: Layout paper sheet <= 0");
|
2015-01-11 14:11:56 +01:00
|
|
|
prepare = false;
|
|
|
|
return prepare;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (details.isEmpty())
|
|
|
|
{
|
2016-07-26 12:33:58 +02:00
|
|
|
qCDebug(lBank, "Preparing data for layout error: List of details is empty");
|
2015-01-11 14:11:56 +01:00
|
|
|
prepare = false;
|
|
|
|
return prepare;
|
|
|
|
}
|
|
|
|
|
2016-02-23 13:13:10 +01:00
|
|
|
diagonal = 0;
|
2015-01-11 14:11:56 +01:00
|
|
|
for (int i=0; i < details.size(); ++i)
|
|
|
|
{
|
|
|
|
details[i].SetLayoutWidth(layoutWidth);
|
2017-01-25 12:29:03 +01:00
|
|
|
details[i].SetLayoutAllowancePoints();
|
2019-02-23 17:43:06 +01:00
|
|
|
if (not details.at(i).IsLayoutAllowanceValid())
|
2017-12-12 09:23:46 +01:00
|
|
|
{
|
2019-02-23 17:43:06 +01:00
|
|
|
const QString errorMsg = QObject::tr("Piece '%1' has invalid layout allowance. Please, check seam allowance"
|
|
|
|
" to check how seam allowance behave.").arg(details.at(i).GetName());
|
|
|
|
qApp->IsPedantic() ? throw VException(errorMsg) : qWarning() << errorMsg;
|
2017-12-12 09:23:46 +01:00
|
|
|
}
|
2016-02-23 13:13:10 +01:00
|
|
|
|
|
|
|
const qreal d = details.at(i).Diagonal();
|
|
|
|
if (d > diagonal)
|
|
|
|
{
|
|
|
|
diagonal = d;
|
|
|
|
}
|
|
|
|
|
2015-01-11 14:11:56 +01:00
|
|
|
const qint64 square = details.at(i).Square();
|
|
|
|
if (square <= 0)
|
|
|
|
{
|
2018-05-21 10:35:50 +02:00
|
|
|
qCDebug(lBank, "Preparing data for layout error: Detail '%s' square <= 0",
|
|
|
|
qUtf8Printable(details.at(i).GetName()));
|
2015-01-11 14:11:56 +01:00
|
|
|
prepare = false;
|
|
|
|
return prepare;
|
|
|
|
}
|
|
|
|
unsorted.insert(i, square);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrepareGroup();
|
|
|
|
|
|
|
|
prepare = true;
|
|
|
|
return prepare;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VBank::Reset()
|
|
|
|
{
|
|
|
|
prepare = false;
|
|
|
|
unsorted.clear();
|
|
|
|
big.clear();
|
|
|
|
middle.clear();
|
|
|
|
small.clear();
|
2016-02-23 13:13:10 +01:00
|
|
|
diagonal = 0;
|
2015-01-11 14:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VBank::SetCaseType(Cases caseType)
|
|
|
|
{
|
|
|
|
this->caseType = caseType;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VBank::AllDetailsCount() const
|
|
|
|
{
|
|
|
|
return unsorted.count() + big.count() + middle.count() + small.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VBank::LeftArrange() const
|
|
|
|
{
|
|
|
|
return big.count() + middle.count() + small.count();
|
|
|
|
}
|
|
|
|
|
2015-01-12 13:12:15 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-02-23 13:13:10 +01:00
|
|
|
qreal VBank::GetBiggestDiagonal() const
|
2015-01-12 13:12:15 +01:00
|
|
|
{
|
2016-02-23 13:13:10 +01:00
|
|
|
return diagonal;
|
2015-01-12 13:12:15 +01:00
|
|
|
}
|
|
|
|
|
2015-01-16 13:53:14 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VBank::ArrangedCount() const
|
|
|
|
{
|
|
|
|
return details.size() - AllDetailsCount();
|
|
|
|
}
|
|
|
|
|
2015-01-11 14:11:56 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VBank::PrepareGroup()
|
|
|
|
{
|
2015-03-02 18:11:43 +01:00
|
|
|
switch (caseType)
|
2015-01-11 14:11:56 +01:00
|
|
|
{
|
|
|
|
case Cases::CaseThreeGroup:
|
|
|
|
PrepareThreeGroups();
|
|
|
|
break;
|
|
|
|
case Cases::CaseTwoGroup:
|
|
|
|
PrepareTwoGroups();
|
|
|
|
break;
|
|
|
|
case Cases::CaseDesc:
|
|
|
|
PrepareDescGroup();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VBank::PrepareThreeGroups()
|
|
|
|
{
|
|
|
|
qint64 sMax = LLONG_MIN;
|
|
|
|
qint64 sMin = LLONG_MAX;
|
|
|
|
|
|
|
|
SqMaxMin(sMax, sMin);
|
|
|
|
|
|
|
|
const qint64 s1 = sMax - (sMax - sMin)/3;
|
|
|
|
const qint64 s2 = sMin + (sMax - sMin)/3;
|
|
|
|
|
|
|
|
QHash<int, qint64>::const_iterator i = unsorted.constBegin();
|
|
|
|
while (i != unsorted.constEnd())
|
|
|
|
{
|
|
|
|
if (i.value() > s1)
|
|
|
|
{
|
|
|
|
big.insert(i.key(), i.value());
|
|
|
|
}
|
2015-01-12 13:12:15 +01:00
|
|
|
else if (s1 >= i.value() && i.value() > s2)
|
2015-01-11 14:11:56 +01:00
|
|
|
{
|
|
|
|
middle.insert(i.key(), i.value());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
small.insert(i.key(), i.value());
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
unsorted.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VBank::PrepareTwoGroups()
|
|
|
|
{
|
|
|
|
qint64 sMax = LLONG_MIN;
|
|
|
|
qint64 sMin = LLONG_MAX;
|
|
|
|
|
|
|
|
SqMaxMin(sMax, sMin);
|
|
|
|
|
|
|
|
const qint64 s = (sMax + sMin)/2;
|
|
|
|
QHash<int, qint64>::const_iterator i = unsorted.constBegin();
|
|
|
|
while (i != unsorted.constEnd())
|
|
|
|
{
|
|
|
|
if (i.value() >= s)
|
|
|
|
{
|
|
|
|
big.insert(i.key(), i.value());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
small.insert(i.key(), i.value());
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
unsorted.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VBank::PrepareDescGroup()
|
|
|
|
{
|
|
|
|
big = unsorted;
|
|
|
|
unsorted.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VBank::GetNextThreeGroups() const
|
|
|
|
{
|
|
|
|
if (big.isEmpty() == false)
|
|
|
|
{
|
|
|
|
QHash<int, qint64>::const_iterator i = big.constBegin();
|
|
|
|
return i.key();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (middle.isEmpty() == false)
|
|
|
|
{
|
|
|
|
QHash<int, qint64>::const_iterator i = middle.constBegin();
|
|
|
|
return i.key();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (small.isEmpty() == false)
|
|
|
|
{
|
|
|
|
QHash<int, qint64>::const_iterator i = small.constBegin();
|
|
|
|
return i.key();
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VBank::GetNextTwoGroups() const
|
|
|
|
{
|
|
|
|
if (big.isEmpty() == false)
|
|
|
|
{
|
|
|
|
QHash<int, qint64>::const_iterator i = big.constBegin();
|
|
|
|
return i.key();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (small.isEmpty() == false)
|
|
|
|
{
|
|
|
|
QHash<int, qint64>::const_iterator i = small.constBegin();
|
|
|
|
return i.key();
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VBank::GetNextDescGroup() const
|
|
|
|
{
|
|
|
|
int index = -1;
|
|
|
|
qint64 sMax = LLONG_MIN;
|
|
|
|
|
|
|
|
QHash<int, qint64>::const_iterator i = big.constBegin();
|
|
|
|
while (i != big.constEnd())
|
|
|
|
{
|
|
|
|
if (i.value() > sMax)
|
|
|
|
{
|
|
|
|
sMax = i.value();
|
|
|
|
index = i.key();
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VBank::SqMaxMin(qint64 &sMax, qint64 &sMin) const
|
|
|
|
{
|
|
|
|
sMax = LLONG_MIN;
|
|
|
|
sMin = LLONG_MAX;
|
|
|
|
|
|
|
|
QHash<int, qint64>::const_iterator i = unsorted.constBegin();
|
|
|
|
while (i != unsorted.constEnd())
|
|
|
|
{
|
|
|
|
if (i.value() < sMin)
|
|
|
|
{
|
|
|
|
sMin = i.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i.value() > sMax)
|
|
|
|
{
|
|
|
|
sMax = i.value();
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-08-05 15:44:20 +02:00
|
|
|
|
|
|
|
#if defined (Q_OS_WIN) && defined (Q_CC_MSVC)
|
|
|
|
#pragma pop_macro("small")
|
|
|
|
#endif
|