Add DynamicCostFunctionToFunctor.

This adds a new wrapper class called DynamicCostFunctionToFunctor
that closes a gap in the current API: the existing
CostFunctionToFunctor can only be used with a SizedCostFunction, where
the number and sizes of all parameter vectors are known at compile-time.
The DynamicCostFunctionToFunctor allows you to wrap a generic
CostFunction into a templated functor which can then be used in a
DynamicAutoDiffCostFunction.

Also updates the existing CostFunctionToFunctor class to internally use
DynamicCostFunctionToFunctor.

Change-Id: I088adc3271c58d2519126c27037c3576965a36d6
This commit is contained in:
David Gossow
2015-06-16 14:10:56 -07:00
parent 6d1dedad50
commit 2a1dfd2b71
4 changed files with 331 additions and 107 deletions
@@ -29,6 +29,8 @@
// Author: sameeragarwal@google.com (Sameer Agarwal)
#include "ceres/cost_function_to_functor.h"
#include "ceres/dynamic_autodiff_cost_function.h"
#include "ceres/dynamic_cost_function_to_functor.h"
#include "ceres/autodiff_cost_function.h"
#include "gtest/gtest.h"
@@ -242,6 +244,18 @@ struct TenParameterBlockFunctor {
}
};
class DynamicTwoParameterBlockFunctor {
public:
template <typename T>
bool operator()(T const* const* parameters, T* residuals) const {
for (int i = 0; i < 2; ++i) {
residuals[0] = parameters[i][0] * parameters[i][0];
residuals[1] = parameters[i][1] * parameters[i][1];
}
return true;
}
};
#define TEST_BODY(NAME) \
TEST(CostFunctionToFunctor, NAME) { \
scoped_ptr<CostFunction> cost_function( \
@@ -315,5 +329,23 @@ TEST(CostFunctionToFunctor, DynamicNumberOfResiduals) {
ExpectCostFunctionsAreEqual(*cost_function, *actual_cost_function);
}
TEST(CostFunctionToFunctor, DynamicCostFunctionToFunctor) {
DynamicAutoDiffCostFunction<DynamicTwoParameterBlockFunctor>*
actual_cost_function(
new DynamicAutoDiffCostFunction<DynamicTwoParameterBlockFunctor>(
new DynamicTwoParameterBlockFunctor));
actual_cost_function->AddParameterBlock(2);
actual_cost_function->AddParameterBlock(2);
actual_cost_function->SetNumResiduals(2);
DynamicAutoDiffCostFunction<DynamicCostFunctionToFunctor> cost_function(
new DynamicCostFunctionToFunctor(actual_cost_function));
cost_function.AddParameterBlock(2);
cost_function.AddParameterBlock(2);
cost_function.SetNumResiduals(2);
ExpectCostFunctionsAreEqual(cost_function, *actual_cost_function);
}
} // namespace internal
} // namespace ceres