collision_robot_fcl.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 
39 collision_detection::CollisionRobotFCL::CollisionRobotFCL(const robot_model::RobotModelConstPtr& model, double padding,
40  double scale)
41  : CollisionRobot(model, padding, scale)
42 {
43  const std::vector<const robot_model::LinkModel*>& links = robot_model_->getLinkModelsWithCollisionGeometry();
44  std::size_t index;
45  geoms_.resize(robot_model_->getLinkGeometryCount());
46  fcl_objs_.resize(robot_model_->getLinkGeometryCount());
47  // we keep the same order of objects as what RobotState *::getLinkState() returns
48  for (auto link : links)
49  for (std::size_t j = 0; j < link->getShapes().size(); ++j)
50  {
51  FCLGeometryConstPtr g = createCollisionGeometry(link->getShapes()[j], getLinkScale(link->getName()),
52  getLinkPadding(link->getName()), link, j);
53  if (g)
54  {
55  index = link->getFirstCollisionBodyTransformIndex() + j;
56  geoms_[index] = g;
57 
58  // Need to store the FCL object so the AABB does not get recreated every time.
59  // Every time this object is created, g->computeLocalAABB() is called which is
60  // very expensive and should only be calculated once. To update the AABB, use the
61  // collObj->setTransform and then call collObj->computeAABB() to transform the AABB.
62  fcl_objs_[index] = FCLCollisionObjectConstPtr(new fcl::CollisionObject(g->collision_geometry_));
63  }
64  else
65  CONSOLE_BRIDGE_logError("Unable to construct collision geometry for link '%s'", link->getName().c_str());
66  }
67 }
68 
70 {
71  geoms_ = other.geoms_;
72  fcl_objs_ = other.fcl_objs_;
73 }
74 
76  std::vector<FCLGeometryConstPtr>& geoms) const
77 {
78  const std::vector<shapes::ShapeConstPtr>& shapes = ab->getShapes();
79  for (std::size_t i = 0; i < shapes.size(); ++i)
80  {
81  FCLGeometryConstPtr co = createCollisionGeometry(shapes[i], ab, i);
82  if (co)
83  geoms.push_back(co);
84  }
85 }
86 
88  FCLObject& fcl_obj) const
89 {
90  fcl_obj.collision_objects_.reserve(geoms_.size());
91  fcl::Transform3f fcl_tf;
92 
93  for (std::size_t i = 0; i < geoms_.size(); ++i)
94  if (geoms_[i] && geoms_[i]->collision_geometry_)
95  {
96  transform2fcl(state.getCollisionBodyTransform(geoms_[i]->collision_geometry_data_->ptr.link,
97  geoms_[i]->collision_geometry_data_->shape_index),
98  fcl_tf);
99  auto collObj = new fcl::CollisionObject(*fcl_objs_[i]);
100  collObj->setTransform(fcl_tf);
101  collObj->computeAABB();
102  fcl_obj.collision_objects_.push_back(FCLCollisionObjectPtr(collObj));
103  }
104 
105  // TODO: Implement a method for caching fcl::CollisionObject's for robot_state::AttachedBody's
106  std::vector<const robot_state::AttachedBody*> ab;
107  state.getAttachedBodies(ab);
108  for (auto& body : ab)
109  {
110  std::vector<FCLGeometryConstPtr> objs;
111  getAttachedBodyObjects(body, objs);
112  const EigenSTL::vector_Affine3d& ab_t = body->getGlobalCollisionBodyTransforms();
113  for (std::size_t k = 0; k < objs.size(); ++k)
114  if (objs[k]->collision_geometry_)
115  {
116  transform2fcl(ab_t[k], fcl_tf);
117  fcl_obj.collision_objects_.push_back(
118  FCLCollisionObjectPtr(new fcl::CollisionObject(objs[k]->collision_geometry_, fcl_tf)));
119  // we copy the shared ptr to the CollisionGeometryData, as this is not stored by the class itself,
120  // and would be destroyed when objs goes out of scope.
121  fcl_obj.collision_geometry_.push_back(objs[k]);
122  }
123  }
124 }
125 
127  FCLManager& manager) const
128 {
129  auto m = new fcl::DynamicAABBTreeCollisionManager();
130  // m->tree_init_level = 2;
131  manager.manager_.reset(m);
132  constructFCLObject(state, manager.object_);
133  manager.object_.registerTo(manager.manager_.get());
134  // manager.manager_->update();
135 }
136 
138  const robot_state::RobotState& state) const
139 {
140  checkSelfCollisionHelper(req, res, state, nullptr);
141 }
142 
144  const robot_state::RobotState& state,
145  const AllowedCollisionMatrix& acm) const
146 {
147  checkSelfCollisionHelper(req, res, state, &acm);
148 }
149 
151  const robot_state::RobotState& state1,
152  const robot_state::RobotState& state2) const
153 {
154  CONSOLE_BRIDGE_logError("FCL continuous collision checking not yet implemented");
155 }
156 
158  const robot_state::RobotState& state1,
159  const robot_state::RobotState& state2,
160  const AllowedCollisionMatrix& acm) const
161 {
162  CONSOLE_BRIDGE_logError("FCL continuous collision checking not yet implemented");
163 }
164 
166  const robot_state::RobotState& state,
167  const AllowedCollisionMatrix* acm) const
168 {
169  FCLManager manager;
170  allocSelfCollisionBroadPhase(state, manager);
171  CollisionData cd(&req, &res, acm);
173  manager.manager_->collide(&cd, &collisionCallback);
174  if (req.distance)
175  res.distance = distanceSelfHelper(state, acm);
176 }
177 
179  const robot_state::RobotState& state,
180  const CollisionRobot& other_robot,
181  const robot_state::RobotState& other_state) const
182 {
183  checkOtherCollisionHelper(req, res, state, other_robot, other_state, nullptr);
184 }
185 
187  const robot_state::RobotState& state,
188  const CollisionRobot& other_robot,
189  const robot_state::RobotState& other_state,
190  const AllowedCollisionMatrix& acm) const
191 {
192  checkOtherCollisionHelper(req, res, state, other_robot, other_state, &acm);
193 }
194 
196  const robot_state::RobotState& state1,
197  const robot_state::RobotState& state2,
198  const CollisionRobot& other_robot,
199  const robot_state::RobotState& other_state1,
200  const robot_state::RobotState& other_state2) const
201 {
202  CONSOLE_BRIDGE_logError("FCL continuous collision checking not yet implemented");
203 }
204 
206  const robot_state::RobotState& state1,
207  const robot_state::RobotState& state2,
208  const CollisionRobot& other_robot,
209  const robot_state::RobotState& other_state1,
210  const robot_state::RobotState& other_state2,
211  const AllowedCollisionMatrix& acm) const
212 {
213  CONSOLE_BRIDGE_logError("FCL continuous collision checking not yet implemented");
214 }
215 
217  CollisionResult& res,
218  const robot_state::RobotState& state,
219  const CollisionRobot& other_robot,
220  const robot_state::RobotState& other_state,
221  const AllowedCollisionMatrix* acm) const
222 {
223  FCLManager manager;
224  allocSelfCollisionBroadPhase(state, manager);
225 
226  const CollisionRobotFCL& fcl_rob = dynamic_cast<const CollisionRobotFCL&>(other_robot);
227  FCLObject other_fcl_obj;
228  fcl_rob.constructFCLObject(other_state, other_fcl_obj);
229 
230  CollisionData cd(&req, &res, acm);
232  for (std::size_t i = 0; !cd.done_ && i < other_fcl_obj.collision_objects_.size(); ++i)
233  manager.manager_->collide(other_fcl_obj.collision_objects_[i].get(), &cd, &collisionCallback);
234  if (req.distance)
235  res.distance = distanceOtherHelper(state, other_robot, other_state, acm);
236 }
237 
238 void collision_detection::CollisionRobotFCL::updatedPaddingOrScaling(const std::vector<std::string>& links)
239 {
240  std::size_t index;
241  for (const auto& link : links)
242  {
243  const robot_model::LinkModel* lmodel = robot_model_->getLinkModel(link);
244  if (lmodel)
245  {
246  for (std::size_t j = 0; j < lmodel->getShapes().size(); ++j)
247  {
248  FCLGeometryConstPtr g = createCollisionGeometry(lmodel->getShapes()[j], getLinkScale(lmodel->getName()),
249  getLinkPadding(lmodel->getName()), lmodel, j);
250  if (g)
251  {
252  index = lmodel->getFirstCollisionBodyTransformIndex() + j;
253  geoms_[index] = g;
254  fcl_objs_[index] = FCLCollisionObjectConstPtr(new fcl::CollisionObject(g->collision_geometry_));
255  }
256  }
257  }
258  else
259  CONSOLE_BRIDGE_logError("Updating padding or scaling for unknown link: '%s'", link.c_str());
260  }
261 }
262 
264 {
265  return distanceSelfHelper(state, nullptr);
266 }
267 
269  const AllowedCollisionMatrix& acm) const
270 {
271  return distanceSelfHelper(state, &acm);
272 }
273 
275  const AllowedCollisionMatrix* acm) const
276 {
277  FCLManager manager;
278  allocSelfCollisionBroadPhase(state, manager);
279 
280  CollisionRequest req;
281  CollisionResult res;
282  CollisionData cd(&req, &res, acm);
284 
285  manager.manager_->distance(&cd, &distanceCallback);
286 
287  return res.distance;
288 }
289 
291  const CollisionRobot& other_robot,
292  const robot_state::RobotState& other_state) const
293 {
294  return distanceOtherHelper(state, other_robot, other_state, nullptr);
295 }
296 
298  const CollisionRobot& other_robot,
299  const robot_state::RobotState& other_state,
300  const AllowedCollisionMatrix& acm) const
301 {
302  return distanceOtherHelper(state, other_robot, other_state, &acm);
303 }
304 
306  const CollisionRobot& other_robot,
307  const robot_state::RobotState& other_state,
308  const AllowedCollisionMatrix* acm) const
309 {
310  FCLManager manager;
311  allocSelfCollisionBroadPhase(state, manager);
312 
313  const CollisionRobotFCL& fcl_rob = dynamic_cast<const CollisionRobotFCL&>(other_robot);
314  FCLObject other_fcl_obj;
315  fcl_rob.constructFCLObject(other_state, other_fcl_obj);
316 
317  CollisionRequest req;
318  CollisionResult res;
319  CollisionData cd(&req, &res, acm);
321  for (std::size_t i = 0; !cd.done_ && i < other_fcl_obj.collision_objects_.size(); ++i)
322  manager.manager_->distance(other_fcl_obj.collision_objects_[i].get(), &cd, &distanceCallback);
323 
324  return res.distance;
325 }
const std::string & getName() const
The name of this link.
Definition: link_model.h:81
void getAttachedBodies(std::vector< const AttachedBody * > &attached_bodies) const
Get all bodies attached to the model corresponding to this state.
void constructFCLObject(const robot_state::RobotState &state, FCLObject &fcl_obj) const
bool done_
Flag indicating whether collision checking is complete.
std::vector< Eigen::Affine3d, Eigen::aligned_allocator< Eigen::Affine3d > > vector_Affine3d
FCLGeometryConstPtr createCollisionGeometry(const shapes::ShapeConstPtr &shape, const robot_model::LinkModel *link, int shape_index)
virtual void checkOtherCollision(const CollisionRequest &req, CollisionResult &res, const robot_state::RobotState &state, const CollisionRobot &other_robot, const robot_state::RobotState &other_state) const
Check for collision with a different robot (possibly a different kinematic model as well)...
#define CONSOLE_BRIDGE_logError(fmt,...)
bool collisionCallback(fcl::CollisionObject *o1, fcl::CollisionObject *o2, void *data)
CollisionRobotFCL(const robot_model::RobotModelConstPtr &kmodel, double padding=0.0, double scale=1.0)
const std::map< std::string, double > & getLinkPadding() const
Get the link paddings as a map (from link names to padding value)
void allocSelfCollisionBroadPhase(const robot_state::RobotState &state, FCLManager &manager) const
const robot_model::RobotModelConstPtr & getRobotModel() const
The kinematic model corresponding to this collision model.
std::vector< FCLGeometryConstPtr > geoms_
int getFirstCollisionBodyTransformIndex() const
Definition: link_model.h:97
double distanceSelfHelper(const robot_state::RobotState &state, const AllowedCollisionMatrix *acm) const
robot_model::RobotModelConstPtr robot_model_
The kinematic model corresponding to this collision model.
std::shared_ptr< fcl::BroadPhaseCollisionManager > manager_
const std::vector< shapes::ShapeConstPtr > & getShapes() const
Get the shapes that make up this attached body.
Definition: attached_body.h:88
double distanceOtherHelper(const robot_state::RobotState &state, const CollisionRobot &other_robot, const robot_state::RobotState &other_state, const AllowedCollisionMatrix *acm) const
virtual void checkSelfCollision(const CollisionRequest &req, CollisionResult &res, const robot_state::RobotState &state) const
Check for self collision. Any collision between any pair of links is checked for, NO collisions are i...
unsigned int index
void checkSelfCollisionHelper(const CollisionRequest &req, CollisionResult &res, const robot_state::RobotState &state, const AllowedCollisionMatrix *acm) const
const Eigen::Affine3d & getCollisionBodyTransform(const LinkModel *link, std::size_t index)
Definition: robot_state.h:1324
virtual double distanceOther(const robot_state::RobotState &state, const CollisionRobot &other_robot, const robot_state::RobotState &other_state) const
The distance to another robot instance.
Definition of a structure for the allowed collision matrix. All elements in the collision world are r...
void getAttachedBodyObjects(const robot_state::AttachedBody *ab, std::vector< FCLGeometryConstPtr > &geoms) const
This class represents a collision model of the robot and can be used for self collision checks (to ch...
std::vector< FCLCollisionObjectConstPtr > fcl_objs_
const std::vector< shapes::ShapeConstPtr > & getShapes() const
Get shape associated to the collision geometry for this link.
Definition: link_model.h:174
std::shared_ptr< const fcl::CollisionObject > FCLCollisionObjectConstPtr
virtual double distanceSelf(const robot_state::RobotState &state) const
The distance to self-collision given the robot is at state state.
const std::map< std::string, double > & getLinkScale() const
Get the link scaling as a map (from link names to scale value)
void enableGroup(const robot_model::RobotModelConstPtr &kmodel)
Compute active_components_only_ based on req_.
void transform2fcl(const Eigen::Affine3d &b, fcl::Transform3f &f)
Representation of a robot&#39;s state. This includes position, velocity, acceleration and effort...
Definition: robot_state.h:82
A link from the robot. Contains the constant transform applied to the link and its geometry...
Definition: link_model.h:72
virtual void updatedPaddingOrScaling(const std::vector< std::string > &links)
When the scale or padding is changed for a set of links by any of the functions in this class...
void checkOtherCollisionHelper(const CollisionRequest &req, CollisionResult &res, const robot_state::RobotState &state, const CollisionRobot &other_robot, const robot_state::RobotState &other_state, const AllowedCollisionMatrix *acm) const
bool distanceCallback(fcl::CollisionObject *o1, fcl::CollisionObject *o2, void *data, double &min_dist)
Object defining bodies that can be attached to robot links. This is useful when handling objects pick...
Definition: attached_body.h:56
std::shared_ptr< fcl::CollisionObject > FCLCollisionObjectPtr
void registerTo(fcl::BroadPhaseCollisionManager *manager)


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Mon Jan 15 2018 03:50:44