diff --git a/src/libs/vgeometry/vabstractcubicbezier.cpp b/src/libs/vgeometry/vabstractcubicbezier.cpp index 2f0686719..d86fffb7f 100644 --- a/src/libs/vgeometry/vabstractcubicbezier.cpp +++ b/src/libs/vgeometry/vabstractcubicbezier.cpp @@ -83,17 +83,7 @@ QPointF VAbstractCubicBezier::CutSpline(qreal length, QPointF &spl1p2, QPointF & length = GetLength()*0.98; } - const qreal eps = 0.001 * qAbs(length); - qreal parT = 0.5; - qreal step = parT; - qreal splLength = LengthT(parT); - - while (qAbs(splLength - length) > eps) - { - step = step/2.0; - splLength > length ? parT -= step : parT += step; - splLength = LengthT(parT); - } + const qreal parT = GetParmT(length); QLineF seg1_2 ( GetP1 ().toQPointF(), GetControlPoint1 () ); seg1_2.setLength(seg1_2.length () * parT); @@ -137,6 +127,32 @@ QString VAbstractCubicBezier::NameForHistory(const QString &toolName) const return name; } +//--------------------------------------------------------------------------------------------------------------------- +qreal VAbstractCubicBezier::GetParmT(qreal length) const +{ + if (length < 0) + { + return 0; + } + else if (length > GetLength()) + { + length = GetLength(); + } + + const qreal eps = 0.001 * length; + qreal parT = 0.5; + qreal step = parT; + qreal splLength = LengthT(parT); + + while (qAbs(splLength - length) > eps) + { + step /= 2.0; + splLength > length ? parT -= step : parT += step; + splLength = LengthT(parT); + } + return parT; +} + //--------------------------------------------------------------------------------------------------------------------- void VAbstractCubicBezier::CreateName() { diff --git a/src/libs/vgeometry/vabstractcubicbezier.h b/src/libs/vgeometry/vabstractcubicbezier.h index 21d8029ea..2f2825b51 100644 --- a/src/libs/vgeometry/vabstractcubicbezier.h +++ b/src/libs/vgeometry/vabstractcubicbezier.h @@ -48,6 +48,9 @@ public: virtual QString NameForHistory(const QString &toolName) const Q_DECL_OVERRIDE; + qreal GetParmT(qreal length) const; + qreal LengthT(qreal t) const; + protected: virtual void CreateName() Q_DECL_OVERRIDE; @@ -60,9 +63,6 @@ protected: virtual QPointF GetControlPoint1() const =0; virtual QPointF GetControlPoint2() const =0; - -private: - qreal LengthT(qreal t) const; }; #endif // VABSTRACTCUBICBEZIER_H diff --git a/src/test/ValentinaTest/tst_vspline.cpp b/src/test/ValentinaTest/tst_vspline.cpp index 912b8ebb0..27d1ce216 100644 --- a/src/test/ValentinaTest/tst_vspline.cpp +++ b/src/test/ValentinaTest/tst_vspline.cpp @@ -293,6 +293,20 @@ void TST_VSpline::CompareThreeWays() CompareSplines(spl3, spl1); } +//--------------------------------------------------------------------------------------------------------------------- +void TST_VSpline::TestParametrT() +{ + VPointF p1(1168.8582803149607, 39.999874015748034, "p1", 5.0000125984251973, 9.9999874015748045); + VPointF p4(681.33729132409951, 1815.7969526662778, "p4", 5.0000125984251973, 9.9999874015748045); + + VSpline spl(p1, p4, 229.381, 41.6325, 0.96294100000000005, 1.00054, 1); + + const qreal halfLength = spl.GetLength()/2.0; + const qreal resLength = spl.LengthT(spl.GetParmT(halfLength)); + + QVERIFY(qAbs(halfLength - resLength) < UnitConvertor(0.5, Unit::Mm, Unit::Px)); +} + //--------------------------------------------------------------------------------------------------------------------- void TST_VSpline::CompareSplines(const VSpline &spl1, const VSpline &spl2) const { diff --git a/src/test/ValentinaTest/tst_vspline.h b/src/test/ValentinaTest/tst_vspline.h index 5de0c1eb7..bfd49e0c1 100644 --- a/src/test/ValentinaTest/tst_vspline.h +++ b/src/test/ValentinaTest/tst_vspline.h @@ -47,6 +47,7 @@ private slots: void GetSegmentPoints_TestPuzzle(); void GetSegmentPoints_NullSegment(); void CompareThreeWays(); + void TestParametrT(); private: Q_DISABLE_COPY(TST_VSpline)