John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_CONTROLS_POLYTOPE_H_ |
| 2 | #define AOS_CONTROLS_POLYTOPE_H_ |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 3 | |
| 4 | #include "Eigen/Dense" |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 5 | |
| 6 | #ifdef __linux__ |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 7 | // 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 Silverman | 258b917 | 2015-09-19 14:32:57 -0400 | [diff] [blame] | 10 | #include "third_party/cddlib/lib-src/setoper.h" |
| 11 | #include "third_party/cddlib/lib-src/cdd.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 12 | // clang-format on |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 13 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | #include "glog/logging.h" |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 15 | #endif // __linux__ |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 16 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 17 | namespace frc971 { |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 18 | namespace controls { |
| 19 | |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 20 | // 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 Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 27 | template <int number_of_dimensions, typename Scalar = double> |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 28 | class Polytope { |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 29 | public: |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 30 | virtual ~Polytope() {} |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 31 | |
| 32 | // Returns a reference to H. |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 33 | virtual Eigen::Ref< |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 34 | const Eigen::Matrix<Scalar, Eigen::Dynamic, number_of_dimensions>> |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 35 | H() const = 0; |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 36 | |
| 37 | // Returns a reference to k. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 38 | virtual Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k() |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 39 | const = 0; |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 40 | |
| 41 | // Returns the number of dimensions in the polytope. |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 42 | constexpr int ndim() const { return number_of_dimensions; } |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 43 | |
| 44 | // Returns the number of constraints currently in the polytope. |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 45 | int num_constraints() const { return k().rows(); } |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 46 | |
| 47 | // Returns true if the point is inside the polytope. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 48 | bool IsInside(Eigen::Matrix<Scalar, number_of_dimensions, 1> point) const; |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 49 | |
| 50 | // Returns the list of vertices inside the polytope. |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 51 | virtual Eigen::Matrix<Scalar, number_of_dimensions, Eigen::Dynamic> Vertices() |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 52 | const = 0; |
| 53 | }; |
| 54 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 55 | template <int number_of_dimensions, int num_vertices, typename Scalar = double> |
| 56 | Eigen::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 Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 60 | for (int i = 0; i < num_vertices; ++i) { |
| 61 | ans.col(i) += offset; |
| 62 | } |
| 63 | return ans; |
| 64 | } |
| 65 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 66 | template <int number_of_dimensions, int num_constraints, int num_vertices, |
| 67 | typename Scalar = double> |
| 68 | class HVPolytope : public Polytope<number_of_dimensions, Scalar> { |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 69 | public: |
| 70 | // Constructs a polytope given the H and k matrices. |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 71 | 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 Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 79 | : H_(H), k_(k), vertices_(vertices) {} |
| 80 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 81 | Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, number_of_dimensions>> |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 82 | static_H() const { |
| 83 | return H_; |
| 84 | } |
| 85 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 86 | Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, number_of_dimensions>> |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 87 | H() const override { |
| 88 | return H_; |
| 89 | } |
| 90 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 91 | Eigen::Ref<const Eigen::Matrix<Scalar, num_constraints, 1>> static_k() const { |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 92 | return k_; |
| 93 | } |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 94 | Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>> k() |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 95 | const override { |
| 96 | return k_; |
| 97 | } |
| 98 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 99 | Eigen::Matrix<Scalar, number_of_dimensions, Eigen::Dynamic> Vertices() |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 100 | const override { |
| 101 | return vertices_; |
| 102 | } |
| 103 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 104 | Eigen::Matrix<Scalar, number_of_dimensions, num_vertices> StaticVertices() |
| 105 | const { |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 106 | return vertices_; |
| 107 | } |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 108 | |
| 109 | private: |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 110 | 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 Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 113 | }; |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 114 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 115 | #ifdef __linux__ |
James Kuszmaul | 0ee1fce | 2020-01-11 16:59:21 -0800 | [diff] [blame] | 116 | |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 117 | template <int number_of_dimensions> |
| 118 | class HPolytope : public Polytope<number_of_dimensions> { |
| 119 | public: |
| 120 | // Constructs a polytope given the H and k matrices. |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 121 | 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 Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 125 | : 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 Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 140 | } |
| 141 | |
James Kuszmaul | 0ee1fce | 2020-01-11 16:59:21 -0800 | [diff] [blame] | 142 | // 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 Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 144 | Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> Vertices() |
| 145 | const override { |
| 146 | return vertices_; |
| 147 | } |
| 148 | |
| 149 | private: |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 150 | const Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions> H_; |
| 151 | const Eigen::Matrix<double, Eigen::Dynamic, 1> k_; |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 152 | const Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices_; |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 153 | |
| 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 Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 161 | #endif // __linux__ |
| 162 | |
| 163 | template <int number_of_dimensions, typename Scalar> |
| 164 | bool 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 Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 167 | for (int i = 0; i < k_ptr.rows(); ++i) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 168 | Scalar ev = (H().row(i) * point)(0, 0); |
Austin Schuh | 550e781 | 2017-11-19 12:11:02 -0800 | [diff] [blame] | 169 | if (ev > k_ptr(i, 0)) { |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 176 | #ifdef __linux__ |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 177 | template <int number_of_dimensions> |
| 178 | Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 179 | HPolytope<number_of_dimensions>::CalculateVertices( |
Austin Schuh | c7a0a3d | 2016-10-15 16:22:47 -0700 | [diff] [blame] | 180 | 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 Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 183 | dd_MatrixPtr matrix = dd_CreateMatrix(k.rows(), number_of_dimensions + 1); |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 184 | |
| 185 | // Copy the data over. TODO(aschuh): Is there a better way? I hate copying... |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 186 | 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 Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 190 | } |
| 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 201 | LOG(ERROR) << "bad H" << H; |
| 202 | LOG(ERROR) << "bad k_" << k; |
| 203 | LOG(FATAL) << "dd_DDMatrix2Poly failed"; |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 204 | } |
| 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 Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 218 | // Rays are unsupported right now. This may change in the future. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 219 | CHECK_EQ(0, num_rays); |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 220 | |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 221 | 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 Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 239 | #endif // __linux__ |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 240 | |
| 241 | } // namespace controls |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame] | 242 | } // namespace frc971 |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 243 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 244 | #endif // AOS_CONTROLS_POLYTOPE_H_ |