This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Inheriting from Matrix and using Map

Tags: None
(comma "," separated)
jsommerville
Registered Member
Posts
1
Karma
0
I have a class that inherits from Matrix. We'll call it "MyVectorType," which is a specialization of a vector that has certain, specific behaviors. I have followed the instructions
(http://eigen.tuxfamily.org/dox/TopicCustomizing_InheritingMatrix.html) and that works. However, now I would like to create a
Map<MyVectorType> so that I can use an external memory block as a MyVectorType. This is a disaster. Numerous compiler errors are generated complaining about various undefined things. For instance:

Core/Map.h(20): error C2504: 'Eigen::internal::traits<PlainObjectType>' : base class undefined
Core/Map.h(30): error C2027: use of undefined type 'Eigen::internal::traits<PlainObjectType>'
Core/Map.h(30): error C2065: 'Flags' : undeclared identifier


I could go on, but the basic question is, what is necessary to make a Matrix-derived class that can be used in Map?

To prevent the detour, yes, I'm really sure that I don't want to modify all of the matrices and vectors by using EIGEN_MATRIXBASE_PLUGIN.

A minimalist example follows:

Code: Select all
#include <Eigen/Core>
#include <iostream>

class MyVectorType : public Eigen::Matrix < double, 3, 1 >
{
   public:
      typedef Eigen::Matrix<double, 3, 1> super;

      MyVectorType(void) : super() {}
      // This constructor allows you to construct MyVectorType from Eigen expressions
      template<typename OtherDerived>
      MyVectorType(const Eigen::MatrixBase<OtherDerived>& other)
         : super(other)
      { }
      // This method allows you to assign Eigen expressions to MyVectorType
      template<typename OtherDerived>
      MyVectorType& operator=(const Eigen::MatrixBase <OtherDerived>& other)
      {
         this->super::operator=(other);
         return *this;
      }

      double special_function() { return 2 * sum(); }
};

int main()
{
   // The following three lines work
   Eigen::Matrix<double, 3, 1> base = Eigen::Matrix<double, 3, 1>::Ones(3);
   MyVectorType v(base);
   std::cout << v << std::endl;

   // These lines cause compiler errors
   double data[3] = { 0.0, 1.1, 2.2 };
   Eigen::Map<MyVectorType> m(data);
   std::cout << m << std::endl;
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
This cannot work as Map<MyVectorType> cannot magically inherits the "special" behavior of MyVectorType. Moreover, please note that if vec is a MyVectorType, then "2*vec" will be a simple dense matrix expression without any of the "special" behavior. I don't know what's your goal here, but if you only want to support Map<MyVectorType> then you can specialize Map for MyVectorType, make it inherit Map<Matrix> and implement your special functions there (probably using a common internal free-function to factorize the code).


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]