Commit 7bad89ed authored by Valentin Platzgummer's avatar Valentin Platzgummer

optTools added

parent 257fcdbc
......@@ -428,7 +428,8 @@ HEADERS += \
src/Wima/CircularSurveyComplexItem.h \
src/Wima/PlanimetryCalculus.h \
src/Wima/Circle.h \
src/Wima/PolygonCalculus.h
src/Wima/PolygonCalculus.h \
src/Wima/OptimisationTools.h
SOURCES += \
src/api/QGCCorePlugin.cc \
src/api/QGCOptions.cc \
......@@ -455,7 +456,8 @@ SOURCES += \
src/Wima/CircularSurveyComplexItem.cc \
src/Wima/PlanimetryCalculus.cc \
src/Wima/Circle.cc \
src/Wima/PolygonCalculus.cc
src/Wima/PolygonCalculus.cc \
src/Wima/OptimisationTools.cc
#
# Unit Test specific configuration goes here (requires full debug build with all plugins)
......
......@@ -47,6 +47,8 @@ double CircularSurveyComplexItem::additionalTimeDelay() const
void CircularSurveyComplexItem::_rebuildTransectsPhase1()
{
_transects.clear();
}
void CircularSurveyComplexItem::_recalcComplexDistance()
......
#include "OptimisationTools.h"
namespace OptimisationTools {
namespace {
} // end anonymous namespace
/*!
* \fn bool dijkstraAlgorithm(int startIndex, int endIndex, const QList<T> elements, QList<T> &elementPath, double(*distance)(const T &t1, const T &t2))
* Calculates the shortest path between the elements stored in \a elements.
* The \l {Dijkstra Algorithm} is used to find the shorest path.
* Stores the result inside \a elementPath when sucessfull.
* The function handle \a distance is used to calculate the distance between two elements. The distance must be positive.
* Returns \c true if successful, \c false else.
*
* \sa QList
*/
template <typename T>
bool dijkstraAlgorithm(const QList<T> elements, int startIndex, int endIndex, QList<T> &elementPath, double(*distance)(const T &t1, const T &t2)) // don't seperate parameters with new lines or documentation will break
{
if ( elements.isEmpty() || startIndex < 0
|| startIndex >= elements.size() || endIndex < 0
|| endIndex >= elements.size()) {
return false;
}
// Each element of type T gets stuff into a Node
/// @param distance is the distance between the Node and it's predecessor
struct Node{
T element;
double distance = std::numeric_limits<qreal>::infinity();
Node* predecessorNode = nullptr;
};
// The list with all Nodes (elements)
QList<Node> nodeList;
// This list will be initalized with (pointer to) all elements of nodeList.
// Elements will be successively remove during the execution of the Dijkstra Algorithm.
QList<Node*> workingSet;
//append elements to node list
for (int i = 0; i < elements.size(); i++) {
Node node;
node.element = elements[i];
nodeList.append(node);
workingSet.append(&nodeList[i]);
}
nodeList[startIndex].distance = 0;
// Dijkstra Algorithm
// https://de.wikipedia.org/wiki/Dijkstra-Algorithmus
while (workingSet.size() > 0) {
// serach Node with minimal distance
double minDist = std::numeric_limits<qreal>::infinity();
int minDistIndex = 0;
for (int i = 0; i < workingSet.size(); i++) {
Node* node = workingSet.value(i);
double dist = node->distance;
if (dist < minDist) {
minDist = dist;
minDistIndex = i;
}
}
Node* u = workingSet.takeAt(minDistIndex);
//update distance
for (int i = 0; i < workingSet.size(); i++) {
Node* v = workingSet[i];
double dist = distance(u->element, v->element);
// is ther a alternative path which is shorter?
double alternative = u->distance + dist;
if (alternative < v->distance) {
v->distance = alternative;
v->predecessorNode = u;
}
}
}
// end Djikstra Algorithm
// reverse assemble path
Node* node = &nodeList[endIndex];
while (1) {
if (node == nullptr) {
if (elementPath[0] == elementPath[startIndex])// check if starting point was reached
break;
return false;
}
elementPath.prepend(node->element);
//Update Node
node = node->predecessorNode;
}
return true;
}
} // end OptimisationTools namespace
#ifndef OPTIMISATIONTOOLS_H
#define OPTIMISATIONTOOLS_H
#include <QObject>
namespace OptimisationTools {
template <typename T>
bool dijkstraAlgorithm(const QList<T> elements, int startIndex, int endIndex, QList<T> &elementPath, double(*distance)(const T &t1, const T &t2));
}
#endif // OPTIMISATIONTOOLS_H
This diff is collapsed.
......@@ -6,6 +6,8 @@
#include <QtMath>
#include <QLineF>
#include "PolygonCalculus.h"
class Circle;
namespace PlanimetryCalculus {
......@@ -14,33 +16,44 @@ namespace PlanimetryCalculus {
OutsideIntersection, OutsideTouching, OutsideNoIntersection,
CirclesEqual, //Circle Circle intersection
NoIntersection, Tangent, Secant, // Circle Line Intersetion
Tangent, Secant, // Circle Line Intersetion
EdgeCornerIntersection, EdgeEdgeIntersection, CornerCornerIntersection,
LinesParallel, LinesEqual, // Line Line intersection
Error // general
NoIntersection, Error // general
};
typedef QList<QPair<int, int>> NeighbourList;
typedef QList<QPointF> QPointFList;
typedef QList<IntersectType> IntersectList;
void rotateReference(QPointF &point, double alpha);
void rotateReference(QList<QPointF> &points, double alpha);
void rotateReference(QPointFList &points, double alpha);
void rotateReference(QLineF &line, double alpha);
//void rotateReference(QPolygonF &polygon, double alpha);
QPointF rotateReturn(QPointF point, double alpha);
QList<QPointF> rotateReturn(QList<QPointF> points, double alpha);
QLineF rotateReturn(QLineF line, double alpha);
QPointF rotateReturn(QPointF point, double alpha);
QPointFList rotateReturn(QPointFList points, double alpha);
QLineF rotateReturn(QLineF line, double alpha);
//QPolygonF rotateReturn(QPolygonF &polygon, double alpha);
IntersectType intersects(const Circle &circle1, const Circle &circle2);
IntersectType intersects(const Circle &circle1, const Circle &circle2, QList<QPointF> &intersectionPoints);
IntersectType intersects(const Circle &circle, const QLineF &line);
IntersectType intersects(const Circle &circle, const QLineF &line, QList<QPointF> &intersectionPoints);
IntersectType intersects(const QLineF &line1, const QLineF &line2, QPointF &intersectionPt);
QList<IntersectType> intersects(const QPolygonF &polygon, const QLineF &line, QList<QPointF> &intersectionList, QList<QPair<int, int>> &neighbourList);
IntersectType intersects(const QPolygonF &polygon, const QLineF &line);
bool intersects(const Circle &circle1, const Circle &circle2);
bool intersects(const Circle &circle1, const Circle &circle2, IntersectType &type);
bool intersects(const Circle &circle1, const Circle &circle2, QPointFList &intersectionPoints);
bool intersects(const Circle &circle1, const Circle &circle2, QPointFList &intersectionPoints, IntersectType &type);
bool intersects(const Circle &circle, const QLineF &line);
bool intersects(const Circle &circle, const QLineF &line, IntersectType &type);
bool intersects(const Circle &circle, const QLineF &line, QPointFList &intersectionPoints);
bool intersects(const Circle &circle, const QLineF &line, QPointFList &intersectionPoints, IntersectType &type);
bool intersects(const QLineF &line1, const QLineF &line2, QPointF &intersectionPt);
bool intersects(const QLineF &line1, const QLineF &line2, QPointF &intersectionPt, IntersectType &type);
bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList);
bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList, IntersectList &typeList);
bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList, NeighbourList &neighbourList);
bool intersects(const QPolygonF &polygon, const QLineF &line, QPointFList &intersectionList, NeighbourList &neighbourList, IntersectList &typeList);
double distance(const QPointF &p1, const QPointF p2);
double angle(const QPointF &p1, const QPointF p2);
......
This diff is collapsed.
#ifndef POLYGONCALCULUS_H
#define POLYGONCALCULUS_H
#endif
#include <QPointF>
#include <QPolygonF>
#include "PlanimetryCalculus.h"
#include "OptimisationTools.h"
namespace PolygonCalculus {
......@@ -13,14 +15,16 @@ namespace PolygonCalculus {
int closestVertexIndex (const QPolygonF &polygon, const QPointF &coordinate);
QPointF closestVertex (const QPolygonF &polygon, const QPointF &coordinate);
int nextPolygonIndex (int pathsize, int index);
int previousPolygonIndex(int pathsize, int index);
int nextVertexIndex (int pathsize, int index);
int previousVertexIndex (int pathsize, int index);
JoinPolygonError joinPolygon (QPolygonF polygon1, QPolygonF polygon2, QPolygonF &joinedPolygon);
bool isSimplePolygon (const QPolygonF &polygon);
bool hasClockwiseWinding (const QPolygonF &path);
void reversePath (QPolygonF &path);
bool offsetPolygon (QPolygonF &polygon, double offset);
double distanceInsidePolygon (const QPointF &c1, const QPointF &c2, QPolygonF polygon);
void offsetPolygon (QPolygonF &polygon, double offset);
bool containsPath (QPolygonF polygon, const QPointF &c1, const QPointF &c2);
void decomposeToConvex (const QPolygonF &polygon, QList<QPolygon> &convexPolygons);
bool shortestPath (const QPolygonF &polygon, const QPointF &startVertex, const QPointF &endVertex, QList<QPointF> &shortestPath);
}
#endif // POLYGONCALCULUS_H
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