-
目录
-
ceres中仿函数的使用
https://www.cnblogs.com/gary-guo/p/9767777.html
//构建代价函数结构体,abc为待优化参数,residual为残差
struct CURVE_FITTING_COST
{
CURVE_FITTING_COST(double x, double y): _x(x), _y(y){}
template<typename T>
bool operator()(const T* const abc, T* residual) const
{
residual[0] = _y - ceres::exp(abc[0] * _x * _x + abc[1] * _x + abc[2]);
return true;
}
const double _x, _y;
};
ceres::Problem problem;
for(int i = 0; i < 1000; i++)
{
problem.AddResidualBlock(
new ceres::AutoDiffCostFunction<CURVE_FITTING_COST, 1, 3>(
new CURVE_FITTING_COST(x_data[i], y_data[i])),
nullptr,
abc
);
}
//“1”是残差的维度,“3”是输入参数的维度
// Add a residual block by providing a vector of parameter blocks.
ResidualBlockId AddResidualBlock(
CostFunction* cost_function,
LossFunction* loss_function,
const std::vector<double*>& parameter_blocks);
仿函数是声明了一个类对象的结构体后,通过重载()函数,使得类实例化后的对象能像函数一样使用。
new CURVE_FITTING_COST(x_data[i], y_data[i])进行实例化对象,然后在AddResidualBlock函数中进行括号重载函数的应用。
emplace_back
std::move
将一个左值强制转化为右值引用,继而可以通过右值引用使用该值,以用于移动语义。从实现上讲,std::move基本等同于一个类型转换:static_cast<T&&>(lvalue)