From 0fbbceac2332221f651c225415045b538b6abdd5 Mon Sep 17 00:00:00 2001 From: Arnaud TANGUY Date: Fri, 19 Jun 2020 21:03:36 +0900 Subject: [PATCH] [clang-format] Setup and run clang-format --- .clang-format | 105 +++++++++++++++++- .clang-format-check.sh | 20 ++++ .clang-format-common.sh | 14 +++ .clang-format-fix.sh | 9 ++ CMakeLists.txt | 2 +- .../gram_savitzky_golay/gram_savitzky_golay.h | 34 +++--- include/gram_savitzky_golay/spatial_filters.h | 48 ++++---- src/gram_savitzky_golay.cpp | 40 ++++--- src/spatial_filters.cpp | 33 +++--- {test => tests}/CMakeLists.txt | 0 {test => tests}/test_gram_savitzky_golay.cpp | 92 +++++++-------- {test => tests}/test_spatial_filters.cpp | 47 ++++---- 12 files changed, 293 insertions(+), 151 deletions(-) create mode 100755 .clang-format-check.sh create mode 100644 .clang-format-common.sh create mode 100755 .clang-format-fix.sh rename {test => tests}/CMakeLists.txt (100%) rename {test => tests}/test_gram_savitzky_golay.cpp (78%) rename {test => tests}/test_spatial_filters.cpp (56%) diff --git a/.clang-format b/.clang-format index c7993f5..becae90 100644 --- a/.clang-format +++ b/.clang-format @@ -1,5 +1,106 @@ --- -BasedOnStyle: Google +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlinesLeft: true +AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: false +BreakBeforeBinaryOperators: NonAssignment BreakBeforeBraces: Allman -ColumnLimit: 0 +BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 120 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 0 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^( $tmpfile + if ! [[ -z `diff $tmpfile $f` ]]; then + echo "Wrong formatting in $f" + out=1 + fi +done +rm -f $tmpfile +if [[ $out -eq 1 ]]; then + echo "You can run ./.clang-format-fix.sh to fix the issues locally, then commit/push again" +fi +exit $out diff --git a/.clang-format-common.sh b/.clang-format-common.sh new file mode 100644 index 0000000..665b6e3 --- /dev/null +++ b/.clang-format-common.sh @@ -0,0 +1,14 @@ +# This script is meant to be sourced from other scripts + +# Check for clang-format, prefer 6.0 if available +if [[ -x "$(command -v clang-format-6.0)" ]]; then + clang_format=clang-format-6.0 +elif [[ -x "$(command -v clang-format)" ]]; then + clang_format=clang-format +else + echo "clang-format or clang-format-6.0 must be installed" + exit 1 +fi + +# Find all source files in the project minus those that are auto-generated or we do not maintain +src_files=`find include include src tests -name '*.cpp' -or -name '*.h' -or -name '*.hpp'` diff --git a/.clang-format-fix.sh b/.clang-format-fix.sh new file mode 100755 index 0000000..4695d2c --- /dev/null +++ b/.clang-format-fix.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +readonly this_dir=`cd $(dirname $0); pwd` +cd $this_dir +source .clang-format-common.sh + +for f in ${src_files}; do + $clang_format -style=file -i $f +done diff --git a/CMakeLists.txt b/CMakeLists.txt index 562a52e..4b95c93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ add_subdirectory(src) if(BUILD_TESTS) enable_testing() find_package(Boost COMPONENTS unit_test_framework REQUIRED) - add_subdirectory(test) + add_subdirectory(tests) endif() install( diff --git a/include/gram_savitzky_golay/gram_savitzky_golay.h b/include/gram_savitzky_golay/gram_savitzky_golay.h index 2a99945..37105de 100644 --- a/include/gram_savitzky_golay/gram_savitzky_golay.h +++ b/include/gram_savitzky_golay/gram_savitzky_golay.h @@ -26,8 +26,8 @@ namespace gram_sg { /*! GRAMPOLY Calculates the Gram Polynomial (s=0) or its sth derivative - * evaluated at i, order k, over 2m+1 points - */ + * evaluated at i, order k, over 2m+1 points + */ double GramPoly(const int i, const int m, const int k, const int s); /*! GenFact Calculates the generalized factorial (a)(a-1)(a-2)...(a-b+1) @@ -36,9 +36,9 @@ double GramPoly(const int i, const int m, const int k, const int s); double GenFact(const int a, const int b); /*! - * Weight Calculates the weight of the ith data point for the t'th - * Least-Square point of the s'th derivative, over 2m+1 points, order n - */ + * Weight Calculates the weight of the ith data point for the t'th + * Least-Square point of the s'th derivative, over 2m+1 points, order n + */ double Weight(const int i, const int t, const int m, const int n, const int s); /*! @@ -62,7 +62,8 @@ struct SavitzkyGolayFilterConfig double dt = 1; SavitzkyGolayFilterConfig() {} - SavitzkyGolayFilterConfig(const int m, const int t, const int n, const int s, const double dt = 1.) : m(m), t(t), n(n), s(s), dt(dt) + SavitzkyGolayFilterConfig(const int m, const int t, const int n, const int s, const double dt = 1.) + : m(m), t(t), n(n), s(s), dt(dt) { } @@ -83,7 +84,7 @@ struct SavitzkyGolayFilterConfig unsigned window_size() const { - return 2*m+1; + return 2 * m + 1; } double time_step() const @@ -91,16 +92,16 @@ struct SavitzkyGolayFilterConfig return dt; } - friend std::ostream& operator<<(std::ostream& os, const SavitzkyGolayFilterConfig& conf); + friend std::ostream & operator<<(std::ostream & os, const SavitzkyGolayFilterConfig & conf); }; struct SavitzkyGolayFilter { SavitzkyGolayFilter(const int m, const int t, const int n, const int s, const double dt = 1.); - SavitzkyGolayFilter(const SavitzkyGolayFilterConfig& conf); + SavitzkyGolayFilter(const SavitzkyGolayFilterConfig & conf); SavitzkyGolayFilter(); - void configure(const SavitzkyGolayFilterConfig& conf); + void configure(const SavitzkyGolayFilterConfig & conf); /** * @brief Apply Savitzky-Golay convolution to the data x should have size 2*m+1 @@ -115,13 +116,14 @@ struct SavitzkyGolayFilter * * @return Filtered value according to the precomputed filter weights. */ - template - typename ContainerT::value_type filter(const ContainerT& v) const + template + typename ContainerT::value_type filter(const ContainerT & v) const { assert(v.size() == weights_.size() && v.size() > 0); using T = typename ContainerT::value_type; - T res = weights_[0]*v[0]; - for (int i = 1; i < v.size(); ++i) { + T res = weights_[0] * v[0]; + for(int i = 1; i < v.size(); ++i) + { res += weights_[i] * v[i]; } return res / dt_; @@ -137,11 +139,11 @@ struct SavitzkyGolayFilter return conf_; } - private: +private: SavitzkyGolayFilterConfig conf_; std::vector weights_; void init(); double dt_; }; -} /* gram_sg */ +} // namespace gram_sg diff --git a/include/gram_savitzky_golay/spatial_filters.h b/include/gram_savitzky_golay/spatial_filters.h index 2b4e66c..ebb3923 100644 --- a/include/gram_savitzky_golay/spatial_filters.h +++ b/include/gram_savitzky_golay/spatial_filters.h @@ -17,37 +17,37 @@ // along with robcalib. If not, see . #pragma once -#include #include #include #include +#include namespace gram_sg { -using Vector6d = Eigen::Matrix< double, 6, 1>; +using Vector6d = Eigen::Matrix; -template +template class EigenVectorFilter { - protected: +protected: /** Filtering **/ gram_sg::SavitzkyGolayFilterConfig sg_conf; gram_sg::SavitzkyGolayFilter sg_filter; // Buffers for Savitzky_golay boost::circular_buffer buffer; - public: - EigenVectorFilter(const gram_sg::SavitzkyGolayFilterConfig& conf) - : sg_conf(conf), sg_filter(conf), buffer(2 * sg_filter.config().m + 1) +public: + EigenVectorFilter(const gram_sg::SavitzkyGolayFilterConfig & conf) + : sg_conf(conf), sg_filter(conf), buffer(2 * sg_filter.config().m + 1) { reset(T::Zero()); } - void reset(const T& data) + void reset(const T & data) { buffer.clear(); // Initialize to data - for (size_t i = 0; i < buffer.capacity(); i++) + for(size_t i = 0; i < buffer.capacity(); i++) { buffer.push_back(data); } @@ -63,8 +63,14 @@ class EigenVectorFilter buffer.clear(); } - void add(const T& data) { buffer.push_back(data); } - T filter() const { return sg_filter.filter(buffer); } + void add(const T & data) + { + buffer.push_back(data); + } + T filter() const + { + return sg_filter.filter(buffer); + } gram_sg::SavitzkyGolayFilterConfig config() const { return sg_conf; @@ -90,12 +96,12 @@ class RotationFilter // Buffers for Savitzky_golay boost::circular_buffer buffer; - public: - RotationFilter(const gram_sg::SavitzkyGolayFilterConfig& conf); - void reset(const Eigen::Matrix3d& r); +public: + RotationFilter(const gram_sg::SavitzkyGolayFilterConfig & conf); + void reset(const Eigen::Matrix3d & r); void reset(); void clear(); - void add(const Eigen::Matrix3d& r); + void add(const Eigen::Matrix3d & r); Eigen::Matrix3d filter() const; bool ready() const { @@ -111,16 +117,16 @@ class RotationFilter */ class TransformFilter { - private: +private: EigenVectorFilter trans_filter; RotationFilter rot_filter; - public: - TransformFilter(const gram_sg::SavitzkyGolayFilterConfig& conf); - void reset(const Eigen::Affine3d& T); +public: + TransformFilter(const gram_sg::SavitzkyGolayFilterConfig & conf); + void reset(const Eigen::Affine3d & T); void reset(); void clear(); - void add(const Eigen::Affine3d& T); + void add(const Eigen::Affine3d & T); Eigen::Affine3d filter() const; gram_sg::SavitzkyGolayFilterConfig config() const { @@ -132,4 +138,4 @@ class TransformFilter } }; -} +} // namespace gram_sg diff --git a/src/gram_savitzky_golay.cpp b/src/gram_savitzky_golay.cpp index b273c64..86e5dfd 100644 --- a/src/gram_savitzky_golay.cpp +++ b/src/gram_savitzky_golay.cpp @@ -24,13 +24,14 @@ namespace gram_sg // OK double GramPoly(const int i, const int m, const int k, const int s) { - if (k > 0) + if(k > 0) { - return (4. * k - 2.) / (k * (2. * m - k + 1.)) * (i * GramPoly(i, m, k - 1, s) + s * GramPoly(i, m, k - 1, s - 1)) - ((k - 1.) * (2. * m + k)) / (k * (2. * m - k + 1.)) * GramPoly(i, m, k - 2, s); + return (4. * k - 2.) / (k * (2. * m - k + 1.)) * (i * GramPoly(i, m, k - 1, s) + s * GramPoly(i, m, k - 1, s - 1)) + - ((k - 1.) * (2. * m + k)) / (k * (2. * m - k + 1.)) * GramPoly(i, m, k - 2, s); } else { - if (k == 0 && s == 0) + if(k == 0 && s == 0) return 1.; else return 0.; @@ -42,7 +43,7 @@ double GenFact(const int a, const int b) { double gf = 1.; - for (int j = (a - b) + 1; j <= a; j++) + for(int j = (a - b) + 1; j <= a; j++) { gf *= j; } @@ -52,9 +53,11 @@ double GenFact(const int a, const int b) double Weight(const int i, const int t, const int m, const int n, const int s) { double w = 0; - for (int k = 0; k <= n; ++k) + for(int k = 0; k <= n; ++k) { - w = w + (2 * k + 1) * (GenFact(2 * m, k) / GenFact(2 * m + k + 1, k + 1)) * GramPoly(i, m, k, 0) * GramPoly(t, m, k, s); + w = w + + (2 * k + 1) * (GenFact(2 * m, k) / GenFact(2 * m + k + 1, k + 1)) * GramPoly(i, m, k, 0) + * GramPoly(t, m, k, s); } return w; } @@ -62,19 +65,20 @@ double Weight(const int i, const int t, const int m, const int n, const int s) std::vector ComputeWeights(const int m, const int t, const int n, const int s) { std::vector weights(2 * m + 1); - for (int i = 0; i < 2 * m + 1; ++i) + for(int i = 0; i < 2 * m + 1; ++i) { weights[i] = Weight(i - m, t, m, n, s); } return weights; } -SavitzkyGolayFilter::SavitzkyGolayFilter(const int m, const int t, const int n, const int s, const double dt) : conf_(m,t,n,s,dt) +SavitzkyGolayFilter::SavitzkyGolayFilter(const int m, const int t, const int n, const int s, const double dt) +: conf_(m, t, n, s, dt) { init(); } -SavitzkyGolayFilter::SavitzkyGolayFilter(const SavitzkyGolayFilterConfig& conf) : conf_(conf) +SavitzkyGolayFilter::SavitzkyGolayFilter(const SavitzkyGolayFilterConfig & conf) : conf_(conf) { init(); } @@ -84,7 +88,7 @@ SavitzkyGolayFilter::SavitzkyGolayFilter() : conf_(SavitzkyGolayFilterConfig()) init(); } -void SavitzkyGolayFilter::configure(const SavitzkyGolayFilterConfig& conf) +void SavitzkyGolayFilter::configure(const SavitzkyGolayFilterConfig & conf) { conf_ = conf; init(); @@ -98,14 +102,14 @@ void SavitzkyGolayFilter::init() dt_ = std::pow(conf_.time_step(), conf_.derivation_order()); } -std::ostream& operator<<(std::ostream& os, const SavitzkyGolayFilterConfig& conf) +std::ostream & operator<<(std::ostream & os, const SavitzkyGolayFilterConfig & conf) { - os << "m : " << conf.m << std::endl - << "Window Size (2*m+1) : " << 2*conf.m+1 << std::endl - << "n (Order) :" << conf.n << std::endl - << "s (Differentiate) : " << conf.s << std::endl - << "t: Filter point ([-m,m]): " << conf.t << std::endl; - return os; + os << "m : " << conf.m << std::endl + << "Window Size (2*m+1) : " << 2 * conf.m + 1 << std::endl + << "n (Order) :" << conf.n << std::endl + << "s (Differentiate) : " << conf.s << std::endl + << "t: Filter point ([-m,m]): " << conf.t << std::endl; + return os; } -} /* gram_sg */ +} // namespace gram_sg diff --git a/src/spatial_filters.cpp b/src/spatial_filters.cpp index 75849d1..2882717 100644 --- a/src/spatial_filters.cpp +++ b/src/spatial_filters.cpp @@ -18,23 +18,23 @@ #include "gram_savitzky_golay/spatial_filters.h" -#include #include +#include namespace gram_sg { -RotationFilter::RotationFilter(const gram_sg::SavitzkyGolayFilterConfig& conf) - : sg_conf(conf), sg_filter(conf), buffer(2 * sg_filter.config().m + 1) +RotationFilter::RotationFilter(const gram_sg::SavitzkyGolayFilterConfig & conf) +: sg_conf(conf), sg_filter(conf), buffer(2 * sg_filter.config().m + 1) { reset(Eigen::Matrix3d::Zero()); } -void RotationFilter::reset(const Eigen::Matrix3d& r) +void RotationFilter::reset(const Eigen::Matrix3d & r) { buffer.clear(); // Initialize to data - for (size_t i = 0; i < buffer.capacity(); i++) + for(size_t i = 0; i < buffer.capacity(); i++) { buffer.push_back(r); } @@ -50,22 +50,25 @@ void RotationFilter::clear() buffer.clear(); } -void RotationFilter::add(const Eigen::Matrix3d& r) { buffer.push_back(r); } +void RotationFilter::add(const Eigen::Matrix3d & r) +{ + buffer.push_back(r); +} Eigen::Matrix3d RotationFilter::filter() const { // Apply a temporal (savitzky-golay) convolution, // followed by an orthogonalization - const Eigen::Matrix3d& result = sg_filter.filter(buffer); + const Eigen::Matrix3d & result = sg_filter.filter(buffer); Eigen::JacobiSVD svd(result, Eigen::ComputeFullV | Eigen::ComputeFullU); Eigen::Matrix3d res = svd.matrixU() * svd.matrixV().transpose(); return res; } -TransformFilter::TransformFilter(const gram_sg::SavitzkyGolayFilterConfig& conf) : trans_filter(conf), rot_filter(conf) +TransformFilter::TransformFilter(const gram_sg::SavitzkyGolayFilterConfig & conf) : trans_filter(conf), rot_filter(conf) { } -void TransformFilter::reset(const Eigen::Affine3d& T) +void TransformFilter::reset(const Eigen::Affine3d & T) { trans_filter.reset(T.translation()); rot_filter.reset(T.rotation()); @@ -83,7 +86,7 @@ void TransformFilter::clear() rot_filter.clear(); } -void TransformFilter::add(const Eigen::Affine3d& T) +void TransformFilter::add(const Eigen::Affine3d & T) { trans_filter.add(T.translation()); rot_filter.add(T.rotation()); @@ -91,12 +94,12 @@ void TransformFilter::add(const Eigen::Affine3d& T) Eigen::Affine3d TransformFilter::filter() const { - const Eigen::Vector3d& trans_res = trans_filter.filter(); - const Eigen::Matrix3d& rot_res = rot_filter.filter(); + const Eigen::Vector3d & trans_res = trans_filter.filter(); + const Eigen::Matrix3d & rot_res = rot_filter.filter(); Eigen::Matrix4d rot = Eigen::Matrix4d::Identity(); - rot.block<3,3>(0,0) = rot_res; - rot.block<3,1>(0,3) = trans_res; + rot.block<3, 3>(0, 0) = rot_res; + rot.block<3, 1>(0, 3) = trans_res; return Eigen::Affine3d(rot); } -} +} // namespace gram_sg diff --git a/test/CMakeLists.txt b/tests/CMakeLists.txt similarity index 100% rename from test/CMakeLists.txt rename to tests/CMakeLists.txt diff --git a/test/test_gram_savitzky_golay.cpp b/tests/test_gram_savitzky_golay.cpp similarity index 78% rename from test/test_gram_savitzky_golay.cpp rename to tests/test_gram_savitzky_golay.cpp index 7c8a0ac..62aa4bc 100644 --- a/test/test_gram_savitzky_golay.cpp +++ b/tests/test_gram_savitzky_golay.cpp @@ -16,17 +16,17 @@ // You should have received a copy of the GNU Lesser General Public License // along with robcalib. If not, see . -//Link to Boost +// Link to Boost #define BOOST_TEST_DYN_LINK -//Define our Module name (prints at testing) +// Define our Module name (prints at testing) #define BOOST_TEST_MODULE MyTest #include +#include "gram_savitzky_golay/gram_savitzky_golay.h" +#include #include #include -#include -#include "gram_savitzky_golay/gram_savitzky_golay.h" using namespace gram_sg; @@ -35,20 +35,13 @@ BOOST_AUTO_TEST_CASE(TestGorryTables) // Compare with tables in the paper from Gorry. // Convolution weights for quadratic initial-point smoothing: // polynomial order = 2, derivative = 0 - std::vector sg7_gram{ - 32, - 15, - 3, - -4, - -6, - -3, - 5}; + std::vector sg7_gram{32, 15, 3, -4, -6, -3, 5}; int m = 3; SavitzkyGolayFilter filter(m, -m, 2, 0); - const auto& filter_weights = filter.weights(); - for (unsigned int i = 0; i < sg7_gram.size(); i++) + const auto & filter_weights = filter.weights(); + for(unsigned int i = 0; i < sg7_gram.size(); i++) { std::cout << "ref: " << sg7_gram[i] << ", computed: " << filter_weights[i] * 42 << std::endl; BOOST_REQUIRE_CLOSE(sg7_gram[i], filter_weights[i] * 42, 10e-6); @@ -62,27 +55,19 @@ BOOST_AUTO_TEST_CASE(TestGorryDerivative) // Compare with tables in the paper from Gorry. // Convolution weights for quadratic initial-point first derivative: // polynomial order = 2, derivative = 1 - std::vector sg7_deriv_gram{ - -13, - -2, - 5, - 8, - 7, - 2, - -7}; + std::vector sg7_deriv_gram{-13, -2, 5, 8, 7, 2, -7}; int m = 3; SavitzkyGolayFilter filter(m, -m, 2, 1); - const auto& filter_weights = filter.weights(); - for (unsigned int i = 0; i < sg7_deriv_gram.size(); i++) + const auto & filter_weights = filter.weights(); + for(unsigned int i = 0; i < sg7_deriv_gram.size(); i++) { std::cout << "ref: " << sg7_deriv_gram[i] << ", computed: " << filter_weights[i] * 28 << std::endl; BOOST_REQUIRE_CLOSE(sg7_deriv_gram[i], filter_weights[i] * 28, 10e-6); } } - BOOST_AUTO_TEST_CASE(TestIdentity) { int m = 3; @@ -121,7 +106,6 @@ BOOST_AUTO_TEST_CASE(TestRealTimeDerivative) // Points are defined in range [-m;m] const int t = m; - // Test First Order Derivative SavitzkyGolayFilter filter(m, t, n, 1); SavitzkyGolayFilter filter_dt(m, t, n, 1, 0.005); @@ -136,7 +120,7 @@ BOOST_AUTO_TEST_CASE(TestRealTimeDerivative) // Test filtering with timestep=0.005 data = {.1, .2, .3, .4, .5, .6, .7}; result = filter_dt.filter(data); - result_ref = 0.1/filter_dt.config().time_step(); + result_ref = 0.1 / filter_dt.config().time_step(); BOOST_REQUIRE_CLOSE(result, result_ref, 10e-6); // Filter some data @@ -146,11 +130,9 @@ BOOST_AUTO_TEST_CASE(TestRealTimeDerivative) BOOST_REQUIRE_CLOSE(result, result_ref, 10e-6); // Test filtering with timestep=0.005 result = filter_dt.filter(data); - result_ref = -1./filter_dt.config().time_step(); + result_ref = -1. / filter_dt.config().time_step(); BOOST_REQUIRE_CLOSE(result, result_ref, 10e-6); - - // Test Second Order Derivative SavitzkyGolayFilter second_order_filter(m, t, n, 2); @@ -175,7 +157,6 @@ BOOST_AUTO_TEST_CASE(TestPolynomialDerivative) double d = -4; double timeStep = 0.42; - // Window size is 2*m+1 const int m = 50; // Polynomial Order @@ -188,14 +169,15 @@ BOOST_AUTO_TEST_CASE(TestPolynomialDerivative) SavitzkyGolayFilter filter_order2(m, t, n, 2, timeStep); std::vector data; std::vector derivative_order1, derivative_order2; - data.resize(2*m+1); - derivative_order1.resize(2*m+1); - derivative_order2.resize(2*m+1); + data.resize(2 * m + 1); + derivative_order1.resize(2 * m + 1); + derivative_order2.resize(2 * m + 1); // Generate some data points - for (unsigned x = 0; x < data.size(); ++x) { + for(unsigned x = 0; x < data.size(); ++x) + { data[x] = a * std::pow(x, 3) + b * std::pow(x, 2) + c * std::pow(x, 1) + d; - derivative_order1[x] = (3*a*std::pow(x, 2) + 2*b*std::pow(x,1) + c)/timeStep; - derivative_order2[x] = (6*a*std::pow(x, 1) + 2*b) / std::pow(timeStep,2); + derivative_order1[x] = (3 * a * std::pow(x, 2) + 2 * b * std::pow(x, 1) + c) / timeStep; + derivative_order2[x] = (6 * a * std::pow(x, 1) + 2 * b) / std::pow(timeStep, 2); } const auto result_order1 = filter_order1.filter(data); const auto expected_result_order1 = derivative_order1[m]; @@ -208,21 +190,23 @@ BOOST_AUTO_TEST_CASE(TestPolynomialDerivative) BOOST_AUTO_TEST_CASE(FilterSpeed) { - int m = 10000; - SavitzkyGolayFilter filter(m, 0, 2, 0); - std::vector data(2*m+1,1.); - - double totalTime = 0; - int Nsample = 1000; - for (int i = 0; i < Nsample; ++i) { - auto start_time = std::chrono::high_resolution_clock::now(); - filter.filter(data); - auto end_time = std::chrono::high_resolution_clock::now(); - std::chrono::duration elapsed = end_time - start_time; - totalTime += elapsed.count(); - } - totalTime /= Nsample; - std::cout << "Filtering performed in " << totalTime << " (ms)" << std::endl;; - - BOOST_REQUIRE(totalTime < 0.0001); + int m = 10000; + SavitzkyGolayFilter filter(m, 0, 2, 0); + std::vector data(2 * m + 1, 1.); + + double totalTime = 0; + int Nsample = 1000; + for(int i = 0; i < Nsample; ++i) + { + auto start_time = std::chrono::high_resolution_clock::now(); + filter.filter(data); + auto end_time = std::chrono::high_resolution_clock::now(); + std::chrono::duration elapsed = end_time - start_time; + totalTime += elapsed.count(); + } + totalTime /= Nsample; + std::cout << "Filtering performed in " << totalTime << " (ms)" << std::endl; + ; + + BOOST_REQUIRE(totalTime < 0.0001); } diff --git a/test/test_spatial_filters.cpp b/tests/test_spatial_filters.cpp similarity index 56% rename from test/test_spatial_filters.cpp rename to tests/test_spatial_filters.cpp index bf04153..9d49ef2 100644 --- a/test/test_spatial_filters.cpp +++ b/tests/test_spatial_filters.cpp @@ -16,7 +16,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with robcalib. If not, see . -//Link to Boost +// Link to Boost #define BOOST_TEST_DYN_LINK #define BOOST_TEST_MODULE TestSpatialFilters @@ -34,46 +34,45 @@ BOOST_AUTO_TEST_CASE(test_transform_filter) TransformFilter filter(sg_conf); Eigen::Matrix3d rot_sg; - rot_sg = Eigen::AngleAxisd(1.2, Eigen::Vector3d::UnitX()) * - Eigen::AngleAxisd(1.9, Eigen::Vector3d::UnitY()) * - Eigen::AngleAxisd(2.3, Eigen::Vector3d::UnitZ()); + rot_sg = Eigen::AngleAxisd(1.2, Eigen::Vector3d::UnitX()) * Eigen::AngleAxisd(1.9, Eigen::Vector3d::UnitY()) + * Eigen::AngleAxisd(2.3, Eigen::Vector3d::UnitZ()); Eigen::Affine3d X_init = Eigen::Affine3d::Identity(); - X_init.matrix().block<3,3>(0,0) = rot_sg; - X_init.matrix().block<3,1>(0,3) = Eigen::Vector3d{0.1, 5, 0.3}; + X_init.matrix().block<3, 3>(0, 0) = rot_sg; + X_init.matrix().block<3, 1>(0, 3) = Eigen::Vector3d{0.1, 5, 0.3}; filter.reset(X_init); - const auto& res_init = filter.filter(); - BOOST_CHECK_MESSAGE(X_init.matrix().isApprox(res_init.matrix()), "\n" << X_init.matrix() << "\n-----\n" << res_init.matrix()); + const auto & res_init = filter.filter(); + BOOST_CHECK_MESSAGE(X_init.matrix().isApprox(res_init.matrix()), "\n" << X_init.matrix() << "\n-----\n" + << res_init.matrix()); } { gram_sg::SavitzkyGolayFilterConfig sg_conf(50, 50, 2, 0); TransformFilter filter(sg_conf); Eigen::Matrix3d rot_sg; - rot_sg = Eigen::AngleAxisd(M_PI, Eigen::Vector3d::UnitX()) * - Eigen::AngleAxisd(-M_PI/2, Eigen::Vector3d::UnitY()) * - Eigen::AngleAxisd(M_PI, Eigen::Vector3d::UnitZ()); + rot_sg = Eigen::AngleAxisd(M_PI, Eigen::Vector3d::UnitX()) * Eigen::AngleAxisd(-M_PI / 2, Eigen::Vector3d::UnitY()) + * Eigen::AngleAxisd(M_PI, Eigen::Vector3d::UnitZ()); Eigen::Affine3d X_init = Eigen::Affine3d::Identity(); - X_init.matrix().block<3,3>(0,0) = rot_sg; - X_init.matrix().block<3,1>(0,3) = Eigen::Vector3d{0.1, 5, 0.3}; + X_init.matrix().block<3, 3>(0, 0) = rot_sg; + X_init.matrix().block<3, 1>(0, 3) = Eigen::Vector3d{0.1, 5, 0.3}; filter.reset(X_init); - const auto& res_init = filter.filter(); - BOOST_CHECK_MESSAGE(X_init.matrix().isApprox(res_init.matrix()), "\n" << X_init.matrix() << "\n-----\n" << res_init.matrix()); + const auto & res_init = filter.filter(); + BOOST_CHECK_MESSAGE(X_init.matrix().isApprox(res_init.matrix()), "\n" << X_init.matrix() << "\n-----\n" + << res_init.matrix()); } } BOOST_AUTO_TEST_CASE(test_rotation_filter) { - Eigen::Matrix3d rot; - rot = Eigen::AngleAxisd(1.2, Eigen::Vector3d::UnitX()) * - Eigen::AngleAxisd(M_PI/2, Eigen::Vector3d::UnitY()) * - Eigen::AngleAxisd(0.3, Eigen::Vector3d::UnitZ()); + Eigen::Matrix3d rot; + rot = Eigen::AngleAxisd(1.2, Eigen::Vector3d::UnitX()) * Eigen::AngleAxisd(M_PI / 2, Eigen::Vector3d::UnitY()) + * Eigen::AngleAxisd(0.3, Eigen::Vector3d::UnitZ()); - gram_sg::SavitzkyGolayFilterConfig sg_conf(50, 50, 2, 0); - RotationFilter filter(sg_conf); + gram_sg::SavitzkyGolayFilterConfig sg_conf(50, 50, 2, 0); + RotationFilter filter(sg_conf); - filter.reset(rot); + filter.reset(rot); - const Eigen::Matrix3d res = filter.filter(); - BOOST_CHECK(rot.isApprox(res)); + const Eigen::Matrix3d res = filter.filter(); + BOOST_CHECK(rot.isApprox(res)); }