DynamicCostFunctionToFunctor

class DynamicCostFunctionToFunctor

DynamicCostFunctionToFunctor 提供了与 CostFunctionToFunctor 相同的功能,适用于在编译时不知道参数向量和残差的数量和大小的情况。DynamicCostFunctionToFunctor 提供的应用程序接口与 DynamicAutoDiffCostFunction 相匹配,即它提供了这种形式的模板化函数:

template<typename T>
bool operator()(T const* const* parameters, T* residuals) const;

类似于CostFunctionToFunctor 的例子,假定

class IntrinsicProjection : public CostFunction {
  public:
    IntrinsicProjection(const double* observation);
    virtual bool Evaluate(double const* const* parameters,
                          double* residuals,
                          double** jacobians) const;
};

上述 CostFunction 实现了一个点在其本地(相机)坐标系中对其图像平面的投影,计算不涉及相机外参,并将其观测点和投影点相减,计算残差。在模板化 functor 中使用此 CostFunction 如下所示:

struct CameraProjection {
  explicit CameraProjection(double* observation)
      : intrinsic_projection_(std::make_unique<IntrinsicProjection>(observation)) {
  }

  template <typename T>
  bool operator()(T const* const* parameters,
                  T* residual) const {
    const T* rotation = parameters[0];
    const T* translation = parameters[1];
    const T* intrinsics = parameters[2];
    const T* point = parameters[3];

    T transformed_point[3];
    RotateAndTranslatePoint(rotation, translation, point, transformed_point);

    const T* projection_parameters[2];
    projection_parameters[0] = intrinsics;
    projection_parameters[1] = transformed_point;
    return intrinsic_projection_(projection_parameters, residual);
  }

 private:
  DynamicCostFunctionToFunctor intrinsic_projection_;
};

CostFunctionToFunctor 类似,DynamicCostFunctionToFunctor 也拥有传入构造函数的 CostFunction 的所有权。

Last updated