Commit 68512dd5 authored by Valentin Platzgummer's avatar Valentin Platzgummer

planiCalc now namespace not class, about to add polygonCalc

parent 38ec24c5
......@@ -427,7 +427,8 @@ HEADERS += \
src/Wima/SphereCalculus.h \
src/Wima/CircularSurveyComplexItem.h \
src/Wima/PlanimetryCalculus.h \
src/Wima/Circle.h
src/Wima/Circle.h \
src/Wima/PolygonCalculus.h
SOURCES += \
src/api/QGCCorePlugin.cc \
src/api/QGCOptions.cc \
......@@ -453,7 +454,8 @@ SOURCES += \
src/Wima/SphereCalculus.cc \
src/Wima/CircularSurveyComplexItem.cc \
src/Wima/PlanimetryCalculus.cc \
src/Wima/Circle.cc
src/Wima/Circle.cc \
src/Wima/PolygonCalculus.cc
#
# Unit Test specific configuration goes here (requires full debug build with all plugins)
......
......@@ -97,16 +97,47 @@ QPolygonF Circle::approximate(int numberOfCorners) const
{
if ( numberOfCorners < 3)
return QPolygonF();
return approximateSektor(numberOfCorners, 0, 2*M_PI);
}
QPolygonF Circle::approximate(double angleDiscretisation) const
{
return approximateSektor(angleDiscretisation, 0, 2*M_PI);
}
QPolygonF Circle::approximateSektor(int numberOfCorners, double alpha1, double alpha2) const
{
return approximateSektor((alpha2-alpha1)/double(numberOfCorners-1), alpha1, alpha2);
}
QPolygonF Circle::approximateSektor(double angleDiscretisation, double alpha1, double alpha2) const
{
// truncate alpha1 to [0, 2*pi], fmod() does not work in this case
alpha1 = PlanimetryCalculus::truncateAngle(alpha1);
alpha2 = PlanimetryCalculus::truncateAngle(alpha2);
double deltaAlpha = PlanimetryCalculus::truncateAngle(alpha2 - alpha1);
angleDiscretisation = PlanimetryCalculus::truncateAngle(angleDiscretisation);
if (angleDiscretisation > deltaAlpha || qFuzzyIsNull(angleDiscretisation))
return QPolygonF();
double rotationAngle = 2*M_PI/numberOfCorners;
QPolygonF polygon;
QPointF vertex(-_circleRadius,0); // initial vertex
polygon.append(vertex + _circleOrigin);
double currentAngle = alpha1;
// rotate the vertex numberOfCorners-1 times add the origin and append to the polygon.
for(int i = 0; i < numberOfCorners; i++) {
rotatePoint(vertex, rotationAngle);
while(currentAngle < alpha2) {
PlanimetryCalculus::rotatePoint(vertex, currentAngle);
polygon.append(vertex + _circleOrigin);
currentAngle = PlanimetryCalculus::truncateAngle(currentAngle + angleDiscretisation);
}
// append last point if necessarry
PlanimetryCalculus::rotatePoint(vertex, alpha2);
vertex = vertex + _circleOrigin;
if ( !qFuzzyIsNull(PlanimetryCalculus::distance(polygon.first(), vertex))
&& !qFuzzyIsNull(PlanimetryCalculus::distance(polygon.last(), vertex )) ){
polygon.append(vertex);
}
return polygon;
......
......@@ -4,9 +4,11 @@
#include <QPointF>
#include <QPolygonF>
#include <cmath>
#include "PlanimetryCalculus.h"
class Circle : public QObject, protected PlanimetryCalculus
class Circle : public QObject
{
Q_OBJECT
public:
......@@ -25,7 +27,10 @@ public:
QPointF origin() const;
// Member methodes
QPolygonF approximate(int numberOfCorners) const;
QPolygonF approximate (int numberOfCorners) const;
QPolygonF approximate (double angleDiscretisation) const;
QPolygonF approximateSektor(int numberOfCorners, double alpha1, double alpha2) const;
QPolygonF approximateSektor(double angleDiscretisation, double alpha1, double alpha2) const;
bool isNull() const;
signals:
......
#include "PlanimetryCalculus.h"
#include "Circle.h"
PlanimetryCalculus::PlanimetryCalculus()
{
namespace PlanimetryCalculus {
namespace {
/*!
\fn IntersectType intersects(const Circle &circle, const QLineF &line, QList<QPointF> &intersectionPoints, bool calcInstersect)
Returns the Intersection type of \a circle and \a line.
Stores the intersection points in \a intersectionPoints if \a calcIntersect is \c true.
Returns \c Error if either line or circe \c {isNull() == true}.
}
\sa QPointF, Circle
*/
IntersectType intersects(const Circle &circle, const QLineF &line, QList<QPointF> &intersectionPoints, bool calcInstersect)
{
if (!circle.isNull() && ! line.isNull()) {
QPointF translationVector = line.p1();
double angleWLDegree = line.angle(); // angle between wold and line coordinate system
/*!
\fn void PlanimetryCalculus::rotatePoint(QPointF &point, double alpha)
QPointF originCircleL = circle.origin() - translationVector;
rotatePoint(originCircleL, -angleWLDegree); // circle origin in line corrdinate system
double y = originCircleL.y();
double r = circle.radius();
if (qAbs(y) > r)
return NoIntersection;
else if ( qFuzzyCompare(qFabs(y), r) ) { // tangent
double x_ori = originCircleL.x();
if (x_ori >= 0 && x_ori <= line.length()) {
if (calcInstersect) {
QPointF intersectionPt = QPointF(x_ori, 0);
rotatePoint(intersectionPt, angleWLDegree);
intersectionPoints.append(intersectionPt + translationVector);
}
return Tangent;
}
return NoIntersection;
} else { // sekant
double x_ori = originCircleL.x();
double y_ori = originCircleL.y();
double delta = qSqrt(qPow(r, 2)-qPow(y_ori, 2));
double x1 = x_ori + delta; // x coordinate (line system) of fist intersection point
double x2 = x_ori - delta;// x coordinate (line system) of second intersection point
bool doesIntersect = false; // remember if actual intersection was on the line
if (x1 >= 0 && x1 <= line.length()) { // check if intersection point is on the line
if (calcInstersect) {
QPointF intersectionPt = QPointF(x1, 0); // first intersection point (line system)
rotatePoint(intersectionPt, angleWLDegree);
intersectionPoints.append(intersectionPt + translationVector); // transform (to world system) and append first intersection point
}
doesIntersect = true;
}
if (x2 >= 0 && x2 <= line.length()) { // check if intersection point is on the line
if (calcInstersect) {
QPointF intersectionPt = QPointF(x2, 0); // second intersection point (line system)
rotatePoint(intersectionPt, angleWLDegree);
intersectionPoints.append(intersectionPt + translationVector); // transform (to world system) and append second intersection point
}
doesIntersect = true;
}
return doesIntersect ? Secant : NoIntersection;
}
}
return Error;
}
} // end anonymous namespace
/*!
\fn void rotatePoint(QPointF &point, double alpha)
Rotates the \a point counter clockwisely by the angle \a alpha (in radiants).
*/
void PlanimetryCalculus::rotatePoint(QPointF &point, double alpha)
{
*/
void rotatePoint(QPointF &point, double alpha)
{
if (!point.isNull()) {
double x = point.x();
double y = point.y();
......@@ -19,41 +85,41 @@ void PlanimetryCalculus::rotatePoint(QPointF &point, double alpha)
point.setX(x*qCos(alpha) - y*qSin(alpha));
point.setY(x*qSin(alpha) + y*qCos(alpha));
}
}
}
void PlanimetryCalculus::rotatePoint(QList<QPointF> &points, double alpha)
{
void rotatePoint(QList<QPointF> &points, double alpha)
{
for (int i = 0; i < points.size(); i++) {
rotatePoint(points[i], alpha);
}
}
}
/*!
\fn void PlanimetryCalculus::rotatePointDegree(QPointF &point, double alpha)
/*!
\fn void rotatePointDegree(QPointF &point, double alpha)
Rotates the \a point counter clockwisely by the angle \a alpha (in degrees).
*/
void PlanimetryCalculus::rotatePointDegree(QPointF &point, double alpha)
{
*/
void rotatePointDegree(QPointF &point, double alpha)
{
rotatePoint(point, alpha/180*M_PI);
}
}
void PlanimetryCalculus::rotatePointDegree(QList<QPointF> &points, double alpha)
{
void rotatePointDegree(QList<QPointF> &points, double alpha)
{
for (int i = 0; i < points.size(); i++) {
rotatePointDegree(points[i], alpha);
}
}
}
/*!
\fn PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &circle1, const Circle &circle2)
/*!
\fn IntersectType intersects(const Circle &circle1, const Circle &circle2)
Returns the intersection type of the two cirles \a circle1 and \a circle2.
\note Returns Error if circle.isNull() returns true;
\sa Circle
*/
PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &circle1, const Circle &circle2)
{
*/
IntersectType intersects(const Circle &circle1, const Circle &circle2)
{
// r1 == 0 || r2 == 0 results in indefined behavior
if (!circle1.isNull() && !circle2.isNull()) {
double r1 = circle1.radius();
......@@ -72,27 +138,27 @@ PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &c
if (r + d < R) {
// this branch is also reached if d < rLarge && rSmall == 0
return PlanimetryCalculus::InsideNoIntersection;
return InsideNoIntersection;
} else if (qFuzzyCompare(r + d, R)) {
if (qFuzzyIsNull(d))
return PlanimetryCalculus::CirclesEqual;
return CirclesEqual;
else
return PlanimetryCalculus::InsideTouching;
return InsideTouching;
} else if (d < R) {
return PlanimetryCalculus::InsideIntersection;
return InsideIntersection;
} else if (d - r < R) {
return PlanimetryCalculus::OutsideIntersection;
return OutsideIntersection;
} else if (qFuzzyCompare(d - r, R)) {
return PlanimetryCalculus::OutsideTouching;
return OutsideTouching;
} else {
return PlanimetryCalculus::OutsideNoIntersection;
return OutsideNoIntersection;
}
}
return Error;
}
return PlanimetryCalculus::Error;
}
/*!
\fn PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &circle1, const Circle &circle2, QList<QPointF> intersectionPoints)
/*!
\fn IntersectType intersects(const Circle &circle1, const Circle &circle2, QList<QPointF> intersectionPoints)
Calculates the intersection points of two circles if present and stores the result in \a intersectionPoints.
Returns the intersection type of the two cirles \a circle1 and \a circle2.
......@@ -101,15 +167,15 @@ PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &c
\note Returns Error if circle.isNull() returns true;
\sa Circle
*/
PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &circle1, const Circle &circle2, QList<QPointF> &intersectionPoints)
{
PlanimetryCalculus::IntersectType returnValue = intersects(circle1, circle2);
if ( returnValue == PlanimetryCalculus::InsideNoIntersection
|| returnValue == PlanimetryCalculus::OutsideNoIntersection
|| returnValue == PlanimetryCalculus::CirclesEqual
|| returnValue == PlanimetryCalculus::Error ) {
*/
IntersectType intersects(const Circle &circle1, const Circle &circle2, QList<QPointF> &intersectionPoints)
{
IntersectType returnValue = intersects(circle1, circle2);
if ( returnValue == InsideNoIntersection
|| returnValue == OutsideNoIntersection
|| returnValue == CirclesEqual
|| returnValue == Error ) {
return returnValue; // No intersection Points, or infinitly many (in case of CirclesEqual).
} else {
double r1 = circle1.radius();
......@@ -127,14 +193,14 @@ PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &c
r = r1;
}
if ( returnValue == PlanimetryCalculus::InsideTouching
|| returnValue == PlanimetryCalculus::OutsideTouching) {
if ( returnValue == InsideTouching
|| returnValue == OutsideTouching) {
// Intersection point in coordinate system of circle 1.
// Coordinate system circle1: origin = circle1.origin(), x-axis towars circle2.origin() y-axis such that the
// coordinate system is dextrorse with z-axis outward faceing with respect to the drawing plane.
intersectionPoints.append(QPointF(0, r1));
} else { //triggered if ( returnValue == PlanimetryCalculus::InsideIntersection
// || returnValue == PlanimetryCalculus::OutsideIntersection)
} else { //triggered if ( returnValue == InsideIntersection
// || returnValue == OutsideIntersection)
// See fist branch for explanation
// this equations are obtained by solving x^2+y^2=R^2 and (x - d)^2+y^2=r^2
......@@ -149,135 +215,86 @@ PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &c
return returnValue;
}
}
}
/*!
\fn PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &circle, const QLineF &line)
/*!
\fn IntersectType intersects(const Circle &circle, const QLineF &line)
Returns the Intersection type of \a circle and \a line.
Returns \c Error if either line or circe \c {isNull() == true}.
\sa QPointF, Circle
*/
PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &circle, const QLineF &line)
{
*/
IntersectType intersects(const Circle &circle, const QLineF &line)
{
QList<QPointF> dummyList;
return intersects(circle, line, dummyList, false /* calculate intersection points*/);
}
}
PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &circle, const QLineF &line, QList<QPointF> &intersectionPoints)
{
IntersectType intersects(const Circle &circle, const QLineF &line, QList<QPointF> &intersectionPoints)
{
return intersects(circle, line, intersectionPoints, true /* calculate intersection points*/);
}
}
/*!
\fn double PlanimetryCalculus::distance(const QPointF &p1, const QPointF p2)
/*!
\fn double distance(const QPointF &p1, const QPointF p2)
Calculates the distance (2-norm) between \a p1 and \a p2.
\sa QPointF
*/
double PlanimetryCalculus::distance(const QPointF &p1, const QPointF p2)
{
*/
double distance(const QPointF &p1, const QPointF p2)
{
double dx = p2.x()-p1.x();
double dy = p2.y()-p1.y();
return qSqrt(dx*dx+dy*dy);
}
}
/*!
\fn double PlanimetryCalculus::distance(const QPointF &p1, const QPointF p2)
/*!
\fn double distance(const QPointF &p1, const QPointF p2)
Calculates the angle (in radiants) between the line defined by \a p1 and \a p2 and the x-axis according to the following rule.
Angle = qAtan2(dy, dx), where dx = p2.x()-p1.x() and dy = p2.y()-p1.y().
\note The order of \a p1 and \a p2 matters. Swapping \a p1 and \a p2 will result in a angle of oposite sign.
\sa QPointF
*/
double PlanimetryCalculus::angle(const QPointF &p1, const QPointF p2)
{
*/
double angle(const QPointF &p1, const QPointF p2)
{
double dx = p2.x()-p1.x();
double dy = p2.y()-p1.y();
return qAtan2(dy, dx);
}
}
/*!
\fn double PlanimetryCalculus::distance(const QPointF &p1, const QPointF p2)
/*!
\fn double distance(const QPointF &p1, const QPointF p2)
Calculates the angle (in degrees) between the line defined by \a p1 and \a p2 and the x-axis according to the following rule.
Angle = qAtan2(dy, dx)*180/pi, where dx = p2.x()-p1.x() and dy = p2.y()-p1.y().
\note The order of \a p1 and \a p2 matters. Swapping \a p1 and \a p2 will result in a angle of oposite sign.
\sa QPointF
*/
double PlanimetryCalculus::angleDegree(const QPointF &p1, const QPointF p2)
{
*/
double angleDegree(const QPointF &p1, const QPointF p2)
{
return angle(p1, p2)*180/M_PI;
}
/*!
\fn PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &circle, const QLineF &line, QList<QPointF> &intersectionPoints, bool calcInstersect)
Returns the Intersection type of \a circle and \a line.
Stores the intersection points in \a intersectionPoints if \a calcIntersect is \c true.
Returns \c Error if either line or circe \c {isNull() == true}.
\sa QPointF, Circle
*/
PlanimetryCalculus::IntersectType PlanimetryCalculus::intersects(const Circle &circle, const QLineF &line, QList<QPointF> &intersectionPoints, bool calcInstersect)
{
if (!circle.isNull() && ! line.isNull()) {
QPointF translationVector = line.p1();
double angleWLDegree = line.angle(); // angle between wold and line coordinate system
}
QPointF originCircleL = circle.origin() - translationVector;
rotatePoint(originCircleL, -angleWLDegree); // circle origin in line corrdinate system
double truncateAngle(double angle)
{
while (angle < 0 ) { angle += 2*M_PI;}
while (angle > 2*M_PI) { angle -= 2*M_PI;}
double y = originCircleL.y();
double r = circle.radius();
if (qAbs(y) > r)
return PlanimetryCalculus::NoIntersection;
else if ( qFuzzyCompare(qFabs(y), r) ) { // tangent
double x_ori = originCircleL.x();
if (x_ori >= 0 && x_ori <= line.length()) {
if (calcInstersect) {
QPointF intersectionPt = QPointF(x_ori, 0);
rotatePoint(intersectionPt, angleWLDegree);
intersectionPoints.append(intersectionPt + translationVector);
return angle;
}
return PlanimetryCalculus::Tangent;
double truncateAngleDegree(double angle)
{
return truncateAngle(angle/180*M_PI);
}
return PlanimetryCalculus::NoIntersection;
} else { // sekant
double x_ori = originCircleL.x();
double y_ori = originCircleL.y();
double delta = qSqrt(qPow(r, 2)-qPow(y_ori, 2));
double x1 = x_ori + delta; // x coordinate (line system) of fist intersection point
double x2 = x_ori - delta;// x coordinate (line system) of second intersection point
bool doesIntersect = false; // remember if actual intersection was on the line
if (x1 >= 0 && x1 <= line.length()) { // check if intersection point is on the line
if (calcInstersect) {
QPointF intersectionPt = QPointF(x1, 0); // first intersection point (line system)
rotatePoint(intersectionPt, angleWLDegree);
intersectionPoints.append(intersectionPt + translationVector); // transform (to world system) and append first intersection point
}
doesIntersect = true;
}
if (x2 >= 0 && x2 <= line.length()) { // check if intersection point is on the line
if (calcInstersect) {
QPointF intersectionPt = QPointF(x2, 0); // second intersection point (line system)
rotatePoint(intersectionPt, angleWLDegree);
intersectionPoints.append(intersectionPt + translationVector); // transform (to world system) and append second intersection point
}
doesIntersect = true;
}
} // end namespace PlanimetryCalculus
return doesIntersect ? PlanimetryCalculus::Secant : PlanimetryCalculus::NoIntersection;
}
}
return PlanimetryCalculus::Error;
}
/*!
\class PlanimetryCalculus
......
......@@ -5,11 +5,9 @@
#include <QtMath>
#include <QLineF>
#include "Circle.h"
class PlanimetryCalculus
{
public:
PlanimetryCalculus();
class Circle;
namespace PlanimetryCalculus {
enum IntersectType{InsideNoIntersection, InsideTouching, InsideIntersection,
OutsideIntersection, OutsideTouching, OutsideNoIntersection,
......@@ -31,8 +29,9 @@ public:
double distance(const QPointF &p1, const QPointF p2);
double angle(const QPointF &p1, const QPointF p2);
double angleDegree(const QPointF &p1, const QPointF p2);
double truncateAngle(double angle);
double truncateAngleDegree(double angle);
}
private:
IntersectType intersects(const Circle &circle, const QLineF &line, QList<QPointF> &intersectionPoints, bool calcIntersect);
};
#include "PolygonCalculus.h"
#ifndef POLYGONCALCULUS_H
#define POLYGONCALCULUS_H
namespace PolygonCalculus {
}
#endif // POLYGONCALCULUS_H
......@@ -668,23 +668,7 @@ void WimaArea::saveToJson(QJsonObject &json)
bool WimaArea::loadFromJson(const QJsonObject &json, QString& errorString)
{
if ( this->QGCMapPolygon::loadFromJson(json, false /*no poly required*/, errorString) ) {
if ( json.contains(maxAltitudeName) && json[maxAltitudeName].i// Overrides from ComplexMissionItem
bool load (const QJsonObject& complexObject, int sequenceNumber, QString& errorString) final;
QString mapVisualQML (void) const final { return QStringLiteral("SurveyMapVisual.qml"); }
// Overrides from TransectStyleComplexItem
void save (QJsonArray& planItems) final;
bool specifiesCoordinate (void) const final { return true; }
void appendMissionItems (QList<MissionItem*>& items, QObject* missionItemParent) final;
void applyNewAltitude (double newAltitude) final;
double timeBetweenShots (void) final;
// Overrides from VisualMissionionItem
QString commandDescription (void) const final { return tr("Survey"); }
QString commandName (void) const final { return tr("Survey"); }
QString abbreviation (void) const final { return tr("S"); }
bool readyForSave (void) const final;
double additionalTimeDelay (void) const final;sDouble()) {
if ( json.contains(maxAltitudeName) && json[maxAltitudeName].isDouble()) {
_maxAltitude = json[maxAltitudeName].toDouble();
return true;
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment