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

Multiply Vectors of Xsize as if they are X-YSize

Tags: None
(comma "," separated)
brad_c6
Registered Member
Posts
3
Karma
0
Hello,
I have two floating point vectors (Vector4f), but I would like to get the dot product of the two of them without making another instance of the two as Vector3f. I was able to make a hack to do it, but I imagine there is a much more elegant solution. Thank You ;D

Code: Select all
    Eigen::Vector4f firstVector;
    firstVector.x() = 1;
    firstVector.y() = 2;
    firstVector.z() = 3;
    firstVector.w() =4;
   
    Eigen::Vector4f secondVector;
    secondVector.x() = 5;
    secondVector.y() = 6;
    secondVector.z() = 7;
    secondVector.w() = 8;
   
    Eigen::Vector3f *firstVectorCast = (Eigen::Vector3f *) &firstVector;
    Eigen::Vector3f *secondVectorCast = (Eigen::Vector3f *) &secondVector;
   
    float resultDotProduct = firstVectorCast->dot(*secondVectorCast);
jitseniesen
Registered Member
Posts
204
Karma
2
You can get the first y entries of a vector by writing vector.head(y); if y is known at compile-time then vector.head<y>() is slightly faster. So you can write the computation in your program alternatively as
Code: Select all
float resultDotProduct = firstVector.head(3).dot(secondVector.head(3));
or
Code: Select all
float resultDotProduct = firstVector.head<3>().dot(secondVector.head<3>());


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], Yahoo [Bot]