pointcloud_moveit_filter.h
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 
37 #ifndef JSK_PCL_ROS_POINTCLOUD_MOVEIT_FILTER_H_
38 #define JSK_PCL_ROS_POINTCLOUD_MOVEIT_FILTER_H_
39 
40 #include <ros/ros.h>
41 #include <tf/tf.h>
43 #include <sensor_msgs/PointCloud2.h>
44 #include <moveit/version.h>
48 #include <XmlRpcException.h>
49 #include <pcl/point_types.h>
50 #include <pcl/point_cloud.h>
52 #include <pcl/filters/extract_indices.h>
53 
54 #if (ROS_VERSION_MINIMUM(1,14,0) || MOVEIT_VERSION_MAJOR >= 1) // melodic or MoveIt 1.0
55 #include <tf2_ros/message_filter.h>
56 #else
57 #include <tf/message_filter.h>
58 #endif
59 
60 namespace jsk_pcl_ros
61 {
66  {
67  public:
69  virtual ~PointCloudMoveitFilter();
70 
71  virtual bool setParams(XmlRpc::XmlRpcValue &params);
72  virtual bool initialize();
73  virtual void start();
74  virtual void stop();
75  virtual ShapeHandle excludeShape(const shapes::ShapeConstPtr &shape);
76  virtual void forgetShape(ShapeHandle handle);
77 
78  protected:
79  virtual void stopHelper();
80  virtual bool getShapeTransform(ShapeHandle h,
81 #if (ROS_VERSION_MINIMUM(1,14,0) || MOVEIT_VERSION_MAJOR >= 1) // melodic or MoveIt 1.0
82  Eigen::Isometry3d &transform) const;
83 #else
84  Eigen::Affine3d &transform) const;
85 #endif
86  template <typename PointT>
88  const sensor_msgs::PointCloud2::ConstPtr &cloud_msg)
89  {
90  if (monitor_->getMapFrame().empty())
91  monitor_->setMapFrame(cloud_msg->header.frame_id);
92  tf::StampedTransform map_H_sensor;
93  if (monitor_->getMapFrame() == cloud_msg->header.frame_id) {
94  map_H_sensor.setIdentity();
95  }
96  else {
97  if (tf_) {
98  try
99  {
100 #if (ROS_VERSION_MINIMUM(1,14,0) || MOVEIT_VERSION_MAJOR >= 1) // melodic or MoveIt 1.0
102  cloud_msg->header.frame_id,
103  cloud_msg->header.stamp),
104  map_H_sensor);
105 #else
106  tf_->lookupTransform(monitor_->getMapFrame(),
107  cloud_msg->header.frame_id,
108  cloud_msg->header.stamp,
109  map_H_sensor);
110 #endif
111  }
112  catch (tf::TransformException& ex)
113  {
114  ROS_ERROR_STREAM("Transform error of sensor data: "
115  << ex.what() << "; quitting callback");
116  return;
117  }
118  }
119  else {
120  return;
121  }
122  }
123  /* convert cloud message to pcl cloud object */
124  //typename pcl::PointCloud<PointT>::Ptr cloud(pcl::PointCloud<PointT>);
125  typename pcl::PointCloud<PointT>::Ptr cloud;
126  cloud.reset(new pcl::PointCloud<PointT>());
127  pcl::PointCloud<pcl::PointXYZ> xyz_cloud;
128  pcl::fromROSMsg(*cloud_msg, *cloud);
129  pcl::fromROSMsg(*cloud_msg, xyz_cloud);
130  const tf::Vector3 &sensor_origin_tf = map_H_sensor.getOrigin();
131  octomap::point3d sensor_origin(
132  sensor_origin_tf.getX(),
133  sensor_origin_tf.getY(),
134  sensor_origin_tf.getZ());
135  Eigen::Vector3d sensor_origin_eigen(
136  sensor_origin_tf.getX(),
137  sensor_origin_tf.getY(),
138  sensor_origin_tf.getZ());
139  if (!updateTransformCache(cloud_msg->header.frame_id,
140  cloud_msg->header.stamp))
141  {
143  1, "Transform cache was not updated. Self-filtering may fail.");
144  return;
145  }
146  /* mask out points on the robot */
147 #if (MOVEIT_VERSION_MAJOR == 0 and MOVEIT_VERSION_MINOR < 6)
148  shape_mask_->maskContainment(xyz_cloud, sensor_origin_eigen, 0.0,
149  max_range_, mask_);
150 #else // from moveit 0.6 (indigo), PCL dependency is removed
151  shape_mask_->maskContainment(*cloud_msg, sensor_origin_eigen, 0.0,
152  max_range_, mask_);
153 #endif
154  typename pcl::PointCloud<PointT>::Ptr
155  filtered_cloud (new pcl::PointCloud<PointT>);
156  pcl::PointIndices::Ptr indices (new pcl::PointIndices);
157  // convert mask into indices
158  for (size_t i = 0; i < mask_.size(); i++) {
160  indices->indices.push_back(i);
161  }
162  }
163  pcl::ExtractIndices<PointT> ex;
164  ex.setInputCloud(cloud);
165  ex.setIndices(indices);
166  ex.setKeepOrganized(keep_organized_);
167  ex.filter(*filtered_cloud);
168  sensor_msgs::PointCloud2 ros_cloud;
169  pcl::toROSMsg(*filtered_cloud, ros_cloud);
170  ros_cloud.header = cloud_msg->header;
172  }
173 
175  // ROS variables
179 #if (ROS_VERSION_MINIMUM(1,14,0) || MOVEIT_VERSION_MAJOR >= 1) // melodic and MoveIt 1.0
180  std::shared_ptr<tf2_ros::Buffer> tf_;
181 #else
183 #endif
184  std::string point_cloud_topic_;
185  double scale_;
186  double padding_;
187  double max_range_;
188  unsigned int point_subsample_;
191 
193 #if (ROS_VERSION_MINIMUM(1,14,0) || MOVEIT_VERSION_MAJOR >= 1) // melodic or MoveIt 1.0
195 #else
197 #endif
198 
199  boost::scoped_ptr<point_containment_filter::ShapeMask> shape_mask_;
200  std::vector<int> mask_;
201 
204 
205  private:
206 
207  };
208 }
209 
210 #endif
boost::scoped_ptr< point_containment_filter::ShapeMask > shape_mask_
std::map< ShapeHandle, Eigen::Affine3d, std::less< ShapeHandle >, Eigen::aligned_allocator< std::pair< const ShapeHandle, Eigen::Affine3d > > > ShapeTransformCache
void publish(const boost::shared_ptr< M > &message) const
void setMapFrame(const std::string &frame)
virtual ShapeHandle excludeShape(const shapes::ShapeConstPtr &shape)
virtual void forgetShape(ShapeHandle handle)
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
TFSIMD_FORCE_INLINE const tfScalar & getY() const
occupancy_map_monitor::ShapeHandle ShapeHandle
TFSIMD_FORCE_INLINE const tfScalar & getZ() const
occupancy_map_monitor::ShapeTransformCache ShapeTransformCache
void setIdentity()
virtual bool setParams(XmlRpc::XmlRpcValue &params)
#define ROS_ERROR_THROTTLE(rate,...)
tf::MessageFilter< sensor_msgs::PointCloud2 > * point_cloud_filter_
unsigned int ShapeHandle
const std::string & getMapFrame() const
void toROSMsg(const pcl::PointCloud< T > &pcl_cloud, sensor_msgs::PointCloud2 &cloud)
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
boost::shared_ptr< tf::Transformer > tf_
message_filters::Subscriber< sensor_msgs::PointCloud2 > * point_cloud_subscriber_
virtual bool getShapeTransform(ShapeHandle h, Eigen::Affine3d &transform) const
TFSIMD_FORCE_INLINE const tfScalar & getX() const
bool updateTransformCache(const std::string &target_frame, const ros::Time &target_time)
#define ROS_ERROR_STREAM(args)
cloud
static void transformStampedMsgToTF(const geometry_msgs::TransformStamped &msg, StampedTransform &bt)
void cloudMsgCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud_msg)
std::shared_ptr< const Shape > ShapeConstPtr


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:47