2016-02-10 05:59:29 +01:00
|
|
|
|
/************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** @file vellipticalarc.cpp
|
|
|
|
|
** @author Valentina Zhuravska <zhuravska19(at)gmail.com>
|
|
|
|
|
** @date February 1, 2016
|
|
|
|
|
**
|
|
|
|
|
** @brief
|
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2016-02-10 05:59:29 +01:00
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2016-02-10 05:59:29 +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 "vellipticalarc.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
2016-02-10 05:59:29 +01:00
|
|
|
|
#include <QLineF>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
#include <QPoint>
|
2020-07-10 19:15:19 +02:00
|
|
|
|
#include <QPainterPath>
|
2021-07-24 10:15:48 +02:00
|
|
|
|
#include <QtDebug>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
2016-08-09 15:55:46 +02:00
|
|
|
|
#include "../vmisc/def.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
#include "../ifc/ifcdef.h"
|
2021-07-24 10:15:48 +02:00
|
|
|
|
#include "../ifc/exception/vexception.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
#include "../vmisc/vabstractapplication.h"
|
2022-08-27 15:46:25 +02:00
|
|
|
|
#include "../vmisc/fpm/fixed.hpp"
|
|
|
|
|
#include "../vmisc/fpm/math.hpp"
|
2020-01-16 16:34:08 +01:00
|
|
|
|
#include "../vmisc/compatibility.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
#include "vabstractcurve.h"
|
|
|
|
|
#include "vellipticalarc_p.h"
|
2022-08-27 15:46:25 +02:00
|
|
|
|
#include "../vmisc/vmath.h"
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
2022-08-27 16:57:08 +02:00
|
|
|
|
constexpr qreal tolerance = accuracyPointOnLine/8;
|
2022-08-30 13:19:40 +02:00
|
|
|
|
|
|
|
|
|
// Because of overflow we cannot generate arcs more than maxRadius
|
|
|
|
|
constexpr int maxRadius = 10000;
|
|
|
|
|
|
2022-08-27 15:46:25 +02:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
auto VLen(fpm::fixed_16_16 x, fpm::fixed_16_16 y) -> fpm::fixed_16_16
|
|
|
|
|
{
|
|
|
|
|
x = fpm::abs(x);
|
|
|
|
|
y = fpm::abs(y);
|
|
|
|
|
if (x > y)
|
|
|
|
|
{
|
|
|
|
|
return x + qMax(y/8, y/2 - x/8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return y + qMax(x/8, x/2 - y/8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
auto AuxRadius(fpm::fixed_16_16 xP, fpm::fixed_16_16 yP, fpm::fixed_16_16 xQ, fpm::fixed_16_16 yQ) -> fpm::fixed_16_16
|
|
|
|
|
{
|
|
|
|
|
fpm::fixed_16_16 dP = VLen(xP, yP);
|
|
|
|
|
fpm::fixed_16_16 dQ = VLen(xQ, yQ);
|
|
|
|
|
fpm::fixed_16_16 dJ = VLen(xP + xQ, yP + yQ);
|
|
|
|
|
fpm::fixed_16_16 dK = VLen(xP - xQ, yP - yQ);
|
|
|
|
|
fpm::fixed_16_16 r1 = qMax(dP, dQ);
|
|
|
|
|
fpm::fixed_16_16 r2 = qMax(dJ, dK);
|
|
|
|
|
return qMax(r1 + r1/16, r2 - r2/4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
auto AngularInc(fpm::fixed_16_16 xP, fpm::fixed_16_16 yP, fpm::fixed_16_16 xQ, fpm::fixed_16_16 yQ,
|
|
|
|
|
fpm::fixed_16_16 flatness) -> int
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
fpm::fixed_16_16 r = AuxRadius(xP, yP, xQ, yQ);
|
|
|
|
|
fpm::fixed_16_16 err2{r >> 3};
|
|
|
|
|
// 2nd-order term
|
|
|
|
|
fpm::fixed_16_16 err4{r >> 7};
|
|
|
|
|
// 4th-order term
|
|
|
|
|
const int kmax = qRound(0.5 * std::log2(maxSceneSize / (8. * tolerance)));
|
|
|
|
|
for (int k = 0; k < kmax; ++k)
|
|
|
|
|
{
|
|
|
|
|
if (flatness >= err2 + err4)
|
|
|
|
|
{
|
|
|
|
|
return k;
|
|
|
|
|
}
|
|
|
|
|
err2 >>= 2;
|
|
|
|
|
err4 >>= 4;
|
|
|
|
|
}
|
|
|
|
|
return kmax;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
inline void CircleGen(fpm::fixed_16_16& u, fpm::fixed_16_16& v, uint k)
|
|
|
|
|
{
|
|
|
|
|
u -= v >> k;
|
|
|
|
|
v += u >> k;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
auto InitialValue(fpm::fixed_16_16 u0, fpm::fixed_16_16 v0, uint k) -> fpm::fixed_16_16
|
|
|
|
|
{
|
|
|
|
|
uint shift = 2*k + 3;
|
|
|
|
|
fpm::fixed_16_16 w {u0 >> shift};
|
|
|
|
|
|
|
|
|
|
fpm::fixed_16_16 U0 = u0 - w + (v0 >> (k + 1));
|
|
|
|
|
w >>= (shift + 1);
|
|
|
|
|
U0 -= w;
|
|
|
|
|
w >>= shift;
|
|
|
|
|
U0 -= w;
|
|
|
|
|
return U0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
auto EllipseCore(fpm::fixed_16_16 xC, fpm::fixed_16_16 yC, fpm::fixed_16_16 xP, fpm::fixed_16_16 yP,
|
|
|
|
|
fpm::fixed_16_16 xQ, fpm::fixed_16_16 yQ, fpm::fixed_16_16 sweep,
|
|
|
|
|
fpm::fixed_16_16 flatness) -> QVector<QPointF>
|
|
|
|
|
{
|
|
|
|
|
uint k = qMin(static_cast<uint>(AngularInc(xP, yP, xQ, yQ, flatness)), 16U);
|
|
|
|
|
const uint count = static_cast<std::uint32_t>(sweep.raw_value()) >> (16 - k);
|
|
|
|
|
|
|
|
|
|
QVector<QPointF> arc;
|
|
|
|
|
arc.reserve(static_cast<int>(count) + 1);
|
|
|
|
|
|
|
|
|
|
// Arc start point
|
|
|
|
|
arc.append({static_cast<qreal>(xP + xC), static_cast<qreal>(yP + yC)});
|
|
|
|
|
|
|
|
|
|
xQ = InitialValue(xQ, xP, k);
|
|
|
|
|
yQ = InitialValue(yQ, yP, k);
|
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < count; ++i)
|
|
|
|
|
{
|
|
|
|
|
CircleGen(xQ, xP, k);
|
|
|
|
|
CircleGen(yQ, yP, k);
|
|
|
|
|
arc.append({static_cast<qreal>(xP + xC), static_cast<qreal>(yP + yC)});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return arc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
auto EllipticArcPoints(QPointF c, qreal radius1, qreal radius2, qreal astart, qreal asweep,
|
|
|
|
|
qreal approximationScale) -> QVector<QPointF>
|
|
|
|
|
{
|
|
|
|
|
fpm::fixed_16_16 xC{c.x()};
|
|
|
|
|
fpm::fixed_16_16 yC{c.y()};
|
|
|
|
|
|
|
|
|
|
fpm::fixed_16_16 xP{c.x() + radius1};
|
|
|
|
|
fpm::fixed_16_16 yP{c.y()};
|
|
|
|
|
|
|
|
|
|
fpm::fixed_16_16 xQ{c.x()};
|
|
|
|
|
fpm::fixed_16_16 yQ{c.y() - radius2};
|
|
|
|
|
|
|
|
|
|
xP -= xC;
|
|
|
|
|
yP -= yC;
|
|
|
|
|
xQ -= xC;
|
|
|
|
|
yQ -= yC;
|
|
|
|
|
|
|
|
|
|
if (not qFuzzyIsNull(astart))
|
|
|
|
|
{
|
|
|
|
|
// Set new conjugate diameter end points P’ and Q’
|
|
|
|
|
fpm::fixed_16_16 cosa {cos(astart)};
|
|
|
|
|
fpm::fixed_16_16 sina {sin(astart)};
|
|
|
|
|
fpm::fixed_16_16 x {xP * cosa + xQ * sina};
|
|
|
|
|
fpm::fixed_16_16 y {yP * cosa + yQ * sina};
|
|
|
|
|
|
|
|
|
|
xQ = xQ * cosa - xP * sina;
|
|
|
|
|
yQ = yQ * cosa - yP * sina;
|
|
|
|
|
xP = x;
|
|
|
|
|
yP = y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If sweep angle is negative, switch direction
|
|
|
|
|
if (asweep < 0)
|
|
|
|
|
{
|
|
|
|
|
xQ = -xQ;
|
|
|
|
|
yQ = -yQ;
|
|
|
|
|
asweep = -asweep;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(approximationScale < minCurveApproximationScale || approximationScale > maxCurveApproximationScale)
|
|
|
|
|
{
|
|
|
|
|
approximationScale = VAbstractApplication::VApp()->Settings()->GetCurveApproximationScale();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fpm::fixed_16_16 flatness {maxCurveApproximationScale / approximationScale * tolerance};
|
|
|
|
|
fpm::fixed_16_16 swangle{asweep};
|
|
|
|
|
QVector<QPointF> arc = EllipseCore(xC, yC, xP, yP, xQ, yQ, swangle, flatness);
|
|
|
|
|
|
|
|
|
|
// Arc end point
|
|
|
|
|
fpm::fixed_16_16 cosb {qCos(asweep)};
|
|
|
|
|
fpm::fixed_16_16 sinb {qSin(asweep)};
|
|
|
|
|
xP = xP*cosb + xQ*sinb;
|
|
|
|
|
yP = yP*cosb + yQ*sinb;
|
|
|
|
|
arc.append({static_cast<qreal>(xP+xC), static_cast<qreal>(yP+yC)});
|
|
|
|
|
|
|
|
|
|
return arc;
|
|
|
|
|
}
|
2022-08-30 13:19:40 +02:00
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
auto JoinVectors(const QVector<QPointF> &v1, const QVector<QPointF> &v2) -> QVector<QPointF>
|
|
|
|
|
{
|
|
|
|
|
QVector<QPointF> v;
|
|
|
|
|
v.reserve(v1.size() + v2.size());
|
|
|
|
|
|
|
|
|
|
v = v1;
|
|
|
|
|
|
2022-09-02 16:14:03 +02:00
|
|
|
|
constexpr qreal accuracy = MmToPixel(0.0001);
|
2022-08-30 13:19:40 +02:00
|
|
|
|
|
|
|
|
|
for (auto p : v2)
|
|
|
|
|
{
|
|
|
|
|
if (not VFuzzyComparePoints(ConstLast(v), p, accuracy))
|
|
|
|
|
{
|
|
|
|
|
v.append(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v;
|
|
|
|
|
}
|
2022-08-27 15:46:25 +02:00
|
|
|
|
} // namespace
|
2016-02-10 05:59:29 +01:00
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief VEllipticalArc default constructor.
|
|
|
|
|
*/
|
|
|
|
|
VEllipticalArc::VEllipticalArc()
|
2016-04-10 16:40:55 +02:00
|
|
|
|
: VAbstractArc(GOType::EllipticalArc), d (new VEllipticalArcData)
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief VEllipticalArc constructor.
|
|
|
|
|
* @param center center point.
|
|
|
|
|
* @param radius1 arc major radius.
|
|
|
|
|
* @param radius2 arc minor radius.
|
|
|
|
|
* @param f1 start angle (degree).
|
|
|
|
|
* @param f2 end angle (degree).
|
|
|
|
|
*/
|
2016-04-10 13:40:04 +02:00
|
|
|
|
VEllipticalArc::VEllipticalArc (const VPointF ¢er, qreal radius1, qreal radius2, const QString &formulaRadius1,
|
2022-08-27 15:46:25 +02:00
|
|
|
|
const QString &formulaRadius2, qreal f1, const QString &formulaF1, qreal f2,
|
|
|
|
|
const QString &formulaF2, qreal rotationAngle, const QString &formulaRotationAngle,
|
|
|
|
|
quint32 idObject, Draw mode)
|
2016-04-10 16:40:55 +02:00
|
|
|
|
: VAbstractArc(GOType::EllipticalArc, center, f1, formulaF1, f2, formulaF2, idObject, mode),
|
2022-08-27 15:46:25 +02:00
|
|
|
|
d (new VEllipticalArcData(radius1, radius2, formulaRadius1, formulaRadius2, rotationAngle, formulaRotationAngle))
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2016-02-27 15:53:17 +01:00
|
|
|
|
CreateName();
|
2016-02-10 05:59:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-04-10 13:40:04 +02:00
|
|
|
|
VEllipticalArc::VEllipticalArc(const VPointF ¢er, qreal radius1, qreal radius2, qreal f1, qreal f2,
|
|
|
|
|
qreal rotationAngle)
|
2016-04-10 16:40:55 +02:00
|
|
|
|
: VAbstractArc(GOType::EllipticalArc, center, f1, f2, NULL_ID, Draw::Calculation),
|
2022-08-27 15:46:25 +02:00
|
|
|
|
d (new VEllipticalArcData(radius1, radius2, rotationAngle))
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2016-02-27 15:53:17 +01:00
|
|
|
|
CreateName();
|
2016-02-10 05:59:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-04-10 13:40:04 +02:00
|
|
|
|
VEllipticalArc::VEllipticalArc(qreal length, const QString &formulaLength, const VPointF ¢er, qreal radius1,
|
|
|
|
|
qreal radius2, const QString &formulaRadius1, const QString &formulaRadius2, qreal f1,
|
2016-12-17 02:41:33 +01:00
|
|
|
|
const QString &formulaF1, qreal rotationAngle, const QString &formulaRotationAngle,
|
|
|
|
|
quint32 idObject, Draw mode)
|
2016-04-10 16:40:55 +02:00
|
|
|
|
: VAbstractArc(GOType::EllipticalArc, formulaLength, center, f1, formulaF1, idObject, mode),
|
2022-08-27 15:46:25 +02:00
|
|
|
|
d (new VEllipticalArcData(radius1, radius2, formulaRadius1, formulaRadius2, rotationAngle, formulaRotationAngle))
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2016-02-27 15:53:17 +01:00
|
|
|
|
CreateName();
|
2016-02-10 05:59:29 +01:00
|
|
|
|
FindF2(length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-04-10 13:40:04 +02:00
|
|
|
|
VEllipticalArc::VEllipticalArc(qreal length, const VPointF ¢er, qreal radius1, qreal radius2, qreal f1,
|
|
|
|
|
qreal rotationAngle)
|
2016-04-10 16:40:55 +02:00
|
|
|
|
: VAbstractArc(GOType::EllipticalArc, center, f1, NULL_ID, Draw::Calculation),
|
2022-08-27 15:46:25 +02:00
|
|
|
|
d (new VEllipticalArcData(radius1, radius2, rotationAngle))
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2016-02-27 15:53:17 +01:00
|
|
|
|
CreateName();
|
2016-02-10 05:59:29 +01:00
|
|
|
|
FindF2(length);
|
2022-11-21 17:49:41 +01:00
|
|
|
|
SetFormulaLength(QString::number(length));
|
2016-02-10 05:59:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief VEllipticalArc copy constructor
|
|
|
|
|
* @param arc arc
|
|
|
|
|
*/
|
|
|
|
|
VEllipticalArc::VEllipticalArc(const VEllipticalArc &arc)
|
2016-04-10 16:40:55 +02:00
|
|
|
|
: VAbstractArc(arc), d (arc.d)
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief operator = assignment operator
|
|
|
|
|
* @param arc arc
|
|
|
|
|
* @return arc
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::operator =(const VEllipticalArc &arc) -> VEllipticalArc &
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
|
|
|
|
if ( &arc == this )
|
|
|
|
|
{
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2016-04-10 16:40:55 +02:00
|
|
|
|
VAbstractArc::operator=(arc);
|
2016-02-10 05:59:29 +01:00
|
|
|
|
d = arc.d;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 12:00:57 +01:00
|
|
|
|
#ifdef Q_COMPILER_RVALUE_REFS
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
VEllipticalArc::VEllipticalArc(VEllipticalArc &&arc) Q_DECL_NOTHROW
|
|
|
|
|
: VAbstractArc(std::move(arc)),
|
|
|
|
|
d(std::move(arc.d))
|
2019-12-30 16:13:18 +01:00
|
|
|
|
{}
|
2019-12-30 12:00:57 +01:00
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::operator=(VEllipticalArc &&arc) Q_DECL_NOTHROW -> VEllipticalArc &
|
2019-12-30 12:00:57 +01:00
|
|
|
|
{
|
2019-12-30 16:13:18 +01:00
|
|
|
|
VAbstractArc::operator=(arc);
|
2019-12-30 12:00:57 +01:00
|
|
|
|
std::swap(d, arc.d);
|
2019-12-30 16:13:18 +01:00
|
|
|
|
return *this;
|
2019-12-30 12:00:57 +01:00
|
|
|
|
}
|
2019-12-30 16:13:18 +01:00
|
|
|
|
#endif
|
2019-12-30 12:00:57 +01:00
|
|
|
|
|
2016-04-10 13:40:04 +02:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::Rotate(QPointF originPoint, qreal degrees, const QString &prefix) const -> VEllipticalArc
|
2016-04-10 13:40:04 +02:00
|
|
|
|
{
|
2018-01-05 18:41:09 +01:00
|
|
|
|
originPoint = d->m_transform.inverted().map(originPoint);
|
2017-07-27 15:01:18 +02:00
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
QTransform t = d->m_transform;
|
|
|
|
|
t.translate(originPoint.x(), originPoint.y());
|
2018-03-30 08:43:11 +02:00
|
|
|
|
t.rotate(IsFlipped() ? degrees : -degrees);
|
2018-01-05 18:41:09 +01:00
|
|
|
|
t.translate(-originPoint.x(), -originPoint.y());
|
2017-07-27 15:01:18 +02:00
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
VEllipticalArc elArc(VAbstractArc::GetCenter(), GetRadius1(), GetRadius2(), VAbstractArc::GetStartAngle(),
|
|
|
|
|
VAbstractArc::GetEndAngle(), GetRotationAngle());
|
2016-04-10 13:40:04 +02:00
|
|
|
|
elArc.setName(name() + prefix);
|
2020-11-07 14:02:30 +01:00
|
|
|
|
|
|
|
|
|
if (not GetAliasSuffix().isEmpty())
|
|
|
|
|
{
|
|
|
|
|
elArc.SetAliasSuffix(GetAliasSuffix() + prefix);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 12:07:53 +02:00
|
|
|
|
elArc.SetColor(GetColor());
|
|
|
|
|
elArc.SetPenStyle(GetPenStyle());
|
2017-07-27 15:01:18 +02:00
|
|
|
|
elArc.SetFlipped(IsFlipped());
|
2018-01-05 18:41:09 +01:00
|
|
|
|
elArc.SetTransform(t);
|
2022-08-30 13:19:40 +02:00
|
|
|
|
elArc.SetApproximationScale(GetApproximationScale());
|
2016-04-10 13:40:04 +02:00
|
|
|
|
return elArc;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 05:59:29 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::Flip(const QLineF &axis, const QString &prefix) const -> VEllipticalArc
|
2016-09-10 20:40:30 +02:00
|
|
|
|
{
|
2018-01-05 18:41:09 +01:00
|
|
|
|
VEllipticalArc elArc(VAbstractArc::GetCenter(), GetRadius1(), GetRadius2(), VAbstractArc::GetStartAngle(),
|
|
|
|
|
VAbstractArc::GetEndAngle(), GetRotationAngle());
|
2016-09-10 20:40:30 +02:00
|
|
|
|
elArc.setName(name() + prefix);
|
2020-11-07 14:02:30 +01:00
|
|
|
|
|
|
|
|
|
if (not GetAliasSuffix().isEmpty())
|
|
|
|
|
{
|
|
|
|
|
elArc.SetAliasSuffix(GetAliasSuffix() + prefix);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 12:07:53 +02:00
|
|
|
|
elArc.SetColor(GetColor());
|
|
|
|
|
elArc.SetPenStyle(GetPenStyle());
|
2017-07-27 15:01:18 +02:00
|
|
|
|
elArc.SetFlipped(not IsFlipped());
|
2018-01-05 18:41:09 +01:00
|
|
|
|
elArc.SetTransform(d->m_transform * VGObject::FlippingMatrix(d->m_transform.inverted().map(axis)));
|
2022-08-30 13:19:40 +02:00
|
|
|
|
elArc.SetApproximationScale(GetApproximationScale());
|
2016-09-10 20:40:30 +02:00
|
|
|
|
return elArc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::Move(qreal length, qreal angle, const QString &prefix) const -> VEllipticalArc
|
2016-10-04 16:34:37 +02:00
|
|
|
|
{
|
2018-01-05 18:41:09 +01:00
|
|
|
|
const VPointF oldCenter = VAbstractArc::GetCenter();
|
|
|
|
|
const VPointF center = oldCenter.Move(length, angle);
|
2017-07-27 15:01:18 +02:00
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
const QPointF position = d->m_transform.inverted().map(center.toQPointF()) -
|
2022-08-27 15:46:25 +02:00
|
|
|
|
d->m_transform.inverted().map(oldCenter.toQPointF());
|
2017-07-27 15:01:18 +02:00
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
QTransform t = d->m_transform;
|
|
|
|
|
t.translate(position.x(), position.y());
|
2017-07-27 15:01:18 +02:00
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
VEllipticalArc elArc(oldCenter, GetRadius1(), GetRadius2(), VAbstractArc::GetStartAngle(),
|
|
|
|
|
VAbstractArc::GetEndAngle(), GetRotationAngle());
|
2016-10-04 16:34:37 +02:00
|
|
|
|
elArc.setName(name() + prefix);
|
2020-11-07 14:02:30 +01:00
|
|
|
|
|
|
|
|
|
if (not GetAliasSuffix().isEmpty())
|
|
|
|
|
{
|
|
|
|
|
elArc.SetAliasSuffix(GetAliasSuffix() + prefix);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 12:07:53 +02:00
|
|
|
|
elArc.SetColor(GetColor());
|
|
|
|
|
elArc.SetPenStyle(GetPenStyle());
|
2017-07-27 15:01:18 +02:00
|
|
|
|
elArc.SetFlipped(IsFlipped());
|
2018-01-05 18:41:09 +01:00
|
|
|
|
elArc.SetTransform(t);
|
2022-08-30 13:19:40 +02:00
|
|
|
|
elArc.SetApproximationScale(GetApproximationScale());
|
2016-10-04 16:34:37 +02:00
|
|
|
|
return elArc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
VEllipticalArc::~VEllipticalArc() // NOLINT(hicpp-use-equals-default, modernize-use-equals-default)
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief GetLength return arc length.
|
|
|
|
|
* @return length.
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetLength() const -> qreal
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2016-04-02 13:59:43 +02:00
|
|
|
|
qreal length = PathLength(GetPoints());
|
2016-02-10 05:59:29 +01:00
|
|
|
|
|
2016-04-10 16:40:55 +02:00
|
|
|
|
if (IsFlipped())
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
|
|
|
|
length = length * -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief GetP1 return point associated with start angle.
|
|
|
|
|
* @return point.
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetP1() const -> QPointF
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2018-01-05 18:41:09 +01:00
|
|
|
|
return GetTransform().map(GetP(VAbstractArc::GetStartAngle()));
|
2016-02-10 05:59:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief GetP2 return point associated with end angle.
|
|
|
|
|
* @return point.
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetP2 () const -> QPointF
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2018-01-05 18:41:09 +01:00
|
|
|
|
return GetTransform().map(GetP(VAbstractArc::GetEndAngle()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetTransform() const -> QTransform
|
2018-01-05 18:41:09 +01:00
|
|
|
|
{
|
|
|
|
|
return d->m_transform;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
void VEllipticalArc::SetTransform(const QTransform &matrix, bool combine)
|
|
|
|
|
{
|
|
|
|
|
d->m_transform = combine ? d->m_transform * matrix : matrix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetCenter() const -> VPointF
|
2018-01-05 18:41:09 +01:00
|
|
|
|
{
|
|
|
|
|
VPointF center = VAbstractArc::GetCenter();
|
|
|
|
|
const QPointF p = d->m_transform.map(center.toQPointF());
|
|
|
|
|
center.setX(p.x());
|
|
|
|
|
center.setY(p.y());
|
|
|
|
|
return center;
|
2016-02-12 19:17:55 +01:00
|
|
|
|
}
|
2016-02-10 05:59:29 +01:00
|
|
|
|
|
2016-02-12 19:17:55 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief GetPoints return list of points needed for drawing arc.
|
|
|
|
|
* @return list of points
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetPoints() const -> QVector<QPointF>
|
2016-02-12 19:17:55 +01:00
|
|
|
|
{
|
2018-01-05 18:41:09 +01:00
|
|
|
|
const QPointF center = VAbstractArc::GetCenter().toQPointF();
|
2017-09-13 08:17:21 +02:00
|
|
|
|
|
2022-08-30 13:19:40 +02:00
|
|
|
|
// Don't work with 0 radius. Always make it bigger than 0.
|
2022-09-01 09:11:44 +02:00
|
|
|
|
Q_RELAXED_CONSTEXPR qreal threshold = ToPixel(0.001, Unit::Mm);
|
2022-08-30 13:19:40 +02:00
|
|
|
|
qreal radius1 = qMax(d->radius1, threshold);
|
|
|
|
|
qreal radius2 = qMax(d->radius2, threshold);
|
|
|
|
|
qreal max = qMax(d->radius1, d->radius2);
|
|
|
|
|
qreal scale = 1;
|
|
|
|
|
|
|
|
|
|
if (max > maxRadius)
|
|
|
|
|
{
|
|
|
|
|
scale = max / maxRadius;
|
|
|
|
|
radius1 /= scale;
|
|
|
|
|
radius2 /= scale;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 15:46:25 +02:00
|
|
|
|
// Generate complete ellipse because angles are not correct and have to be fixed manually
|
2022-08-30 13:19:40 +02:00
|
|
|
|
QVector<QPointF> points = EllipticArcPoints(center, radius1, radius2, 0.0, M_2PI, GetApproximationScale());
|
2022-08-27 15:46:25 +02:00
|
|
|
|
points = ArcPoints(points);
|
2016-02-10 05:59:29 +01:00
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
QTransform t = d->m_transform;
|
|
|
|
|
t.translate(center.x(), center.y());
|
2022-08-30 13:19:40 +02:00
|
|
|
|
if (not VFuzzyComparePossibleNulls(scale, 1))
|
|
|
|
|
{
|
|
|
|
|
// Because fixed 16.16 type has limitations it is very easy to get overflow error.
|
|
|
|
|
// To avoid this we calculate an arc for scaled radiuses and then scale up to original size.
|
|
|
|
|
t.scale(scale, scale);
|
|
|
|
|
}
|
2017-09-13 06:29:35 +02:00
|
|
|
|
t.rotate(-GetRotationAngle());
|
2018-01-05 18:41:09 +01:00
|
|
|
|
t.translate(-center.x(), -center.y());
|
2016-02-12 19:17:55 +01:00
|
|
|
|
|
2022-08-27 15:46:25 +02:00
|
|
|
|
std::transform(points.begin(), points.end(), points.begin(), [t](const QPointF &point) { return t.map(point); });
|
2016-02-12 19:17:55 +01:00
|
|
|
|
|
2022-08-27 15:46:25 +02:00
|
|
|
|
return points;
|
2016-02-10 05:59:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetStartAngle() const -> qreal
|
2018-01-05 18:41:09 +01:00
|
|
|
|
{
|
|
|
|
|
return QLineF(GetCenter().toQPointF(), GetP1()).angle() - GetRotationAngle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetEndAngle() const -> qreal
|
2018-01-05 18:41:09 +01:00
|
|
|
|
{
|
|
|
|
|
return QLineF(GetCenter().toQPointF(), GetP2()).angle() - GetRotationAngle();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 05:59:29 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief CutArc cut arc into two arcs.
|
|
|
|
|
* @param length length first arc.
|
|
|
|
|
* @param arc1 first arc.
|
|
|
|
|
* @param arc2 second arc.
|
|
|
|
|
* @return point cutting
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::CutArc(const qreal &length, VEllipticalArc &arc1, VEllipticalArc &arc2,
|
|
|
|
|
const QString &pointName) const -> QPointF
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2016-02-26 20:30:20 +01:00
|
|
|
|
//Always need return two arcs, so we must correct wrong length.
|
|
|
|
|
qreal len = 0;
|
2016-04-02 19:47:04 +02:00
|
|
|
|
const qreal fullLength = GetLength();
|
|
|
|
|
|
|
|
|
|
if (fullLength <= minLength)
|
|
|
|
|
{
|
|
|
|
|
arc1 = VEllipticalArc();
|
|
|
|
|
arc2 = VEllipticalArc();
|
2021-07-24 10:15:48 +02:00
|
|
|
|
|
|
|
|
|
const QString errorMsg = QObject::tr("Unable to cut curve '%1'. The curve is too short.").arg(name());
|
|
|
|
|
VAbstractApplication::VApp()->IsPedantic() ? throw VException(errorMsg) :
|
2022-08-27 15:46:25 +02:00
|
|
|
|
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-02-26 20:30:20 +01:00
|
|
|
|
{
|
2016-04-02 19:47:04 +02:00
|
|
|
|
len = minLength;
|
2021-07-24 10:15:48 +02:00
|
|
|
|
|
2022-05-20 14:49:15 +02:00
|
|
|
|
QString errorMsg;
|
|
|
|
|
if (not pointName.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
errorMsg = QObject::tr("Curve '%1'. Length of a cut segment (%2) is too small. Optimize it to minimal "
|
|
|
|
|
"value.").arg(name(), pointName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
errorMsg = QObject::tr("Curve '%1'. Length of a cut segment is too small. Optimize it to minimal value.")
|
2022-08-27 15:46:25 +02:00
|
|
|
|
.arg(name());
|
2022-05-20 14:49:15 +02:00
|
|
|
|
}
|
2021-07-24 10:15:48 +02:00
|
|
|
|
VAbstractApplication::VApp()->IsPedantic() ? throw VException(errorMsg) :
|
2022-08-27 15:46:25 +02:00
|
|
|
|
qWarning() << VAbstractApplication::warningMessageSignature + errorMsg;
|
2016-02-26 20:30:20 +01:00
|
|
|
|
}
|
2016-04-02 19:47:04 +02:00
|
|
|
|
else if (length > maxLength)
|
2016-02-26 20:30:20 +01:00
|
|
|
|
{
|
2016-04-02 19:47:04 +02:00
|
|
|
|
len = maxLength;
|
2021-07-24 10:15:48 +02:00
|
|
|
|
|
2022-05-20 14:49:15 +02:00
|
|
|
|
QString errorMsg;
|
|
|
|
|
if (not pointName.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
errorMsg = QObject::tr("Curve '%1'. Length of a cut segment (%2) is too big. Optimize it to maximal value.")
|
2022-08-27 15:46:25 +02:00
|
|
|
|
.arg(name(), pointName);
|
2022-05-20 14:49:15 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
errorMsg = QObject::tr("Curve '%1'. Length of a cut segment is too big. Optimize it to maximal value.")
|
2022-08-27 15:46:25 +02:00
|
|
|
|
.arg(name());
|
2022-05-20 14:49:15 +02:00
|
|
|
|
}
|
2021-07-24 10:15:48 +02:00
|
|
|
|
VAbstractApplication::VApp()->IsPedantic() ? throw VException(errorMsg) :
|
2022-08-27 15:46:25 +02:00
|
|
|
|
qWarning() << VAbstractApplication::warningMessageSignature + errorMsg;
|
2016-02-26 20:30:20 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
len = length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the first arc has given length and startAngle just like in the origin arc
|
2016-04-10 16:40:55 +02:00
|
|
|
|
arc1 = VEllipticalArc (len, QString().setNum(length), GetCenter(), d->radius1, d->radius2,
|
2022-08-27 15:46:25 +02:00
|
|
|
|
d->formulaRadius1, d->formulaRadius2, GetStartAngle(), GetFormulaF1(), d->rotationAngle,
|
|
|
|
|
GetFormulaRotationAngle(), getIdObject(), getMode());
|
2016-02-26 20:30:20 +01:00
|
|
|
|
// the second arc has startAngle just like endAngle of the first arc
|
|
|
|
|
// and it has endAngle just like endAngle of the origin arc
|
2016-04-10 16:40:55 +02:00
|
|
|
|
arc2 = VEllipticalArc (GetCenter(), d->radius1, d->radius2, d->formulaRadius1, d->formulaRadius2,
|
2022-08-27 15:46:25 +02:00
|
|
|
|
arc1.GetEndAngle(), arc1.GetFormulaF2(), GetEndAngle(), GetFormulaF2(), d->rotationAngle,
|
|
|
|
|
GetFormulaRotationAngle(), getIdObject(), getMode());
|
2016-02-26 20:30:20 +01:00
|
|
|
|
return arc1.GetP1();
|
2016-02-10 05:59:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::CutArc(const qreal &length, const QString &pointName) const -> QPointF
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2016-02-26 20:30:20 +01:00
|
|
|
|
VEllipticalArc arc1;
|
|
|
|
|
VEllipticalArc arc2;
|
2022-05-20 14:49:15 +02:00
|
|
|
|
return this->CutArc(length, arc1, arc2, pointName);
|
2016-02-10 05:59:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-02-27 15:53:17 +01:00
|
|
|
|
void VEllipticalArc::CreateName()
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2022-08-27 15:46:25 +02:00
|
|
|
|
QString name = ELARC_ + QStringLiteral("%1").arg(this->GetCenter().name());
|
2022-05-20 16:08:32 +02:00
|
|
|
|
const QString nameStr = QStringLiteral("_%1");
|
2016-02-27 15:53:17 +01:00
|
|
|
|
|
2020-06-02 17:11:52 +02:00
|
|
|
|
if (getMode() == Draw::Modeling && getIdObject() != NULL_ID)
|
|
|
|
|
{
|
2022-05-20 16:08:32 +02:00
|
|
|
|
name += nameStr.arg(getIdObject());
|
2020-06-02 17:11:52 +02:00
|
|
|
|
}
|
|
|
|
|
else if (VAbstractCurve::id() != NULL_ID)
|
2016-02-27 15:53:17 +01:00
|
|
|
|
{
|
2022-05-20 16:08:32 +02:00
|
|
|
|
name += nameStr.arg(VAbstractCurve::id());
|
2016-02-27 15:53:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GetDuplicate() > 0)
|
|
|
|
|
{
|
2022-05-20 16:08:32 +02:00
|
|
|
|
name += nameStr.arg(GetDuplicate());
|
2016-02-27 15:53:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setName(name);
|
2016-02-10 05:59:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-04 10:01:20 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
void VEllipticalArc::CreateAlias()
|
|
|
|
|
{
|
|
|
|
|
const QString aliasSuffix = GetAliasSuffix();
|
|
|
|
|
if (aliasSuffix.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
SetAlias(QString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetAlias(ELARC_ + aliasSuffix);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 05:59:29 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
void VEllipticalArc::FindF2(qreal length)
|
|
|
|
|
{
|
2016-02-24 16:34:03 +01:00
|
|
|
|
qreal gap = 180;
|
|
|
|
|
if (length < 0)
|
|
|
|
|
{
|
2016-04-10 16:40:55 +02:00
|
|
|
|
SetFlipped(true);
|
2016-02-24 16:34:03 +01:00
|
|
|
|
gap = -gap;
|
|
|
|
|
}
|
|
|
|
|
while (length > MaxLength())
|
|
|
|
|
{
|
2018-01-05 18:41:09 +01:00
|
|
|
|
length = MaxLength();
|
2016-02-24 16:34:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We need to calculate the second angle
|
|
|
|
|
// first approximation of angle between start and end angles
|
|
|
|
|
|
2017-09-13 08:17:21 +02:00
|
|
|
|
QLineF radius1(GetCenter().x(), GetCenter().y(), GetCenter().x() + d->radius1, GetCenter().y());
|
|
|
|
|
radius1.setAngle(GetStartAngle());
|
|
|
|
|
radius1.setAngle(radius1.angle() + gap);
|
|
|
|
|
qreal endAngle = radius1.angle();
|
2016-04-10 16:40:55 +02:00
|
|
|
|
|
|
|
|
|
// we need to set the end angle, because we want to use GetLength()
|
|
|
|
|
SetFormulaF2(QString::number(endAngle), endAngle);
|
2016-02-24 16:34:03 +01:00
|
|
|
|
|
|
|
|
|
qreal lenBez = GetLength(); // first approximation of length
|
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
const qreal eps = ToPixel(0.001, Unit::Mm);
|
2016-02-24 16:34:03 +01:00
|
|
|
|
|
|
|
|
|
while (qAbs(lenBez - length) > eps)
|
|
|
|
|
{
|
|
|
|
|
gap = gap/2;
|
2016-04-10 13:40:04 +02:00
|
|
|
|
if (gap < 0.0001)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-02-24 16:34:03 +01:00
|
|
|
|
if (lenBez > length)
|
|
|
|
|
{ // we selected too big end angle
|
2017-09-13 08:17:21 +02:00
|
|
|
|
radius1.setAngle(endAngle - qAbs(gap));
|
2016-02-24 16:34:03 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{ // we selected too little end angle
|
2017-09-13 08:17:21 +02:00
|
|
|
|
radius1.setAngle(endAngle + qAbs(gap));
|
2016-02-24 16:34:03 +01:00
|
|
|
|
}
|
2017-09-13 08:17:21 +02:00
|
|
|
|
endAngle = radius1.angle();
|
2016-02-26 20:27:55 +01:00
|
|
|
|
// we need to set d->f2, because we use it when we calculate GetLength
|
2016-04-10 16:40:55 +02:00
|
|
|
|
SetFormulaF2(QString::number(endAngle), endAngle);
|
2016-02-24 16:34:03 +01:00
|
|
|
|
lenBez = GetLength();
|
|
|
|
|
}
|
2016-02-10 05:59:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::MaxLength() const -> qreal
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
2016-04-10 13:40:04 +02:00
|
|
|
|
const qreal h = qPow(d->radius1 - d->radius2, 2) / qPow(d->radius1 + d->radius2, 2);
|
|
|
|
|
const qreal ellipseLength = M_PI * (d->radius1 + d->radius2) * (1+3*h/(10+qSqrt(4-3*h)));
|
2016-02-19 10:43:46 +01:00
|
|
|
|
return ellipseLength;
|
2016-02-10 05:59:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetP(qreal angle) const -> QPointF
|
2018-01-05 18:41:09 +01:00
|
|
|
|
{
|
2021-05-31 11:07:28 +02:00
|
|
|
|
if (qFuzzyIsNull(GetRadius1()) && qFuzzyIsNull(GetRadius2()))
|
|
|
|
|
{
|
|
|
|
|
return GetCenter().toQPointF();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
QLineF line(0, 0, 100, 0);
|
|
|
|
|
line.setAngle(angle);
|
|
|
|
|
|
2021-05-31 11:07:28 +02:00
|
|
|
|
const qreal a = not qFuzzyIsNull(GetRadius1()) ? line.p2().x() / GetRadius1() : 0;
|
|
|
|
|
const qreal b = not qFuzzyIsNull(GetRadius2()) ? line.p2().y() / GetRadius2() : 0;
|
2018-01-05 18:41:09 +01:00
|
|
|
|
const qreal k = qSqrt(a*a + b*b);
|
2021-05-31 11:07:28 +02:00
|
|
|
|
|
|
|
|
|
if (qFuzzyIsNull(k))
|
|
|
|
|
{
|
|
|
|
|
return GetCenter().toQPointF();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 18:41:09 +01:00
|
|
|
|
QPointF p(line.p2().x() / k, line.p2().y() / k);
|
|
|
|
|
|
|
|
|
|
QLineF line2(QPointF(), p);
|
|
|
|
|
SCASSERT(VFuzzyComparePossibleNulls(line2.angle(), line.angle()))
|
|
|
|
|
|
|
|
|
|
line2.setAngle(line2.angle() + GetRotationAngle());
|
|
|
|
|
return line2.p2() + VAbstractArc::GetCenter().toQPointF();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 18:56:30 +02:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-08-27 15:46:25 +02:00
|
|
|
|
auto VEllipticalArc::ArcPoints(QVector<QPointF> points) const -> QVector<QPointF>
|
2021-04-20 18:56:30 +02:00
|
|
|
|
{
|
2022-08-30 13:19:40 +02:00
|
|
|
|
if (points.size() < 2 || (qFuzzyIsNull(d->radius1) && qFuzzyIsNull(d->radius2)))
|
2022-08-27 15:46:25 +02:00
|
|
|
|
{
|
|
|
|
|
return points;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 13:19:40 +02:00
|
|
|
|
QPointF center = VAbstractArc::GetCenter().toQPointF();
|
2022-08-27 15:46:25 +02:00
|
|
|
|
qreal radius = qMax(d->radius1, d->radius2) * 2;
|
|
|
|
|
|
|
|
|
|
QLineF start(center.x(), center.y(), center.x() + radius, center.y());
|
|
|
|
|
start.setAngle(VAbstractArc::GetStartAngle());
|
|
|
|
|
|
|
|
|
|
QLineF end(center.x(), center.y(), center.x() + radius, center.y());
|
|
|
|
|
end.setAngle(VAbstractArc::GetEndAngle());
|
|
|
|
|
|
|
|
|
|
auto IsBoundedIntersection = [](QLineF::IntersectType type, QPointF p, const QLineF &segment1,
|
|
|
|
|
const QLineF &segment2)
|
|
|
|
|
{
|
|
|
|
|
return type == QLineF::BoundedIntersection ||
|
2022-08-30 13:19:40 +02:00
|
|
|
|
(type == QLineF::UnboundedIntersection &&
|
|
|
|
|
VGObject::IsPointOnLineSegment (p, segment1.p1(), segment2.p1()) &&
|
2022-08-27 15:46:25 +02:00
|
|
|
|
VGObject::IsPointOnLineSegment (p, segment2.p1(), segment2.p2()));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool begin = true;
|
|
|
|
|
|
2022-08-30 13:19:40 +02:00
|
|
|
|
if (start.angle() >= end.angle())
|
2022-08-27 15:46:25 +02:00
|
|
|
|
{
|
|
|
|
|
for (int i=0; i < points.size()-1; ++i)
|
|
|
|
|
{
|
|
|
|
|
QLineF edge(points.at(i), points.at(i+1));
|
|
|
|
|
|
|
|
|
|
QPointF p;
|
|
|
|
|
QLineF::IntersectType type = Intersects(start, edge, &p);
|
|
|
|
|
|
|
|
|
|
// QLineF::intersects not always accurate on edge cases
|
|
|
|
|
if (IsBoundedIntersection(type, p, edge, start))
|
|
|
|
|
{
|
|
|
|
|
QVector<QPointF> head = points.mid(0, i+1);
|
|
|
|
|
QVector<QPointF> tail = points.mid(i+1, -1);
|
2022-08-30 13:19:40 +02:00
|
|
|
|
|
|
|
|
|
tail = JoinVectors({p}, tail);
|
|
|
|
|
points = JoinVectors(tail, head);
|
|
|
|
|
points = JoinVectors(points, {p});
|
|
|
|
|
|
|
|
|
|
if (VFuzzyComparePossibleNulls(start.angle(), end.angle()))
|
|
|
|
|
{
|
|
|
|
|
return points;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 15:46:25 +02:00
|
|
|
|
begin = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-20 18:56:30 +02:00
|
|
|
|
|
2022-08-27 15:46:25 +02:00
|
|
|
|
QVector<QPointF> arc;
|
|
|
|
|
arc.reserve(points.size());
|
|
|
|
|
|
|
|
|
|
for (int i=0; i < points.size()-1; ++i)
|
2021-04-20 18:56:30 +02:00
|
|
|
|
{
|
2022-08-27 15:46:25 +02:00
|
|
|
|
QLineF edge(points.at(i), points.at(i+1));
|
|
|
|
|
|
|
|
|
|
if (begin)
|
|
|
|
|
{
|
|
|
|
|
QPointF p;
|
|
|
|
|
QLineF::IntersectType type = Intersects(start, edge, &p);
|
|
|
|
|
|
|
|
|
|
// QLineF::intersects not always accurate on edge cases
|
|
|
|
|
if (IsBoundedIntersection(type, p, edge, start))
|
|
|
|
|
{
|
|
|
|
|
arc.append(p);
|
|
|
|
|
begin = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QPointF p;
|
|
|
|
|
QLineF::IntersectType type = Intersects(end, edge, &p);
|
|
|
|
|
|
|
|
|
|
// QLineF::intersects not always accurate on edge cases
|
|
|
|
|
if (IsBoundedIntersection(type, p, edge, end))
|
|
|
|
|
{
|
|
|
|
|
arc.append(points.at(i));
|
|
|
|
|
arc.append(p);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arc.append(points.at(i));
|
|
|
|
|
}
|
2021-04-20 18:56:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 15:46:25 +02:00
|
|
|
|
if (arc.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
return points;
|
|
|
|
|
}
|
2021-04-20 18:56:30 +02:00
|
|
|
|
|
2022-08-27 15:46:25 +02:00
|
|
|
|
return arc;
|
2021-04-20 18:56:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 05:59:29 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
2016-12-17 02:41:33 +01:00
|
|
|
|
* @brief GetFormulaRadius1 return formula for major radius.
|
2016-02-10 05:59:29 +01:00
|
|
|
|
* @return radius.
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetFormulaRadius1() const -> QString
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
|
|
|
|
return d->formulaRadius1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
2016-12-17 02:41:33 +01:00
|
|
|
|
* @brief GetFormulaRadius2 return formula for minor radius.
|
2016-02-10 05:59:29 +01:00
|
|
|
|
* @return radius.
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetFormulaRadius2() const -> QString
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
|
|
|
|
return d->formulaRadius2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-02-12 19:19:46 +01:00
|
|
|
|
/**
|
2016-12-17 02:41:33 +01:00
|
|
|
|
* @brief GetFormulaRotationAngle return formula for rotation angle.
|
2016-02-12 19:19:46 +01:00
|
|
|
|
* @return rotationAngle.
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetFormulaRotationAngle() const -> QString
|
2016-02-12 19:19:46 +01:00
|
|
|
|
{
|
2016-12-17 02:41:33 +01:00
|
|
|
|
return d->formulaRotationAngle;
|
2016-02-12 19:19:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-02-10 05:59:29 +01:00
|
|
|
|
void VEllipticalArc::SetFormulaRadius1(const QString &formula, qreal value)
|
|
|
|
|
{
|
|
|
|
|
d->formulaRadius1 = formula;
|
|
|
|
|
d->radius1 = value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 11:07:28 +02:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
void VEllipticalArc::SetRadius1(qreal value)
|
|
|
|
|
{
|
|
|
|
|
d->formulaRadius1 = QString::number(value);
|
|
|
|
|
d->radius1 = value;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 05:59:29 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
void VEllipticalArc::SetFormulaRadius2(const QString &formula, qreal value)
|
|
|
|
|
{
|
|
|
|
|
d->formulaRadius2 = formula;
|
|
|
|
|
d->radius2 = value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 11:07:28 +02:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
void VEllipticalArc::SetRadius2(qreal value)
|
|
|
|
|
{
|
|
|
|
|
d->formulaRadius2 = QString::number(value);
|
|
|
|
|
d->radius2 = value;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 02:41:33 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
void VEllipticalArc::SetFormulaRotationAngle(const QString &formula, qreal value)
|
|
|
|
|
{
|
|
|
|
|
d->formulaRotationAngle = formula;
|
|
|
|
|
d->rotationAngle = value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 11:07:28 +02:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
void VEllipticalArc::SetRotationAngle(qreal value)
|
|
|
|
|
{
|
|
|
|
|
d->formulaRotationAngle = QString::number(value);
|
|
|
|
|
d->rotationAngle = value;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 05:59:29 +01:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
2016-12-17 02:41:33 +01:00
|
|
|
|
* @brief GetRadius1 return elliptical arc major radius.
|
2016-02-10 05:59:29 +01:00
|
|
|
|
* @return string with formula.
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetRadius1() const -> qreal
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
|
|
|
|
return d->radius1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
2016-12-17 02:41:33 +01:00
|
|
|
|
* @brief GetRadius2 return elliptical arc minor radius.
|
2016-02-10 05:59:29 +01:00
|
|
|
|
* @return string with formula.
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetRadius2() const -> qreal
|
2016-02-10 05:59:29 +01:00
|
|
|
|
{
|
|
|
|
|
return d->radius2;
|
|
|
|
|
}
|
2016-12-17 02:41:33 +01:00
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
|
|
|
* @brief GetRotationAngle return rotation angle.
|
|
|
|
|
* @return rotationAngle.
|
|
|
|
|
*/
|
2022-05-20 16:08:32 +02:00
|
|
|
|
auto VEllipticalArc::GetRotationAngle() const -> qreal
|
2016-12-17 02:41:33 +01:00
|
|
|
|
{
|
|
|
|
|
return d->rotationAngle;
|
|
|
|
|
}
|