Access Eigen:MatrixXd pointers for function taking Eigen::Vector3d of pointers

Viewed 111

Probably a very simple thing, but I am new to C++, Eigen, etc.

I have a MatrixXD with n rows. Each row holds 3 points (x,y,z) and I have a function that takes a vector3d type pointer as an input. I want to iterate over all rows n of the MatrixXd and use pass each row as a vector to my function.

I assume it is a combination of accessing the MatrixXd pointers - maybe with something like this:

 int r = mydata.rows();
 int c = mydata.cols();

     for (int i = 0; i < r; ++i)
    {
        for (int j = 0; j < c; ++j)
        {
            myObject.myfunction(&mydata(i,j));
        }
    }

and using the returned pointers to call my function on each of the rows i.e. for each iteration.

Update 1: This seems it might work. However, I need to have mydata(i,j) return pointers instead of the data.

Another problem I think I can see: In the current form, I think this is just returning the elements at i,j but I actually need to return a pointer to a Vector3D. Might data.row(i) work for that?

Update 2:

Something like this might be more what I need. Still not working though. I removed the & - which makes sense - and it's working now.

    int r = data.rows();
    int c = data.cols();

     for (int i = 0; i < r; ++i)
    {
            myObject.myFunction(data.row(i));
    }

Can you give me some idea if I am going down the right road, on how to approach this or what other details you would need to help me more?

1 Answers

Solved. The above-posted code works for me.

int r = data.rows();
    int c = data.cols();

     for (int i = 0; i < r; ++i)
    {
            myObject.myFunction(data.row(i));
    }
Related