谷神星求解器:残差函子使用的可变对象是否良好实践?还有什么其他选择
Ceres-Solver: is a mutable used by a residual functor good pratice? What are other alternatives?
据我了解,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 使用多个线程来评估残差/雅可比函数,则可能会遇到竞争条件。
相关文章:
- 是否需要删除包含对象的"pair"?
- 是否删除在对象构造过程中创建的对象
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 在这种情况下,java对象是否可以调用本机函数
- 返回指向对象的指针的函数调用是否为 prvalue?
- 具有引用成员的结构是否具有唯一的对象表示形式
- 对象初始化中是否允许指向此成员的指针?
- COM :是否可以查看是否存在对我的某个 COM 对象的进程外引用?我可以释放它吗?
- 堆分配的对象是否存在永不为空的唯一所有者?
- 在函数内创建的对象的范围 - 如果在函数外部存储和访问引用,它们是否有效?
- 是否可以使用一个类来控制 C++ 中另一个类的对象?(阿杜伊诺)
- 在对象指针上调用 Delete 是否会递归删除其动态分配的成员
- QFileSystemModel 对象是否会被删除?
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 是否可以使用分配器对象来释放另一个分配器分配的内存?
- 线程调用的函数对对象删除是否安全?
- 将对象的字节复制到数组并再次复制回来是否安全
- std::memmove在同一对象之间是否始终安全
- 类对象在 c++ 中是否具有数据类型?
- 是否可以将不可复制的成员用作使对象不可复制的替代方法?