谷神星求解器:残差函子使用的可变对象是否良好实践?还有什么其他选择

Ceres-Solver: is a mutable used by a residual functor good pratice? What are other alternatives?

本文关键字:是否 对象 其他 选择 什么      更新时间:2023-10-16

据我了解,Ceres的接口要求将每个残差定义为一个函子,其中operator()是一个const成员函数。以下是我感兴趣的示例:

class some_residual
{
   public:
    template<typename type>
    bool operator()(const type* const some_params, type* residual) const;
    Eigen::MatrixXd m_M;/*The explanation follows. Nevermind its type, all
                          that matters is that it is not a raw buffer*/
};

现在,我处于一种特殊情况下,我需要"帮助器"矩阵m_M,我想在operator()内部使用。有几个选项,例如,我可以将其声明为 mutable Eigen::MatrixXd m_M; 或将其更改为std::shared_ptr<Eigen::MatrixXd> m_pM;并从内部operator()更新*mP(或者类似地,我可以使用引用(。作为另一种选择,我可以将此矩阵的数据作为原始 C 指针传递给 operator(),并在Ceres优化中修复它们。

宁愿尽可能避免原始指针,并且我倾向于认为使用mutable是最好的解决方案。一般来说,这种做法是好的做法还是坏做法,特别是将其与Ceres一起使用是否安全?我还有哪些其他选择?

可变是一个

坏主意。

调用 operator(( 是 const 的原因是,如果您决定在多个 CostFunctions 中使用相同的函子,或者在多个残差块中使用相同的 CostFunction,那么如果 Ceres 使用多个线程来评估残差/雅可比函数,则可能会遇到竞争条件。