valentina/src/libs/vgeometry/vellipticalarc.cpp

943 lines
33 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file vellipticalarc.cpp
** @author Valentina Zhuravska <zhuravska19(at)gmail.com>
** @date February 1, 2016
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2013-2015 Valentina project
** <https://gitlab.com/smart-pattern/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/>.
**
*************************************************************************/
#include "vellipticalarc.h"
#include <QLineF>
#include <QPainterPath>
2023-07-15 09:58:28 +02:00
#include <QPoint>
#include <QtDebug>
#include "../ifc/exception/vexception.h"
2023-07-15 09:58:28 +02:00
#include "../ifc/ifcdef.h"
#include "../vmisc/compatibility.h"
#include "../vmisc/def.h"
2022-08-27 15:46:25 +02:00
#include "../vmisc/fpm/fixed.hpp"
#include "../vmisc/fpm/math.hpp"
2023-07-15 09:58:28 +02:00
#include "../vmisc/vabstractapplication.h"
#include "../vmisc/vmath.h"
#include "vabstractcurve.h"
#include "vellipticalarc_p.h"
2022-08-27 15:46:25 +02:00
namespace
{
2023-07-15 09:58:28 +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)
{
2023-07-15 09:58:28 +02:00
return x + qMax(y / 8, y / 2 - x / 8);
2022-08-27 15:46:25 +02:00
}
2023-07-15 09:58:28 +02:00
return y + qMax(x / 8, x / 2 - y / 8);
2022-08-27 15:46:25 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
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);
2023-07-15 09:58:28 +02:00
return qMax(r1 + r1 / 16, r2 - r2 / 4);
2022-08-27 15:46:25 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
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;
}
//---------------------------------------------------------------------------------------------------------------------
2023-07-15 09:58:28 +02:00
inline void CircleGen(fpm::fixed_16_16 &u, fpm::fixed_16_16 &v, uint k)
2022-08-27 15:46:25 +02:00
{
u -= v >> k;
v += u >> k;
}
//---------------------------------------------------------------------------------------------------------------------
auto InitialValue(fpm::fixed_16_16 u0, fpm::fixed_16_16 v0, uint k) -> fpm::fixed_16_16
{
2023-07-15 09:58:28 +02:00
uint shift = 2 * k + 3;
fpm::fixed_16_16 w{u0 >> shift};
2022-08-27 15:46:25 +02:00
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,
2023-07-15 09:58:28 +02:00
fpm::fixed_16_16 xQ, fpm::fixed_16_16 yQ, fpm::fixed_16_16 sweep, fpm::fixed_16_16 flatness)
-> QVector<QPointF>
2022-08-27 15:46:25 +02:00
{
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;
}
//---------------------------------------------------------------------------------------------------------------------
2023-07-15 09:58:28 +02:00
auto EllipticArcPoints(QPointF c, qreal radius1, qreal radius2, qreal astart, qreal asweep, qreal approximationScale)
-> QVector<QPointF>
2022-08-27 15:46:25 +02:00
{
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
2023-07-15 09:58:28 +02:00
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};
2022-08-27 15:46:25 +02:00
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;
}
2023-07-15 09:58:28 +02:00
if (approximationScale < minCurveApproximationScale || approximationScale > maxCurveApproximationScale)
2022-08-27 15:46:25 +02:00
{
approximationScale = VAbstractApplication::VApp()->Settings()->GetCurveApproximationScale();
}
2023-07-15 09:58:28 +02:00
fpm::fixed_16_16 flatness{maxCurveApproximationScale / approximationScale * tolerance};
2022-08-27 15:46:25 +02:00
fpm::fixed_16_16 swangle{asweep};
QVector<QPointF> arc = EllipseCore(xC, yC, xP, yP, xQ, yQ, swangle, flatness);
// Arc end point
2023-07-15 09:58:28 +02:00
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)});
2022-08-27 15:46:25 +02:00
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(v.constLast(), p, accuracy))
2022-08-30 13:19:40 +02:00
{
v.append(p);
}
}
return v;
}
2023-07-15 09:58:28 +02:00
} // namespace
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief VEllipticalArc default constructor.
*/
VEllipticalArc::VEllipticalArc()
2023-07-15 09:58:28 +02:00
: VAbstractArc(GOType::EllipticalArc),
d(new VEllipticalArcData)
{
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @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).
*/
2023-07-15 09:58:28 +02:00
VEllipticalArc::VEllipticalArc(const VPointF &center, 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)
2023-07-15 09:58:28 +02:00
: VAbstractArc(GOType::EllipticalArc, center, f1, formulaF1, f2, formulaF2, idObject, mode),
d(new VEllipticalArcData(radius1, radius2, formulaRadius1, formulaRadius2, rotationAngle, formulaRotationAngle))
{
CreateName();
}
//---------------------------------------------------------------------------------------------------------------------
VEllipticalArc::VEllipticalArc(const VPointF &center, qreal radius1, qreal radius2, qreal f1, qreal f2,
qreal rotationAngle)
2023-07-15 09:58:28 +02:00
: VAbstractArc(GOType::EllipticalArc, center, f1, f2, NULL_ID, Draw::Calculation),
d(new VEllipticalArcData(radius1, radius2, rotationAngle))
{
CreateName();
}
//---------------------------------------------------------------------------------------------------------------------
VEllipticalArc::VEllipticalArc(qreal length, const QString &formulaLength, const VPointF &center, qreal radius1,
qreal radius2, const QString &formulaRadius1, const QString &formulaRadius2, qreal f1,
const QString &formulaF1, qreal rotationAngle, const QString &formulaRotationAngle,
quint32 idObject, Draw mode)
2023-07-15 09:58:28 +02:00
: VAbstractArc(GOType::EllipticalArc, formulaLength, center, f1, formulaF1, idObject, mode),
d(new VEllipticalArcData(radius1, radius2, formulaRadius1, formulaRadius2, rotationAngle, formulaRotationAngle))
{
CreateName();
FindF2(length);
}
//---------------------------------------------------------------------------------------------------------------------
VEllipticalArc::VEllipticalArc(qreal length, const VPointF &center, qreal radius1, qreal radius2, qreal f1,
qreal rotationAngle)
2023-07-15 09:58:28 +02:00
: VAbstractArc(GOType::EllipticalArc, center, f1, NULL_ID, Draw::Calculation),
d(new VEllipticalArcData(radius1, radius2, rotationAngle))
{
CreateName();
FindF2(length);
2022-11-21 17:49:41 +01:00
SetFormulaLength(QString::number(length));
}
//---------------------------------------------------------------------------------------------------------------------
2023-07-15 09:58:28 +02:00
COPY_CONSTRUCTOR_IMPL_2(VEllipticalArc, VAbstractArc)
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief operator = assignment operator
* @param arc arc
* @return arc
*/
2023-07-15 09:58:28 +02:00
auto VEllipticalArc::operator=(const VEllipticalArc &arc) -> VEllipticalArc &
{
2023-07-15 09:58:28 +02:00
if (&arc == this)
{
return *this;
}
VAbstractArc::operator=(arc);
d = arc.d;
return *this;
}
#ifdef Q_COMPILER_RVALUE_REFS
//---------------------------------------------------------------------------------------------------------------------
2023-07-13 16:49:20 +02:00
VEllipticalArc::VEllipticalArc(VEllipticalArc &&arc) noexcept
2023-07-15 09:58:28 +02:00
: VAbstractArc(std::move(arc)),
d(std::move(arc.d))
{
}
//---------------------------------------------------------------------------------------------------------------------
2023-07-13 16:49:20 +02:00
auto VEllipticalArc::operator=(VEllipticalArc &&arc) noexcept -> VEllipticalArc &
{
VAbstractArc::operator=(arc);
std::swap(d, arc.d);
return *this;
}
#endif
//---------------------------------------------------------------------------------------------------------------------
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::Rotate(QPointF originPoint, qreal degrees, const QString &prefix) const -> VEllipticalArc
{
originPoint = d->m_transform.inverted().map(originPoint);
QTransform t = d->m_transform;
t.translate(originPoint.x(), originPoint.y());
t.rotate(IsFlipped() ? degrees : -degrees);
t.translate(-originPoint.x(), -originPoint.y());
VEllipticalArc elArc(VAbstractArc::GetCenter(), GetRadius1(), GetRadius2(), VAbstractArc::GetStartAngle(),
VAbstractArc::GetEndAngle(), GetRotationAngle());
elArc.setName(name() + prefix);
2020-11-07 14:02:30 +01:00
if (not GetAliasSuffix().isEmpty())
{
elArc.SetAliasSuffix(GetAliasSuffix() + prefix);
}
elArc.SetColor(GetColor());
elArc.SetPenStyle(GetPenStyle());
elArc.SetFlipped(IsFlipped());
elArc.SetTransform(t);
2022-08-30 13:19:40 +02:00
elArc.SetApproximationScale(GetApproximationScale());
return elArc;
}
//---------------------------------------------------------------------------------------------------------------------
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::Flip(const QLineF &axis, const QString &prefix) const -> VEllipticalArc
{
VEllipticalArc elArc(VAbstractArc::GetCenter(), GetRadius1(), GetRadius2(), VAbstractArc::GetStartAngle(),
VAbstractArc::GetEndAngle(), GetRotationAngle());
elArc.setName(name() + prefix);
2020-11-07 14:02:30 +01:00
if (not GetAliasSuffix().isEmpty())
{
elArc.SetAliasSuffix(GetAliasSuffix() + prefix);
}
elArc.SetColor(GetColor());
elArc.SetPenStyle(GetPenStyle());
elArc.SetFlipped(not IsFlipped());
elArc.SetTransform(d->m_transform * VGObject::FlippingMatrix(d->m_transform.inverted().map(axis)));
2022-08-30 13:19:40 +02:00
elArc.SetApproximationScale(GetApproximationScale());
return elArc;
}
//---------------------------------------------------------------------------------------------------------------------
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::Move(qreal length, qreal angle, const QString &prefix) const -> VEllipticalArc
{
const VPointF oldCenter = VAbstractArc::GetCenter();
const VPointF center = oldCenter.Move(length, angle);
2023-07-15 09:58:28 +02:00
const QPointF position =
d->m_transform.inverted().map(center.toQPointF()) - d->m_transform.inverted().map(oldCenter.toQPointF());
QTransform t = d->m_transform;
t.translate(position.x(), position.y());
VEllipticalArc elArc(oldCenter, GetRadius1(), GetRadius2(), VAbstractArc::GetStartAngle(),
VAbstractArc::GetEndAngle(), GetRotationAngle());
elArc.setName(name() + prefix);
2020-11-07 14:02:30 +01:00
if (not GetAliasSuffix().isEmpty())
{
elArc.SetAliasSuffix(GetAliasSuffix() + prefix);
}
elArc.SetColor(GetColor());
elArc.SetPenStyle(GetPenStyle());
elArc.SetFlipped(IsFlipped());
elArc.SetTransform(t);
2022-08-30 13:19:40 +02:00
elArc.SetApproximationScale(GetApproximationScale());
return elArc;
}
//---------------------------------------------------------------------------------------------------------------------
2023-07-15 09:58:28 +02:00
VEllipticalArc::~VEllipticalArc() = default;
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetLength return arc length.
* @return length.
*/
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetLength() const -> qreal
{
qreal length = PathLength(GetPoints());
if (IsFlipped())
{
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
{
return GetTransform().map(GetP(VAbstractArc::GetStartAngle()));
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetP2 return point associated with end angle.
* @return point.
*/
2023-07-15 09:58:28 +02:00
auto VEllipticalArc::GetP2() const -> QPointF
{
return GetTransform().map(GetP(VAbstractArc::GetEndAngle()));
}
//---------------------------------------------------------------------------------------------------------------------
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetTransform() const -> QTransform
{
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
{
VPointF center = VAbstractArc::GetCenter();
const QPointF p = d->m_transform.map(center.toQPointF());
center.setX(p.x());
center.setY(p.y());
return center;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @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>
{
const QPointF center = VAbstractArc::GetCenter().toQPointF();
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);
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);
}
t.rotate(-GetRotationAngle());
t.translate(-center.x(), -center.y());
2022-08-27 15:46:25 +02:00
std::transform(points.begin(), points.end(), points.begin(), [t](const QPointF &point) { return t.map(point); });
2022-08-27 15:46:25 +02:00
return points;
}
//---------------------------------------------------------------------------------------------------------------------
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetStartAngle() const -> qreal
{
return QLineF(GetCenter().toQPointF(), GetP1()).angle() - GetRotationAngle();
}
//---------------------------------------------------------------------------------------------------------------------
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetEndAngle() const -> qreal
{
return QLineF(GetCenter().toQPointF(), GetP2()).angle() - GetRotationAngle();
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @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
{
2023-07-15 09:58:28 +02:00
// Always need return two arcs, so we must correct wrong length.
qreal len = 0;
const qreal fullLength = GetLength();
if (fullLength <= minLength)
{
arc1 = VEllipticalArc();
arc2 = VEllipticalArc();
const QString errorMsg = QObject::tr("Unable to cut curve '%1'. The curve is too short.").arg(name());
2023-07-15 09:58:28 +02:00
VAbstractApplication::VApp()->IsPedantic()
? throw VException(errorMsg)
: qWarning() << VAbstractApplication::warningMessageSignature + errorMsg;
2022-05-20 14:49:15 +02:00
return {};
}
const qreal maxLength = fullLength - minLength;
if (length < minLength)
{
len = minLength;
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 "
2023-07-15 09:58:28 +02:00
"value.")
.arg(name(), pointName);
2022-05-20 14:49:15 +02:00
}
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
}
2023-07-15 09:58:28 +02:00
VAbstractApplication::VApp()->IsPedantic()
? throw VException(errorMsg)
: qWarning() << VAbstractApplication::warningMessageSignature + errorMsg;
}
else if (length > maxLength)
{
len = maxLength;
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
}
2023-07-15 09:58:28 +02:00
VAbstractApplication::VApp()->IsPedantic()
? throw VException(errorMsg)
: qWarning() << VAbstractApplication::warningMessageSignature + errorMsg;
}
else
{
len = length;
}
// the first arc has given length and startAngle just like in the origin arc
2023-07-15 09:58:28 +02:00
arc1 = VEllipticalArc(len, QString().setNum(length), GetCenter(), d->radius1, d->radius2, d->formulaRadius1,
d->formulaRadius2, GetStartAngle(), GetFormulaF1(), d->rotationAngle,
2022-08-27 15:46:25 +02:00
GetFormulaRotationAngle(), getIdObject(), getMode());
// the second arc has startAngle just like endAngle of the first arc
// and it has endAngle just like endAngle of the origin arc
2023-07-15 09:58:28 +02:00
arc2 = VEllipticalArc(GetCenter(), d->radius1, d->radius2, d->formulaRadius1, d->formulaRadius2, arc1.GetEndAngle(),
arc1.GetFormulaF2(), GetEndAngle(), GetFormulaF2(), d->rotationAngle,
2022-08-27 15:46:25 +02:00
GetFormulaRotationAngle(), getIdObject(), getMode());
return arc1.GetP1();
}
//---------------------------------------------------------------------------------------------------------------------
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::CutArc(const qreal &length, const QString &pointName) const -> QPointF
{
VEllipticalArc arc1;
VEllipticalArc arc2;
2022-05-20 14:49:15 +02:00
return this->CutArc(length, arc1, arc2, pointName);
}
//---------------------------------------------------------------------------------------------------------------------
void VEllipticalArc::CreateName()
{
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");
if (getMode() == Draw::Modeling && getIdObject() != NULL_ID)
{
2022-05-20 16:08:32 +02:00
name += nameStr.arg(getIdObject());
}
else if (VAbstractCurve::id() != NULL_ID)
{
2022-05-20 16:08:32 +02:00
name += nameStr.arg(VAbstractCurve::id());
}
if (GetDuplicate() > 0)
{
2022-05-20 16:08:32 +02:00
name += nameStr.arg(GetDuplicate());
}
setName(name);
}
2020-11-04 10:01:20 +01:00
//---------------------------------------------------------------------------------------------------------------------
void VEllipticalArc::CreateAlias()
{
const QString aliasSuffix = GetAliasSuffix();
if (aliasSuffix.isEmpty())
{
SetAlias(QString());
return;
}
SetAlias(ELARC_ + aliasSuffix);
}
//---------------------------------------------------------------------------------------------------------------------
void VEllipticalArc::FindF2(qreal length)
{
qreal gap = 180;
if (length < 0)
{
SetFlipped(true);
gap = -gap;
}
while (length > MaxLength())
{
length = MaxLength();
}
// We need to calculate the second angle
// first approximation of angle between start and end angles
QLineF radius1(GetCenter().x(), GetCenter().y(), GetCenter().x() + d->radius1, GetCenter().y());
radius1.setAngle(GetStartAngle());
radius1.setAngle(radius1.angle() + gap);
qreal endAngle = radius1.angle();
// we need to set the end angle, because we want to use GetLength()
SetFormulaF2(QString::number(endAngle), endAngle);
qreal lenBez = GetLength(); // first approximation of length
const qreal eps = ToPixel(0.001, Unit::Mm);
while (qAbs(lenBez - length) > eps)
{
2023-07-15 09:58:28 +02:00
gap = gap / 2;
if (gap < 0.0001)
{
break;
}
if (lenBez > length)
{ // we selected too big end angle
radius1.setAngle(endAngle - qAbs(gap));
}
else
{ // we selected too little end angle
radius1.setAngle(endAngle + qAbs(gap));
}
endAngle = radius1.angle();
// we need to set d->f2, because we use it when we calculate GetLength
SetFormulaF2(QString::number(endAngle), endAngle);
lenBez = GetLength();
}
}
//---------------------------------------------------------------------------------------------------------------------
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::MaxLength() const -> qreal
{
const qreal h = qPow(d->radius1 - d->radius2, 2) / qPow(d->radius1 + d->radius2, 2);
2023-07-15 09:58:28 +02:00
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;
}
//---------------------------------------------------------------------------------------------------------------------
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetP(qreal angle) const -> QPointF
{
if (qFuzzyIsNull(GetRadius1()) && qFuzzyIsNull(GetRadius2()))
{
return GetCenter().toQPointF();
}
QLineF line(0, 0, 100, 0);
line.setAngle(angle);
const qreal a = not qFuzzyIsNull(GetRadius1()) ? line.p2().x() / GetRadius1() : 0;
const qreal b = not qFuzzyIsNull(GetRadius2()) ? line.p2().y() / GetRadius2() : 0;
2023-07-15 09:58:28 +02:00
const qreal k = qSqrt(a * a + b * b);
if (qFuzzyIsNull(k))
{
return GetCenter().toQPointF();
}
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();
}
//---------------------------------------------------------------------------------------------------------------------
2022-08-27 15:46:25 +02:00
auto VEllipticalArc::ArcPoints(QVector<QPointF> points) const -> QVector<QPointF>
{
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());
2023-07-15 09:58:28 +02:00
auto IsBoundedIntersection =
[](QLineF::IntersectType type, QPointF p, const QLineF &segment1, const QLineF &segment2)
2022-08-27 15:46:25 +02:00
{
return type == QLineF::BoundedIntersection ||
2022-08-30 13:19:40 +02:00
(type == QLineF::UnboundedIntersection &&
2023-07-15 09:58:28 +02:00
VGObject::IsPointOnLineSegment(p, segment1.p1(), segment2.p1()) &&
VGObject::IsPointOnLineSegment(p, segment2.p1(), segment2.p2()));
2022-08-27 15:46:25 +02:00
};
bool begin = true;
2022-08-30 13:19:40 +02:00
if (start.angle() >= end.angle())
2022-08-27 15:46:25 +02:00
{
2023-07-15 09:58:28 +02:00
for (int i = 0; i < points.size() - 1; ++i)
2022-08-27 15:46:25 +02:00
{
2023-07-15 09:58:28 +02:00
QLineF edge(points.at(i), points.at(i + 1));
2022-08-27 15:46:25 +02:00
QPointF p;
QLineF::IntersectType type = Intersects(start, edge, &p);
// QLineF::intersects not always accurate on edge cases
if (IsBoundedIntersection(type, p, edge, start))
{
2023-07-15 09:58:28 +02:00
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;
}
}
}
2022-08-27 15:46:25 +02:00
QVector<QPointF> arc;
arc.reserve(points.size());
2023-07-15 09:58:28 +02:00
for (int i = 0; i < points.size() - 1; ++i)
{
2023-07-15 09:58:28 +02:00
QLineF edge(points.at(i), points.at(i + 1));
2022-08-27 15:46:25 +02:00
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));
}
}
2022-08-27 15:46:25 +02:00
if (arc.isEmpty())
{
return points;
}
2022-08-27 15:46:25 +02:00
return arc;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetFormulaRadius1 return formula for major radius.
* @return radius.
*/
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetFormulaRadius1() const -> QString
{
return d->formulaRadius1;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetFormulaRadius2 return formula for minor radius.
* @return radius.
*/
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetFormulaRadius2() const -> QString
{
return d->formulaRadius2;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetFormulaRotationAngle return formula for rotation angle.
* @return rotationAngle.
*/
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetFormulaRotationAngle() const -> QString
{
return d->formulaRotationAngle;
}
//---------------------------------------------------------------------------------------------------------------------
void VEllipticalArc::SetFormulaRadius1(const QString &formula, qreal value)
{
d->formulaRadius1 = formula;
d->radius1 = value;
}
//---------------------------------------------------------------------------------------------------------------------
void VEllipticalArc::SetRadius1(qreal value)
{
d->formulaRadius1 = QString::number(value);
d->radius1 = value;
}
//---------------------------------------------------------------------------------------------------------------------
void VEllipticalArc::SetFormulaRadius2(const QString &formula, qreal value)
{
d->formulaRadius2 = formula;
d->radius2 = value;
}
//---------------------------------------------------------------------------------------------------------------------
void VEllipticalArc::SetRadius2(qreal value)
{
d->formulaRadius2 = QString::number(value);
d->radius2 = value;
}
//---------------------------------------------------------------------------------------------------------------------
void VEllipticalArc::SetFormulaRotationAngle(const QString &formula, qreal value)
{
d->formulaRotationAngle = formula;
d->rotationAngle = value;
}
//---------------------------------------------------------------------------------------------------------------------
void VEllipticalArc::SetRotationAngle(qreal value)
{
d->formulaRotationAngle = QString::number(value);
d->rotationAngle = value;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetRadius1 return elliptical arc major radius.
* @return string with formula.
*/
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetRadius1() const -> qreal
{
return d->radius1;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetRadius2 return elliptical arc minor radius.
* @return string with formula.
*/
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetRadius2() const -> qreal
{
return d->radius2;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetRotationAngle return rotation angle.
* @return rotationAngle.
*/
2022-05-20 16:08:32 +02:00
auto VEllipticalArc::GetRotationAngle() const -> qreal
{
return d->rotationAngle;
}