refactore
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* @file point.cpp
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-09-03
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
Point::Point(double lat, double lon, uint32_t horizontalAccuracy, uint32_t creationTime) {
|
||||
this->coordinates.lat = lat;
|
||||
this->coordinates.lon = lon;
|
||||
this->init(horizontalAccuracy, creationTime);
|
||||
}
|
||||
|
||||
Point::Point(int32_t lat, int32_t lon, uint32_t horizontalAccuracy, uint32_t creationTime) {
|
||||
this->coordinates.lat = lat / 10000000.0;
|
||||
this->coordinates.lon = lon / 10000000.0;
|
||||
this->init(horizontalAccuracy, creationTime);
|
||||
}
|
||||
|
||||
Point::Point(Coordinates coords, uint32_t horizontalAccuracy, uint32_t creationTime) {
|
||||
this->coordinates = coords;
|
||||
this->init(horizontalAccuracy, creationTime);
|
||||
}
|
||||
|
||||
Point::Point(Coordinates coords, bool imported) {
|
||||
this->coordinates = coords;
|
||||
if (imported)
|
||||
this->init(UINT32_MAX, 0);
|
||||
else
|
||||
this->init(0, 0);
|
||||
}
|
||||
|
||||
Point::Point() {
|
||||
this->coordinates.lat = 0;
|
||||
this->coordinates.lon = 0;
|
||||
this->init(0, 0);
|
||||
}
|
||||
|
||||
bool Point::operator==(const Point& rhs) const {
|
||||
return this->coordinates == rhs.getCoordinates();
|
||||
}
|
||||
|
||||
// distance = sqrt(dx * dx + dy * dy)
|
||||
// mit distance: Entfernung in km
|
||||
// dx = 111.3 * cos(lat) * (lon1 - lon2)
|
||||
// lat = (lat1 + lat2) / 2 * 0.01745
|
||||
// dy = 111.3 * (lat1 - lat2)
|
||||
// lat1, lat2, lon1, lon2: Breite, Länge in Grad
|
||||
double Point::distanceTo(const Coordinates& point) const {
|
||||
Coordinates begin = this->coordinates;
|
||||
Coordinates end = point;
|
||||
|
||||
double lat = (begin.lat + end.lat) / 2 * ROUTE_DEGREE_TO_RADIANT;
|
||||
double dy = ROUTE_DISTANCE_BETWEEN_LATITUDE * (begin.lat - end.lat);
|
||||
double dx = ROUTE_DISTANCE_BETWEEN_LATITUDE * cos(lat) * (begin.lon - end.lon);
|
||||
|
||||
return sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
|
||||
double Point::distanceTo(const Point &point) const {
|
||||
return this->distanceTo(point.getCoordinates());
|
||||
}
|
||||
|
||||
int16_t Point::courseTo(const Coordinates& point) const {
|
||||
Coordinates begin = this->coordinates;
|
||||
Coordinates end = point;
|
||||
|
||||
double phi = log( tan(end.lat * ROUTE_DEGREE_TO_RADIANT / 2 + M_PI / 4) / tan(begin.lat * ROUTE_DEGREE_TO_RADIANT / 2 + M_PI / 4) );
|
||||
double lon = (begin.lon * ROUTE_DEGREE_TO_RADIANT - end.lon * ROUTE_DEGREE_TO_RADIANT);
|
||||
|
||||
int16_t res = static_cast<int16_t>(atan2(lon, phi) / ROUTE_DEGREE_TO_RADIANT) * -1;
|
||||
|
||||
// if (res < 0)
|
||||
// res += 360;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int16_t Point::courseTo(const Point &point) const {
|
||||
return this->courseTo(point.getCoordinates());
|
||||
}
|
||||
|
||||
void Point::init(uint32_t horizontalAccuracy, uint32_t creationTime) {
|
||||
this->creationTime = creationTime;
|
||||
|
||||
if (horizontalAccuracy == UINT32_MAX)
|
||||
this->accuracy = Accuracy::imported;
|
||||
else if (horizontalAccuracy > 9999)
|
||||
this->accuracy = Accuracy::fourDigOfCM;
|
||||
else if (horizontalAccuracy > 999)
|
||||
this->accuracy = Accuracy::threeDigOfCM;
|
||||
else if (horizontalAccuracy > 99)
|
||||
this->accuracy = Accuracy::twoDigOfCM;
|
||||
else if (horizontalAccuracy > 1)
|
||||
this->accuracy = Accuracy::oneDigOfCM;
|
||||
else
|
||||
this->accuracy = Accuracy::none;
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* @file point.h
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-09-03
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef POINT_H
|
||||
#define POINT_H
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#define ROUTE_DEGREE_TO_RADIANT 0.01745
|
||||
#define ROUTE_DISTANCE_BETWEEN_LATITUDE 111300
|
||||
|
||||
/**
|
||||
* @brief A to handle points on the earth
|
||||
*
|
||||
* The points inherits latidue and longitude as doubles
|
||||
*
|
||||
*/
|
||||
class Point{
|
||||
public:
|
||||
/**
|
||||
* @brief Hold the data longitude and latitude
|
||||
*
|
||||
*/
|
||||
struct Coordinates {
|
||||
double lon;
|
||||
double lat;
|
||||
|
||||
bool operator==(const Coordinates rhs) const {
|
||||
return ( this->lon == rhs.lon ) && ( this->lon == rhs.lon );
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief The Accuracy is set by the constructor
|
||||
*
|
||||
*/
|
||||
enum Accuracy {
|
||||
none,
|
||||
fourDigOfCM,
|
||||
threeDigOfCM,
|
||||
twoDigOfCM,
|
||||
oneDigOfCM,
|
||||
imported
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Construct a new Point object
|
||||
*
|
||||
* @param lat latitude
|
||||
* @param lon longitude
|
||||
* @param horizontalAccuracy mm
|
||||
* @param coords Coordinates
|
||||
* @param imported if true than highest accuracy
|
||||
*/
|
||||
Point(double lat, double lon, uint32_t horizontalAccuracy = 0, uint32_t creationTime = 0);
|
||||
Point(int32_t lat, int32_t lon, uint32_t horizontalAccuracy = 0, uint32_t creationTime = 0);
|
||||
Point(Coordinates coords, uint32_t horizontalAccuracy = 0, uint32_t creationTime = 0);
|
||||
Point(Coordinates coords, bool imported);
|
||||
Point();
|
||||
|
||||
/**
|
||||
* @brief Checks if to points are equal.
|
||||
*
|
||||
* @param rhs
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool operator==(const Point& rhs) const;
|
||||
|
||||
/**
|
||||
* @brief Checks if the point is initalized.
|
||||
*
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool isInit() const { return this->coordinates.lat + this->coordinates.lon; }
|
||||
|
||||
/**
|
||||
* @brief Checks if the point is valid.
|
||||
*
|
||||
* If the accuracy is higher than zero, true will be returned.
|
||||
*
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool isValid() const { return (this->accuracy > 0) ? true : false; }
|
||||
|
||||
/**
|
||||
* @brief Calculates the distance between to points.
|
||||
*
|
||||
* @param point
|
||||
* @return double meter
|
||||
*/
|
||||
double distanceTo(const Coordinates& point) const;
|
||||
double distanceTo(const Point& point) const;
|
||||
|
||||
/**
|
||||
* @brief Calculates the course to an other point.
|
||||
*
|
||||
* @param point
|
||||
* @return int16_t degree
|
||||
*/
|
||||
int16_t courseTo(const Coordinates& point) const;
|
||||
int16_t courseTo(const Point& point) const;
|
||||
|
||||
uint32_t getCreationTime() const { return this->creationTime; }
|
||||
double getLongitude() const { return this->coordinates.lon; }
|
||||
double getLatitude() const { return this->coordinates.lat; }
|
||||
Coordinates getCoordinates() const { return this->coordinates; }
|
||||
|
||||
/**
|
||||
* @brief Get the Accuracy object
|
||||
*
|
||||
* The higher the value, the greater the accuracy.
|
||||
* You can check it by Accuracy.
|
||||
*
|
||||
* @return Accuracy
|
||||
*/
|
||||
Accuracy getAccuracy() const { return this->accuracy; }
|
||||
|
||||
private:
|
||||
void init(uint32_t horizontalAccuracy, uint32_t creationTime);
|
||||
|
||||
Accuracy accuracy = Accuracy::none;
|
||||
Coordinates coordinates;
|
||||
|
||||
uint32_t creationTime = 0;
|
||||
};
|
||||
|
||||
#endif //POINT_H
|
||||
Reference in New Issue
Block a user