-1

How would you build a Point class on top of an Eigen::Vector2f? I want to do something like...

Point p0(1, 2);
Point p1(3, 4);
p0.x += p0.y;
p1 += p0;
cout << p1.transpose() << endl;

I basically need to name the elements of the vector so I can refer to them as x, y, but I still want all the linear algebra from Eigen to work. Kind of like a union in C. Do I have to build a wrapper around the Eigen::Vector2f that forwards all the math operators to Eigen?

This is just a simple example of what I need. In reality, I have long state vectors composed of named variables that I want to pack into Eigen::VectorXd's and do linear algebra on. I would like a class called StateVector that looks like an Eigen::Vector when I do math to it, but where I can manipulate the member variables by name.

yassadi
  • 524
  • 1
  • 9
  • 20
J. Ford
  • 15
  • 3
  • 1
    Is writing `p0.x() += p0.y();` so bad? – chtz Apr 21 '18 at 07:48
  • I don't mind p0.x(), but I also need things like p0.kappa() or p0.theta(). I want to store a vector containing [x, y, theta, kappa], access the member variables by name, have the ability to modify the layout of the vector (by adding or rearranging the variables), and still be able to operate on it as if it were a normal Eigen::Vector. – J. Ford Apr 22 '18 at 06:23

1 Answers1

0

Would it be ok to decouple both usages by making your state vector type return a Eigen::Map when doing linear-algebra:

State s;
s.theta = ...;
s.phi = ...;
auto v = s.vec(); // returns Map<Vector2f>(&s);
// use v just like a Vector2f:
v += 0.2*v;

v, is an alias to s, so the above is same as:

s.theta += O.2*s.theta;
s.phi   += O.2*s.phi;

Otherwise, you can use the plugin mechanism to add phi(), theta() methods returning const/non-const references just as the current x()/y() methods.

ggael
  • 28,425
  • 2
  • 65
  • 71