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
|
2015-01-21 22:49:38 +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 "vposition.h"
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QDir>
|
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 <QStaticStringData>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringData>
|
|
|
|
#include <QStringDataPtr>
|
|
|
|
#include <Qt>
|
|
|
|
|
|
|
|
#include "../vmisc/def.h"
|
2016-08-16 18:57:32 +02:00
|
|
|
#include "../vmisc/vmath.h"
|
2015-01-21 22:49:38 +01:00
|
|
|
|
2018-12-28 12:19:41 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-12-28 21:40:19 +01:00
|
|
|
#ifdef SHOW_DIRECTION
|
2018-12-28 12:19:41 +01:00
|
|
|
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;
|
|
|
|
}
|
2018-12-28 21:40:19 +01:00
|
|
|
#endif
|
2018-12-28 12:19:41 +01:00
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
2018-12-28 12:19:41 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPainterPath DrawContour(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;
|
|
|
|
}
|
2019-03-26 17:54:59 +01:00
|
|
|
#endif
|
2018-12-28 12:19:41 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-12-28 21:40:19 +01:00
|
|
|
#ifdef ARRANGED_DETAILS
|
2018-12-28 12:19:41 +01:00
|
|
|
QPainterPath DrawDetails(const QVector<VLayoutPiece> &details)
|
|
|
|
{
|
|
|
|
QPainterPath path;
|
|
|
|
path.setFillRule(Qt::WindingFill);
|
|
|
|
if (details.count() > 0)
|
|
|
|
{
|
|
|
|
for (auto &detail : details)
|
|
|
|
{
|
|
|
|
path.addPath(detail.ContourPath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
2018-12-28 21:40:19 +01:00
|
|
|
#endif
|
2018-12-28 12:19:41 +01:00
|
|
|
|
|
|
|
} //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)
|
2017-02-22 13:04:47 +01:00
|
|
|
: QRunnable(),
|
2019-03-26 17:54:59 +01:00
|
|
|
m_bestResult(VBestSquare(data.gContour.GetSize(), saveLength)),
|
|
|
|
m_data(data),
|
|
|
|
#ifdef LAYOUT_DEBUG
|
2017-02-22 13:04:47 +01:00
|
|
|
paperIndex(0),
|
|
|
|
frame(0),
|
|
|
|
detailsCount(0),
|
|
|
|
details(),
|
2019-03-26 17:54:59 +01:00
|
|
|
#endif
|
2017-02-22 13:04:47 +01:00
|
|
|
stop(stop),
|
2019-03-26 17:54:59 +01:00
|
|
|
angle_between(0)
|
2015-01-22 15:11:50 +01:00
|
|
|
{
|
2019-03-26 17:54:59 +01:00
|
|
|
if (not (m_data.rotationIncrease >= 1 && m_data.rotationIncrease <= 180 && 360 % m_data.rotationIncrease == 0))
|
2015-01-22 15:11:50 +01:00
|
|
|
{
|
2019-03-26 17:54:59 +01:00
|
|
|
m_data.rotationIncrease = 180;
|
2015-01-22 15:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-21 22:49:38 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPosition::run()
|
|
|
|
{
|
2017-02-22 13:04:47 +01:00
|
|
|
if (stop->load())
|
2015-01-22 12:39:04 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
FindBestPosition();
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
2015-01-21 22:49:38 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2015-01-21 22:49:38 +01:00
|
|
|
quint32 VPosition::getPaperIndex() const
|
|
|
|
{
|
|
|
|
return paperIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-12-28 12:08:53 +01:00
|
|
|
void VPosition::setPaperIndex(quint32 value)
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
|
|
|
paperIndex = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2015-01-21 22:49:38 +01:00
|
|
|
quint32 VPosition::getFrame() const
|
|
|
|
{
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-12-28 12:08:53 +01:00
|
|
|
void VPosition::setFrame(quint32 value)
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
|
|
|
frame = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2015-01-21 22:49:38 +01:00
|
|
|
quint32 VPosition::getDetailsCount() const
|
|
|
|
{
|
|
|
|
return detailsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-12-28 12:08:53 +01:00
|
|
|
void VPosition::setDetailsCount(quint32 value)
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
|
|
|
detailsCount = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
void VPosition::setDetails(const QVector<VLayoutPiece> &details)
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
|
|
|
this->details = details;
|
|
|
|
}
|
2019-03-26 17:54:59 +01:00
|
|
|
#endif
|
2015-01-21 22:49:38 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VBestSquare VPosition::getBestResult() const
|
|
|
|
{
|
2019-03-26 17:54:59 +01:00
|
|
|
return m_bestResult;
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
2015-01-21 22:49:38 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
void VPosition::DrawDebug(const VContour &contour, const VLayoutPiece &detail, int frame, quint32 paperIndex,
|
|
|
|
int detailsCount, const QVector<VLayoutPiece> &details)
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2015-05-06 14:03:45 +02:00
|
|
|
const int biasWidth = Bias(contour.GetWidth(), QIMAGE_MAX);
|
|
|
|
const int biasHeight = Bias(contour.GetHeight(), QIMAGE_MAX);
|
|
|
|
|
2015-05-11 09:48:57 +02:00
|
|
|
QPicture picture;
|
2015-01-21 22:49:38 +01:00
|
|
|
QPainter paint;
|
2015-05-11 09:48:57 +02:00
|
|
|
paint.begin(&picture);
|
2015-01-21 22:49:38 +01:00
|
|
|
|
2015-03-23 13:13:44 +01:00
|
|
|
paint.setPen(QPen(Qt::black, 6, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
2015-01-21 22:49:38 +01:00
|
|
|
QPainterPath p;
|
|
|
|
if (contour.GetContour().isEmpty())
|
|
|
|
{
|
2016-03-27 21:53:41 +02:00
|
|
|
p = DrawContour(contour.CutEmptySheetEdge());
|
2015-05-06 14:03:45 +02:00
|
|
|
p.translate(biasWidth/2, biasHeight/2);
|
2015-01-21 22:49:38 +01:00
|
|
|
paint.drawPath(p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p = DrawContour(contour.GetContour());
|
2015-05-06 14:03:45 +02:00
|
|
|
p.translate(biasWidth/2, biasHeight/2);
|
2015-01-21 22:49:38 +01:00
|
|
|
paint.drawPath(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SHOW_CANDIDATE
|
2015-03-23 13:13:44 +01:00
|
|
|
paint.setPen(QPen(Qt::darkGreen, 6, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
2017-01-25 12:29:03 +01:00
|
|
|
p = DrawContour(detail.GetLayoutAllowancePoints());
|
2015-05-06 14:03:45 +02:00
|
|
|
p.translate(biasWidth/2, biasHeight/2);
|
2015-01-21 22:49:38 +01:00
|
|
|
paint.drawPath(p);
|
2015-03-16 13:23:02 +01:00
|
|
|
#else
|
|
|
|
Q_UNUSED(detail)
|
|
|
|
Q_UNUSED(details)
|
2015-01-21 22:49:38 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ARRANGED_DETAILS
|
2015-03-23 13:13:44 +01:00
|
|
|
paint.setPen(QPen(Qt::blue, 2, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
2015-01-21 22:49:38 +01:00
|
|
|
p = DrawDetails(details);
|
2015-05-06 14:03:45 +02:00
|
|
|
p.translate(biasWidth/2, biasHeight/2);
|
2015-01-21 22:49:38 +01:00
|
|
|
paint.drawPath(p);
|
2015-03-16 13:23:02 +01:00
|
|
|
#else
|
|
|
|
Q_UNUSED(details)
|
2015-01-21 22:49:38 +01:00
|
|
|
#endif
|
|
|
|
|
2015-05-11 09:48:57 +02:00
|
|
|
// Calculate bounding rect before draw sheet rect
|
|
|
|
const QRect pictureRect = picture.boundingRect();
|
|
|
|
|
|
|
|
// Sheet
|
2015-05-15 17:16:15 +02:00
|
|
|
#ifdef SHOW_SHEET
|
2015-05-11 09:48:57 +02:00
|
|
|
paint.setPen(QPen(Qt::darkRed, 15, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
|
|
|
paint.drawRect(QRectF(biasWidth/2, biasHeight/2, contour.GetWidth(), contour.GetHeight()));
|
2015-05-15 17:16:15 +02:00
|
|
|
#endif
|
2015-05-11 09:48:57 +02:00
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
paint.end();
|
2015-05-11 09:48:57 +02:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2018-12-28 12:08:53 +01:00
|
|
|
const QString path = QDir::homePath()+QStringLiteral("/LayoutDebug/%1_%2_%3.png").arg(paperIndex)
|
2015-01-21 22:49:38 +01:00
|
|
|
.arg(detailsCount).arg(frame);
|
|
|
|
frameImage.save (path);
|
|
|
|
}
|
|
|
|
|
2015-05-06 14:03:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VPosition::Bias(int length, int maxLength)
|
|
|
|
{
|
2018-12-28 12:08:53 +01:00
|
|
|
return length < maxLength && length*2 < maxLength ? length : maxLength-length;
|
2015-05-06 14:03:45 +02:00
|
|
|
}
|
2019-03-26 17:54:59 +01:00
|
|
|
#endif
|
2015-05-06 14:03:45 +02:00
|
|
|
|
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);
|
2015-01-21 22:49:38 +01:00
|
|
|
newGContour.append(newGContour.first());
|
2015-05-08 12:10:56 +02:00
|
|
|
const QSizeF size = QPolygonF(newGContour).boundingRect().size();
|
2019-03-27 08:31:02 +01:00
|
|
|
const qreal depthPosition = m_data.gContour.IsPortrait() ? detail.DetailBoundingRect().y() :
|
|
|
|
detail.DetailBoundingRect().x();
|
|
|
|
|
|
|
|
VBestSquareResData data;
|
|
|
|
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;
|
|
|
|
|
|
|
|
bestResult.NewResult(data);
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
bool VPosition::CheckCombineEdges(VLayoutPiece &detail, int j, int &dEdge)
|
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);
|
|
|
|
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_COMBINE
|
|
|
|
DrawDebug(gContour, detail, frame, paperIndex, detailsCount, details);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CrossingType type = CrossingType::Intersection;
|
2017-12-17 14:06:46 +01:00
|
|
|
if (not detail.IsForceFlipping() && SheetContains(detail.DetailBoundingRect()))
|
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
|
|
|
{
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
#ifdef SHOW_MIRROR
|
|
|
|
DrawDebug(gContour, detail, frame+1, paperIndex, detailsCount, details);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
if (m_data.gContour.GetContour().isEmpty())
|
2016-12-10 17:38:34 +01:00
|
|
|
{
|
|
|
|
dEdge = detail.DetailEdgeByPoint(globalEdge.p2());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dEdge = detail.LayoutEdgeByPoint(globalEdge.p2());
|
|
|
|
}
|
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
if (dEdge <= 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CrossingType type = CrossingType::Intersection;
|
2016-12-10 17:38:34 +01:00
|
|
|
if (SheetContains(detail.DetailBoundingRect()))
|
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
|
|
|
bool VPosition::CheckRotationEdges(VLayoutPiece &detail, int j, int dEdge, qreal angle) const
|
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);
|
|
|
|
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
#ifdef SHOW_ROTATION
|
|
|
|
DrawDebug(gContour, detail, frame, paperIndex, detailsCount, details);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CrossingType type = CrossingType::Intersection;
|
2016-12-10 17:38:34 +01:00
|
|
|
if (SheetContains(detail.DetailBoundingRect()))
|
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
|
|
|
{
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_CANDIDATE_BEST
|
|
|
|
++frame;
|
|
|
|
DrawDebug(gContour, workDetail, frame, paperIndex, detailsCount, 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
|
|
|
}
|
2019-03-26 17:54:59 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
2018-12-27 14:54:29 +01:00
|
|
|
++frame;
|
2019-03-26 17:54:59 +01:00
|
|
|
#endif
|
2018-12-27 14:54:29 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
VPosition::CrossingType VPosition::Crossing(const VLayoutPiece &detail) const
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2019-03-26 17:54:59 +01:00
|
|
|
const QRectF gRect = m_data.gContour.BoundingRect();
|
2016-06-21 15:04:15 +02:00
|
|
|
if (not gRect.intersects(detail.LayoutBoundingRect()) && not gRect.contains(detail.DetailBoundingRect()))
|
2016-03-28 10:22:15 +02:00
|
|
|
{
|
|
|
|
// This we can determine efficiently.
|
|
|
|
return CrossingType::NoIntersection;
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
const QPainterPath gPath = m_data.gContour.ContourPath();
|
2018-12-28 12:08:53 +01:00
|
|
|
CrossingType crossing = CrossingType::EdgeError;
|
2017-01-25 12:29:03 +01:00
|
|
|
if (not gPath.intersects(detail.LayoutAllowancePath()) && not gPath.contains(detail.ContourPath()))
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2018-12-28 12:08:53 +01:00
|
|
|
crossing = CrossingType::NoIntersection;
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-28 12:08:53 +01:00
|
|
|
crossing = CrossingType::Intersection;
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
2018-12-28 12:08:53 +01:00
|
|
|
return crossing;
|
2015-01-21 22:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VPosition::SheetContains(const QRectF &rect) const
|
|
|
|
{
|
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
|
|
|
{
|
2016-12-10 17:38:34 +01:00
|
|
|
QLineF detailEdge;
|
2019-03-26 17:54:59 +01:00
|
|
|
if (m_data.gContour.GetContour().isEmpty())
|
2016-12-10 17:38:34 +01:00
|
|
|
{
|
|
|
|
detailEdge = detail.DetailEdge(dEdge);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-12-27 14:54:29 +01:00
|
|
|
void VPosition::RotateEdges(VLayoutPiece &detail, const QLineF &globalEdge, int dEdge, qreal angle) const
|
2015-01-21 22:49:38 +01:00
|
|
|
{
|
2016-12-10 17:38:34 +01:00
|
|
|
QLineF detailEdge;
|
2019-03-26 17:54:59 +01:00
|
|
|
if (m_data.gContour.GetContour().isEmpty())
|
2016-12-10 17:38:34 +01:00
|
|
|
{
|
|
|
|
detailEdge = detail.DetailEdge(dEdge);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
|
|
|
|
// Now we move detail to position near to global contour edge.
|
|
|
|
detail.Translate(dx, dy);
|
|
|
|
detail.Rotate(globalEdge.p2(), angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-22 12:47:20 +01:00
|
|
|
void VPosition::Rotate(int increase)
|
|
|
|
{
|
2015-05-15 17:16:15 +02:00
|
|
|
int startAngle = 0;
|
2016-03-23 15:09:30 +01:00
|
|
|
if (VFuzzyComparePossibleNulls(angle_between, 360))
|
2015-05-15 17:16:15 +02:00
|
|
|
{
|
|
|
|
startAngle = increase;
|
|
|
|
}
|
|
|
|
for (int angle = startAngle; angle < 360; angle = angle+increase)
|
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);
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2018-12-27 14:54:29 +01:00
|
|
|
const qreal angle = detailGrainline.angleTo(FabricGrainline());
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
if (m_data.detail.GrainlineArrowType() == ArrowType::atBoth ||
|
|
|
|
m_data.detail.GrainlineArrowType() == ArrowType::atFront)
|
2018-12-27 14:54:29 +01:00
|
|
|
{
|
|
|
|
RotateOnAngle(angle);
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:54:59 +01:00
|
|
|
if (m_data.detail.GrainlineArrowType() == ArrowType::atBoth ||
|
|
|
|
m_data.detail.GrainlineArrowType() == ArrowType::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()
|
|
|
|
{
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_CANDIDATE_BEST
|
|
|
|
DrawDebug(gContour, workDetail, frame+2, paperIndex, detailsCount, details);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SaveCandidate(m_bestResult, workDetail, m_data.j, dEdge, BestFrom::Combine);
|
|
|
|
}
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
frame = frame + 3;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (m_data.rotate)
|
|
|
|
{
|
|
|
|
Rotate(m_data.rotationIncrease);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_data.gContour.GetContour().isEmpty())
|
|
|
|
{
|
|
|
|
Rotate(m_data.rotationIncrease);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FollowGrainline();
|
|
|
|
}
|
|
|
|
}
|