1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Alessandro Tasora, Radu Serban
13 // =============================================================================
14 
15 #ifndef CH_COLLISION_SYSTEM_BULLET_H
16 #define CH_COLLISION_SYSTEM_BULLET_H
17 
18 #include "chrono/collision/ChCollisionSystem.h"
19 #include "chrono/collision/bullet/btBulletCollisionCommon.h"
20 #include "chrono/core/ChApiCE.h"
21 
22 namespace chrono {
23 namespace collision {
24 
25 /// @addtogroup collision_bullet
26 /// @{
27 
28 /// Collision engine based on the Bullet library.
29 /// Contains both the broadphase and the narrow phase Bullet methods.
30 class ChApi ChCollisionSystemBullet : public ChCollisionSystem {
31   public:
32     ChCollisionSystemBullet();
33     virtual ~ChCollisionSystemBullet();
34 
35     /// Return the type of this collision system.
GetType()36     virtual ChCollisionSystemType GetType() const override { return ChCollisionSystemType::BULLET; }
37 
38     /// Clears all data instanced by this algorithm
39     /// if any (like persistent contact manifolds)
40     virtual void Clear(void) override;
41 
42     /// Adds a collision model to the collision
43     /// engine (custom data may be allocated).
44     virtual void Add(ChCollisionModel* model) override;
45 
46     /// Removes a collision model from the collision
47     /// engine (custom data may be deallocated).
48     virtual void Remove(ChCollisionModel* model) override;
49 
50     /// Removes all collision models from the collision
51     /// engine (custom data may be deallocated).
52     // virtual void RemoveAll();
53 
54     /// Set the number of OpenMP threads for collision detection.
55     virtual void SetNumThreads(int nthreads) override;
56 
57     /// Run the algorithm and finds all the contacts.
58     /// (Contacts will be managed by the Bullet persistent contact cache).
59     virtual void Run() override;
60 
61     /// Return an AABB bounding all collision shapes in the system
62     virtual void GetBoundingBox(ChVector<>& aabb_min, ChVector<>& aabb_max) const override;
63 
64     /// Reset timers for collision detection.
65     virtual void ResetTimers() override;
66 
67     /// Return the time (in seconds) for broadphase collision detection.
68     virtual double GetTimerCollisionBroad() const override;
69 
70     /// Return the time (in seconds) for narrowphase collision detection.
71     virtual double GetTimerCollisionNarrow() const override;
72 
73     /// After the Run() has completed, you can call this function to
74     /// fill a 'contact container', that is an object inherited from class
75     /// ChContactContainer. For instance ChSystem, after each Run()
76     /// collision detection, calls this method multiple times for all contact containers in the system,
77     /// The basic behavior of the implementation is the following: collision system
78     /// will call in sequence the functions BeginAddContact(), AddContact() (x n times),
79     /// EndAddContact() of the contact container.
80     virtual void ReportContacts(ChContactContainer* mcontactcontainer) override;
81 
82     /// After the Run() has completed, you can call this function to
83     /// fill a 'proximity container' (container of narrow phase pairs), that is
84     /// an object inherited from class ChProximityContainer. For instance ChSystem, after each Run()
85     /// collision detection, calls this method multiple times for all proximity containers in the system,
86     /// The basic behavior of the implementation is  the following: collision system
87     /// will call in sequence the functions BeginAddProximities(), AddProximity() (x n times),
88     /// EndAddProximities() of the proximity container.
89     virtual void ReportProximities(ChProximityContainer* mproximitycontainer) override;
90 
91     /// Perform a ray-hit test with all collision models.
92     virtual bool RayHit(const ChVector<>& from, const ChVector<>& to, ChRayhitResult& result) const override;
93 
94     /// Perform a ray-hit test with the specified collision model.
95     virtual bool RayHit(const ChVector<>& from,
96                         const ChVector<>& to,
97                         ChCollisionModel* model,
98                         ChRayhitResult& result) const override;
99 
100     // Get the underlying Bullet collision world.
GetBulletCollisionWorld()101     btCollisionWorld* GetBulletCollisionWorld() { return bt_collision_world; }
102 
103     // Change default contact breaking/merging threshold tolerance of Bullet.
104     // This is the static gContactBreakingThreshold scalar in Bullet.
105     // Call this function only once, before running the simulation.
106     static void SetContactBreakingThreshold(double threshold);
107 
108   private:
109     /// Perform a ray-hit test with all collision models. This version allows specifying the Bullet
110     /// collision filter group and mask (see btBroadphaseProxy::CollisionFilterGroups).
111     bool RayHit(const ChVector<>& from,
112                 const ChVector<>& to,
113                 ChRayhitResult& result,
114                 short int filter_group,
115                 short int filter_mask) const;
116 
117     /// Perform a ray-hit test with the specified collision model. This version allows specifying the Bullet
118     /// collision filter group and mask (see btBroadphaseProxy::CollisionFilterGroups).
119     bool RayHit(const ChVector<>& from,
120                 const ChVector<>& to,
121                 ChCollisionModel* model,
122                 ChRayhitResult& result,
123                 short int filter_group,
124                 short int filter_mask) const;
125 
126     btCollisionConfiguration* bt_collision_configuration;
127     btCollisionDispatcher* bt_dispatcher;
128     btBroadphaseInterface* bt_broadphase;
129     btCollisionWorld* bt_collision_world;
130 
131     btCollisionAlgorithmCreateFunc* m_collision_capsule_box;
132     btCollisionAlgorithmCreateFunc* m_collision_box_capsule;
133     btCollisionAlgorithmCreateFunc* m_collision_cylshell_box;
134     btCollisionAlgorithmCreateFunc* m_collision_box_cylshell;
135     btCollisionAlgorithmCreateFunc* m_collision_arc_seg;
136     btCollisionAlgorithmCreateFunc* m_collision_seg_arc;
137     btCollisionAlgorithmCreateFunc* m_collision_arc_arc;
138     btCollisionAlgorithmCreateFunc* m_collision_cetri_cetri;
139     void* m_tmp_mem;
140     btCollisionAlgorithmCreateFunc* m_emptyCreateFunc;
141 };
142 
143 /// @} collision_bullet
144 
145 }  // end namespace collision
146 }  // end namespace chrono
147 
148 #endif
149