From 40eab84c412a13dd0069f43d5f1f82d07dc2b059 Mon Sep 17 00:00:00 2001 From: dismine Date: Wed, 20 Aug 2014 16:02:41 +0300 Subject: [PATCH] Implicit Sharing for class VSpline. --HG-- branch : develop --- src/app/geometry/geometry.pri | 3 +- src/app/geometry/vspline.cpp | 160 ++++++++++++++++++++++------------ src/app/geometry/vspline.h | 119 +------------------------ src/app/geometry/vspline_p.h | 114 ++++++++++++++++++++++++ 4 files changed, 222 insertions(+), 174 deletions(-) create mode 100644 src/app/geometry/vspline_p.h diff --git a/src/app/geometry/geometry.pri b/src/app/geometry/geometry.pri index 5f7ffc6e3..635f902b8 100644 --- a/src/app/geometry/geometry.pri +++ b/src/app/geometry/geometry.pri @@ -12,7 +12,8 @@ HEADERS += \ geometry/vnodedetail_p.h \ geometry/vdetail_p.h \ geometry/vgobject_p.h \ - geometry/varc_p.h + geometry/varc_p.h \ + geometry/vspline_p.h SOURCES += \ geometry/vsplinepoint.cpp \ diff --git a/src/app/geometry/vspline.cpp b/src/app/geometry/vspline.cpp index 6a321dd3c..09f9bc161 100644 --- a/src/app/geometry/vspline.cpp +++ b/src/app/geometry/vspline.cpp @@ -27,6 +27,7 @@ *************************************************************************/ #include "vspline.h" +#include "vspline_p.h" #include #include @@ -39,8 +40,7 @@ * @brief VSpline default constructor */ VSpline::VSpline() - :VAbstractCurve(GOType::Spline), p1(VPointF()), p2(QPointF()), p3(QPointF()), p4(VPointF()), angle1(0), angle2(0), - kAsm1(1), kAsm2(1), kCurve(1) + :VAbstractCurve(GOType::Spline), d(new VSplineData) {} //--------------------------------------------------------------------------------------------------------------------- @@ -49,9 +49,7 @@ VSpline::VSpline() * @param spline spline from which the copy. */ VSpline::VSpline ( const VSpline & spline ) - :VAbstractCurve(spline), p1(spline.GetP1 ()), p2(spline.GetP2 ()), p3(spline.GetP3 ()), p4(spline.GetP4 ()), - angle1(spline.GetAngle1 ()), angle2(spline.GetAngle2 ()), kAsm1(spline.GetKasm1()), kAsm2(spline.GetKasm2()), - kCurve(spline.GetKcurve()) + :VAbstractCurve(spline), d(spline.d) {} //--------------------------------------------------------------------------------------------------------------------- @@ -67,30 +65,9 @@ VSpline::VSpline ( const VSpline & spline ) */ VSpline::VSpline (VPointF p1, VPointF p4, qreal angle1, qreal angle2, qreal kAsm1, qreal kAsm2, qreal kCurve, quint32 idObject, Draw mode) - :VAbstractCurve(GOType::Spline, idObject, mode), p1(p1), p2(QPointF()), p3(QPointF()), p4(p4), angle1(angle1), - angle2(angle2), kAsm1(kAsm1), kAsm2(kAsm2), kCurve(kCurve) + :VAbstractCurve(GOType::Spline, idObject, mode), d(new VSplineData(p1, p4, angle1, angle2, kAsm1, kAsm2, kCurve)) { CreateName(); - - this->p1 = p1; - this->p4 = p4; - this->angle1 = angle1; - this->angle2 = angle2; - this->kAsm1 = kAsm1; - this->kAsm2 = kAsm2; - this->kCurve = kCurve; - - qreal L = 0, radius = 0, angle = 90; - QPointF point1 = GetP1().toQPointF(); - QPointF point4 = GetP4().toQPointF(); - radius = QLineF(point1, point4).length()/1.414213;//1.414213=sqrt(2); - L = kCurve * radius * 4 / 3 * tan( angle * M_PI / 180.0 / 4 ); - QLineF p1p2(GetP1().x(), GetP1().y(), GetP1().x() + L * kAsm1, GetP1().y()); - p1p2.setAngle(angle1); - QLineF p4p3(GetP4().x(), GetP4().y(), GetP4().x() + L * kAsm2, GetP4().y()); - p4p3.setAngle(angle2); - this->p2 = p1p2.p2(); - this->p3 = p4p3.p2(); } //--------------------------------------------------------------------------------------------------------------------- @@ -102,29 +79,16 @@ VSpline::VSpline (VPointF p1, VPointF p4, qreal angle1, qreal angle2, qreal kAsm * @param p4 second point spline. */ VSpline::VSpline (VPointF p1, QPointF p2, QPointF p3, VPointF p4, qreal kCurve, quint32 idObject, Draw mode) - :VAbstractCurve(GOType::Spline, idObject, mode), p1(p1), p2(p2), p3(p3), p4(p4), angle1(0), angle2(0), kAsm1(1), - kAsm2(1), kCurve(1) + :VAbstractCurve(GOType::Spline, idObject, mode), d(new VSplineData(p1, p2, p3, p4, kCurve)) { CreateName(); - - this->p1 = p1; - this->p2 = p2; - this->p3 = p3; - this->p4 = p4; - this->angle1 = QLineF ( GetP1().toQPointF(), p2 ).angle(); - this->angle2 = QLineF ( GetP4().toQPointF(), p3 ).angle(); - - qreal L = 0, radius = 0, angle = 90; - QPointF point1 = GetP1().toQPointF(); - QPointF point4 = GetP4().toQPointF(); - radius = QLineF(point1, point4).length()/1.414213;//1.414213=sqrt(2); - L = kCurve * radius * 4 / 3 * tan( angle * M_PI / 180.0 / 4 ); - - this->kCurve = kCurve; - this->kAsm1 = QLineF ( GetP1().toQPointF(), p2 ).length()/L; - this->kAsm2 = QLineF ( GetP4().toQPointF(), p3 ).length()/L; } + +//--------------------------------------------------------------------------------------------------------------------- +VSpline::~VSpline() +{} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief GetLength return length of spline. @@ -132,7 +96,7 @@ VSpline::VSpline (VPointF p1, QPointF p2, QPointF p3, VPointF p4, qreal kCurve, */ qreal VSpline::GetLength () const { - return LengthBezier ( GetP1().toQPointF(), this->p2, this->p3, GetP4().toQPointF()); + return LengthBezier ( GetP1().toQPointF(), d->p2, d->p3, GetP4().toQPointF()); } //--------------------------------------------------------------------------------------------------------------------- @@ -284,7 +248,7 @@ QPointF VSpline::CutSpline ( qreal length, QPointF &spl1p2, QPointF &spl1p3, QPo */ QVector VSpline::GetPoints () const { - return GetPoints(GetP1().toQPointF(), p2, p3, GetP4().toQPointF()); + return GetPoints(GetP1().toQPointF(), d->p2, d->p3, GetP4().toQPointF()); } //--------------------------------------------------------------------------------------------------------------------- @@ -693,14 +657,96 @@ VSpline &VSpline::operator =(const VSpline &spline) return *this; } VAbstractCurve::operator=(spline); - this->p1 = spline.GetP1 (); - this->p2 = spline.GetP2 (); - this->p3 = spline.GetP3 (); - this->p4 = spline.GetP4 (); - this->angle1 = spline.GetAngle1 (); - this->angle2 = spline.GetAngle2 (); - this->kAsm1 = spline.GetKasm1(); - this->kAsm2 = spline.GetKasm2(); - this->kCurve = spline.GetKcurve(); + d = spline.d; return *this; } + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetP1 return first spline point. + * @return first point. + */ +VPointF VSpline::GetP1() const +{ + return d->p1; +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetP2 return first control point. + * @return first control point. + */ +QPointF VSpline::GetP2() const +{ + return d->p2; +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetP3 return second control point. + * @return second control point. + */ +QPointF VSpline::GetP3() const +{ + return d->p3; +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetP4 return last spline point. + * @return остання точка сплайну. + */ +VPointF VSpline::GetP4() const +{ + return d->p4; +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetAngle1 return first angle control line. + * @return angle. + */ +qreal VSpline::GetAngle1() const +{ + return d->angle1; +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetAngle2 return second angle control line. + * @return angle. + */ +qreal VSpline::GetAngle2() const +{ + return d->angle2; +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetKasm1 return coefficient of length first control line. + * @return coefficient. + */ +qreal VSpline::GetKasm1() const +{ + return d->kAsm1; +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetKasm2 return coefficient of length second control line. + * @return coefficient. + */ +qreal VSpline::GetKasm2() const +{ + return d->kAsm2; +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief GetKcurve return coefficient of curvature spline. + * @return coefficient + */ +qreal VSpline::GetKcurve() const +{ + return d->kCurve; +} diff --git a/src/app/geometry/vspline.h b/src/app/geometry/vspline.h index 4f4ea8bbc..48f724613 100644 --- a/src/app/geometry/vspline.h +++ b/src/app/geometry/vspline.h @@ -35,6 +35,7 @@ #include class QPainterPath; +class VSplineData; #define M_2PI 6.28318530717958647692528676655900576 @@ -50,6 +51,7 @@ public: quint32 idObject = 0, Draw mode = Draw::Calculation); VSpline (VPointF p1, QPointF p2, QPointF p3, VPointF p4, qreal kCurve, quint32 idObject = 0, Draw mode = Draw::Calculation); + virtual ~VSpline(); VSpline &operator=(const VSpline &spl); VPointF GetP1 () const; QPointF GetP2 () const; @@ -72,32 +74,7 @@ public: protected: static QVector GetPoints (const QPointF &p1, const QPointF &p2, const QPointF &p3, const QPointF &p4 ); private: - /** @brief p1 first spline point. */ - VPointF p1; - - /** @brief p2 first control point. */ - QPointF p2; - - /** @brief p3 second control point. */ - QPointF p3; - - /** @brief p4 last spline point. */ - VPointF p4; - - /** @brief angle1 first angle control line. */ - qreal angle1; - - /** @brief angle2 second angle control line. */ - qreal angle2; - - /** @brief kAsm1 coefficient of length first control line. */ - qreal kAsm1; - - /** @brief kAsm2 coefficient of length second control line. */ - qreal kAsm2; - - /** @brief kCurve coefficient of curvature spline. */ - qreal kCurve; + QSharedDataPointer d; qreal LengthBezier (const QPointF &p1, const QPointF &p2, const QPointF &p3, const QPointF &p4 ) const; static void PointBezier_r ( qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4, qint16 level, QVector &px, QVector &py); @@ -105,94 +82,4 @@ private: void CreateName(); }; -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief GetP1 return first spline point. - * @return first point. - */ -inline VPointF VSpline::GetP1() const -{ - return p1; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief GetP2 return first control point. - * @return first control point. - */ -inline QPointF VSpline::GetP2() const -{ - return p2; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief GetP3 return second control point. - * @return second control point. - */ -inline QPointF VSpline::GetP3() const -{ - return p3; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief GetP4 return last spline point. - * @return остання точка сплайну. - */ -inline VPointF VSpline::GetP4() const -{ - return p4; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief GetAngle1 return first angle control line. - * @return angle. - */ -inline qreal VSpline::GetAngle1() const -{ - return angle1; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief GetAngle2 return second angle control line. - * @return angle. - */ -inline qreal VSpline::GetAngle2() const -{ - return angle2; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief GetKasm1 return coefficient of length first control line. - * @return coefficient. - */ -inline qreal VSpline::GetKasm1() const -{ - return kAsm1; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief GetKasm2 return coefficient of length second control line. - * @return coefficient. - */ -inline qreal VSpline::GetKasm2() const -{ - return kAsm2; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief GetKcurve return coefficient of curvature spline. - * @return coefficient - */ -inline qreal VSpline::GetKcurve() const -{ - return kCurve; -} - #endif // VSPLINE_H diff --git a/src/app/geometry/vspline_p.h b/src/app/geometry/vspline_p.h new file mode 100644 index 000000000..b51be5278 --- /dev/null +++ b/src/app/geometry/vspline_p.h @@ -0,0 +1,114 @@ +/************************************************************************ + ** + ** @file vspline_p.h + ** @author Roman Telezhynskyi + ** @date 20 8, 2014 + ** + ** @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) 2014 Valentina project + ** 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 . + ** + *************************************************************************/ + +#ifndef VSPLINE_P_H +#define VSPLINE_P_H + +#include +#include "../options.h" +#include "vpointf.h" +#include + +class VSplineData : public QSharedData +{ +public: + VSplineData() + :p1(VPointF()), p2(QPointF()), p3(QPointF()), p4(VPointF()), angle1(0), angle2(0), kAsm1(1), kAsm2(1), kCurve(1) + {} + + VSplineData ( const VSplineData &spline ) + :QSharedData(spline), p1(spline.p1), p2(spline.p2), p3(spline.p3), p4(spline.p4), angle1(spline.angle1), + angle2(spline.angle2), kAsm1(spline.angle2), kAsm2(spline.kAsm1), kCurve(spline.kCurve) + {} + + VSplineData (VPointF p1, VPointF p4, qreal angle1, qreal angle2, qreal kAsm1, qreal kAsm2, qreal kCurve) + :p1(p1), p2(QPointF()), p3(QPointF()), p4(p4), angle1(angle1), angle2(angle2), kAsm1(kAsm1), kAsm2(kAsm2), + kCurve(kCurve) + { + qreal L = 0, radius = 0, angle = 90; + QPointF point1 = this->p1.toQPointF(); + QPointF point4 = this->p4.toQPointF(); + radius = QLineF(point1, point4).length()/1.414213;//1.414213=sqrt(2); + L = kCurve * radius * 4 / 3 * tan( angle * M_PI / 180.0 / 4 ); + QLineF p1p2(this->p1.x(), this->p1.y(), this->p1.x() + L * kAsm1, this->p1.y()); + p1p2.setAngle(angle1); + QLineF p4p3(this->p4.x(), this->p4.y(), this->p4.x() + L * kAsm2, this->p4.y()); + p4p3.setAngle(angle2); + this->p2 = p1p2.p2(); + this->p3 = p4p3.p2(); + } + + VSplineData (VPointF p1, QPointF p2, QPointF p3, VPointF p4, qreal kCurve) + :p1(p1), p2(p2), p3(p3), p4(p4), angle1(0), angle2(0), kAsm1(1), kAsm2(1), kCurve(1) + { + this->angle1 = QLineF ( this->p1.toQPointF(), this->p2 ).angle(); + this->angle2 = QLineF ( this->p4.toQPointF(), this->p3 ).angle(); + + qreal L = 0, radius = 0, angle = 90; + QPointF point1 = this->p1.toQPointF(); + QPointF point4 = this->p4.toQPointF(); + radius = QLineF(point1, point4).length()/1.414213;//1.414213=sqrt(2); + L = kCurve * radius * 4 / 3 * tan( angle * M_PI / 180.0 / 4 ); + + this->kCurve = kCurve; + this->kAsm1 = QLineF ( this->p1.toQPointF(), this->p2 ).length()/L; + this->kAsm2 = QLineF ( this->p4.toQPointF(), this->p3 ).length()/L; + } + + virtual ~VSplineData() {} + + /** @brief p1 first spline point. */ + VPointF p1; + + /** @brief p2 first control point. */ + QPointF p2; + + /** @brief p3 second control point. */ + QPointF p3; + + /** @brief p4 last spline point. */ + VPointF p4; + + /** @brief angle1 first angle control line. */ + qreal angle1; + + /** @brief angle2 second angle control line. */ + qreal angle2; + + /** @brief kAsm1 coefficient of length first control line. */ + qreal kAsm1; + + /** @brief kAsm2 coefficient of length second control line. */ + qreal kAsm2; + + /** @brief kCurve coefficient of curvature spline. */ + qreal kCurve; +}; + + +#endif // VSPLINE_P_H