C++ Ceres解算器:残差函子使用的变量是好的吗?还有其他选择吗?

C++ Ceres解算器:残差函子使用的变量是好的吗?还有其他选择吗?,c++,computer-vision,mutable,ceres-solver,C++,Computer Vision,Mutable,Ceres Solver,据我所知,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

据我所知,
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*/
};
class一些剩余
{
公众:
模板
布尔运算符()(常量类型*常量某些参数,类型*残差)常量;
本征::MatrixXd m_m;/*解释如下。不管它的类型是什么,都可以
重要的是它不是一个原始缓冲区*/
};
现在,在一个特殊情况下,我需要“helper”矩阵
m\m
,我想在
操作符()中使用它。有几个选项,例如,我可以将其声明为
mutable Eigen::MatrixXd m或将其更改为
std::shared_ptr m_pM
和更新
*mP
(或者类似地,我可以使用参考)。作为另一种选择,我可以将此矩阵的数据作为原始C指针传递到
操作符()
,并在
Ceres
优化中修复它们


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

可变是个坏主意


调用运算符()的原因是const,因为如果您决定在多个CostFunction中使用同一个函子,或者在多个剩余块中使用同一个CostFunction,如果Ceres使用多个线程来计算残差/雅可比数,则可能会出现竞争条件。

函子通常是轻量级对象,算法有时会利用这一点多次复制函子(例如递归算法)。在将重对象传递给此类算法之前,您需要检查实现细节。@RichardCriten,谢谢。因此在这种情况下,
std::shared_ptr
成员似乎是更好的选择。