moveit2
The MoveIt Motion Planning Framework for ROS 2.
collision_common_distance_field.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
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/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage 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 /* Author: E. Gil Jones */
36 
38 #include <rclcpp/duration.hpp>
39 #include <rclcpp/logger.hpp>
40 #include <rclcpp/logging.hpp>
41 #include <rclcpp/time.hpp>
42 #include <tf2_eigen/tf2_eigen.hpp>
43 #include <memory>
44 #include <mutex>
45 
46 namespace collision_detection
47 {
48 static const rclcpp::Logger LOGGER =
49  rclcpp::get_logger("moveit_collision_distance_field.collision_common_distance_field");
51 {
52  using Comperator = std::owner_less<shapes::ShapeConstWeakPtr>;
53  using Map = std::map<shapes::ShapeConstWeakPtr, BodyDecompositionConstPtr, Comperator>;
54 
56  {
57  }
58  static const unsigned int MAX_CLEAN_COUNT = 100;
60  unsigned int clean_count_;
61  std::mutex lock_;
62 };
63 
65 {
66  static BodyDecompositionCache cache;
67  return cache;
68 }
69 
70 BodyDecompositionConstPtr getBodyDecompositionCacheEntry(const shapes::ShapeConstPtr& shape, double resolution)
71 {
72  // TODO - deal with changing resolution?
74  shapes::ShapeConstWeakPtr wptr(shape);
75  {
76  std::scoped_lock slock(cache.lock_);
77  BodyDecompositionCache::Map::const_iterator cache_it = cache.map_.find(wptr);
78  if (cache_it != cache.map_.end())
79  {
80  return cache_it->second;
81  }
82  }
83 
84  BodyDecompositionConstPtr bdcp = std::make_shared<const BodyDecomposition>(shape, resolution);
85  {
86  std::scoped_lock slock(cache.lock_);
87  cache.map_[wptr] = bdcp;
88  cache.clean_count_++;
89  return bdcp;
90  }
91  // TODO - clean cache
92 }
93 
94 PosedBodyPointDecompositionVectorPtr getCollisionObjectPointDecomposition(const collision_detection::World::Object& obj,
95  double resolution)
96 {
97  PosedBodyPointDecompositionVectorPtr ret = std::make_shared<PosedBodyPointDecompositionVector>();
98  for (unsigned int i = 0; i < obj.shapes_.size(); ++i)
99  {
100  PosedBodyPointDecompositionPtr pbd(
102  ret->addToVector(pbd);
103  ret->updatePose(ret->getSize() - 1, obj.global_shape_poses_[i]);
104  }
105  return ret;
106 }
107 
108 PosedBodySphereDecompositionVectorPtr getAttachedBodySphereDecomposition(const moveit::core::AttachedBody* att,
109  double resolution)
110 {
111  PosedBodySphereDecompositionVectorPtr ret = std::make_shared<PosedBodySphereDecompositionVector>();
112  for (unsigned int i = 0; i < att->getShapes().size(); ++i)
113  {
114  PosedBodySphereDecompositionPtr pbd(
116  pbd->updatePose(att->getGlobalCollisionBodyTransforms()[i]);
117  ret->addToVector(pbd);
118  }
119  return ret;
120 }
121 
122 PosedBodyPointDecompositionVectorPtr getAttachedBodyPointDecomposition(const moveit::core::AttachedBody* att,
123  double resolution)
124 {
125  PosedBodyPointDecompositionVectorPtr ret = std::make_shared<PosedBodyPointDecompositionVector>();
126  for (unsigned int i = 0; i < att->getShapes().size(); ++i)
127  {
128  PosedBodyPointDecompositionPtr pbd =
129  std::make_shared<PosedBodyPointDecomposition>(getBodyDecompositionCacheEntry(att->getShapes()[i], resolution));
130  ret->addToVector(pbd);
131  ret->updatePose(ret->getSize() - 1, att->getGlobalCollisionBodyTransforms()[i]);
132  }
133  return ret;
134 }
135 
136 void getBodySphereVisualizationMarkers(const GroupStateRepresentationConstPtr& gsr, const std::string& reference_frame,
137  visualization_msgs::msg::MarkerArray& body_marker_array)
138 {
139  // creating namespaces
140  std::string robot_ns = gsr->dfce_->group_name_ + "_sphere_decomposition";
141  std::string attached_ns = "attached_sphere_decomposition";
142 
143  // creating colors
144  std_msgs::msg::ColorRGBA robot_color;
145  robot_color.r = 0;
146  robot_color.b = 0.8f;
147  robot_color.g = 0;
148  robot_color.a = 0.5;
149 
150  std_msgs::msg::ColorRGBA attached_color;
151  attached_color.r = 1;
152  attached_color.g = 1;
153  attached_color.b = 0;
154  attached_color.a = 0.5;
155 
156  // creating sphere marker
157  visualization_msgs::msg::Marker sphere_marker;
158  sphere_marker.header.frame_id = reference_frame;
159  sphere_marker.header.stamp = rclcpp::Time(0);
160  sphere_marker.ns = robot_ns;
161  sphere_marker.id = 0;
162  sphere_marker.type = visualization_msgs::msg::Marker::SPHERE;
163  sphere_marker.action = visualization_msgs::msg::Marker::ADD;
164  sphere_marker.pose.orientation.x = 0;
165  sphere_marker.pose.orientation.y = 0;
166  sphere_marker.pose.orientation.z = 0;
167  sphere_marker.pose.orientation.w = 1;
168  sphere_marker.color = robot_color;
169  sphere_marker.lifetime = rclcpp::Duration(0, 0);
170 
171  const moveit::core::RobotState& state = *(gsr->dfce_->state_);
172  unsigned int id = 0;
173  for (unsigned int i = 0; i < gsr->dfce_->link_names_.size(); ++i)
174  {
175  const moveit::core::LinkModel* ls = state.getLinkModel(gsr->dfce_->link_names_[i]);
176  if (gsr->dfce_->link_has_geometry_[i])
177  {
178  gsr->link_body_decompositions_[i]->updatePose(state.getFrameTransform(ls->getName()));
179 
180  collision_detection::PosedBodySphereDecompositionConstPtr sphere_representation =
181  gsr->link_body_decompositions_[i];
182  for (unsigned int j = 0; j < sphere_representation->getCollisionSpheres().size(); ++j)
183  {
184  sphere_marker.pose.position = tf2::toMsg(sphere_representation->getSphereCenters()[j]);
185  sphere_marker.scale.x = sphere_marker.scale.y = sphere_marker.scale.z =
186  sphere_representation->getCollisionSpheres()[j].radius_;
187  sphere_marker.id = id;
188  id++;
189 
190  body_marker_array.markers.push_back(sphere_marker);
191  }
192  }
193  }
194 
195  sphere_marker.ns = attached_ns;
196  sphere_marker.color = attached_color;
197  for (unsigned int i = 0; i < gsr->dfce_->attached_body_names_.size(); ++i)
198  {
199  const moveit::core::AttachedBody* att = state.getAttachedBody(gsr->dfce_->attached_body_names_[i]);
200  if (!att)
201  {
202  RCLCPP_WARN(LOGGER,
203  "Attached body '%s' was not found, skipping sphere "
204  "decomposition visualization",
205  gsr->dfce_->attached_body_names_[i].c_str());
206  continue;
207  }
208 
209  if (gsr->attached_body_decompositions_[i]->getSize() != att->getShapes().size())
210  {
211  RCLCPP_WARN(LOGGER, "Attached body size discrepancy");
212  continue;
213  }
214 
215  for (unsigned int j = 0; j < att->getShapes().size(); ++j)
216  {
217  PosedBodySphereDecompositionVectorPtr sphere_decp = gsr->attached_body_decompositions_[i];
218  sphere_decp->updatePose(j, att->getGlobalCollisionBodyTransforms()[j]);
219 
220  sphere_marker.pose.position = tf2::toMsg(sphere_decp->getSphereCenters()[j]);
221  sphere_marker.scale.x = sphere_marker.scale.y = sphere_marker.scale.z =
222  sphere_decp->getCollisionSpheres()[j].radius_;
223  sphere_marker.id = id;
224  body_marker_array.markers.push_back(sphere_marker);
225  id++;
226  }
227  }
228 }
229 } // namespace collision_detection
Object defining bodies that can be attached to robot links.
Definition: attached_body.h:58
const EigenSTL::vector_Isometry3d & getGlobalCollisionBodyTransforms() const
Get the global transforms (in world frame) for the collision bodies. The returned transforms are guar...
const std::vector< shapes::ShapeConstPtr > & getShapes() const
Get the shapes that make up this attached body.
A link from the robot. Contains the constant transform applied to the link and its geometry.
Definition: link_model.h:72
const std::string & getName() const
The name of this link.
Definition: link_model.h:86
Representation of a robot's state. This includes position, velocity, acceleration and effort.
Definition: robot_state.h:90
const LinkModel * getLinkModel(const std::string &link) const
Get the model of a particular link.
Definition: robot_state.h:122
const Eigen::Isometry3d & getFrameTransform(const std::string &frame_id, bool *frame_found=nullptr)
Get the transformation matrix from the model frame (root of model) to the frame identified by frame_i...
const AttachedBody * getAttachedBody(const std::string &name) const
Get the attached body named name. Return nullptr if not found.
PosedBodyPointDecompositionVectorPtr getCollisionObjectPointDecomposition(const collision_detection::World::Object &obj, double resolution)
void getBodySphereVisualizationMarkers(const GroupStateRepresentationPtr &gsr, const std::string &reference_frame, visualization_msgs::msg::MarkerArray &body_marker_array)
BodyDecompositionCache & getBodyDecompositionCache()
PosedBodySphereDecompositionVectorPtr getAttachedBodySphereDecomposition(const moveit::core::AttachedBody *att, double resolution)
BodyDecompositionConstPtr getBodyDecompositionCacheEntry(const shapes::ShapeConstPtr &shape, double resolution)
PosedBodyPointDecompositionVectorPtr getAttachedBodyPointDecomposition(const moveit::core::AttachedBody *att, double resolution)
std::owner_less< shapes::ShapeConstWeakPtr > Comperator
std::map< shapes::ShapeConstWeakPtr, BodyDecompositionConstPtr, Comperator > Map
A representation of an object.
Definition: world.h:79
EigenSTL::vector_Isometry3d global_shape_poses_
The poses of the corresponding entries in shapes_, relative to the world frame.
Definition: world.h:106
std::vector< shapes::ShapeConstPtr > shapes_
All the shapes making up this object.
Definition: world.h:96