moveit2
The MoveIt Motion Planning Framework for ROS 2.
bullet_cast_bvh_manager.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD-2-Clause)
3  *
4  * Copyright (c) 2017, Southwest Research Institute
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  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *********************************************************************/
31 
32 /* Author: Levi Armstrong, Jens Petit */
33 
35 
36 #include <rclcpp/logger.hpp>
37 #include <rclcpp/logging.hpp>
38 #include <map>
39 #include <utility>
40 
41 static const rclcpp::Logger BULLET_LOGGER = rclcpp::get_logger("collision_detection.bullet");
42 
44 {
45 BulletCastBVHManagerPtr BulletCastBVHManager::clone() const
46 {
47  BulletCastBVHManagerPtr manager = std::make_shared<BulletCastBVHManager>();
48 
49  for (const std::pair<const std::string, collision_detection_bullet::CollisionObjectWrapperPtr>& cow : link2cow_)
50  {
51  CollisionObjectWrapperPtr new_cow = cow.second->clone();
52 
53  assert(new_cow->getCollisionShape());
54  assert(new_cow->getCollisionShape()->getShapeType() != CUSTOM_CONVEX_SHAPE_TYPE);
55 
56  new_cow->setWorldTransform(cow.second->getWorldTransform());
57  new_cow->setContactProcessingThreshold(static_cast<btScalar>(contact_distance_));
58  manager->addCollisionObject(new_cow);
59  }
60 
61  manager->setActiveCollisionObjects(active_);
62  manager->setContactDistanceThreshold(contact_distance_);
63 
64  return manager;
65 }
66 
67 void BulletCastBVHManager::setCastCollisionObjectsTransform(const std::string& name, const Eigen::Isometry3d& pose1,
68  const Eigen::Isometry3d& pose2)
69 {
70  // TODO: Find a way to remove this check. Need to store information in Tesseract EnvState indicating transforms with
71  // geometry
72  auto it = link2cow_.find(name);
73  if (it != link2cow_.end())
74  {
75  CollisionObjectWrapperPtr& cow = it->second;
76  assert(cow->m_collisionFilterGroup == btBroadphaseProxy::KinematicFilter);
77 
78  btTransform tf1 = convertEigenToBt(pose1);
79  btTransform tf2 = convertEigenToBt(pose2);
80 
81  cow->setWorldTransform(tf1);
82  link2cow_[name]->setWorldTransform(tf1);
83 
84  // If collision object is disabled don't proceed
85  if (cow->m_enabled)
86  {
87  if (btBroadphaseProxy::isConvex(cow->getCollisionShape()->getShapeType()))
88  {
89  static_cast<CastHullShape*>(cow->getCollisionShape())->updateCastTransform(tf1.inverseTimes(tf2));
90  }
91  else if (btBroadphaseProxy::isCompound(cow->getCollisionShape()->getShapeType()))
92  {
93  btCompoundShape* compound = static_cast<btCompoundShape*>(cow->getCollisionShape());
94 
95  for (int i = 0; i < compound->getNumChildShapes(); ++i)
96  {
97  if (btBroadphaseProxy::isConvex(compound->getChildShape(i)->getShapeType()))
98  {
99  const btTransform& local_tf = compound->getChildTransform(i);
100 
101  btTransform delta_tf = (tf1 * local_tf).inverseTimes(tf2 * local_tf);
102  static_cast<CastHullShape*>(compound->getChildShape(i))->updateCastTransform(delta_tf);
103  compound->updateChildTransform(i, local_tf, false); // This is required to update the BVH tree
104  }
105  else if (btBroadphaseProxy::isCompound(compound->getChildShape(i)->getShapeType()))
106  {
107  btCompoundShape* second_compound = static_cast<btCompoundShape*>(compound->getChildShape(i));
108 
109  for (int j = 0; j < second_compound->getNumChildShapes(); ++j)
110  {
111  assert(!btBroadphaseProxy::isCompound(second_compound->getChildShape(j)->getShapeType()));
112  const btTransform& local_tf = second_compound->getChildTransform(j);
113 
114  btTransform delta_tf = (tf1 * local_tf).inverseTimes(tf2 * local_tf);
115  static_cast<CastHullShape*>(second_compound->getChildShape(j))->updateCastTransform(delta_tf);
116  second_compound->updateChildTransform(j, local_tf, false); // This is required to update the BVH tree
117  }
118  second_compound->recalculateLocalAabb();
119  }
120  }
121  compound->recalculateLocalAabb();
122  }
123  else
124  {
125  RCLCPP_ERROR_STREAM(BULLET_LOGGER, "I can only continuous collision check convex shapes and "
126  "compound shapes made of convex shapes");
127  throw std::runtime_error(
128  "I can only continuous collision check convex shapes and compound shapes made of convex shapes");
129  }
130 
131  // Now update Broadphase AABB (See BulletWorld updateSingleAabb function)
133  }
134  }
135 }
136 
139  const collision_detection::AllowedCollisionMatrix* acm, bool /*self*/ = false)
140 {
141  ContactTestData cdata(active_, contact_distance_, collisions, req);
142  broadphase_->calculateOverlappingPairs(dispatcher_.get());
143  btOverlappingPairCache* pair_cache = broadphase_->getOverlappingPairCache();
144 
145  RCLCPP_DEBUG_STREAM(BULLET_LOGGER, "Number overlapping candidates " << pair_cache->getNumOverlappingPairs());
146 
147  BroadphaseContactResultCallback cc(cdata, contact_distance_, acm, false, true);
148  TesseractCollisionPairCallback collision_callback(dispatch_info_, dispatcher_.get(), cc);
149  pair_cache->processAllOverlappingPairs(&collision_callback, dispatcher_.get());
150 }
151 
152 void BulletCastBVHManager::addCollisionObject(const CollisionObjectWrapperPtr& cow)
153 {
154  std::string name = cow->getName();
155  if (cow->m_collisionFilterGroup == btBroadphaseProxy::KinematicFilter)
156  {
157  CollisionObjectWrapperPtr cast_cow = makeCastCollisionObject(cow);
158  link2cow_[name] = cast_cow;
159  }
160  else
161  {
162  link2cow_[name] = cow;
163  }
164 
165  btVector3 aabb_min, aabb_max;
166  link2cow_[name]->getAABB(aabb_min, aabb_max);
167 
168  int type = link2cow_[name]->getCollisionShape()->getShapeType();
169  link2cow_[name]->setBroadphaseHandle(
170  broadphase_->createProxy(aabb_min, aabb_max, type, link2cow_[name].get(), link2cow_[name]->m_collisionFilterGroup,
171  link2cow_[name]->m_collisionFilterMask, dispatcher_.get()));
172 }
173 
174 } // namespace collision_detection_bullet
Definition of a structure for the allowed collision matrix. All elements in the collision world are r...
std::vector< std::string > active_
A list of the active collision links.
std::unique_ptr< btBroadphaseInterface > broadphase_
The bullet broadphase interface.
double contact_distance_
The contact distance threshold.
std::map< std::string, CollisionObjectWrapperPtr > link2cow_
A map of collision objects being managed.
btDispatcherInfo dispatch_info_
The bullet collision dispatcher configuration information.
std::unique_ptr< btCollisionDispatcher > dispatcher_
The bullet collision dispatcher used for getting object to object collision algorithm.
void setCastCollisionObjectsTransform(const std::string &name, const Eigen::Isometry3d &pose1, const Eigen::Isometry3d &pose2)
Set a single cast (moving) collision object's tansforms.
void contactTest(collision_detection::CollisionResult &collisions, const collision_detection::CollisionRequest &req, const collision_detection::AllowedCollisionMatrix *acm, bool self) override
Perform a contact test for all objects.
BulletCastBVHManagerPtr clone() const
Clone the manager.
void addCollisionObject(const CollisionObjectWrapperPtr &cow) override
Add a tesseract collision object to the manager.
A callback function that is called as part of the broadphase collision checking.
Definition: bullet_utils.h:639
btVector3 convertEigenToBt(const Eigen::Vector3d &v)
Converts eigen vector to bullet vector.
Definition: bullet_utils.h:65
CollisionObjectWrapperPtr makeCastCollisionObject(const CollisionObjectWrapperPtr &cow)
void updateBroadphaseAABB(const CollisionObjectWrapperPtr &cow, const std::unique_ptr< btBroadphaseInterface > &broadphase, const std::unique_ptr< btCollisionDispatcher > &dispatcher)
Update the Broadphase AABB for the input collision object.
Definition: bullet_utils.h:678
name
Definition: setup.py:7
const rclcpp::Logger BULLET_LOGGER
Representation of a collision checking request.
Representation of a collision checking result.
Callback structure for both discrete and continuous broadphase collision pair.
Definition: bullet_utils.h:532
Casted collision shape used for checking if an object is collision free between two discrete poses.
Definition: bullet_utils.h:241
Bundles the data for a collision query.
Definition: basic_types.h:59