From fa6abac1f89bac43e9d34f544a93cdebbd209278 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Fri, 10 Oct 2014 20:27:14 +0200 Subject: [PATCH] Fix calculation of absolute values of the first tick on scales --- Anyanka.pro | 3 ++- helpers.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++ helpers.h | 24 ++++++++++------------ signalcontroller.cpp | 10 +++------ signaldrawer.cpp | 4 ++-- 5 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 helpers.cpp diff --git a/Anyanka.pro b/Anyanka.pro index aebc60b..d7ad74f 100644 --- a/Anyanka.pro +++ b/Anyanka.pro @@ -50,7 +50,8 @@ SOURCES += main.cpp\ signaldrawer.cpp \ jsonserializable.cpp \ libHPCS/libhpcs.c \ - gui/failedfilesdialog.cpp + gui/failedfilesdialog.cpp \ + helpers.cpp HEADERS += \ datafilesloader.h \ diff --git a/helpers.cpp b/helpers.cpp new file mode 100644 index 0000000..046e4dd --- /dev/null +++ b/helpers.cpp @@ -0,0 +1,48 @@ +/* + Copyright (c) 2013 Michal Malý + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include "helpers.h" + +const double Helpers::EPSILON = 2e-14; + +double Helpers::firstTick(const double from, const double step) +{ + const double mod = fabs(fmod(from, step)); + double ret; + + if (fabs(mod - 0.0) < EPSILON) + ret = from; + else + ret = from + (step - fmod(from, step)); + + return ret; +} + +QString Helpers::getFileNameSuffix(const QString& name) +{ + int dotIdx = name.lastIndexOf('.'); + + if (dotIdx < 0) + return ""; + + return name.mid(dotIdx+1).toLower(); +} diff --git a/helpers.h b/helpers.h index 5aaaa91..bef1ad5 100644 --- a/helpers.h +++ b/helpers.h @@ -24,36 +24,33 @@ #ifndef HELPERS_H #define HELPERS_H +#include "logger.h" #include - class Helpers { public: - template static T average(T* vals, size_t len) { + static const double EPSILON; + + static double firstTick(const double from, const double step); + static QString getFileNameSuffix(const QString& name); + + template static T average(T* vals, size_t len) + { T sum = 0; for (size_t i = 0; i < len; i++) sum += vals[i]; return sum / len; } - template static T clamp(T in, T min, T max) { + template static T clamp(T in, T min, T max) + { if (in < min) return min; if (in > max) return max; } - static QString getFileNameSuffix(const QString& name) - { - int dotIdx = name.lastIndexOf('.'); - - if (dotIdx < 0) - return ""; - - return name.mid(dotIdx+1).toLower(); - } - template static T maxof(T n, ...) { T idx; @@ -85,4 +82,5 @@ public: } }; + #endif // HELPERS_H diff --git a/signalcontroller.cpp b/signalcontroller.cpp index da3e5c3..452189c 100644 --- a/signalcontroller.cpp +++ b/signalcontroller.cpp @@ -20,6 +20,7 @@ THE SOFTWARE. */ +#include "helpers.h" #include "signalcontroller.h" #include "logger.h" #include @@ -195,13 +196,8 @@ RulerDrawData SignalController::getRulerDrawData(const double from, const double //qDebug() << __QFUNC__ << "Step" << step << "RelStep" << relStep << "relDiff" << diffRel << fromAbs; /* Calculate position of the first major tick */ - if (from == 0.0) { - firstTickAbs = fromAbs; - firstTickRel = from; - } else { - firstTickAbs = fromAbs + (step - fmod(fromAbs, step)); - firstTickRel = from + (relStep - fmod(from, relStep)); - } + firstTickAbs = Helpers::firstTick(fromAbs, step); + firstTickRel = Helpers::firstTick(from, relStep); //qDebug() << __QFUNC__ << "First tick Abs:" << firstTickAbs << "Rel:" << firstTickRel; return RulerDrawData(firstTickAbs, firstTickRel, step, relStep); diff --git a/signaldrawer.cpp b/signaldrawer.cpp index cda8ca9..a07b478 100644 --- a/signaldrawer.cpp +++ b/signaldrawer.cpp @@ -20,6 +20,7 @@ THE SOFTWARE. */ +#include "helpers.h" #include "logger.h" #include "signaldrawer.h" #include @@ -233,8 +234,7 @@ void SignalDrawer::drawLeadingSubticks(const RulerDrawData& rd, std::function