我写了一个这样的函子:
class Functor
{
...
static CostFunction* create()
{
return new AutoDiffCostFunction<Functor, N_RES, N_PARAMS>(new Functor());
}
...
}
我这样称呼它:
std::vector<double> params(N_PARAMS, 0);
CostFunction* costFunc = Functor::create();
problem.AddResidualBlock(costFunc, nullptr, params.data());
奇怪的是,这在 时不起作用N_PARAMS=236
,但适用于其他类似6
,237
(我试过)。错误消息是段错误。而且我之前用过Ceres Solver,从来没有遇到过这样的问题。
Ceres Solver中的参数数量有限制吗?
===========================================更新======== =========== 我这样写:
class ReprojError {
public:
ReprojError(
const std::vector<std::vector<Eigen::Vector3d>>& pt2d,
const std::vector<Eigen::Matrix<double, 3, 4>>& vmProjs) :
m_pt2d(pt2d), m_vmProjs(vmProjs) { }
template<typename _Tp>
bool operator () (const _Tp* const pts, _Tp* aResiduals) const
{
for(int i = 0; i < 24; ++i)
{
std::cout << "?" << std::endl;
if(m_pt2d[i].empty())
{
for(int j = 0; j < 276; ++j)
{
aResiduals[i * 276 * 2 + j * 2] = _Tp(0);
aResiduals[i * 276 * 2 + j * 2 + 1] = _Tp(0);
}
}
else
{
auto proj = m_vmProjs[i];
for(int j = 0; j < 276; ++j)
{
auto X = pts[j * 3];
auto Y = pts[j * 3 + 1];
auto Z = pts[j * 3 + 2];
auto newX = (_Tp)proj(0, 0) * X + (_Tp)proj(0, 1) * Y + (_Tp)proj(0, 2) * Z + (_Tp)proj(0, 3);
auto newY = (_Tp)proj(1, 0) * X + (_Tp)proj(1, 1) * Y + (_Tp)proj(1, 2) * Z + (_Tp)proj(1, 3);
auto newZ = (_Tp)proj(2, 0) * X + (_Tp)proj(2, 1) * Y + (_Tp)proj(2, 2) * Z + (_Tp)proj(2, 3);
aResiduals[i * 276 * 2 + j * 2] = newX / newZ - m_pt2d[i][j](0);
aResiduals[i * 276 * 2 + j * 2 + 1] = newY / newZ - m_pt2d[i][j](1);
}
}
}
return true;
}
static ceres::CostFunction* create(
const std::vector<std::vector<Eigen::Vector3d>>& pt2d,
const std::vector<Eigen::Matrix<double, 3, 4>>& vmProjs)
{
return (new ceres::AutoDiffCostFunction<ReprojError, 24 * 2 * 276, 276 * 3>(
new ReprojError(pt2d, vmProjs)));
}
private:
const std::vector<std::vector<Eigen::Vector3d>>& m_pt2d;
const std::vector<Eigen::Matrix<double, 3, 4>>& m_vmProjs;
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
并定义了:
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Eigen::Vector3d)
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Eigen::Matrix<double, 3, 4>)
和 '?' 没有输出,这意味着 operator() 没有被执行。