2015-01-21 19:56:59 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vcontour.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 21 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 19:56:59 +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 19:56:59 +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 "vcontour.h"
|
|
|
|
|
|
|
|
#include <QLineF>
|
2016-03-28 10:53:02 +02:00
|
|
|
#include <QPainterPath>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QPoint>
|
|
|
|
#include <QPointF>
|
|
|
|
#include <QPolygonF>
|
|
|
|
#include <QRectF>
|
|
|
|
#include <Qt>
|
|
|
|
|
|
|
|
#include "vcontour_p.h"
|
2017-01-22 10:02:02 +01:00
|
|
|
#include "vlayoutpiece.h"
|
2016-08-16 18:57:32 +02:00
|
|
|
#include "../vmisc/vmath.h"
|
2019-07-22 16:06:30 +02:00
|
|
|
#include "../vgeometry/vgeometrydef.h"
|
|
|
|
#include "../vgeometry/vgobject.h"
|
2015-01-21 19:56:59 +01:00
|
|
|
|
2019-03-27 10:45:25 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void AppendToContour(QVector<QPointF> &contour, QPointF point)
|
|
|
|
{
|
|
|
|
if (not contour.isEmpty())
|
|
|
|
{
|
|
|
|
if (not VFuzzyComparePoints(contour.last(), point))
|
|
|
|
{
|
|
|
|
contour.append(point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
contour.append(point);
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 13:42:47 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVector<QPointF> OptimizeCombining(const QVector<QPointF> &contour, const QPointF &withdrawEnd)
|
|
|
|
{
|
|
|
|
if (contour.size() < 2)
|
|
|
|
{
|
|
|
|
return contour;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF withdrawFirst = contour.last();
|
|
|
|
bool optimize = false;
|
|
|
|
int count = 0;
|
|
|
|
int cutIndex = -1;
|
|
|
|
|
|
|
|
for (int i = contour.size() - 2; i >= 0; --i)
|
|
|
|
{
|
|
|
|
if (not VGObject::IsPointOnLineSegment(contour.at(i), withdrawFirst, withdrawEnd, accuracyPointOnLine*2))
|
|
|
|
{
|
|
|
|
optimize = true;
|
|
|
|
cutIndex = i+1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optimize && count > 0)
|
|
|
|
{
|
|
|
|
return contour.mid(0, cutIndex+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return contour;
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 10:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 19:56:59 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VContour::VContour()
|
|
|
|
:d(new VContourData())
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2019-03-27 08:03:19 +01:00
|
|
|
VContour::VContour(int height, int width, qreal layoutWidth)
|
|
|
|
:d(new VContourData(height, width, layoutWidth))
|
2015-01-21 19:56:59 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VContour::VContour(const VContour &contour)
|
|
|
|
:d (contour.d)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VContour &VContour::operator=(const VContour &contour)
|
|
|
|
{
|
|
|
|
if ( &contour == this )
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
d = contour.d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VContour::~VContour()
|
|
|
|
{}
|
|
|
|
|
2019-03-27 08:03:19 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VContour::CeateEmptySheetContour()
|
|
|
|
{
|
|
|
|
if (d->globalContour.isEmpty())
|
|
|
|
{
|
|
|
|
d->globalContour = CutEmptySheetEdge();
|
|
|
|
d->globalContour.append(d->globalContour.first()); // Close path
|
2019-04-02 14:30:31 +02:00
|
|
|
|
|
|
|
ResetAttributes();
|
2019-03-27 08:03:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 19:56:59 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VContour::SetContour(const QVector<QPointF> &contour)
|
|
|
|
{
|
|
|
|
d->globalContour = contour;
|
2019-04-02 14:30:31 +02:00
|
|
|
|
|
|
|
ResetAttributes();
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVector<QPointF> VContour::GetContour() const
|
|
|
|
{
|
|
|
|
return d->globalContour;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2019-03-29 18:52:37 +01:00
|
|
|
qreal VContour::GetShift() const
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
|
|
|
return d->shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2019-03-29 18:52:37 +01:00
|
|
|
void VContour::SetShift(qreal shift)
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
|
|
|
d->shift = shift;
|
2019-04-02 14:30:31 +02:00
|
|
|
|
|
|
|
ResetAttributes();
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VContour::GetHeight() const
|
|
|
|
{
|
|
|
|
return d->paperHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VContour::SetHeight(int height)
|
|
|
|
{
|
|
|
|
d->paperHeight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VContour::GetWidth() const
|
|
|
|
{
|
|
|
|
return d->paperWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VContour::SetWidth(int width)
|
|
|
|
{
|
|
|
|
d->paperWidth = width;
|
|
|
|
}
|
|
|
|
|
2015-05-08 12:10:56 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QSizeF VContour::GetSize() const
|
|
|
|
{
|
|
|
|
return QSizeF(d->paperWidth, d->paperHeight);
|
|
|
|
}
|
|
|
|
|
2015-01-21 19:56:59 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
QVector<QPointF> VContour::UniteWithContour(const VLayoutPiece &detail, int globalI, int detJ, BestFrom type) const
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
|
|
|
QVector<QPointF> newContour;
|
2015-10-28 15:22:36 +01:00
|
|
|
if (d->globalContour.isEmpty()) //-V807
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
2016-12-10 17:38:34 +01:00
|
|
|
AppendWhole(newContour, detail, 0);
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-27 21:53:41 +02:00
|
|
|
if (globalI <= 0 || globalI > GlobalEdgesCount())
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
|
|
|
return QVector<QPointF>();
|
|
|
|
}
|
|
|
|
|
2016-12-10 17:38:34 +01:00
|
|
|
if (detJ <= 0 || detJ > detail.LayoutEdgesCount())
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
|
|
|
return QVector<QPointF>();
|
|
|
|
}
|
|
|
|
|
|
|
|
int i=0;
|
2015-03-02 18:11:43 +01:00
|
|
|
while (i < d->globalContour.count())
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
2019-07-21 09:40:41 +02:00
|
|
|
AppendToContour(newContour, d->globalContour.at(i));
|
|
|
|
|
|
|
|
if (type == BestFrom::Rotation)
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
2019-07-21 09:40:41 +02:00
|
|
|
if (i == globalI)
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
|
|
|
AppendWhole(newContour, detail, detJ);
|
|
|
|
}
|
2019-07-21 09:40:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i == globalI-1)
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
2019-03-26 18:10:13 +01:00
|
|
|
InsertDetail(newContour, detail, detJ);
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newContour;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-03-27 21:53:41 +02:00
|
|
|
int VContour::GlobalEdgesCount() const
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
2019-04-02 14:30:31 +02:00
|
|
|
return d->m_emptySheetEdgesCount;
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QLineF VContour::GlobalEdge(int i) const
|
|
|
|
{
|
2018-12-28 14:57:24 +01:00
|
|
|
QLineF edge;
|
2015-10-28 15:22:36 +01:00
|
|
|
if (d->globalContour.isEmpty()) //-V807
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
2018-12-28 14:57:24 +01:00
|
|
|
// Because sheet is blank we have one global edge for all cases.
|
2016-03-27 21:53:41 +02:00
|
|
|
if (i < 1 || i > GlobalEdgesCount())
|
2015-01-21 19:56:59 +01:00
|
|
|
{ // Doesn't exist such edge
|
2016-03-27 21:53:41 +02:00
|
|
|
return EmptySheetEdge();
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
|
2019-07-21 09:23:17 +02:00
|
|
|
const QLineF emptyEdge = EmptySheetEdge();
|
|
|
|
const qreal nShift = emptyEdge.length()/GlobalEdgesCount();
|
|
|
|
edge = IsPortrait() ? QLineF(nShift*(i-1), emptyEdge.y1(), nShift*i, emptyEdge.y2()) :
|
|
|
|
QLineF(emptyEdge.x1(), nShift*(i-1), emptyEdge.x2(), nShift*i);
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-27 21:53:41 +02:00
|
|
|
if (i < 1 || i > GlobalEdgesCount())
|
2015-01-21 19:56:59 +01:00
|
|
|
{ // Doesn't exist such edge
|
|
|
|
return QLineF();
|
|
|
|
}
|
2018-12-28 14:57:24 +01:00
|
|
|
|
2016-03-27 21:53:41 +02:00
|
|
|
if (i < GlobalEdgesCount())
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
|
|
|
edge = QLineF(d->globalContour.at(i-1), d->globalContour.at(i));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Closed countour
|
2016-03-27 21:53:41 +02:00
|
|
|
edge = QLineF(d->globalContour.at(GlobalEdgesCount()-1), d->globalContour.at(0));
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-28 14:57:24 +01:00
|
|
|
return edge;
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVector<QPointF> VContour::CutEdge(const QLineF &edge) const
|
|
|
|
{
|
|
|
|
QVector<QPointF> points;
|
2019-03-29 18:52:37 +01:00
|
|
|
if (qFuzzyIsNull(d->shift))
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
|
|
|
points.append(edge.p1());
|
|
|
|
points.append(edge.p2());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-27 21:53:41 +02:00
|
|
|
const int n = qFloor(edge.length()/d->shift);
|
|
|
|
|
|
|
|
if (n <= 0)
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
2016-03-27 21:53:41 +02:00
|
|
|
points.append(edge.p1());
|
|
|
|
points.append(edge.p2());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-02 14:30:31 +02:00
|
|
|
points.reserve(n);
|
2016-03-27 21:53:41 +02:00
|
|
|
const qreal nShift = edge.length()/n;
|
|
|
|
for (int i = 1; i <= n+1; ++i)
|
|
|
|
{
|
|
|
|
QLineF l1 = edge;
|
|
|
|
l1.setLength(nShift*(i-1));
|
|
|
|
points.append(l1.p2());
|
|
|
|
}
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-03-27 21:53:41 +02:00
|
|
|
QVector<QPointF> VContour::CutEmptySheetEdge() const
|
|
|
|
{
|
|
|
|
QVector<QPointF> points;
|
|
|
|
const qreal nShift = EmptySheetEdge().length()/GlobalEdgesCount();
|
|
|
|
for (int i = 1; i <= GlobalEdgesCount()+1; ++i)
|
|
|
|
{
|
|
|
|
QLineF l1 = EmptySheetEdge();
|
|
|
|
l1.setLength(nShift*(i-1));
|
|
|
|
points.append(l1.p2());
|
|
|
|
}
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-21 19:56:59 +01:00
|
|
|
const QPointF &VContour::at(int i) const
|
|
|
|
{
|
|
|
|
return d->globalContour.at(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
void VContour::AppendWhole(QVector<QPointF> &contour, const VLayoutPiece &detail, int detJ) const
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
|
|
|
int processedEdges = 0;
|
2016-12-10 17:38:34 +01:00
|
|
|
const int nD = detail.LayoutEdgesCount();
|
2019-07-21 09:40:41 +02:00
|
|
|
int j = detJ;
|
2019-07-21 13:42:47 +02:00
|
|
|
|
|
|
|
contour = OptimizeCombining(contour, detail.LayoutEdge(j).p2());
|
|
|
|
|
2015-01-21 19:56:59 +01:00
|
|
|
do
|
|
|
|
{
|
2019-07-21 09:40:41 +02:00
|
|
|
if (j >= nD)
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
2019-07-21 09:40:41 +02:00
|
|
|
j=0;
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
2019-07-21 09:40:41 +02:00
|
|
|
|
|
|
|
for(auto &point : CutEdge(detail.LayoutEdge(j+1)))
|
2015-01-21 19:56:59 +01:00
|
|
|
{
|
2019-07-21 09:40:41 +02:00
|
|
|
AppendToContour(contour, point);
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
|
|
|
++processedEdges;
|
|
|
|
++j;
|
2019-07-21 09:40:41 +02:00
|
|
|
}
|
|
|
|
while (processedEdges < nD);
|
2015-01-21 19:56:59 +01:00
|
|
|
}
|
2015-05-15 17:15:14 +02:00
|
|
|
|
2019-03-26 18:10:13 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VContour::InsertDetail(QVector<QPointF> &contour, const VLayoutPiece &detail, int detJ) const
|
|
|
|
{
|
|
|
|
int processedEdges = 0;
|
|
|
|
const int nD = detail.LayoutEdgesCount();
|
2019-07-21 09:40:41 +02:00
|
|
|
int j = detJ;
|
2019-07-21 13:42:47 +02:00
|
|
|
|
|
|
|
contour = OptimizeCombining(contour, detail.LayoutEdge(j).p2());
|
|
|
|
|
2019-03-26 18:10:13 +01:00
|
|
|
do
|
|
|
|
{
|
2019-07-21 09:40:41 +02:00
|
|
|
if (j >= nD)
|
2019-03-26 18:10:13 +01:00
|
|
|
{
|
2019-07-21 09:40:41 +02:00
|
|
|
j=0;
|
2019-03-26 18:10:13 +01:00
|
|
|
}
|
2019-07-21 09:40:41 +02:00
|
|
|
|
|
|
|
if (j != detJ-1)
|
2019-03-26 18:10:13 +01:00
|
|
|
{
|
2019-07-21 09:40:41 +02:00
|
|
|
for(auto &point : CutEdge(detail.LayoutEdge(j+1)))
|
2019-03-26 18:10:13 +01:00
|
|
|
{
|
|
|
|
AppendToContour(contour, point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++processedEdges;
|
|
|
|
++j;
|
|
|
|
}
|
2019-07-21 09:40:41 +02:00
|
|
|
while (processedEdges < nD);
|
2019-03-26 18:10:13 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:30:31 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VContour::ResetAttributes()
|
|
|
|
{
|
|
|
|
if (not d->globalContour.isEmpty())
|
|
|
|
{
|
|
|
|
d->m_emptySheetEdgesCount = d->globalContour.count(); // Edges count
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d->m_emptySheetEdgesCount = EmptySheetEdgesCount(); // Edges count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VContour::EmptySheetEdgesCount() const
|
|
|
|
{
|
2019-07-18 16:40:41 +02:00
|
|
|
qreal defaultShift = ToPixel(0.5, Unit::Cm);
|
|
|
|
const qreal emptyEdgeLength = EmptySheetEdge().length();
|
|
|
|
if (emptyEdgeLength < defaultShift)
|
|
|
|
{
|
|
|
|
defaultShift = emptyEdgeLength / 2.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const qreal shift = (qFuzzyIsNull(d->shift) || d->shift > defaultShift) ? defaultShift : d->shift;
|
|
|
|
return qFloor(emptyEdgeLength/shift);
|
2019-04-02 14:30:31 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 14:57:24 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VContour::IsPortrait() const
|
|
|
|
{
|
|
|
|
return d->paperHeight >= d->paperWidth;
|
|
|
|
}
|
|
|
|
|
2015-05-15 17:15:14 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QLineF VContour::EmptySheetEdge() const
|
|
|
|
{
|
2019-07-21 09:23:17 +02:00
|
|
|
const int offset = qRound(accuracyPointOnLine*4.);
|
|
|
|
const int layoutOffset = qCeil(d->layoutWidth - accuracyPointOnLine*4.);
|
2019-03-27 08:03:19 +01:00
|
|
|
return IsPortrait() ? QLineF(offset, -layoutOffset, d->paperWidth-offset, -layoutOffset) :
|
|
|
|
QLineF(-layoutOffset, offset, -layoutOffset, d->paperHeight-offset);
|
2015-05-15 17:15:14 +02:00
|
|
|
}
|