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