blob: 070de1217baae26e29e4b35d14a0d587b97f1890 [file] [log] [blame]
Milind Upadhyayb67c6182022-10-22 13:45:45 -07001#include "frc971/vision/geometry.h"
milind-udb98afa2022-03-01 19:54:57 -08002
3#include <cmath>
4
Austin Schuh99f7c6a2024-06-25 22:07:44 -07005#include "absl/log/check.h"
6#include "absl/log/log.h"
milind-udb98afa2022-03-01 19:54:57 -08007#include "gtest/gtest.h"
8
Philipp Schrader790cb542023-07-05 21:06:52 -07009#include "aos/util/math.h"
10
Milind Upadhyayb67c6182022-10-22 13:45:45 -070011namespace frc971::vision::testing {
milind-udb98afa2022-03-01 19:54:57 -080012
13TEST(GeometryTest, SlopeInterceptLine) {
14 // Test a normal line
15 {
16 SlopeInterceptLine l({2.0, 3.0}, {4.0, 2.0});
17 EXPECT_DOUBLE_EQ(l.m, -0.5);
18 EXPECT_DOUBLE_EQ(l.b, 4.0);
19 EXPECT_DOUBLE_EQ(l(5), 1.5);
20 }
21 // Test a horizontal line
22 {
23 SlopeInterceptLine l({2.0, 3.0}, {4.0, 3.0});
24 EXPECT_DOUBLE_EQ(l.m, 0.0);
25 EXPECT_DOUBLE_EQ(l.b, 3.0);
26 EXPECT_DOUBLE_EQ(l(1000.0), 3.0);
27 }
28 // Test duplicate points
29 {
30 SlopeInterceptLine l({2.0, 3.0}, {2.0, 3.0});
31 EXPECT_DOUBLE_EQ(l.m, 0.0);
32 EXPECT_DOUBLE_EQ(l.b, 3.0);
33 EXPECT_DOUBLE_EQ(l(1000.0), 3.0);
34 }
35 // Test infinite slope
36 {
37 EXPECT_DEATH(SlopeInterceptLine({2.0, 3.0}, {2.0, 5.0}),
38 "(.*)infinite slope(.*)");
39 }
40}
41
42TEST(GeometryTest, StdFormLine) {
43 // Test the intersection of normal lines
44 {
45 StdFormLine l{3.0, 2.0, 2.3};
46 StdFormLine m{-2.0, 1.2, -0.3};
47 const cv::Point2d kIntersection = {42.0 / 95.0, 37.0 / 76.0};
48 EXPECT_EQ(*l.Intersection(m), kIntersection);
49 EXPECT_EQ(*m.Intersection(l), kIntersection);
50 }
51 // Test the intersection of parallel lines
52 {
53 StdFormLine l{-3.0, 2.0, -3.7};
54 StdFormLine m{-6.0, 4.0, 0.1};
55 EXPECT_EQ(l.Intersection(m), std::nullopt);
56 EXPECT_EQ(m.Intersection(l), std::nullopt);
57 }
58 // Test the intersection of duplicate lines
59 {
60 StdFormLine l{6.0, -8.0, 0.23};
61 StdFormLine m{6.0, -8.0, 0.23};
62 EXPECT_EQ(l.Intersection(m), std::nullopt);
63 EXPECT_EQ(m.Intersection(l), std::nullopt);
64 }
65}
66
67TEST(GeometryTest, Circle) {
68 // Test fitting a normal circle
69 {
70 auto c = Circle::Fit({{-6.0, 3.2}, {-3.0, 2.0}, {-9.3, 1.4}});
71 EXPECT_TRUE(c.has_value());
72 EXPECT_NEAR(c->center.x, -5.901, 1e-3);
73 EXPECT_NEAR(c->center.y, -0.905, 1e-3);
74 EXPECT_NEAR(c->radius, 4.106, 1e-3);
75
76 // Coordinate systems flipped because of image
77 const cv::Point2d kPoint = {c->center.x - c->radius * std::sqrt(3.0) / 2.0,
78 c->center.y - c->radius / 2.0};
79 EXPECT_NEAR(c->AngleOf(kPoint), 5.0 * M_PI / 6.0, 1e-5);
80 EXPECT_TRUE(c->InAngleRange(kPoint, 4.0 * M_PI / 6.0, M_PI));
81 EXPECT_FALSE(c->InAngleRange(kPoint, 0, 2.0 * M_PI / 6.0));
82 EXPECT_EQ(c->DistanceTo(kPoint), 0.0);
83
84 const cv::Point2d kZeroPoint = {c->center.x + c->radius, c->center.y};
85 EXPECT_NEAR(c->AngleOf(kZeroPoint), 0.0, 1e-5);
Milind Upadhyay8e2582b2022-03-06 15:14:15 -080086 EXPECT_TRUE(c->InAngleRange(kZeroPoint, (2.0 * M_PI) - 0.1, 0.1));
milind-udb98afa2022-03-01 19:54:57 -080087 EXPECT_EQ(c->DistanceTo(kZeroPoint), 0.0);
88
89 // Test the distance to another point
90 const cv::Point2d kDoubleDistPoint = {
91 c->center.x - (c->radius * 2.0) * std::sqrt(3.0) / 2.0,
92 c->center.y - (c->radius * 2.0) / 2.0};
93 EXPECT_DOUBLE_EQ(c->DistanceTo(kDoubleDistPoint), c->radius);
94
95 // Distance to center should be radius
96 EXPECT_DOUBLE_EQ(c->DistanceTo(c->center), c->radius);
97 }
98 // Test fitting an invalid circle (duplicate points)
99 {
100 auto c = Circle::Fit({{-6.0, 3.2}, {-3.0, 2.0}, {-6.0, 3.2}});
101 EXPECT_FALSE(c.has_value());
102 }
Milind Upadhyay8e2582b2022-03-06 15:14:15 -0800103 // Test if angles are in ranges
104 {
105 EXPECT_TRUE(Circle::AngleInRange(0.5, 0.4, 0.6));
106 EXPECT_TRUE(Circle::AngleInRange(0, (2.0 * M_PI) - 0.2, 0.2));
107 EXPECT_FALSE(
108 Circle::AngleInRange(0, (2.0 * M_PI) - 0.2, (2.0 * M_PI) - 0.1));
109 EXPECT_TRUE(Circle::AngleInRange(0.5, (2.0 * M_PI) - 0.1, 0.6));
110 }
milind-udb98afa2022-03-01 19:54:57 -0800111}
112
Milind Upadhyayb67c6182022-10-22 13:45:45 -0700113} // namespace frc971::vision::testing