RainerKuemmerle / g2o

g2o: A General Framework for Graph Optimization
3.1k stars 1.11k forks source link

How to define a variable size edge in g2o #694

Closed gitouni closed 1 year ago

gitouni commented 1 year ago

Question

I have known that the latest g2o has supported variable size vertex. Has g2o supported variable sized edge? For example, can I defeine a variable size edge with g2o::VectorX type?

Environment

System CMake g++ g2o C++ Standard
Ubuntu 20.04 3.25.0 9.4.0 2023 C++ 17

Trial


class IBATestEdge: public g2o::BaseUnaryEdge<2, g2o::Vector2, VertexSim3>
{
public:
    IBATestEdge(const int D){
        _error.resize(D);
        _jacobianOplusXi.resize(D, 7);
 }

    template <typename T>
    bool operator()(const T* data, T* error) const{
       /**Imp**/
        return true;
    }

    virtual bool read(std::istream &in) override {return false;}
    virtual bool write(std::ostream &out) const override {return false;}

public:
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
    G2O_MAKE_AUTO_AD_FUNCTIONS 
};

Error (Thrown Runtime)

iba_calib_plane: /usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:312: void Eigen::PlainObjectBase<Derived>::resize(Eigen::Index) [with Derived = Eigen::Matrix<double, 2, 1>; Eigen::Index = long int]: Assertion `((SizeAtCompileTime == Dynamic && (MaxSizeAtCompileTime==Dynamic || size<=MaxSizeAtCompileTime)) || SizeAtCompileTime == size) && size>=0' failed.
Segment Fault

Other Trials

I also tried to change the first template arg of Edge Class to -1 but g++ throwed an error during compiling.

/usr/local/include/g2o/core/auto_differentiation.h:171:35: error: static assertion failed: Dynamically sized edges are not supported
  171 |     static_assert(Edge::Dimension > 0,

I appreciate your reply.

RainerKuemmerle commented 1 year ago

So far for auto differentiation I only had the chance to implement it for statically sized error functions (edges and vertices). Hence, I put the static_assert in the code to catch those cases during compile time. In principle, g2o supports variable sized error functions/edges but not in combination with AD.

gitouni commented 1 year ago

Got it, thanks!