example_nurbs_fitting_closed_curve.cpp
Go to the documentation of this file.
00001 #include <pcl/surface/on_nurbs/fitting_curve_2d_pdm.h>
00002 #include <pcl/surface/on_nurbs/fitting_curve_2d_tdm.h>
00003 #include <pcl/surface/on_nurbs/fitting_curve_2d_sdm.h>
00004 #include <pcl/surface/on_nurbs/triangulation.h>
00005 
00006 #include <pcl/point_cloud.h>
00007 #include <pcl/point_types.h>
00008 #include <pcl/io/pcd_io.h>
00009 
00010 #include <pcl/visualization/pcl_visualizer.h>
00011 
00012 pcl::visualization::PCLVisualizer viewer ("Curve Fitting PDM (red), SDM (green), TDM (blue)");
00013 
00014 void
00015 PointCloud2Vector2d (pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::on_nurbs::vector_vec2d &data)
00016 {
00017   for (unsigned i = 0; i < cloud->size (); i++)
00018   {
00019     pcl::PointXYZ &p = cloud->at (i);
00020     if (!pcl_isnan (p.x) && !pcl_isnan (p.y))
00021       data.push_back (Eigen::Vector2d (p.x, p.y));
00022   }
00023 }
00024 
00025 void
00026 VisualizeCurve (ON_NurbsCurve &curve, double r, double g, double b, bool show_cps)
00027 {
00028   pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
00029   pcl::on_nurbs::Triangulation::convertCurve2PointCloud (curve, cloud, 8);
00030 
00031   for (std::size_t i = 0; i < cloud->size () - 1; i++)
00032   {
00033     pcl::PointXYZRGB &p1 = cloud->at (i);
00034     pcl::PointXYZRGB &p2 = cloud->at (i + 1);
00035     std::ostringstream os;
00036     os << "line_" << r << "_" << g << "_" << b << "_" << i;
00037     viewer.addLine<pcl::PointXYZRGB> (p1, p2, r, g, b, os.str ());
00038   }
00039 
00040   if (show_cps)
00041   {
00042     pcl::PointCloud<pcl::PointXYZ>::Ptr cps (new pcl::PointCloud<pcl::PointXYZ>);
00043     for (int i = 0; i < curve.CVCount (); i++)
00044     {
00045       ON_3dPoint cp;
00046       curve.GetCV (i, cp);
00047 
00048       pcl::PointXYZ p;
00049       p.x = float (cp.x);
00050       p.y = float (cp.y);
00051       p.z = float (cp.z);
00052       cps->push_back (p);
00053     }
00054     pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> handler (cps, 255 * r, 255 * g, 255 * b);
00055     viewer.addPointCloud<pcl::PointXYZ> (cps, handler, "cloud_cps");
00056   }
00057 }
00058 
00059 int
00060 main (int argc, char *argv[])
00061 {
00062   std::string pcd_file;
00063 
00064   if (argc > 1)
00065   {
00066     pcd_file = argv[1];
00067   }
00068   else
00069   {
00070     printf ("\nUsage: pcl_example_nurbs_fitting_curve pcd-file \n\n");
00071     printf ("  pcd-file    point-cloud file\n");
00072     exit (0);
00073   }
00074 
00075   // #################### LOAD FILE #########################
00076   printf ("  loading %s\n", pcd_file.c_str ());
00077   pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
00078   pcl::PCLPointCloud2 cloud2;
00079 
00080   if (pcl::io::loadPCDFile (pcd_file, cloud2) == -1)
00081     throw std::runtime_error ("  PCD file not found.");
00082 
00083   fromPCLPointCloud2 (cloud2, *cloud);
00084 
00085   // convert to NURBS data structure
00086   pcl::on_nurbs::NurbsDataCurve2d data;
00087   PointCloud2Vector2d (cloud, data.interior);
00088 
00089   viewer.setSize (800, 600);
00090   viewer.addPointCloud<pcl::PointXYZ> (cloud, "cloud");
00091 
00092   // #################### CURVE PARAMETERS #########################
00093   unsigned order (3);
00094   unsigned n_control_points (20);
00095 
00096   pcl::on_nurbs::FittingCurve2dPDM::Parameter curve_params;
00097   curve_params.smoothness = 0.000001;
00098   curve_params.rScale = 1.0;
00099 
00100   // #################### CURVE FITTING #########################
00101   ON_NurbsCurve curve = pcl::on_nurbs::FittingCurve2dPDM::initNurbsCurve2D (order, data.interior, n_control_points);
00102 
00103   pcl::on_nurbs::FittingCurve2dPDM fit_pdm (&data, curve);
00104   fit_pdm.assemble (curve_params);
00105   fit_pdm.solve ();
00106 
00107   pcl::on_nurbs::FittingCurve2dSDM fit_sdm (&data, curve);
00108   fit_sdm.assemble (curve_params);
00109   fit_sdm.solve ();
00110 
00111   pcl::on_nurbs::FittingCurve2dTDM fit_tdm (&data, curve);
00112   fit_tdm.assemble (curve_params);
00113   fit_tdm.solve ();
00114 
00115   // visualize
00116   VisualizeCurve (fit_pdm.m_nurbs, 1.0, 0.0, 0.0, false);
00117   VisualizeCurve (fit_sdm.m_nurbs, 0.0, 1.0, 0.0, false);
00118   VisualizeCurve (fit_tdm.m_nurbs, 0.0, 0.0, 1.0, false);
00119   viewer.spin ();
00120 
00121   return 0;
00122 }
00123 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:23:36