@@ -12,39 +12,38 @@ add_executable(ground_plane_segmentation src/ground_plane_segmentation.cpp)
1212target_link_libraries(ground_plane_segmentation ${catkin_LIBRARIES})
1313*/
1414
15- #include < ros/ros.h>
1615#include " object_detection/boost.h"
1716#include < visualization_msgs/Marker.h>
1817
1918// TF includes
2019#include < tf/tf.h>
21- #include < tf/transform_listener.h>
2220#include < tf/transform_broadcaster.h>
21+ #include < tf/transform_listener.h>
2322#include < tf_conversions/tf_eigen.h>
2423
2524// Pose includes
26- #include < geometry_msgs/Pose.h>
2725#include < eigen_conversions/eigen_msg.h>
26+ #include < geometry_msgs/Pose.h>
2827
2928// PCL specific includes
30- #include < sensor_msgs/PointCloud2 .h>
29+ #include < pcl/ModelCoefficients .h>
3130#include < pcl/io/pcd_io.h>
3231#include < pcl/io/ply_io.h>
3332#include < pcl/point_types.h>
3433#include < pcl/registration/icp.h>
35- #include < pcl/ModelCoefficients.h>
3634#include < pcl/sample_consensus/method_types.h>
3735#include < pcl/sample_consensus/model_types.h>
38- #include < pcl/segmentation/sac_segmentation.h>
39- #include < pcl/segmentation/extract_clusters.h>
4036#include < pcl/search/kdtree.h>
41- #include < pcl_ros/point_cloud.h>
37+ #include < pcl/segmentation/extract_clusters.h>
38+ #include < pcl/segmentation/sac_segmentation.h>
39+ #include < pcl_conversions/pcl_conversions.h>
40+ #include < pcl_ros/filters/extract_indices.h>
4241#include < pcl_ros/filters/passthrough.h>
4342#include < pcl_ros/filters/voxel_grid.h>
44- #include < pcl_ros/filters/extract_indices .h>
43+ #include < pcl_ros/point_cloud .h>
4544#include < pcl_ros/point_cloud.h>
4645#include < pcl_ros/transforms.h>
47- #include < pcl_conversions/pcl_conversions .h>
46+ #include < sensor_msgs/PointCloud2 .h>
4847
4948// Local includes
5049#include " object_detection/ObjectDetection.h"
@@ -74,17 +73,17 @@ vector<Eigen::Matrix4d> icp_transforms;
7473sensor_msgs::PointCloud2 bbox_msg;
7574sensor_msgs::PointCloud2 output_msg;
7675sensor_msgs::PointCloud2 template_msg;
77- tf::TransformListener * listener;
76+ tf::TransformListener* listener;
7877geometry_msgs::Pose pose_msg;
7978Eigen::Affine3d pose_transform;
80- double dimensions[] = {0.1 , 0.02 , 0.02 };
79+ double dimensions[] = { 0.1 , 0.02 , 0.02 };
8180double icp_fitness_score;
8281pcl::PCLPointCloud2::Ptr input_pcl (new pcl::PCLPointCloud2);
8382
8483// Template paths
8584string template_path;
86- string template_filenames[] = {" " , " screwdriver_ascii_tf.pcd" , " eraser_ascii_tf.pcd" ,
87- " clamp_ascii_tf.pcd" , " marker_ascii_tf.pcd" };
85+ string template_filenames[] = { " " , " screwdriver_ascii_tf.pcd" , " eraser_ascii_tf.pcd" ,
86+ " clamp_ascii_tf.pcd" , " marker_ascii_tf.pcd" };
8887
8988// Flags
9089bool DEBUG = false ;
@@ -139,13 +138,13 @@ void publish_pose(Eigen::Matrix4d H)
139138 // Set translation
140139 tf::Vector3 origin;
141140 origin.setValue (H (0 , 3 ), H (1 , 3 ), H (2 , 3 ));
142-
141+
143142 // Set rotation
144143 tf::Quaternion quaternion;
145144 tf::Matrix3x3 rotation_matrix;
146- rotation_matrix.setValue (H (0 , 0 ), H (0 , 1 ), H (0 , 2 ),
147- H (1 , 0 ), H (1 , 1 ), H (1 , 2 ),
148- H (2 , 0 ), H (2 , 1 ), H (2 , 2 ));
145+ rotation_matrix.setValue (H (0 , 0 ), H (0 , 1 ), H (0 , 2 ),
146+ H (1 , 0 ), H (1 , 1 ), H (1 , 2 ),
147+ H (2 , 0 ), H (2 , 1 ), H (2 , 2 ));
149148 rotation_matrix.getRotation (quaternion);
150149
151150 // Make tf transform message
@@ -181,14 +180,14 @@ void publish_bounding_box(Eigen::Matrix4d H)
181180
182181 // Create a point cloud from the vertices
183182 pcl::PointCloud<pcl::PointXYZ> box_cloud;
184- box_cloud.push_back (pcl::PointXYZ (-l/ 2 , -w/ 2 , -h/ 2 ));
185- box_cloud.push_back (pcl::PointXYZ (-l/ 2 , -w/ 2 , h/ 2 ));
186- box_cloud.push_back (pcl::PointXYZ (-l/ 2 , w/ 2 , -h/ 2 ));
187- box_cloud.push_back (pcl::PointXYZ (-l/ 2 , w/ 2 , h/ 2 ));
188- box_cloud.push_back (pcl::PointXYZ (l/ 2 , -w/ 2 , -h/ 2 ));
189- box_cloud.push_back (pcl::PointXYZ (l/ 2 , -w/ 2 , h/ 2 ));
190- box_cloud.push_back (pcl::PointXYZ (l/ 2 , w/ 2 , -h/ 2 ));
191- box_cloud.push_back (pcl::PointXYZ (l/ 2 , w/ 2 , h/ 2 ));
183+ box_cloud.push_back (pcl::PointXYZ (-l / 2 , -w / 2 , -h / 2 ));
184+ box_cloud.push_back (pcl::PointXYZ (-l / 2 , -w / 2 , h / 2 ));
185+ box_cloud.push_back (pcl::PointXYZ (-l / 2 , w / 2 , -h / 2 ));
186+ box_cloud.push_back (pcl::PointXYZ (-l / 2 , w / 2 , h / 2 ));
187+ box_cloud.push_back (pcl::PointXYZ (l / 2 , -w / 2 , -h / 2 ));
188+ box_cloud.push_back (pcl::PointXYZ (l / 2 , -w / 2 , h / 2 ));
189+ box_cloud.push_back (pcl::PointXYZ (l / 2 , w / 2 , -h / 2 ));
190+ box_cloud.push_back (pcl::PointXYZ (l / 2 , w / 2 , h / 2 ));
192191
193192 // Transform point cloud
194193 pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud (new pcl::PointCloud<pcl::PointXYZ>);
@@ -211,8 +210,7 @@ void publish_bounding_box(Eigen::Matrix4d H)
211210double icp_registration (pcl::PointCloud<pcl::PointXYZ>::Ptr input_pcl, pcl::PointCloud<pcl::PointXYZ>::Ptr template_pcl)
212211{
213212 int max_iter = 0 ;
214- while (1 )
215- {
213+ while (1 ) {
216214 // Point cloud containers
217215 pcl::PointCloud<pcl::PointXYZ>::Ptr output_cloud (new pcl::PointCloud<pcl::PointXYZ>);
218216
@@ -232,11 +230,11 @@ double icp_registration(pcl::PointCloud<pcl::PointXYZ>::Ptr input_pcl, pcl::Poin
232230 cerr << " ICP Score Before: " << icp.getFitnessScore () << endl;
233231 max_iter++;
234232
235- if ((icp.hasConverged () && icp.getFitnessScore () < icp_fitness_score) || (max_iter > 10 ))
236- {
233+ if ((icp.hasConverged () && icp.getFitnessScore () < icp_fitness_score) || (max_iter > 10 )) {
237234 // Display ICP results
238235 cerr << " \n ICP Score: " << icp.getFitnessScore () << endl;
239- cerr << " ICP Transform:\n " << icp_transform_local << endl;
236+ cerr << " ICP Transform:\n "
237+ << icp_transform_local << endl;
240238
241239 // Convert to ROS data type
242240 pcl::toROSMsg (*output_cloud, output_msg);
@@ -250,21 +248,21 @@ void pcl_callback(const sensor_msgs::PointCloud2ConstPtr& input)
250248{
251249 // Save the input point cloud to a global variable
252250 pcl_conversions::toPCL (*input, *input_pcl);
253- if (DEBUG) cerr << " PointCloud before filtering: " << input_pcl->width << " " << input_pcl->height << " data points." << endl;
251+ if (DEBUG)
252+ cerr << " PointCloud before filtering: " << input_pcl->width << " " << input_pcl->height << " data points." << endl;
254253
255254 // Publish template point cloud
256- if (ICP_SUCCESS and argmin != -1 )
257- {
255+ if (ICP_SUCCESS and argmin != -1 ) {
258256 icp_pub.publish (output_msg);
259257 template_msg.header .frame_id = " camera_depth_optical_frame" ;
260258 template_pub.publish (template_msg);
261259 // publish_bounding_box(icp_transform);
262260 publish_pose (icp_transform);
263- }
261+ }
264262}
265263
266- bool service_callback (object_detection::ObjectDetection::Request & req,
267- object_detection::ObjectDetection::Response & res)
264+ bool service_callback (object_detection::ObjectDetection::Request& req,
265+ object_detection::ObjectDetection::Response& res)
268266{
269267 // Filter the points in z-axis
270268 pcl::PCLPointCloud2::Ptr cloud_filtered_ptr_z (new pcl::PCLPointCloud2);
@@ -273,7 +271,8 @@ bool service_callback(object_detection::ObjectDetection::Request &req,
273271 pass_z.setFilterFieldName (" z" );
274272 pass_z.setFilterLimits (0.0 , 0.9 );
275273 pass_z.filter (*cloud_filtered_ptr_z);
276- if (DEBUG) cerr << " PointCloud after filtering: " << cloud_filtered_ptr_z->width << " " << cloud_filtered_ptr_z->height << " data points." << endl;
274+ if (DEBUG)
275+ cerr << " PointCloud after filtering: " << cloud_filtered_ptr_z->width << " " << cloud_filtered_ptr_z->height << " data points." << endl;
277276
278277 // Filter the points in y-axis
279278 pcl::PCLPointCloud2::Ptr cloud_filtered_ptr (new pcl::PCLPointCloud2);
@@ -282,7 +281,8 @@ bool service_callback(object_detection::ObjectDetection::Request &req,
282281 pass.setFilterFieldName (" x" );
283282 pass.setFilterLimits (-0.2 , 0.2 );
284283 pass.filter (*cloud_filtered_ptr);
285- if (DEBUG) cerr << " PointCloud after filtering in x-axis: " << cloud_filtered_ptr->width << " " << cloud_filtered_ptr->height << " data points." << endl;
284+ if (DEBUG)
285+ cerr << " PointCloud after filtering in x-axis: " << cloud_filtered_ptr->width << " " << cloud_filtered_ptr->height << " data points." << endl;
286286
287287 // Downsample the points
288288 pcl::PCLPointCloud2::Ptr voxel_ptr (new pcl::PCLPointCloud2);
@@ -300,7 +300,7 @@ bool service_callback(object_detection::ObjectDetection::Request &req,
300300 pcl::PointCloud<pcl::PointXYZ>::Ptr pcl_voxel_ptr (new pcl::PointCloud<pcl::PointXYZ>);
301301 pcl::fromPCLPointCloud2 (*voxel_ptr, *pcl_voxel_ptr);
302302
303- // Segmentation paramters
303+ // Segmentation paramters
304304 seg.setOptimizeCoefficients (true );
305305 seg.setModelType (pcl::SACMODEL_PLANE);
306306 seg.setMethodType (pcl::SAC_RANSAC);
@@ -318,7 +318,8 @@ bool service_callback(object_detection::ObjectDetection::Request &req,
318318 extract.setIndices (inliers);
319319 extract.setNegative (invert);
320320 extract.filter (*plane_cloud_ptr);
321- if (DEBUG) cerr << " PointCloud representing the planar component: " << plane_cloud_ptr->width << " " << plane_cloud_ptr->height << " data points." << endl;
321+ if (DEBUG)
322+ cerr << " PointCloud representing the planar component: " << plane_cloud_ptr->width << " " << plane_cloud_ptr->height << " data points." << endl;
322323
323324 // Additional filtering
324325 pcl::PCLPointCloud2::Ptr cloud_filtered_ptr_z2 (new pcl::PCLPointCloud2);
@@ -327,7 +328,8 @@ bool service_callback(object_detection::ObjectDetection::Request &req,
327328 pass_z2.setFilterFieldName (" z" );
328329 pass_z2.setFilterLimits (0.0 , 0.75 );
329330 pass_z2.filter (*cloud_filtered_ptr_z2);
330- if (DEBUG) cerr << " PointCloud after filtering: " << cloud_filtered_ptr_z2->width << " " << cloud_filtered_ptr->height << " data points." << endl;
331+ if (DEBUG)
332+ cerr << " PointCloud after filtering: " << cloud_filtered_ptr_z2->width << " " << cloud_filtered_ptr->height << " data points." << endl;
331333
332334 // Convert to ROS data type
333335 sensor_msgs::PointCloud2 output;
@@ -363,8 +365,7 @@ bool service_callback(object_detection::ObjectDetection::Request &req,
363365 icp_transforms.clear ();
364366 ICP_SUCCESS = false ;
365367
366- for (vector<pcl::PointIndices>::const_iterator it = object_cluster_indices.begin (); it != object_cluster_indices.end (); ++it)
367- {
368+ for (vector<pcl::PointIndices>::const_iterator it = object_cluster_indices.begin (); it != object_cluster_indices.end (); ++it) {
368369 // Create a pcl object to hold the extracted cluster
369370 pcl::PCLPointCloud2::Ptr object_cluster (new pcl::PCLPointCloud2);
370371 pcl::ExtractIndices<pcl::PCLPointCloud2> obj_extract;
@@ -377,7 +378,7 @@ bool service_callback(object_detection::ObjectDetection::Request &req,
377378 pcl::PointCloud<pcl::PointXYZ>::Ptr object_cluster_pcl (new pcl::PointCloud<pcl::PointXYZ>);
378379 pcl::fromPCLPointCloud2 (*object_cluster, *object_cluster_pcl);
379380 // pcl::io::savePCDFile("/home/heethesh/ROS-Workspaces/dash_ws/src/perception/object_detection/templates/clamp.pcd", *object_cluster_pcl, true);
380-
381+
381382 // Convert to ROS data type
382383 // sensor_msgs::PointCloud2 output;
383384 // pcl_conversions::fromPCL(*object_cluster, output);
@@ -386,8 +387,7 @@ bool service_callback(object_detection::ObjectDetection::Request &req,
386387 // Read template point cloud
387388 string template_filename = template_path + template_filenames[req.object_id ];
388389 pcl::PointCloud<pcl::PointXYZ>::Ptr template_pcl (new pcl::PointCloud<pcl::PointXYZ>);
389- if (pcl::io::loadPCDFile<pcl::PointXYZ>(template_filename, *template_pcl) == -1 )
390- {
390+ if (pcl::io::loadPCDFile<pcl::PointXYZ>(template_filename, *template_pcl) == -1 ) {
391391 PCL_ERROR (" Couldn't read the template PCL file" );
392392 res.success = false ;
393393 return false ;
@@ -401,10 +401,8 @@ bool service_callback(object_detection::ObjectDetection::Request &req,
401401 // Find the object with best ICP score
402402 double min_score = 1000 ;
403403 argmin = -1 ;
404- for (int i = 0 ; i < icp_scores.size (); i++)
405- {
406- if (icp_scores[i] < min_score)
407- {
404+ for (int i = 0 ; i < icp_scores.size (); i++) {
405+ if (icp_scores[i] < min_score) {
408406 argmin = i;
409407 min_score = icp_scores[i];
410408 }
@@ -414,12 +412,14 @@ bool service_callback(object_detection::ObjectDetection::Request &req,
414412 icp_transform = icp_transforms[argmin];
415413
416414 if (min_score < icp_fitness_score) {
417- cerr << " ICP Registration Success!\n " << endl;
415+ cerr << " ICP Registration Success!\n "
416+ << endl;
418417 ICP_SUCCESS = true ;
419418 res.success = true ;
420419 return true ;
421420 } else {
422- cerr << " ICP Registration Failed to Converge within Threshold!\n " << endl;
421+ cerr << " ICP Registration Failed to Converge within Threshold!\n "
422+ << endl;
423423 ICP_SUCCESS = false ;
424424 res.success = false ;
425425 return false ;
@@ -497,7 +497,7 @@ int main(int argc, char** argv)
497497
498498// // Convert to ROS data type
499499// pcl::toROSMsg(*template_cuboid, template_msg);
500-
500+
501501// // Publish template point cloud
502502// template_msg.header.frame_id = "cuboid_frame";
503503// template_pub.publish(template_msg);
0 commit comments