From b1f6be42b0542dd71c6671e0131b001ea4991510 Mon Sep 17 00:00:00 2001
From: "l.dressen" <l.dressen@fz-juelich.de>
Date: Fri, 29 Apr 2022 09:29:04 +0200
Subject: [PATCH] Resolve "Follow-up from "Resolve "Add more intrinsic
 calibration parameters"""

---
 include/helper.h               |  2 ++
 src/helper.cpp                 | 53 ++++++++++++++++++++++++++++++++++
 src/petrack.cpp                |  9 +++---
 tests/unit_test/CMakeLists.txt |  1 +
 tests/unit_test/tst_helper.cpp | 49 +++++++++++++++++++++++++++++++
 5 files changed, 110 insertions(+), 4 deletions(-)
 create mode 100644 tests/unit_test/tst_helper.cpp

diff --git a/include/helper.h b/include/helper.h
index 9634afdc7..bed375d95 100644
--- a/include/helper.h
+++ b/include/helper.h
@@ -292,6 +292,8 @@ inline clock_t getElapsedTime()
     lastTime = clock();
     return diffTime;
 }
+bool lessThanVersion(const QString &q1, const QString &q2);
+
 
 /**
  * Computes the median of the values in a given vector.
diff --git a/src/helper.cpp b/src/helper.cpp
index eaccacfd5..cbaeb0f90 100644
--- a/src/helper.cpp
+++ b/src/helper.cpp
@@ -186,3 +186,56 @@ cv::Mat getRoi(cv::Mat &img, const QRect &roi, cv::Rect &rect, bool evenPixelNum
 
     return img(rect);
 }
+#include <iostream>
+#include <vector>
+/**
+ * @brief Compares two PeTrack version strings and returns if the first version string is newer than the second one
+ *
+ * @param q1: first PeTrack version string
+ * @param q2: second PeTrack version string
+ * @throws std::invalid_argument Thrown if one of the input strings is not in the right format
+ * @return boolean, whether the first version is higher than the second one
+ */
+bool lessThanVersion(const QString &q1, const QString &q2)
+{
+    QStringList      version1 = q1.split(QLatin1Char('.'));
+    QStringList      version2 = q2.split(QLatin1Char('.'));
+    std::vector<int> version1_digits;
+    std::vector<int> version2_digits;
+
+    for(int i = 0; i < version1.length(); ++i)
+    {
+        if(version1[i].length() == 1 && version1[i][0].isDigit())
+        {
+            version1_digits.push_back(version1[i].toInt());
+        }
+        else
+        {
+            throw std::invalid_argument("Invalid Input String!");
+        }
+    }
+    for(int i = 0; i < version2.length(); ++i)
+    {
+        if(version2[i].length() == 1 && version2[i][0].isDigit())
+        {
+            version2_digits.push_back(version2[i].toInt());
+        }
+        else
+        {
+            throw std::invalid_argument("Invalid Input String!");
+        }
+    }
+    int length = std::min(version1_digits.size(), version2_digits.size());
+    for(int i = 0; i < length; ++i)
+    {
+        if(version1_digits[i] > version2_digits[i])
+        {
+            return true;
+        }
+        else if(version1_digits[i] < version2_digits[i])
+        {
+            return false;
+        }
+    }
+    return version1_digits.size() > version2_digits.size();
+}
diff --git a/src/petrack.cpp b/src/petrack.cpp
index 603f70e09..169a7e6aa 100644
--- a/src/petrack.cpp
+++ b/src/petrack.cpp
@@ -34,6 +34,7 @@
 #include "colorRangeWidget.h"
 #include "control.h"
 #include "gridItem.h"
+#include "helper.h"
 #include "imageItem.h"
 #include "logoItem.h"
 #include "moCapItem.h"
@@ -652,16 +653,16 @@ void Petrack::openProject(QString fileName, bool openSeq) // default fileName=""
         openXml(doc, openSeq);
         mLastTrackerExport = mTrcFileName;
 
-        if(mControlWidget->getCalibS1Value() == 0 && mControlWidget->getCalibS2Value() == 0 &&
-           mControlWidget->getCalibS3Value() == 0 && mControlWidget->getCalibS4Value() == 0 &&
-           mControlWidget->getCalibTAUXValue() == 0 && mControlWidget->getCalibTAUYValue() == 0)
+        if(!lessThanVersion(root.attribute("VERSION"), QString("0.9.0")))
         {
             PWarning(
                 this,
                 tr("PeTrack"),
-                tr("You are using an old project! Therefore the old intr. calibration model is set the default"));
+                tr("You are using a project version lower than 0.9: Therefore, the extended intrinsic calibration "
+                   "model is disabled."));
             mControlWidget->setNewModelChecked(false);
         }
+
         updateWindowTitle();
     }
 }
diff --git a/tests/unit_test/CMakeLists.txt b/tests/unit_test/CMakeLists.txt
index 558dec966..30e66a31d 100644
--- a/tests/unit_test/CMakeLists.txt
+++ b/tests/unit_test/CMakeLists.txt
@@ -9,5 +9,6 @@ target_sources(petrack_tests PRIVATE
     tst_moCapController.cpp
     tst_recognition.cpp
     tst_codeMarkerWidget.cpp
+    tst_helper.cpp
     tst_petrack.cpp
 )
diff --git a/tests/unit_test/tst_helper.cpp b/tests/unit_test/tst_helper.cpp
new file mode 100644
index 000000000..83d2d6afa
--- /dev/null
+++ b/tests/unit_test/tst_helper.cpp
@@ -0,0 +1,49 @@
+/*
+ * PeTrack - Software for tracking pedestrians movement in videos
+ * Copyright (C) 2010-2021 Forschungszentrum Jülich GmbH,
+ * Maik Boltes, Juliane Adrian, Ricardo Martin Brualla, Arne Graf, Paul Häger, Daniel Hillebrand,
+ * Deniz Kilic, Paul Lieberenz, Daniel Salden, Tobias Schrödter, Ann Katrin Seemann
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "helper.h"
+
+#include <catch2/catch.hpp>
+
+
+TEST_CASE("Petrack version strings are compared", "[lessThanVersion]")
+{
+    CHECK(lessThanVersion(QString("0.9.1"), QString("0.9.0")));
+    CHECK(lessThanVersion(QString("0.9.0"), QString("0.8.0")));
+    CHECK(lessThanVersion(QString("1.8.0"), QString("0.9.0")));
+    CHECK(lessThanVersion(QString("0.9.1.1"), QString("0.9.0")));
+    CHECK(lessThanVersion(QString("0.9.0.0"), QString("0.9.0")));
+
+    CHECK_FALSE(lessThanVersion(QString("0.9.0"), QString("0.9.1")));
+    CHECK_FALSE(lessThanVersion(QString("0.8.0"), QString("0.9.0")));
+    CHECK_FALSE(lessThanVersion(QString("0.9.0"), QString("1.9.0")));
+    CHECK_FALSE(lessThanVersion(QString("0.9.0"), QString("0.9.1.1")));
+    CHECK_FALSE(lessThanVersion(QString("0.9.0.1"), QString("0.9.1")));
+    CHECK_FALSE(lessThanVersion(QString("0.9.0"), QString("0.9.0.0")));
+    CHECK_FALSE(lessThanVersion(QString("0.9.0"), QString("0.9.0")));
+
+    CHECK_THROWS(lessThanVersion(QString("0.8.k"), QString("0.9.0")));
+    CHECK_THROWS(lessThanVersion(QString("0.8.9"), QString("0.9.k")));
+    CHECK_THROWS(lessThanVersion(QString("0.k.9"), QString("0.9.0")));
+    CHECK_THROWS(lessThanVersion(QString("0.8.9"), QString("0.k.9")));
+    CHECK_THROWS(lessThanVersion(QString("k.8.9"), QString("0.9.0")));
+    CHECK_THROWS(lessThanVersion(QString("0.8.9"), QString("k.9.0")));
+    CHECK_THROWS(lessThanVersion(QString("0.8.9"), QString("k.9.0")));
+}
-- 
GitLab