diff options
Diffstat (limited to 'src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp')
-rw-r--r-- | src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp | 73 |
1 files changed, 66 insertions, 7 deletions
diff --git a/src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp b/src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp index 5a42bf1f33..91886cf49e 100644 --- a/src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp +++ b/src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp @@ -54,24 +54,83 @@ QLineF GeometryUtilities::stretch(const QLineF &line, double p1Extension, double } bool GeometryUtilities::intersect(const QPolygonF &polygon, const QLineF &line, - QPointF *intersectionPoint, QLineF *intersectionLine) + QPointF *intersectionPoint, QLineF *intersectionLine, + int nearestPoint) { + bool found = false; + qreal mindist = 0; + QPointF ipoint; + QLineF iline; for (int i = 0; i <= polygon.size() - 2; ++i) { QLineF polygonLine(polygon.at(i), polygon.at(i+1)); + QPointF point; #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) - QLineF::IntersectType intersectionType = polygonLine.intersect(line, intersectionPoint); + QLineF::IntersectType intersectionType = polygonLine.intersect(line, &point); #else - QLineF::IntersectType intersectionType = polygonLine.intersects(line, intersectionPoint); + QLineF::IntersectType intersectionType = polygonLine.intersects(line, &point); #endif if (intersectionType == QLineF::BoundedIntersection) { - if (intersectionLine) - *intersectionLine = polygonLine; - return true; + qreal dist = QLineF(point, nearestPoint <= 0 ? line.p1() : line.p2()).length(); + if (!found || dist < mindist) { + mindist = dist; + ipoint = point; + iline = polygonLine; + found = true; + } + } + } + if (found) { + if (intersectionPoint) + *intersectionPoint = ipoint; + if (intersectionLine) + *intersectionLine = iline; + } + return found; +} + +bool GeometryUtilities::intersect(const QList<QPolygonF> &polygons, const QLineF &line, + int *intersectionPolygon, QPointF *intersectionPoint, + QLineF *intersectionLine, int nearestPoint) +{ + bool found = false; + qreal mindist = 0; + int ipolygon = -1; + QPointF ipoint; + QLineF iline; + for (int p = 0; p < polygons.size(); ++p) { + const QPolygonF polygon = polygons.at(p); + for (int i = 0; i <= polygon.size() - 2; ++i) { + const QLineF polygonLine(polygon.at(i), polygon.at(i + 1)); + QPointF point; +#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) + QLineF::IntersectType intersectionType = polygonLine.intersect(line, &point); +#else + QLineF::IntersectType intersectionType = polygonLine.intersects(line, &point); +#endif + if (intersectionType == QLineF::BoundedIntersection) { + qreal dist = QLineF(point, nearestPoint <= 0 ? line.p1() : line.p2()).length(); + if (!found || dist < mindist) { + mindist = dist; + ipolygon = p; + ipoint = point; + iline = polygonLine; + found = true; + } + } } } - return false; + if (found) { + if (intersectionPolygon) + *intersectionPolygon = ipolygon; + if (intersectionPoint) + *intersectionPoint = ipoint; + if (intersectionLine) + *intersectionLine = iline; + } + return found; } + namespace { class Candidate |