interactive_cuboid_likelihood_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/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
40 
41 namespace jsk_pcl_ros
42 {
44  {
45  DiagnosticNodelet::onInit();
47  pub_ = pnh_->advertise<std_msgs::Float32>("output", 1);
48  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
49  pnh_->param("frame_id", frame_id_, std::string("odom"));
50  pnh_->param("sensor_frame", sensor_frame_, std::string("odom"));
51 
52  std::vector<double> initial_pos;
53  std::vector<double> initial_rot;
54  if (!jsk_topic_tools::readVectorParameter(*pnh_, "initial_pos", initial_pos)) {
55  initial_pos.push_back(0);
56  initial_pos.push_back(0);
57  initial_pos.push_back(0);
58  }
59  if (!jsk_topic_tools::readVectorParameter(*pnh_, "initial_rot", initial_rot)) {
60  initial_rot.push_back(0);
61  initial_rot.push_back(0);
62  initial_rot.push_back(0);
63  }
64  particle_.x = initial_pos[0];
65  particle_.y = initial_pos[1];
66  particle_.z = initial_pos[2];
67  particle_.roll = initial_rot[0];
68  particle_.pitch = initial_rot[1];
69  particle_.yaw = initial_rot[2];
70  typename dynamic_reconfigure::Server<Config>::CallbackType f =
71  boost::bind (&InteractiveCuboidLikelihood::configCallback, this, _1, _2);
72  srv_->setCallback (f);
73  sub_ = pnh_->subscribe("input", 1, &InteractiveCuboidLikelihood::likelihood, this);
74  // Cuboid
76  visualization_msgs::InteractiveMarker int_marker = particleToInteractiveMarker(particle_);
77  server_->insert(int_marker, boost::bind(&InteractiveCuboidLikelihood::processFeedback, this, _1));
78  server_->applyChanges();
79 
80  // SupportPlane
82  plane_pose_ = Eigen::Affine3f::Identity();
83  visualization_msgs::InteractiveMarker plane_int_marker = planeInteractiveMarker();
84  plane_server_->insert(plane_int_marker, boost::bind(&InteractiveCuboidLikelihood::processPlaneFeedback, this, _1));
85  plane_server_->applyChanges();
87  }
88 
90  {
91 
92  }
93 
95  {
96 
97  }
98 
100  const visualization_msgs::InteractiveMarkerFeedback::ConstPtr& feedback)
101  {
102  boost::mutex::scoped_lock lock(mutex_);
103  Eigen::Affine3f pose;
104  tf::poseMsgToEigen(feedback->pose, pose);
105  particle_.fromEigen(pose);
106  }
107 
109  const visualization_msgs::InteractiveMarkerFeedback::ConstPtr& feedback)
110  {
111  boost::mutex::scoped_lock lock(mutex_);
112  tf::poseMsgToEigen(feedback->pose, plane_pose_);
113  }
114 
116  const sensor_msgs::PointCloud2::ConstPtr& msg)
117  {
118  boost::mutex::scoped_lock lock(mutex_);
119  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
120  pcl::fromROSMsg(*msg, *cloud);
121  tf::StampedTransform transform
122  = lookupTransformWithDuration(tf_, sensor_frame_, msg->header.frame_id,
123  ros::Time(0.0),
124  ros::Duration(0.0));
125  Eigen::Vector3f vp;
126  tf::vectorTFToEigen(transform.getOrigin(), vp);
128  vertices.push_back(Eigen::Vector3f(plane_pose_ * (Eigen::Vector3f::UnitX() + Eigen::Vector3f::UnitY())));
129  vertices.push_back(Eigen::Vector3f(plane_pose_ * (- Eigen::Vector3f::UnitX() + Eigen::Vector3f::UnitY())));
130  vertices.push_back(Eigen::Vector3f(plane_pose_ * (- Eigen::Vector3f::UnitX() - Eigen::Vector3f::UnitY())));
131  vertices.push_back(Eigen::Vector3f(plane_pose_ * (Eigen::Vector3f::UnitX() - Eigen::Vector3f::UnitY())));
132  Polygon::Ptr plane(new Polygon(vertices));
133  //particle_.plane = plane;
135  std::vector<Polygon::Ptr> polygons;
136  polygons.push_back(plane);
137  // for (size_t i = 0; i < vertices.size(); i++) {
138  // ROS_INFO("v: [%f, %f, %f]", vertices[i][0], vertices[i][1], vertices[i][2]);
139  // }
140  pcl::KdTreeFLANN<pcl::PointXYZ> tree;
141  tree.setInputCloud(cloud);
142  std::vector<float> polygon_likelihood(1, 1.0);
143  double l = computeLikelihood(particle_, cloud, tree, vp, polygons, polygon_likelihood, config_);
144  NODELET_INFO("likelihood: %f", l);
145  std_msgs::Float32 float_msg;
146  float_msg.data = l;
147  pub_.publish(float_msg);
148  }
149 
151  Config& config, uint32_t level)
152  {
153  boost::mutex::scoped_lock lock(mutex_);
154  config_ = config;
155  particle_.dx = config_.dx;
156  particle_.dy = config_.dy;
157  particle_.dz = config_.dz;
158  if (server_) {
159  visualization_msgs::InteractiveMarker int_marker = particleToInteractiveMarker(particle_);
160  server_->insert(int_marker, boost::bind(&InteractiveCuboidLikelihood::processFeedback, this, _1));
161  server_->applyChanges();
162  }
163  }
164 
165  visualization_msgs::InteractiveMarker
167  {
168  visualization_msgs::InteractiveMarker int_marker;
169  int_marker.header.frame_id = frame_id_;
170  int_marker.header.stamp = ros::Time::now();
171  int_marker.name = getName() + "_plane";
172  visualization_msgs::InteractiveMarkerControl control;
173  control.orientation.w = 1;
174  control.orientation.x = 1;
175  control.orientation.y = 0;
176  control.orientation.z = 0;
177  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
178  int_marker.controls.push_back(control);
179  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
180  int_marker.controls.push_back(control);
181 
182  control.orientation.w = 1;
183  control.orientation.x = 0;
184  control.orientation.y = 1;
185  control.orientation.z = 0;
186  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
187  int_marker.controls.push_back(control);
188  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
189  int_marker.controls.push_back(control);
190 
191  control.orientation.w = 1;
192  control.orientation.x = 0;
193  control.orientation.y = 0;
194  control.orientation.z = 1;
195  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
196  int_marker.controls.push_back(control);
197  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
198  int_marker.controls.push_back(control);
199  visualization_msgs::InteractiveMarkerControl box_control;
200  box_control.always_visible = true;
201  visualization_msgs::Marker plane_marker;
202  plane_marker.type = visualization_msgs::Marker::CUBE;
203  plane_marker.scale.x = 1.0;
204  plane_marker.scale.y = 1.0;
205  plane_marker.scale.z = 0.01;
206  plane_marker.color.r = 1.0;
207  plane_marker.color.a = 1.0;
208  plane_marker.pose.orientation.w = 1.0;
209  box_control.markers.push_back(plane_marker);
210  int_marker.controls.push_back(box_control);
211  return int_marker;
212  }
213 
214  visualization_msgs::Marker
216  const Particle& p)
217  {
218  visualization_msgs::Marker marker;
219  marker.type = visualization_msgs::Marker::CUBE;
220  marker.scale.x = p.dx;
221  marker.scale.y = p.dy;
222  marker.scale.z = p.dz;
223  marker.pose.orientation.w = 1.0;
224  marker.color.r = 0.5;
225  marker.color.g = 0.5;
226  marker.color.b = 0.5;
227  marker.color.a = 1.0;
228  return marker;
229  }
230 
231  visualization_msgs::InteractiveMarker InteractiveCuboidLikelihood::particleToInteractiveMarker(const Particle& p)
232  {
233  visualization_msgs::InteractiveMarker int_marker;
234  int_marker.header.frame_id = frame_id_;
235  int_marker.header.stamp = ros::Time::now();
236  int_marker.name = getName();
237  Eigen::Affine3f pose = particle_.toEigenMatrix();
238  tf::poseEigenToMsg(pose, int_marker.pose);
239 
240  visualization_msgs::InteractiveMarkerControl control;
241  control.orientation.w = 1;
242  control.orientation.x = 1;
243  control.orientation.y = 0;
244  control.orientation.z = 0;
245  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
246  int_marker.controls.push_back(control);
247  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
248  int_marker.controls.push_back(control);
249 
250  control.orientation.w = 1;
251  control.orientation.x = 0;
252  control.orientation.y = 1;
253  control.orientation.z = 0;
254  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
255  int_marker.controls.push_back(control);
256  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
257  int_marker.controls.push_back(control);
258 
259  control.orientation.w = 1;
260  control.orientation.x = 0;
261  control.orientation.y = 0;
262  control.orientation.z = 1;
263  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
264  int_marker.controls.push_back(control);
265  control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
266  int_marker.controls.push_back(control);
267  visualization_msgs::InteractiveMarkerControl box_control;
268  box_control.always_visible = true;
269  box_control.markers.push_back(particleToMarker(particle_));
270  int_marker.controls.push_back(box_control);
271  return int_marker;
272  }
273 
274 
275 
276 }
277 
void publish(const boost::shared_ptr< M > &message) const
virtual void processFeedback(const visualization_msgs::InteractiveMarkerFeedback::ConstPtr &feedback)
void poseEigenToMsg(const Eigen::Affine3d &e, geometry_msgs::Pose &m)
virtual visualization_msgs::Marker particleToMarker(const Particle &p)
void poseMsgToEigen(const geometry_msgs::Pose &m, Eigen::Affine3d &e)
tf::StampedTransform lookupTransformWithDuration(tf::TransformListener *listener, const std::string &to_frame, const std::string &from_frame, const ros::Time &stamp, ros::Duration duration)
virtual visualization_msgs::InteractiveMarker particleToInteractiveMarker(const Particle &p)
const std::string & getName() const
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
Eigen::Affine3f toEigenMatrix() const
boost::shared_ptr< interactive_markers::InteractiveMarkerServer > plane_server_
virtual void processPlaneFeedback(const visualization_msgs::InteractiveMarkerFeedback::ConstPtr &feedback)
pose
bool readVectorParameter(ros::NodeHandle &nh, const std::string &param_name, std::vector< double > &result)
long l
boost::shared_ptr< ros::NodeHandle > pnh_
virtual visualization_msgs::InteractiveMarker planeInteractiveMarker()
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros::InteractiveCuboidLikelihood, nodelet::Nodelet)
void fromEigen(const Eigen::Affine3f &pose)
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
std::vector< Eigen::Vector3f, Eigen::aligned_allocator< Eigen::Vector3f > > Vertices
void vectorTFToEigen(const tf::Vector3 &t, Eigen::Vector3d &e)
virtual void likelihood(const sensor_msgs::PointCloud2::ConstPtr &msg)
#define NODELET_INFO(...)
double computeLikelihood(const pcl::tracking::ParticleCuboid &p, pcl::PointCloud< pcl::PointXYZ >::ConstPtr cloud, pcl::KdTreeFLANN< pcl::PointXYZ > &tree, const Eigen::Vector3f &viewpoint, const std::vector< Polygon::Ptr > &polygons, const std::vector< float > &polygon_likelihood, const Config &config)
static Time now()
virtual void configCallback(Config &config, uint32_t level)
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
static tf::TransformListener * getInstance()
cloud
boost::shared_ptr< interactive_markers::InteractiveMarkerServer > server_
polygons


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