Support varying numbers of residuals in autodiff.

This commit modifies the only function in autodiff that takes a
templated number of outputs (i.e. residuals) and makes that
template parameter a normal parameter. With that change, it
is a trivial matter to support a dynamic number of residuals.

The API for dynamic residuals is to pass a fake number of
residuals as the second template argument to
AutoDiffCostFunction, and to pass the real number of
parameters as a second constructor argument.
This commit is contained in:
Keir Mierle
2012-05-09 07:38:07 -07:00
parent da3e0563cc
commit fdeb5772cc
6 changed files with 142 additions and 52 deletions
+10 -4
View File
@@ -30,11 +30,16 @@
//
// A convenience class for cost functions which are statically sized.
// Compared to the dynamically-sized base class, this reduces boilerplate.
//
// The kNumResiduals template parameter can be a constant such as 2 or 5, or it
// can be ceres::DYNAMIC. If kNumResiduals is ceres::DYNAMIC, then subclasses
// are responsible for calling set_num_residuals() at runtime.
#ifndef CERES_PUBLIC_SIZED_COST_FUNCTION_H_
#define CERES_PUBLIC_SIZED_COST_FUNCTION_H_
#include <glog/logging.h>
#include "ceres/types.h"
#include "ceres/cost_function.h"
namespace ceres {
@@ -45,11 +50,12 @@ class SizedCostFunction : public CostFunction {
public:
SizedCostFunction() {
// Sanity checking.
DCHECK_GT(kNumResiduals, 0) << "Cost functions must have at least "
<< "one residual block.";
DCHECK_GT(N0, 0)
CHECK(kNumResiduals > 0 || kNumResiduals == DYNAMIC)
<< "Cost functions must have at least one residual block.";
CHECK_GT(N0, 0)
<< "Cost functions must have at least one parameter block.";
DCHECK((!N1 && !N2 && !N3 && !N4 && !N5) ||
CHECK((!N1 && !N2 && !N3 && !N4 && !N5) ||
((N1 > 0) && !N2 && !N3 && !N4 && !N5) ||
((N1 > 0) && (N2 > 0) && !N3 && !N4 && !N5) ||
((N1 > 0) && (N2 > 0) && (N3 > 0) && !N4 && !N5) ||