blob: 542c98187d81cf8ed587720e326096b3835581db [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080011namespace frc971::controls {
Brian Silvermanaba7bf62016-01-31 18:03:59 -050012
13class HPolytopeTest : public ::testing::Test {
14 protected:
15 HPolytope<2> Polytope1() {
16 return HPolytope<2>{
17 (Eigen::Matrix<double, 4, 2>() << 1, 0, -1, 0, 0, 1, 0, -1).finished(),
18 (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0, 12.0, 12.0).finished()};
19 }
20 HPolytope<2> Polytope2() {
21 return HPolytope<2>{
22 (Eigen::Matrix<double, 4, 2>() << 1, 1, -1, -1, 0, 1, 0, -1).finished(),
23 (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0, 12.0, 12.0).finished()};
24 }
25 HPolytope<1> Polytope3() {
26 return HPolytope<1>{(Eigen::Matrix<double, 2, 1>() << 1, -0.5).finished(),
27 (Eigen::Matrix<double, 2, 1>() << 5.0, 2.0).finished()};
28 }
29 HPolytope<2> Polytope4() {
30 return HPolytope<2>{
31 (Eigen::Matrix<double, 4, 2>() << 1, 1, -1, -1, 1, -1, -1, 1)
32 .finished(),
33 (Eigen::Matrix<double, 4, 1>() << 2, -1, 2, -1).finished()};
34 }
35 HPolytope<2> Polytope5() {
36 return HPolytope<2>{
37 (Eigen::Matrix<double, 4, 2>() << 1, 1, -1, -1, 1, -1, -1, 1)
38 .finished(),
39 (Eigen::Matrix<double, 4, 1>() << 1.5, -0.5, 1.5, -0.5).finished()};
40 }
41
42 void SetUp() override {
43 ::aos::testing::EnableTestLogging();
44 ::aos::testing::ForcePrintLogsDuringTests();
45 HPolytope<0>::Init();
46 }
47
48 template <typename T>
49 ::std::vector<::std::vector<double>> MatrixToVectors(const T &matrix) {
50 ::std::vector<::std::vector<double>> r;
51 for (int i = 0; i < matrix.cols(); ++i) {
52 ::std::vector<double> col;
53 for (int j = 0; j < matrix.rows(); ++j) {
54 col.emplace_back(matrix(j, i));
55 }
56 r.emplace_back(col);
57 }
58 return r;
59 }
60
61 template <typename T>
62 ::std::vector<::testing::Matcher<::std::vector<double>>> MatrixToMatchers(
63 const T &matrix) {
64 ::std::vector<::testing::Matcher<::std::vector<double>>> r;
65 for (int i = 0; i < matrix.cols(); ++i) {
66 ::std::vector<::testing::Matcher<double>> col;
67 for (int j = 0; j < matrix.rows(); ++j) {
68 col.emplace_back(::testing::DoubleNear(matrix(j, i), 0.000001));
69 }
70 r.emplace_back(::testing::ElementsAreArray(col));
71 }
72 return r;
73 }
Brian Silvermanaba7bf62016-01-31 18:03:59 -050074};
75
76// Tests that the vertices for various polytopes calculated from H and k are
77// correct.
78TEST_F(HPolytopeTest, CalculatedVertices) {
Philipp Schrader790cb542023-07-05 21:06:52 -070079 EXPECT_THAT(
80 MatrixToVectors(Polytope1().Vertices()),
81 ::testing::UnorderedElementsAreArray(MatrixToVectors(
82 (Eigen::Matrix<double, 2, 4>() << -12, -12, 12, 12, -12, 12, 12, -12)
83 .finished())));
84 EXPECT_THAT(
85 MatrixToVectors(Polytope2().Vertices()),
86 ::testing::UnorderedElementsAreArray(MatrixToVectors(
87 (Eigen::Matrix<double, 2, 4>() << 24, 0, -24, 0, -12, 12, 12, -12)
88 .finished())));
Brian Silvermanaba7bf62016-01-31 18:03:59 -050089 EXPECT_THAT(MatrixToVectors(Polytope3().Vertices()),
90 ::testing::UnorderedElementsAreArray(MatrixToVectors(
91 (Eigen::Matrix<double, 1, 2>() << 5, -4).finished())));
Philipp Schrader790cb542023-07-05 21:06:52 -070092 EXPECT_THAT(
93 MatrixToVectors(Polytope4().Vertices()),
94 ::testing::UnorderedElementsAreArray(MatrixToVectors(
95 (Eigen::Matrix<double, 2, 4>() << 1, 1.5, 1.5, 2, 0, -0.5, 0.5, 0)
96 .finished())));
97 EXPECT_THAT(
98 MatrixToVectors(Polytope5().Vertices()),
99 ::testing::UnorderedElementsAreArray(MatrixToVectors(
100 (Eigen::Matrix<double, 2, 4>() << 0.5, 1, 1.5, 1, 0, 0.5, 0, -0.5)
101 .finished())));
Brian Silvermanaba7bf62016-01-31 18:03:59 -0500102}
103
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800104} // namespace frc971::controls