blob: cba25f678a8d3b6ccf502560fe636f959ce83219 [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__
Philipp Schrader790cb542023-07-05 21:06:52 -07007// For reasons I haven't looked into, these headers fail to compile when
8// included after the glog header. Prevent clang-format from ordering them.
9// clang-format off
Brian Silverman258b9172015-09-19 14:32:57 -040010#include "third_party/cddlib/lib-src/setoper.h"
11#include "third_party/cddlib/lib-src/cdd.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070012// clang-format on
Austin Schuha6c257a2013-10-27 15:36:40 -070013
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "glog/logging.h"
Austin Schuhbcce26a2018-03-26 23:41:24 -070015#endif // __linux__
Brian Silvermanaba7bf62016-01-31 18:03:59 -050016
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080017namespace frc971::controls {
Austin Schuha6c257a2013-10-27 15:36:40 -070018
Brian Silvermanaba7bf62016-01-31 18:03:59 -050019// A number_of_dimensions dimensional polytope.
20// This represents the region consisting of all points X such that H * X <= k.
21// The vertices are calculated at construction time because we always use those
22// and libcdd is annoying about calculating vertices. In particular, for some
23// random-seeming polytopes it refuses to calculate the vertices completely. To
24// avoid issues with that, using the "shifting" constructor is recommended
25// whenever possible.
Austin Schuhbcce26a2018-03-26 23:41:24 -070026template <int number_of_dimensions, typename Scalar = double>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070027class Polytope {
Austin Schuha6c257a2013-10-27 15:36:40 -070028 public:
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070029 virtual ~Polytope() {}
Austin Schuha6c257a2013-10-27 15:36:40 -070030
31 // Returns a reference to H.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070032 virtual Eigen::Ref<
Austin Schuhbcce26a2018-03-26 23:41:24 -070033 const Eigen::Matrix<Scalar, Eigen::Dynamic, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070034 H() const = 0;
Austin Schuha6c257a2013-10-27 15:36:40 -070035
36 // Returns a reference to k.
Austin Schuhbcce26a2018-03-26 23:41:24 -070037 virtual Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070038 const = 0;
Austin Schuha6c257a2013-10-27 15:36:40 -070039
40 // Returns the number of dimensions in the polytope.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070041 constexpr int ndim() const { return number_of_dimensions; }
Austin Schuha6c257a2013-10-27 15:36:40 -070042
43 // Returns the number of constraints currently in the polytope.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070044 int num_constraints() const { return k().rows(); }
Austin Schuha6c257a2013-10-27 15:36:40 -070045
46 // Returns true if the point is inside the polytope.
Austin Schuhbcce26a2018-03-26 23:41:24 -070047 bool IsInside(Eigen::Matrix<Scalar, number_of_dimensions, 1> point) const;
Austin Schuha6c257a2013-10-27 15:36:40 -070048
49 // Returns the list of vertices inside the polytope.
Austin Schuhbcce26a2018-03-26 23:41:24 -070050 virtual Eigen::Matrix<Scalar, number_of_dimensions, Eigen::Dynamic> Vertices()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070051 const = 0;
52};
53
Austin Schuhbcce26a2018-03-26 23:41:24 -070054template <int number_of_dimensions, int num_vertices, typename Scalar = double>
55Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> ShiftPoints(
56 const Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> &vertices,
57 const Eigen::Matrix<Scalar, number_of_dimensions, 1> &offset) {
58 Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> ans = vertices;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070059 for (int i = 0; i < num_vertices; ++i) {
60 ans.col(i) += offset;
61 }
62 return ans;
63}
64
Austin Schuhbcce26a2018-03-26 23:41:24 -070065template <int number_of_dimensions, int num_constraints, int num_vertices,
66 typename Scalar = double>
67class HVPolytope : public Polytope<number_of_dimensions, Scalar> {
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070068 public:
69 // Constructs a polytope given the H and k matrices.
Philipp Schrader790cb542023-07-05 21:06:52 -070070 HVPolytope(
71 Eigen::Ref<
72 const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions>>
73 H,
74 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, 1>> k,
75 Eigen::Ref<
76 const Eigen::Matrix<Scalar, number_of_dimensions, num_vertices>>
77 vertices)
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070078 : H_(H), k_(k), vertices_(vertices) {}
79
Austin Schuhbcce26a2018-03-26 23:41:24 -070080 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070081 static_H() const {
82 return H_;
83 }
84
Austin Schuhbcce26a2018-03-26 23:41:24 -070085 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070086 H() const override {
87 return H_;
88 }
89
Philipp Schrader790cb542023-07-05 21:06:52 -070090 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, 1>> static_k() const {
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070091 return k_;
92 }
Austin Schuhbcce26a2018-03-26 23:41:24 -070093 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070094 const override {
95 return k_;
96 }
97
Austin Schuhbcce26a2018-03-26 23:41:24 -070098 Eigen::Matrix<Scalar, number_of_dimensions, Eigen::Dynamic> Vertices()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070099 const override {
100 return vertices_;
101 }
102
Philipp Schrader790cb542023-07-05 21:06:52 -0700103 Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> StaticVertices()
104 const {
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500105 return vertices_;
106 }
Austin Schuha6c257a2013-10-27 15:36:40 -0700107
108 private:
Austin Schuhbcce26a2018-03-26 23:41:24 -0700109 const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions> H_;
110 const Eigen::Matrix<Scalar, num_constraints, 1> k_;
111 const Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> vertices_;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700112};
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500113
Austin Schuhbcce26a2018-03-26 23:41:24 -0700114#ifdef __linux__
James Kuszmaul0ee1fce2020-01-11 16:59:21 -0800115
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700116template <int number_of_dimensions>
117class HPolytope : public Polytope<number_of_dimensions> {
118 public:
119 // Constructs a polytope given the H and k matrices.
Philipp Schrader790cb542023-07-05 21:06:52 -0700120 HPolytope(Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic,
121 number_of_dimensions>>
122 H,
123 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> k)
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700124 : H_(H), k_(k), vertices_(CalculateVertices(H, k)) {}
125
126 // This is an initialization function shared across all instantiations of this
127 // template.
128 // This must be called at least once before creating any instances. It is
129 // not thread-safe.
130 static void Init() { dd_set_global_constants(); }
131
132 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>>
133 H() const override {
134 return H_;
135 }
136 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> k()
137 const override {
138 return k_;
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500139 }
140
James Kuszmaul0ee1fce2020-01-11 16:59:21 -0800141 // NOTE: If you are getting bizarre errors that you are tracing back to the
142 // vertex matrix being funky, please check that you correctly called Init().
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700143 Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> Vertices()
144 const override {
145 return vertices_;
146 }
147
148 private:
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500149 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions> H_;
150 const Eigen::Matrix<double, Eigen::Dynamic, 1> k_;
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500151 const Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices_;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700152
153 static Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic>
154 CalculateVertices(
155 Eigen::Ref<
156 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> &H,
157 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> &k);
Austin Schuha6c257a2013-10-27 15:36:40 -0700158};
159
Austin Schuhbcce26a2018-03-26 23:41:24 -0700160#endif // __linux__
161
162template <int number_of_dimensions, typename Scalar>
163bool Polytope<number_of_dimensions, Scalar>::IsInside(
164 Eigen::Matrix<Scalar, number_of_dimensions, 1> point) const {
165 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k_ptr = k();
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700166 for (int i = 0; i < k_ptr.rows(); ++i) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700167 Scalar ev = (H().row(i) * point)(0, 0);
Austin Schuh550e7812017-11-19 12:11:02 -0800168 if (ev > k_ptr(i, 0)) {
Austin Schuha6c257a2013-10-27 15:36:40 -0700169 return false;
170 }
171 }
172 return true;
173}
174
Austin Schuhbcce26a2018-03-26 23:41:24 -0700175#ifdef __linux__
Austin Schuha6c257a2013-10-27 15:36:40 -0700176template <int number_of_dimensions>
177Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic>
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500178HPolytope<number_of_dimensions>::CalculateVertices(
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700179 Eigen::Ref<
180 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> &H,
181 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> &k) {
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500182 dd_MatrixPtr matrix = dd_CreateMatrix(k.rows(), number_of_dimensions + 1);
Austin Schuha6c257a2013-10-27 15:36:40 -0700183
184 // Copy the data over. TODO(aschuh): Is there a better way? I hate copying...
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500185 for (int i = 0; i < k.rows(); ++i) {
186 dd_set_d(matrix->matrix[i][0], k(i, 0));
187 for (int j = 0; j < number_of_dimensions; ++j) {
188 dd_set_d(matrix->matrix[i][j + 1], -H(i, j));
Austin Schuha6c257a2013-10-27 15:36:40 -0700189 }
190 }
191
192 matrix->representation = dd_Inequality;
193 matrix->numbtype = dd_Real;
194
195 dd_ErrorType error;
196 dd_PolyhedraPtr polyhedra = dd_DDMatrix2Poly(matrix, &error);
197 if (error != dd_NoError || polyhedra == NULL) {
198 dd_WriteErrorMessages(stderr, error);
199 dd_FreeMatrix(matrix);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700200 LOG(ERROR) << "bad H" << H;
201 LOG(ERROR) << "bad k_" << k;
202 LOG(FATAL) << "dd_DDMatrix2Poly failed";
Austin Schuha6c257a2013-10-27 15:36:40 -0700203 }
204
205 dd_MatrixPtr vertex_matrix = dd_CopyGenerators(polyhedra);
206
207 int num_vertices = 0;
208 int num_rays = 0;
209 for (int i = 0; i < vertex_matrix->rowsize; ++i) {
210 if (dd_get_d(vertex_matrix->matrix[i][0]) == 0) {
211 num_rays += 1;
212 } else {
213 num_vertices += 1;
214 }
215 }
216
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500217 // Rays are unsupported right now. This may change in the future.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700218 CHECK_EQ(0, num_rays);
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500219
Austin Schuha6c257a2013-10-27 15:36:40 -0700220 Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices(
221 number_of_dimensions, num_vertices);
222
223 int vertex_index = 0;
224 for (int i = 0; i < vertex_matrix->rowsize; ++i) {
225 if (dd_get_d(vertex_matrix->matrix[i][0]) != 0) {
226 for (int j = 0; j < number_of_dimensions; ++j) {
227 vertices(j, vertex_index) = dd_get_d(vertex_matrix->matrix[i][j + 1]);
228 }
229 ++vertex_index;
230 }
231 }
232 dd_FreeMatrix(vertex_matrix);
233 dd_FreePolyhedra(polyhedra);
234 dd_FreeMatrix(matrix);
235
236 return vertices;
237}
Austin Schuhbcce26a2018-03-26 23:41:24 -0700238#endif // __linux__
Austin Schuha6c257a2013-10-27 15:36:40 -0700239
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800240} // namespace frc971::controls
Austin Schuha6c257a2013-10-27 15:36:40 -0700241
John Park33858a32018-09-28 23:05:48 -0700242#endif // AOS_CONTROLS_POLYTOPE_H_