From df104b16c62dc05e8d8a28e490919f2d776d8033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Thu, 13 Sep 2018 01:45:17 +0200 Subject: [PATCH] Yet another optimization --- src/common/greatcircle.cpp | 32 ++++++++++++++++++++------------ src/common/greatcircle.h | 4 ++-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/common/greatcircle.cpp b/src/common/greatcircle.cpp index 607dc23d..7192bf0c 100644 --- a/src/common/greatcircle.cpp +++ b/src/common/greatcircle.cpp @@ -2,25 +2,33 @@ GreatCircle::GreatCircle(const Coordinates &c1, const Coordinates &c2) { - _lat1 = deg2rad(c1.lat()); - _lon1 = deg2rad(c1.lon()); - _lat2 = deg2rad(c2.lat()); - _lon2 = deg2rad(c2.lon()); + double lat1 = deg2rad(c1.lat()); + double lon1 = deg2rad(c1.lon()); + double lat2 = deg2rad(c2.lat()); + double lon2 = deg2rad(c2.lon()); - _cosLat1 = cos(_lat1); - _cosLat2 = cos(_lat2); - _d = 2 * asin(sqrt(pow((sin((_lat1 - _lat2) / 2)), 2) + _cosLat1 - * _cosLat2 * pow(sin((_lon1 - _lon2) / 2), 2))); + double cosLat1 = cos(lat1); + double cosLat2 = cos(lat2); + _sinLat1 = sin(lat1); + _sinLat2 = sin(lat2); + + _xA = cosLat1 * cos(lon1); + _xB = cosLat2 * cos(lon2); + _yA = cosLat1 * sin(lon1); + _yB = cosLat2 * sin(lon2); + + _d = 2 * asin(sqrt(pow((sin((lat1 - lat2) / 2)), 2) + cosLat1 * cosLat2 + * pow(sin((lon1 - lon2) / 2), 2))); _sinD = sin(_d); } Coordinates GreatCircle::pointAt(double f) const { double A = sin((1.0 - f) * _d) / _sinD; - double B = sin(f*_d) / _sinD; - double x = A * _cosLat1 * cos(_lon1) + B * _cosLat2 * cos(_lon2); - double y = A * _cosLat1 * sin(_lon1) + B * _cosLat2 * sin(_lon2); - double z = A * sin(_lat1) + B * sin(_lat2); + double B = sin(f * _d) / _sinD; + double x = A * _xA + B * _xB; + double y = A * _yA + B * _yB; + double z = A * _sinLat1 + B * _sinLat2; return Coordinates(rad2deg(atan2(y, x)), rad2deg(atan2(z, sqrt(x*x + y*y)))); } diff --git a/src/common/greatcircle.h b/src/common/greatcircle.h index 5ed1db98..f67f8433 100644 --- a/src/common/greatcircle.h +++ b/src/common/greatcircle.h @@ -11,10 +11,10 @@ public: Coordinates pointAt(double f) const; private: - double _lat1, _lon1, _lat2, _lon2; - double _cosLat1, _cosLat2; + double _xA, _xB, _yA, _yB; double _d; double _sinD; + double _sinLat1, _sinLat2; }; #endif // GREATCIRCLE_H