From 7b546c7dba9a85c38a5cc9f3f954fd7a847e757d Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 11 Jul 2014 15:12:44 +0200 Subject: [PATCH] Fixes 2,4,6 refs #287 https://dev.vatsim-germany.org/issues/287#note-14 --- src/blackcore/vatsimdatafilereader.cpp | 13 ++-- src/blackmisc/avairport.cpp | 3 +- src/blackmisc/avairport.h | 8 +-- src/blackmisc/avairportlist.cpp | 18 ++--- src/blackmisc/avatcstation.cpp | 3 +- src/blackmisc/avatcstation.h | 8 +-- src/blackmisc/coordinategeodetic.cpp | 4 +- src/blackmisc/coordinategeodetic.h | 4 +- src/blackmisc/mathematics.cpp | 18 +---- src/blackmisc/mathematics.h | 99 +++++--------------------- 10 files changed, 45 insertions(+), 133 deletions(-) diff --git a/src/blackcore/vatsimdatafilereader.cpp b/src/blackcore/vatsimdatafilereader.cpp index 23e459d5a..cabbe5b6d 100644 --- a/src/blackcore/vatsimdatafilereader.cpp +++ b/src/blackcore/vatsimdatafilereader.cpp @@ -292,12 +292,13 @@ namespace BlackCore } // for each // this part needs to be synchronized - this->m_lock.lockForWrite(); - this->m_updateTimestamp = updateTimestampFromFile; - this->m_aircrafts = aircrafts; - this->m_atcStations = atcStations; - this->m_voiceServers = voiceServers; - this->m_lock.unlock(); + { + QWriteLocker wl(&this->m_lock); + this->m_updateTimestamp = updateTimestampFromFile; + this->m_aircrafts = aircrafts; + this->m_atcStations = atcStations; + this->m_voiceServers = voiceServers; + } } // read success nwReply->close(); diff --git a/src/blackmisc/avairport.cpp b/src/blackmisc/avairport.cpp index f5d5e7dfc..cece9e41b 100644 --- a/src/blackmisc/avairport.cpp +++ b/src/blackmisc/avairport.cpp @@ -135,8 +135,9 @@ namespace BlackMisc /* * Distance to planne */ - const CLength &CAirport::calculcateDistanceAndBearingToPlane(const CCoordinateGeodetic &position) + CLength CAirport::calculcateDistanceAndBearingToPlane(const CCoordinateGeodetic &position, bool updateValues) { + if (!updateValues) return Geo::greatCircleDistance(this->m_position, position); this->m_distanceToPlane = Geo::greatCircleDistance(this->m_position, position); this->m_bearingToPlane = Geo::initialBearing(this->m_position, position); return this->m_distanceToPlane; diff --git a/src/blackmisc/avairport.h b/src/blackmisc/avairport.h index 8404370a5..a87ee85aa 100644 --- a/src/blackmisc/avairport.h +++ b/src/blackmisc/avairport.h @@ -97,12 +97,8 @@ namespace BlackMisc //! Valid ICAO code bool hasValidIcaoCode() const { return !this->getIcao().isEmpty(); } - /*! - * Calculcate distance and bearing to plane, set it, and return distance - * \param position other position - * \return - */ - const BlackMisc::PhysicalQuantities::CLength &calculcateDistanceAndBearingToPlane(const BlackMisc::Geo::CCoordinateGeodetic &position); + //! Calculcate distance and bearing to plane, set it, and return distance + BlackMisc::PhysicalQuantities::CLength calculcateDistanceAndBearingToPlane(const BlackMisc::Geo::CCoordinateGeodetic &position, bool updateValues = true); //! \copydoc ICoordinateGeodetic::latitude virtual const BlackMisc::Geo::CLatitude &latitude() const override diff --git a/src/blackmisc/avairportlist.cpp b/src/blackmisc/avairportlist.cpp index ec4c71e07..bfd9c3328 100644 --- a/src/blackmisc/avairportlist.cpp +++ b/src/blackmisc/avairportlist.cpp @@ -62,7 +62,7 @@ namespace BlackMisc } /* - * airports within range + * Airports within range */ CAirportList CAirportList::findWithinRange(const BlackMisc::Geo::ICoordinateGeodetic &coordinate, const PhysicalQuantities::CLength &range) const { @@ -73,7 +73,7 @@ namespace BlackMisc } /* - * Distances to own plane + * Distances, bearing to own plane */ void CAirportList::calculcateDistanceAndBearingToPlane(const Geo::CCoordinateGeodetic &position) { @@ -83,19 +83,15 @@ namespace BlackMisc }); } - void CAirportList::removeIfOutsideRange(const Geo::CCoordinateGeodetic &position, const CLength &maxDistance, bool updateDistance) + /* + * Remove outside range + */ + void CAirportList::removeIfOutsideRange(const Geo::CCoordinateGeodetic &position, const CLength &maxDistance, bool updateValues) { CLength d; for (CAirportList::iterator i = begin(); i != end();) { - if (updateDistance) - { - d = i->calculcateDistanceAndBearingToPlane(position); - } - else - { - d = i->greatCircleDistance(position); - } + d = i->calculcateDistanceAndBearingToPlane(position, updateValues); if (maxDistance < d) { i = this->erase(i); diff --git a/src/blackmisc/avatcstation.cpp b/src/blackmisc/avatcstation.cpp index eb275d59a..feab2edca 100644 --- a/src/blackmisc/avatcstation.cpp +++ b/src/blackmisc/avatcstation.cpp @@ -256,8 +256,9 @@ namespace BlackMisc /* * Distance to planne */ - const CLength &CAtcStation::calculcateDistanceToPlane(const CCoordinateGeodetic &position) + CLength CAtcStation::calculcateDistanceToPlane(const CCoordinateGeodetic &position, bool update) { + if (!update) return Geo::greatCircleDistance(this->m_position, position); this->m_distanceToPlane = Geo::greatCircleDistance(this->m_position, position); return this->m_distanceToPlane; } diff --git a/src/blackmisc/avatcstation.h b/src/blackmisc/avatcstation.h index ca65f1ec0..e491b3489 100644 --- a/src/blackmisc/avatcstation.h +++ b/src/blackmisc/avatcstation.h @@ -172,12 +172,8 @@ namespace BlackMisc //! Valid distance? bool hasValidDistance() const { return !this->m_distanceToPlane.isNull();} - /*! - * Calculcate distance to plane, set it, and also return it - * \param position other position - * \return - */ - const BlackMisc::PhysicalQuantities::CLength &calculcateDistanceToPlane(const BlackMisc::Geo::CCoordinateGeodetic &position); + //! Calculcate distance to plane, set it, and also return it + BlackMisc::PhysicalQuantities::CLength calculcateDistanceToPlane(const BlackMisc::Geo::CCoordinateGeodetic &position, bool update = true); //! Is station online (or just booked)? bool isOnline() const { return m_isOnline; } diff --git a/src/blackmisc/coordinategeodetic.cpp b/src/blackmisc/coordinategeodetic.cpp index 3c90706b0..30600d909 100644 --- a/src/blackmisc/coordinategeodetic.cpp +++ b/src/blackmisc/coordinategeodetic.cpp @@ -189,7 +189,7 @@ namespace BlackMisc /* * Great circle distance */ - CLength ICoordinateGeodetic::greatCircleDistance(const ICoordinateGeodetic &otherCoordinate) + CLength ICoordinateGeodetic::greatCircleDistance(const ICoordinateGeodetic &otherCoordinate) const { return Geo::greatCircleDistance((*this), otherCoordinate); } @@ -197,7 +197,7 @@ namespace BlackMisc /* * Initial bearing */ - CAngle ICoordinateGeodetic::initialBearing(const ICoordinateGeodetic &otherCoordinate) + CAngle ICoordinateGeodetic::initialBearing(const ICoordinateGeodetic &otherCoordinate) const { return Geo::initialBearing((*this), otherCoordinate); } diff --git a/src/blackmisc/coordinategeodetic.h b/src/blackmisc/coordinategeodetic.h index b2c368874..10ac35c5c 100644 --- a/src/blackmisc/coordinategeodetic.h +++ b/src/blackmisc/coordinategeodetic.h @@ -55,10 +55,10 @@ namespace BlackMisc } //! Great circle distance - BlackMisc::PhysicalQuantities::CLength greatCircleDistance(const ICoordinateGeodetic &otherCoordinate); + BlackMisc::PhysicalQuantities::CLength greatCircleDistance(const ICoordinateGeodetic &otherCoordinate) const; //! Initial bearing - BlackMisc::PhysicalQuantities::CAngle initialBearing(const ICoordinateGeodetic &otherCoordinate); + BlackMisc::PhysicalQuantities::CAngle initialBearing(const ICoordinateGeodetic &otherCoordinate) const; //! In range static bool indexInRange(int index); diff --git a/src/blackmisc/mathematics.cpp b/src/blackmisc/mathematics.cpp index 44ba29c0c..34f07cc96 100644 --- a/src/blackmisc/mathematics.cpp +++ b/src/blackmisc/mathematics.cpp @@ -92,22 +92,8 @@ namespace BlackMisc */ double CMath::normalizeDegrees(double degrees) { - if (degrees == 0.0 || degrees == 360.0 || degrees == -360.0) return 0.0; - if (degrees > 0) - { - while (degrees >= 360.0) - { - degrees -= 360.0; - } - } - else - { - while (degrees < 0.0) - { - degrees += 360.0; - } - } - return degrees; + double result = std::fmod(degrees, 360.0); + return (result >= 0.0) ? result : result + 360.0; } } // namespace diff --git a/src/blackmisc/mathematics.h b/src/blackmisc/mathematics.h index 64122dc40..6352999d3 100644 --- a/src/blackmisc/mathematics.h +++ b/src/blackmisc/mathematics.h @@ -13,148 +13,83 @@ namespace BlackMisc namespace Math { - /*! - * \brief Math utils - */ + //! Math utils class CMath { public: - /*! - * \brief Calculates the hypotenuse of x and y without overflow - * \param x - * \param y - * \return - */ + //! Calculates the hypotenuse of x and y without overflow static double hypot(double x, double y); - /*! - * \brief Calculates the square of x - * \param x - * \return - */ + //! Calculates the square of x static inline double square(double x) { return x * x; } - /*! - * \brief Calculates x to the power of three - * \param x - * \return - */ + //! Calculates x to the power of three static inline double cubic(const double x) { return x * x * x; } - /*! - * \brief Calculates the real cubic root - * \param x - * \return - */ + //! Calculates the real cubic root static double cubicRootReal(double x); - /*! - * \brief Utility round method - * \param value - * \param digits - * \return - */ + //! Utility round method static double round(double value, int digits); - /*! - * \brief Round by given epsilon, e.g. - * \param value - * \param epsilon - * \return - */ + //! Round by given epsilon static double roundEpsilon(double value, double epsilon); - /*! - * \brief Epsilon safe equal - * \param v1 - * \param v2 - * \param epsilon - * \return - */ + //! Epsilon safe equal static bool epsilonEqual(double v1, double v2, double epsilon = 1E-06); - /*! - * \brief Nearest integer not greater in magnitude than value, correcting for epsilon - * \param value - * \param epsilon - */ + //! Nearest integer not greater in magnitude than value, correcting for epsilon static inline double trunc(double value, double epsilon = 1e-10) { return value < 0 ? ceil(value - epsilon) : floor(value + epsilon); } - /*! - * \brief Fractional part of value - * \param value - */ + //! Fractional part of value static inline double fract(double value) { double unused; return modf(value, &unused); } - /*! - * \brief PI - * \return - */ + //! PI / 2 static const double &PIHALF() { static double pi = 2.0 * qAtan(1.0); return pi; } - /*! - * \brief PI - * \return - */ + //! PI static const double &PI() { static double pi = 4.0 * qAtan(1.0); return pi; } - /*! - * \brief PI * 2 - * \return - */ + //! PI * 2 static const double &PI2() { static double pi2 = 8.0 * qAtan(1.0); return pi2; } - /*! - * \brief Degree to radians - * \param degree - * \return - */ + //! Degrees to radians static double deg2rad(double degree); - /*! - * \brief Radians to degrees - * \param radians - * \return - */ + //! Radians to degrees static double rad2deg(double radians); - /*! - * \brief Normalize to 0..360 degrees - * \param degrees - * \return - */ + //! Normalize: 0≤ degrees <360 static double normalizeDegrees(double degrees); private: - /*! - * \brief No objects, just static - */ + //! No objects, just static CMath(); };