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

Mapping std::vector<float> to std::vector< Vector3f >

Tags: None
(comma "," separated)
linello
Registered Member
Posts
56
Karma
0
OS
What is in Eigen the best, fastest and memory conservative to map a vector of floats with the following data layout:

Code: Select all
x1,y1,z1,x2,y2,z2,...,xN,yN,zN


to a
Code: Select all
std::vector<Eigen::Vector3f>
?
where obviously every Vector3f contains the 3 elements x,y,z?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
That's not possible, and the best is to map it as a Matrix<float,3,Dynamic,ColMajor> so that you can exploit all the poser of Eigen:
Code: Select all
Map<Matrix<float,3,Dynamic,ColMajor> > points(&vec.at(0), 3, N);
points.col(i) -> i-th point
points.rowwise().mean() -> gives you the centroid
(points*weights)/weights.sum() -> gives you a weighted centroid
points.colswise().normalize() -> normalize each vector
Affine3d A = ..;
A * points -> apply an affine transformation to all samples
etc.

As you see, no more loops!

If you want something like a std::vector<Vector3f>, you can cast '&vec.at(0)' to a Vector3f* pointer, and if you need the same API (.begin(), .end(), .size(), etc.) then you to write your own wrapper.
linello
Registered Member
Posts
56
Karma
0
OS
Cool thanks, this is what I was looking for. I like to avoid loops and exploit vectorization when possible! In particular since the vector of float are vertices to be sent to GPU I can now simply apply eigen operations to the vertices and compare the GLSL shader results with CPU results 8)


Bookmarks



Who is online

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