const T * const指向Eigen Matrix

时间:2014-05-05 19:51:36

标签: c++ eigen

struct AscendReprojectionError {
    AscendReprojectionError(double observed_x, double observed_y)
        : observed_x(observed_x), observed_y(observed_y) {}

    template <typename T>
    bool operator()(const T* const camera,
        const T* const point,
        T* residuals) const {       

        Eigen::Matrix<T, 3, 3, Eigen::RowMajor> rot = Eigen::Map <Eigen::Matrix< T, 3, 3, Eigen::RowMajor> >(camera);

        return true;
    }

    // Factory to hide the construction of the CostFunction object from
    // the client code.
    static ceres::CostFunction* Create(const double observed_x,
        const double observed_y) {
        return (new ceres::AutoDiffCostFunction<AscendReprojectionError, 2, 9, 3>(
            new AscendReprojectionError(observed_x, observed_y)));
    }

    double observed_x;
    double observed_y;
};

如何定义包含const T * const点中9个点的特征矩阵?以上是我失败的尝试

Error   3   error C2440: '<function-style-cast>' : cannot convert from 'const JetT *const ' to 'Eigen::Map<Derived,0,Eigen::Stride<0,0>>'   C:\dev\ceres-solver-1.8.0\examples\simple_bundle_adjuster.cc    133 1   simple_bundle_adjuster
Error   2   error C2440: '<function-style-cast>' : cannot convert from 'const double *const ' to 'Eigen::Map<Derived,0,Eigen::Stride<0,0>>' C:\dev\ceres-solver-1.8.0\examples\simple_bundle_adjuster.cc    133 1   simple_bundle_adjuster

评论中的问题:

class CostFunction {
 public:
  CostFunction() : num_residuals_(0) {}

  virtual ~CostFunction() {}

再次添加其余代码后。

给出以下错误。不确定它是否与特征或束调整器有关,不使用特征矩阵对我感到高兴。

Error   2   error C2039: 'epsilon' : is not a member of 'Eigen::NumTraits<ceres::Jet<T,12>>'    c:\dev\eigen-eigen-ffa86ffb5570\eigen\src\Core\IO.h 132 1   simple_bundle_adjuster

3 个答案:

答案 0 :(得分:4)

Eigen::Map用于包装C风格的数组,以便它可以用作Eigen::Matrix。通常,这允许它甚至写入底层数组。由于您只有T const*,因此不允许写入。为了保持const-correct,你需要告诉Eigen不允许写入的映射,因为你只有T const*个指针。为此,请指定Mapconst Matrix<...>

template <typename T>
bool operator()(const T* const camera,
    const T* const point,
    T* residuals) const {

                                                          //   vvvvv
    Eigen::Matrix<T, 3, 3, Eigen::RowMajor> rot = Eigen::Map < const Eigen::Matrix< T, 3, 3, Eigen::RowMajor> >(camera);
    std::cout << rot << std::endl;                            
}

答案 1 :(得分:1)

我认为您将需要更新版本的Ceres。查看git repo中的版本。

中添加了epsilon()

https://ceres-solver.googlesource.com/ceres-solver/+/91da310aebe855e3d97f150c698221b3e4c0bce3

晚于1.8.0版本。

答案 2 :(得分:0)

operator()是否为类成员函数?你不能在课外定义它。下面的代码编译

class MyClass{
public:
template <typename T>
bool operator()(const T* const camera,
    const T* const point,
    T* residuals) const 
    {
        Eigen::Matrix<T, 3, 3, Eigen::RowMajor> rot = Eigen::Map <Eigen::Matrix< T, 3, 3,     Eigen::RowMajor> >(camera);
        std::cout << rot << std::endl;
        // must return true or false, as you declare `bool` as the return type
        return true;
    }
};

或者,您可以制作整个MyClass模板,然后从template <typename T>

中删除operator()
template <typename T> class MyClass{...};
相关问题