convex_hull.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id: convex_hull.hpp 32993 2010-09-30 23:08:57Z rusu $
35  *
36  */
37 
40 #include <geometry_msgs/PolygonStamped.h>
41 
43 void
45 {
47 
48  pub_output_ = advertise<PointCloud> (*pnh_, "output", max_queue_size_);
49  pub_plane_ = advertise<geometry_msgs::PolygonStamped> (*pnh_, "output_polygon", max_queue_size_);
50 
51  // ---[ Optional parameters
52  pnh_->getParam ("use_indices", use_indices_);
53 
54  NODELET_DEBUG ("[%s::onInit] Nodelet successfully created with the following parameters:\n"
55  " - use_indices : %s",
56  getName ().c_str (),
57  (use_indices_) ? "true" : "false");
58 
60 }
61 
63 void
65 {
66  // If we're supposed to look for PointIndices (indices)
67  if (use_indices_)
68  {
69  // Subscribe to the input using a filter
70  sub_input_filter_.subscribe (*pnh_, "input", 1);
71  // If indices are enabled, subscribe to the indices
72  sub_indices_filter_.subscribe (*pnh_, "indices", 1);
73 
75  {
76  sync_input_indices_a_ = boost::make_shared <message_filters::Synchronizer<sync_policies::ApproximateTime<PointCloud, PointIndices> > >(max_queue_size_);
77  // surface not enabled, connect the input-indices duo and register
79  sync_input_indices_a_->registerCallback (bind (&ConvexHull2D::input_indices_callback, this, _1, _2));
80  }
81  else
82  {
83  sync_input_indices_e_ = boost::make_shared <message_filters::Synchronizer<sync_policies::ExactTime<PointCloud, PointIndices> > >(max_queue_size_);
84  // surface not enabled, connect the input-indices duo and register
86  sync_input_indices_e_->registerCallback (bind (&ConvexHull2D::input_indices_callback, this, _1, _2));
87  }
88  }
89  else
90  // Subscribe in an old fashion to input only (no filters)
91  sub_input_ = pnh_->subscribe<PointCloud> ("input", 1, bind (&ConvexHull2D::input_indices_callback, this, _1, PointIndicesConstPtr ()));
92 }
93 
95 void
97 {
98  if (use_indices_)
99  {
102  }
103  else
105 }
106 
108 void
110  const PointIndicesConstPtr &indices)
111 {
112  // No subscribers, no work
114  return;
115 
116  PointCloud output;
117 
118  // If cloud is given, check if it's valid
119  if (!isValid (cloud))
120  {
121  NODELET_ERROR ("[%s::input_indices_callback] Invalid input!", getName ().c_str ());
122  // Publish an empty message
123  output.header = cloud->header;
124  pub_output_.publish (ros_ptr(output.makeShared ()));
125  return;
126  }
127  // If indices are given, check if they are valid
128  if (indices && !isValid (indices, "indices"))
129  {
130  NODELET_ERROR ("[%s::input_indices_callback] Invalid indices!", getName ().c_str ());
131  // Publish an empty message
132  output.header = cloud->header;
133  pub_output_.publish (ros_ptr(output.makeShared ()));
134  return;
135  }
136 
138  if (indices)
139  NODELET_DEBUG ("[%s::input_indices_model_callback]\n"
140  " - PointCloud with %d data points (%s), stamp %f, and frame %s on topic %s received.\n"
141  " - PointIndices with %zu values, stamp %f, and frame %s on topic %s received.",
142  getName ().c_str (),
143  cloud->width * cloud->height, pcl::getFieldsList (*cloud).c_str (), fromPCL(cloud->header).stamp.toSec (), cloud->header.frame_id.c_str (), getMTPrivateNodeHandle ().resolveName ("input").c_str (),
144  indices->indices.size (), indices->header.stamp.toSec (), indices->header.frame_id.c_str (), getMTPrivateNodeHandle ().resolveName ("indices").c_str ());
145  else
146  NODELET_DEBUG ("[%s::input_indices_callback] PointCloud with %d data points, stamp %f, and frame %s on topic %s received.", getName ().c_str (), cloud->width * cloud->height, fromPCL(cloud->header).stamp.toSec (), cloud->header.frame_id.c_str (), getMTPrivateNodeHandle ().resolveName ("input").c_str ());
147 
148  // Reset the indices and surface pointers
149  IndicesPtr indices_ptr;
150  if (indices)
151  indices_ptr.reset (new std::vector<int> (indices->indices));
152 
153  impl_.setInputCloud (pcl_ptr(cloud));
154  impl_.setIndices (indices_ptr);
155 
156  // Estimate the feature
157  impl_.reconstruct (output);
158 
159  // If more than 3 points are present, send a PolygonStamped hull too
160  if (output.points.size () >= 3)
161  {
162  geometry_msgs::PolygonStamped poly;
163  poly.header = fromPCL(output.header);
164  poly.polygon.points.resize (output.points.size ());
165  // Get three consecutive points (without copying)
166  pcl::Vector4fMap O = output.points[1].getVector4fMap ();
167  pcl::Vector4fMap B = output.points[0].getVector4fMap ();
168  pcl::Vector4fMap A = output.points[2].getVector4fMap ();
169  // Check the direction of points -- polygon must have CCW direction
170  Eigen::Vector4f OA = A - O;
171  Eigen::Vector4f OB = B - O;
172  Eigen::Vector4f N = OA.cross3 (OB);
173  double theta = N.dot (O);
174  bool reversed = false;
175  if (theta < (M_PI / 2.0))
176  reversed = true;
177  for (size_t i = 0; i < output.points.size (); ++i)
178  {
179  if (reversed)
180  {
181  size_t j = output.points.size () - i - 1;
182  poly.polygon.points[i].x = output.points[j].x;
183  poly.polygon.points[i].y = output.points[j].y;
184  poly.polygon.points[i].z = output.points[j].z;
185  }
186  else
187  {
188  poly.polygon.points[i].x = output.points[i].x;
189  poly.polygon.points[i].y = output.points[i].y;
190  poly.polygon.points[i].z = output.points[i].z;
191  }
192  }
193  pub_plane_.publish (boost::make_shared<const geometry_msgs::PolygonStamped> (poly));
194  }
195  // Publish a Boost shared ptr const data
196  output.header = cloud->header;
197  pub_output_.publish (ros_ptr(output.makeShared ()));
198 }
199 
202 
virtual void unsubscribe()
Definition: convex_hull.cpp:96
std::string getFieldsList(const sensor_msgs::PointCloud2 &cloud)
#define NODELET_ERROR(...)
boost::shared_ptr< message_filters::Synchronizer< sync_policies::ExactTime< PointCloud, PointIndices > > > sync_input_indices_e_
Synchronized input, and indices.
Definition: convex_hull.h:85
virtual void onInit()
Nodelet initialization routine. Reads in global parameters used by all nodelets.
Definition: pcl_nodelet.h:204
ros::Subscriber sub_input_
The input PointCloud subscriber.
Definition: convex_hull.h:79
ros::Publisher pub_plane_
Publisher for PolygonStamped.
Definition: convex_hull.h:82
ConvexHull2D represents a 2D ConvexHull implementation.
Definition: convex_hull.h:53
message_filters::Subscriber< PointIndices > sub_indices_filter_
The message filter subscriber for PointIndices.
Definition: pcl_nodelet.h:122
void input_indices_callback(const PointCloudConstPtr &cloud, const PointIndicesConstPtr &indices)
Input point cloud callback.
bool isValid(const PointCloud2::ConstPtr &cloud, const std::string &topic_name="input")
Test whether a given PointCloud message is "valid" (i.e., has points, and width and height are non-ze...
Definition: pcl_nodelet.h:141
ros::NodeHandle & getMTPrivateNodeHandle() const
const std::string & getName() const
void publish(const boost::shared_ptr< M > &message) const
boost::shared_ptr< ros::NodeHandle > pnh_
virtual void subscribe()
LazyNodelet connection routine.
Definition: convex_hull.cpp:64
pcl::ConvexHull< pcl::PointXYZ > impl_
The PCL implementation used.
Definition: convex_hull.h:76
std::string resolveName(const std::string &name, bool remap=true) const
pcl::PointCloud< pcl::PointXYZ > PointCloud
Definition: pcl_nodelet.h:78
virtual void onInit()
Nodelet initialization routine.
Definition: convex_hull.cpp:44
void subscribe(ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size, const ros::TransportHints &transport_hints=ros::TransportHints(), ros::CallbackQueueInterface *callback_queue=0)
bool approximate_sync_
True if we use an approximate time synchronizer versus an exact one (false by default).
Definition: pcl_nodelet.h:131
pcl::IndicesPtr IndicesPtr
Definition: pcl_nodelet.h:90
int max_queue_size_
The maximum queue size (default: 3).
Definition: pcl_nodelet.h:128
ros::Publisher pub_output_
The output PointCloud publisher.
Definition: pcl_nodelet.h:125
boost::shared_ptr< message_filters::Synchronizer< sync_policies::ApproximateTime< PointCloud, PointIndices > > > sync_input_indices_a_
Definition: convex_hull.h:86
void fromPCL(const std::uint64_t &pcl_stamp, ros::Time &stamp)
uint32_t getNumSubscribers() const
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
boost::shared_ptr< T > pcl_ptr(const boost::shared_ptr< T > &p)
Definition: point_cloud.h:392
#define NODELET_DEBUG(...)
pcl_ros::ConvexHull2D ConvexHull2D
bool use_indices_
Set to true if point indices are used.
Definition: pcl_nodelet.h:95
PointIndices::ConstPtr PointIndicesConstPtr
Definition: pcl_nodelet.h:84
boost::shared_ptr< T > ros_ptr(const boost::shared_ptr< T > &p)
Definition: point_cloud.h:354
message_filters::Subscriber< PointCloud > sub_input_filter_
The message filter subscriber for PointCloud2.
Definition: pcl_nodelet.h:119


pcl_ros
Author(s): Open Perception, Julius Kammerl , William Woodall
autogenerated on Thu Feb 16 2023 03:08:02