blob: 794219bd158f41110d6f59349110fe9876c1cafd [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
James Kuszmaul61750662021-06-21 21:32:33 -070017namespace frc971 {
Austin Schuha6c257a2013-10-27 15:36:40 -070018namespace controls {
19
Brian Silvermanaba7bf62016-01-31 18:03:59 -050020// A number_of_dimensions dimensional polytope.
21// This represents the region consisting of all points X such that H * X <= k.
22// The vertices are calculated at construction time because we always use those
23// and libcdd is annoying about calculating vertices. In particular, for some
24// random-seeming polytopes it refuses to calculate the vertices completely. To
25// avoid issues with that, using the "shifting" constructor is recommended
26// whenever possible.
Austin Schuhbcce26a2018-03-26 23:41:24 -070027template <int number_of_dimensions, typename Scalar = double>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070028class Polytope {
Austin Schuha6c257a2013-10-27 15:36:40 -070029 public:
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070030 virtual ~Polytope() {}
Austin Schuha6c257a2013-10-27 15:36:40 -070031
32 // Returns a reference to H.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070033 virtual Eigen::Ref<
Austin Schuhbcce26a2018-03-26 23:41:24 -070034 const Eigen::Matrix<Scalar, Eigen::Dynamic, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070035 H() const = 0;
Austin Schuha6c257a2013-10-27 15:36:40 -070036
37 // Returns a reference to k.
Austin Schuhbcce26a2018-03-26 23:41:24 -070038 virtual Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070039 const = 0;
Austin Schuha6c257a2013-10-27 15:36:40 -070040
41 // Returns the number of dimensions in the polytope.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070042 constexpr int ndim() const { return number_of_dimensions; }
Austin Schuha6c257a2013-10-27 15:36:40 -070043
44 // Returns the number of constraints currently in the polytope.
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070045 int num_constraints() const { return k().rows(); }
Austin Schuha6c257a2013-10-27 15:36:40 -070046
47 // Returns true if the point is inside the polytope.
Austin Schuhbcce26a2018-03-26 23:41:24 -070048 bool IsInside(Eigen::Matrix<Scalar, number_of_dimensions, 1> point) const;
Austin Schuha6c257a2013-10-27 15:36:40 -070049
50 // Returns the list of vertices inside the polytope.
Austin Schuhbcce26a2018-03-26 23:41:24 -070051 virtual Eigen::Matrix<Scalar, number_of_dimensions, Eigen::Dynamic> Vertices()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070052 const = 0;
53};
54
Austin Schuhbcce26a2018-03-26 23:41:24 -070055template <int number_of_dimensions, int num_vertices, typename Scalar = double>
56Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> ShiftPoints(
57 const Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> &vertices,
58 const Eigen::Matrix<Scalar, number_of_dimensions, 1> &offset) {
59 Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> ans = vertices;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070060 for (int i = 0; i < num_vertices; ++i) {
61 ans.col(i) += offset;
62 }
63 return ans;
64}
65
Austin Schuhbcce26a2018-03-26 23:41:24 -070066template <int number_of_dimensions, int num_constraints, int num_vertices,
67 typename Scalar = double>
68class HVPolytope : public Polytope<number_of_dimensions, Scalar> {
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070069 public:
70 // Constructs a polytope given the H and k matrices.
Philipp Schrader790cb542023-07-05 21:06:52 -070071 HVPolytope(
72 Eigen::Ref<
73 const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions>>
74 H,
75 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, 1>> k,
76 Eigen::Ref<
77 const Eigen::Matrix<Scalar, number_of_dimensions, num_vertices>>
78 vertices)
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070079 : H_(H), k_(k), vertices_(vertices) {}
80
Austin Schuhbcce26a2018-03-26 23:41:24 -070081 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070082 static_H() const {
83 return H_;
84 }
85
Austin Schuhbcce26a2018-03-26 23:41:24 -070086 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, number_of_dimensions>>
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070087 H() const override {
88 return H_;
89 }
90
Philipp Schrader790cb542023-07-05 21:06:52 -070091 Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, 1>> static_k() const {
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070092 return k_;
93 }
Austin Schuhbcce26a2018-03-26 23:41:24 -070094 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -070095 const override {
96 return k_;
97 }
98
Austin Schuhbcce26a2018-03-26 23:41:24 -070099 Eigen::Matrix<Scalar, number_of_dimensions, Eigen::Dynamic> Vertices()
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700100 const override {
101 return vertices_;
102 }
103
Philipp Schrader790cb542023-07-05 21:06:52 -0700104 Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> StaticVertices()
105 const {
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500106 return vertices_;
107 }
Austin Schuha6c257a2013-10-27 15:36:40 -0700108
109 private:
Austin Schuhbcce26a2018-03-26 23:41:24 -0700110 const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions> H_;
111 const Eigen::Matrix<Scalar, num_constraints, 1> k_;
112 const Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> vertices_;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700113};
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500114
Austin Schuhbcce26a2018-03-26 23:41:24 -0700115#ifdef __linux__
James Kuszmaul0ee1fce2020-01-11 16:59:21 -0800116
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700117template <int number_of_dimensions>
118class HPolytope : public Polytope<number_of_dimensions> {
119 public:
120 // Constructs a polytope given the H and k matrices.
Philipp Schrader790cb542023-07-05 21:06:52 -0700121 HPolytope(Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic,
122 number_of_dimensions>>
123 H,
124 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> k)
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700125 : H_(H), k_(k), vertices_(CalculateVertices(H, k)) {}
126
127 // This is an initialization function shared across all instantiations of this
128 // template.
129 // This must be called at least once before creating any instances. It is
130 // not thread-safe.
131 static void Init() { dd_set_global_constants(); }
132
133 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>>
134 H() const override {
135 return H_;
136 }
137 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> k()
138 const override {
139 return k_;
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500140 }
141
James Kuszmaul0ee1fce2020-01-11 16:59:21 -0800142 // NOTE: If you are getting bizarre errors that you are tracing back to the
143 // vertex matrix being funky, please check that you correctly called Init().
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700144 Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> Vertices()
145 const override {
146 return vertices_;
147 }
148
149 private:
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500150 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions> H_;
151 const Eigen::Matrix<double, Eigen::Dynamic, 1> k_;
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500152 const Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices_;
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700153
154 static Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic>
155 CalculateVertices(
156 Eigen::Ref<
157 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> &H,
158 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> &k);
Austin Schuha6c257a2013-10-27 15:36:40 -0700159};
160
Austin Schuhbcce26a2018-03-26 23:41:24 -0700161#endif // __linux__
162
163template <int number_of_dimensions, typename Scalar>
164bool Polytope<number_of_dimensions, Scalar>::IsInside(
165 Eigen::Matrix<Scalar, number_of_dimensions, 1> point) const {
166 Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k_ptr = k();
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700167 for (int i = 0; i < k_ptr.rows(); ++i) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700168 Scalar ev = (H().row(i) * point)(0, 0);
Austin Schuh550e7812017-11-19 12:11:02 -0800169 if (ev > k_ptr(i, 0)) {
Austin Schuha6c257a2013-10-27 15:36:40 -0700170 return false;
171 }
172 }
173 return true;
174}
175
Austin Schuhbcce26a2018-03-26 23:41:24 -0700176#ifdef __linux__
Austin Schuha6c257a2013-10-27 15:36:40 -0700177template <int number_of_dimensions>
178Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic>
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500179HPolytope<number_of_dimensions>::CalculateVertices(
Austin Schuhc7a0a3d2016-10-15 16:22:47 -0700180 Eigen::Ref<
181 const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions>> &H,
182 Eigen::Ref<const Eigen::Matrix<double, Eigen::Dynamic, 1>> &k) {
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500183 dd_MatrixPtr matrix = dd_CreateMatrix(k.rows(), number_of_dimensions + 1);
Austin Schuha6c257a2013-10-27 15:36:40 -0700184
185 // Copy the data over. TODO(aschuh): Is there a better way? I hate copying...
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500186 for (int i = 0; i < k.rows(); ++i) {
187 dd_set_d(matrix->matrix[i][0], k(i, 0));
188 for (int j = 0; j < number_of_dimensions; ++j) {
189 dd_set_d(matrix->matrix[i][j + 1], -H(i, j));
Austin Schuha6c257a2013-10-27 15:36:40 -0700190 }
191 }
192
193 matrix->representation = dd_Inequality;
194 matrix->numbtype = dd_Real;
195
196 dd_ErrorType error;
197 dd_PolyhedraPtr polyhedra = dd_DDMatrix2Poly(matrix, &error);
198 if (error != dd_NoError || polyhedra == NULL) {
199 dd_WriteErrorMessages(stderr, error);
200 dd_FreeMatrix(matrix);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700201 LOG(ERROR) << "bad H" << H;
202 LOG(ERROR) << "bad k_" << k;
203 LOG(FATAL) << "dd_DDMatrix2Poly failed";
Austin Schuha6c257a2013-10-27 15:36:40 -0700204 }
205
206 dd_MatrixPtr vertex_matrix = dd_CopyGenerators(polyhedra);
207
208 int num_vertices = 0;
209 int num_rays = 0;
210 for (int i = 0; i < vertex_matrix->rowsize; ++i) {
211 if (dd_get_d(vertex_matrix->matrix[i][0]) == 0) {
212 num_rays += 1;
213 } else {
214 num_vertices += 1;
215 }
216 }
217
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500218 // Rays are unsupported right now. This may change in the future.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700219 CHECK_EQ(0, num_rays);
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500220
Austin Schuha6c257a2013-10-27 15:36:40 -0700221 Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices(
222 number_of_dimensions, num_vertices);
223
224 int vertex_index = 0;
225 for (int i = 0; i < vertex_matrix->rowsize; ++i) {
226 if (dd_get_d(vertex_matrix->matrix[i][0]) != 0) {
227 for (int j = 0; j < number_of_dimensions; ++j) {
228 vertices(j, vertex_index) = dd_get_d(vertex_matrix->matrix[i][j + 1]);
229 }
230 ++vertex_index;
231 }
232 }
233 dd_FreeMatrix(vertex_matrix);
234 dd_FreePolyhedra(polyhedra);
235 dd_FreeMatrix(matrix);
236
237 return vertices;
238}
Austin Schuhbcce26a2018-03-26 23:41:24 -0700239#endif // __linux__
Austin Schuha6c257a2013-10-27 15:36:40 -0700240
241} // namespace controls
James Kuszmaul61750662021-06-21 21:32:33 -0700242} // namespace frc971
Austin Schuha6c257a2013-10-27 15:36:40 -0700243
John Park33858a32018-09-28 23:05:48 -0700244#endif // AOS_CONTROLS_POLYTOPE_H_