Skip to content

Commit

Permalink
[clang-format] Setup and run clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
arntanguy committed Jun 19, 2020
1 parent 3bbd370 commit 0fbbcea
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 151 deletions.
105 changes: 103 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -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: '^(<mc_[a-z]*/)'
Priority: 1
- Regex: '^(<Tasks/)'
Priority: 2
- Regex: '^(<mc_rbdyn_urdf/)'
Priority: 3
- Regex: '^(<RBDyn/)'
Priority: 4
- Regex: '^(<SpaceVecAlg/)'
Priority: 5
- Regex: '^(<sch-core/)'
Priority: 6
- Regex: '^(<boost/)'
Priority: 7
- Regex: '^(<(nanomsg|geos|.*_msgs|ros|tf2.*)/)'
Priority: 8
- Regex: '.*'
Priority: 9
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentPPDirectives: AfterHash
IndentWidth: 2
IndentWrappedFunctionNames: true
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 150
PenaltyReturnTypeOnItsOwnLine: 300
PointerAlignment: Middle
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 2
UseTab: Never
...

20 changes: 20 additions & 0 deletions .clang-format-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

readonly this_dir=`cd $(dirname $0); pwd`
cd $this_dir
source .clang-format-common.sh

out=0
tmpfile=$(mktemp /tmp/clang-format-check.XXXXXX)
for f in ${src_files}; do
$clang_format -style=file $f > $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
14 changes: 14 additions & 0 deletions .clang-format-common.sh
Original file line number Diff line number Diff line change
@@ -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'`
9 changes: 9 additions & 0 deletions .clang-format-fix.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
34 changes: 18 additions & 16 deletions include/gram_savitzky_golay/gram_savitzky_golay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

/*!
Expand All @@ -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)
{
}

Expand All @@ -83,24 +84,24 @@ struct SavitzkyGolayFilterConfig

unsigned window_size() const
{
return 2*m+1;
return 2 * m + 1;
}

double time_step() const
{
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
Expand All @@ -115,13 +116,14 @@ struct SavitzkyGolayFilter
*
* @return Filtered value according to the precomputed filter weights.
*/
template <typename ContainerT>
typename ContainerT::value_type filter(const ContainerT& v) const
template<typename ContainerT>
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_;
Expand All @@ -137,11 +139,11 @@ struct SavitzkyGolayFilter
return conf_;
}

private:
private:
SavitzkyGolayFilterConfig conf_;
std::vector<double> weights_;
void init();
double dt_;
};

} /* gram_sg */
} // namespace gram_sg
48 changes: 27 additions & 21 deletions include/gram_savitzky_golay/spatial_filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,37 @@
// along with robcalib. If not, see <http://www.gnu.org/licenses/>.

#pragma once
#include <gram_savitzky_golay/gram_savitzky_golay.h>
#include <boost/circular_buffer.hpp>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <gram_savitzky_golay/gram_savitzky_golay.h>

namespace gram_sg
{
using Vector6d = Eigen::Matrix< double, 6, 1>;
using Vector6d = Eigen::Matrix<double, 6, 1>;

template <typename T>
template<typename T>
class EigenVectorFilter
{
protected:
protected:
/** Filtering **/
gram_sg::SavitzkyGolayFilterConfig sg_conf;
gram_sg::SavitzkyGolayFilter sg_filter;
// Buffers for Savitzky_golay
boost::circular_buffer<T> 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);
}
Expand All @@ -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;
Expand All @@ -90,12 +96,12 @@ class RotationFilter
// Buffers for Savitzky_golay
boost::circular_buffer<Eigen::Matrix3d> 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
{
Expand All @@ -111,16 +117,16 @@ class RotationFilter
*/
class TransformFilter
{
private:
private:
EigenVectorFilter<Eigen::Vector3d> 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
{
Expand All @@ -132,4 +138,4 @@ class TransformFilter
}
};

}
} // namespace gram_sg
Loading

0 comments on commit 0fbbcea

Please sign in to comment.