title: Bad Orientation of Principal Axis of a Point Cloud
body: <p>I'm trying to calculate the principal axis via principal component analysis. I have a pointcloud and use for this the Point Cloud Library (pcl). Furthermore, I try to visualize the principal axis I calculated in rviz with markers. Here is the code snipped I use:</p>
  
  <pre><code>void computePrincipalAxis(const PointCloud&amp; cloud, Eigen::Vector4f&amp; centroid, Eigen::Matrix3f&amp; evecs, Eigen::Vector3f&amp; evals) {
    Eigen::Matrix3f covariance_matrix;
    pcl::computeCovarianceMatrix(cloud, centroid, covariance_matrix);
    pcl::eigen33(covariance_matrix, evecs, evals);
  }
  
  void createArrowMarker(Eigen::Vector3f&amp; vec, int id, double length) {
    visualization_msgs::Marker marker;
    marker.header.frame_id = frameId;
    marker.header.stamp = ros::Time();
    marker.id = id;
    marker.type = visualization_msgs::Marker::ARROW;
    marker.action = visualization_msgs::Marker::ADD;
    marker.pose.position.x = centroid[0];
    marker.pose.position.y = centroid[1];
    marker.pose.position.z = centroid[2];
    marker.pose.orientation.x = vec[0];
    marker.pose.orientation.y = vec[1];
    marker.pose.orientation.z = vec[2];
    marker.pose.orientation.w = 1.0;
    marker.scale.x = length;
    marker.scale.y = 0.02;
    marker.scale.z = 0.02;
    marker.color.a = 1.0;
    marker.color.r = 1.0;
    marker.color.g = 1.0;
    marker.color.b = 0.0;
    featureVis.markers.push_back(marker);
  }
  
  Eigen::Vector4f centroid;
  Eigen::Matrix3f evecs;
  Eigen::Vector3f evals;
  
  // Table is the pointcloud of the table only.
  pcl::compute3DCentroid(*table, centroid); 
  computePrincipalAxis(*table, centroid, evecs, evals);
  
  Eigen::Vector3f vec;
  
  vec &lt;&lt; evecs.col(0);
  createArrowMarker(vec, 1, evals[0]);
  
  vec &lt;&lt; evecs.col(1);
  createArrowMarker(vec, 2, evals[1]);
  
  vec &lt;&lt; evecs.col(2);
  createArrowMarker(vec, 3, evals[2]);
  
  publish();
  </code></pre>
  
  <p>This results in the following visualization:</p>
  
  <p><img src="https://i.stack.imgur.com/9pMYs.png" alt="Segmented scene with table&#39;s principal axis."></p>
  
  <p>I'm aware that the scale is not very perfect. The two longer arrows are much too long. But I'm confused about a few things:</p>
  
  <ol>
  <li>I think the small arrow should go either up, or downwards.</li>
  <li>What does the value orientation.w of the arrow's orientation mean?</li>
  </ol>
  
  <p>Do you have some hints what I did wrong?</p>