blob: cb994a21bfd07e05b20cdcb2c976c50f65e0274c [file] [log] [blame]
James Kuszmaul9f9676d2019-01-25 21:27:58 -08001#include "frc971/control_loops/pose.h"
2
3#include "gtest/gtest.h"
4
5namespace frc971 {
6namespace control_loops {
7namespace testing {
8
9// Test that basic accessors on an individual Pose object work as expected.
10TEST(PoseTest, BasicPoseTest) {
11 // Provide a basic Pose with non-zero components for everything.
12 Pose pose({1, 1, 0.5}, 0.5);
13 // The xy_norm should just be based on the x/y positions, not the Z; hence
14 // sqrt(2) rather than sqrt(1^2 + 1^2 + 0.5^2).
15 EXPECT_DOUBLE_EQ(::std::sqrt(2.0), pose.xy_norm());
16 // Similarly, heading should just be atan2(y, x).
17 EXPECT_DOUBLE_EQ(M_PI / 4.0, pose.heading());
18 // Global and relative poses should be the same since we did not construct
19 // this off of a separate Pose.
20 EXPECT_EQ(1.0, pose.rel_pos().x());
21 EXPECT_EQ(1.0, pose.rel_pos().y());
22 EXPECT_EQ(0.5, pose.rel_pos().z());
23
24 EXPECT_EQ(1.0, pose.abs_pos().x());
25 EXPECT_EQ(1.0, pose.abs_pos().y());
26 EXPECT_EQ(0.5, pose.abs_pos().z());
27
28 EXPECT_EQ(0.5, pose.rel_theta());
29 EXPECT_EQ(0.5, pose.abs_theta());
30
31 pose.set_theta(3.14);
32 EXPECT_EQ(3.14, pose.rel_theta());
33 pose.mutable_pos()->x() = 9.71;
34 EXPECT_EQ(9.71, pose.rel_pos().x());
35}
36
37// Check that Poses behave as expected when constructed relative to another
38// POse.
39TEST(PoseTest, BaseTest) {
40 // Tolerance for the EXPECT_NEARs. Because we are doing enough trig operations
41 // under the hood we actually start to lose some precision.
42 constexpr double kEps = 1e-15;
43 // The points we will construct have absolute positions at:
44 // base1: (1, 1)
45 // base2: (-1, 1)
46 // rel1: (0, 2)
47 // Where rel1 is expressed as compared to base1, noting that because base1
48 // has a yaw of M_PI, the position of rel1 compared to base1 is (1, -1)
49 // rather than (-1, 1).
50 Pose base1({1, 1, 0}, M_PI);
51 Pose base2({-1, 1, 0}, -M_PI / 2.0);
52 Pose rel1(&base1, {1, -1, 0}, 0.0);
53 EXPECT_NEAR(0.0, rel1.abs_pos().x(), kEps);
54 EXPECT_NEAR(2.0, rel1.abs_pos().y(), kEps);
55 EXPECT_NEAR(M_PI, rel1.abs_theta(), kEps);
56 // Check that, when rebasing to base2, the absolute position does not change
57 // and the relative POse changes to be relative to base2.
58 Pose rel2 = rel1.Rebase(&base2);
59 EXPECT_NEAR(rel1.abs_pos().x(), rel2.abs_pos().x(), kEps);
60 EXPECT_NEAR(rel1.abs_pos().y(), rel2.abs_pos().y(), kEps);
61 EXPECT_NEAR(rel1.abs_pos().z(), rel2.abs_pos().z(), kEps);
62 EXPECT_NEAR(rel1.abs_theta(), rel2.abs_theta(), kEps);
63 EXPECT_NEAR(-1.0, rel2.rel_pos().x(), kEps);
64 EXPECT_NEAR(1.0, rel2.rel_pos().y(), kEps);
65 EXPECT_NEAR(-M_PI / 2.0, rel2.rel_theta(), kEps);
66 // Check that rebasing onto nullptr results in a Pose based in the global
67 // frame.
68 Pose abs = rel1.Rebase(nullptr);
69 EXPECT_NEAR(rel1.abs_pos().x(), abs.abs_pos().x(), kEps);
70 EXPECT_NEAR(rel1.abs_pos().y(), abs.abs_pos().y(), kEps);
71 EXPECT_NEAR(rel1.abs_pos().z(), abs.abs_pos().z(), kEps);
72 EXPECT_NEAR(rel1.abs_theta(), abs.abs_theta(), kEps);
73 EXPECT_NEAR(rel1.abs_pos().x(), abs.rel_pos().x(), kEps);
74 EXPECT_NEAR(rel1.abs_pos().y(), abs.rel_pos().y(), kEps);
75 EXPECT_NEAR(rel1.abs_pos().z(), abs.rel_pos().z(), kEps);
76 EXPECT_NEAR(rel1.abs_theta(), abs.rel_theta(), kEps);
77}
78
79// Tests that basic checks for intersection function as expected.
80TEST(LineSegmentTest, TrivialIntersectTest) {
81 Pose p1({0, 0, 0}, 0.0), p2({2, 0, 0}, 0.0);
82 // A line segment from (0, 0) to (0, 2).
83 LineSegment l1(p1, p2);
84 Pose q1({1, -1, 0}, 0.0), q2({1, 1, 0}, 0.0);
85 // A line segment from (1, -1) to (1, 1).
86 LineSegment l2(q1, q2);
87 // The two line segments should intersect.
88 EXPECT_TRUE(l1.Intersects(l2));
89 EXPECT_TRUE(l2.Intersects(l1));
90
91 // If we switch around the orderings such that the line segments are
92 // (0, 0) -> (1, -1) and (2, 0)->(1, 1) then the line segments do not
93 // intersect.
94 LineSegment l3(p1, q1);
95 LineSegment l4(p2, q2);
96 EXPECT_FALSE(l3.Intersects(l4));
97 EXPECT_FALSE(l4.Intersects(l3));
98}
99
100// Check that when we construct line segments that are collinear, both with
101// overlapping bits and without overlapping bits, they register as not
102// intersecting.
103// We may want this behavior to change in the future, but for now check for
104// consistency.
105TEST(LineSegmentTest, CollinearIntersectTest) {
106 Pose p1({0, 0, 0}, 0.0), p2({1, 0, 0}, 0.0), p3({2, 0, 0}, 0.0),
107 p4({3, 0, 0}, 0.0);
108 // These two line segments overlap and are collinear, one going from 0 to 2
109 // and the other from 1 to 3 on the X-axis.
110 LineSegment l1(p1, p3);
111 LineSegment l2(p2, p4);
112 EXPECT_FALSE(l1.Intersects(l2));
113 EXPECT_FALSE(l2.Intersects(l1));
114
115 // These two line segments do not overlap and are collinear, one going from 0
116 // to 1 and the other from 2 to 3 on the X-axis.
117 LineSegment l3(p1, p2);
118 LineSegment l4(p3, p4);
119 EXPECT_FALSE(l3.Intersects(l4));
120 EXPECT_FALSE(l4.Intersects(l3));
121
122 // Test when one line segment is completely contained within the other.
123 LineSegment l5(p1, p4);
124 LineSegment l6(p3, p2);
125 EXPECT_FALSE(l5.Intersects(l6));
126 EXPECT_FALSE(l6.Intersects(l5));
127}
128
129} // namespace testing
130} // namespace control_loops
131} // namespace frc971