blob: c63166f4d10b84e85de62935f0acde8f7dedc387 [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__
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700112template <int number_of_dimensions>
113class HPolytope : public Polytope<number_of_dimensions> {
114 public:
115 // Constructs a polytope given the H and k matrices.
116 HPolytope(Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic,
117 number_of_dimensions>> H,
118 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> k)
119 : H_(H), k_(k), vertices_(CalculateVertices(H, k)) {}
120
121 // This is an initialization function shared across all instantiations of this
122 // template.
123 // This must be called at least once before creating any instances. It is
124 // not thread-safe.
125 static void Init() { dd_set_global_constants(); }
126
127 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>>
128 H() const override {
129 return H_;
130 }
131 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> k()
132 const override {
133 return k_;
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500134 }
135
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700136 Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> Vertices()
137 const override {
138 return vertices_;
139 }
140
141 private:
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500142 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions> H_;
143 const Eigen::Matrix<double, Eigen::Dynamic, 1> k_;
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500144 const Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices_;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700145
146 static Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic>
147 CalculateVertices(
148 Eigen::Ref<
149 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> &H,
150 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> &k);
Austin Schuha6c257a2013-10-27 15:36:40 -0700151};
152
Austin Schuhbcce26a2018-03-26 23:41:24 -0700153#endif // __linux__
154
155template <int number_of_dimensions, typename Scalar>
156bool Polytope<number_of_dimensions, Scalar>::IsInside(
157 Eigen::Matrix<Scalar, number_of_dimensions, 1> point) const {
158 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k_ptr = k();
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700159 for (int i = 0; i < k_ptr.rows(); ++i) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700160 Scalar ev = (H().row(i) * point)(0, 0);
Austin Schuh550e7812017-11-19 12:11:02 -0800161 if (ev > k_ptr(i, 0)) {
Austin Schuha6c257a2013-10-27 15:36:40 -0700162 return false;
163 }
164 }
165 return true;
166}
167
Austin Schuhbcce26a2018-03-26 23:41:24 -0700168#ifdef __linux__
Austin Schuha6c257a2013-10-27 15:36:40 -0700169template <int number_of_dimensions>
170Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic>
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500171HPolytope<number_of_dimensions>::CalculateVertices(
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700172 Eigen::Ref<
173 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> &H,
174 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> &k) {
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500175 dd_MatrixPtr matrix = dd_CreateMatrix(k.rows(), number_of_dimensions + 1);
Austin Schuha6c257a2013-10-27 15:36:40 -0700176
177 // Copy the data over. TODO(aschuh): Is there a better way? I hate copying...
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500178 for (int i = 0; i < k.rows(); ++i) {
179 dd_set_d(matrix->matrix[i][0], k(i, 0));
180 for (int j = 0; j < number_of_dimensions; ++j) {
181 dd_set_d(matrix->matrix[i][j + 1], -H(i, j));
Austin Schuha6c257a2013-10-27 15:36:40 -0700182 }
183 }
184
185 matrix->representation = dd_Inequality;
186 matrix->numbtype = dd_Real;
187
188 dd_ErrorType error;
189 dd_PolyhedraPtr polyhedra = dd_DDMatrix2Poly(matrix, &error);
190 if (error != dd_NoError || polyhedra == NULL) {
191 dd_WriteErrorMessages(stderr, error);
192 dd_FreeMatrix(matrix);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 LOG(ERROR) << "bad H" << H;
194 LOG(ERROR) << "bad k_" << k;
195 LOG(FATAL) << "dd_DDMatrix2Poly failed";
Austin Schuha6c257a2013-10-27 15:36:40 -0700196 }
197
198 dd_MatrixPtr vertex_matrix = dd_CopyGenerators(polyhedra);
199
200 int num_vertices = 0;
201 int num_rays = 0;
202 for (int i = 0; i < vertex_matrix->rowsize; ++i) {
203 if (dd_get_d(vertex_matrix->matrix[i][0]) == 0) {
204 num_rays += 1;
205 } else {
206 num_vertices += 1;
207 }
208 }
209
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500210 // Rays are unsupported right now. This may change in the future.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700211 CHECK_EQ(0, num_rays);
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500212
Austin Schuha6c257a2013-10-27 15:36:40 -0700213 Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices(
214 number_of_dimensions, num_vertices);
215
216 int vertex_index = 0;
217 for (int i = 0; i < vertex_matrix->rowsize; ++i) {
218 if (dd_get_d(vertex_matrix->matrix[i][0]) != 0) {
219 for (int j = 0; j < number_of_dimensions; ++j) {
220 vertices(j, vertex_index) = dd_get_d(vertex_matrix->matrix[i][j + 1]);
221 }
222 ++vertex_index;
223 }
224 }
225 dd_FreeMatrix(vertex_matrix);
226 dd_FreePolyhedra(polyhedra);
227 dd_FreeMatrix(matrix);
228
229 return vertices;
230}
Austin Schuhbcce26a2018-03-26 23:41:24 -0700231#endif // __linux__
Austin Schuha6c257a2013-10-27 15:36:40 -0700232
233} // namespace controls
234} // namespace aos
235
John Park33858a32018-09-28 23:05:48 -0700236#endif // AOS_CONTROLS_POLYTOPE_H_