collision_world_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 #include <fcl/shape/geometric_shape_to_BVH_model.h>
39 #include <fcl/traversal/traversal_node_bvhs.h>
40 #include <fcl/traversal/traversal_node_setup.h>
41 #include <fcl/collision_node.h>
42 #include <boost/bind.hpp>
43 
45 {
46  auto m = new fcl::DynamicAABBTreeCollisionManager();
47  // m->tree_init_level = 2;
48  manager_.reset(m);
49 
50  // request notifications about changes to new world
51  observer_handle_ = getWorld()->addObserver(boost::bind(&CollisionWorldFCL::notifyObjectChange, this, _1, _2));
52 }
53 
55 {
56  auto m = new fcl::DynamicAABBTreeCollisionManager();
57  // m->tree_init_level = 2;
58  manager_.reset(m);
59 
60  // request notifications about changes to new world
61  observer_handle_ = getWorld()->addObserver(boost::bind(&CollisionWorldFCL::notifyObjectChange, this, _1, _2));
62  getWorld()->notifyObserverAllObjects(observer_handle_, World::CREATE);
63 }
64 
66  : CollisionWorld(other, world)
67 {
68  auto m = new fcl::DynamicAABBTreeCollisionManager();
69  // m->tree_init_level = 2;
70  manager_.reset(m);
71 
72  fcl_objs_ = other.fcl_objs_;
73  for (auto& fcl_obj : fcl_objs_)
74  fcl_obj.second.registerTo(manager_.get());
75  // manager_->update();
76 
77  // request notifications about changes to new world
78  observer_handle_ = getWorld()->addObserver(boost::bind(&CollisionWorldFCL::notifyObjectChange, this, _1, _2));
79 }
80 
82 {
83  getWorld()->removeObserver(observer_handle_);
84 }
85 
87  const CollisionRobot& robot,
88  const robot_state::RobotState& state) const
89 {
90  checkRobotCollisionHelper(req, res, robot, state, nullptr);
91 }
92 
94  const CollisionRobot& robot,
95  const robot_state::RobotState& state,
96  const AllowedCollisionMatrix& acm) const
97 {
98  checkRobotCollisionHelper(req, res, robot, state, &acm);
99 }
100 
102  const CollisionRobot& robot,
103  const robot_state::RobotState& state1,
104  const robot_state::RobotState& state2) const
105 {
106  CONSOLE_BRIDGE_logError("FCL continuous collision checking not yet implemented");
107 }
108 
110  const CollisionRobot& robot,
111  const robot_state::RobotState& state1,
112  const robot_state::RobotState& state2,
113  const AllowedCollisionMatrix& acm) const
114 {
115  CONSOLE_BRIDGE_logError("FCL continuous collision checking not yet implemented");
116 }
117 
119  CollisionResult& res,
120  const CollisionRobot& robot,
121  const robot_state::RobotState& state,
122  const AllowedCollisionMatrix* acm) const
123 {
124  const CollisionRobotFCL& robot_fcl = dynamic_cast<const CollisionRobotFCL&>(robot);
125  FCLObject fcl_obj;
126  robot_fcl.constructFCLObject(state, fcl_obj);
127 
128  CollisionData cd(&req, &res, acm);
129  cd.enableGroup(robot.getRobotModel());
130  for (std::size_t i = 0; !cd.done_ && i < fcl_obj.collision_objects_.size(); ++i)
131  manager_->collide(fcl_obj.collision_objects_[i].get(), &cd, &collisionCallback);
132 
133  if (req.distance)
134  {
135  DistanceRequest dreq;
136  DistanceResult dres;
137 
138  dreq.group_name = req.group_name;
139  dreq.acm = acm;
140  dreq.enableGroup(robot.getRobotModel());
141  distanceRobot(dreq, dres, robot, state);
143  }
144 }
145 
147  const CollisionWorld& other_world) const
148 {
149  checkWorldCollisionHelper(req, res, other_world, nullptr);
150 }
151 
153  const CollisionWorld& other_world,
154  const AllowedCollisionMatrix& acm) const
155 {
156  checkWorldCollisionHelper(req, res, other_world, &acm);
157 }
158 
160  CollisionResult& res,
161  const CollisionWorld& other_world,
162  const AllowedCollisionMatrix* acm) const
163 {
164  const CollisionWorldFCL& other_fcl_world = dynamic_cast<const CollisionWorldFCL&>(other_world);
165  CollisionData cd(&req, &res, acm);
166  manager_->collide(other_fcl_world.manager_.get(), &cd, &collisionCallback);
167 
168  if (req.distance)
169  {
170  DistanceRequest dreq;
171  DistanceResult dres;
172 
173  dreq.group_name = req.group_name;
174  dreq.acm = acm;
175  distanceWorld(dreq, dres, other_world);
177  }
178 }
179 
181 {
182  for (std::size_t i = 0; i < obj->shapes_.size(); ++i)
183  {
184  FCLGeometryConstPtr g = createCollisionGeometry(obj->shapes_[i], obj);
185  if (g)
186  {
187  auto co = new fcl::CollisionObject(g->collision_geometry_, transform2fcl(obj->shape_poses_[i]));
188  fcl_obj.collision_objects_.push_back(FCLCollisionObjectPtr(co));
189  fcl_obj.collision_geometry_.push_back(g);
190  }
191  }
192 }
193 
195 {
196  // remove FCL objects that correspond to this object
197  auto jt = fcl_objs_.find(id);
198  if (jt != fcl_objs_.end())
199  {
200  jt->second.unregisterFrom(manager_.get());
201  jt->second.clear();
202  }
203 
204  // check to see if we have this object
205  auto it = getWorld()->find(id);
206  if (it != getWorld()->end())
207  {
208  // construct FCL objects that correspond to this object
209  if (jt != fcl_objs_.end())
210  {
211  constructFCLObject(it->second.get(), jt->second);
212  jt->second.registerTo(manager_.get());
213  }
214  else
215  {
216  constructFCLObject(it->second.get(), fcl_objs_[id]);
217  fcl_objs_[id].registerTo(manager_.get());
218  }
219  }
220  else
221  {
222  if (jt != fcl_objs_.end())
223  fcl_objs_.erase(jt);
224  }
225 
226  // manager_->update();
227 }
228 
230 {
231  if (world == getWorld())
232  return;
233 
234  // turn off notifications about old world
235  getWorld()->removeObserver(observer_handle_);
236 
237  // clear out objects from old world
238  manager_->clear();
239  fcl_objs_.clear();
241 
243 
244  // request notifications about changes to new world
245  observer_handle_ = getWorld()->addObserver(boost::bind(&CollisionWorldFCL::notifyObjectChange, this, _1, _2));
246 
247  // get notifications any objects already in the new world
248  getWorld()->notifyObserverAllObjects(observer_handle_, World::CREATE);
249 }
250 
252 {
253  if (action == World::DESTROY)
254  {
255  auto it = fcl_objs_.find(obj->id_);
256  if (it != fcl_objs_.end())
257  {
258  it->second.unregisterFrom(manager_.get());
259  it->second.clear();
260  fcl_objs_.erase(it);
261  }
263  }
264  else
265  {
266  updateFCLObject(obj->id_);
267  if (action & (World::DESTROY | World::REMOVE_SHAPE))
269  }
270 }
271 
273  const CollisionRobot& robot,
274  const robot_state::RobotState& state) const
275 {
276  const CollisionRobotFCL& robot_fcl = dynamic_cast<const CollisionRobotFCL&>(robot);
277  FCLObject fcl_obj;
278  robot_fcl.constructFCLObject(state, fcl_obj);
279 
280  DistanceData drd(&req, &res);
281  for (std::size_t i = 0; !drd.done && i < fcl_obj.collision_objects_.size(); ++i)
282  manager_->distance(fcl_obj.collision_objects_[i].get(), &drd, &distanceCallback);
283 }
284 
286  const CollisionWorld& world) const
287 {
288  const CollisionWorldFCL& other_fcl_world = dynamic_cast<const CollisionWorldFCL&>(world);
289  DistanceData drd(&req, &res);
290  manager_->distance(other_fcl_world.manager_.get(), &drd, &distanceCallback);
291 }
292 
void updateFCLObject(const std::string &id)
void constructFCLObject(const robot_state::RobotState &state, FCLObject &fcl_obj) const
bool done_
Flag indicating whether collision checking is complete.
void checkRobotCollisionHelper(const CollisionRequest &req, CollisionResult &res, const CollisionRobot &robot, const robot_state::RobotState &state, const AllowedCollisionMatrix *acm) const
FCLGeometryConstPtr createCollisionGeometry(const shapes::ShapeConstPtr &shape, const robot_model::LinkModel *link, int shape_index)
virtual void distanceRobot(const DistanceRequest &req, DistanceResult &res, const CollisionRobot &robot, const robot_state::RobotState &state) const override
Compute the distance between a robot and the world.
const AllowedCollisionMatrix * acm
The allowed collision matrix used to filter checks.
std::vector< shapes::ShapeConstPtr > shapes_
All the shapes making up this object.
Definition: world.h:102
bool collisionCallback(fcl::CollisionObject *o1, fcl::CollisionObject *o2, void *data)
virtual void distanceWorld(const DistanceRequest &req, DistanceResult &res, const CollisionWorld &world) const override
Compute the distance between another world.
DistanceResultsData minimum_distance
ResultsData for the two objects with the minimum distance.
Represents an action that occurred on an object in the world. Several bits may be set indicating seve...
Definition: world.h:192
const robot_model::RobotModelConstPtr & getRobotModel() const
The kinematic model corresponding to this collision model.
std::unique_ptr< fcl::BroadPhaseCollisionManager > manager_
void enableGroup(const robot_model::RobotModelConstPtr &kmodel)
Compute active_components_only_ based on req_.
World::ObjectConstPtr ObjectConstPtr
virtual void setWorld(const WorldPtr &world)
A representation of an object.
Definition: world.h:80
void checkWorldCollisionHelper(const CollisionRequest &req, CollisionResult &res, const CollisionWorld &other_world, const AllowedCollisionMatrix *acm) const
virtual void checkWorldCollision(const CollisionRequest &req, CollisionResult &res, const CollisionWorld &other_world) const
Check whether a given set of objects is in collision with objects from another world. Any contacts are considered.
Definition of a structure for the allowed collision matrix. All elements in the collision world are r...
This class represents a collision model of the robot and can be used for self collision checks (to ch...
std::string group_name
The group name to check collisions for (optional; if empty, assume the complete robot) ...
double distance
The distance between two objects. If two objects are in collision, distance <= 0. ...
virtual void checkRobotCollision(const CollisionRequest &req, CollisionResult &res, const CollisionRobot &robot, const robot_state::RobotState &state) const
Check whether the robot model is in collision with the world. Any collisions between a robot link and...
virtual void setWorld(const WorldPtr &world)
#define CONSOLE_BRIDGE_logError(fmt,...)
Perform collision checking with the environment. The collision world maintains a representation of th...
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:110
void notifyObjectChange(const ObjectConstPtr &obj, World::Action action)
bool distanceCallback(fcl::CollisionObject *o1, fcl::CollisionObject *o2, void *data, double &min_dist)
EigenSTL::vector_Affine3d shape_poses_
The poses of the corresponding entries in shapes_.
Definition: world.h:107
std::shared_ptr< fcl::CollisionObject > FCLCollisionObjectPtr
std::map< std::string, FCLObject > fcl_objs_
void constructFCLObject(const World::Object *obj, FCLObject &fcl_obj) const


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Sat Apr 21 2018 02:54:51