2013-12-29 17:48:57 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vgobject.cpp
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
2013-12-29 17:48:57 +01:00
|
|
|
** @date 27 12, 2013
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2013-12-29 17:48:57 +01:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2013-12-29 17:48:57 +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 "vgobject.h"
|
|
|
|
|
2019-08-29 14:01:27 +02:00
|
|
|
#include <QJsonObject>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QLine>
|
2014-10-23 10:38:57 +02:00
|
|
|
#include <QLineF>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QPoint>
|
2014-10-23 10:38:57 +02:00
|
|
|
#include <QPointF>
|
|
|
|
#include <QRectF>
|
2016-09-10 15:40:38 +02:00
|
|
|
#include <QTransform>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
2016-08-09 15:55:46 +02:00
|
|
|
#include "../vmisc/def.h"
|
2016-08-16 18:57:32 +02:00
|
|
|
#include "../vmisc/vmath.h"
|
2020-01-16 16:34:08 +01:00
|
|
|
#include "../vmisc/compatibility.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "../ifc/ifcdef.h"
|
|
|
|
#include "vgobject_p.h"
|
2020-11-04 13:33:40 +01:00
|
|
|
#include "../vmisc/vabstractapplication.h"
|
2014-10-23 10:38:57 +02:00
|
|
|
|
2019-08-29 14:14:17 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief PerpDotProduct Calculates the area of the parallelogram of the three points.
|
|
|
|
* This is actually the same as the area of the triangle defined by the three points, multiplied by 2.
|
|
|
|
* @return 2 * triangleArea(a,b,c)
|
|
|
|
*/
|
|
|
|
double PerpDotProduct(const QPointF &p1, const QPointF &p2, const QPointF &t)
|
|
|
|
{
|
|
|
|
return (p1.x() - t.x()) * (p2.y() - t.y()) - (p1.y() - t.y()) * (p2.x() - t.x());
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief GetEpsilon solve the floating-point accuraccy problem.
|
|
|
|
*
|
|
|
|
* There is the floating-point accuraccy problem, so instead of checking against zero, some epsilon value has to be
|
|
|
|
* used. Because the size of the pdp value depends on the length of the vectors, no static value can be used. One
|
|
|
|
* approach is to compare the pdp/area value to the fraction of another area which also depends on the length of the
|
|
|
|
* line e1=(p1, p2), e.g. the minimal area calucalted with PerpDotProduc() if point still not on the line. This distance
|
|
|
|
* is controled by variable accuracyPointOnLine
|
|
|
|
*/
|
|
|
|
double GetEpsilon(const QPointF &t, QPointF p1, QPointF p2, qreal accuracy)
|
|
|
|
{
|
|
|
|
QLineF edge1(p1, p2);
|
|
|
|
QLineF edge2(p1, t);
|
|
|
|
if (edge2.length() > edge1.length())
|
|
|
|
{
|
|
|
|
edge1.setLength(edge2.length());
|
|
|
|
p1 = edge1.p1();
|
|
|
|
p2 = edge1.p2();
|
|
|
|
}
|
|
|
|
|
|
|
|
QLineF line(p1, p2);
|
|
|
|
line.setAngle(line.angle() + 90);
|
|
|
|
line.setLength(accuracy); // less than accuracy means the same point
|
|
|
|
|
|
|
|
return qAbs(PerpDotProduct(p1, p2, line.p2()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief VGObject default constructor.
|
|
|
|
*/
|
2013-12-29 17:48:57 +01:00
|
|
|
VGObject::VGObject()
|
2014-08-20 14:01:35 +02:00
|
|
|
:d(new VGObjectData)
|
2014-05-02 13:11:30 +02:00
|
|
|
{}
|
2013-12-29 17:48:57 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief VGObject constructor.
|
|
|
|
* @param type type graphical object.
|
|
|
|
* @param idObject id parent object.
|
|
|
|
* @param mode mode creation. Used in modeling mode.
|
|
|
|
*/
|
2014-06-12 09:22:29 +02:00
|
|
|
VGObject::VGObject(const GOType &type, const quint32 &idObject, const Draw &mode)
|
2014-08-20 14:01:35 +02:00
|
|
|
:d(new VGObjectData(type, idObject, mode))
|
2014-05-02 13:11:30 +02:00
|
|
|
{}
|
2013-12-29 17:48:57 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief VGObject copy constructor.
|
|
|
|
* @param obj object.
|
|
|
|
*/
|
2013-12-29 17:48:57 +01:00
|
|
|
VGObject::VGObject(const VGObject &obj)
|
2014-08-20 14:01:35 +02:00
|
|
|
:d (obj.d)
|
2014-05-02 13:11:30 +02:00
|
|
|
{}
|
2013-12-29 17:48:57 +01:00
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief operator = assignment operator.
|
|
|
|
* @param obj object
|
|
|
|
* @return object
|
|
|
|
*/
|
2013-12-29 17:48:57 +01:00
|
|
|
VGObject &VGObject::operator=(const VGObject &obj)
|
|
|
|
{
|
2014-07-27 14:30:28 +02:00
|
|
|
if ( &obj == this )
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2014-08-20 14:01:35 +02:00
|
|
|
d = obj.d;
|
2013-12-29 17:48:57 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-12-30 12:00:57 +01:00
|
|
|
#ifdef Q_COMPILER_RVALUE_REFS
|
2019-12-30 16:13:18 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2022-08-12 17:50:13 +02:00
|
|
|
VGObject::VGObject(VGObject &&obj) Q_DECL_NOTHROW
|
|
|
|
:d (std::move(obj.d))
|
2019-12-30 16:13:18 +01:00
|
|
|
{}
|
2019-12-30 12:00:57 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VGObject &VGObject::operator=(VGObject &&obj) Q_DECL_NOTHROW
|
|
|
|
{
|
2019-12-30 16:13:18 +01:00
|
|
|
std::swap(d, obj.d);
|
2019-12-30 12:00:57 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-20 14:01:35 +02:00
|
|
|
VGObject::~VGObject()
|
|
|
|
{}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief getIdObject return parent id.
|
|
|
|
* @return parent id or 0 if object don't have parent.
|
|
|
|
*/
|
2014-02-25 15:40:24 +01:00
|
|
|
quint32 VGObject::getIdObject() const
|
2013-12-29 17:48:57 +01:00
|
|
|
{
|
2014-08-20 14:01:35 +02:00
|
|
|
return d->idObject;
|
2013-12-29 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief setIdObject set parent id.
|
|
|
|
* @param value parent id.
|
|
|
|
*/
|
2014-02-25 15:40:24 +01:00
|
|
|
void VGObject::setIdObject(const quint32 &value)
|
2013-12-29 17:48:57 +01:00
|
|
|
{
|
2014-08-20 14:01:35 +02:00
|
|
|
d->idObject = value;
|
2013-12-29 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief name return name graphical object.
|
|
|
|
* @return name
|
|
|
|
*/
|
2013-12-29 17:48:57 +01:00
|
|
|
QString VGObject::name() const
|
|
|
|
{
|
2014-08-20 14:01:35 +02:00
|
|
|
return d->_name;
|
2013-12-29 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief setName set name graphical object.
|
|
|
|
* @param name name graphical object.
|
|
|
|
*/
|
2013-12-29 17:48:57 +01:00
|
|
|
void VGObject::setName(const QString &name)
|
|
|
|
{
|
2014-08-20 14:01:35 +02:00
|
|
|
d->_name = name;
|
2013-12-29 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2014-06-01 09:35:13 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief getMode return mode creation.
|
|
|
|
* @return mode.
|
|
|
|
*/
|
2014-06-12 09:22:29 +02:00
|
|
|
Draw VGObject::getMode() const
|
2013-12-29 17:48:57 +01:00
|
|
|
{
|
2014-08-20 14:01:35 +02:00
|
|
|
return d->mode;
|
2013-12-29 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief setMode set mode creation.
|
|
|
|
* @param value mode.
|
|
|
|
*/
|
2014-06-12 09:22:29 +02:00
|
|
|
void VGObject::setMode(const Draw &value)
|
2013-12-29 17:48:57 +01:00
|
|
|
{
|
2014-08-20 14:01:35 +02:00
|
|
|
d->mode = value;
|
2013-12-29 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief getType return object type.
|
|
|
|
* @return type.
|
|
|
|
*/
|
2014-06-12 09:22:29 +02:00
|
|
|
GOType VGObject::getType() const
|
2013-12-29 17:48:57 +01:00
|
|
|
{
|
2014-08-20 14:01:35 +02:00
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-08-20 14:01:35 +02:00
|
|
|
void VGObject::setType(const GOType &type)
|
|
|
|
{
|
|
|
|
d->type = type;
|
2013-12-29 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief id return id object.
|
|
|
|
* @return id
|
|
|
|
*/
|
2014-02-25 15:40:24 +01:00
|
|
|
quint32 VGObject::id() const
|
2013-12-29 17:48:57 +01:00
|
|
|
{
|
2014-08-20 14:01:35 +02:00
|
|
|
return d->_id;
|
2013-12-29 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-01 09:35:13 +02:00
|
|
|
/**
|
|
|
|
* @brief setId set id object.
|
|
|
|
* @param id id.
|
|
|
|
*/
|
2014-02-25 15:40:24 +01:00
|
|
|
void VGObject::setId(const quint32 &id)
|
2013-12-29 17:48:57 +01:00
|
|
|
{
|
2014-08-20 14:01:35 +02:00
|
|
|
d->_id = id;
|
2013-12-29 17:48:57 +01:00
|
|
|
}
|
2014-10-23 10:38:57 +02:00
|
|
|
|
2020-11-04 09:55:34 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGObject::SetAlias(const QString &alias)
|
|
|
|
{
|
|
|
|
d->m_alias = alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VGObject::GetAlias() const
|
|
|
|
{
|
|
|
|
return d->m_alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGObject::SetAliasSuffix(const QString &aliasSuffix)
|
|
|
|
{
|
|
|
|
d->m_aliasSuffix = aliasSuffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VGObject::GetAliasSuffix() const
|
|
|
|
{
|
|
|
|
return d->m_aliasSuffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VGObject::ObjectName() const
|
|
|
|
{
|
2021-02-06 14:52:21 +01:00
|
|
|
const QString alias = VAbstractApplication::VApp()->TrVars()->VarToUser(d->m_alias);
|
|
|
|
const QString name = VAbstractApplication::VApp()->TrVars()->VarToUser(d->_name);
|
2020-11-04 13:33:40 +01:00
|
|
|
return not d->m_alias.isEmpty() ? QString("%1 (%2)").arg(alias, name) : name;
|
2020-11-04 09:55:34 +01:00
|
|
|
}
|
|
|
|
|
2014-10-23 10:38:57 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-02-02 14:34:29 +01:00
|
|
|
quint32 VGObject::getIdTool() const
|
|
|
|
{
|
|
|
|
if (d->mode == Draw::Calculation)
|
|
|
|
{
|
|
|
|
if (d->idObject != NULL_ID)
|
|
|
|
{
|
|
|
|
return d->idObject;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return d->_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return d->_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:01:27 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QJsonObject VGObject::ToJson() const
|
|
|
|
{
|
|
|
|
QJsonObject object
|
|
|
|
{
|
|
|
|
{"id", static_cast<qint64>(id())},
|
|
|
|
{"type", static_cast<int>(getType())},
|
|
|
|
};
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2016-02-02 14:34:29 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-10-23 10:38:57 +02:00
|
|
|
QLineF VGObject::BuildLine(const QPointF &p1, const qreal &length, const qreal &angle)
|
|
|
|
{
|
|
|
|
QLineF line = QLineF();
|
|
|
|
line.setP1(p1);
|
2015-11-25 14:45:38 +01:00
|
|
|
line.setAngle(angle);// First set angle then length. Length can have negative value.
|
2014-10-23 10:38:57 +02:00
|
|
|
line.setLength(length);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPointF VGObject::BuildRay(const QPointF &firstPoint, const qreal &angle, const QRectF &scRect)
|
|
|
|
{
|
2014-11-11 09:16:37 +01:00
|
|
|
QRectF rect = scRect;
|
2014-11-30 12:34:27 +01:00
|
|
|
if (rect.isValid() == false)
|
|
|
|
{
|
|
|
|
rect = QRectF(0, 0, 1200, 700);
|
|
|
|
}
|
2014-11-11 09:16:37 +01:00
|
|
|
if (rect.contains(firstPoint) == false)
|
|
|
|
{
|
2014-11-30 12:34:27 +01:00
|
|
|
// If point outside of scene rect create one around point and unite two rects.
|
|
|
|
QRectF rectangle(firstPoint.x()-rect.width()/2, firstPoint.y()-rect.height()/2, rect.width(), rect.height());
|
2014-11-11 09:16:37 +01:00
|
|
|
rect = rect.united(rectangle);
|
|
|
|
}
|
|
|
|
const qreal diagonal = qSqrt(pow(rect.height(), 2) + pow(rect.width(), 2));
|
|
|
|
const QLineF line = BuildLine(firstPoint, diagonal, angle);
|
2014-10-23 10:38:57 +02:00
|
|
|
|
2014-11-11 09:16:37 +01:00
|
|
|
return LineIntersectRect(rect, line);
|
2014-10-23 10:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QLineF VGObject::BuildAxis(const QPointF &p, const qreal &angle, const QRectF &scRect)
|
|
|
|
{
|
2014-11-11 09:16:37 +01:00
|
|
|
const QPointF endP1 = BuildRay(p, angle+180, scRect);
|
|
|
|
const QPointF endP2 = BuildRay(p, angle, scRect);
|
2014-10-23 10:38:57 +02:00
|
|
|
return QLineF(endP1, endP2);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QLineF VGObject::BuildAxis(const QPointF &p1, const QPointF &p2, const QRectF &scRect)
|
|
|
|
{
|
|
|
|
QLineF line(p1, p2);
|
|
|
|
return BuildAxis(p1, line.angle(), scRect);
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:43:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VGObject::ContactPoints(const QPointF &p, const QPointF ¢er, qreal radius, QPointF &p1, QPointF &p2)
|
|
|
|
{
|
|
|
|
const int flag = PointInCircle(p, center, radius);
|
|
|
|
|
|
|
|
if (flag == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag == 1)
|
|
|
|
{
|
|
|
|
p1 = p;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const double d = QLineF (p, center).length();
|
|
|
|
const double k = sqrt (d * d - radius * radius);
|
|
|
|
return IntersectionCircles(p, k, center, radius, p1, p2);
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:38:57 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief LineIntersectRect find point intersection line and rect.
|
|
|
|
* @param rec rect.
|
|
|
|
* @param line line.
|
|
|
|
* @return point intersection.
|
|
|
|
*/
|
2014-11-11 09:16:37 +01:00
|
|
|
QPointF VGObject::LineIntersectRect(const QRectF &rec, const QLineF &line)
|
2014-10-23 10:38:57 +02:00
|
|
|
{
|
|
|
|
qreal x1, y1, x2, y2;
|
|
|
|
rec.getCoords(&x1, &y1, &x2, &y2);
|
|
|
|
QPointF point;
|
2020-01-16 16:34:08 +01:00
|
|
|
QLineF::IntersectType type = Intersects(line, QLineF(QPointF(x1, y1), QPointF(x1, y2)), &point);
|
2020-01-06 17:34:35 +01:00
|
|
|
|
2014-10-23 10:38:57 +02:00
|
|
|
if ( type == QLineF::BoundedIntersection )
|
|
|
|
{
|
|
|
|
return point;
|
|
|
|
}
|
2020-01-06 17:34:35 +01:00
|
|
|
|
2020-01-16 16:34:08 +01:00
|
|
|
type = Intersects(line, QLineF(QPointF(x1, y1), QPointF(x2, y1)), &point);
|
2020-01-06 17:34:35 +01:00
|
|
|
|
2014-10-23 10:38:57 +02:00
|
|
|
if ( type == QLineF::BoundedIntersection )
|
|
|
|
{
|
|
|
|
return point;
|
|
|
|
}
|
2020-01-06 17:34:35 +01:00
|
|
|
|
2020-01-16 16:34:08 +01:00
|
|
|
type = Intersects(line, QLineF(QPointF(x1, y2), QPointF(x2, y2)), &point);
|
2020-01-06 17:34:35 +01:00
|
|
|
|
2014-10-23 10:38:57 +02:00
|
|
|
if ( type == QLineF::BoundedIntersection )
|
|
|
|
{
|
|
|
|
return point;
|
|
|
|
}
|
2020-01-06 17:34:35 +01:00
|
|
|
|
2020-01-16 16:34:08 +01:00
|
|
|
type = Intersects(line, QLineF(QPointF(x2, y1), QPointF(x2, y2)), &point);
|
2020-01-06 17:34:35 +01:00
|
|
|
|
2014-10-23 10:38:57 +02:00
|
|
|
if ( type == QLineF::BoundedIntersection )
|
|
|
|
{
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2015-05-25 14:49:28 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VGObject::IntersectionCircles(const QPointF &c1, double r1, const QPointF &c2, double r2, QPointF &p1, QPointF &p2)
|
|
|
|
{
|
2016-03-23 15:09:30 +01:00
|
|
|
if (VFuzzyComparePossibleNulls(c1.x(), c2.x()) && VFuzzyComparePossibleNulls(c1.y(), c2.y())
|
|
|
|
&& VFuzzyComparePossibleNulls(r1, r2))
|
2015-05-25 14:49:28 +02:00
|
|
|
{
|
|
|
|
return 3;// Circles are equal
|
|
|
|
}
|
2015-05-27 18:24:20 +02:00
|
|
|
const double a = - 2.0 * (c2.x() - c1.x());
|
|
|
|
const double b = - 2.0 * (c2.y() - c1.y());
|
|
|
|
const double c = (c2.x() - c1.x())* (c2.x() - c1.x()) + (c2.y() - c1.y()) * (c2.y() - c1.y()) + r1 * r1 - r2 * r2;
|
|
|
|
|
|
|
|
const double x0 = -a*c/(a*a+b*b);
|
|
|
|
const double y0 = -b*c/(a*a+b*b);
|
|
|
|
|
|
|
|
if (c*c > r1*r1*(a*a+b*b))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-23 15:09:30 +01:00
|
|
|
else if (VFuzzyComparePossibleNulls(c*c, r1*r1*(a*a+b*b)))
|
2015-05-27 18:24:20 +02:00
|
|
|
{
|
|
|
|
p1 = QPointF(x0 + c1.x(), y0 + c1.y());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const double d = r1*r1 - c*c/(a*a+b*b);
|
|
|
|
const double mult = sqrt (d / (a*a+b*b));
|
|
|
|
|
|
|
|
const double ax = x0 + b * mult;
|
|
|
|
const double bx = x0 - b * mult;
|
|
|
|
const double ay = y0 - a * mult;
|
|
|
|
const double by = y0 + a * mult;
|
|
|
|
|
|
|
|
p1 = QPointF(ax + c1.x(), ay + c1.y());
|
|
|
|
p2 = QPointF(bx + c1.x(), by + c1.y());
|
|
|
|
return 2;
|
|
|
|
}
|
2015-05-25 14:49:28 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 10:38:57 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief LineIntersectCircle find point intersection line and circle.
|
|
|
|
* @param center arc center.
|
|
|
|
* @param radius arc radius.
|
|
|
|
* @param line line
|
|
|
|
* @param p1 first intersection point.
|
|
|
|
* @param p2 second intersection point.
|
|
|
|
* @return 0 - intersection doesn't exist, 1 - one intersection point, 2 - two intersection points.
|
|
|
|
*/
|
|
|
|
qint32 VGObject::LineIntersectCircle(const QPointF ¢er, qreal radius, const QLineF &line, QPointF &p1, QPointF &p2)
|
|
|
|
{
|
2016-05-07 10:37:46 +02:00
|
|
|
// Fix for issue #485. https://bitbucket.org/dismine/valentina/issues/485/error-when-drawing-a-curved-path
|
|
|
|
if (qFuzzyIsNull(line.length()))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:38:57 +02:00
|
|
|
//coefficient for equation of segment
|
|
|
|
qreal a = 0, b = 0, c = 0;
|
|
|
|
LineCoefficients(line, &a, &b, &c);
|
|
|
|
// projection center of circle on to line
|
2015-05-25 14:51:44 +02:00
|
|
|
const QPointF p = ClosestPoint (line, center);
|
2014-10-23 10:38:57 +02:00
|
|
|
// how many solutions?
|
|
|
|
qint32 flag = 0;
|
2015-05-25 14:51:44 +02:00
|
|
|
const qreal d = QLineF (center, p).length();
|
2016-03-23 15:09:30 +01:00
|
|
|
if (VFuzzyComparePossibleNulls(d, radius))
|
2014-10-23 10:38:57 +02:00
|
|
|
{
|
|
|
|
flag = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (radius > d)
|
|
|
|
{
|
|
|
|
flag = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// find distance from projection to points of intersection
|
2015-05-25 14:51:44 +02:00
|
|
|
const qreal k = qSqrt (qAbs(radius * radius - d * d));
|
|
|
|
const qreal t = QLineF (QPointF (0, 0), QPointF (b, - a)).length();
|
2014-10-23 10:38:57 +02:00
|
|
|
// add to projection a vectors aimed to points of intersection
|
|
|
|
p1 = addVector (p, QPointF (0, 0), QPointF (- b, a), k / t);
|
|
|
|
p2 = addVector (p, QPointF (0, 0), QPointF (b, - a), k / t);
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief ClosestPoint find point projection of point onto line.
|
|
|
|
* @param line line.
|
|
|
|
* @return point on line or extended line if origin size too small.
|
|
|
|
*/
|
|
|
|
QPointF VGObject::ClosestPoint(const QLineF &line, const QPointF &point)
|
|
|
|
{
|
|
|
|
qreal a = 0, b = 0, c = 0;
|
|
|
|
LineCoefficients(line, &a, &b, &c);
|
|
|
|
qreal x = point.x() + a;
|
|
|
|
qreal y = b + point.y();
|
|
|
|
QLineF lin (point, QPointF(x, y));
|
|
|
|
QPointF p;
|
2020-01-16 16:34:08 +01:00
|
|
|
QLineF::IntersectType intersect = Intersects(line, lin, &p);
|
2020-01-06 17:34:35 +01:00
|
|
|
|
2014-10-23 10:38:57 +02:00
|
|
|
if (intersect == QLineF::UnboundedIntersection || intersect == QLineF::BoundedIntersection)
|
|
|
|
{
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-25 10:42:08 +02:00
|
|
|
return point;
|
2014-10-23 10:38:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPointF VGObject::addVector(const QPointF &p, const QPointF &p1, const QPointF &p2, qreal k)
|
|
|
|
{
|
|
|
|
return QPointF (p.x() + (p2.x() - p1.x()) * k, p.y() + (p2.y() - p1.y()) * k);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief LineCoefficients coefficient for equation of segment. Segment equestion ax+by+c=0.
|
|
|
|
* @param line line
|
|
|
|
* @param a a value
|
|
|
|
* @param b b value
|
|
|
|
* @param c c value
|
|
|
|
*/
|
|
|
|
void VGObject::LineCoefficients(const QLineF &line, qreal *a, qreal *b, qreal *c)
|
|
|
|
{
|
|
|
|
//coefficient for equation of segment
|
|
|
|
QPointF p1 = line.p1();
|
|
|
|
*a = line.p2().y() - p1.y();
|
|
|
|
*b = p1.x() - line.p2().x();
|
|
|
|
*c = - *a * p1.x() - *b * p1.y();
|
|
|
|
}
|
2014-12-15 15:25:40 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-05-07 11:39:08 +02:00
|
|
|
/**
|
|
|
|
* @brief IsPointOnLineSegment Check if the point is on the line segment.
|
|
|
|
*/
|
2021-09-24 11:51:39 +02:00
|
|
|
auto VGObject::IsPointOnLineSegment(const QPointF &t, const QPointF &p1, const QPointF &p2, qreal accuracy) -> bool
|
2014-12-15 15:25:40 +01:00
|
|
|
{
|
2021-09-24 11:51:39 +02:00
|
|
|
// Because of accuracy issues, this operation is slightly different from ordinary checking point on segment.
|
|
|
|
// Here we deal with more like cigar shape.
|
|
|
|
|
|
|
|
// Front and rear easy to check
|
2022-11-11 16:57:34 +01:00
|
|
|
if (VFuzzyComparePoints(p1, t, accuracy) || VFuzzyComparePoints(p2, t, accuracy))
|
2021-09-24 11:51:39 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-19 13:11:57 +02:00
|
|
|
|
2021-09-24 11:51:39 +02:00
|
|
|
// Check if we have a segment. On previous step we already confirmed that we don't have intersection
|
2022-11-11 16:57:34 +01:00
|
|
|
if (VFuzzyComparePoints(p1, p2, accuracy))
|
2017-10-19 13:11:57 +02:00
|
|
|
{
|
2021-09-24 11:51:39 +02:00
|
|
|
return false;
|
2015-05-07 11:39:08 +02:00
|
|
|
}
|
2014-12-15 15:25:40 +01:00
|
|
|
|
2021-09-24 11:51:39 +02:00
|
|
|
// Calculate the main rectangle shape. QLineF is not 100% accurate in calculating positions for points, but this
|
|
|
|
// should be good enough for us.
|
|
|
|
const qreal degrees = 90;
|
|
|
|
QLineF edge(p1, p2);
|
|
|
|
edge.setAngle(edge.angle()+degrees);
|
|
|
|
edge.setLength(accuracy);
|
|
|
|
QPointF sP1 = edge.p2();
|
|
|
|
|
|
|
|
edge = QLineF(p2, p1);
|
|
|
|
edge.setAngle(edge.angle()-degrees);
|
|
|
|
edge.setLength(accuracy);
|
|
|
|
QPointF sP2 = edge.p2();
|
|
|
|
|
|
|
|
edge = QLineF(p2, p1);
|
|
|
|
edge.setAngle(edge.angle()+degrees);
|
|
|
|
edge.setLength(accuracy);
|
|
|
|
QPointF sP3 = edge.p2();
|
|
|
|
|
|
|
|
edge = QLineF(p1, p2);
|
|
|
|
edge.setAngle(edge.angle()-degrees);
|
|
|
|
edge.setLength(accuracy);
|
|
|
|
QPointF sP4 = edge.p2();
|
|
|
|
|
|
|
|
QVector<QPointF> shape{sP1, sP2, sP3, sP4, sP1};
|
|
|
|
|
|
|
|
for (int i=0; i < shape.size()-1; ++i)
|
2014-12-15 15:25:40 +01:00
|
|
|
{
|
2021-09-24 11:51:39 +02:00
|
|
|
const QPointF &sp1 = shape.at(i);
|
|
|
|
const QPointF &sp2 = shape.at(i+1);
|
|
|
|
// This formula helps to determine on which side of a vector lies a point.
|
|
|
|
qreal position = (sp2.x()-sp1.x())*(t.y()-sp1.y())-(sp2.y()-sp1.y())*(t.x()-sp1.x());
|
|
|
|
if (position < 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-15 15:25:40 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 11:51:39 +02:00
|
|
|
return true;
|
2015-05-07 11:39:08 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 15:37:55 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VGObject::IsLineSegmentOnLineSegment(const QLineF &seg1, const QLineF &seg2, qreal accuracy)
|
|
|
|
{
|
|
|
|
const bool onLine = IsPointOnLineviaPDP(seg1.p1(), seg2.p1(), seg2.p2(), accuracy) &&
|
|
|
|
IsPointOnLineviaPDP(seg1.p2(), seg2.p1(), seg2.p2(), accuracy);
|
|
|
|
if (onLine)
|
|
|
|
{
|
|
|
|
return IsPointOnLineSegment(seg1.p1(), seg2.p1(), seg2.p2(), accuracy) ||
|
|
|
|
IsPointOnLineSegment(seg1.p2(), seg2.p1(), seg2.p2(), accuracy) ||
|
|
|
|
IsPointOnLineSegment(seg2.p1(), seg1.p1(), seg1.p2(), accuracy) ||
|
|
|
|
IsPointOnLineSegment(seg2.p2(), seg1.p1(), seg1.p2(), accuracy);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return onLine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 11:39:08 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-10-19 15:54:06 +02:00
|
|
|
QPointF VGObject::CorrectDistortion(const QPointF &t, const QPointF &p1, const QPointF &p2)
|
|
|
|
{
|
|
|
|
if (not VFuzzyComparePoints(p1, p2))
|
|
|
|
{
|
2018-10-19 18:48:36 +02:00
|
|
|
return VGObject::ClosestPoint(QLineF(p1, p2), t);
|
2018-10-19 15:54:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-05-07 11:39:08 +02:00
|
|
|
/**
|
|
|
|
* @brief IsPointOnLineviaPDP use the perp dot product (PDP) way.
|
|
|
|
*
|
|
|
|
* The pdp is zero only if the t lies on the line e1 = vector from p1 to p2.
|
2016-12-08 12:56:17 +01:00
|
|
|
* @return true if point is on line
|
2015-05-07 11:39:08 +02:00
|
|
|
*/
|
2019-07-21 08:00:42 +02:00
|
|
|
bool VGObject::IsPointOnLineviaPDP(const QPointF &t, const QPointF &p1, const QPointF &p2, qreal accuracy)
|
2015-05-07 11:39:08 +02:00
|
|
|
{
|
2017-02-12 14:02:07 +01:00
|
|
|
const double p = qAbs(PerpDotProduct(p1, p2, t));
|
2019-08-29 14:14:17 +02:00
|
|
|
const double e = GetEpsilon(t, p1, p2, accuracy);
|
|
|
|
|
2017-02-12 12:20:41 +01:00
|
|
|
// We can't use common "<=" here because of the floating-point accuraccy problem
|
|
|
|
return p < e || VFuzzyComparePossibleNulls(p, e);
|
2014-12-15 15:25:40 +01:00
|
|
|
}
|
|
|
|
|
2015-06-05 15:43:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VGObject::PointInCircle(const QPointF &p, const QPointF ¢er, qreal radius)
|
|
|
|
{
|
|
|
|
const double d = QLineF (p, center).length();
|
2016-03-23 15:09:30 +01:00
|
|
|
if (VFuzzyComparePossibleNulls(radius, d))
|
2015-06-05 15:43:41 +02:00
|
|
|
{
|
|
|
|
return 1; // on circle
|
|
|
|
}
|
|
|
|
if (radius > d)
|
|
|
|
{
|
|
|
|
return 0; // outside circle
|
|
|
|
}
|
|
|
|
return 2; // inside circle
|
|
|
|
}
|
|
|
|
|
2014-12-16 14:33:00 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief GetLengthContour return length of contour.
|
|
|
|
* @param contour container with points of contour.
|
|
|
|
* @param newPoints point whos we try to add to contour.
|
|
|
|
* @return length length of contour.
|
|
|
|
*/
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-12-16 14:33:00 +01:00
|
|
|
int VGObject::GetLengthContour(const QVector<QPointF> &contour, const QVector<QPointF> &newPoints)
|
|
|
|
{
|
|
|
|
qreal length = 0;
|
|
|
|
QVector<QPointF> points;
|
|
|
|
points << contour << newPoints;
|
|
|
|
for (qint32 i = 0; i < points.size()-1; ++i)
|
|
|
|
{
|
|
|
|
QLineF line(points.at(i), points.at(i+1));
|
|
|
|
length += line.length();
|
|
|
|
}
|
|
|
|
return qFloor(length);
|
|
|
|
}
|
2016-09-10 15:40:38 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QTransform VGObject::FlippingMatrix(const QLineF &axis)
|
|
|
|
{
|
|
|
|
QTransform matrix;
|
|
|
|
|
|
|
|
if (axis.isNull())
|
|
|
|
{
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QLineF axisOX = QLineF(axis.x2(), axis.y2(), axis.x2() + 100, axis.y2()); // Ox axis
|
|
|
|
|
|
|
|
const qreal angle = axis.angleTo(axisOX);
|
|
|
|
const QPointF p2 = axis.p2();
|
|
|
|
|
|
|
|
QTransform m;
|
|
|
|
m.translate(p2.x(), p2.y());
|
|
|
|
m.rotate(-angle);
|
|
|
|
m.translate(-p2.x(), -p2.y());
|
|
|
|
matrix *= m;
|
|
|
|
|
|
|
|
m.reset();
|
|
|
|
m.translate(p2.x(), p2.y());
|
|
|
|
m.scale(m.m11(), m.m22()*-1);
|
|
|
|
m.translate(-p2.x(), -p2.y());
|
|
|
|
matrix *= m;
|
|
|
|
|
|
|
|
m.reset();
|
|
|
|
m.translate(p2.x(), p2.y());
|
|
|
|
m.rotate(-(360-angle));
|
|
|
|
m.translate(-p2.x(), -p2.y());
|
|
|
|
matrix *= m;
|
|
|
|
|
|
|
|
return matrix;
|
|
|
|
}
|