Refactoring.

This commit is contained in:
Roman Telezhynskyi 2020-08-01 11:55:56 +03:00
parent 2814ff073a
commit 07858f7fcb
3 changed files with 83 additions and 69 deletions

View File

@ -99,16 +99,17 @@ Q_DECLARE_METATYPE(VLayoutPassmark)
constexpr qreal accuracyPointOnLine = (0.117/*mm*/ / 25.4) * PrintDPI; constexpr qreal accuracyPointOnLine = (0.117/*mm*/ / 25.4) * PrintDPI;
Q_REQUIRED_RESULT static inline bool VFuzzyComparePoints(const QPointF &p1, const QPointF &p2); Q_REQUIRED_RESULT static inline bool VFuzzyComparePoints(const QPointF &p1, const QPointF &p2,
static inline bool VFuzzyComparePoints(const QPointF &p1, const QPointF &p2) qreal accuracy = accuracyPointOnLine);
static inline bool VFuzzyComparePoints(const QPointF &p1, const QPointF &p2, qreal accuracy)
{ {
return QLineF(p1, p2).length() <= accuracyPointOnLine; return QLineF(p1, p2).length() <= accuracy;
} }
Q_REQUIRED_RESULT static inline bool VFuzzyOnAxis(qreal v1, qreal v2); Q_REQUIRED_RESULT static inline bool VFuzzyOnAxis(qreal v1, qreal v2, qreal accuracy = accuracyPointOnLine);
static inline bool VFuzzyOnAxis(qreal v1, qreal v2) static inline bool VFuzzyOnAxis(qreal v1, qreal v2, qreal accuracy)
{ {
return qAbs(v1 - v2) <= accuracyPointOnLine; return qAbs(v1 - v2) <= accuracy;
} }
#endif // VGEOMETRYDEF_H #endif // VGEOMETRYDEF_H

View File

@ -1548,69 +1548,7 @@ bool VAbstractPiece::IsAllowanceValid(const QVector<QPointF> &base, const QVecto
return false; // Wrong direction return false; // Wrong direction
} }
// Edges must not intersect return IsInsidePolygon(base, allowance);
for (auto i = 0; i < base.count(); ++i)
{
int nextI = -1;
if (i < base.count()-1)
{
nextI = i + 1;
}
else
{
nextI = 0;
}
QLineF baseSegment(base.at(i), base.at(nextI));
if (baseSegment.isNull())
{
continue;
}
for (auto j = 0; j < allowance.count(); ++j)
{
int nextJ = -1;
if (j < allowance.count()-1)
{
nextJ = j + 1;
}
else
{
nextJ = 0;
}
QLineF allowanceSegment(allowance.at(j), allowance.at(nextJ));
if (allowanceSegment.isNull())
{
continue;
}
QPointF crosPoint;
const auto type = Intersects(baseSegment, allowanceSegment, &crosPoint);
if (type == QLineF::BoundedIntersection
&& not VFuzzyComparePoints(baseSegment.p1(), crosPoint)
&& not VFuzzyComparePoints(baseSegment.p2(), crosPoint)
&& not VGObject::IsPointOnLineviaPDP(allowanceSegment.p1(), baseSegment.p1(), baseSegment.p2())
&& not VGObject::IsPointOnLineviaPDP(allowanceSegment.p2(), baseSegment.p1(), baseSegment.p2()))
{
return false;
}
}
}
// Just instersection edges is not enough. The base must be inside of the allowance.
QPolygonF allowancePolygon(allowance);
for (auto &point : base)
{
if (not allowancePolygon.containsPoint(point, Qt::WindingFill))
{
return false;
}
}
return true;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -37,6 +37,7 @@
#include "../vmisc/diagnostic.h" #include "../vmisc/diagnostic.h"
#include "../vmisc/def.h" #include "../vmisc/def.h"
#include "../vmisc/compatibility.h"
#include "../vgeometry/vgobject.h" #include "../vgeometry/vgobject.h"
#include "vsapoint.h" #include "vsapoint.h"
#include "testpath.h" #include "testpath.h"
@ -100,6 +101,9 @@ public:
bool *needRollback = nullptr); bool *needRollback = nullptr);
static QLineF ParallelLine(const VSAPoint &p1, const VSAPoint &p2, qreal width); static QLineF ParallelLine(const VSAPoint &p1, const VSAPoint &p2, qreal width);
static bool IsAllowanceValid(const QVector<QPointF> &base, const QVector<QPointF> &allowance); static bool IsAllowanceValid(const QVector<QPointF> &base, const QVector<QPointF> &allowance);
template <class T>
static bool IsInsidePolygon(const QVector<T> &path, const QVector<T> &polygon,
qreal accuracy = accuracyPointOnLine);
template <class T> template <class T>
static QVector<T> CorrectEquidistantPoints(const QVector<T> &points, bool removeFirstAndLast = true); static QVector<T> CorrectEquidistantPoints(const QVector<T> &points, bool removeFirstAndLast = true);
@ -254,4 +258,75 @@ QVector<T> VAbstractPiece::RemoveDublicates(const QVector<T> &points, bool remov
return p; return p;
} }
//---------------------------------------------------------------------------------------------------------------------
template <class T>
bool VAbstractPiece::IsInsidePolygon(const QVector<T> &path, const QVector<T> &polygon, qreal accuracy)
{
// Edges must not intersect
for (auto i = 0; i < path.count(); ++i)
{
int nextI = -1;
if (i < path.count()-1)
{
nextI = i + 1;
}
else
{
nextI = 0;
}
QLineF baseSegment(path.at(i), path.at(nextI));
if (baseSegment.isNull())
{
continue;
}
for (auto j = 0; j < polygon.count(); ++j)
{
int nextJ = -1;
if (j < polygon.count()-1)
{
nextJ = j + 1;
}
else
{
nextJ = 0;
}
QLineF allowanceSegment(polygon.at(j), polygon.at(nextJ));
if (allowanceSegment.isNull())
{
continue;
}
QPointF crosPoint;
const auto type = Intersects(baseSegment, allowanceSegment, &crosPoint);
if (type == QLineF::BoundedIntersection
&& not VFuzzyComparePoints(baseSegment.p1(), crosPoint, accuracy)
&& not VFuzzyComparePoints(baseSegment.p2(), crosPoint, accuracy)
&& not VGObject::IsPointOnLineviaPDP(allowanceSegment.p1(), baseSegment.p1(), baseSegment.p2(),
accuracy)
&& not VGObject::IsPointOnLineviaPDP(allowanceSegment.p2(), baseSegment.p1(), baseSegment.p2(),
accuracy))
{
return false;
}
}
}
// Just instersection edges is not enough. The base must be inside of the allowance.
QPolygonF allowancePolygon(polygon);
for (auto &point : path)
{
if (not allowancePolygon.containsPoint(point, Qt::WindingFill))
{
return false;
}
}
return true;
}
#endif // VABSTRACTPIECE_H #endif // VABSTRACTPIECE_H