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