Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Eigen Vector Matrix< float, 2, 1 >

Tags:

c++

eigen

I am iterating through a vector, which consists of Vector

Matrix<float, 2, 1>

for(auto it = uvVertices.begin(); it != uvVertices.end(); ++it) {
    std::cout << *it;  
}

this gives an output like: which works

0.123120.212354

which is correct, how can i access only the first or the second component? So that i get

0.12312

http://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html here is a reference but i could not figur eit out.

like image 995
user1767754 Avatar asked Oct 21 '25 19:10

user1767754


1 Answers

If I understand correctly... You can just dereference the iterator to a temporary reference inside the loop for convenience and access coefficients inside like with any Eigen object:

for(auto it = uvVertices.begin(); it != uvVertices.end(); ++it) {
    Matrix<float, 2, 1>& v = *it;
    //auto& v = *it; // should also work
    std::cout << v(0,0);  
    std::cout << v(1,0);
}

You could also use range-for:

for(auto& v  : uvVertices) {
    std::cout << v(0,0);  
    std::cout << v(1,0);
}

I would also consider using Eigen::Vector type for a vector.

like image 175
luk32 Avatar answered Oct 23 '25 08:10

luk32