/
minkowski_sum.cc
296 lines (268 loc) · 10 KB
/
minkowski_sum.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
#include "drake/geometry/optimization/minkowski_sum.h"
#include <memory>
#include <fmt/format.h>
#include "drake/common/copyable_unique_ptr.h"
#include "drake/geometry/optimization/hpolyhedron.h"
#include "drake/geometry/optimization/hyperellipsoid.h"
#include "drake/solvers/mathematical_program.h"
#include "drake/solvers/solve.h"
namespace drake {
namespace geometry {
namespace optimization {
using Eigen::MatrixXd;
using Eigen::RowVectorXd;
using Eigen::Vector3d;
using Eigen::VectorXd;
using math::RigidTransformd;
using solvers::Binding;
using solvers::Constraint;
using solvers::MathematicalProgram;
using solvers::Solve;
using solvers::VectorXDecisionVariable;
using symbolic::Variable;
namespace {
int GetAmbientDimension(const ConvexSets& sets) {
if (sets.empty()) {
return 0;
}
const int ambient_dimension = sets[0]->ambient_dimension();
for (const copyable_unique_ptr<ConvexSet>& set : sets) {
DRAKE_THROW_UNLESS(set != nullptr);
DRAKE_THROW_UNLESS(set->ambient_dimension() == ambient_dimension);
}
return ambient_dimension;
}
} // namespace
MinkowskiSum::MinkowskiSum() : MinkowskiSum(ConvexSets{}) {}
MinkowskiSum::MinkowskiSum(const ConvexSets& sets)
: ConvexSet(GetAmbientDimension(sets), false), sets_(sets) {}
MinkowskiSum::MinkowskiSum(const ConvexSet& setA, const ConvexSet& setB)
: ConvexSet(setA.ambient_dimension(), false) {
DRAKE_THROW_UNLESS(setB.ambient_dimension() == setA.ambient_dimension());
sets_.emplace_back(setA.Clone());
sets_.emplace_back(setB.Clone());
}
MinkowskiSum::MinkowskiSum(const QueryObject<double>& query_object,
GeometryId geometry_id,
std::optional<FrameId> reference_frame)
: ConvexSet(3, false) {
const Shape& shape = query_object.inspector().GetShape(geometry_id);
if (shape.type_name() != "Capsule") {
throw std::logic_error(fmt::format(
"MinkowskiSum(geometry_id={}, ...) cannot convert a {}, only a Capsule",
geometry_id, shape));
}
const Capsule& capsule = dynamic_cast<const Capsule&>(shape);
// Sphere at zero.
sets_.emplace_back(
Hyperellipsoid::MakeHypersphere(capsule.radius(), Vector3d::Zero())
.Clone());
const RigidTransformd X_WF =
reference_frame ? query_object.GetPoseInWorld(*reference_frame)
: RigidTransformd::Identity();
const RigidTransformd& X_WG = query_object.GetPoseInWorld(geometry_id);
const RigidTransformd X_GF = X_WG.InvertAndCompose(X_WF);
// Line segment as a HPolyhedron (the VPolytope would be easier here, but
// HPolyhedron is nicer for most of the computations).
HPolyhedron H_G =
HPolyhedron::MakeBox(Vector3d{0, 0, -capsule.length() / 2.0},
Vector3d{0, 0, capsule.length() / 2.0});
// A_G*(p_GF + R_GF*p_FF_var) ≤ b_G
sets_.emplace_back(
std::make_unique<HPolyhedron>(H_G.A() * X_GF.rotation().matrix(),
H_G.b() - H_G.A() * X_GF.translation()));
}
MinkowskiSum::~MinkowskiSum() = default;
const ConvexSet& MinkowskiSum::term(int index) const {
DRAKE_THROW_UNLESS(0 <= index && index < ssize(sets_));
return *sets_[index];
}
std::unique_ptr<ConvexSet> MinkowskiSum::DoClone() const {
return std::make_unique<MinkowskiSum>(*this);
}
std::optional<bool> MinkowskiSum::DoIsBoundedShortcut() const {
for (const auto& s : sets_) {
if (!s->IsBounded()) {
return false;
}
}
return true;
}
bool MinkowskiSum::DoIsEmpty() const {
if (sets_.size() == 0) {
return false;
}
// The empty set is annihilatory in Minkowski addition.
for (const auto& s : sets_) {
if (s->IsEmpty()) {
return true;
}
}
return false;
}
std::optional<VectorXd> MinkowskiSum::DoMaybeGetPoint() const {
std::optional<VectorXd> result;
for (const auto& s : sets_) {
if (std::optional<VectorXd> point = s->MaybeGetPoint()) {
if (result.has_value()) {
*result += *point;
} else {
result = std::move(point);
}
} else {
return std::nullopt;
}
}
return result;
}
std::optional<VectorXd> MinkowskiSum::DoMaybeGetFeasiblePoint() const {
std::optional<VectorXd> result;
for (const auto& s : sets_) {
if (std::optional<VectorXd> point = s->MaybeGetFeasiblePoint()) {
if (result.has_value()) {
*result += *point;
} else {
result = std::move(point);
}
} else {
return std::nullopt;
}
}
return result;
}
bool MinkowskiSum::DoPointInSet(const Eigen::Ref<const VectorXd>& x,
double) const {
// TODO(russt): Figure out if there is a general way to communicate tol
// to/through the solver.
MathematicalProgram prog;
auto X = prog.NewContinuousVariables(ambient_dimension(), num_terms(), "x");
const VectorXd ones = VectorXd::Ones(num_terms());
for (int i = 0; i < ambient_dimension(); ++i) {
// ∑ⱼ xⱼ[i] = x[i]
prog.AddLinearEqualityConstraint(ones, x[i], X.row(i).transpose());
}
for (int j = 0; j < num_terms(); ++j) {
sets_[j]->AddPointInSetConstraints(&prog, X.col(j));
}
auto result = Solve(prog);
return result.is_success();
}
std::pair<VectorX<Variable>, std::vector<Binding<Constraint>>>
MinkowskiSum::DoAddPointInSetConstraints(
MathematicalProgram* prog,
const Eigen::Ref<const VectorXDecisionVariable>& x) const {
std::vector<Variable> new_vars;
std::vector<Binding<Constraint>> new_constraints;
auto X = prog->NewContinuousVariables(ambient_dimension(), num_terms(), "x");
new_vars.reserve(X.size());
for (int j = 0; j < X.cols(); ++j) {
for (int i = 0; i < X.rows(); ++i) {
new_vars.push_back(X(i, j));
}
}
RowVectorXd a = RowVectorXd::Ones(num_terms() + 1);
a[0] = -1;
for (int i = 0; i < ambient_dimension(); ++i) {
// ∑ⱼ xⱼ[i] = x[i]
new_constraints.push_back(prog->AddLinearEqualityConstraint(
a, 0.0, {Vector1<Variable>(x[i]), X.row(i).transpose()}));
}
for (int j = 0; j < num_terms(); ++j) {
if (sets_[j]->ambient_dimension() == 0) {
std::optional<Variable> new_var =
ConvexSet::HandleZeroAmbientDimensionConstraints(prog, *sets_[j],
&new_constraints);
if (new_var.has_value()) {
new_vars.push_back(new_var.value());
}
continue;
}
const auto [new_vars_in_sets_j, new_constraints_in_sets_j] =
sets_[j]->AddPointInSetConstraints(prog, X.col(j));
for (int k = 0; k < new_vars_in_sets_j.rows(); ++k) {
new_vars.push_back(new_vars_in_sets_j(k));
}
new_constraints.insert(new_constraints.end(),
new_constraints_in_sets_j.begin(),
new_constraints_in_sets_j.end());
}
VectorX<Variable> new_vars_vec =
Eigen::Map<VectorX<Variable>>(new_vars.data(), new_vars.size());
return {std::move(new_vars_vec), std::move(new_constraints)};
}
std::vector<Binding<Constraint>>
MinkowskiSum::DoAddPointInNonnegativeScalingConstraints(
MathematicalProgram* prog,
const Eigen::Ref<const VectorXDecisionVariable>& x,
const Variable& t) const {
// We add the constraint
// x in t (S1 ⨁ ... ⨁ Sn)
// by enforcing
// x in t S1 ⨁ ... ⨁ t Sn.
// This can be done because t is nonnegative and S1,..., Sn are convex.
std::vector<Binding<Constraint>> constraints;
auto X = prog->NewContinuousVariables(ambient_dimension(), num_terms(), "x");
RowVectorXd a = RowVectorXd::Ones(num_terms() + 1);
a[0] = -1;
for (int i = 0; i < ambient_dimension(); ++i) {
// ∑ⱼ xⱼ[i] = x[i]
constraints.emplace_back(prog->AddLinearEqualityConstraint(
a, 0.0, {Vector1<Variable>(x[i]), X.row(i).transpose()}));
}
for (int j = 0; j < num_terms(); ++j) {
if (sets_[j]->ambient_dimension() == 0) {
ConvexSet::HandleZeroAmbientDimensionConstraints(prog, *sets_[j],
&constraints);
continue;
}
auto new_constraints =
sets_[j]->AddPointInNonnegativeScalingConstraints(prog, X.col(j), t);
constraints.insert(constraints.end(),
std::make_move_iterator(new_constraints.begin()),
std::make_move_iterator(new_constraints.end()));
}
return constraints;
}
std::vector<Binding<Constraint>>
MinkowskiSum::DoAddPointInNonnegativeScalingConstraints(
solvers::MathematicalProgram* prog, const Eigen::Ref<const MatrixXd>& A,
const Eigen::Ref<const VectorXd>& b, const Eigen::Ref<const VectorXd>& c,
double d, const Eigen::Ref<const VectorXDecisionVariable>& x,
const Eigen::Ref<const VectorXDecisionVariable>& t) const {
// We add the constraint
// A*x+b in (c't+d) (S1 ⨁ ... ⨁ Sn)
// by enforcing
// A*x+b in (c't+d) S1 ⨁ ... ⨁ (c't+d) Sn.
// This can be done because c't+d is nonnegative and S1,..., Sn are convex.
std::vector<Binding<Constraint>> constraints;
auto X = prog->NewContinuousVariables(x.size(), num_terms(), "x");
RowVectorXd a = RowVectorXd::Ones(num_terms() + 1);
a[0] = -1;
for (int i = 0; i < x.size(); ++i) {
// ∑ⱼ xⱼ[i] = x[i]
constraints.emplace_back(prog->AddLinearEqualityConstraint(
a, 0.0, {Vector1<Variable>(x[i]), X.row(i).transpose()}));
}
for (int j = 0; j < num_terms(); ++j) {
if (sets_[j]->ambient_dimension() == 0) {
ConvexSet::HandleZeroAmbientDimensionConstraints(prog, *sets_[j],
&constraints);
continue;
}
auto new_constraints = sets_[j]->AddPointInNonnegativeScalingConstraints(
prog, A, b, c, d, X.col(j), t);
constraints.insert(constraints.end(),
std::make_move_iterator(new_constraints.begin()),
std::make_move_iterator(new_constraints.end()));
}
return constraints;
}
std::pair<std::unique_ptr<Shape>, math::RigidTransformd>
MinkowskiSum::DoToShapeWithPose() const {
// TODO(russt): Consider handling Capsule as a (very) special case.
throw std::runtime_error(
"ToShapeWithPose is not implemented yet for MinkowskiSum.");
}
} // namespace optimization
} // namespace geometry
} // namespace drake