blob: 28f011edf3a3d82a636d69d9a400117ed34b59a2 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_CONTROLS_POLYTOPE_H_
2#define AOS_CONTROLS_POLYTOPE_H_
Austin Schuha6c257a2013-10-27 15:36:40 -07003
4#include "Eigen/Dense"
Austin Schuhbcce26a2018-03-26 23:41:24 -07005
6#ifdef __linux__
Brian Silverman258b9172015-09-19 14:32:57 -04007#include "third_party/cddlib/lib-src/setoper.h"
8#include "third_party/cddlib/lib-src/cdd.h"
Austin Schuha6c257a2013-10-27 15:36:40 -07009
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "glog/logging.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -070011#endif // __linux__
Brian Silvermanaba7bf62016-01-31 18:03:59 -050012
Austin Schuha6c257a2013-10-27 15:36:40 -070013namespace aos {
14namespace controls {
15
Brian Silvermanaba7bf62016-01-31 18:03:59 -050016// A number_of_dimensions dimensional polytope.
17// This represents the region consisting of all points X such that H * X <= k.
18// The vertices are calculated at construction time because we always use those
19// and libcdd is annoying about calculating vertices. In particular, for some
20// random-seeming polytopes it refuses to calculate the vertices completely. To
21// avoid issues with that, using the "shifting" constructor is recommended
22// whenever possible.
Austin Schuhbcce26a2018-03-26 23:41:24 -070023template <int number_of_dimensions, typename Scalar = double>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070024class Polytope {
Austin Schuha6c257a2013-10-27 15:36:40 -070025 public:
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070026 virtual ~Polytope() {}
Austin Schuha6c257a2013-10-27 15:36:40 -070027
28 // Returns a reference to H.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070029 virtual Eigen::Ref<
Austin Schuhbcce26a2018-03-26 23:41:24 -070030 const Eigen::Matrix<Scalar, Eigen::Dynamic, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070031 H() const = 0;
Austin Schuha6c257a2013-10-27 15:36:40 -070032
33 // Returns a reference to k.
Austin Schuhbcce26a2018-03-26 23:41:24 -070034 virtual Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070035 const = 0;
Austin Schuha6c257a2013-10-27 15:36:40 -070036
37 // Returns the number of dimensions in the polytope.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070038 constexpr int ndim() const { return number_of_dimensions; }
Austin Schuha6c257a2013-10-27 15:36:40 -070039
40 // Returns the number of constraints currently in the polytope.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070041 int num_constraints() const { return k().rows(); }
Austin Schuha6c257a2013-10-27 15:36:40 -070042
43 // Returns true if the point is inside the polytope.
Austin Schuhbcce26a2018-03-26 23:41:24 -070044 bool IsInside(Eigen::Matrix<Scalar, number_of_dimensions, 1> point) const;
Austin Schuha6c257a2013-10-27 15:36:40 -070045
46 // Returns the list of vertices inside the polytope.
Austin Schuhbcce26a2018-03-26 23:41:24 -070047 virtual Eigen::Matrix<Scalar, number_of_dimensions, Eigen::Dynamic> Vertices()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070048 const = 0;
49};
50
Austin Schuhbcce26a2018-03-26 23:41:24 -070051template <int number_of_dimensions, int num_vertices, typename Scalar = double>
52Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> ShiftPoints(
53 const Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> &vertices,
54 const Eigen::Matrix<Scalar, number_of_dimensions, 1> &offset) {
55 Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> ans = vertices;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070056 for (int i = 0; i < num_vertices; ++i) {
57 ans.col(i) += offset;
58 }
59 return ans;
60}
61
Austin Schuhbcce26a2018-03-26 23:41:24 -070062template <int number_of_dimensions, int num_constraints, int num_vertices,
63 typename Scalar = double>
64class HVPolytope : public Polytope<number_of_dimensions, Scalar> {
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070065 public:
66 // Constructs a polytope given the H and k matrices.
Austin Schuhbcce26a2018-03-26 23:41:24 -070067 HVPolytope(Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints,
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070068 number_of_dimensions>> H,
Austin Schuhbcce26a2018-03-26 23:41:24 -070069 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, 1>> k,
70 Eigen::Ref<const Eigen::Matrix<Scalar, number_of_dimensions,
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070071 num_vertices>> vertices)
72 : H_(H), k_(k), vertices_(vertices) {}
73
Austin Schuhbcce26a2018-03-26 23:41:24 -070074 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070075 static_H() const {
76 return H_;
77 }
78
Austin Schuhbcce26a2018-03-26 23:41:24 -070079 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070080 H() const override {
81 return H_;
82 }
83
Austin Schuhbcce26a2018-03-26 23:41:24 -070084 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, 1>> static_k()
Brian Silvermanaba7bf62016-01-31 18:03:59 -050085 const {
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070086 return k_;
87 }
Austin Schuhbcce26a2018-03-26 23:41:24 -070088 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070089 const override {
90 return k_;
91 }
92
Austin Schuhbcce26a2018-03-26 23:41:24 -070093 Eigen::Matrix<Scalar, number_of_dimensions, Eigen::Dynamic> Vertices()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070094 const override {
95 return vertices_;
96 }
97
Austin Schuhbcce26a2018-03-26 23:41:24 -070098 Eigen::Matrix<Scalar, number_of_dimensions, num_vertices>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070099 StaticVertices() const {
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500100 return vertices_;
101 }
Austin Schuha6c257a2013-10-27 15:36:40 -0700102
103 private:
Austin Schuhbcce26a2018-03-26 23:41:24 -0700104 const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions> H_;
105 const Eigen::Matrix<Scalar, num_constraints, 1> k_;
106 const Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> vertices_;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700107};
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500108
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500109
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700110
Austin Schuhbcce26a2018-03-26 23:41:24 -0700111#ifdef __linux__
James Kuszmaul0ee1fce2020-01-11 16:59:21 -0800112
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700113template <int number_of_dimensions>
114class HPolytope : public Polytope<number_of_dimensions> {
115 public:
116 // Constructs a polytope given the H and k matrices.
James Kuszmaul0ee1fce2020-01-11 16:59:21 -0800117 HPolytope(
118 Eigen::Ref<
119 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> H,
120 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> k)
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700121 : H_(H), k_(k), vertices_(CalculateVertices(H, k)) {}
122
123 // This is an initialization function shared across all instantiations of this
124 // template.
125 // This must be called at least once before creating any instances. It is
126 // not thread-safe.
127 static void Init() { dd_set_global_constants(); }
128
129 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>>
130 H() const override {
131 return H_;
132 }
133 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> k()
134 const override {
135 return k_;
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500136 }
137
James Kuszmaul0ee1fce2020-01-11 16:59:21 -0800138 // NOTE: If you are getting bizarre errors that you are tracing back to the
139 // vertex matrix being funky, please check that you correctly called Init().
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700140 Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> Vertices()
141 const override {
142 return vertices_;
143 }
144
145 private:
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500146 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions> H_;
147 const Eigen::Matrix<double, Eigen::Dynamic, 1> k_;
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500148 const Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices_;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700149
150 static Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic>
151 CalculateVertices(
152 Eigen::Ref<
153 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> &H,
154 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> &k);
Austin Schuha6c257a2013-10-27 15:36:40 -0700155};
156
Austin Schuhbcce26a2018-03-26 23:41:24 -0700157#endif // __linux__
158
159template <int number_of_dimensions, typename Scalar>
160bool Polytope<number_of_dimensions, Scalar>::IsInside(
161 Eigen::Matrix<Scalar, number_of_dimensions, 1> point) const {
162 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k_ptr = k();
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700163 for (int i = 0; i < k_ptr.rows(); ++i) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700164 Scalar ev = (H().row(i) * point)(0, 0);
Austin Schuh550e7812017-11-19 12:11:02 -0800165 if (ev > k_ptr(i, 0)) {
Austin Schuha6c257a2013-10-27 15:36:40 -0700166 return false;
167 }
168 }
169 return true;
170}
171
Austin Schuhbcce26a2018-03-26 23:41:24 -0700172#ifdef __linux__
Austin Schuha6c257a2013-10-27 15:36:40 -0700173template <int number_of_dimensions>
174Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic>
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500175HPolytope<number_of_dimensions>::CalculateVertices(
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700176 Eigen::Ref<
177 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> &H,
178 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> &k) {
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500179 dd_MatrixPtr matrix = dd_CreateMatrix(k.rows(), number_of_dimensions + 1);
Austin Schuha6c257a2013-10-27 15:36:40 -0700180
181 // Copy the data over. TODO(aschuh): Is there a better way? I hate copying...
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500182 for (int i = 0; i < k.rows(); ++i) {
183 dd_set_d(matrix->matrix[i][0], k(i, 0));
184 for (int j = 0; j < number_of_dimensions; ++j) {
185 dd_set_d(matrix->matrix[i][j + 1], -H(i, j));
Austin Schuha6c257a2013-10-27 15:36:40 -0700186 }
187 }
188
189 matrix->representation = dd_Inequality;
190 matrix->numbtype = dd_Real;
191
192 dd_ErrorType error;
193 dd_PolyhedraPtr polyhedra = dd_DDMatrix2Poly(matrix, &error);
194 if (error != dd_NoError || polyhedra == NULL) {
195 dd_WriteErrorMessages(stderr, error);
196 dd_FreeMatrix(matrix);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700197 LOG(ERROR) << "bad H" << H;
198 LOG(ERROR) << "bad k_" << k;
199 LOG(FATAL) << "dd_DDMatrix2Poly failed";
Austin Schuha6c257a2013-10-27 15:36:40 -0700200 }
201
202 dd_MatrixPtr vertex_matrix = dd_CopyGenerators(polyhedra);
203
204 int num_vertices = 0;
205 int num_rays = 0;
206 for (int i = 0; i < vertex_matrix->rowsize; ++i) {
207 if (dd_get_d(vertex_matrix->matrix[i][0]) == 0) {
208 num_rays += 1;
209 } else {
210 num_vertices += 1;
211 }
212 }
213
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500214 // Rays are unsupported right now. This may change in the future.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700215 CHECK_EQ(0, num_rays);
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500216
Austin Schuha6c257a2013-10-27 15:36:40 -0700217 Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices(
218 number_of_dimensions, num_vertices);
219
220 int vertex_index = 0;
221 for (int i = 0; i < vertex_matrix->rowsize; ++i) {
222 if (dd_get_d(vertex_matrix->matrix[i][0]) != 0) {
223 for (int j = 0; j < number_of_dimensions; ++j) {
224 vertices(j, vertex_index) = dd_get_d(vertex_matrix->matrix[i][j + 1]);
225 }
226 ++vertex_index;
227 }
228 }
229 dd_FreeMatrix(vertex_matrix);
230 dd_FreePolyhedra(polyhedra);
231 dd_FreeMatrix(matrix);
232
233 return vertices;
234}
Austin Schuhbcce26a2018-03-26 23:41:24 -0700235#endif // __linux__
Austin Schuha6c257a2013-10-27 15:36:40 -0700236
237} // namespace controls
238} // namespace aos
239
John Park33858a32018-09-28 23:05:48 -0700240#endif // AOS_CONTROLS_POLYTOPE_H_