QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
TrajectoryPoints.cc
Go to the documentation of this file.
1#include "TrajectoryPoints.h"
2#include "Vehicle.h"
3
4#include <QtCore/qmath.h>
5
6#include <cmath>
7
9 : QObject (parent)
10 , _vehicle (vehicle)
11{
12}
13
14void TrajectoryPoints::_vehicleCoordinateChanged(QGeoCoordinate coordinate)
15{
16 // The goal of this algorithm is to limit the number of trajectory points whic represent the vehicle path.
17 // Fewer points means higher performance of map display.
18
19 if (_lastPoint.isValid()) {
20 const double horizontalDistance = _lastPoint.distanceTo(coordinate);
21 double altitudeDelta = coordinate.altitude() - _lastPoint.altitude();
22 if (!std::isfinite(altitudeDelta)) {
23 altitudeDelta = 0.0;
24 }
25 // Use 3D distance so purely vertical climbs/descents also generate trajectory points
26 const double distance = std::hypot(horizontalDistance, altitudeDelta);
27 if (distance > _distanceTolerance) {
28 //-- Update flight distance
29 _vehicle->updateFlightDistance(distance);
30 // Vehicle has moved far enough from previous point for an update.
31 // Unit direction of the new segment in local east/north/up coordinates.
32 double east = 0.0;
33 double north = 0.0;
34 if (horizontalDistance > 0.0) {
35 const double azimuthRad = qDegreesToRadians(_lastPoint.azimuthTo(coordinate));
36 east = std::sin(azimuthRad) * horizontalDistance / distance;
37 north = std::cos(azimuthRad) * horizontalDistance / distance;
38 }
39 const QVector3D newDirection(static_cast<float>(east), static_cast<float>(north),
40 static_cast<float>(altitudeDelta / distance));
41 static const float colinearDotThreshold =
42 static_cast<float>(std::cos(qDegreesToRadians(_directionTolerance)));
43 if (_lastDirection.isNull() || QVector3D::dotProduct(newDirection, _lastDirection) < colinearDotThreshold) {
44 // The new position IS NOT colinear with the last segment. Append the new position to the list.
45 _lastDirection = newDirection;
46 _lastPoint = coordinate;
47 _points.append(QVariant::fromValue(coordinate));
48 emit pointAdded(coordinate);
49 } else {
50 // The new position IS colinear with the last segment. Don't add a new point, just update
51 // the last point to be the new position.
52 _lastPoint = coordinate;
53 _points[_points.count() - 1] = QVariant::fromValue(coordinate);
54 emit updateLastPoint(coordinate);
55 }
56 }
57 } else {
58 // Add the very first trajectory point to the list
59 _lastPoint = coordinate;
60 _points.append(QVariant::fromValue(coordinate));
61 emit pointAdded(coordinate);
62 }
63}
64
66{
67 clear();
68 connect(_vehicle, &Vehicle::coordinateChanged, this, &TrajectoryPoints::_vehicleCoordinateChanged);
69}
70
72{
73 disconnect(_vehicle, &Vehicle::coordinateChanged, this, &TrajectoryPoints::_vehicleCoordinateChanged);
74}
75
77{
78 _points.clear();
79 _lastPoint = QGeoCoordinate();
80 _lastDirection = QVector3D();
81 emit pointsCleared();
82}
void pointAdded(QGeoCoordinate coordinate)
void updateLastPoint(QGeoCoordinate coordinate)
TrajectoryPoints(Vehicle *vehicle, QObject *parent=nullptr)
void pointsCleared(void)
void coordinateChanged(QGeoCoordinate coordinate)
void updateFlightDistance(double distance)
Definition Vehicle.cc:2913