Brian | a6553ed | 2014-04-02 21:26:46 -0700 | [diff] [blame] | 1 | #ifndef AOS_COMMON_CONTROLS_POLYTOPE_H_ |
| 2 | #define AOS_COMMON_CONTROLS_POLYTOPE_H_ |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 3 | |
| 4 | #include "Eigen/Dense" |
Brian Silverman | 8f1018b | 2013-11-17 21:35:15 -0800 | [diff] [blame] | 5 | #include "libcdd-094g-prefix/include/setoper.h" |
| 6 | #include "libcdd-094g-prefix/include/cdd.h" |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 7 | |
| 8 | namespace aos { |
| 9 | namespace controls { |
| 10 | |
| 11 | // A n dimension polytope. |
| 12 | template <int number_of_dimensions> |
| 13 | class HPolytope { |
| 14 | public: |
| 15 | // Constructs a polytope given the H and k matricies. |
| 16 | HPolytope(Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions> H, |
| 17 | Eigen::Matrix<double, Eigen::Dynamic, 1> k) |
| 18 | : H_(H), |
| 19 | k_(k) { |
| 20 | } |
| 21 | |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 22 | // This is an initialization function shared across all instantiations of this |
| 23 | // template. |
| 24 | // This must be called at least once before calling any of the methods. It is |
| 25 | // not thread-safe. |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 26 | static void Init() { |
| 27 | dd_set_global_constants(); |
| 28 | } |
| 29 | |
| 30 | // Returns a reference to H. |
| 31 | const Eigen::Matrix<double, Eigen::Dynamic, |
| 32 | number_of_dimensions> &H() const { |
| 33 | return H_; |
| 34 | } |
| 35 | |
| 36 | // Returns a reference to k. |
| 37 | const Eigen::Matrix<double, Eigen::Dynamic, |
| 38 | 1> &k() const { |
| 39 | return k_; |
| 40 | } |
| 41 | |
| 42 | // Returns the number of dimensions in the polytope. |
| 43 | int ndim() const { return number_of_dimensions; } |
| 44 | |
| 45 | // Returns the number of constraints currently in the polytope. |
| 46 | int num_constraints() const { return k_.rows(); } |
| 47 | |
| 48 | // Returns true if the point is inside the polytope. |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 49 | bool IsInside(Eigen::Matrix<double, number_of_dimensions, 1> point) const; |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 50 | |
| 51 | // Returns the list of vertices inside the polytope. |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 52 | Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> Vertices() const; |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | Eigen::Matrix<double, Eigen::Dynamic, number_of_dimensions> H_; |
| 56 | Eigen::Matrix<double, Eigen::Dynamic, 1> k_; |
| 57 | }; |
| 58 | |
| 59 | template <int number_of_dimensions> |
| 60 | bool HPolytope<number_of_dimensions>::IsInside( |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 61 | Eigen::Matrix<double, number_of_dimensions, 1> point) const { |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 62 | auto ev = H_ * point; |
| 63 | for (int i = 0; i < num_constraints(); ++i) { |
| 64 | if (ev(i, 0) > k_(i, 0)) { |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | template <int number_of_dimensions> |
| 72 | Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 73 | HPolytope<number_of_dimensions>::Vertices() const { |
Austin Schuh | a6c257a | 2013-10-27 15:36:40 -0700 | [diff] [blame] | 74 | dd_MatrixPtr matrix = dd_CreateMatrix(num_constraints(), ndim() + 1); |
| 75 | |
| 76 | // Copy the data over. TODO(aschuh): Is there a better way? I hate copying... |
| 77 | for (int i = 0; i < num_constraints(); ++i) { |
| 78 | dd_set_d(matrix->matrix[i][0], k_(i, 0)); |
| 79 | for (int j = 0; j < ndim(); ++j) { |
| 80 | dd_set_d(matrix->matrix[i][j + 1], -H_(i, j)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | matrix->representation = dd_Inequality; |
| 85 | matrix->numbtype = dd_Real; |
| 86 | |
| 87 | dd_ErrorType error; |
| 88 | dd_PolyhedraPtr polyhedra = dd_DDMatrix2Poly(matrix, &error); |
| 89 | if (error != dd_NoError || polyhedra == NULL) { |
| 90 | dd_WriteErrorMessages(stderr, error); |
| 91 | dd_FreeMatrix(matrix); |
| 92 | Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> ans(0, 0); |
| 93 | return ans; |
| 94 | } |
| 95 | |
| 96 | dd_MatrixPtr vertex_matrix = dd_CopyGenerators(polyhedra); |
| 97 | |
| 98 | int num_vertices = 0; |
| 99 | int num_rays = 0; |
| 100 | for (int i = 0; i < vertex_matrix->rowsize; ++i) { |
| 101 | if (dd_get_d(vertex_matrix->matrix[i][0]) == 0) { |
| 102 | num_rays += 1; |
| 103 | } else { |
| 104 | num_vertices += 1; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | Eigen::Matrix<double, number_of_dimensions, Eigen::Dynamic> vertices( |
| 109 | number_of_dimensions, num_vertices); |
| 110 | |
| 111 | int vertex_index = 0; |
| 112 | for (int i = 0; i < vertex_matrix->rowsize; ++i) { |
| 113 | if (dd_get_d(vertex_matrix->matrix[i][0]) != 0) { |
| 114 | for (int j = 0; j < number_of_dimensions; ++j) { |
| 115 | vertices(j, vertex_index) = dd_get_d(vertex_matrix->matrix[i][j + 1]); |
| 116 | } |
| 117 | ++vertex_index; |
| 118 | } |
| 119 | } |
| 120 | dd_FreeMatrix(vertex_matrix); |
| 121 | dd_FreePolyhedra(polyhedra); |
| 122 | dd_FreeMatrix(matrix); |
| 123 | |
| 124 | return vertices; |
| 125 | } |
| 126 | |
| 127 | } // namespace controls |
| 128 | } // namespace aos |
| 129 | |
Brian | a6553ed | 2014-04-02 21:26:46 -0700 | [diff] [blame] | 130 | #endif // AOS_COMMON_CONTROLS_POLYTOPE_H_ |