fix formatting for (non-generated) internal source files

- Change formatting standard to Cpp11. Main difference is not having
  the space between two closing >> for nested templates. We don't
  choose c++14, because older versions of clang-format (version 9
  and earlier) don't know this value yet, and it doesn't make a
  difference in the formatting.
- Apply clang-format to all (non generated) internal source files.
- Manually fix some code sections (clang-format on/off) and c-strings
- Exclude some embedded external files with very different formatting
  (gtest/gmock)
- Add script to format all source files

Change-Id: Ic6cea41575ad6e37c9e136dbce176b0d505dc44d
This commit is contained in:
Nikolaus Demmel
2020-09-20 21:45:24 +02:00
parent 921368ce31
commit 7b8f675bfd
272 changed files with 3449 additions and 3319 deletions
+36 -40
View File
@@ -33,12 +33,12 @@
#include <algorithm>
#include <cmath>
#include "ceres/cost_function.h"
#include "ceres/test_util.h"
#include "ceres/types.h"
#include "gtest/gtest.h"
namespace ceres {
namespace internal {
@@ -55,23 +55,22 @@ bool EasyFunctor::operator()(const double* x1,
}
void EasyFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
const CostFunction& cost_function,
NumericDiffMethodType method) const {
// The x1[0] is made deliberately small to test the performance near
// zero.
const CostFunction& cost_function, NumericDiffMethodType method) const {
// The x1[0] is made deliberately small to test the performance near zero.
// clang-format off
double x1[] = { 1e-64, 2.0, 3.0, 4.0, 5.0 };
double x2[] = { 9.0, 9.0, 5.0, 5.0, 1.0 };
double *parameters[] = { &x1[0], &x2[0] };
// clang-format on
double dydx1[15]; // 3 x 5, row major.
double dydx2[15]; // 3 x 5, row major.
double *jacobians[2] = { &dydx1[0], &dydx2[0] };
double* jacobians[2] = {&dydx1[0], &dydx2[0]};
double residuals[3] = {-1e-100, -2e-100, -3e-100 };
double residuals[3] = {-1e-100, -2e-100, -3e-100};
ASSERT_TRUE(cost_function.Evaluate(&parameters[0],
&residuals[0],
&jacobians[0]));
ASSERT_TRUE(
cost_function.Evaluate(&parameters[0], &residuals[0], &jacobians[0]));
double expected_residuals[3];
EasyFunctor functor;
@@ -97,12 +96,14 @@ void EasyFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
}
for (int i = 0; i < 5; ++i) {
// clang-format off
ExpectClose(x2[i], dydx1[5 * 0 + i], tolerance); // y1
ExpectClose(x1[i], dydx2[5 * 0 + i], tolerance);
ExpectClose(2 * x2[i] * residuals[0], dydx1[5 * 1 + i], tolerance); // y2
ExpectClose(2 * x1[i] * residuals[0], dydx2[5 * 1 + i], tolerance);
ExpectClose(0.0, dydx1[5 * 2 + i], tolerance); // y3
ExpectClose(2 * x2[i], dydx2[5 * 2 + i], tolerance);
// clang-format on
}
}
@@ -119,14 +120,13 @@ bool TranscendentalFunctor::operator()(const double* x1,
}
void TranscendentalFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
const CostFunction& cost_function,
NumericDiffMethodType method) const {
const CostFunction& cost_function, NumericDiffMethodType method) const {
struct TestParameterBlocks {
double x1[5];
double x2[5];
};
// clang-format off
std::vector<TestParameterBlocks> kTests = {
{ { 1.0, 2.0, 3.0, 4.0, 5.0 }, // No zeros.
{ 9.0, 9.0, 5.0, 5.0, 1.0 },
@@ -147,21 +147,21 @@ void TranscendentalFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
{ 0.0, 0.0, 0.0, 0.0, 0.0 },
},
};
// clang-format on
for (int k = 0; k < kTests.size(); ++k) {
double *x1 = &(kTests[k].x1[0]);
double *x2 = &(kTests[k].x2[0]);
double *parameters[] = { x1, x2 };
double* x1 = &(kTests[k].x1[0]);
double* x2 = &(kTests[k].x2[0]);
double* parameters[] = {x1, x2};
double dydx1[10];
double dydx2[10];
double *jacobians[2] = { &dydx1[0], &dydx2[0] };
double* jacobians[2] = {&dydx1[0], &dydx2[0]};
double residuals[2];
ASSERT_TRUE(cost_function.Evaluate(&parameters[0],
&residuals[0],
&jacobians[0]));
ASSERT_TRUE(
cost_function.Evaluate(&parameters[0], &residuals[0], &jacobians[0]));
double x1x2 = 0;
for (int i = 0; i < 5; ++i) {
x1x2 += x1[i] * x2[i];
@@ -184,39 +184,37 @@ void TranscendentalFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
}
for (int i = 0; i < 5; ++i) {
// clang-format off
ExpectClose( x2[i] * cos(x1x2), dydx1[5 * 0 + i], tolerance);
ExpectClose( x1[i] * cos(x1x2), dydx2[5 * 0 + i], tolerance);
ExpectClose(-x2[i] * exp(-x1x2 / 10.) / 10., dydx1[5 * 1 + i], tolerance);
ExpectClose(-x1[i] * exp(-x1x2 / 10.) / 10., dydx2[5 * 1 + i], tolerance);
// clang-format on
}
}
}
bool ExponentialFunctor::operator()(const double* x1,
double* residuals) const {
bool ExponentialFunctor::operator()(const double* x1, double* residuals) const {
residuals[0] = exp(x1[0]);
return true;
}
void ExponentialFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
const CostFunction& cost_function) const {
// Evaluating the functor at specific points for testing.
std::vector<double> kTests = { 1.0, 2.0, 3.0, 4.0, 5.0 };
std::vector<double> kTests = {1.0, 2.0, 3.0, 4.0, 5.0};
// Minimal tolerance w.r.t. the cost function and the tests.
const double kTolerance = 2e-14;
for (int k = 0; k < kTests.size(); ++k) {
double *parameters[] = { &kTests[k] };
double* parameters[] = {&kTests[k]};
double dydx;
double *jacobians[1] = { &dydx };
double* jacobians[1] = {&dydx};
double residual;
ASSERT_TRUE(cost_function.Evaluate(&parameters[0],
&residual,
&jacobians[0]));
ASSERT_TRUE(
cost_function.Evaluate(&parameters[0], &residual, &jacobians[0]));
double expected_result = exp(kTests[k]);
@@ -228,10 +226,9 @@ void ExponentialFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
}
}
bool RandomizedFunctor::operator()(const double* x1,
double* residuals) const {
double random_value = static_cast<double>(rand()) /
static_cast<double>(RAND_MAX);
bool RandomizedFunctor::operator()(const double* x1, double* residuals) const {
double random_value =
static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
// Normalize noise to [-factor, factor].
random_value *= 2.0;
@@ -244,7 +241,7 @@ bool RandomizedFunctor::operator()(const double* x1,
void RandomizedFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
const CostFunction& cost_function) const {
std::vector<double> kTests = { 0.0, 1.0, 3.0, 4.0, 50.0 };
std::vector<double> kTests = {0.0, 1.0, 3.0, 4.0, 50.0};
const double kTolerance = 2e-4;
@@ -252,14 +249,13 @@ void RandomizedFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect(
srand(random_seed_);
for (int k = 0; k < kTests.size(); ++k) {
double *parameters[] = { &kTests[k] };
double* parameters[] = {&kTests[k]};
double dydx;
double *jacobians[1] = { &dydx };
double* jacobians[1] = {&dydx};
double residual;
ASSERT_TRUE(cost_function.Evaluate(&parameters[0],
&residual,
&jacobians[0]));
ASSERT_TRUE(
cost_function.Evaluate(&parameters[0], &residual, &jacobians[0]));
// Expect residual to be close to x^2 w.r.t. noise factor.
ExpectClose(residual, kTests[k] * kTests[k], noise_factor_);