2013-11-15 13:41:26 +01:00
|
|
|
/************************************************************************
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:50:05 +01:00
|
|
|
** @file vsplinepath.cpp
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
2013-11-15 13:50:05 +01:00
|
|
|
** @date November 15, 2013
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2013 Valentina project
|
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
2013-09-18 21:16:19 +02:00
|
|
|
** 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.
|
|
|
|
**
|
2013-10-27 13:36:29 +01:00
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
2013-09-18 21:16:19 +02:00
|
|
|
** 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/>.
|
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
*************************************************************************/
|
2013-09-18 21:16:19 +02:00
|
|
|
|
2013-08-09 08:49:34 +02:00
|
|
|
#include "vsplinepath.h"
|
2013-11-06 16:49:07 +01:00
|
|
|
#include "../exception/vexception.h"
|
2013-08-09 08:49:34 +02:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-12 09:22:29 +02:00
|
|
|
VSplinePath::VSplinePath(qreal kCurve, quint32 idObject, Draw mode)
|
|
|
|
: VGObject(GOType::SplinePath, idObject, mode), path(QVector<VSplinePoint>()), kCurve(kCurve), maxCountPoints(0)
|
2014-05-02 13:11:30 +02:00
|
|
|
{}
|
2013-08-20 12:26:02 +02:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-04 21:35:15 +01:00
|
|
|
VSplinePath::VSplinePath(const VSplinePath &splPath)
|
2014-01-08 21:23:08 +01:00
|
|
|
: VGObject(splPath), path(*splPath.GetPoint()), kCurve(splPath.getKCurve()),
|
|
|
|
maxCountPoints(splPath.getMaxCountPoints())
|
2014-05-02 13:11:30 +02:00
|
|
|
{}
|
2013-08-28 10:55:11 +02:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-07 15:40:39 +01:00
|
|
|
void VSplinePath::append(const VSplinePoint &point)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-08-09 08:49:34 +02:00
|
|
|
path.append(point);
|
2014-05-13 10:08:37 +02:00
|
|
|
_name = splPath;
|
2014-02-06 15:17:06 +01:00
|
|
|
_name.append(QString("_%1").arg(path.first().P().name()));
|
|
|
|
if (path.size() > 1)
|
2013-11-12 13:48:45 +01:00
|
|
|
{
|
2014-02-06 15:17:06 +01:00
|
|
|
_name.append(QString("_%1").arg(path.last().P().name()));
|
2013-11-12 13:48:45 +01:00
|
|
|
}
|
2013-08-09 08:49:34 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-04 21:35:15 +01:00
|
|
|
qint32 VSplinePath::Count() const
|
|
|
|
{
|
|
|
|
if (path.size() == 0)
|
|
|
|
{
|
2013-08-09 08:49:34 +02:00
|
|
|
return 0;
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-09 08:49:34 +02:00
|
|
|
return path.size() - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-04 21:35:15 +01:00
|
|
|
VSpline VSplinePath::GetSpline(qint32 index) const
|
|
|
|
{
|
|
|
|
if (Count()<1)
|
|
|
|
{
|
2013-09-30 18:29:03 +02:00
|
|
|
throw VException(tr("Not enough points to create the spline."));
|
2013-08-09 08:49:34 +02:00
|
|
|
}
|
2013-11-04 21:35:15 +01:00
|
|
|
if (index < 1 || index > Count())
|
|
|
|
{
|
2013-12-19 15:30:11 +01:00
|
|
|
throw VException(tr("This spline does not exist."));
|
2013-08-09 08:49:34 +02:00
|
|
|
}
|
2014-06-09 21:11:33 +02:00
|
|
|
VSpline spl(path.at(index-1).P(), path.at(index).P(), path.at(index-1).Angle2(), path.at(index).Angle1(),
|
|
|
|
path.at(index-1).KAsm2(), path.at(index).KAsm1(), this->kCurve);
|
2013-08-09 08:49:34 +02:00
|
|
|
return spl;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-04 21:35:15 +01:00
|
|
|
QPainterPath VSplinePath::GetPath() const
|
|
|
|
{
|
2013-08-09 08:49:34 +02:00
|
|
|
QPainterPath painterPath;
|
2013-11-04 21:35:15 +01:00
|
|
|
for (qint32 i = 1; i <= Count(); ++i)
|
|
|
|
{
|
2014-06-09 21:11:33 +02:00
|
|
|
VSpline spl(path.at(i-1).P(), path.at(i).P(), path.at(i-1).Angle2(), path.at(i).Angle1(),
|
|
|
|
path.at(i-1).KAsm2(), path.at(i).KAsm1(), this->kCurve);
|
2013-08-09 08:49:34 +02:00
|
|
|
painterPath.addPath(spl.GetPath());
|
|
|
|
}
|
|
|
|
return painterPath;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-04 21:35:15 +01:00
|
|
|
QVector<QPointF> VSplinePath::GetPathPoints() const
|
|
|
|
{
|
2013-08-28 10:55:11 +02:00
|
|
|
QVector<QPointF> pathPoints;
|
2013-11-04 21:35:15 +01:00
|
|
|
for (qint32 i = 1; i <= Count(); ++i)
|
|
|
|
{
|
2014-06-09 21:11:33 +02:00
|
|
|
VSpline spl(path.at(i-1).P(), path.at(i).P(), path.at(i-1).Angle2(), path.at(i).Angle1(),
|
|
|
|
path.at(i-1).KAsm2(), path.at(i).KAsm1(), this->kCurve);
|
2014-01-29 09:53:01 +01:00
|
|
|
pathPoints += spl.GetPoints();
|
2013-08-28 10:55:11 +02:00
|
|
|
}
|
|
|
|
return pathPoints;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-04 21:35:15 +01:00
|
|
|
qreal VSplinePath::GetLength() const
|
|
|
|
{
|
2013-08-09 08:49:34 +02:00
|
|
|
qreal length = 0;
|
2013-11-04 21:35:15 +01:00
|
|
|
for (qint32 i = 1; i <= Count(); ++i)
|
|
|
|
{
|
2014-06-09 21:11:33 +02:00
|
|
|
VSpline spl(path.at(i-1).P(), path.at(i).P(), path.at(i-1).Angle2(), path.at(i).Angle1(), path.at(i-1).KAsm2(),
|
|
|
|
path.at(i).KAsm1(), kCurve);
|
2013-08-09 08:49:34 +02:00
|
|
|
length += spl.GetLength();
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-12 09:22:29 +02:00
|
|
|
void VSplinePath::UpdatePoint(qint32 indexSpline, const SplinePointPosition &pos, const VSplinePoint &point)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
|
|
|
if (indexSpline < 1 || indexSpline > Count())
|
|
|
|
{
|
2013-12-19 15:30:11 +01:00
|
|
|
throw VException(tr("This spline does not exist."));
|
2013-08-09 08:49:34 +02:00
|
|
|
}
|
2014-06-12 09:22:29 +02:00
|
|
|
if (pos == SplinePointPosition::FirstPoint)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-08-09 08:49:34 +02:00
|
|
|
path[indexSpline-1] = point;
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-09 08:49:34 +02:00
|
|
|
path[indexSpline] = point;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-12 09:22:29 +02:00
|
|
|
VSplinePoint VSplinePath::GetSplinePoint(qint32 indexSpline, SplinePointPosition pos) const
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
|
|
|
if (indexSpline < 1 || indexSpline > Count())
|
|
|
|
{
|
2013-12-19 15:30:11 +01:00
|
|
|
throw VException(tr("This spline does not exist."));
|
2013-08-09 08:49:34 +02:00
|
|
|
}
|
2014-06-12 09:22:29 +02:00
|
|
|
if (pos == SplinePointPosition::FirstPoint)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-09-12 15:25:24 +02:00
|
|
|
return path.at(indexSpline-1);
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-12 15:25:24 +02:00
|
|
|
return path.at(indexSpline);
|
2013-08-09 08:49:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-04 21:35:15 +01:00
|
|
|
VSplinePath &VSplinePath::operator =(const VSplinePath &path)
|
|
|
|
{
|
2013-12-29 17:48:57 +01:00
|
|
|
VGObject::operator=(path);
|
2013-08-09 08:49:34 +02:00
|
|
|
this->path = path.GetSplinePath();
|
|
|
|
this->kCurve = path.getKCurve();
|
2014-01-09 11:28:27 +01:00
|
|
|
this->maxCountPoints = path.getMaxCountPoints();
|
2013-08-09 08:49:34 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-06 20:27:43 +01:00
|
|
|
VSplinePoint & VSplinePath::operator[](ptrdiff_t indx)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-08-09 08:49:34 +02:00
|
|
|
return path[indx];
|
|
|
|
}
|
2013-12-18 12:13:32 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-12-29 17:48:57 +01:00
|
|
|
const VSplinePoint &VSplinePath::at(ptrdiff_t indx) const
|
|
|
|
{
|
|
|
|
return path[indx];
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-12-20 14:35:14 +01:00
|
|
|
QPointF VSplinePath::CutSplinePath(qreal length, qint32 &p1, qint32 &p2, QPointF &spl1p2, QPointF &spl1p3,
|
|
|
|
QPointF &spl2p2, QPointF &spl2p3) const
|
2013-12-18 12:13:32 +01:00
|
|
|
{
|
2013-12-31 09:44:54 +01:00
|
|
|
if (Count() < 2)
|
2013-12-18 12:13:32 +01:00
|
|
|
{
|
|
|
|
throw VException(tr("Can't cut spline path with one point"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Always need return two spline paths, so we must correct wrong length.
|
|
|
|
qreal fullLength = GetLength();
|
2013-12-31 09:44:54 +01:00
|
|
|
if (length < fullLength * 0.02)
|
2013-12-18 12:13:32 +01:00
|
|
|
{
|
|
|
|
length = fullLength * 0.02;
|
|
|
|
}
|
|
|
|
else if ( length > fullLength * 0.98)
|
|
|
|
{
|
|
|
|
length = fullLength * 0.98;
|
|
|
|
}
|
|
|
|
|
|
|
|
fullLength = 0;
|
|
|
|
for (qint32 i = 1; i <= Count(); ++i)
|
|
|
|
{
|
2014-06-09 21:11:33 +02:00
|
|
|
VSpline spl = VSpline(path.at(i-1).P(), path.at(i).P(), path.at(i-1).Angle2(), path.at(i).Angle1(),
|
|
|
|
path.at(i-1).KAsm2(), path.at(i).KAsm1(), kCurve);
|
2013-12-18 12:13:32 +01:00
|
|
|
fullLength += spl.GetLength();
|
2013-12-31 09:44:54 +01:00
|
|
|
if (fullLength > length)
|
2013-12-18 12:13:32 +01:00
|
|
|
{
|
|
|
|
p1 = i-1;
|
|
|
|
p2 = i;
|
|
|
|
return spl.CutSpline(length - (fullLength - spl.GetLength()), spl1p2, spl1p3, spl2p2, spl2p3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QPointF();
|
|
|
|
}
|
2014-05-02 13:11:30 +02:00
|
|
|
|
2014-06-02 09:43:27 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VSplinePath::name() const{return _name;}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-01-08 21:23:08 +01:00
|
|
|
qint32 VSplinePath::getMaxCountPoints() const
|
|
|
|
{
|
|
|
|
return maxCountPoints;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-01-08 21:23:08 +01:00
|
|
|
void VSplinePath::setMaxCountPoints(const qint32 &value)
|
|
|
|
{
|
|
|
|
maxCountPoints = value;
|
|
|
|
}
|