@@ -44,6 +44,8 @@ target_link_libraries(ground_plane_segmentation ${catkin_LIBRARIES})
4444#include < pcl_ros/transforms.h>
4545#include < pcl_conversions/pcl_conversions.h>
4646
47+ using namespace std ;
48+
4749// Publishers
4850ros::Publisher pcl_pub;
4951ros::Publisher coef_pub;
@@ -55,9 +57,9 @@ ros::Publisher pose_pub;
5557bool invert;
5658double voxel_size;
5759double distance_threshold;
58- std:: string input_topic;
59- std:: string output_topic;
60- std:: string coefficients_topic;
60+ string input_topic;
61+ string output_topic;
62+ string coefficients_topic;
6163
6264Eigen::Matrix4d icp_transform;
6365string template_cuboid_filename;
@@ -76,6 +78,81 @@ bool ICP_SUCCESS = false;
7678bool FIRST = true ;
7779bool POSE_FLAG = false ;
7880
81+ void publish_pose (Eigen::Matrix4d H)
82+ {
83+ // Set translation
84+ tf::Vector3 origin;
85+ origin.setValue (H (0 , 3 ), H (1 , 3 ), H (2 , 3 ));
86+
87+ // Set rotation
88+ tf::Quaternion quaternion;
89+ tf::Matrix3x3 rotation_matrix;
90+ rotation_matrix.setValue (H (0 , 0 ), H (0 , 1 ), H (0 , 2 ),
91+ H (1 , 0 ), H (1 , 1 ), H (1 , 2 ),
92+ H (2 , 0 ), H (2 , 1 ), H (2 , 2 ));
93+ rotation_matrix.getRotation (quaternion);
94+
95+ // Make tf transform message
96+ tf::Transform transform;
97+ transform.setOrigin (origin);
98+ transform.setRotation (quaternion);
99+
100+ // Make pose message
101+ geometry_msgs::Pose p;
102+ p.position .x = H (0 , 3 );
103+ p.position .y = H (1 , 3 );
104+ p.position .z = H (2 , 3 );
105+ p.orientation .x = quaternion.x ();
106+ p.orientation .y = quaternion.y ();
107+ p.orientation .z = quaternion.z ();
108+ p.orientation .w = quaternion.w ();
109+
110+ // Broadcast the transforms
111+ static tf::TransformBroadcaster br;
112+ br.sendTransform (tf::StampedTransform (transform, ros::Time::now (), " camera_depth_optical_frame" , " icp_cuboid_frame" ));
113+ pose_pub.publish (p);
114+ }
115+
116+ void publish_bounding_box (Eigen::Matrix4d H)
117+ {
118+ // Extract cuboid dimensions
119+ double l = dimensions[0 ];
120+ double w = dimensions[1 ];
121+ double h = dimensions[2 ];
122+
123+ // Create a point cloud from the vertices
124+ pcl::PointCloud<pcl::PointXYZ> box_cloud;
125+ box_cloud.push_back (pcl::PointXYZ (-l/2 , -w/2 , -h/2 ));
126+ box_cloud.push_back (pcl::PointXYZ (-l/2 , -w/2 , h/2 ));
127+ box_cloud.push_back (pcl::PointXYZ (-l/2 , w/2 , -h/2 ));
128+ box_cloud.push_back (pcl::PointXYZ (-l/2 , w/2 , h/2 ));
129+ box_cloud.push_back (pcl::PointXYZ (l/2 , -w/2 , -h/2 ));
130+ box_cloud.push_back (pcl::PointXYZ (l/2 , -w/2 , h/2 ));
131+ box_cloud.push_back (pcl::PointXYZ (l/2 , w/2 , -h/2 ));
132+ box_cloud.push_back (pcl::PointXYZ (l/2 , w/2 , h/2 ));
133+
134+ // Transform point cloud
135+ pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud (new pcl::PointCloud<pcl::PointXYZ>);
136+ pcl::transformPointCloud (box_cloud, *transformed_cloud, H.cast <float >());
137+
138+ if (FIRST)
139+ {
140+ FIRST = false ;
141+ cout << " Bounding Box Points:" << endl;
142+ for (int i = 0 ; i < transformed_cloud->size (); i++)
143+ {
144+ cout << " X: " << transformed_cloud->points [i].x << " | "
145+ << " Y: " << transformed_cloud->points [i].y << " | "
146+ << " Z: " << transformed_cloud->points [i].z << endl;
147+ }
148+ }
149+
150+ // Convert to ROS data type and publish
151+ pcl::toROSMsg (*transformed_cloud, bbox_msg);
152+ bbox_msg.header .frame_id = " camera_depth_optical_frame" ;
153+ bbox_pub.publish (bbox_msg);
154+ }
155+
79156void icp_registration (const sensor_msgs::PointCloud2::ConstPtr& msg)
80157{
81158 // Compute ICP only once
@@ -152,23 +229,23 @@ void callback(const sensor_msgs::PointCloud2ConstPtr& input)
152229 pcl::PCLPointCloud2::Ptr cloud_filtered_ptr_z (new pcl::PCLPointCloud2);
153230 pcl::PCLPointCloud2::Ptr cloud_filtered_ptr (new pcl::PCLPointCloud2);
154231 pcl_conversions::toPCL (*input, *cloud_ptr);
155- if (DEBUG) std:: cerr << " PointCloud before filtering: " << cloud_ptr->width << " " << cloud_ptr->height << " data points." << std:: endl;
232+ if (DEBUG) cerr << " PointCloud before filtering: " << cloud_ptr->width << " " << cloud_ptr->height << " data points." << endl;
156233
157234 // Filter the points in z-axis
158235 pcl::PassThrough<pcl::PCLPointCloud2> pass_z;
159236 pass_z.setInputCloud (cloud_ptr);
160237 pass_z.setFilterFieldName (" z" );
161238 pass_z.setFilterLimits (0.0 , 0.9 );
162239 pass_z.filter (*cloud_filtered_ptr_z);
163- if (DEBUG) std:: cerr << " PointCloud after filtering: " << cloud_filtered_ptr_z->width << " " << cloud_filtered_ptr->height << " data points." << std:: endl;
240+ if (DEBUG) cerr << " PointCloud after filtering: " << cloud_filtered_ptr_z->width << " " << cloud_filtered_ptr->height << " data points." << endl;
164241
165242 // Filter the points in y-axis
166243 pcl::PassThrough<pcl::PCLPointCloud2> pass;
167244 pass.setInputCloud (cloud_filtered_ptr_z);
168245 pass.setFilterFieldName (" x" );
169246 pass.setFilterLimits (-0.2 , 0.2 );
170247 pass.filter (*cloud_filtered_ptr);
171- if (DEBUG) std:: cerr << " PointCloud after filtering in x-axis: " << cloud_filtered_ptr->width << " " << cloud_filtered_ptr->height << " data points." << std:: endl;
248+ if (DEBUG) cerr << " PointCloud after filtering in x-axis: " << cloud_filtered_ptr->width << " " << cloud_filtered_ptr->height << " data points." << endl;
172249
173250 // Downsample the points
174251 pcl::PCLPointCloud2::Ptr voxel_ptr (new pcl::PCLPointCloud2);
@@ -204,7 +281,7 @@ void callback(const sensor_msgs::PointCloud2ConstPtr& input)
204281 extract.setIndices (inliers);
205282 extract.setNegative (invert);
206283 extract.filter (*plane_cloud_ptr);
207- if (DEBUG) std:: cerr << " PointCloud representing the planar component: " << plane_cloud_ptr->width << " " << plane_cloud_ptr->height << " data points." << std:: endl;
284+ if (DEBUG) cerr << " PointCloud representing the planar component: " << plane_cloud_ptr->width << " " << plane_cloud_ptr->height << " data points." << endl;
208285
209286 // Additional filtering
210287 pcl::PCLPointCloud2::Ptr cloud_filtered_ptr_z2 (new pcl::PCLPointCloud2);
@@ -213,7 +290,7 @@ void callback(const sensor_msgs::PointCloud2ConstPtr& input)
213290 pass_z2.setFilterFieldName (" z" );
214291 pass_z2.setFilterLimits (0.0 , 0.7 );
215292 pass_z2.filter (*cloud_filtered_ptr_z2);
216- if (DEBUG) std:: cerr << " PointCloud after filtering: " << cloud_filtered_ptr_z2->width << " " << cloud_filtered_ptr->height << " data points." << std:: endl;
293+ if (DEBUG) cerr << " PointCloud after filtering: " << cloud_filtered_ptr_z2->width << " " << cloud_filtered_ptr->height << " data points." << endl;
217294
218295 // Create the KdTree object for the search method of the extraction
219296 pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
@@ -222,7 +299,7 @@ void callback(const sensor_msgs::PointCloud2ConstPtr& input)
222299 tree->setInputCloud (pcl_cloud_cleaned);
223300
224301 // Create the extraction object for the clusters
225- std:: vector<pcl::PointIndices> object_cluster_indices;
302+ vector<pcl::PointIndices> object_cluster_indices;
226303 // pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
227304 pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
228305 // specify euclidean cluster parameters
@@ -235,10 +312,10 @@ void callback(const sensor_msgs::PointCloud2ConstPtr& input)
235312 ec.extract (object_cluster_indices);
236313
237314 // Display number of objects detceted
238- std:: cerr << " Objects Detected: " << object_cluster_indices.size () << std:: endl;
315+ cerr << " Objects Detected: " << object_cluster_indices.size () << endl;
239316
240317 // What is this?
241- for (std:: vector<pcl::PointIndices>::const_iterator it = object_cluster_indices.begin (); it != object_cluster_indices.end (); ++it)
318+ for (vector<pcl::PointIndices>::const_iterator it = object_cluster_indices.begin (); it != object_cluster_indices.end (); ++it)
242319 {
243320 // Create a pcl object to hold the extracted cluster
244321 pcl::PCLPointCloud2::Ptr object_cluster (new pcl::PCLPointCloud2);
@@ -257,7 +334,7 @@ void callback(const sensor_msgs::PointCloud2ConstPtr& input)
257334
258335 // now we are in a vector of indices pertaining to a single cluster.
259336 // Assign each point corresponding to this cluster in xyzCloudPtrPassthroughFiltered a specific color for identification purposes
260- // for (std:: vector<int>::const_iterator idx = it->indices.begin(); idx != it->indices.end(); ++idx)
337+ // for (vector<int>::const_iterator idx = it->indices.begin(); idx != it->indices.end(); ++idx)
261338 // {
262339 // object_cluster->points.push_back(cloud_filtered_ptr_z2->points[*idx]);
263340 // }
@@ -298,17 +375,17 @@ void callback(const sensor_msgs::PointCloud2ConstPtr& input)
298375// nh.param<bool>("invert", invert, true);
299376// nh.param<double>("voxel_size", voxel_size, 0.01);
300377// nh.param<double>("distance_threshold", distance_threshold, 0.01);
301- // nh.param<std:: string>("input", input_topic, "/camera/depth/color/points");
302- // nh.param<std:: string>("output", output_topic, "/ground_plane_segmentation/points");
303- // nh.param<std:: string>("plane_coefficients", coefficients_topic, "/ground_plane_segmentation/coefficients");
378+ // nh.param<string>("input", input_topic, "/camera/depth/color/points");
379+ // nh.param<string>("output", output_topic, "/ground_plane_segmentation/points");
380+ // nh.param<string>("plane_coefficients", coefficients_topic, "/ground_plane_segmentation/coefficients");
304381
305382// // Display params
306- // std:: cout << "\nInvert Segmentation: " << invert << std:: endl;
307- // std:: cout << "Voxel Size: " << voxel_size << std:: endl;
308- // std:: cout << "Distance Threshold: " << distance_threshold << std:: endl;
309- // std:: cout << "Input Topic: " << input_topic << std:: endl;
310- // std:: cout << "Output Topic: " << output_topic << std:: endl;
311- // std:: cout << "Co-efficients Topic: " << coefficients_topic << std:: endl;
383+ // cout << "\nInvert Segmentation: " << invert << endl;
384+ // cout << "Voxel Size: " << voxel_size << endl;
385+ // cout << "Distance Threshold: " << distance_threshold << endl;
386+ // cout << "Input Topic: " << input_topic << endl;
387+ // cout << "Output Topic: " << output_topic << endl;
388+ // cout << "Co-efficients Topic: " << coefficients_topic << endl;
312389
313390// // Create a ROS subscriber for the input point cloud
314391// ros::Subscriber sub = nh.subscribe(input_topic, 1, callback);
@@ -328,7 +405,7 @@ int main(int argc, char **argv)
328405 ros::NodeHandle nh (" ~" );
329406
330407 template_pub = nh.advertise <sensor_msgs::PointCloud2>(" /icp/template" , 1 );
331- std:: string template_cuboid_filename = " " ;
408+ string template_cuboid_filename = " " ;
332409
333410 while (1 )
334411 {
@@ -340,7 +417,7 @@ int main(int argc, char **argv)
340417 if (pcl::io::loadPCDFile<pcl::PointXYZ>(template_cuboid_filename, *template_cuboid) == -1 )
341418 {
342419 PCL_ERROR (" Couldn't read the template PCL file" );
343- return ;
420+ return 0 ;
344421 }
345422
346423 // Convert to ROS data type
0 commit comments