|
| 1 | +/* |
| 2 | + ********************************************************* |
| 3 | + * ******************************************************* |
| 4 | + * *********** Iterative Closest Point (ICP) ************ |
| 5 | + * ******************************************************* |
| 6 | + * ************ written by Avinash on 04-02-2019 ********* |
| 7 | + ********************************************************* |
| 8 | + */ |
| 9 | + |
| 10 | + |
| 11 | +#include <iostream> |
| 12 | +#include <ros/ros.h> |
| 13 | +#include <pcl_ros/point_cloud.h> |
| 14 | +#include <pcl_conversions/pcl_conversions.h> |
| 15 | +#include <pcl/io/pcd_io.h> |
| 16 | +#include <pcl/point_types.h> |
| 17 | +#include <pcl/registration/icp.h> |
| 18 | +#include <sensor_msgs/point_cloud_conversion.h> |
| 19 | +#include <sensor_msgs/PointCloud.h> |
| 20 | +#include <sensor_msgs/PointCloud2.h> |
| 21 | + |
| 22 | + |
| 23 | +typedef pcl::PointCloud<pcl::PointXYZ> PointCloud; |
| 24 | +static ros::Publisher icp_PCL_pub; |
| 25 | + |
| 26 | +pcl::PointCloud<pcl::PointXYZ>::Ptr template_cuboid(new pcl::PointCloud<pcl::PointXYZ>); |
| 27 | +pcl::PointCloud<pcl::PointXYZ>::Ptr input_cuboid(new pcl::PointCloud<pcl::PointXYZ>); |
| 28 | + |
| 29 | +int performICP() |
| 30 | +{ |
| 31 | + pcl::IterativeClosestPoint <pcl::PointXYZ, pcl::PointXYZ> icp; |
| 32 | + icp.setInputSource(input_cuboid); |
| 33 | + icp.setInputTarget(template_cuboid); |
| 34 | + pcl::PointCloud <pcl::PointXYZ> Final; |
| 35 | + icp.align(Final); |
| 36 | + std::cout << "has converged:" << icp.hasConverged() << " score: " << |
| 37 | + icp.getFitnessScore() << std::endl; |
| 38 | + std::cout << icp.getFinalTransformation() << std::endl; |
| 39 | + |
| 40 | + //icp_PCL_pub.publish(icp.getFinalTransformation()); |
| 41 | + |
| 42 | + return (0); |
| 43 | +} |
| 44 | + |
| 45 | +void rawPCL_cb(const sensor_msgs::PointCloud2::ConstPtr& msg) |
| 46 | +{ |
| 47 | + pcl::PCLPointCloud2 pcl_pc2; |
| 48 | + pcl_conversions::toPCL(*msg,pcl_pc2); |
| 49 | + pcl::fromPCLPointCloud2(pcl_pc2,*input_cuboid); |
| 50 | + performICP(); |
| 51 | +} |
| 52 | + |
| 53 | +int main(int argc, char **argv) |
| 54 | +{ |
| 55 | + ros::init(argc,argv,"iterative_closest_point"); |
| 56 | + ros::NodeHandle nh; |
| 57 | + //icp_PCL_pub = nh.advertise<geometry_msgs::>("iterative_closest_point", 1000); |
| 58 | + ros::Subscriber raw_PCL_sub = nh.subscribe<sensor_msgs::PointCloud2>("/ground_plane_segmentation/points", 1000, rawPCL_cb); |
| 59 | + |
| 60 | + if (pcl::io::loadPCDFile<pcl::PointXYZ> ("$/{workspaceFolder}/src/perception/cuboid_detection/templates/template_cuboid_L200_W100_H75.pcd", *template_cuboid) == -1) //* load the file |
| 61 | + { |
| 62 | + PCL_ERROR ("Couldn't read file test_pcd.pcd \n"); |
| 63 | + return (-1); |
| 64 | + } |
| 65 | + |
| 66 | + ros::spinOnce(); |
| 67 | + return 0; |
| 68 | +} |
0 commit comments