polygon_flipper_nodelet.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2015, 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/or 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 
36 #define BOOST_PARAMETER_MAX_ARITY 7
39 #include <algorithm>
40 #include <iterator>
41 
42 namespace jsk_pcl_ros_utils
43 {
45  {
46  DiagnosticNodelet::onInit();
47  if (!pnh_->getParam("sensor_frame", sensor_frame_)) {
48  NODELET_FATAL("no ~sensor_frame is specified");
49  return;
50  }
51 
52  pnh_->param<int>("queue_size", queue_size_, 100);
53  pnh_->param<bool>("use_indices", use_indices_, true);
54 
56  pub_polygons_ = advertise<jsk_recognition_msgs::PolygonArray>(
57  *pnh_, "output/polygons", 1);
58  pub_coefficients_ = advertise<jsk_recognition_msgs::ModelCoefficientsArray>(
59  *pnh_, "output/coefficients", 1);
60  if (use_indices_)
61  pub_indices_ = advertise<jsk_recognition_msgs::ClusterPointIndices>(
62  *pnh_, "output/indices", 1);
63 
64  onInitPostProcess();
65  }
66 
68  // message_filters::Synchronizer needs to be called reset
69  // before message_filters::Subscriber is freed.
70  // Calling reset fixes the following error on shutdown of the nodelet:
71  // terminate called after throwing an instance of
72  // 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
73  // what(): boost: mutex lock failed in pthread_mutex_lock: Invalid argument
74  // Also see https://github.com/ros/ros_comm/issues/720 .
75  sync_.reset();
76  }
77 
79  {
80  sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(queue_size_);
81  sub_polygons_.subscribe(*pnh_, "input/polygons", 1);
82  sub_coefficients_.subscribe(*pnh_, "input/coefficients", 1);
83  if (use_indices_) {
84  sub_indices_.subscribe(*pnh_, "input/indices", 1);
86  } else {
89  }
90  sync_->registerCallback(boost::bind(&PolygonFlipper::flip, this, _1, _2, _3));
91  }
92 
94  {
97  if (use_indices_)
99  }
100 
102  const jsk_recognition_msgs::PolygonArray::ConstPtr& polygons_msg)
103  {
104  jsk_recognition_msgs::ClusterPointIndices indices;
105  indices.header.stamp = polygons_msg->header.stamp;
106  indices.cluster_indices.resize(polygons_msg->polygons.size());
108  boost::make_shared<jsk_recognition_msgs::ClusterPointIndices>(indices));
109  }
110 
112  const jsk_recognition_msgs::PolygonArray::ConstPtr& polygons_msg,
113  const jsk_recognition_msgs::ClusterPointIndices::ConstPtr& indices_msg,
114  const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr& coefficients_msg)
115  {
116  vital_checker_->poke();
117  if (polygons_msg->polygons.size() != coefficients_msg->coefficients.size()) {
118  NODELET_ERROR("The size of polygons and coefficients are not same");
119  return;
120  }
121  jsk_recognition_msgs::PolygonArray flipped_polygons = *polygons_msg;
122  jsk_recognition_msgs::ModelCoefficientsArray flipped_coefficients;
123  jsk_recognition_msgs::ClusterPointIndices flipped_indices;
124  flipped_polygons.polygons.clear();
125  flipped_coefficients.header = coefficients_msg->header;
126  flipped_indices.header = indices_msg->header;
127  try {
128  for (size_t i = 0; i < polygons_msg->polygons.size(); i++) {
129  geometry_msgs::PolygonStamped target_polygon = polygons_msg->polygons[i];
130  PCLModelCoefficientMsg target_coefficients = coefficients_msg->coefficients[i];
131  PCLIndicesMsg target_indices = indices_msg->cluster_indices[i];
132  tf::StampedTransform tf_transform
134  tf_listener_, target_coefficients.header.frame_id,
135  sensor_frame_, target_coefficients.header.stamp,
136  ros::Duration(1.0));
137  Eigen::Affine3f sensor_transform;
138  tf::transformTFToEigen(tf_transform, sensor_transform);
139  {
140  // poygons
142  Eigen::Vector3f polygon_normal = convex.getNormal();
143  if (polygon_normal.dot(Eigen::Vector3f(sensor_transform.translation()))
144  < 0) {
145  geometry_msgs::PolygonStamped flipped_polygon;
146  std::reverse_copy(
147  target_polygon.polygon.points.begin(),
148  target_polygon.polygon.points.end(),
149  std::back_inserter(flipped_polygon.polygon.points));
150  flipped_polygon.header = target_polygon.header;
151  flipped_polygons.polygons.push_back(flipped_polygon);
152  }
153  else {
154  flipped_polygons.polygons.push_back(target_polygon);
155  }
156  }
157 
158  {
159  // coefficients
160  Eigen::Vector3f local_normal(target_coefficients.values[0],
161  target_coefficients.values[1],
162  target_coefficients.values[2]);
163  if (local_normal.dot(Eigen::Vector3f(sensor_transform.translation()))
164  < 0) {
165  PCLModelCoefficientMsg the_flipped_coefficients;
166  PCLIndicesMsg the_flipped_indices;
167  for (size_t j = 0; j < target_coefficients.values.size(); j++) {
168  the_flipped_coefficients.values.push_back(
169  - target_coefficients.values[j]);
170  }
171  std::reverse_copy(target_indices.indices.begin(), target_indices.indices.end(),
172  std::back_inserter(the_flipped_indices.indices));
173  the_flipped_coefficients.header = target_coefficients.header;
174  the_flipped_indices.header = target_indices.header;
175  flipped_coefficients.coefficients.push_back(the_flipped_coefficients);
176  //flipped_coefficients.coefficients.push_back(target_coefficients);
177  flipped_indices.cluster_indices.push_back(the_flipped_indices);
178  //flipped_indices.cluster_indices.push_back(target_indices);
179  }
180  else {
181  // no need to flip
182  flipped_coefficients.coefficients.push_back(target_coefficients);
183  flipped_indices.cluster_indices.push_back(target_indices);
184  }
185  }
186  }
187  pub_polygons_.publish(flipped_polygons);
188  pub_coefficients_.publish(flipped_coefficients);
189  if (use_indices_)
190  pub_indices_.publish(flipped_indices);
191  }
192  catch (tf2::TransformException& e) {
193  NODELET_ERROR("Failed to lookup transformation: %s", e.what());
194  }
195  }
196 }
197 
polygon_flipper.h
jsk_pcl_ros_utils
Definition: add_point_indices.h:51
NODELET_FATAL
#define NODELET_FATAL(...)
NODELET_ERROR
#define NODELET_ERROR(...)
_2
boost::arg< 2 > _2
jsk_recognition_utils::ConvexPolygon
_3
boost::arg< 3 > _3
i
int i
jsk_pcl_ros_utils::PolygonFlipper::subscribe
virtual void subscribe()
Definition: polygon_flipper_nodelet.cpp:78
jsk_recognition_utils::ConvexPolygon::fromROSMsg
static ConvexPolygon fromROSMsg(const geometry_msgs::Polygon &polygon)
jsk_pcl_ros_utils::PolygonFlipper::flip
virtual void flip(const jsk_recognition_msgs::PolygonArray::ConstPtr &polygon_msg, const jsk_recognition_msgs::ClusterPointIndices::ConstPtr &indices_msg, const jsk_recognition_msgs::ModelCoefficientsArray::ConstPtr &coefficients_msg)
Definition: polygon_flipper_nodelet.cpp:111
PCLIndicesMsg
pcl::PointIndices PCLIndicesMsg
jsk_pcl_ros_utils::PolygonFlipper::~PolygonFlipper
virtual ~PolygonFlipper()
Definition: polygon_flipper_nodelet.cpp:67
jsk_pcl_ros_utils::PolygonFlipper::pub_polygons_
ros::Publisher pub_polygons_
Definition: polygon_flipper.h:146
tf::StampedTransform
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
message_filters::Subscriber::unsubscribe
void unsubscribe()
class_list_macros.h
PCLModelCoefficientMsg
pcl::ModelCoefficients PCLModelCoefficientMsg
jsk_pcl_ros_utils::PolygonFlipper::unsubscribe
virtual void unsubscribe()
Definition: polygon_flipper_nodelet.cpp:93
tf::transformTFToEigen
void transformTFToEigen(const tf::Transform &t, Eigen::Affine3f &eigen)
jsk_pcl_ros_utils::PolygonFlipper::sync_
boost::shared_ptr< message_filters::Synchronizer< SyncPolicy > > sync_
Definition: polygon_flipper.h:141
_1
boost::arg< 1 > _1
jsk_pcl_ros_utils::PolygonFlipper
Definition: polygon_flipper.h:87
message_filters::SimpleFilter::registerCallback
Connection registerCallback(const boost::function< void(P)> &callback)
message_filters::PassThrough::add
void add(const EventType &evt)
jsk_pcl_ros_utils::PolygonFlipper::sub_polygons_
message_filters::Subscriber< jsk_recognition_msgs::PolygonArray > sub_polygons_
Definition: polygon_flipper.h:142
pcl_conversion_util.h
jsk_pcl_ros_utils::PolygonFlipper::sub_indices_
message_filters::Subscriber< jsk_recognition_msgs::ClusterPointIndices > sub_indices_
Definition: polygon_flipper.h:144
jsk_pcl_ros_utils::PolygonFlipper::sub_coefficients_
message_filters::Subscriber< jsk_recognition_msgs::ModelCoefficientsArray > sub_coefficients_
Definition: polygon_flipper.h:143
jsk_pcl_ros_utils::PolygonFlipper::sensor_frame_
std::string sensor_frame_
Definition: polygon_flipper.h:150
message_filters::Subscriber::subscribe
void subscribe()
nodelet::Nodelet
jsk_pcl_ros_utils::PolygonFlipper::fillEmptyIndices
virtual void fillEmptyIndices(const jsk_recognition_msgs::PolygonArray::ConstPtr &polygon_msg)
Definition: polygon_flipper_nodelet.cpp:101
jsk_pcl_ros_utils::PolygonFlipper::use_indices_
bool use_indices_
Definition: polygon_flipper.h:151
jsk_pcl_ros_utils::PolygonFlipper::tf_listener_
tf::TransformListener * tf_listener_
Definition: polygon_flipper.h:149
jsk_pcl_ros_utils::PolygonFlipper::onInit
virtual void onInit()
Definition: polygon_flipper_nodelet.cpp:44
jsk_pcl_ros_utils::PolygonFlipper::pub_indices_
ros::Publisher pub_indices_
Definition: polygon_flipper.h:147
jsk_pcl_ros_utils::PolygonFlipper::pub_coefficients_
ros::Publisher pub_coefficients_
Definition: polygon_flipper.h:148
jsk_pcl_ros_utils::PolygonFlipper::sub_indices_null_
message_filters::PassThrough< jsk_recognition_msgs::ClusterPointIndices > sub_indices_null_
Definition: polygon_flipper.h:145
jsk_recognition_utils::TfListenerSingleton::getInstance
static tf::TransformListener * getInstance()
jsk_recognition_utils::lookupTransformWithDuration
tf::StampedTransform lookupTransformWithDuration(tf::TransformListener *listener, const std::string &to_frame, const std::string &from_frame, const ros::Time &stamp, ros::Duration duration)
tf2::TransformException
ros::Duration
jsk_pcl_ros_utils::PolygonFlipper::queue_size_
int queue_size_
Definition: polygon_flipper.h:152
jsk_recognition_utils::Plane::getNormal
virtual Eigen::Vector3f getNormal()
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros_utils::PolygonFlipper, nodelet::Nodelet)


jsk_pcl_ros_utils
Author(s): Yohei Kakiuchi
autogenerated on Fri May 16 2025 03:11:43