moveit2
The MoveIt Motion Planning Framework for ROS 2.
collision_tools.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, 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 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: Ioan Sucan */
36 
38 #include <rclcpp/clock.hpp>
39 #include <rclcpp/time.hpp>
40 #include <tf2_eigen/tf2_eigen.hpp>
41 
42 namespace collision_detection
43 {
44 void getCostMarkers(visualization_msgs::msg::MarkerArray& arr, const std::string& frame_id,
45  std::set<CostSource>& cost_sources)
46 {
47  std_msgs::msg::ColorRGBA color;
48  color.r = 1.0f;
49  color.g = 0.5f;
50  color.b = 0.0f;
51  color.a = 0.4f;
52  getCostMarkers(arr, frame_id, cost_sources, color, rclcpp::Duration(60, 0));
53 }
54 
55 void getCollisionMarkersFromContacts(visualization_msgs::msg::MarkerArray& arr, const std::string& frame_id,
56  const CollisionResult::ContactMap& con)
57 {
58  std_msgs::msg::ColorRGBA color;
59  color.r = 1.0f;
60  color.g = 0.0f;
61  color.b = 0.0f;
62  color.a = 0.8f;
63  getCollisionMarkersFromContacts(arr, frame_id, con, color, rclcpp::Duration(60, 0));
64 }
65 
66 void getCostMarkers(visualization_msgs::msg::MarkerArray& arr, const std::string& frame_id,
67  std::set<CostSource>& cost_sources, const std_msgs::msg::ColorRGBA& color,
68  const rclcpp::Duration& lifetime)
69 {
70  int id = 0;
71  for (const auto& cost_source : cost_sources)
72  {
73  visualization_msgs::msg::Marker mk;
74  mk.header.stamp = rclcpp::Clock(RCL_ROS_TIME).now();
75  mk.header.frame_id = frame_id;
76  mk.ns = "cost_source";
77  mk.id = id++;
78  mk.type = visualization_msgs::msg::Marker::CUBE;
79  mk.action = visualization_msgs::msg::Marker::ADD;
80  mk.pose.position.x = (cost_source.aabb_max[0] + cost_source.aabb_min[0]) / 2.0;
81  mk.pose.position.y = (cost_source.aabb_max[1] + cost_source.aabb_min[1]) / 2.0;
82  mk.pose.position.z = (cost_source.aabb_max[2] + cost_source.aabb_min[2]) / 2.0;
83  mk.pose.orientation.x = 0.0;
84  mk.pose.orientation.y = 0.0;
85  mk.pose.orientation.z = 0.0;
86  mk.pose.orientation.w = 1.0;
87  mk.scale.x = cost_source.aabb_max[0] - cost_source.aabb_min[0];
88  mk.scale.y = cost_source.aabb_max[1] - cost_source.aabb_min[1];
89  mk.scale.z = cost_source.aabb_max[2] - cost_source.aabb_min[2];
90  mk.color = color;
91  if (mk.color.a == 0.0)
92  mk.color.a = 1.0;
93  mk.lifetime = lifetime;
94  arr.markers.push_back(mk);
95  }
96 }
97 
98 void getCollisionMarkersFromContacts(visualization_msgs::msg::MarkerArray& arr, const std::string& frame_id,
99  const CollisionResult::ContactMap& con, const std_msgs::msg::ColorRGBA& color,
100  const rclcpp::Duration& lifetime, double radius)
101 
102 {
103  std::map<std::string, unsigned> ns_counts;
104 
105  for (const auto& collision : con)
106  {
107  for (const auto& contact : collision.second)
108  {
109  std::string ns_name = contact.body_name_1 + "=" + contact.body_name_2;
110  if (ns_counts.find(ns_name) == ns_counts.end())
111  ns_counts[ns_name] = 0;
112  else
113  ns_counts[ns_name]++;
114  visualization_msgs::msg::Marker mk;
115  mk.header.stamp = rclcpp::Clock(RCL_ROS_TIME).now();
116  mk.header.frame_id = frame_id;
117  mk.ns = ns_name;
118  mk.id = ns_counts[ns_name];
119  mk.type = visualization_msgs::msg::Marker::SPHERE;
120  mk.action = visualization_msgs::msg::Marker::ADD;
121  mk.pose.position.x = contact.pos.x();
122  mk.pose.position.y = contact.pos.y();
123  mk.pose.position.z = contact.pos.z();
124  mk.pose.orientation.x = 0.0;
125  mk.pose.orientation.y = 0.0;
126  mk.pose.orientation.z = 0.0;
127  mk.pose.orientation.w = 1.0;
128  mk.scale.x = mk.scale.y = mk.scale.z = radius * 2.0;
129  mk.color = color;
130  if (mk.color.a == 0.0)
131  mk.color.a = 1.0;
132  mk.lifetime = lifetime;
133  arr.markers.push_back(mk);
134  }
135  }
136 }
137 
138 bool getSensorPositioning(geometry_msgs::msg::Point& point, const std::set<CostSource>& cost_sources)
139 {
140  if (cost_sources.empty())
141  return false;
142  auto it = cost_sources.begin();
143  for (std::size_t i = 0; i < 4 * cost_sources.size() / 5; ++i)
144  ++it;
145  point.x = (it->aabb_max[0] + it->aabb_min[0]) / 2.0;
146  point.y = (it->aabb_max[1] + it->aabb_min[1]) / 2.0;
147  point.z = (it->aabb_max[2] + it->aabb_min[2]) / 2.0;
148  return true;
149 }
150 
151 double getTotalCost(const std::set<CostSource>& cost_sources)
152 {
153  double cost = 0.0;
154  for (const auto& cost_source : cost_sources)
155  cost += cost_source.getVolume() * cost_source.cost;
156  return cost;
157 }
158 
159 void intersectCostSources(std::set<CostSource>& cost_sources, const std::set<CostSource>& a,
160  const std::set<CostSource>& b)
161 {
162  cost_sources.clear();
163  CostSource tmp;
164  for (const auto& source_a : a)
165  for (const auto& source_b : b)
166  {
167  tmp.aabb_min[0] = std::max(source_a.aabb_min[0], source_b.aabb_min[0]);
168  tmp.aabb_min[1] = std::max(source_a.aabb_min[1], source_b.aabb_min[1]);
169  tmp.aabb_min[2] = std::max(source_a.aabb_min[2], source_b.aabb_min[2]);
170 
171  tmp.aabb_max[0] = std::min(source_a.aabb_max[0], source_b.aabb_max[0]);
172  tmp.aabb_max[1] = std::min(source_a.aabb_max[1], source_b.aabb_max[1]);
173  tmp.aabb_max[2] = std::min(source_a.aabb_max[2], source_b.aabb_max[2]);
174 
175  if (tmp.aabb_min[0] >= tmp.aabb_max[0] || tmp.aabb_min[1] >= tmp.aabb_max[1] || tmp.aabb_min[2] >= tmp.aabb_max[2])
176  continue;
177  tmp.cost = std::max(source_a.cost, source_b.cost);
178  cost_sources.insert(tmp);
179  }
180 }
181 
182 void removeOverlapping(std::set<CostSource>& cost_sources, const double overlap_fraction)
183 {
184  double p[3], q[3];
185  for (auto it = cost_sources.begin(); it != cost_sources.end(); ++it)
186  {
187  const double vol = it->getVolume() * overlap_fraction;
188  std::vector<std::set<CostSource>::iterator> remove;
189  auto it1 = it;
190  for (auto jt = ++it1; jt != cost_sources.end(); ++jt)
191  {
192  p[0] = std::max(it->aabb_min[0], jt->aabb_min[0]);
193  p[1] = std::max(it->aabb_min[1], jt->aabb_min[1]);
194  p[2] = std::max(it->aabb_min[2], jt->aabb_min[2]);
195 
196  q[0] = std::min(it->aabb_max[0], jt->aabb_max[0]);
197  q[1] = std::min(it->aabb_max[1], jt->aabb_max[1]);
198  q[2] = std::min(it->aabb_max[2], jt->aabb_max[2]);
199 
200  if (p[0] >= q[0] || p[1] >= q[1] || p[2] >= q[2])
201  continue;
202 
203  const double intersect_volume = (q[0] - p[0]) * (q[1] - p[1]) * (q[2] - p[2]);
204  if (intersect_volume >= vol)
205  remove.push_back(jt);
206  }
207  for (auto& r : remove)
208  cost_sources.erase(r);
209  }
210 }
211 
212 void removeCostSources(std::set<CostSource>& cost_sources, const std::set<CostSource>& cost_sources_to_remove,
213  const double overlap_fraction)
214 {
215  // remove all the boxes that overlap with the intersection previously computed in \e rem
216  double p[3], q[3];
217  for (const auto& source_remove : cost_sources_to_remove)
218  {
219  std::vector<std::set<CostSource>::iterator> remove;
220  std::set<CostSource> add;
221  for (auto it = cost_sources.begin(); it != cost_sources.end(); ++it)
222  {
223  p[0] = std::max(it->aabb_min[0], source_remove.aabb_min[0]);
224  p[1] = std::max(it->aabb_min[1], source_remove.aabb_min[1]);
225  p[2] = std::max(it->aabb_min[2], source_remove.aabb_min[2]);
226 
227  q[0] = std::min(it->aabb_max[0], source_remove.aabb_max[0]);
228  q[1] = std::min(it->aabb_max[1], source_remove.aabb_max[1]);
229  q[2] = std::min(it->aabb_max[2], source_remove.aabb_max[2]);
230 
231  if (p[0] >= q[0] || p[1] >= q[1] || p[2] >= q[2])
232  continue;
233 
234  const double intersect_volume = (q[0] - p[0]) * (q[1] - p[1]) * (q[2] - p[2]);
235  if (intersect_volume >= it->getVolume() * overlap_fraction)
236  remove.push_back(it);
237  else
238  {
239  // there is some overlap, but not too large, so we split the cost source into multiple ones
240  for (int i = 0; i < 3; ++i)
241  {
242  // is there a box above axis i in the intersection?
243  if (it->aabb_max[i] > q[i])
244  {
245  CostSource cs = *it;
246  cs.aabb_min[i] = q[i];
247  add.insert(cs);
248  }
249  // is there a box below axis i in the intersection?
250  if (it->aabb_min[i] < p[i])
251  {
252  CostSource cs = *it;
253  cs.aabb_max[i] = p[i];
254  add.insert(cs);
255  }
256  }
257  }
258  }
259  for (auto& r : remove)
260  cost_sources.erase(r);
261  cost_sources.insert(add.begin(), add.end());
262  }
263 }
264 
265 void costSourceToMsg(const CostSource& cost_source, moveit_msgs::msg::CostSource& msg)
266 {
267  msg.cost_density = cost_source.cost;
268  msg.aabb_min.x = cost_source.aabb_min[0];
269  msg.aabb_min.y = cost_source.aabb_min[1];
270  msg.aabb_min.z = cost_source.aabb_min[2];
271  msg.aabb_max.x = cost_source.aabb_max[0];
272  msg.aabb_max.y = cost_source.aabb_max[1];
273  msg.aabb_max.z = cost_source.aabb_max[2];
274 }
275 
276 void contactToMsg(const Contact& contact, moveit_msgs::msg::ContactInformation& msg)
277 {
278  msg.position = tf2::toMsg(contact.pos);
279  msg.normal = tf2::toMsg2(contact.normal);
280  msg.depth = contact.depth;
281  msg.contact_body_1 = contact.body_name_1;
282  msg.contact_body_2 = contact.body_name_2;
283  if (contact.body_type_1 == BodyTypes::ROBOT_LINK)
285  else if (contact.body_type_1 == BodyTypes::ROBOT_ATTACHED)
287  else
289  if (contact.body_type_2 == BodyTypes::ROBOT_LINK)
291  else if (contact.body_type_2 == BodyTypes::ROBOT_ATTACHED)
293  else
295 }
296 
297 } // end of namespace collision_detection
@ ROBOT_LINK
A link on the robot.
@ ROBOT_ATTACHED
A body attached to a robot link.
@ WORLD_OBJECT
A body in the environment.
bool getSensorPositioning(geometry_msgs::msg::Point &point, const std::set< CostSource > &cost_sources)
double getTotalCost(const std::set< CostSource > &cost_sources)
void getCollisionMarkersFromContacts(visualization_msgs::msg::MarkerArray &arr, const std::string &frame_id, const CollisionResult::ContactMap &con, const std_msgs::msg::ColorRGBA &color, const rclcpp::Duration &lifetime, const double radius=0.035)
void getCostMarkers(visualization_msgs::msg::MarkerArray &arr, const std::string &frame_id, std::set< CostSource > &cost_sources)
void intersectCostSources(std::set< CostSource > &cost_sources, const std::set< CostSource > &a, const std::set< CostSource > &b)
void removeCostSources(std::set< CostSource > &cost_sources, const std::set< CostSource > &cost_sources_to_remove, double overlap_fraction)
void contactToMsg(const Contact &contact, moveit_msgs::msg::ContactInformation &msg)
void removeOverlapping(std::set< CostSource > &cost_sources, double overlap_fraction)
void costSourceToMsg(const CostSource &cost_source, moveit_msgs::msg::CostSource &msg)
frame_id
Definition: pick.py:63
p
Definition: pick.py:62
a
Definition: plan.py:54
r
Definition: plan.py:56
std::map< std::pair< std::string, std::string >, std::vector< Contact > > ContactMap
Definition of a contact point.
std::string body_name_2
The id of the second body involved in the contact.
std::string body_name_1
The id of the first body involved in the contact.
BodyType body_type_1
The type of the first body involved in the contact.
BodyType body_type_2
The type of the second body involved in the contact.
Eigen::Vector3d normal
normal unit vector at contact
double depth
depth (penetration between bodies)
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Eigen::Vector3d pos
contact position
When collision costs are computed, this structure contains information about the partial cost incurre...
std::array< double, 3 > aabb_max
The maximum bound of the AABB defining the volume responsible for this partial cost.
double cost
The partial cost (the probability of existence for the object there is a collision with)
std::array< double, 3 > aabb_min
The minimum bound of the AABB defining the volume responsible for this partial cost.