blob: 5248ea363df51fdec5660d5b6b8904b51ba70263 [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
John Park33858a32018-09-28 23:05:48 -070010#include "aos/logging/logging.h"
11#include "aos/logging/matrix_logging.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -070012#endif // __linux__
Brian Silvermanaba7bf62016-01-31 18:03:59 -050013
Austin Schuha6c257a2013-10-27 15:36:40 -070014namespace aos {
15namespace controls {
16
Brian Silvermanaba7bf62016-01-31 18:03:59 -050017// A number_of_dimensions dimensional polytope.
18// This represents the region consisting of all points X such that H * X <= k.
19// The vertices are calculated at construction time because we always use those
20// and libcdd is annoying about calculating vertices. In particular, for some
21// random-seeming polytopes it refuses to calculate the vertices completely. To
22// avoid issues with that, using the "shifting" constructor is recommended
23// whenever possible.
Austin Schuhbcce26a2018-03-26 23:41:24 -070024template <int number_of_dimensions, typename Scalar = double>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070025class Polytope {
Austin Schuha6c257a2013-10-27 15:36:40 -070026 public:
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070027 virtual ~Polytope() {}
Austin Schuha6c257a2013-10-27 15:36:40 -070028
29 // Returns a reference to H.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070030 virtual Eigen::Ref<
Austin Schuhbcce26a2018-03-26 23:41:24 -070031 const Eigen::Matrix<Scalar, Eigen::Dynamic, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070032 H() const = 0;
Austin Schuha6c257a2013-10-27 15:36:40 -070033
34 // Returns a reference to k.
Austin Schuhbcce26a2018-03-26 23:41:24 -070035 virtual Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070036 const = 0;
Austin Schuha6c257a2013-10-27 15:36:40 -070037
38 // Returns the number of dimensions in the polytope.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070039 constexpr int ndim() const { return number_of_dimensions; }
Austin Schuha6c257a2013-10-27 15:36:40 -070040
41 // Returns the number of constraints currently in the polytope.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070042 int num_constraints() const { return k().rows(); }
Austin Schuha6c257a2013-10-27 15:36:40 -070043
44 // Returns true if the point is inside the polytope.
Austin Schuhbcce26a2018-03-26 23:41:24 -070045 bool IsInside(Eigen::Matrix<Scalar, number_of_dimensions, 1> point) const;
Austin Schuha6c257a2013-10-27 15:36:40 -070046
47 // Returns the list of vertices inside the polytope.
Austin Schuhbcce26a2018-03-26 23:41:24 -070048 virtual Eigen::Matrix<Scalar, number_of_dimensions, Eigen::Dynamic> Vertices()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070049 const = 0;
50};
51
Austin Schuhbcce26a2018-03-26 23:41:24 -070052template <int number_of_dimensions, int num_vertices, typename Scalar = double>
53Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> ShiftPoints(
54 const Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> &vertices,
55 const Eigen::Matrix<Scalar, number_of_dimensions, 1> &offset) {
56 Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> ans = vertices;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070057 for (int i = 0; i < num_vertices; ++i) {
58 ans.col(i) += offset;
59 }
60 return ans;
61}
62
Austin Schuhbcce26a2018-03-26 23:41:24 -070063template <int number_of_dimensions, int num_constraints, int num_vertices,
64 typename Scalar = double>
65class HVPolytope : public Polytope<number_of_dimensions, Scalar> {
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070066 public:
67 // Constructs a polytope given the H and k matrices.
Austin Schuhbcce26a2018-03-26 23:41:24 -070068 HVPolytope(Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints,
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070069 number_of_dimensions>> H,
Austin Schuhbcce26a2018-03-26 23:41:24 -070070 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, 1>> k,
71 Eigen::Ref<const Eigen::Matrix<Scalar, number_of_dimensions,
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070072 num_vertices>> vertices)
73 : H_(H), k_(k), vertices_(vertices) {}
74
Austin Schuhbcce26a2018-03-26 23:41:24 -070075 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070076 static_H() const {
77 return H_;
78 }
79
Austin Schuhbcce26a2018-03-26 23:41:24 -070080 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070081 H() const override {
82 return H_;
83 }
84
Austin Schuhbcce26a2018-03-26 23:41:24 -070085 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, 1>> static_k()
Brian Silvermanaba7bf62016-01-31 18:03:59 -050086 const {
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070087 return k_;
88 }
Austin Schuhbcce26a2018-03-26 23:41:24 -070089 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070090 const override {
91 return k_;
92 }
93
Austin Schuhbcce26a2018-03-26 23:41:24 -070094 Eigen::Matrix<Scalar, number_of_dimensions, Eigen::Dynamic> Vertices()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070095 const override {
96 return vertices_;
97 }
98
Austin Schuhbcce26a2018-03-26 23:41:24 -070099 Eigen::Matrix<Scalar, number_of_dimensions, num_vertices>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700100 StaticVertices() const {
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500101 return vertices_;
102 }
Austin Schuha6c257a2013-10-27 15:36:40 -0700103
104 private:
Austin Schuhbcce26a2018-03-26 23:41:24 -0700105 const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions> H_;
106 const Eigen::Matrix<Scalar, num_constraints, 1> k_;
107 const Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> vertices_;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700108};
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500109
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500110
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700111
Austin Schuhbcce26a2018-03-26 23:41:24 -0700112#ifdef __linux__
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.
117 HPolytope(Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic,
118 number_of_dimensions>> H,
119 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> k)
120 : H_(H), k_(k), vertices_(CalculateVertices(H, k)) {}
121
122 // This is an initialization function shared across all instantiations of this
123 // template.
124 // This must be called at least once before creating any instances. It is
125 // not thread-safe.
126 static void Init() { dd_set_global_constants(); }
127
128 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>>
129 H() const override {
130 return H_;
131 }
132 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> k()
133 const override {
134 return k_;
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500135 }
136
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700137 Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> Vertices()
138 const override {
139 return vertices_;
140 }
141
142 private:
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500143 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions> H_;
144 const Eigen::Matrix<double, Eigen::Dynamic, 1> k_;
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500145 const Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices_;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700146
147 static Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic>
148 CalculateVertices(
149 Eigen::Ref<
150 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> &H,
151 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> &k);
Austin Schuha6c257a2013-10-27 15:36:40 -0700152};
153
Austin Schuhbcce26a2018-03-26 23:41:24 -0700154#endif // __linux__
155
156template <int number_of_dimensions, typename Scalar>
157bool Polytope<number_of_dimensions, Scalar>::IsInside(
158 Eigen::Matrix<Scalar, number_of_dimensions, 1> point) const {
159 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k_ptr = k();
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700160 for (int i = 0; i < k_ptr.rows(); ++i) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700161 Scalar ev = (H().row(i) * point)(0, 0);
Austin Schuh550e7812017-11-19 12:11:02 -0800162 if (ev > k_ptr(i, 0)) {
Austin Schuha6c257a2013-10-27 15:36:40 -0700163 return false;
164 }
165 }
166 return true;
167}
168
Austin Schuhbcce26a2018-03-26 23:41:24 -0700169#ifdef __linux__
Austin Schuha6c257a2013-10-27 15:36:40 -0700170template <int number_of_dimensions>
171Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic>
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500172HPolytope<number_of_dimensions>::CalculateVertices(
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700173 Eigen::Ref<
174 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> &H,
175 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> &k) {
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500176 dd_MatrixPtr matrix = dd_CreateMatrix(k.rows(), number_of_dimensions + 1);
Austin Schuha6c257a2013-10-27 15:36:40 -0700177
178 // Copy the data over. TODO(aschuh): Is there a better way? I hate copying...
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500179 for (int i = 0; i < k.rows(); ++i) {
180 dd_set_d(matrix->matrix[i][0], k(i, 0));
181 for (int j = 0; j < number_of_dimensions; ++j) {
182 dd_set_d(matrix->matrix[i][j + 1], -H(i, j));
Austin Schuha6c257a2013-10-27 15:36:40 -0700183 }
184 }
185
186 matrix->representation = dd_Inequality;
187 matrix->numbtype = dd_Real;
188
189 dd_ErrorType error;
190 dd_PolyhedraPtr polyhedra = dd_DDMatrix2Poly(matrix, &error);
191 if (error != dd_NoError || polyhedra == NULL) {
192 dd_WriteErrorMessages(stderr, error);
193 dd_FreeMatrix(matrix);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700194 AOS_LOG_MATRIX(ERROR, "bad H", H);
195 AOS_LOG_MATRIX(ERROR, "bad k_", k);
196 AOS_LOG(FATAL, "dd_DDMatrix2Poly failed\n");
Austin Schuha6c257a2013-10-27 15:36:40 -0700197 }
198
199 dd_MatrixPtr vertex_matrix = dd_CopyGenerators(polyhedra);
200
201 int num_vertices = 0;
202 int num_rays = 0;
203 for (int i = 0; i < vertex_matrix->rowsize; ++i) {
204 if (dd_get_d(vertex_matrix->matrix[i][0]) == 0) {
205 num_rays += 1;
206 } else {
207 num_vertices += 1;
208 }
209 }
210
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500211 // Rays are unsupported right now. This may change in the future.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700212 AOS_CHECK_EQ(0, num_rays);
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500213
Austin Schuha6c257a2013-10-27 15:36:40 -0700214 Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices(
215 number_of_dimensions, num_vertices);
216
217 int vertex_index = 0;
218 for (int i = 0; i < vertex_matrix->rowsize; ++i) {
219 if (dd_get_d(vertex_matrix->matrix[i][0]) != 0) {
220 for (int j = 0; j < number_of_dimensions; ++j) {
221 vertices(j, vertex_index) = dd_get_d(vertex_matrix->matrix[i][j + 1]);
222 }
223 ++vertex_index;
224 }
225 }
226 dd_FreeMatrix(vertex_matrix);
227 dd_FreePolyhedra(polyhedra);
228 dd_FreeMatrix(matrix);
229
230 return vertices;
231}
Austin Schuhbcce26a2018-03-26 23:41:24 -0700232#endif // __linux__
Austin Schuha6c257a2013-10-27 15:36:40 -0700233
234} // namespace controls
235} // namespace aos
236
John Park33858a32018-09-28 23:05:48 -0700237#endif // AOS_CONTROLS_POLYTOPE_H_