From bcd13feb1140ed550b18608dacea18754e3347d9 Mon Sep 17 00:00:00 2001
From: Federico Lolli <federico.lolli@skywarder.eu>
Date: Wed, 25 Oct 2023 13:55:45 +0200
Subject: [PATCH] [SFD] renamed VectorIn and moved to common module

---
 src/shared/algorithms/SFD/SFDAscent.cpp  | 12 ++++++------
 src/shared/algorithms/SFD/SFDAscent.h    |  5 ++---
 src/shared/algorithms/SFD/SFDCommon.h    |  9 +++++++++
 src/shared/algorithms/SFD/SFDDescent.cpp | 10 +++++-----
 src/shared/algorithms/SFD/SFDDescent.h   |  5 ++---
 5 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/src/shared/algorithms/SFD/SFDAscent.cpp b/src/shared/algorithms/SFD/SFDAscent.cpp
index 0ce072078..8842f7221 100644
--- a/src/shared/algorithms/SFD/SFDAscent.cpp
+++ b/src/shared/algorithms/SFD/SFDAscent.cpp
@@ -31,11 +31,11 @@ namespace Boardcore
 
 SFDAscent::SFDAscent(const SFDAscentConfig& config) : svm(config.modelParameters) {}
 
-SFDAscent::FeaturesVec SFDAscent::getFeatures(const VectorIn& input)
+SFDAscent::FeaturesVec SFDAscent::getFeatures(const SFDVectorIn& input)
 {
     float delta, min, max, u, var, s2, m4, rfmean, rfvar;
-    VectorIn rfourier, x0;
-    VectorIn data        = VectorIn::Zero();
+    SFDVectorIn rfourier, x0;
+    SFDVectorIn data        = SFDVectorIn::Zero();
     FeaturesVec features = FeaturesVec::Zero();
 
     min   = input.minCoeff();
@@ -44,14 +44,14 @@ SFDAscent::FeaturesVec SFDAscent::getFeatures(const VectorIn& input)
     for (int i = 0; i < LEN_CHUNK; i++)
         data(i) = (input(i) - min) / (std::max(delta, 1e-25f) * 2) - 1;
     u   = data.mean();
-    x0  = data - u * VectorIn::Ones();
+    x0  = data - u * SFDVectorIn::Ones();
     var = x0.squaredNorm() / LEN_CHUNK;
     s2  = x0.array().pow(2).mean();
     m4  = x0.array().pow(4).mean();
 
     rfourier = FFT32::fft(data).real();  // TODO: fix complex -> float
     rfmean   = rfourier.mean();
-    rfvar    = (rfourier - rfmean * VectorIn::Ones()).squaredNorm() / LEN_CHUNK;
+    rfvar    = (rfourier - rfmean * SFDVectorIn::Ones()).squaredNorm() / LEN_CHUNK;
 
     features(0) = delta;
     features(1) = var;
@@ -63,7 +63,7 @@ SFDAscent::FeaturesVec SFDAscent::getFeatures(const VectorIn& input)
     return features;
 }
 
-bool SFDAscent::classify(const VectorIn& input)
+bool SFDAscent::classify(const SFDVectorIn& input)
 {
     FeaturesVec features = getFeatures(input);
     float score          = svm.score(features);
diff --git a/src/shared/algorithms/SFD/SFDAscent.h b/src/shared/algorithms/SFD/SFDAscent.h
index 0e3d99ce9..95f98437c 100644
--- a/src/shared/algorithms/SFD/SFDAscent.h
+++ b/src/shared/algorithms/SFD/SFDAscent.h
@@ -40,7 +40,6 @@ public:
 
     using SVMn        = SVM<NUM_FEATURES>;
     using FeaturesVec = Eigen::Vector<float, NUM_FEATURES>;
-    using VectorIn    = Eigen::Vector<float, LEN_CHUNK>;
 
     struct SFDAscentConfig
     {
@@ -49,12 +48,12 @@ public:
 
     explicit SFDAscent(const SFDAscentConfig& config);
 
-    bool classify(const VectorIn& input);
+    bool classify(const SFDVectorIn& input);
 
 private:
     SVMn svm;
 
-    FeaturesVec getFeatures(const VectorIn& input);
+    FeaturesVec getFeatures(const SFDVectorIn& input);
 };
 
 }  // namespace Boardcore
diff --git a/src/shared/algorithms/SFD/SFDCommon.h b/src/shared/algorithms/SFD/SFDCommon.h
index 0ee8e7c72..8c5621d43 100644
--- a/src/shared/algorithms/SFD/SFDCommon.h
+++ b/src/shared/algorithms/SFD/SFDCommon.h
@@ -23,4 +23,13 @@
 
 #pragma once
 
+#include <Eigen/Core>
+
+namespace Boardcore
+{
+
 static constexpr int LEN_CHUNK = 32;
+
+using SFDVectorIn = Eigen::Vector<float, LEN_CHUNK>;
+
+}  // namespace Boardcore
diff --git a/src/shared/algorithms/SFD/SFDDescent.cpp b/src/shared/algorithms/SFD/SFDDescent.cpp
index aad11791e..447d38812 100644
--- a/src/shared/algorithms/SFD/SFDDescent.cpp
+++ b/src/shared/algorithms/SFD/SFDDescent.cpp
@@ -31,11 +31,11 @@ SFDDescent::SFDDescent(const SFDDescentConfig& config) : svm(config.modelParamet
 {
 }
 
-SFDDescent::FeaturesVec SFDDescent::getFeatures(const VectorIn& input)
+SFDDescent::FeaturesVec SFDDescent::getFeatures(const SFDVectorIn& input)
 {
     float delta, min, max, u, s2, m3, m4, rms;
-    VectorIn rfourier, x0;
-    VectorIn data        = VectorIn::Zero();
+    SFDVectorIn rfourier, x0;
+    SFDVectorIn data        = SFDVectorIn::Zero();
     FeaturesVec features = FeaturesVec::Zero();
 
     min   = input.minCoeff();
@@ -44,7 +44,7 @@ SFDDescent::FeaturesVec SFDDescent::getFeatures(const VectorIn& input)
     for (int i = 0; i < LEN_CHUNK; i++)
         data(i) = (input(i) - min) / (std::max(delta, 1e-25f) * 2) - 1;
     u   = data.mean();
-    x0  = data - u * VectorIn::Ones();
+    x0  = data - u * SFDVectorIn::Ones();
     s2  = x0.array().pow(2).mean();
     m3  = x0.array().pow(3).mean();
     m4  = x0.array().pow(4).mean();
@@ -59,7 +59,7 @@ SFDDescent::FeaturesVec SFDDescent::getFeatures(const VectorIn& input)
     return features;
 }
 
-bool SFDDescent::classify(const VectorIn& input)
+bool SFDDescent::classify(const SFDVectorIn& input)
 {
     FeaturesVec features = getFeatures(input);
     float score          = svm.score(features);
diff --git a/src/shared/algorithms/SFD/SFDDescent.h b/src/shared/algorithms/SFD/SFDDescent.h
index 90a717bce..c41a57aa3 100644
--- a/src/shared/algorithms/SFD/SFDDescent.h
+++ b/src/shared/algorithms/SFD/SFDDescent.h
@@ -40,7 +40,6 @@ public:
 
     using SVMn        = SVM<NUM_FEATURES>;
     using FeaturesVec = Eigen::Vector<float, NUM_FEATURES>;
-    using VectorIn    = Eigen::Vector<float, LEN_CHUNK>;
 
     struct SFDDescentConfig
     {
@@ -49,12 +48,12 @@ public:
 
     explicit SFDDescent(const SFDDescentConfig& config);
 
-    bool classify(const VectorIn& input);
+    bool classify(const SFDVectorIn& input);
 
 private:
     SVMn svm;
 
-    FeaturesVec getFeatures(const VectorIn& input);
+    FeaturesVec getFeatures(const SFDVectorIn& input);
 };
 
 }  // namespace Boardcore
-- 
GitLab