00001 #include <agile_grasp/plot.h>
00002
00003
00004 void Plot::plotHands(const std::vector<GraspHypothesis>& hand_list,
00005 const std::vector<GraspHypothesis>& antipodal_hand_list, const PointCloud::Ptr& cloud, std::string str,
00006 bool use_grasp_bottom)
00007 {
00008 PointCloudNormal::Ptr hands_cloud = createNormalsCloud(hand_list, false, false);
00009 PointCloudNormal::Ptr antipodal_hands_cloud = createNormalsCloud(antipodal_hand_list, true,
00010 false);
00011 plotHandsHelper(hands_cloud, antipodal_hands_cloud, cloud, str, use_grasp_bottom);
00012 }
00013
00014
00015 void Plot::plotHands(const std::vector<GraspHypothesis>& hand_list, const PointCloud::Ptr& cloud,
00016 std::string str, bool use_grasp_bottom)
00017 {
00018 PointCloudNormal::Ptr hands_cloud = createNormalsCloud(hand_list, false, false);
00019 PointCloudNormal::Ptr antipodal_hands_cloud = createNormalsCloud(hand_list, true, false);
00020 plotHandsHelper(hands_cloud, antipodal_hands_cloud, cloud, str, use_grasp_bottom);
00021 }
00022
00023
00024 void Plot::plotSamples(const std::vector<int>& index_list, const PointCloud::Ptr& cloud)
00025 {
00026 PointCloud::Ptr samples_cloud(new PointCloud);
00027 for (int i = 0; i < index_list.size(); i++)
00028 samples_cloud->points.push_back(cloud->points[index_list[i]]);
00029
00030 boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer = createViewer("Samples");
00031 viewer->addPointCloud<pcl::PointXYZ>(cloud, "registered point cloud");
00032 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
00033 "registered point cloud");
00034 viewer->addPointCloud<pcl::PointXYZ>(samples_cloud, "samples cloud");
00035 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "samples cloud");
00036 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 0.0, 0.0,
00037 "samples cloud");
00038
00039 runViewer(viewer);
00040 }
00041
00042
00043 void Plot::plotLocalAxes(const std::vector<Quadric>& quadric_list, const PointCloud::Ptr& cloud)
00044 {
00045 boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer = createViewer("Local Axes");
00046 viewer->addPointCloud<pcl::PointXYZ>(cloud, "registered point cloud");
00047 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
00048 "registered point cloud");
00049
00050 for (int i = 0; i < quadric_list.size(); i++)
00051 quadric_list[i].plotAxes((void*) &viewer, i);
00052
00053 runViewer(viewer);
00054 }
00055
00056
00057 void Plot::plotCameraSource(const Eigen::VectorXi& pts_cam_source_in, const PointCloud::Ptr& cloud)
00058 {
00059 PointCloud::Ptr left_cloud(new PointCloud);
00060 PointCloud::Ptr right_cloud(new PointCloud);
00061
00062 for (int i = 0; i < pts_cam_source_in.size(); i++)
00063 {
00064 if (pts_cam_source_in(i) == 0)
00065 left_cloud->points.push_back(cloud->points[i]);
00066 else if (pts_cam_source_in(i) == 1)
00067 right_cloud->points.push_back(cloud->points[i]);
00068 }
00069
00070 boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer = createViewer("Camera Sources");
00071 viewer->addPointCloud<pcl::PointXYZ>(left_cloud, "left point cloud");
00072 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
00073 "left point cloud");
00074 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 0.0, 0.0,
00075 "left point cloud");
00076 viewer->addPointCloud<pcl::PointXYZ>(right_cloud, "right point cloud");
00077 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
00078 "right point cloud");
00079 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0.0, 1.0, 0.0,
00080 "right point cloud");
00081 runViewer(viewer);
00082 }
00083
00084
00085 PointCloudNormal::Ptr Plot::createNormalsCloud(
00086 const std::vector<GraspHypothesis>& hand_list, bool plots_only_antipodal, bool plots_grasp_bottom)
00087 {
00088 PointCloudNormal::Ptr cloud(new PointCloudNormal);
00089
00090 for (int i = 0; i < hand_list.size(); i++)
00091 {
00092 Eigen::Matrix3Xd grasp_surface = hand_list[i].getGraspSurface();
00093 Eigen::Matrix3Xd grasp_bottom = hand_list[i].getGraspBottom();
00094 Eigen::Matrix3Xd hand_approach = hand_list[i].getApproach();
00095
00096 if (!plots_only_antipodal || (plots_only_antipodal && hand_list[i].isFullAntipodal()))
00097 {
00098 pcl::PointNormal p;
00099 if (!plots_grasp_bottom)
00100 {
00101 p.x = grasp_surface(0);
00102 p.y = grasp_surface(1);
00103 p.z = grasp_surface(2);
00104 }
00105 else
00106 {
00107 p.x = grasp_bottom(0);
00108 p.y = grasp_bottom(1);
00109 p.z = grasp_bottom(2);
00110 }
00111 p.normal[0] = -hand_approach(0);
00112 p.normal[1] = -hand_approach(1);
00113 p.normal[2] = -hand_approach(2);
00114 cloud->points.push_back(p);
00115 }
00116 }
00117
00118 return cloud;
00119 }
00120
00121
00122 void Plot::addCloudNormalsToViewer(boost::shared_ptr<pcl::visualization::PCLVisualizer>& viewer,
00123 const PointCloudNormal::Ptr& cloud, double line_width, double* color_cloud,
00124 double* color_normals, const std::string& cloud_name, const std::string& normals_name)
00125 {
00126 viewer->addPointCloud<pcl::PointNormal>(cloud, cloud_name);
00127 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, color_cloud[0],
00128 color_cloud[1], color_cloud[2], cloud_name);
00129 viewer->addPointCloudNormals<pcl::PointNormal>(cloud, 1, 0.04, normals_name);
00130 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_LINE_WIDTH, line_width,
00131 normals_name);
00132 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, color_normals[0],
00133 color_normals[1], color_normals[2], normals_name);
00134 }
00135
00136
00137 void Plot::plotHandsHelper(const PointCloudNormal::Ptr& hands_cloud,
00138 const PointCloudNormal::Ptr& antipodal_hands_cloud, const PointCloud::Ptr& cloud,
00139 std::string str, bool use_grasp_bottom)
00140 {
00141 std::cout << "Drawing " << hands_cloud->size() << " grasps of which " << antipodal_hands_cloud->size()
00142 << " are antipodal grasps.\n";
00143
00144 std::string title = "Hands: " + str;
00145 boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer = createViewer(title);
00146 viewer->addPointCloud<pcl::PointXYZ>(cloud, "registered point cloud");
00147 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
00148 "registered point cloud");
00149 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0.0, 0.8, 0.0,
00150 "registered point cloud");
00151
00152 double green[3] = { 0.0, 1.0, 0.0 };
00153 double cyan[3] = { 0.0, 0.4, 0.8 };
00154 addCloudNormalsToViewer(viewer, hands_cloud, 2, green, cyan, std::string("hands"),
00155 std::string("approaches"));
00156
00157 if (antipodal_hands_cloud->size() > 0)
00158 {
00159 double red[3] = { 1.0, 0.0, 0.0 };
00160 addCloudNormalsToViewer(viewer, antipodal_hands_cloud, 2, green, red, std::string("antipodal_hands"),
00161 std::string("antipodal_approaches"));
00162 }
00163
00164 runViewer(viewer);
00165 }
00166
00167
00168 void Plot::runViewer(boost::shared_ptr<pcl::visualization::PCLVisualizer>& viewer)
00169 {
00170
00171 viewer->initCameraParameters();
00172 viewer->setPosition(0, 0);
00173 viewer->setSize(640, 480);
00174
00175 while (!viewer->wasStopped())
00176 {
00177 viewer->spinOnce(100);
00178 boost::this_thread::sleep(boost::posix_time::microseconds(100000));
00179 }
00180
00181 viewer->close();
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 }
00192
00193
00194 boost::shared_ptr<pcl::visualization::PCLVisualizer> Plot::createViewer(std::string title)
00195 {
00196 boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer(title));
00197 viewer->setBackgroundColor(1, 1, 1);
00198 return viewer;
00199 }