From caba2db95bef877d3590a0705e4800de2c9a2133 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 12 Nov 2016 14:00:07 +0200 Subject: [PATCH] Make sure that a point is really lies on curve. --HG-- branch : feature --- src/libs/vgeometry/vabstractcurve.cpp | 27 ++++++++++++++++++ src/libs/vgeometry/vabstractcurve.h | 2 ++ src/libs/vpatterndb/vpiece.cpp | 40 +++++++++++++++++++-------- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/libs/vgeometry/vabstractcurve.cpp b/src/libs/vgeometry/vabstractcurve.cpp index d1f89096d..514964aec 100644 --- a/src/libs/vgeometry/vabstractcurve.cpp +++ b/src/libs/vgeometry/vabstractcurve.cpp @@ -236,6 +236,33 @@ bool VAbstractCurve::IsIntersectLine(const QLineF &line) const return not points.isEmpty(); } +//--------------------------------------------------------------------------------------------------------------------- +bool VAbstractCurve::IsPointOnCurve(const QPointF &p) const +{ + const QVector points = GetPoints(); + + if (points.isEmpty()) + { + return false; + } + else if (points.size() < 2) + { + return points.at(0) == p; + } + else + { + for (qint32 i = 0; i < points.count()-1; ++i) + { + if (IsPointOnLineSegment(p, points.at(i), points.at(i+1))) + { + return true; + } + } + } + + return false; +} + //--------------------------------------------------------------------------------------------------------------------- quint32 VAbstractCurve::GetDuplicate() const { diff --git a/src/libs/vgeometry/vabstractcurve.h b/src/libs/vgeometry/vabstractcurve.h index 3b58a8c2c..629572adf 100644 --- a/src/libs/vgeometry/vabstractcurve.h +++ b/src/libs/vgeometry/vabstractcurve.h @@ -70,6 +70,8 @@ public: virtual QVector IntersectLine(const QLineF &line) const; virtual bool IsIntersectLine(const QLineF &line) const; + bool IsPointOnCurve(const QPointF &p) const; + virtual qreal GetStartAngle () const=0; virtual qreal GetEndAngle () const=0; diff --git a/src/libs/vpatterndb/vpiece.cpp b/src/libs/vpatterndb/vpiece.cpp index aeef29177..faaf6d956 100644 --- a/src/libs/vpatterndb/vpiece.cpp +++ b/src/libs/vpatterndb/vpiece.cpp @@ -471,9 +471,13 @@ VSAPoint VPiece::StartSegment(const VContainer *data, int i, bool reverse) const if (at(CountNodes()-1).GetTypeTool() == Tool::NodePoint) { const VPieceNode node = at(CountNodes()-1); - begin = VSAPoint(*data->GeometricObject(node.GetId())); - begin.SetSAAfter(node.GetSAAfter()); - begin.SetSABefore(node.GetSABefore()); + const QPointF p = *data->GeometricObject(node.GetId()); + if (curve->IsPointOnCurve(p)) + { + begin = VSAPoint(p); + begin.SetSAAfter(node.GetSAAfter()); + begin.SetSABefore(node.GetSABefore()); + } } } else @@ -481,9 +485,13 @@ VSAPoint VPiece::StartSegment(const VContainer *data, int i, bool reverse) const if (at(i-1).GetTypeTool() == Tool::NodePoint) { const VPieceNode node = at(i-1); - begin = VSAPoint(*data->GeometricObject(node.GetId())); - begin.SetSAAfter(node.GetSAAfter()); - begin.SetSABefore(node.GetSABefore()); + const QPointF p = *data->GeometricObject(node.GetId()); + if (curve->IsPointOnCurve(p)) + { + begin = VSAPoint(p); + begin.SetSAAfter(node.GetSAAfter()); + begin.SetSABefore(node.GetSABefore()); + } } } } @@ -514,9 +522,13 @@ VSAPoint VPiece::EndSegment(const VContainer *data, int i, bool reverse) const if (at(0).GetTypeTool() == Tool::NodePoint) { const VPieceNode node = at(0); - end = VSAPoint(*data->GeometricObject(node.GetId())); - end.SetSAAfter(node.GetSAAfter()); - end.SetSABefore(node.GetSABefore()); + const QPointF p = *data->GeometricObject(node.GetId()); + if (curve->IsPointOnCurve(p)) + { + end = VSAPoint(p); + end.SetSAAfter(node.GetSAAfter()); + end.SetSABefore(node.GetSABefore()); + } } } else @@ -524,9 +536,13 @@ VSAPoint VPiece::EndSegment(const VContainer *data, int i, bool reverse) const if (at(i+1).GetTypeTool() == Tool::NodePoint) { const VPieceNode node = at(i+1); - end = VSAPoint(*data->GeometricObject(node.GetId())); - end.SetSAAfter(node.GetSAAfter()); - end.SetSABefore(node.GetSABefore()); + const QPointF p = *data->GeometricObject(node.GetId()); + if (curve->IsPointOnCurve(p)) + { + end = VSAPoint(p); + end.SetSAAfter(node.GetSAAfter()); + end.SetSABefore(node.GetSABefore()); + } } } }