blob: 0a0124865e85e4e95261c964edb87a4572d97cdb [file] [log] [blame]
James Kuszmaul61750662021-06-21 21:32:33 -07001#include "frc971/control_loops/polytope.h"
Brian Silvermanaba7bf62016-01-31 18:03:59 -05002
3#include <vector>
4
5#include "Eigen/Dense"
Brian Silvermanaba7bf62016-01-31 18:03:59 -05006#include "gmock/gmock.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "gtest/gtest.h"
Brian Silvermanaba7bf62016-01-31 18:03:59 -05008
9#include "aos/testing/test_logging.h"
10
James Kuszmaul61750662021-06-21 21:32:33 -070011namespace frc971 {
Brian Silvermanaba7bf62016-01-31 18:03:59 -050012namespace controls {
13
14class HPolytopeTest : public ::testing::Test {
15 protected:
16 HPolytope<2> Polytope1() {
17 return HPolytope<2>{
18 (Eigen::Matrix<double, 4, 2>() << 1, 0, -1, 0, 0, 1, 0, -1).finished(),
19 (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0, 12.0, 12.0).finished()};
20 }
21 HPolytope<2> Polytope2() {
22 return HPolytope<2>{
23 (Eigen::Matrix<double, 4, 2>() << 1, 1, -1, -1, 0, 1, 0, -1).finished(),
24 (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0, 12.0, 12.0).finished()};
25 }
26 HPolytope<1> Polytope3() {
27 return HPolytope<1>{(Eigen::Matrix<double, 2, 1>() << 1, -0.5).finished(),
28 (Eigen::Matrix<double, 2, 1>() << 5.0, 2.0).finished()};
29 }
30 HPolytope<2> Polytope4() {
31 return HPolytope<2>{
32 (Eigen::Matrix<double, 4, 2>() << 1, 1, -1, -1, 1, -1, -1, 1)
33 .finished(),
34 (Eigen::Matrix<double, 4, 1>() << 2, -1, 2, -1).finished()};
35 }
36 HPolytope<2> Polytope5() {
37 return HPolytope<2>{
38 (Eigen::Matrix<double, 4, 2>() << 1, 1, -1, -1, 1, -1, -1, 1)
39 .finished(),
40 (Eigen::Matrix<double, 4, 1>() << 1.5, -0.5, 1.5, -0.5).finished()};
41 }
42
43 void SetUp() override {
44 ::aos::testing::EnableTestLogging();
45 ::aos::testing::ForcePrintLogsDuringTests();
46 HPolytope<0>::Init();
47 }
48
49 template <typename T>
50 ::std::vector<::std::vector<double>> MatrixToVectors(const T &matrix) {
51 ::std::vector<::std::vector<double>> r;
52 for (int i = 0; i < matrix.cols(); ++i) {
53 ::std::vector<double> col;
54 for (int j = 0; j < matrix.rows(); ++j) {
55 col.emplace_back(matrix(j, i));
56 }
57 r.emplace_back(col);
58 }
59 return r;
60 }
61
62 template <typename T>
63 ::std::vector<::testing::Matcher<::std::vector<double>>> MatrixToMatchers(
64 const T &matrix) {
65 ::std::vector<::testing::Matcher<::std::vector<double>>> r;
66 for (int i = 0; i < matrix.cols(); ++i) {
67 ::std::vector<::testing::Matcher<double>> col;
68 for (int j = 0; j < matrix.rows(); ++j) {
69 col.emplace_back(::testing::DoubleNear(matrix(j, i), 0.000001));
70 }
71 r.emplace_back(::testing::ElementsAreArray(col));
72 }
73 return r;
74 }
Brian Silvermanaba7bf62016-01-31 18:03:59 -050075};
76
77// Tests that the vertices for various polytopes calculated from H and k are
78// correct.
79TEST_F(HPolytopeTest, CalculatedVertices) {
Philipp Schrader790cb542023-07-05 21:06:52 -070080 EXPECT_THAT(
81 MatrixToVectors(Polytope1().Vertices()),
82 ::testing::UnorderedElementsAreArray(MatrixToVectors(
83 (Eigen::Matrix<double, 2, 4>() << -12, -12, 12, 12, -12, 12, 12, -12)
84 .finished())));
85 EXPECT_THAT(
86 MatrixToVectors(Polytope2().Vertices()),
87 ::testing::UnorderedElementsAreArray(MatrixToVectors(
88 (Eigen::Matrix<double, 2, 4>() << 24, 0, -24, 0, -12, 12, 12, -12)
89 .finished())));
Brian Silvermanaba7bf62016-01-31 18:03:59 -050090 EXPECT_THAT(MatrixToVectors(Polytope3().Vertices()),
91 ::testing::UnorderedElementsAreArray(MatrixToVectors(
92 (Eigen::Matrix<double, 1, 2>() << 5, -4).finished())));
Philipp Schrader790cb542023-07-05 21:06:52 -070093 EXPECT_THAT(
94 MatrixToVectors(Polytope4().Vertices()),
95 ::testing::UnorderedElementsAreArray(MatrixToVectors(
96 (Eigen::Matrix<double, 2, 4>() << 1, 1.5, 1.5, 2, 0, -0.5, 0.5, 0)
97 .finished())));
98 EXPECT_THAT(
99 MatrixToVectors(Polytope5().Vertices()),
100 ::testing::UnorderedElementsAreArray(MatrixToVectors(
101 (Eigen::Matrix<double, 2, 4>() << 0.5, 1, 1.5, 1, 0, 0.5, 0, -0.5)
102 .finished())));
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500103}
104
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500105} // namespace controls
James Kuszmaul61750662021-06-21 21:32:33 -0700106} // namespace frc971