valentina_old/geometry/vsplinepath.cpp

172 lines
5.1 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file vsplinepath.cpp
** @author Roman Telezhinsky <dismine@gmail.com>
** @date November 15, 2013
**
** @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.
**
** 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/>.
**
*************************************************************************/
2013-08-09 08:49:34 +02:00
#include "vsplinepath.h"
#include "../exception/vexception.h"
2013-08-09 08:49:34 +02:00
VSplinePath::VSplinePath()
: path(QVector<VSplinePoint>()), kCurve(1), mode(Draw::Calculation), points(QHash<qint64, VPointF>()), idObject(0),
_name(QString()){}
2013-08-09 08:49:34 +02:00
VSplinePath::VSplinePath(const QHash<qint64, VPointF> *points, qreal kCurve, Draw::Draws mode, qint64 idObject)
: path(QVector<VSplinePoint>()), kCurve(kCurve), mode(mode), points(*points), idObject(idObject), _name(QString())
{}
2013-08-20 12:26:02 +02:00
VSplinePath::VSplinePath(const VSplinePath &splPath)
: path(*splPath.GetPoint()), kCurve(splPath.getKCurve()), mode(splPath.getMode()), points(splPath.GetDataPoints()),
idObject(splPath.getIdObject()), _name(splPath.name()){}
2013-08-28 10:55:11 +02:00
void VSplinePath::append(const VSplinePoint &point)
{
2013-08-09 08:49:34 +02:00
path.append(point);
_name = QString("SplPath");
for (qint32 i = 1; i <= this->Count(); ++i)
{
VSpline spl = this->GetSpline(i);
VPointF first = spl.GetPointP1();
VPointF second = spl.GetPointP4();
QString splName = QString("_%1_%2").arg(first.name(), second.name());
_name.append(splName);
}
2013-08-09 08:49:34 +02:00
}
qint32 VSplinePath::Count() const
{
if (path.size() == 0)
{
2013-08-09 08:49:34 +02:00
return 0;
}
else
{
2013-08-09 08:49:34 +02:00
return path.size() - 1;
}
}
VSpline VSplinePath::GetSpline(qint32 index) const
{
if (Count()<1)
{
throw VException(tr("Not enough points to create the spline."));
2013-08-09 08:49:34 +02:00
}
if (index < 1 || index > Count())
{
throw VException(tr("This spline is not exist."));
2013-08-09 08:49:34 +02:00
}
VSpline spl(&points, path[index-1].P(), path[index].P(), path[index-1].Angle2(), path[index].Angle1(),
2013-08-28 10:55:11 +02:00
path[index-1].KAsm2(), path[index].KAsm1(), this->kCurve);
2013-08-09 08:49:34 +02:00
return spl;
}
QPainterPath VSplinePath::GetPath() const
{
2013-08-09 08:49:34 +02:00
QPainterPath painterPath;
for (qint32 i = 1; i <= Count(); ++i)
{
VSpline spl(&points, path[i-1].P(), path[i].P(), path[i-1].Angle2(), path[i].Angle1(),
path[i-1].KAsm2(), path[i].KAsm1(), this->kCurve);
2013-08-09 08:49:34 +02:00
painterPath.addPath(spl.GetPath());
}
return painterPath;
}
QVector<QPointF> VSplinePath::GetPathPoints() const
{
2013-08-28 10:55:11 +02:00
QVector<QPointF> pathPoints;
for (qint32 i = 1; i <= Count(); ++i)
{
VSpline spl(&points, path[i-1].P(), path[i].P(), path[i-1].Angle2(), path[i].Angle1(),
path[i-1].KAsm2(), path[i].KAsm1(), this->kCurve);
2013-08-28 10:55:11 +02:00
QVector<QPointF> splP = spl.GetPoints();
for (qint32 j = 0; j < splP.size(); ++j)
{
2013-08-28 10:55:11 +02:00
pathPoints.append(splP[j]);
}
}
return pathPoints;
}
qreal VSplinePath::GetLength() const
{
2013-08-09 08:49:34 +02:00
qreal length = 0;
for (qint32 i = 1; i <= Count(); ++i)
{
VSpline spl(&points, path[i-1].P(), path[i].P(), path[i-1].Angle2(), path[i].Angle1(), path[i-1].KAsm2(),
2013-08-09 08:49:34 +02:00
path[i].KAsm1(), kCurve);
length += spl.GetLength();
}
return length;
}
void VSplinePath::UpdatePoint(qint32 indexSpline, const SplinePoint::Position &pos, const VSplinePoint &point)
{
if (indexSpline < 1 || indexSpline > Count())
{
throw VException(tr("This spline is not exist."));
2013-08-09 08:49:34 +02:00
}
if (pos == SplinePoint::FirstPoint)
{
2013-08-09 08:49:34 +02:00
path[indexSpline-1] = point;
}
else
{
2013-08-09 08:49:34 +02:00
path[indexSpline] = point;
}
}
VSplinePoint VSplinePath::GetSplinePoint(qint32 indexSpline, SplinePoint::Position pos) const
{
if (indexSpline < 1 || indexSpline > Count())
{
throw VException(tr("This spline is not exist."));
2013-08-09 08:49:34 +02:00
}
if (pos == SplinePoint::FirstPoint)
{
return path.at(indexSpline-1);
}
else
{
return path.at(indexSpline);
2013-08-09 08:49:34 +02:00
}
}
VSplinePath &VSplinePath::operator =(const VSplinePath &path)
{
2013-08-09 08:49:34 +02:00
this->path = path.GetSplinePath();
this->kCurve = path.getKCurve();
2013-08-28 10:55:11 +02:00
this->mode = path.getMode();
2013-08-09 08:49:34 +02:00
this->points = path.GetDataPoints();
this->idObject = path.getIdObject();
this->_name = path.name();
2013-08-09 08:49:34 +02:00
return *this;
}
VSplinePoint & VSplinePath::operator[](ptrdiff_t indx)
{
2013-08-09 08:49:34 +02:00
return path[indx];
}