colorize_distance_from_plane_nodelet.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2014, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
38 
39 namespace jsk_pcl_ros_utils
40 {
42  {
43  ConnectionBasedNodelet::onInit();
44 
46  // publisher
48  pub_ = advertise<sensor_msgs::PointCloud2>(*pnh_, "output", 1);
49 
51  // dynamic reconfigure
53 
54  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
55  dynamic_reconfigure::Server<Config>::CallbackType f =
56  boost::bind (&ColorizeDistanceFromPlane::configCallback, this, _1, _2);
57  srv_->setCallback (f);
58  }
59 
61  {
63  // subscribe
65  sub_input_.subscribe(*pnh_, "input", 1);
66  sub_coefficients_.subscribe(*pnh_, "input_coefficients", 1);
67  sub_polygons_.subscribe(*pnh_, "input_polygons", 1);
68 
69  sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(100);
71  sync_->registerCallback(boost::bind(
73  this, _1, _2, _3));
74  }
75 
77  {
81  }
82 
84  const PointT& p,
85  const std::vector<jsk_recognition_utils::ConvexPolygon::Ptr>& convexes)
86  {
87  Eigen::Vector3f v = p.getVector3fMap();
88  double min_distance = DBL_MAX;
89  for (size_t i = 0; i < convexes.size(); i++) {
90  jsk_recognition_utils::ConvexPolygon::Ptr convex = convexes[i];
91  if (!only_projectable_ || convex->isProjectableInside(v)) {
92  double d = convex->distanceToPoint(v);
93  if (d < min_distance) {
94  min_distance = d;
95  }
96  }
97  }
98  return min_distance;
99  }
100 
102  {
103  double ratio = 0.0;
104  if (d > max_distance_) {
105  ratio = 1.0;
106  }
107  else if (d < min_distance_) {
108  ratio = 0.0;
109  }
110  else {
111  ratio = fabs(min_distance_ - d) / (max_distance_ - min_distance_);
112  }
113  double r = ratio;
114  double g = 0.0;
115  double b = 1 - ratio;
116  uint8_t ru, gu, bu;
117  ru = (uint8_t)(r * 255);
118  gu = (uint8_t)(g * 255);
119  bu = (uint8_t)(b * 255);
120  return ((uint32_t)ru<<16 | (uint32_t)gu<<8 | (uint32_t)bu);
121  }
122 
124  const sensor_msgs::PointCloud2::ConstPtr& cloud_msg,
125  const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr& coefficients_msg,
126  const jsk_recognition_msgs::PolygonArray::ConstPtr& polygons)
127  {
128  boost::mutex::scoped_lock lock(mutex_);
129  if (coefficients_msg->coefficients.size() == 0) {
130  return;
131  }
132  // convert all the data into pcl format
133  pcl::PointCloud<PointT>::Ptr cloud
134  (new pcl::PointCloud<PointT>);
135  pcl::fromROSMsg(*cloud_msg, *cloud);
136  std::vector<pcl::ModelCoefficients::Ptr> coefficients
138  coefficients_msg->coefficients);
139 
140  // first, build jsk_recognition_utils::ConvexPolygon::Ptr
141  std::vector<jsk_recognition_utils::ConvexPolygon::Ptr> convexes;
142  for (size_t i = 0; i < polygons->polygons.size(); i++) {
143  if (polygons->polygons[i].polygon.points.size() > 0) {
145  jsk_recognition_utils::ConvexPolygon::fromROSMsg(polygons->polygons[i].polygon);
147  = boost::make_shared<jsk_recognition_utils::ConvexPolygon>(convex);
148  convexes.push_back(convex_ptr);
149  }
150  else {
151  NODELET_ERROR_STREAM(__PRETTY_FUNCTION__ << ":: there is no points in the polygon");
152  }
153  }
154 
155  pcl::PointCloud<pcl::PointXYZRGB>::Ptr output_cloud
156  (new pcl::PointCloud<pcl::PointXYZRGB>);
157 
158  for (size_t i = 0; i < cloud->points.size(); i++) {
159  PointT p = cloud->points[i];
160  pcl::PointXYZRGB p_output;
161  jsk_recognition_utils::pointFromXYZToXYZ<PointT, pcl::PointXYZRGB>(p, p_output);
162  double d = distanceToConvexes(p, convexes);
163  if (d != DBL_MAX) {
164  uint32_t color = colorForDistance(d);
165  p_output.rgb = *reinterpret_cast<float*>(&color);
166  output_cloud->points.push_back(p_output);
167  }
168  }
169 
170  sensor_msgs::PointCloud2 ros_output;
171  pcl::toROSMsg(*output_cloud, ros_output);
172  ros_output.header = cloud_msg->header;
173  pub_.publish(ros_output);
174  }
175 
176  void ColorizeDistanceFromPlane::configCallback(Config &config, uint32_t level)
177  {
178  boost::mutex::scoped_lock lock(mutex_);
179  if (config.max_distance < config.min_distance) {
180  if (max_distance_ != config.max_distance) {
181  config.min_distance = config.max_distance;
182  }
183  else if (min_distance_ != config.min_distance) {
184  config.max_distance = config.min_distance;
185  }
186  }
187  else {
188  max_distance_ = config.max_distance;
189  min_distance_ = config.min_distance;
190  }
191  }
192 
193 }
194 
d
f
lock
static ConvexPolygon fromROSMsg(const geometry_msgs::Polygon &polygon)
void publish(const boost::shared_ptr< M > &message) const
std::vector< pcl::ModelCoefficients::Ptr > convertToPCLModelCoefficients(const std::vector< PCLModelCoefficientMsg > &coefficients)
message_filters::Subscriber< jsk_recognition_msgs::ModelCoefficientsArray > sub_coefficients_
virtual void configCallback(Config &config, uint32_t level)
virtual void colorize(const sensor_msgs::PointCloud2::ConstPtr &cloud, const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr &coefficients, const jsk_recognition_msgs::PolygonArray::ConstPtr &polygons)
coefficients
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
message_filters::Subscriber< jsk_recognition_msgs::PolygonArray > sub_polygons_
#define NODELET_ERROR_STREAM(...)
boost::shared_ptr< ros::NodeHandle > pnh_
void toROSMsg(const pcl::PointCloud< T > &pcl_cloud, sensor_msgs::PointCloud2 &cloud)
boost::shared_ptr< message_filters::Synchronizer< SyncPolicy > > sync_
r
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)
p
message_filters::Subscriber< sensor_msgs::PointCloud2 > sub_input_
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
virtual double distanceToConvexes(const PointT &p, const std::vector< jsk_recognition_utils::ConvexPolygon::Ptr > &convexes)
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros_utils::ColorizeDistanceFromPlane, nodelet::Nodelet)


jsk_pcl_ros_utils
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:15