points_rectangle_extractor.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 
7 
8 #include <sensor_msgs/Image.h>
9 #include <sensor_msgs/CameraInfo.h>
10 #include <sensor_msgs/PointCloud2.h>
11 
12 //#include <image_view2/PointArrayStamped.h>
13 #include <geometry_msgs/PointStamped.h>
14 #include <geometry_msgs/PolygonStamped.h>
15 
17 {
18  typedef message_filters::sync_policies::ApproximateTime< sensor_msgs::PointCloud2,
19  geometry_msgs::PolygonStamped > PolygonApproxSyncPolicy;
20 
21  typedef message_filters::sync_policies::ApproximateTime< sensor_msgs::PointCloud2,
22  geometry_msgs::PointStamped > PointApproxSyncPolicy;
23 
24 private:
29 
33 
36 
37 public:
38  PointsRectExtractor() : pnode_("~"), queue_size(200), crop_width(2)
39  {
40  rect_sub_.subscribe (pnode_, "rect", queue_size);
41  point_sub_.subscribe (pnode_, "point", queue_size);
42  points_sub_.subscribe (pnode_, "points", queue_size);
43 
44  sync_a_polygon_ = boost::make_shared < message_filters::Synchronizer< PolygonApproxSyncPolicy > > (queue_size);
45  sync_a_polygon_->connectInput (points_sub_, rect_sub_);
46  sync_a_polygon_->registerCallback (boost::bind (&PointsRectExtractor::callback_polygon, this, _1, _2));
47 
48  sync_a_point_ = boost::make_shared < message_filters::Synchronizer< PointApproxSyncPolicy > > (queue_size);
49  sync_a_point_->connectInput (points_sub_, point_sub_);
50  sync_a_point_->registerCallback (boost::bind (&PointsRectExtractor::callback_point, this, _1, _2));
51 
52  pub_ = pnode_.advertise< sensor_msgs::PointCloud2 > ("output", 1);
53  }
54 
55  void callback_point(const sensor_msgs::PointCloud2ConstPtr& points_ptr,
56  const geometry_msgs::PointStampedConstPtr& array_ptr) {
57  int st_x = array_ptr->point.x - crop_width;
58  int st_y = array_ptr->point.y - crop_width;
59  int ed_x = array_ptr->point.x + crop_width;
60  int ed_y = array_ptr->point.y + crop_width;
61 
62  int wd = points_ptr->width;
63  int ht = points_ptr->height;
64  int rstep = points_ptr->row_step;
65  int pstep = points_ptr->point_step;
66  if (st_x < 0) st_x = 0;
67  if (st_y < 0) st_x = 0;
68  if (ed_x >= wd) ed_x = wd - 1;
69  if (ed_y >= ht) ed_y = ht - 1;
70 
71  sensor_msgs::PointCloud2 pt;
72  pt.header = points_ptr->header;
73  pt.width = ed_x - st_x + 1;
74  pt.height = ed_y - st_y + 1;
75  pt.row_step = pt.width * pstep;
76  pt.point_step = pstep;
77  pt.is_bigendian = false;
78  pt.fields = points_ptr->fields;
79  pt.is_dense = false;
80  pt.data.resize(pt.row_step * pt.height);
81 
82  unsigned char * dst_ptr = &(pt.data[0]);
83 
84  for (size_t idx_y = st_y; idx_y <= ed_y; idx_y++) {
85  for (size_t idx_x = st_x; idx_x <= ed_x; idx_x++) {
86  const unsigned char * src_ptr = &(points_ptr->data[idx_y * rstep + idx_x * pstep]);
87  memcpy(dst_ptr, src_ptr, pstep);
88  dst_ptr += pstep;
89  }
90  }
91  pub_.publish (pt);
92  }
93 
94  void callback_polygon(const sensor_msgs::PointCloud2ConstPtr& points_ptr,
95  const geometry_msgs::PolygonStampedConstPtr& array_ptr) {
96  // Solve all of perception here...
97  ROS_DEBUG("callback");
98 #if 0
99  if(pub_.getNumSubscribers()==0 ){
100  ROS_INFO("number of subscribers is 0, ignoring image");
101  return;
102  }
103 #endif
104 
105  if (array_ptr->polygon.points.size() > 1) {
106  int st_x = array_ptr->polygon.points[0].x;
107  int st_y = array_ptr->polygon.points[0].y;
108  int ed_x = array_ptr->polygon.points[1].x;
109  int ed_y = array_ptr->polygon.points[1].y;
110  int wd = points_ptr->width;
111  int ht = points_ptr->height;
112  int rstep = points_ptr->row_step;
113  int pstep = points_ptr->point_step;
114 
115  sensor_msgs::PointCloud2 pt;
116  pt.header = points_ptr->header;
117  pt.width = ed_x - st_x + 1;
118  pt.height = ed_y - st_y + 1;
119  pt.row_step = pt.width * pstep;
120  pt.point_step = pstep;
121  pt.is_bigendian = false;
122  pt.fields = points_ptr->fields;
123  pt.is_dense = false;
124  pt.data.resize(pt.row_step * pt.height);
125 
126  unsigned char * dst_ptr = &(pt.data[0]);
127 
128  for (size_t idx_y = st_y; idx_y <= ed_y; idx_y++) {
129  for (size_t idx_x = st_x; idx_x <= ed_x; idx_x++) {
130  const unsigned char * src_ptr = &(points_ptr->data[idx_y * rstep + idx_x * pstep]);
131  memcpy(dst_ptr, src_ptr, pstep);
132  dst_ptr += pstep;
133  }
134  }
135  pub_.publish (pt);
136  }
137  }
138 };
139 
140 int main (int argc, char **argv)
141 {
142  ros::init (argc, argv, "points_rectangle_extractor");
143 
144  if(!ros::master::check())
145  return -1;
146 
148 
149  ros::spin();
150 
151  return 0;
152 }
ROSCPP_DECL bool check()
void callback_point(const sensor_msgs::PointCloud2ConstPtr &points_ptr, const geometry_msgs::PointStampedConstPtr &array_ptr)
void publish(const boost::shared_ptr< M > &message) const
boost::shared_ptr< message_filters::Synchronizer< PolygonApproxSyncPolicy > > sync_a_polygon_
message_filters::Subscriber< geometry_msgs::PointStamped > point_sub_
message_filters::Subscriber< sensor_msgs::PointCloud2 > points_sub_
message_filters::Subscriber< geometry_msgs::PolygonStamped > rect_sub_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void callback_polygon(const sensor_msgs::PointCloud2ConstPtr &points_ptr, const geometry_msgs::PolygonStampedConstPtr &array_ptr)
ROSCPP_DECL void spin(Spinner &spinner)
#define ROS_INFO(...)
int main(int argc, char **argv)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
uint32_t getNumSubscribers() const
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)
boost::shared_ptr< message_filters::Synchronizer< PointApproxSyncPolicy > > sync_a_point_
message_filters::sync_policies::ApproximateTime< sensor_msgs::PointCloud2, geometry_msgs::PolygonStamped > PolygonApproxSyncPolicy
message_filters::sync_policies::ApproximateTime< sensor_msgs::PointCloud2, geometry_msgs::PointStamped > PointApproxSyncPolicy
#define ROS_DEBUG(...)


image_view2
Author(s): Kei Okada
autogenerated on Tue Feb 6 2018 03:45:03