1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  *   https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  *   Redistribution and use in source and binary forms, with or
10  *   without modification, are permitted provided that the following
11  *   conditions are met:
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *   * Redistributions in binary form must reproduce the above
15  *     copyright notice, this list of conditions and the following
16  *     disclaimer in the documentation and/or other materials provided
17  *     with the distribution.
18  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  *   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  *   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  *   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  *   POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_COLLISION_BULLET_BULLETCOLLISIONDETECTOR_HPP_
34 #define DART_COLLISION_BULLET_BULLETCOLLISIONDETECTOR_HPP_
35 
36 // Must be included before any Bullet headers.
37 #include "dart/config.hpp"
38 
39 #include <vector>
40 #include <assimp/scene.h>
41 #include <btBulletCollisionCommon.h>
42 #include "dart/collision/CollisionDetector.hpp"
43 #include "dart/collision/bullet/BulletCollisionGroup.hpp"
44 #include "dart/collision/bullet/BulletCollisionShape.hpp"
45 
46 namespace dart {
47 namespace collision {
48 
49 class BulletCollisionDetector : public CollisionDetector
50 {
51 public:
52   using CollisionDetector::createCollisionGroup;
53 
54   friend class CollisionDetector;
55 
56   static std::shared_ptr<BulletCollisionDetector> create();
57 
58   /// Constructor
59   ~BulletCollisionDetector() override;
60 
61   // Documentation inherited
62   std::shared_ptr<CollisionDetector> cloneWithoutCollisionObjects()
63       const override;
64 
65   // Documentation inherited
66   const std::string& getType() const override;
67 
68   /// Get collision detector type for this class
69   static const std::string& getStaticType();
70 
71   // Documentation inherited
72   std::unique_ptr<CollisionGroup> createCollisionGroup() override;
73 
74   // Documentation inherited
75   bool collide(
76       CollisionGroup* group,
77       const CollisionOption& option = CollisionOption(false, 1u, nullptr),
78       CollisionResult* result = nullptr) override;
79 
80   // Documentation inherited
81   bool collide(
82       CollisionGroup* group1,
83       CollisionGroup* group2,
84       const CollisionOption& option = CollisionOption(false, 1u, nullptr),
85       CollisionResult* result = nullptr) override;
86 
87   // Documentation inherited
88   double distance(
89       CollisionGroup* group,
90       const DistanceOption& option = DistanceOption(false, 0.0, nullptr),
91       DistanceResult* result = nullptr) override;
92 
93   // Documentation inherited
94   double distance(
95       CollisionGroup* group1,
96       CollisionGroup* group2,
97       const DistanceOption& option = DistanceOption(false, 0.0, nullptr),
98       DistanceResult* result = nullptr) override;
99 
100   // Documentation inherited
101   bool raycast(
102       CollisionGroup* group,
103       const Eigen::Vector3d& from,
104       const Eigen::Vector3d& to,
105       const RaycastOption& option = RaycastOption(),
106       RaycastResult* result = nullptr) override;
107 
108 protected:
109   /// Constructor
110   BulletCollisionDetector();
111 
112   // Documentation inherited
113   std::unique_ptr<CollisionObject> createCollisionObject(
114       const dynamics::ShapeFrame* shapeFrame) override;
115 
116   // Documentation inherited
117   void refreshCollisionObject(CollisionObject* object) override;
118 
119   // Documentation inherited
120   void notifyCollisionObjectDestroying(CollisionObject* object) override;
121 
122 private:
123   std::shared_ptr<BulletCollisionShape> claimBulletCollisionShape(
124       const dynamics::ConstShapePtr& shape);
125 
126   void reclaimBulletCollisionShape(const dynamics::ConstShapePtr& shape);
127 
128   std::unique_ptr<BulletCollisionShape> createBulletCollisionShape(
129       const dynamics::ConstShapePtr& shape);
130 
131   /// This deleter is responsible for deleting BulletCollsionShape objects and
132   /// removing them from mShapeMap when they are not shared by any
133   /// CollisionObjects.
134   class BulletCollisionShapeDeleter final
135   {
136   public:
137     BulletCollisionShapeDeleter(
138         BulletCollisionDetector* cd, const dynamics::ConstShapePtr& shape);
139 
140     void operator()(BulletCollisionShape* shape) const;
141 
142   private:
143     BulletCollisionDetector* mBulletCollisionDetector;
144 
145     dynamics::ConstShapePtr mShape;
146   };
147 
148   /// Information for a shape that was generated by this collision detector
149   struct ShapeInfo final
150   {
151     /// A weak reference to the shape
152     std::weak_ptr<BulletCollisionShape> mShape;
153 
154     /// The last version of the shape, as known by this collision detector
155     std::size_t mLastKnownVersion;
156   };
157 
158 private:
159   std::map<dynamics::ConstShapePtr, ShapeInfo> mShapeMap;
160 
161   std::unique_ptr<BulletCollisionGroup> mGroupForFiltering;
162 
163   static Registrar<BulletCollisionDetector> mRegistrar;
164 };
165 
166 } // namespace collision
167 } // namespace dart
168 
169 #endif // DART_COLLISION_BULLET_BULLETCOLLISIONDETECTOR_HPP_
170