CostFunctionToFunctor allows dynamic number of residuals.
The code itself was perfectly capable of handling residuals, but there
was an overly strict runtime check that had to be removed.
Thanks to Domink Reitzle for reporting this.
Change-Id: I6a6d000a7c5203dd5945a61b4caeda1b8aeb09c9
diff --git a/include/ceres/cost_function_to_functor.h b/include/ceres/cost_function_to_functor.h
index 0d01f77..b4a516e 100644
--- a/include/ceres/cost_function_to_functor.h
+++ b/include/ceres/cost_function_to_functor.h
@@ -107,9 +107,7 @@
explicit CostFunctionToFunctor(CostFunction* cost_function)
: cost_function_(cost_function) {
CHECK_NOTNULL(cost_function);
-
- CHECK_GE(kNumResiduals, 0);
- CHECK_EQ(cost_function->num_residuals(), kNumResiduals);
+ CHECK(kNumResiduals > 0 || kNumResiduals == DYNAMIC);
// This block breaks the 80 column rule to keep it somewhat readable.
CHECK((!N1 && !N2 && !N3 && !N4 && !N5 && !N6 && !N7 && !N8 && !N9) ||
diff --git a/internal/ceres/cost_function_to_functor_test.cc b/internal/ceres/cost_function_to_functor_test.cc
index fd828ce..f758efe 100644
--- a/internal/ceres/cost_function_to_functor_test.cc
+++ b/internal/ceres/cost_function_to_functor_test.cc
@@ -300,6 +300,19 @@
#undef TEST_BODY
+TEST(CostFunctionToFunctor, DynamicNumberOfResiduals) {
+ scoped_ptr<CostFunction> cost_function(
+ new AutoDiffCostFunction<
+ CostFunctionToFunctor<ceres::DYNAMIC, 2, 2 >, ceres::DYNAMIC, 2, 2>(
+ new CostFunctionToFunctor<ceres::DYNAMIC, 2, 2 >(
+ new AutoDiffCostFunction<TwoParameterBlockFunctor, 2, 2, 2 >(
+ new TwoParameterBlockFunctor)), 2));
+
+ scoped_ptr<CostFunction> actual_cost_function(
+ new AutoDiffCostFunction<TwoParameterBlockFunctor, 2, 2, 2 >(
+ new TwoParameterBlockFunctor));
+ ExpectCostFunctionsAreEqual(*cost_function, *actual_cost_function);
+}
} // namespace internal
} // namespace ceres