diff --git a/src/MissionManager/ComplexMissionItem.cc b/src/MissionManager/ComplexMissionItem.cc index eaed174ea84cc274d4c5e017741a9857bea5fa3f..4cd7cffb84d779052ea33a023ab8b3cd3663e13b 100644 --- a/src/MissionManager/ComplexMissionItem.cc +++ b/src/MissionManager/ComplexMissionItem.cc @@ -25,6 +25,8 @@ This file is part of the QGROUNDCONTROL project #include "MissionController.h" #include "QGCGeo.h" +#include + const char* ComplexMissionItem::_jsonVersionKey = "version"; const char* ComplexMissionItem::_jsonTypeKey = "type"; const char* ComplexMissionItem::_jsonPolygonKey = "polygon"; @@ -252,17 +254,17 @@ void ComplexMissionItem::_generateGrid(void) _gridPoints.clear(); - QList polygonPoints; - QList gridPoints; + QList polygonPoints; + QList gridPoints; - // Convert polygon to NED + // Convert polygon to Qt coordinate system (y positive is down) qDebug() << "Convert polygon"; QGeoCoordinate tangentOrigin = _polygonPath[0].value(); for (int i=0; i<_polygonPath.count(); i++) { - double x, y, z; - convertGeoToNed(_polygonPath[i].value(), tangentOrigin, &x, &y, &z); - polygonPoints += Point_t(x, y); - qDebug() << _polygonPath[i].value() << polygonPoints.last().x << polygonPoints.last().y; + double y, x, down; + convertGeoToNed(_polygonPath[i].value(), tangentOrigin, &y, &x, &down); + polygonPoints += QPointF(x, -y); + qDebug() << _polygonPath[i].value() << polygonPoints.last().x() << polygonPoints.last().y(); } // Generate grid @@ -270,10 +272,10 @@ void ComplexMissionItem::_generateGrid(void) // Convert to Geo and set altitude for (int i=0; i()); _setExitCoordinate(_gridPoints.last().value()); } +} + +QPointF ComplexMissionItem::_rotatePoint(const QPointF& point, const QPointF& origin, double angle) +{ + QPointF rotated; + double radians = (M_PI / 180.0) * angle; + rotated.setX(((point.x() - origin.x()) * cos(radians)) - ((point.y() - origin.y()) * sin(radians)) + origin.x()); + rotated.setY(((point.x() - origin.x()) * sin(radians)) + ((point.y() - origin.y()) * cos(radians)) + origin.y()); + + return rotated; } -void ComplexMissionItem::_gridGenerator(const QList& polygonPoints, QList& gridPoints) +void ComplexMissionItem::_intersectLinesWithRect(const QList& lineList, const QRectF& boundRect, QList& resultLines) { - // FIXME: Hack implementataion + QLineF topLine (boundRect.topLeft(), boundRect.topRight()); + QLineF bottomLine (boundRect.bottomLeft(), boundRect.bottomRight()); + QLineF leftLine (boundRect.topLeft(), boundRect.bottomLeft()); + QLineF rightLine (boundRect.topRight(), boundRect.bottomRight()); - gridPoints.clear(); + for (int i=0; i& lineList, const QPolygonF& polygon, QList& resultLines) +{ + for (int i=0; i& polygonPoints, QList& gridPoints) +{ + double gridAngle = _gridAngleFact.rawValue().toDouble(); - QList> lineList; + gridPoints.clear(); - double x = upperLeft.x; - double gridSpacing = _gridSpacingFact.rawValue().toDouble(); - while (x < lowerRight.x) { - double yTop = upperLeft.y; - double yBottom = lowerRight.y; + // Convert polygon to bounding rect - lineList += qMakePair(Point_t(x, yTop), Point_t(x, yBottom)); - qDebug() << "line" << lineList.last().first.x<< lineList.last().first.y<< lineList.last().second.x<< lineList.last().second.y; + qDebug() << "Polygon"; + QPolygonF polygon; + for (int i=0; i lineList; + float x = largeBoundRect.topLeft().x(); + float gridSpacing = _gridSpacingFact.rawValue().toDouble(); + while (x < largeBoundRect.bottomRight().x()) { + float yTop = largeBoundRect.topLeft().y() - 100.0; + float yBottom = largeBoundRect.bottomRight().y() + 100.0; + + lineList += QLineF(_rotatePoint(QPointF(x, yTop), center, gridAngle), _rotatePoint(QPointF(x, yBottom), center, gridAngle)); + qDebug() << "line" << lineList.last().x1() << lineList.last().y1() << lineList.last().x2() << lineList.last().y2(); x += gridSpacing; } - // Turn into a path + // Now intesect the lines with the smaller bounding rect + QList resultLines; + //_intersectLinesWithRect(lineList, smallBoundRect, resultLines); + _intersectLinesWithPolygon(lineList, polygon, resultLines); - for (int i=0; i points = lineList[i]; + // Turn into a path + for (int i=0; ix = x; - this->y = y; - } - } Point_t; - void _clear(void); void _setExitCoordinate(const QGeoCoordinate& coordinate); void _clearGrid(void); void _generateGrid(void); - void _gridGenerator(const QList& polygonPoints, QList& gridPoints); + void _gridGenerator(const QList& polygonPoints, QList& gridPoints); + QPointF _rotatePoint(const QPointF& point, const QPointF& origin, double angle); + void _intersectLinesWithRect(const QList& lineList, const QRectF& boundRect, QList& resultLines); + void _intersectLinesWithPolygon(const QList& lineList, const QPolygonF& polygon, QList& resultLines); int _sequenceNumber; bool _dirty;