-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
collision_filter.cc
311 lines (284 loc) · 12.5 KB
/
collision_filter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "drake/geometry/proximity/collision_filter.h"
#include "drake/common/drake_assert.h"
#include "drake/common/unused.h"
namespace drake {
namespace geometry {
namespace internal {
using std::unordered_set;
CollisionFilter::CollisionFilter() {
/* The filter history always has at least one entry -- entry[0] is the
persistent base. We associate it with a filter id that can never be
accessed via ApplyTransient() so that it can't accidentally be removed. */
filter_history_.emplace_back(FilterState{}, FilterId::get_new_id());
}
// TODO(SeanCurtis-TRI): Multiple calls to Apply will lead to multiple
// constructions of std::unordered_set from GeometrySet (as opposed to
// re-using a single set). If this is a performance issue, revisit this so
// that I can operate on multiple filter states *per statement*.
void CollisionFilter::Apply(const CollisionFilterDeclaration& declaration,
const CollisionFilter::ExtractIds& extract_ids,
bool is_invariant) {
if (has_transient_history()) {
throw std::runtime_error(
"You cannot attempt to modify the persistent collision filter "
"configuration when there are active, transient filter declarations");
}
// TODO(SeanCurtis-TRI): We're using a brute-force approach to update the
// cached composite state and the persistent state. We assume it's cheaper
// to "apply" the declaration twice than to apply once and copy. If this
// introduces too much cost during configuration, we have a couple of options
// - Find out if copying is faster.
// - In the case where there is *only* persistent state, don't use the
// copy as the persistent state *is* its own composite state.
/* Keep current configuration and persistent base in sync. */
Apply(declaration, extract_ids, is_invariant, &filter_state_);
Apply(declaration, extract_ids, is_invariant,
&filter_history_[0].filter_state);
}
FilterId CollisionFilter::ApplyTransient(
const CollisionFilterDeclaration& declaration,
const CollisionFilter::ExtractIds& extract_ids) {
/* By its very definition, transient declarations *cannot* be invariant. They
are never used by the system to implement invariants and users cannot declare
a filter to be invariant. */
const bool is_invariant = false;
/* Using the same brute-force approach as in Apply(), we need to apply the
declaration to our cached, composite result (filter_state_). Then we need
to add it to the history by:
1. Creating a new FilterState instance (with all the registered geometry
in final_state_).
2. Apply the declaration to that new state in the history. */
// TODO(SeanCurtis-TRI): The brute force approach introduces several costs.
// In addition to the cost of converting GeometrySet --> set<GeometryId>
// twice, we are instantiating a full FilterState for what might be a small
// declaration and applying the declaration twice. If this cost is too high,
// even for out-of-the-loop configuration, revisit how we represent the
// history and maintain the composite copy.
Apply(declaration, extract_ids, is_invariant, &filter_state_);
filter_history_.emplace_back(
InitializeTransientState(filter_state_, kUndefined),
FilterId::get_new_id());
Apply(declaration, extract_ids, is_invariant,
&filter_history_.back().filter_state);
return filter_history_.back().id;
}
bool CollisionFilter::IsActive(FilterId id) const {
for (const auto& delta : filter_history_) {
if (id == delta.id) return true;
}
return false;
}
bool CollisionFilter::RemoveDeclaration(FilterId id) {
/* We skip the first entry, that is always the persistent base and can't be
removed. */
for (auto it = filter_history_.begin() + 1; it != filter_history_.end();
++it) {
if (it->id != id) continue;
/* We found a declaration to remove. We simply pop it out, relying on the
std::vector to use move semantics to slide the subsequent entries down.
Then we just copy the persistent base and "replay" the transient
declarations. */
filter_history_.erase(it);
filter_state_ = filter_history_[0].filter_state;
for (size_t i = 1; i < filter_history_.size(); ++i) {
const FilterState& state = filter_history_[i].filter_state;
/* Note: If the filtered state in transient declarations was sparse, this
application algorithm would directly benefit. It only requires that the
persistent state has all of the required pairs. See the TODO in
AddGeometry() for more discussion. */
for (const auto& [source_id, source_map] : state) {
for (const auto& [pair_id, pair_relation] : source_map) {
if (pair_relation != kUndefined) {
if (filter_state_[source_id][pair_id] != kInvariantFilter) {
filter_state_[source_id][pair_id] = pair_relation;
}
}
}
}
}
return true;
}
return false;
}
void CollisionFilter::Flatten() {
if (filter_history_.size() > 1) {
filter_history_.resize(1);
filter_history_[0].filter_state = filter_state_;
}
}
void CollisionFilter::AddGeometry(GeometryId new_id) {
/* Current and persistent configurations should simply add the id with
unfiltered status. */
AddGeometry(new_id, &filter_state_, kUnfiltered);
AddGeometry(new_id, &filter_history_[0].filter_state, kUnfiltered);
// TODO(SeanCurtis): Can I skip this work by allowing delta filter state to be
// incomplete? If each transient delta *only* contains explicitly declared
// changes, iterating through it would be faster. More complex, but faster.
/* Active transient history should add the id with undefined status. */
for (size_t i = 1; i < filter_history_.size(); ++i) {
AddGeometry(new_id, &filter_history_[i].filter_state, kUndefined);
}
}
void CollisionFilter::RemoveGeometry(GeometryId remove_id) {
/* Simply remove the geometry from everything. */
RemoveGeometry(remove_id, &filter_state_);
for (auto& delta : filter_history_) {
RemoveGeometry(remove_id, &delta.filter_state);
}
}
bool CollisionFilter::CanCollideWith(GeometryId id_A, GeometryId id_B) const {
if (id_A == id_B) return false;
if (id_A < id_B) {
return filter_state_.at(id_A).at(id_B) == kUnfiltered;
} else {
return filter_state_.at(id_B).at(id_A) == kUnfiltered;
}
}
void CollisionFilter::AddFiltersBetween(
const GeometrySet& set_A, const GeometrySet& set_B,
const CollisionFilter::ExtractIds& extract_ids, CollisionFilterScope scope,
bool is_invariant, FilterState* state_out) {
const std::unordered_set<GeometryId> ids_A = extract_ids(set_A, scope);
const std::unordered_set<GeometryId>& ids_B =
&set_A == &set_B ? ids_A : extract_ids(set_B, scope);
for (GeometryId id_A : ids_A) {
for (GeometryId id_B : ids_B) {
AddFilteredPair(id_A, id_B, is_invariant, state_out);
}
}
}
void CollisionFilter::RemoveFiltersBetween(
const GeometrySet& set_A, const GeometrySet& set_B,
const CollisionFilter::ExtractIds& extract_ids, CollisionFilterScope scope,
FilterState* state_out) {
const std::unordered_set<GeometryId> ids_A = extract_ids(set_A, scope);
const std::unordered_set<GeometryId>& ids_B =
&set_A == &set_B ? ids_A : extract_ids(set_B, scope);
for (GeometryId id_A : ids_A) {
for (GeometryId id_B : ids_B) {
RemoveFilteredPair(id_A, id_B, state_out);
}
}
}
void CollisionFilter::AddFilteredPair(GeometryId id_A, GeometryId id_B,
bool is_invariant,
FilterState* state_out) {
FilterState& filter_state = *state_out;
DRAKE_DEMAND(filter_state.contains(id_A) && filter_state.contains(id_B));
if (id_A == id_B) return;
PairRelationship& pair_relation =
id_A < id_B ? filter_state[id_A][id_B] : filter_state[id_B][id_A];
if (pair_relation == kInvariantFilter) return;
pair_relation = is_invariant ? kInvariantFilter : kFiltered;
}
void CollisionFilter::RemoveFilteredPair(GeometryId id_A, GeometryId id_B,
FilterState* state_out) {
FilterState& filter_state = *state_out;
DRAKE_DEMAND(filter_state.contains(id_A) && filter_state.contains(id_B));
if (id_A == id_B) return;
PairRelationship& pair_relation =
id_A < id_B ? filter_state[id_A][id_B] : filter_state[id_B][id_A];
if (pair_relation == kInvariantFilter) return;
pair_relation = kUnfiltered;
}
bool CollisionFilter::operator==(const CollisionFilter& other) const {
if (this == &other) return true;
if (filter_state_.size() != other.filter_state_.size()) return false;
for (const auto& [this_id, this_map] : filter_state_) {
if (!other.HasGeometry(this_id)) return false;
for (const auto& [this_pair_id, can_collide] : this_map) {
unused(can_collide);
if (!other.HasGeometry(this_pair_id)) return false;
if (CanCollideWith(this_id, this_pair_id) !=
other.CanCollideWith(this_id, this_pair_id)) {
return false;
}
}
}
return true;
}
CollisionFilter CollisionFilter::MakeClearCopy() const {
auto clear_state = CollisionFilter::InitializeTransientState(
filter_state_, CollisionFilter::kUnfiltered);
CollisionFilter new_filter;
new_filter.filter_state_ = clear_state;
new_filter.filter_history_[0].filter_state = clear_state;
return new_filter;
}
void CollisionFilter::Apply(const CollisionFilterDeclaration& declaration,
const CollisionFilter::ExtractIds& extract_ids,
bool is_invariant, FilterState* filter_state) {
using Operation = CollisionFilterDeclaration::StatementOp;
const CollisionFilterScope scope = declaration.scope();
for (const auto& statement : declaration.statements()) {
switch (statement.operation) {
case Operation::kAllowBetween:
// Note: GeometryState should never be declaring is_invariant is true
// while removing collision filters.
DRAKE_DEMAND(!is_invariant);
RemoveFiltersBetween(statement.set_A, statement.set_B, extract_ids,
scope, filter_state);
break;
case Operation::kAllowWithin:
DRAKE_DEMAND(!is_invariant);
RemoveFiltersBetween(statement.set_A, statement.set_A, extract_ids,
scope, filter_state);
break;
case Operation::kExcludeWithin:
AddFiltersBetween(statement.set_A, statement.set_A, extract_ids, scope,
is_invariant, filter_state);
break;
case Operation::kExcludeBetween:
AddFiltersBetween(statement.set_A, statement.set_B, extract_ids, scope,
is_invariant, filter_state);
break;
}
}
}
void CollisionFilter::AddGeometry(GeometryId new_id,
FilterState* filter_state_out,
PairRelationship relationship) {
FilterState& filter_state = *filter_state_out;
DRAKE_DEMAND(!filter_state.contains(new_id));
GeometryMap& new_map = filter_state[new_id] = {};
for (auto& [other_id, other_map] : filter_state) {
/* Whichever id is *smaller* tracks the relationship with the other.
That relationship defaults to unfiltered (i.e., "can collide").
Note: we're iterating over filter_state_ and assigning to either new_map
or other_map. This doesn't invalidate the implicit iterator of the range
operator. We never change the *keys* of filter_state_ in this loop, only
its values. And we don't do any iteration in the new_map or other_map
so don't depend on their iterators. */
if (other_id < new_id) {
other_map[new_id] = relationship;
} else {
new_map[other_id] = relationship;
}
}
}
void CollisionFilter::RemoveGeometry(GeometryId remove_id,
FilterState* filter_state_out) {
FilterState& filter_state = *filter_state_out;
DRAKE_DEMAND(filter_state.contains(remove_id));
filter_state.erase(remove_id);
for (auto& [other_id, other_map] : filter_state) {
/* remove_id will only be found in maps belonging to geometries with ids
that are smaller than remove_id. Those that are larger were deleted when we
removed remove_id's entry. */
if (other_id < remove_id) {
other_map.erase(remove_id);
}
}
}
CollisionFilter::FilterState CollisionFilter::InitializeTransientState(
const FilterState& reference, PairRelationship default_relationship) {
FilterState new_state;
for (const auto& [id, _] : reference) {
unused(_);
AddGeometry(id, &new_state, default_relationship);
}
return new_state;
}
} // namespace internal
} // namespace geometry
} // namespace drake