Drake
bullet_model.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include "btBulletCollisionCommon.h"
7 #include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
8 #include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h"
9 #include "BulletCollision/NarrowPhaseCollision/btPointCollector.h"
10 
11 #include "Element.h"
12 #include "Model.h"
13 
14 namespace DrakeCollision {
15 
16 class BulletModel; // forward declaration
17 
18 typedef std::unordered_map<ElementId, std::unique_ptr<btCollisionObject>>
20 
22  // return true when pairs need collision
23  virtual bool needBroadphaseCollision(btBroadphaseProxy* proxy0,
24  btBroadphaseProxy* proxy1) const;
25 
27 };
28 
31  ElementToBtObjMap bt_collision_objects;
32 
33  btDefaultCollisionConfiguration bt_collision_configuration;
34  btDbvtBroadphase bt_collision_broadphase;
36 
37  std::unique_ptr<btCollisionDispatcher> bt_collision_dispatcher;
38  std::unique_ptr<btCollisionWorld> bt_collision_world;
39 };
40 
41 class UnknownShapeException : public std::exception {
42  public:
43  explicit UnknownShapeException(DrakeShapes::Shape shape);
44  virtual const char* what() const throw();
45  virtual ~UnknownShapeException() throw() {}
46 
47  private:
48  std::string shape_name_;
49 };
50 
51 class BulletModel : public Model {
52  public:
54 
55  virtual ~BulletModel() {}
56 
57  void updateModel() override;
58 
59  ElementId addElement(const Element& element) override;
60 
61  bool updateElementWorldTransform(
62  const ElementId, const Eigen::Isometry3d& T_local_to_world) override;
63 
70  bool closestPointsAllToAll(const std::vector<ElementId>& ids_to_check,
71  const bool use_margins,
72  std::vector<PointPair>& closest_points) override;
73 
74  bool ComputeMaximumDepthCollisionPoints(
75  const bool use_margins, std::vector<PointPair>& points) override;
76 
83  bool closestPointsPairwise(const std::vector<ElementIdPair>& id_pairs,
84  const bool use_margins,
85  std::vector<PointPair>& closest_points) override;
86 
87  void collisionDetectFromPoints(
88  const Eigen::Matrix3Xd& points, bool use_margins,
89  std::vector<PointPair>& closest_points) override;
90 
91  void ClearCachedResults(bool use_margins) override;
92 
93  bool collisionRaycast(const Eigen::Matrix3Xd& origins,
94  const Eigen::Matrix3Xd& ray_endpoints, bool use_margins,
95  Eigen::VectorXd& distances,
96  Eigen::Matrix3Xd& normals) override;
97 
120  std::vector<PointPair> potentialCollisionPoints(bool use_margins) override;
121 
122  bool collidingPointsCheckOnly(
123  const std::vector<Eigen::Vector3d>& input_points,
124  double collision_threshold) override;
125 
126  std::vector<size_t> collidingPoints(
127  const std::vector<Eigen::Vector3d>& input_points,
128  double collision_threshold) override;
129 
130  private:
131  enum DispatchMethod {
132  kNotYetDecided,
133  kClosestPointsAllToAll,
134  kCollisionPointsAllToAll,
135  kPotentialCollisionPoints
136  };
137 
138  static constexpr double kSmallMargin = 1e-9;
139  static constexpr double kLargeMargin = 0.05;
140 
141  // BulletModel objects are not copyable
142  BulletModel(const BulletModel&) = delete;
143  BulletModel& operator=(const BulletModel&) = delete;
144 
152  virtual PointPair findClosestPointsBetweenElements(
153  const ElementId idA, const ElementId idB, const bool use_margins);
154 
155  BulletCollisionWorldWrapper& getBulletWorld(bool use_margins);
156  static std::unique_ptr<btCollisionShape> newBulletBoxShape(
157  const DrakeShapes::Box& geometry, bool use_margins);
158  static std::unique_ptr<btCollisionShape> newBulletSphereShape(
159  const DrakeShapes::Sphere& geometry, bool use_margins);
160  static std::unique_ptr<btCollisionShape> newBulletCylinderShape(
161  const DrakeShapes::Cylinder& geometry, bool use_margins);
162  static std::unique_ptr<btCollisionShape> newBulletCapsuleShape(
163  const DrakeShapes::Capsule& geometry, bool use_margins);
164  static std::unique_ptr<btCollisionShape> newBulletMeshShape(
165  const DrakeShapes::Mesh& geometry, bool use_margins);
166  static std::unique_ptr<btCollisionShape> newBulletStaticMeshShape(
167  const DrakeShapes::Mesh& geometry, bool use_margins);
168  static std::unique_ptr<btCollisionShape> newBulletMeshPointsShape(
169  const DrakeShapes::MeshPoints& geometry, bool use_margins);
170 
171  std::vector<std::unique_ptr<btCollisionShape>> bt_collision_shapes_;
172  BulletCollisionWorldWrapper bullet_world_;
173  BulletCollisionWorldWrapper bullet_world_no_margin_;
174  DispatchMethod dispatch_method_in_use_{kNotYetDecided};
175 };
176 
177 } // namespace DrakeCollision
virtual ~UnknownShapeException()
Definition: bullet_model.h:45
Definition: bullet_model.h:21
btDefaultCollisionConfiguration bt_collision_configuration
Definition: bullet_model.h:33
Definition: Geometry.h:81
BulletModel()
Definition: bullet_model.h:53
Definition: Element.h:25
Definition: Geometry.h:116
ElementToBtObjMap bt_collision_objects
Definition: bullet_model.h:31
btDbvtBroadphase bt_collision_broadphase
Definition: bullet_model.h:34
Definition: Geometry.h:98
Definition: bullet_model.h:29
OverlapFilterCallback filter_callback
Definition: bullet_model.h:35
Definition: bullet_model.h:51
virtual ~BulletModel()
Definition: bullet_model.h:55
BulletModel * parent_model
Definition: bullet_model.h:26
Definition: bullet_model.h:41
Definition: Geometry.h:180
Definition: Model.h:16
bool closestPointsPairwise(const vector< ElementIdPair > &id_pairs, const bool use_margins, vector< PointPair > &closest_points)
Definition: Model.cpp:76
std::unique_ptr< btCollisionDispatcher > bt_collision_dispatcher
Definition: bullet_model.h:37
virtual bool needBroadphaseCollision(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1) const
Definition: bullet_model.cc:48
Definition: bullet_model.cc:17
std::unique_ptr< btCollisionWorld > bt_collision_world
Definition: bullet_model.h:38
uintptr_t ElementId
Definition: Element.h:23
Structure containing the results of a collision query.
Definition: point_pair.h:12
Definition: Geometry.h:62
Definition: Geometry.h:136
bool closestPointsAllToAll(const vector< ElementId > &ids_to_check, const bool use_margins, vector< PointPair > &closest_points)
Definition: Model.cpp:65
std::unordered_map< ElementId, std::unique_ptr< btCollisionObject > > ElementToBtObjMap
Definition: bullet_model.h:16