From 0030159642395dee42cefcda58a51f819af8e5aa Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de>
Date: Thu, 11 Nov 2021 23:54:12 +0100
Subject: [PATCH] assert in FT

---
 .clang-tidy                    |  2 +-
 Base/Math/FourierTransform.cpp | 38 +++++++++++++---------------------
 2 files changed, 15 insertions(+), 25 deletions(-)

diff --git a/.clang-tidy b/.clang-tidy
index 0fa3334dd3b..e54bc67fc1f 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -136,4 +136,4 @@ Checks: '*,
 
 
 '
-# note the closing quotation mark
\ No newline at end of file
+# keep the closing quotation mark
\ No newline at end of file
diff --git a/Base/Math/FourierTransform.cpp b/Base/Math/FourierTransform.cpp
index 69bb3603357..e3a74c61dc1 100644
--- a/Base/Math/FourierTransform.cpp
+++ b/Base/Math/FourierTransform.cpp
@@ -14,11 +14,9 @@
 //  ************************************************************************************************
 
 #include "Base/Math/FourierTransform.h"
+#include "Base/Util/Assert.h"
 #include <algorithm>
 #include <cmath>
-#include <iostream>
-#include <sstream>
-#include <stdexcept>
 
 FourierTransform::FourierTransform() = default;
 
@@ -64,15 +62,17 @@ void FourierTransform::fft(const double2d_t& source, double2d_t& result)
     double* ptr = ws.out_fftw;
     result.clear();
 
+    const size_t nh = ws.h_fftw;
+
     // Resize the array for holding the FT output to correct dimensions
-    result.resize(static_cast<size_t>(ws.h_fftw),
+    result.resize(nh,
                   std::vector<double>(static_cast<size_t>(ws.w_fftw)));
 
     // Storing FT output
-    for (size_t i = 0; i < static_cast<size_t>(ws.h_fftw); i++) {
-        size_t k = static_cast<size_t>(ws.h_fftw) - i;
+    for (size_t i = 0; i < nh; i++) {
+        size_t k = nh - i;
         if (i == 0)
-            k -= static_cast<size_t>(ws.h_fftw);
+            k -= nh;
         for (size_t j = 0; j < static_cast<size_t>(ws.w_fftw / 2 + 1); j++) {
             result[i][j] = *ptr;
             size_t l = static_cast<size_t>(ws.w_fftw) - j;
@@ -105,10 +105,9 @@ void FourierTransform::fftshift(double2d_t& result) const
     std::rotate(result.begin(), result.begin() + static_cast<int>(row_shift), result.end());
 
     // Second, shifting the cols
-    for (size_t i = 0; i < static_cast<size_t>(ws.h_fftw); i++) {
+    for (size_t i = 0; i < static_cast<size_t>(ws.h_fftw); i++)
         std::rotate(result[i].begin(), result[i].begin() + static_cast<int>(col_shift),
                     result[i].end());
-    }
 }
 
 /* ************************************************************************* */
@@ -123,9 +122,7 @@ void FourierTransform::fft(const double1d_t& source, double1d_t& result)
     double2d_t result2d;
     fft(source2d, result2d);
 
-    if (result2d.size() != 1)
-        throw std::runtime_error("FourierTransform::fft -> Panic in 1d");
-
+    ASSERT(result2d.size() == 1);
     result = result2d[0];
 }
 
@@ -149,12 +146,8 @@ void FourierTransform::fftshift(double1d_t& result) const
 /* ************************************************************************************ */
 void FourierTransform::init(int h_src, int w_src)
 {
-    if (!h_src || !w_src) {
-        std::ostringstream os;
-        os << "FourierTransform::init() -> Panic! Wrong dimensions " << h_src << " " << w_src
-           << std::endl;
-        throw std::runtime_error(os.str());
-    }
+    ASSERT(h_src);
+    ASSERT(w_src);
 
     ws.clear();
     ws.h_src = h_src;
@@ -173,9 +166,7 @@ void FourierTransform::init(int h_src, int w_src)
     // ws.p_forw_src = fftw_plan_dft_r2c_2d(ws.h_fftw, ws.w_fftw, ws.in_src,
     //                                     static_cast<double*>(ws.out_src), FFTW_ESTIMATE);
 
-    if (ws.p_forw_src == nullptr)
-        throw std::runtime_error(
-            "FourierTransform::init() -> Error! Can't initialise p_forw_src plan.");
+    ASSERT(ws.p_forw_src);
 }
 
 /* ************************************************************************* */
@@ -184,9 +175,8 @@ void FourierTransform::init(int h_src, int w_src)
 
 void FourierTransform::fftw_forward_FT(const double2d_t& src)
 {
-    if (ws.h_fftw <= 0 || ws.w_fftw <= 0)
-        throw std::runtime_error(
-            "FourierTransform::fftw_forward_FT() -> Panic! Initialization is missed.");
+    ASSERT(ws.h_fftw > 0);
+    ASSERT(ws.w_fftw > 0);
 
     double *ptr, *ptr_end;
 
-- 
GitLab