2015-01-21 22:49:38 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vposition.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 20 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-21 22:49:38 +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
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2015-01-21 22:49:38 +01:00
|
|
|
**
|
|
|
|
** 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 "vposition.h"
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QDir>
|
2019-08-22 09:57:38 +02:00
|
|
|
#include <QtConcurrent>
|
|
|
|
#include <QFutureWatcher>
|
2015-01-21 22:49:38 +01:00
|
|
|
#include <QImage>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QLineF>
|
2015-01-21 22:49:38 +01:00
|
|
|
#include <QPainter>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QPainterPath>
|
|
|
|
#include <QPen>
|
|
|
|
#include <QPicture>
|
|
|
|
#include <QPointF>
|
|
|
|
#include <QPolygonF>
|
|
|
|
#include <QRect>
|
|
|
|
#include <QRectF>
|
|
|
|
#include <QSizeF>
|
|
|
|
#include <QString>
|
2019-06-18 14:10:08 +02:00
|
|
|
#include <QThreadPool>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <Qt>
|
2019-08-22 16:47:34 +02:00
|
|
|
#include <functional>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include "../vmisc/def.h"
|
2019-07-22 16:06:30 +02:00
|
|
|
#include "../ifc/exception/vexception.h"
|
|
|
|
#include "../vpatterndb/floatItemData/floatitemdef.h"
|
2022-10-28 15:16:02 +02:00
|
|
|
#include "../vlayout/vlayoutpoint.h"
|
2015-01-21 22:49:38 +01:00
|
|
|
|
2020-11-26 18:10:09 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPainterPath ShowDirection(const QLineF &edge)
|
|
|
|
{
|
|
|
|
const int arrowLength = 14;
|
|
|
|
QPainterPath path;
|
|
|
|
if (edge.length()/arrowLength < 5)
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
QLineF arrow = edge;
|
|
|
|
arrow.setLength(edge.length()/2.0);
|
|
|
|
|
|
|
|
//Reverse line because we want start arrow from this point
|
|
|
|
arrow = QLineF(arrow.p2(), arrow.p1());
|
|
|
|
const qreal angle = arrow.angle();//we each time change line angle, better save original angle value
|
|
|
|
arrow.setLength(arrowLength);//arrow length in pixels
|
|
|
|
|
|
|
|
arrow.setAngle(angle-35);
|
|
|
|
path.moveTo(arrow.p1());
|
|
|
|
path.lineTo(arrow.p2());
|
|
|
|
|
|
|
|
arrow.setAngle(angle+35);
|
|
|
|
path.moveTo(arrow.p1());
|
|
|
|
path.lineTo(arrow.p2());
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPainterPath DumpContour(const QVector<QPointF> &points)
|
|
|
|
{
|
|
|
|
QPainterPath path;
|
|
|
|
path.setFillRule(Qt::WindingFill);
|
|
|
|
if (points.count() >= 2)
|
|
|
|
{
|
|
|
|
for (qint32 i = 0; i < points.count()-1; ++i)
|
|
|
|
{
|
|
|
|
path.moveTo(points.at(i));
|
|
|
|
path.lineTo(points.at(i+1));
|
|
|
|
}
|
|
|
|
path.lineTo(points.at(0));
|
|
|
|
|
|
|
|
#ifdef SHOW_DIRECTION
|
|
|
|
for (qint32 i = 0; i < points.count()-1; ++i)
|
|
|
|
{
|
|
|
|
path.addPath(ShowDirection(QLineF(points.at(i), points.at(i+1))));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SHOW_VERTICES
|
|
|
|
for (qint32 i = 0; i < points.count(); ++i)
|
|
|
|
{
|
|
|
|
path.addRect(points.at(i).x()-3, points.at(i).y()-3, 6, 6);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPainterPath DumpDetails(const QVector<VLayoutPiece> &details)
|
|
|
|
{
|
|
|
|
QPainterPath path;
|
|
|
|
path.setFillRule(Qt::WindingFill);
|
|
|
|
if (details.count() > 0)
|
|
|
|
{
|
|
|
|
for (auto &detail : details)
|
|
|
|
{
|
|
|
|
path.addPath(detail.ContourPath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} //anonymous namespace
|
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2019-03-26 17:54:59 +01:00
|
|
|
VPosition::VPosition(const VPositionData &data, std::atomic_bool *stop, bool saveLength)
|
2019-08-22 09:57:38 +02:00
|
|
|
: m_isValid(true),
|
2019-06-18 13:36:20 +02:00
|
|
|
m_bestResult(VBestSquare(data.gContour.GetSize(), saveLength, data.isOriginPaperOrientationPortrait)),
|
2019-03-26 17:54:59 +01:00
|
|
|
m_data(data),
|
2022-08-16 15:01:17 +02:00
|
|
|
stop(stop)
|
2015-01-22 15:11:50 +01:00
|
|
|
{
|
2019-03-29 18:52:37 +01:00
|
|
|
if (m_data.rotationNumber > 360 || m_data.rotationNumber < 1)
|
2015-01-22 15:11:50 +01:00
|
|
|
{
|
2019-03-29 18:52:37 +01:00
|
|
|
m_data.rotationNumber = 2;
|
2015-01-22 15:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-21 22:49:38 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPosition::run()
|
|
|
|
{
|
2019-08-22 09:57:38 +02:00
|
|
|
if (not m_isValid)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-22 13:04:47 +01:00
|
|
|
if (stop->load())
|
2015-01-22 12:39:04 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:14:10 +02:00
|
|
|
try
|
|
|
|
{
|
2019-08-22 09:57:38 +02:00
|
|
|
for (int i=1; i<= m_data.detail.LayoutEdgesCount(); ++i)
|
|
|
|
{
|
|
|
|
if (stop->load())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_data.i = i;
|
|
|
|
FindBestPosition();
|
|
|
|
|
|
|
|
if (stop->load())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 11:14:10 +02:00
|
|
|
}
|
|
|
|
catch (const VException &e)
|
|
|
|
{
|
|
|
|
m_bestResult.TerminatedByException(QStringLiteral("%1\n\n%2").arg(e.ErrorMessage(), e.DetailedInformation()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
m_bestResult.TerminatedByException(QString::fromLatin1(e.what()));
|
|
|
|
return;
|
|
|
|
}
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-08-16 15:01:17 +02:00
|
|
|
auto VPosition::getBestResult() const -> VBestSquare
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2019-03-26 17:54:59 +01:00
|
|
|
return m_bestResult;
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
2019-06-18 14:10:08 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-08-16 15:01:17 +02:00
|
|
|
auto VPosition::ArrangeDetail(const VPositionData &data, std::atomic_bool *stop, bool saveLength) -> VBestSquare
|
2019-06-18 14:10:08 +02:00
|
|
|
{
|
|
|
|
VBestSquare bestResult(data.gContour.GetSize(), saveLength, data.isOriginPaperOrientationPortrait);
|
|
|
|
|
|
|
|
if (stop->load())
|
|
|
|
{
|
|
|
|
return bestResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First need set size of paper
|
|
|
|
if (data.gContour.GetHeight() <= 0 || data.gContour.GetWidth() <= 0)
|
|
|
|
{
|
|
|
|
return bestResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VLayoutPiece detail = data.detail;
|
2023-02-09 16:23:11 +01:00
|
|
|
const auto detailEdgesCount = detail.LayoutEdgesCount();
|
2019-06-18 14:10:08 +02:00
|
|
|
if (detailEdgesCount < 3 || detail.DetailEdgesCount() < 3)
|
|
|
|
{
|
|
|
|
return bestResult;//Not enough edges
|
|
|
|
}
|
|
|
|
|
2019-08-22 09:57:38 +02:00
|
|
|
QFutureWatcher<VBestSquare> watcher;
|
|
|
|
QVector<VPosition> jobs;
|
|
|
|
jobs.reserve(data.gContour.GlobalEdgesCount());
|
2019-06-18 14:10:08 +02:00
|
|
|
|
|
|
|
for (int j=1; j <= data.gContour.GlobalEdgesCount(); ++j)
|
|
|
|
{
|
2019-08-22 09:57:38 +02:00
|
|
|
VPositionData linkedData = data;
|
|
|
|
linkedData.j = j;
|
2019-06-18 14:10:08 +02:00
|
|
|
|
2019-08-22 09:57:38 +02:00
|
|
|
jobs.append(VPosition(linkedData, stop, saveLength));
|
2019-06-18 14:10:08 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 09:57:38 +02:00
|
|
|
Q_ASSERT(not jobs.isEmpty());
|
|
|
|
|
|
|
|
std::function<VBestSquare (VPosition position)> Nest = [](VPosition position)
|
|
|
|
{
|
|
|
|
position.run();
|
|
|
|
return position.getBestResult();
|
|
|
|
};
|
|
|
|
|
|
|
|
watcher.setFuture(QtConcurrent::mapped(jobs, Nest));
|
|
|
|
|
|
|
|
while(not watcher.isStarted())
|
|
|
|
{
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
QThread::msleep(250);
|
|
|
|
}
|
2019-07-18 16:34:43 +02:00
|
|
|
|
2019-06-18 14:10:08 +02:00
|
|
|
// Wait for done
|
|
|
|
do
|
|
|
|
{
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
QThread::msleep(250);
|
|
|
|
}
|
2019-08-22 09:57:38 +02:00
|
|
|
while(watcher.isRunning() && not stop->load());
|
2019-06-18 14:10:08 +02:00
|
|
|
|
|
|
|
if (stop->load())
|
|
|
|
{
|
2019-08-22 09:57:38 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
QThread::msleep(250);
|
|
|
|
}
|
|
|
|
while(watcher.isRunning());
|
|
|
|
|
2019-06-18 14:10:08 +02:00
|
|
|
return bestResult;
|
|
|
|
}
|
|
|
|
|
2019-08-22 09:57:38 +02:00
|
|
|
QList<VBestSquare> results = watcher.future().results();
|
|
|
|
for (auto &result : results)
|
2019-06-18 14:10:08 +02:00
|
|
|
{
|
2019-08-22 09:57:38 +02:00
|
|
|
bestResult.NewResult(result);
|
2019-06-18 14:10:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return bestResult;
|
|
|
|
}
|
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
void VPosition::SaveCandidate(VBestSquare &bestResult, const VLayoutPiece &detail, int globalI, int detJ,
|
2015-01-21 22:49:38 +01:00
|
|
|
BestFrom type)
|
|
|
|
{
|
2019-03-27 08:03:19 +01:00
|
|
|
if (bestResult.IsSaveLength())
|
|
|
|
{
|
|
|
|
m_data.gContour.CeateEmptySheetContour();
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
QVector<QPointF> newGContour = m_data.gContour.UniteWithContour(detail, globalI, detJ, type);
|
2022-01-29 09:59:02 +01:00
|
|
|
newGContour.append(ConstFirst(newGContour));
|
2015-05-08 12:10:56 +02:00
|
|
|
const QSizeF size = QPolygonF(newGContour).boundingRect().size();
|
2021-05-27 18:52:37 +02:00
|
|
|
const QRectF boundingRect = detail.MappedDetailBoundingRect();
|
2019-06-18 13:28:16 +02:00
|
|
|
const qreal depthPosition = m_data.isOriginPaperOrientationPortrait ? boundingRect.y() : boundingRect.x();
|
|
|
|
const qreal sidePosition = m_data.isOriginPaperOrientationPortrait ? boundingRect.x() : boundingRect.y();
|
2019-03-27 08:31:02 +01:00
|
|
|
|
2023-02-09 15:18:13 +01:00
|
|
|
QT_WARNING_PUSH
|
|
|
|
QT_WARNING_DISABLE_GCC("-Wnoexcept")
|
|
|
|
|
2019-03-27 08:31:02 +01:00
|
|
|
VBestSquareResData data;
|
2023-02-09 15:18:13 +01:00
|
|
|
|
|
|
|
QT_WARNING_POP
|
|
|
|
|
2019-03-27 08:31:02 +01:00
|
|
|
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;
|
2019-06-18 13:28:16 +02:00
|
|
|
data.sidePosition = sidePosition;
|
2019-03-27 08:31:02 +01:00
|
|
|
|
|
|
|
bestResult.NewResult(data);
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-08-16 15:01:17 +02:00
|
|
|
auto VPosition::CheckCombineEdges(VLayoutPiece &detail, int j, int &dEdge) -> bool
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2019-03-26 17:54:59 +01:00
|
|
|
const QLineF globalEdge = m_data.gContour.GlobalEdge(j);
|
2015-01-21 22:49:38 +01:00
|
|
|
bool flagMirror = false;
|
|
|
|
bool flagSquare = false;
|
|
|
|
|
|
|
|
CombineEdges(detail, globalEdge, dEdge);
|
|
|
|
|
2020-11-26 18:10:09 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_COMBINE
|
|
|
|
DumpFrame(m_data.gContour, detail, m_data.mutex, m_data.details);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
CrossingType type = CrossingType::Intersection;
|
2021-05-27 18:52:37 +02:00
|
|
|
if (not detail.IsForceFlipping() && SheetContains(detail.MappedDetailBoundingRect()))
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2019-03-26 17:54:59 +01:00
|
|
|
if (not m_data.gContour.GetContour().isEmpty())
|
2015-05-15 17:16:15 +02:00
|
|
|
{
|
2016-03-28 10:53:02 +02:00
|
|
|
type = Crossing(detail);
|
2015-05-15 17:16:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
type = CrossingType::NoIntersection;
|
|
|
|
}
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case CrossingType::EdgeError:
|
|
|
|
return false;
|
|
|
|
case CrossingType::Intersection:
|
|
|
|
detail.Mirror(globalEdge);
|
|
|
|
flagMirror = true;
|
|
|
|
break;
|
|
|
|
case CrossingType::NoIntersection:
|
2016-03-28 10:53:02 +02:00
|
|
|
flagSquare = true;
|
2015-03-16 13:23:02 +01:00
|
|
|
break;
|
2015-01-21 22:49:38 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-22 09:50:23 +01:00
|
|
|
if (flagMirror && not detail.IsForbidFlipping())
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2020-11-26 18:10:09 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_MIRROR
|
|
|
|
DumpFrame(m_data.gContour, detail, m_data.mutex, m_data.details);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2022-11-21 15:47:32 +01:00
|
|
|
EdgeIndex layoutEdge = detail.LayoutEdgeByPoint(globalEdge.p2());
|
|
|
|
if (not layoutEdge.has_value())
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-22 12:11:08 +01:00
|
|
|
dEdge = *layoutEdge;
|
2015-01-21 22:49:38 +01:00
|
|
|
CrossingType type = CrossingType::Intersection;
|
2021-05-27 18:52:37 +02:00
|
|
|
if (SheetContains(detail.MappedDetailBoundingRect()))
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2016-03-28 10:53:02 +02:00
|
|
|
type = Crossing(detail);
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case CrossingType::EdgeError:
|
|
|
|
return false;
|
|
|
|
case CrossingType::Intersection:
|
|
|
|
flagSquare = false;
|
|
|
|
break;
|
|
|
|
case CrossingType::NoIntersection:
|
2016-03-28 10:53:02 +02:00
|
|
|
flagSquare = true;
|
2015-03-16 13:23:02 +01:00
|
|
|
break;
|
2015-01-21 22:49:38 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flagSquare;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-08-16 15:01:17 +02:00
|
|
|
auto VPosition::CheckRotationEdges(VLayoutPiece &detail, int j, int dEdge, qreal angle) const -> bool
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2019-03-26 17:54:59 +01:00
|
|
|
const QLineF globalEdge = m_data.gContour.GlobalEdge(j);
|
2015-01-21 22:49:38 +01:00
|
|
|
bool flagSquare = false;
|
|
|
|
|
2017-12-17 14:06:46 +01:00
|
|
|
if (detail.IsForceFlipping())
|
|
|
|
{
|
2019-03-26 17:54:59 +01:00
|
|
|
detail.Mirror(not m_data.followGrainline ? globalEdge : QLineF(10, 10, 10, 100));
|
2017-12-17 14:06:46 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
RotateEdges(detail, globalEdge, dEdge, angle);
|
|
|
|
|
2020-11-26 18:10:09 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_ROTATION
|
|
|
|
DumpFrame(m_data.gContour, detail, m_data.mutex, m_data.details);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
CrossingType type = CrossingType::Intersection;
|
2021-05-27 18:52:37 +02:00
|
|
|
if (SheetContains(detail.MappedDetailBoundingRect()))
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2016-03-28 10:53:02 +02:00
|
|
|
type = Crossing(detail);
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case CrossingType::EdgeError:
|
|
|
|
return false;
|
|
|
|
case CrossingType::Intersection:
|
|
|
|
flagSquare = false;
|
|
|
|
break;
|
|
|
|
case CrossingType::NoIntersection:
|
2016-03-28 10:53:02 +02:00
|
|
|
flagSquare = true;
|
2015-03-16 13:23:02 +01:00
|
|
|
break;
|
2015-01-21 22:49:38 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return flagSquare;
|
|
|
|
}
|
|
|
|
|
2018-12-27 14:54:29 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPosition::RotateOnAngle(qreal angle)
|
|
|
|
{
|
|
|
|
// We should use copy of the detail.
|
2019-03-26 17:54:59 +01:00
|
|
|
VLayoutPiece workDetail = m_data.detail;
|
2018-12-27 14:54:29 +01:00
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
if (CheckRotationEdges(workDetail, m_data.j, m_data.i, angle))
|
2018-12-27 14:54:29 +01:00
|
|
|
{
|
2020-11-26 18:11:42 +01:00
|
|
|
if (stop->load())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-26 18:10:09 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_CANDIDATE_BEST
|
|
|
|
DumpFrame(m_data.gContour, workDetail, m_data.mutex, m_data.details);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
SaveCandidate(m_bestResult, workDetail, m_data.j, m_data.i, BestFrom::Rotation);
|
2018-12-27 14:54:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-08-16 15:01:17 +02:00
|
|
|
auto VPosition::Crossing(const VLayoutPiece &detail) const -> VPosition::CrossingType
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2019-04-10 14:54:18 +02:00
|
|
|
if (m_data.positionsCache.isEmpty())
|
2016-03-28 10:22:15 +02:00
|
|
|
{
|
|
|
|
return CrossingType::NoIntersection;
|
|
|
|
}
|
|
|
|
|
2021-05-27 18:52:37 +02:00
|
|
|
const QVector<QPointF> layoutPoints = detail.GetMappedLayoutAllowancePoints();
|
2019-07-22 15:42:37 +02:00
|
|
|
const QRectF layoutBoundingRect = VLayoutPiece::BoundingRect(layoutPoints);
|
2021-01-23 14:24:44 +01:00
|
|
|
const QPainterPath layoutAllowancePath = VAbstractPiece::PainterPath(layoutPoints);
|
2019-04-10 14:54:18 +02:00
|
|
|
|
2022-10-28 12:55:24 +02:00
|
|
|
QVector<QPointF> contourPoints;
|
|
|
|
CastTo(detail.IsSeamAllowance() && not detail.IsSeamAllowanceBuiltIn() ?
|
|
|
|
detail.GetMappedSeamAllowancePoints() : detail.GetMappedContourPoints(), contourPoints);
|
2019-07-22 15:42:37 +02:00
|
|
|
const QRectF detailBoundingRect = VLayoutPiece::BoundingRect(contourPoints);
|
2021-01-23 14:24:44 +01:00
|
|
|
const QPainterPath contourPath = VAbstractPiece::PainterPath(contourPoints);
|
2019-04-10 14:54:18 +02:00
|
|
|
|
2022-08-16 15:01:17 +02:00
|
|
|
for(const auto &position : m_data.positionsCache)
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2020-11-26 18:55:02 +01:00
|
|
|
if (position.boundingRect.intersects(layoutBoundingRect) || position.boundingRect.contains(detailBoundingRect)
|
|
|
|
|| detailBoundingRect.contains(position.boundingRect))
|
2019-04-10 14:54:18 +02:00
|
|
|
{
|
2020-11-26 18:55:02 +01:00
|
|
|
if (position.layoutAllowancePath.contains(contourPath) || contourPath.contains(position.layoutAllowancePath)
|
|
|
|
|| position.layoutAllowancePath.intersects(layoutAllowancePath))
|
2019-07-22 15:42:37 +02:00
|
|
|
{
|
|
|
|
return CrossingType::Intersection;
|
|
|
|
}
|
2019-04-10 14:54:18 +02:00
|
|
|
}
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
2019-04-10 14:54:18 +02:00
|
|
|
|
|
|
|
return CrossingType::NoIntersection;
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-08-16 15:01:17 +02:00
|
|
|
auto VPosition::SheetContains(const QRectF &rect) const -> bool
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2019-03-15 14:33:30 +01:00
|
|
|
const QRectF bRect(-accuracyPointOnLine, -accuracyPointOnLine, m_data.gContour.GetWidth()+accuracyPointOnLine,
|
|
|
|
m_data.gContour.GetHeight()+accuracyPointOnLine);
|
2015-01-21 22:49:38 +01:00
|
|
|
return bRect.contains(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-12-28 12:08:53 +01:00
|
|
|
void VPosition::CombineEdges(VLayoutPiece &detail, const QLineF &globalEdge, int dEdge)
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2019-07-21 09:19:00 +02:00
|
|
|
QLineF detailEdge = detail.LayoutEdge(dEdge);
|
2015-01-21 22:49:38 +01:00
|
|
|
|
|
|
|
// Find distance between two edges for two begin vertex.
|
|
|
|
const qreal dx = globalEdge.x2() - detailEdge.x2();
|
|
|
|
const qreal dy = globalEdge.y2() - detailEdge.y2();
|
|
|
|
|
|
|
|
detailEdge.translate(dx, dy); // Use values for translate detail edge.
|
|
|
|
|
2015-05-15 17:16:15 +02:00
|
|
|
angle_between = globalEdge.angleTo(detailEdge); // Seek angle between two edges.
|
2015-01-21 22:49:38 +01:00
|
|
|
|
|
|
|
// Now we move detail to position near to global contour edge.
|
|
|
|
detail.Translate(dx, dy);
|
2016-03-23 15:09:30 +01:00
|
|
|
if (not qFuzzyIsNull(angle_between) || not qFuzzyCompare(angle_between, 360))
|
2015-05-15 17:16:15 +02:00
|
|
|
{
|
|
|
|
detail.Rotate(detailEdge.p2(), -angle_between);
|
|
|
|
}
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2019-08-22 14:33:32 +02:00
|
|
|
void VPosition::RotateEdges(VLayoutPiece &detail, const QLineF &globalEdge, int dEdge, qreal angle)
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2019-07-21 09:19:00 +02:00
|
|
|
QLineF detailEdge = detail.LayoutEdge(dEdge);
|
2015-01-21 22:49:38 +01:00
|
|
|
// Now we move detail to position near to global contour edge.
|
2019-07-21 09:19:00 +02:00
|
|
|
detail.Translate(globalEdge.x2() - detailEdge.x2(), globalEdge.y2() - detailEdge.y2());
|
2015-01-21 22:49:38 +01:00
|
|
|
detail.Rotate(globalEdge.p2(), angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2019-03-29 18:52:37 +01:00
|
|
|
void VPosition::Rotate(int number)
|
2015-01-22 12:47:20 +01:00
|
|
|
{
|
2022-08-16 15:01:17 +02:00
|
|
|
const qreal step = 360.0/number;
|
2019-03-29 18:52:37 +01:00
|
|
|
qreal startAngle = 0;
|
2016-03-23 15:09:30 +01:00
|
|
|
if (VFuzzyComparePossibleNulls(angle_between, 360))
|
2015-05-15 17:16:15 +02:00
|
|
|
{
|
2019-03-29 18:52:37 +01:00
|
|
|
startAngle = step;
|
2015-05-15 17:16:15 +02:00
|
|
|
}
|
2022-08-16 15:01:17 +02:00
|
|
|
|
|
|
|
qreal angle = startAngle;
|
|
|
|
while(angle < 360)
|
2015-01-22 12:47:20 +01:00
|
|
|
{
|
2017-02-22 13:04:47 +01:00
|
|
|
if (stop->load())
|
2015-01-22 12:47:20 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-27 14:54:29 +01:00
|
|
|
RotateOnAngle(angle);
|
2022-08-16 15:01:17 +02:00
|
|
|
|
|
|
|
angle = angle+step;
|
2018-12-27 14:54:29 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-22 12:47:20 +01:00
|
|
|
|
2018-12-27 14:54:29 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPosition::FollowGrainline()
|
|
|
|
{
|
|
|
|
if (stop->load())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-01-22 12:47:20 +01:00
|
|
|
|
2018-12-27 14:54:29 +01:00
|
|
|
QLineF detailGrainline(10, 10, 100, 10);
|
2019-03-26 17:54:59 +01:00
|
|
|
detailGrainline.setAngle(m_data.detail.GrainlineAngle());
|
2018-12-27 14:54:29 +01:00
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
if (m_data.detail.IsForceFlipping())
|
2018-12-31 10:33:20 +01:00
|
|
|
{
|
2019-03-26 17:54:59 +01:00
|
|
|
VLayoutPiece workDetail = m_data.detail; // We need copy for temp change
|
|
|
|
workDetail.Mirror(not m_data.followGrainline ? m_data.gContour.GlobalEdge(m_data.j) : QLineF(10, 10, 10, 100));
|
2018-12-31 10:33:20 +01:00
|
|
|
detailGrainline = workDetail.GetMatrix().map(detailGrainline);
|
|
|
|
}
|
|
|
|
|
2019-08-22 09:57:38 +02:00
|
|
|
if (stop->load())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-27 14:54:29 +01:00
|
|
|
const qreal angle = detailGrainline.angleTo(FabricGrainline());
|
|
|
|
|
2020-01-07 12:54:23 +01:00
|
|
|
if (m_data.detail.GrainlineArrowType() == GrainlineArrowDirection::atBoth ||
|
|
|
|
m_data.detail.GrainlineArrowType() == GrainlineArrowDirection::atFront)
|
2018-12-27 14:54:29 +01:00
|
|
|
{
|
|
|
|
RotateOnAngle(angle);
|
|
|
|
}
|
|
|
|
|
2019-08-22 09:57:38 +02:00
|
|
|
if (stop->load())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:54:23 +01:00
|
|
|
if (m_data.detail.GrainlineArrowType() == GrainlineArrowDirection::atBoth ||
|
|
|
|
m_data.detail.GrainlineArrowType() == GrainlineArrowDirection::atRear)
|
2018-12-27 14:54:29 +01:00
|
|
|
{
|
|
|
|
RotateOnAngle(angle+180);
|
2015-01-22 12:47:20 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 17:54:59 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPosition::FindBestPosition()
|
|
|
|
{
|
2020-11-26 18:11:42 +01:00
|
|
|
if (stop->load())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
if (not m_data.followGrainline || not m_data.detail.IsGrainlineEnabled())
|
|
|
|
{
|
|
|
|
// We should use copy of the detail.
|
|
|
|
VLayoutPiece workDetail = m_data.detail;
|
|
|
|
|
|
|
|
int dEdge = m_data.i;// For mirror detail edge will be different
|
|
|
|
if (CheckCombineEdges(workDetail, m_data.j, dEdge))
|
|
|
|
{
|
2020-11-26 18:11:42 +01:00
|
|
|
if (stop->load())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-26 18:10:09 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_CANDIDATE_BEST
|
|
|
|
DumpFrame(m_data.gContour, workDetail, m_data.mutex, m_data.details);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
SaveCandidate(m_bestResult, workDetail, m_data.j, dEdge, BestFrom::Combine);
|
|
|
|
}
|
|
|
|
|
2019-08-22 09:57:38 +02:00
|
|
|
if (stop->load())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
if (m_data.rotate)
|
|
|
|
{
|
2019-03-29 18:52:37 +01:00
|
|
|
Rotate(m_data.rotationNumber);
|
2019-03-26 17:54:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FollowGrainline();
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 18:10:09 +01:00
|
|
|
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPosition::DumpFrame(const VContour &contour, const VLayoutPiece &detail, QMutex *mutex,
|
|
|
|
const QVector<VLayoutPiece> &details = QVector<VLayoutPiece>())
|
|
|
|
{
|
|
|
|
auto Bias = [](int length, int maxLength)
|
|
|
|
{
|
|
|
|
return length < maxLength && length*2 < maxLength ? length : maxLength-length;
|
|
|
|
};
|
|
|
|
|
|
|
|
const int biasWidth = Bias(contour.GetWidth(), QIMAGE_MAX);
|
|
|
|
const int biasHeight = Bias(contour.GetHeight(), QIMAGE_MAX);
|
|
|
|
|
|
|
|
QPicture picture;
|
|
|
|
QPainter paint;
|
|
|
|
paint.begin(&picture);
|
|
|
|
|
|
|
|
paint.setPen(QPen(Qt::black, 6, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
|
|
|
QPainterPath p;
|
|
|
|
if (contour.GetContour().isEmpty())
|
|
|
|
{
|
|
|
|
p = DumpContour(contour.CutEmptySheetEdge());
|
|
|
|
p.translate(biasWidth/2, biasHeight/2);
|
|
|
|
paint.drawPath(p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p = DumpContour(contour.GetContour());
|
|
|
|
p.translate(biasWidth/2, biasHeight/2);
|
|
|
|
paint.drawPath(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SHOW_CANDIDATE
|
|
|
|
paint.setPen(QPen(Qt::darkGreen, 6, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
|
|
|
p = DumpContour(detail.GetLayoutAllowancePoints());
|
|
|
|
p.translate(biasWidth/2, biasHeight/2);
|
|
|
|
paint.drawPath(p);
|
|
|
|
#else
|
|
|
|
Q_UNUSED(detail)
|
|
|
|
Q_UNUSED(details)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ARRANGED_DETAILS
|
|
|
|
paint.setPen(QPen(Qt::blue, 2, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
|
|
|
p = DumpDetails(details);
|
|
|
|
p.translate(biasWidth/2, biasHeight/2);
|
|
|
|
paint.drawPath(p);
|
|
|
|
#else
|
|
|
|
Q_UNUSED(details)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Calculate bounding rect before draw sheet rect
|
|
|
|
const QRect pictureRect = picture.boundingRect();
|
|
|
|
|
|
|
|
// Sheet
|
|
|
|
#ifdef SHOW_SHEET
|
|
|
|
paint.setPen(QPen(Qt::darkRed, 15, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
|
|
|
paint.drawRect(QRectF(biasWidth/2, biasHeight/2, contour.GetWidth(), contour.GetHeight()));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
paint.end();
|
|
|
|
|
|
|
|
// Dump frame to image
|
|
|
|
// Note. If program was build with Address Sanitizer possible crashes. Address Sanitizer doesn't support big
|
|
|
|
// allocations. See page https://bitbucket.org/dismine/valentina/wiki/developers/Address_Sanitizer
|
|
|
|
QImage frameImage(pictureRect.width()+biasWidth, pictureRect.height()+biasHeight, QImage::Format_RGB32);
|
|
|
|
|
|
|
|
if (frameImage.isNull())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
frameImage.fill(Qt::white);
|
|
|
|
|
|
|
|
QPainter paintFrameImage;
|
|
|
|
paintFrameImage.begin(&frameImage);
|
|
|
|
paintFrameImage.drawPicture(0, 0, picture);
|
|
|
|
paintFrameImage.end();
|
|
|
|
|
|
|
|
QMutexLocker locker(mutex);
|
|
|
|
|
|
|
|
static int frame = 0;
|
|
|
|
++frame;
|
|
|
|
|
|
|
|
const QString path = QDir::homePath()+QStringLiteral("/LayoutDebug/%1.png").arg(frame);
|
|
|
|
frameImage.save (path);
|
|
|
|
}
|
|
|
|
#endif
|