centroid_publisher_nodelet.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, Ryohei Ueda and JSK Lab
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/o2r other materials provided
16  * with the distribution.
17  * * Neither the name of the JSK Lab 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 
35 #define BOOST_PARAMETER_MAX_ARITY 7
37 
38 #include <geometry_msgs/PoseStamped.h>
39 #include <geometry_msgs/PoseArray.h>
40 #include <geometry_msgs/PointStamped.h>
41 
42 #include <algorithm>
43 #include <iomanip>
44 #include <sstream>
47 
48 namespace jsk_pcl_ros_utils
49 {
50  void CentroidPublisher::extract(const sensor_msgs::PointCloud2ConstPtr& input)
51  {
52  vital_checker_->poke();
53  pcl::PointCloud<pcl::PointXYZ> cloud_xyz;
54  pcl::fromROSMsg(*input, cloud_xyz);
55  Eigen::Vector4f center;
56  bool is_valid = pcl::compute3DCentroid(cloud_xyz, center);
57  if (publish_tf_ && is_valid) {
58  tf::Transform transform;
59  transform.setOrigin(tf::Vector3(center[0], center[1], center[2]));
61  br_.sendTransform(tf::StampedTransform(transform, input->header.stamp,
62  input->header.frame_id, frame_));
63  }
64  geometry_msgs::PoseStamped pose;
65  pose.pose.orientation.w = 1.0;
66  pose.pose.position.x = center[0];
67  pose.pose.position.y = center[1];
68  pose.pose.position.z = center[2];
69  pose.header = input->header;
70  pub_pose_.publish(pose);
71  geometry_msgs::PointStamped point;
72  point.point.x = center[0];
73  point.point.y = center[1];
74  point.point.z = center[2];
75  point.header = input->header;
76  pub_point_.publish(point);
77  }
78 
79  void CentroidPublisher::extractPolygons(const jsk_recognition_msgs::PolygonArray::ConstPtr& input)
80  {
81  vital_checker_->poke();
82 
83  int size = std::min(100, (int)input->polygons.size());
84 
85  geometry_msgs::PoseArray pose_array;
86  pose_array.header = input->header;
87  pose_array.poses.resize(size);
88 
89  for (size_t i = 0; i < size; ++i) {
91  = jsk_recognition_utils::Polygon::fromROSMsgPtr(input->polygons[i].polygon);
92  Eigen::Vector3f c = polygon->centroid(), n = polygon->getNormal();
93  Eigen::Vector3f ez;
94  ez << 0.0f, 0.0f, 1.0f;
95  Eigen::Quaternionf q;
96  q.setFromTwoVectors(ez, n);
97 
98  if (publish_tf_) {
99  std::stringstream ss;
100  ss << frame_ << std::setfill('0') << std::setw(2) << std::right << i;
101  tf::Transform transform;
102  transform.setOrigin(tf::Vector3(c.x(), c.y(), c.z()));
103  transform.setRotation(tf::Quaternion(q.x(), q.y(), q.z(), q.w()));
104  br_.sendTransform(tf::StampedTransform(transform, input->header.stamp,
105  input->header.frame_id, ss.str()));
106  }
107 
108  pose_array.poses[i].position.x = c.x();
109  pose_array.poses[i].position.y = c.y();
110  pose_array.poses[i].position.z = c.z();
111  pose_array.poses[i].orientation.x = q.x();
112  pose_array.poses[i].orientation.y = q.y();
113  pose_array.poses[i].orientation.z = q.z();
114  pose_array.poses[i].orientation.w = q.w();
115  }
116 
117  pub_pose_array_.publish(pose_array);
118  }
119 
121  {
122  sub_cloud_ = pnh_->subscribe("input", 1, &CentroidPublisher::extract, this);
123  sub_polygons_ = pnh_->subscribe("input/polygons", 1, &CentroidPublisher::extractPolygons, this);
124  }
125 
127  {
130  }
131 
133  {
134  DiagnosticNodelet::onInit();
135  pnh_->param("publish_tf", publish_tf_, false);
136  if (publish_tf_) {
137  if (!pnh_->getParam("frame", frame_))
138  {
139  ROS_WARN("~frame is not specified, using %s", getName().c_str());
140  frame_ = getName();
141  }
142  // do not use DiagnosticNodelet functionality when ~publish_tf is false
143  pub_pose_ = pnh_->advertise<geometry_msgs::PoseStamped>("output/pose", 1);
144  pub_point_ = pnh_->advertise<geometry_msgs::PointStamped>(
145  "output/point", 1);
146  pub_pose_array_ = pnh_->advertise<geometry_msgs::PoseArray>("output/pose_array", 1);
147  subscribe();
148  }
149  else {
150  pub_pose_ = advertise<geometry_msgs::PoseStamped>(*pnh_, "output/pose", 1);
151  pub_point_ = advertise<geometry_msgs::PointStamped>(
152  *pnh_, "output/point", 1);
153  pub_pose_array_ = advertise<geometry_msgs::PoseArray>(*pnh_, "output/pose_array", 1);
154  }
155 
157  }
158 }
159 
virtual void extractPolygons(const jsk_recognition_msgs::PolygonArray::ConstPtr &input)
TFSIMD_FORCE_INLINE void setRotation(const Quaternion &q)
void publish(const boost::shared_ptr< M > &message) const
virtual void extract(const sensor_msgs::PointCloud2ConstPtr &input)
const std::string & getName() const
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
pose
#define ROS_WARN(...)
static tf::Quaternion createIdentityQuaternion()
size
q
static Polygon::Ptr fromROSMsgPtr(const geometry_msgs::Polygon &polygon)
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros_utils::CentroidPublisher, nodelet::Nodelet)
boost::shared_ptr< ros::NodeHandle > pnh_
void sendTransform(const StampedTransform &transform)
double x
jsk_topic_tools::VitalChecker::Ptr vital_checker_
c
TFSIMD_FORCE_INLINE void setOrigin(const Vector3 &origin)


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