Drake
collision_filter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <bitset>
4 #include <string>
5 #include <unordered_map>
6 #include <utility>
7 #include <vector>
8 
10 
11 // forward declaration
12 template <typename U>
13 class RigidBody;
14 
15 namespace drake {
16 namespace multibody {
17 namespace collision {
19 constexpr int kMaxNumCollisionFilterGroups = 128;
20 
21 typedef std::bitset<kMaxNumCollisionFilterGroups> bitmask;
22 
23 // Constants
24 // The empty bit mask. Used in the membership group mask represents no
25 // membership. As the ignore mask, the body ignores nothing.
26 constexpr bitmask kNoneMask(0);
27 // The membership bit mask indicating the CFG to which *all* bodies belong. A
28 // body can be made invisible (from a collision perspective) by having setting
29 // its ignore mask to kDefaultGroup.
30 constexpr bitmask kDefaultGroup(1);
31 
62 template <typename T>
64  public:
66 
67 
71 
76  CollisionFilterGroup(const std::string& name, int id);
77 
78  int get_mask_id() const { return mask_id_; }
79 
80  void add_body(const RigidBody<T>& body) { bodies_.push_back(&body); }
81 
82  std::vector<const RigidBody<T>*>& get_bodies() { return bodies_; }
83 
84  const std::vector<std::string>& get_ignore_groups() const {
85  return ignore_groups_;
86  }
87 
88  void add_ignore_group(const std::string& group_name) {
89  ignore_groups_.push_back(group_name);
90  }
91 
92  private:
93  std::string name_{};
94  int mask_id_{};
95  std::vector<const RigidBody<T>*> bodies_{};
96  std::vector<std::string> ignore_groups_{};
97 };
98 
99 // TODO(SeanCurtis-TRI): Per discussion in issue #4729, this will eventually
100 // be expanded to include programmatic manipulation of groups during
101 // construction, and, eventually, dynamic manipulation of groups during
102 // simulation.
141 template <typename T>
143  public:
145 
146 
148 
153  void CompileGroups();
154 
162  void DefineCollisionFilterGroup(const std::string& name);
163 
164  // Note: unlike the other public methods of this class, this method does *not*
165  // throw an exception upon failure. The reason is two-fold:
166  // 1) This method has a single fail condition (which can be succinctly
167  // communicated with a boolean.)
168  // 2) The caller has more context to why this was called and is better able
169  // to provide a meaningful error message. More particularly, this code
170  // cannot create an error message including the body name without creating
171  // a circular dependency between drakeRBM and drakeCollision.
180  bool AddCollisionFilterGroupMember(const std::string& group_name,
181  const RigidBody<T>& body);
182 
192  void AddCollisionFilterIgnoreTarget(const std::string& group_name,
193  const std::string& target_group_name);
194 
200  int GetGroupId(const std::string& group_name);
201 
207  const bitmask& get_group_mask(const RigidBody<T>& body);
208 
214  const bitmask& get_ignore_mask(const RigidBody<T>& body);
215 
216  // TODO(SeanCurtis-TRI): Kill this method when matlab dependencies are
217  // removed. There is a corresponding method on the RigidBodyTree.
227  void SetBodyCollisionFilters(const RigidBody<T>& body, const bitmask& group,
228  const bitmask& ignores);
229 
240  void Clear();
241 
246  static const int kInvalidGroupId;
247 
248  private:
249  // Attempts to provision a group id for the next group. Throws an exception
250  // if this manager is out of ids.
251  int acquire_next_group_id();
252 
253  // The next available collision filter group identifier. Assumes that 0
254  // is the default group (which is implicitly consumed.)
255  int next_id_{1};
256 
257  // Map between group names and its collision filter group specification.
258  std::unordered_map<std::string,
260  collision_filter_groups_{};
261 
262  // Mappings between a RigidBody and the bitmasks that define its group
263  // membership and the groups it ignores. This is populated during
264  // CompileGroups. The pair is: (group mask, ignore mask).
265  std::unordered_map<const RigidBody<T>*, std::pair<bitmask, bitmask>>
266  body_groups_;
267 };
268 } // namespace collision
269 } // namespace multibody
270 } // namespace drake
std::bitset< kMaxNumCollisionFilterGroups > bitmask
Definition: collision_filter.h:21
Definition: automotive_demo.cc:88
constexpr bitmask kDefaultGroup(1)
constexpr int kMaxNumCollisionFilterGroups
The maximum width of the collision filter group bitmasks.
Definition: collision_filter.h:19
The specification of a collision filter group: its name, bodies that belong to it, and the names of collision filter groups that it ignores.
Definition: collision_filter.h:63
CollisionFilterGroup()
Default constructor required by use in std::unordered_map.
Definition: collision_filter.cc:17
static const int kInvalidGroupId
Reported value for group names that do not map to known collision filter groups.
Definition: collision_filter.h:246
Definition: collision_filter.h:13
const std::vector< std::string > & get_ignore_groups() const
Definition: collision_filter.h:84
void add_ignore_group(const std::string &group_name)
Definition: collision_filter.h:88
#define DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Classname)
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN defaults the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:59
void add_body(const RigidBody< T > &body)
Definition: collision_filter.h:80
This class provides management utilities for the definition of collision filter groups for RigidBodyT...
Definition: collision_filter.h:142
constexpr bitmask kNoneMask(0)
int get_mask_id() const
Definition: collision_filter.h:78
The term rigid body implies that the deformations of the body under consideration are so small that t...
Definition: fixed_offset_frame.h:16
std::vector< const RigidBody< T > * > & get_bodies()
Definition: collision_filter.h:82
#define DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(Classname)
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN deletes the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:35
Provides careful macros to selectively enable or disable the special member functions for copy-constr...