2016-03-16 19:07:50 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vabstractcubicbezierpath.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 16 3, 2016
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2016-03-16 19:07:50 +01:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2016 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2016-03-16 19:07:50 +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 "vabstractcubicbezierpath.h"
|
2018-11-13 16:03:35 +01:00
|
|
|
#include "vsplinepoint.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include <QPainterPath>
|
2021-07-24 10:15:48 +02:00
|
|
|
#include <QtDebug>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
2016-03-16 19:07:50 +01:00
|
|
|
#include "../ifc/exception/vexception.h"
|
2023-10-07 17:56:39 +02:00
|
|
|
#include "../ifc/ifcdef.h"
|
|
|
|
#include "../vmisc/def.h"
|
|
|
|
#include "vabstractapplication.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "vpointf.h"
|
2016-03-16 19:07:50 +01:00
|
|
|
#include "vspline.h"
|
2023-10-07 17:56:39 +02:00
|
|
|
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
|
|
|
|
#include "../vmisc/compatibility.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace Qt::Literals::StringLiterals;
|
2016-03-16 19:07:50 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VAbstractCubicBezierPath::VAbstractCubicBezierPath(const GOType &type, const quint32 &idObject, const Draw &mode)
|
2023-10-07 17:56:39 +02:00
|
|
|
: VAbstractBezier(type, idObject, mode)
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
auto VAbstractCubicBezierPath::operator=(const VAbstractCubicBezierPath &curve) -> VAbstractCubicBezierPath &
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
if (&curve == this)
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2016-09-24 19:44:06 +02:00
|
|
|
VAbstractBezier::operator=(curve);
|
2016-03-16 19:07:50 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VAbstractCubicBezierPath::~VAbstractCubicBezierPath()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief GetPath return QPainterPath which reprezent spline path.
|
|
|
|
* @return path.
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
auto VAbstractCubicBezierPath::GetPath() const -> QPainterPath
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
|
|
|
QPainterPath painterPath;
|
|
|
|
for (qint32 i = 1; i <= CountSubSpl(); ++i)
|
|
|
|
{
|
2017-06-23 13:18:31 +02:00
|
|
|
painterPath.addPath(GetSpline(i).GetPath());
|
2016-03-16 19:07:50 +01:00
|
|
|
}
|
|
|
|
return painterPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief GetPathPoints return list of points what located on path.
|
|
|
|
* @return list.
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
auto VAbstractCubicBezierPath::GetPoints() const -> QVector<QPointF>
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
|
|
|
QVector<QPointF> pathPoints;
|
|
|
|
for (qint32 i = 1; i <= CountSubSpl(); ++i)
|
|
|
|
{
|
2017-08-03 12:43:25 +02:00
|
|
|
if (not pathPoints.isEmpty())
|
|
|
|
{
|
|
|
|
pathPoints.removeLast();
|
|
|
|
}
|
|
|
|
|
2016-03-16 19:07:50 +01:00
|
|
|
pathPoints += GetSpline(i).GetPoints();
|
|
|
|
}
|
|
|
|
return pathPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief GetLength return length of spline path.
|
|
|
|
* @return length.
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
auto VAbstractCubicBezierPath::GetLength() const -> qreal
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
|
|
|
qreal length = 0;
|
|
|
|
for (qint32 i = 1; i <= CountSubSpl(); ++i)
|
|
|
|
{
|
|
|
|
length += GetSpline(i).GetLength();
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
auto VAbstractCubicBezierPath::DirectionArrows() const -> QVector<DirectionArrow>
|
2017-06-23 13:18:31 +02:00
|
|
|
{
|
|
|
|
QVector<DirectionArrow> arrows;
|
|
|
|
for (qint32 i = 1; i <= CountSubSpl(); ++i)
|
|
|
|
{
|
|
|
|
arrows += GetSpline(i).DirectionArrows();
|
|
|
|
}
|
|
|
|
return arrows;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
auto VAbstractCubicBezierPath::Segment(const QPointF &p) const -> int
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
|
|
|
int index = -1;
|
|
|
|
for (qint32 i = 1; i <= CountSubSpl(); ++i)
|
|
|
|
{
|
|
|
|
const qreal t = GetSpline(i).ParamT(p);
|
2017-08-21 20:20:42 +02:00
|
|
|
if (qFuzzyIsNull(t) || not qFuzzyCompare(t, -1))
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief CutSplinePath cut spline path into two. This method don't return two spline path. You must create spline
|
|
|
|
* paths by yourself.
|
|
|
|
* Example:
|
|
|
|
* QPointF spl1p2, spl1p3, spl2p2, spl2p3;
|
|
|
|
* qint32 p1 = 0, p2 = 0;
|
|
|
|
* QPointF point = splPath->CutSplinePath(length, p1, p2, spl1p2, spl1p3, spl2p2, spl2p3);
|
|
|
|
*
|
|
|
|
* VSplinePoint splP1 = splPath->at(p1);
|
|
|
|
* VSplinePoint splP2 = splPath->at(p2);
|
|
|
|
* VSpline spl1 = VSpline(splP1.P(), spl1p2, spl1p3, *p, splPath->GetKCurve());
|
|
|
|
* VSpline spl2 = VSpline(*p, spl2p2, spl2p3, splP2.P(), splPath->GetKCurve());
|
|
|
|
* @param length length first spline path.
|
|
|
|
* @param p1 index first spline point in list.
|
|
|
|
* @param p2 index second spline point in list.
|
|
|
|
* @param spl1p2 first control point first spline.
|
|
|
|
* @param spl1p3 second control point first spline.
|
|
|
|
* @param spl2p2 first control point second spline.
|
|
|
|
* @param spl2p3 second control point second spline.
|
2022-05-20 14:49:15 +02:00
|
|
|
* @param pointName cutting point name.
|
2016-03-16 19:07:50 +01:00
|
|
|
* @return cutting point.
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
auto VAbstractCubicBezierPath::CutSplinePath(qreal length, qint32 &p1, qint32 &p2, QPointF &spl1p2, QPointF &spl1p3,
|
2023-10-07 17:56:39 +02:00
|
|
|
QPointF &spl2p2, QPointF &spl2p3, const QString &pointName) const
|
|
|
|
-> QPointF
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
|
|
|
if (CountSubSpl() < 1)
|
|
|
|
{
|
|
|
|
throw VException(tr("Can't cut this spline"));
|
|
|
|
}
|
|
|
|
|
2023-10-07 17:56:39 +02:00
|
|
|
// Always need return two spline paths, so we must correct wrong length.
|
2016-03-16 19:07:50 +01:00
|
|
|
qreal fullLength = GetLength();
|
2016-04-02 19:47:04 +02:00
|
|
|
|
|
|
|
if (fullLength <= minLength)
|
|
|
|
{
|
|
|
|
p1 = p2 = -1;
|
|
|
|
spl1p2 = spl1p3 = spl2p2 = spl2p3 = QPointF();
|
2021-07-24 10:15:48 +02:00
|
|
|
|
2022-05-20 16:08:32 +02:00
|
|
|
const QString errorMsg = tr("Unable to cut curve '%1'. The curve is too short.").arg(name());
|
2023-10-07 17:56:39 +02:00
|
|
|
VAbstractApplication::VApp()->IsPedantic()
|
|
|
|
? throw VException(errorMsg)
|
|
|
|
: qWarning() << VAbstractApplication::warningMessageSignature + errorMsg;
|
2021-07-24 10:15:48 +02:00
|
|
|
|
2022-05-20 14:49:15 +02:00
|
|
|
return {};
|
2016-04-02 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const qreal maxLength = fullLength - minLength;
|
|
|
|
|
|
|
|
if (length < minLength)
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
2016-04-02 19:47:04 +02:00
|
|
|
length = minLength;
|
2021-07-24 10:15:48 +02:00
|
|
|
|
2022-05-20 14:49:15 +02:00
|
|
|
QString errorMsg;
|
|
|
|
if (not pointName.isEmpty())
|
|
|
|
{
|
2022-05-20 16:08:32 +02:00
|
|
|
errorMsg = tr("Curve '%1'. Length of a cut segment (%2) is too small. Optimize it to minimal value.")
|
2023-10-07 17:56:39 +02:00
|
|
|
.arg(name(), pointName);
|
2022-05-20 14:49:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
errorMsg =
|
|
|
|
tr("Curve '%1'. Length of a cut segment is too small. Optimize it to minimal value.").arg(name());
|
2022-05-20 14:49:15 +02:00
|
|
|
}
|
2023-10-07 17:56:39 +02:00
|
|
|
VAbstractApplication::VApp()->IsPedantic()
|
|
|
|
? throw VException(errorMsg)
|
|
|
|
: qWarning() << VAbstractApplication::warningMessageSignature + errorMsg;
|
2016-03-16 19:07:50 +01:00
|
|
|
}
|
2016-04-02 19:47:04 +02:00
|
|
|
else if (length > maxLength)
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
2016-04-02 19:47:04 +02:00
|
|
|
length = maxLength;
|
2021-07-24 10:15:48 +02:00
|
|
|
|
2022-05-20 14:49:15 +02:00
|
|
|
QString errorMsg;
|
|
|
|
if (not pointName.isEmpty())
|
|
|
|
{
|
2022-05-20 16:08:32 +02:00
|
|
|
errorMsg = tr("Curve '%1'. Length of a cut segment (%2) is too big. Optimize it to maximal value.")
|
2023-10-07 17:56:39 +02:00
|
|
|
.arg(name(), pointName);
|
2022-05-20 14:49:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
errorMsg = tr("Curve '%1'. Length of a cut segment is too big. Optimize it to maximal value.").arg(name());
|
2022-05-20 14:49:15 +02:00
|
|
|
}
|
2023-10-07 17:56:39 +02:00
|
|
|
VAbstractApplication::VApp()->IsPedantic()
|
|
|
|
? throw VException(errorMsg)
|
|
|
|
: qWarning() << VAbstractApplication::warningMessageSignature + errorMsg;
|
2016-03-16 19:07:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fullLength = 0;
|
|
|
|
for (qint32 i = 1; i <= CountSubSpl(); ++i)
|
|
|
|
{
|
|
|
|
const VSpline spl = GetSpline(i);
|
2016-04-02 19:47:04 +02:00
|
|
|
const qreal splLength = spl.GetLength();
|
|
|
|
fullLength += splLength;
|
2016-03-16 19:07:50 +01:00
|
|
|
if (fullLength > length)
|
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
p1 = i - 1;
|
2016-03-16 19:07:50 +01:00
|
|
|
p2 = i;
|
2023-10-07 17:56:39 +02:00
|
|
|
const QPointF point =
|
|
|
|
spl.CutSpline(length - (fullLength - splLength), spl1p2, spl1p3, spl2p2, spl2p3, pointName);
|
2018-11-13 16:03:35 +01:00
|
|
|
|
|
|
|
const QVector<VSplinePoint> points = GetSplinePath();
|
|
|
|
|
|
|
|
if (p1 > 0)
|
|
|
|
{
|
2022-05-20 16:08:32 +02:00
|
|
|
const VSplinePoint &splP1 = points.at(p1);
|
2018-11-13 16:03:35 +01:00
|
|
|
QLineF line(splP1.P().toQPointF(), spl1p2);
|
|
|
|
if (qFuzzyIsNull(line.length()))
|
|
|
|
{
|
|
|
|
spl1p2.rx() += ToPixel(0.1, Unit::Mm);
|
|
|
|
QLineF line(splP1.P().toQPointF(), spl1p2);
|
|
|
|
line.setLength(ToPixel(0.1, Unit::Mm));
|
2023-10-07 17:56:39 +02:00
|
|
|
line.setAngle(splP1.Angle1() + 180);
|
2018-11-13 16:03:35 +01:00
|
|
|
spl1p2 = line.p2();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p2 < points.size() - 1)
|
|
|
|
{
|
2022-05-20 16:08:32 +02:00
|
|
|
const VSplinePoint &splP2 = points.at(p2);
|
2018-11-13 16:03:35 +01:00
|
|
|
QLineF line(splP2.P().toQPointF(), spl2p3);
|
|
|
|
if (qFuzzyIsNull(line.length()))
|
|
|
|
{
|
|
|
|
spl2p3.rx() += ToPixel(0.1, Unit::Mm);
|
|
|
|
QLineF line(splP2.P().toQPointF(), spl2p3);
|
2023-10-07 17:56:39 +02:00
|
|
|
line.setAngle(splP2.Angle2() + 180);
|
2018-11-13 16:03:35 +01:00
|
|
|
spl2p3 = line.p2();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return point;
|
2016-03-16 19:07:50 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-02 19:47:04 +02:00
|
|
|
p1 = p2 = -1;
|
|
|
|
spl1p2 = spl1p3 = spl2p2 = spl2p3 = QPointF();
|
2022-05-20 16:08:32 +02:00
|
|
|
return {};
|
2016-03-16 19:07:50 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 19:12:48 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief NameForHistory helps to create name for dialog History.
|
|
|
|
* @param toolName first part of name. Like 'Spline path' or 'Cubic Bezier path'.
|
|
|
|
* @return name of curve for history records.
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
auto VAbstractCubicBezierPath::NameForHistory(const QString &toolName) const -> QString
|
2016-03-17 19:12:48 +01:00
|
|
|
{
|
|
|
|
QString name = toolName;
|
|
|
|
if (CountPoints() > 0)
|
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
name += u" %1"_s.arg(FirstPoint().name());
|
2016-03-17 19:12:48 +01:00
|
|
|
if (CountSubSpl() >= 1)
|
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
name += u"_%1"_s.arg(LastPoint().name());
|
2016-03-17 19:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GetDuplicate() > 0)
|
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
name += u"_%1"_s.arg(GetDuplicate());
|
2016-03-17 19:12:48 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-04 13:50:17 +01:00
|
|
|
|
|
|
|
QString alias;
|
|
|
|
|
|
|
|
if (not GetAliasSuffix().isEmpty())
|
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
alias = u"%1 %2"_s.arg(toolName, GetAliasSuffix());
|
2020-11-04 13:50:17 +01:00
|
|
|
}
|
|
|
|
|
2023-10-07 17:56:39 +02:00
|
|
|
return not alias.isEmpty() ? u"%1 (%2)"_s.arg(alias, name) : name;
|
2016-03-17 19:12:48 +01:00
|
|
|
}
|
|
|
|
|
2016-03-16 19:07:50 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractCubicBezierPath::CreateName()
|
|
|
|
{
|
|
|
|
QString name;
|
|
|
|
if (CountPoints() > 0)
|
|
|
|
{
|
|
|
|
name = splPath;
|
2023-10-07 17:56:39 +02:00
|
|
|
name.append(u"_%1"_s.arg(FirstPoint().name()));
|
2016-03-22 16:25:16 +01:00
|
|
|
if (CountSubSpl() >= 1)
|
2016-03-16 19:07:50 +01:00
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
name.append(u"_%1"_s.arg(LastPoint().name()));
|
2016-03-16 19:07:50 +01:00
|
|
|
|
2016-03-19 15:20:09 +01:00
|
|
|
if (GetDuplicate() > 0)
|
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
name += u"_%1"_s.arg(GetDuplicate());
|
2016-03-19 15:20:09 +01:00
|
|
|
}
|
2016-03-16 19:07:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setName(name);
|
|
|
|
}
|
2020-11-04 10:01:20 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractCubicBezierPath::CreateAlias()
|
|
|
|
{
|
|
|
|
const QString aliasSuffix = GetAliasSuffix();
|
|
|
|
if (aliasSuffix.isEmpty())
|
|
|
|
{
|
|
|
|
SetAlias(QString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-13 14:26:19 +02:00
|
|
|
SetAlias(splPath + '_'_L1 + aliasSuffix);
|
2020-11-04 10:01:20 +01:00
|
|
|
}
|