pose_with_covariance_stamped_to_gaussian_pointcloud_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 
36 #define BOOST_PARAMETER_MAX_ARITY 7
37 
39 #include <sensor_msgs/PointCloud2.h>
41 
42 namespace jsk_pcl_ros_utils
43 {
45  {
46  DiagnosticNodelet::onInit();
47  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
48  typename dynamic_reconfigure::Server<Config>::CallbackType f =
50  srv_->setCallback (f);
51 
52  pub_ = advertise<sensor_msgs::PointCloud2>(*pnh_, "output", 1);
53 
55  }
56 
58  {
59  sub_ = pnh_->subscribe("input", 1, &PoseWithCovarianceStampedToGaussianPointCloud::convert, this);
60  }
61 
63  {
64  sub_.shutdown();
65  }
66 
67  float PoseWithCovarianceStampedToGaussianPointCloud::gaussian(const Eigen::Vector2f& input,
68  const Eigen::Vector2f& mean,
69  const Eigen::Matrix2f& S,
70  const Eigen::Matrix2f& S_inv)
71  {
72  Eigen::Vector2f diff = input - mean;
73  if (normalize_method_ == "normalize_area") {
74  return normalize_value_ * 1 / (2 * M_PI * sqrt(S.determinant())) * exp(- 0.5 * (diff.transpose() * S_inv * diff)[0]);
75  }
76  else if (normalize_method_ == "normalize_height") {
77  return normalize_value_ * exp(- 0.5 * (diff.transpose() * S_inv * diff)[0]);
78  }
79  }
80 
81  void PoseWithCovarianceStampedToGaussianPointCloud::convert(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& msg)
82  {
83  boost::mutex::scoped_lock lock(mutex_);
84  vital_checker_->poke();
85  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
86  Eigen::Vector2f mean;
87  Eigen::Matrix2f S;
88  if (cut_plane_ == "xy" || cut_plane_ == "flipped_xy") {
89  mean = Eigen::Vector2f(msg->pose.pose.position.x, msg->pose.pose.position.y);
90  S << msg->pose.covariance[0], msg->pose.covariance[1],
91  msg->pose.covariance[6], msg->pose.covariance[7];
92  }
93  else if (cut_plane_ == "yz" || cut_plane_ == "flipped_yz") {
94  mean = Eigen::Vector2f(msg->pose.pose.position.y, msg->pose.pose.position.z);
95  S << msg->pose.covariance[7], msg->pose.covariance[8],
96  msg->pose.covariance[13], msg->pose.covariance[14];
97  }
98  else if (cut_plane_ == "zx" || cut_plane_ == "flipped_zx") {
99  mean = Eigen::Vector2f(msg->pose.pose.position.z, msg->pose.pose.position.x);
100  S << msg->pose.covariance[14], msg->pose.covariance[12],
101  msg->pose.covariance[0], msg->pose.covariance[2];
102  }
103  double max_sigma = 0;
104  for (size_t i = 0; i < 2; i++) {
105  for (size_t j = 0; j < 2; j++) {
106  double sigma = sqrt(S(i, j));
107  if (max_sigma < sigma) {
108  max_sigma = sigma;
109  }
110  }
111  }
112  Eigen::Matrix2f S_inv = S.inverse();
113  double dx = 6.0 * max_sigma / sampling_num_;
114  double dy = 6.0 * max_sigma / sampling_num_;
115  for (double x = - 3.0 * max_sigma; x <= 3.0 * max_sigma; x += dx) {
116  for (double y = - 3.0 * max_sigma; y <= 3.0 * max_sigma; y += dy) {
117  Eigen::Vector2f diff(x, y);
118  Eigen::Vector2f input = diff + mean;
119  float z = gaussian(input, mean, S, S_inv);
120  pcl::PointXYZ p;
121  if (cut_plane_ == "xy") {
122  p.x = input[0];
123  p.y = input[1];
124  p.z = z + msg->pose.pose.position.z;
125  }
126  else if (cut_plane_ == "yz") {
127  p.y = input[0];
128  p.z = input[1];
129  p.x = z + msg->pose.pose.position.x;
130  }
131  else if (cut_plane_ == "zx") {
132  p.z = input[0];
133  p.x = input[1];
134  p.y = z + msg->pose.pose.position.y;
135  }
136  else if (cut_plane_ == "flipped_xy") {
137  p.x = input[0];
138  p.y = input[1];
139  p.z = -z + msg->pose.pose.position.z;
140  }
141  else if (cut_plane_ == "flipped_yz") {
142  p.y = input[0];
143  p.z = input[1];
144  p.x = -z + msg->pose.pose.position.x;
145  }
146  else if (cut_plane_ == "flipped_zx") {
147  p.z = input[0];
148  p.x = input[1];
149  p.y = -z + msg->pose.pose.position.y;
150  }
151  cloud->points.push_back(p);
152  }
153  }
154  sensor_msgs::PointCloud2 ros_cloud;
155  pcl::toROSMsg(*cloud, ros_cloud);
156  ros_cloud.header = msg->header;
157  pub_.publish(ros_cloud);
158  }
159 
161  {
162  boost::mutex::scoped_lock lock(mutex_);
163  cut_plane_ = config.cut_plane;
164  sampling_num_ = config.sampling_num;
165  normalize_value_ = config.normalize_value;
166  normalize_method_ = config.normalize_method;
167  }
168 }
169 
f
lock
virtual float gaussian(const Eigen::Vector2f &input, const Eigen::Vector2f &mean, const Eigen::Matrix2f &S, const Eigen::Matrix2f &S_inv)
void publish(const boost::shared_ptr< M > &message) const
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
#define M_PI
virtual void convert(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &msg)
double y
boost::shared_ptr< ros::NodeHandle > pnh_
double z
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
void toROSMsg(const pcl::PointCloud< T > &pcl_cloud, sensor_msgs::PointCloud2 &cloud)
INLINE Rall1d< T, V, S > exp(const Rall1d< T, V, S > &arg)
jsk_topic_tools::VitalChecker::Ptr vital_checker_
p
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros_utils::PoseWithCovarianceStampedToGaussianPointCloud, nodelet::Nodelet)
double x


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