Search code examples
c++mathrotationeigenimage-rotation

Wrong Values in the calculation of normalVector and z-coordinate rotation


I have three points-> point1, point2..

auto it = vectorList.begin();
Eigen::Vector3d point1 = *it++;
Eigen::Vector3d point2 = *it++;
Eigen::Vector3d point3 = *it;

I am using them for creating a plane, and taking the normal vector of it.

Eigen::Hyperplane<double, 3> plane = Eigen::Hyperplane<double, 3>::Through(point1, point2, point3);
Eigen::Vector3d normalVector = plane.normal();
normalVector = normalVector.normalized();

I am calculating the angle between the normalvector and the z-axis and using it as a rotationmatrix (for the x-axis). Now I want to get sure all values have the same z-value.

double anglezaxis;
anglezaxis = std::atan2(normalVector.x(), normalVector.z());
Eigen::AngleAxisd rotation(anglezaxis, Eigen::Vector3d::UnitX());
point1 = rotation*point1;
point2 = rotation *point2;
point3 = rotation * point3;
qDebug() << point1.z() << point2.z() << point3.z();

but the z values are completly different. Where is my thinking mistake?


Solution

  • If you want the plane to be z=constant then you need to rotate the normal vector back onto the z axis. The rotation axis for that is perpendicular to normal vector and unitZ - e.g. by cross product - not the x axis. The angle also has the opposite sign to what you currently have and is better worked out from the z direction cosine of the unit normal vector.