I am trying to visualize a cylinder that I fit to a point cloud using the PCL RANSAC functions. When I fit the model I am given a pcl::ModelCoefficients
object and this has the following coefficients point_on_axis, axis_direction, cylinder_radius_R, see documentation here.
As I understand it, the axis direction values (values 3, 4 and 5 of the coefficient object values member) are the x,y and z components of the cylinder axis vector, respectively. To display this vector in RVIZ using a marker object, I need to convert this vector to a quarternion. I have been doing this using the following c++ code:
//Convert axis vector to quarternion format
double axis_pitch = atan2(coefficients_cylinder.values[5],coefficients_cylinder.values[4]);
double axis_roll = atan2(coefficients_cylinder.values[3],coefficients_cylinder.values[5]);
double axis_yaw = atan2(coefficients_cylinder.values[3],coefficients_cylinder.values[4]);
tf2::Quaternion axis_quarternion;
axis_quarternion.setRPY( axis_roll, axis_pitch, axis_yaw );
axis_quarternion.normalize();
However, whenever I view the cylinder markers overlayed on the original point cloud they have the wrong orientation.
What is causing this? Am I missing a step in the conversion or is my approach completely off?
Thanks for the help!
EDIT:
I found the following code to work quite well:
//Convert axis vector to quarternion format
double axis_roll = atan2(coefficients_cylinder->values[5],coefficients_cylinder->values[4]);
double axis_pitch = -1.0 * atan2(coefficients_cylinder->values[5],coefficients_cylinder->values[3]);
double axis_yaw = atan2(coefficients_cylinder->values[4],coefficients_cylinder->values[3]);
tf2::Quaternion axis_quarternion;
axis_quarternion.setRPY( 0.0, -0.5*M_PI + axis_pitch, axis_yaw);
// axis_quarternion.setRPY( axis_roll, axis_pitch, axis_yaw);
axis_quarternion.normalize();
I had made some incorrect assumptions on the axis directions (see mgrova's answer) and not taken into account that by default the rviz cylinder marker is aligned with the z-axis.