visualizer.cpp
Go to the documentation of this file.
00001 #include <handle_detector/visualizer.h>
00002 
00003 typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
00004 typedef visualization_msgs::MarkerArray MarkerArray;
00005 
00006 Visualizer::Visualizer(double marker_lifetime)
00007 {
00008   this->marker_lifetime = marker_lifetime;
00009 }
00010 
00011 MarkerArray Visualizer::createCylinders(const std::vector<CylindricalShell> &list, const std::string &frame)
00012 {
00013   // create and resize a marker array message
00014   MarkerArray marker_array;
00015   marker_array.markers.resize(list.size());
00016 
00017   for (std::size_t i = 0; i < list.size(); i++)
00018   {
00019     // create marker
00020     visualization_msgs::Marker marker;
00021     marker.header.frame_id = frame;
00022     marker.header.stamp = ros::Time::now();
00023 
00024     // namespace and id to give the marker a unique identification
00025     marker.ns = "cylinders_ns";
00026     marker.id = i;
00027 
00028     // how long the marker exists
00029     marker.lifetime = ros::Duration(this->marker_lifetime);
00030 
00031     // type of marker (cylinder)
00032     marker.type = visualization_msgs::Marker::CYLINDER;
00033     marker.action = visualization_msgs::Marker::ADD;
00034 
00035     // marker position
00036     marker.pose.position.x = list[i].getCentroid()(0);
00037     marker.pose.position.y = list[i].getCentroid()(1);
00038     marker.pose.position.z = list[i].getCentroid()(2);
00039 
00040     // marker rotation
00041     geometry_msgs::PoseStamped cylinder_pose_msg;
00042     Eigen::Vector3d axis = list[i].getCurvatureAxis();
00043     Eigen::Vector3d normal = list[i].getNormal();
00044     Eigen::Vector3d perp = normal.cross(axis);
00045     tf::Matrix3x3 rotation_matrix(perp(0), normal(0), axis(0), perp(1), normal(1), axis(1), perp(2), normal(2),
00046                                   axis(2));
00047     tf::Quaternion quaternion;
00048     rotation_matrix.getRotation(quaternion);
00049     tf::Stamped<tf::Transform> cylinder_tf_pose(tf::Transform(quaternion), marker.header.stamp, frame);
00050     tf::poseStampedTFToMsg(cylinder_tf_pose, cylinder_pose_msg);
00051     marker.pose.orientation = cylinder_pose_msg.pose.orientation;
00052 
00053     // marker scale
00054     marker.scale.x = list[i].getRadius() * 2; // diameter in x-direction
00055     marker.scale.y = list[i].getRadius() * 2; // diameter in y-direction
00056     marker.scale.z = list[i].getExtent(); // height
00057 
00058     // marker color and transparency
00059     marker.color.a = 0.3;
00060     marker.color.r = 0;
00061     marker.color.g = 255;
00062     marker.color.b = 255;
00063 
00064     marker_array.markers[i] = marker;
00065   }
00066 
00067   return marker_array;
00068 }
00069 
00070 MarkerArray Visualizer::createHandleNumbers(const std::vector<std::vector<CylindricalShell> > &handles,
00071                                             const std::string &frame)
00072 {
00073   // create and resize a marker array message
00074   MarkerArray marker_array;
00075   marker_array.markers.resize(handles.size() * 2);
00076 
00077   for (std::size_t i = 0; i < handles.size() * 2; i++)
00078   {
00079     visualization_msgs::Marker marker;
00080     marker.header.frame_id = frame;
00081     marker.header.stamp = ros::Time::now();
00082     marker.ns = "handle_numbers";
00083     marker.id = i;
00084     marker.lifetime = ros::Duration(this->marker_lifetime);
00085 
00086     if (i % 2 == 0)
00087     {
00088       marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
00089       marker.action = visualization_msgs::Marker::ADD;
00090 
00091       // height of an upper-case "A"
00092       marker.scale.z = 0.03;
00093 
00094       // marker position
00095       marker.pose.position.x = handles[i / 2][0].getCentroid()(0);
00096       marker.pose.position.y = handles[i / 2][0].getCentroid()(1);
00097       marker.pose.position.z = handles[i / 2][0].getCentroid()(2) - 0.06;
00098 
00099       // marker color and transparency
00100       marker.color.a = 1.0;
00101       marker.color.r = 1.0;
00102       marker.color.g = 0.0;
00103       marker.color.b = 0.0;
00104 
00105       // displayed text
00106       marker.text = boost::lexical_cast < std::string > ((i / 2) + 1);
00107     }
00108     else
00109     {
00110       // type of marker (cylinder)
00111       marker.type = visualization_msgs::Marker::CUBE;
00112       marker.action = visualization_msgs::Marker::ADD;
00113 
00114       // height of an upper-case "A"
00115       marker.scale.x = 0.03;
00116       marker.scale.y = 0.03;
00117       marker.scale.z = 0.001;
00118 
00119       // marker position
00120       marker.pose.position.x = marker_array.markers[i - 1].pose.position.x;
00121       marker.pose.position.y = marker_array.markers[i - 1].pose.position.y;
00122       marker.pose.position.z = marker_array.markers[i - 1].pose.position.z + 0.015;
00123 
00124       // marker color and transparency
00125       marker.color.a = 1.0;
00126       marker.color.r = 1.0;
00127       marker.color.g = 1.0;
00128       marker.color.b = 1.0;
00129     }
00130     marker_array.markers[i] = marker;
00131   }
00132 
00133   return marker_array;
00134 }
00135 
00136 void Visualizer::createHandles(const std::vector<std::vector<CylindricalShell> > &handles, const std::string &frame,
00137                                std::vector<MarkerArray> &marker_arrays, MarkerArray &all_handle_markers)
00138 {
00139   marker_arrays.resize(handles.size());
00140   std::vector<CylindricalShell> handle_shells;
00141   int k = 0;
00142 
00143   for (std::size_t i = 0; i < handles.size(); i++)
00144   {
00145     marker_arrays[i] = this->createCylinders(handles[i], frame);
00146     for (int j = 0; j < handles[i].size(); j++)
00147       handle_shells.push_back(handles[i][j]);
00148   }
00149 
00150   all_handle_markers = this->createCylinders(handle_shells, frame);
00151 }


handle_detector
Author(s):
autogenerated on Fri Aug 28 2015 10:59:15