milind-u | db98afa | 2022-03-01 19:54:57 -0800 | [diff] [blame^] | 1 | #include "y2022/vision/geometry.h" |
| 2 | |
| 3 | #include <cmath> |
| 4 | |
| 5 | #include "aos/util/math.h" |
| 6 | #include "glog/logging.h" |
| 7 | #include "gtest/gtest.h" |
| 8 | |
| 9 | namespace y2022::vision::testing { |
| 10 | |
| 11 | TEST(GeometryTest, SlopeInterceptLine) { |
| 12 | // Test a normal line |
| 13 | { |
| 14 | SlopeInterceptLine l({2.0, 3.0}, {4.0, 2.0}); |
| 15 | EXPECT_DOUBLE_EQ(l.m, -0.5); |
| 16 | EXPECT_DOUBLE_EQ(l.b, 4.0); |
| 17 | EXPECT_DOUBLE_EQ(l(5), 1.5); |
| 18 | } |
| 19 | // Test a horizontal line |
| 20 | { |
| 21 | SlopeInterceptLine l({2.0, 3.0}, {4.0, 3.0}); |
| 22 | EXPECT_DOUBLE_EQ(l.m, 0.0); |
| 23 | EXPECT_DOUBLE_EQ(l.b, 3.0); |
| 24 | EXPECT_DOUBLE_EQ(l(1000.0), 3.0); |
| 25 | } |
| 26 | // Test duplicate points |
| 27 | { |
| 28 | SlopeInterceptLine l({2.0, 3.0}, {2.0, 3.0}); |
| 29 | EXPECT_DOUBLE_EQ(l.m, 0.0); |
| 30 | EXPECT_DOUBLE_EQ(l.b, 3.0); |
| 31 | EXPECT_DOUBLE_EQ(l(1000.0), 3.0); |
| 32 | } |
| 33 | // Test infinite slope |
| 34 | { |
| 35 | EXPECT_DEATH(SlopeInterceptLine({2.0, 3.0}, {2.0, 5.0}), |
| 36 | "(.*)infinite slope(.*)"); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | TEST(GeometryTest, StdFormLine) { |
| 41 | // Test the intersection of normal lines |
| 42 | { |
| 43 | StdFormLine l{3.0, 2.0, 2.3}; |
| 44 | StdFormLine m{-2.0, 1.2, -0.3}; |
| 45 | const cv::Point2d kIntersection = {42.0 / 95.0, 37.0 / 76.0}; |
| 46 | EXPECT_EQ(*l.Intersection(m), kIntersection); |
| 47 | EXPECT_EQ(*m.Intersection(l), kIntersection); |
| 48 | } |
| 49 | // Test the intersection of parallel lines |
| 50 | { |
| 51 | StdFormLine l{-3.0, 2.0, -3.7}; |
| 52 | StdFormLine m{-6.0, 4.0, 0.1}; |
| 53 | EXPECT_EQ(l.Intersection(m), std::nullopt); |
| 54 | EXPECT_EQ(m.Intersection(l), std::nullopt); |
| 55 | } |
| 56 | // Test the intersection of duplicate lines |
| 57 | { |
| 58 | StdFormLine l{6.0, -8.0, 0.23}; |
| 59 | StdFormLine m{6.0, -8.0, 0.23}; |
| 60 | EXPECT_EQ(l.Intersection(m), std::nullopt); |
| 61 | EXPECT_EQ(m.Intersection(l), std::nullopt); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | TEST(GeometryTest, Circle) { |
| 66 | // Test fitting a normal circle |
| 67 | { |
| 68 | auto c = Circle::Fit({{-6.0, 3.2}, {-3.0, 2.0}, {-9.3, 1.4}}); |
| 69 | EXPECT_TRUE(c.has_value()); |
| 70 | EXPECT_NEAR(c->center.x, -5.901, 1e-3); |
| 71 | EXPECT_NEAR(c->center.y, -0.905, 1e-3); |
| 72 | EXPECT_NEAR(c->radius, 4.106, 1e-3); |
| 73 | |
| 74 | // Coordinate systems flipped because of image |
| 75 | const cv::Point2d kPoint = {c->center.x - c->radius * std::sqrt(3.0) / 2.0, |
| 76 | c->center.y - c->radius / 2.0}; |
| 77 | EXPECT_NEAR(c->AngleOf(kPoint), 5.0 * M_PI / 6.0, 1e-5); |
| 78 | EXPECT_TRUE(c->InAngleRange(kPoint, 4.0 * M_PI / 6.0, M_PI)); |
| 79 | EXPECT_FALSE(c->InAngleRange(kPoint, 0, 2.0 * M_PI / 6.0)); |
| 80 | EXPECT_EQ(c->DistanceTo(kPoint), 0.0); |
| 81 | |
| 82 | const cv::Point2d kZeroPoint = {c->center.x + c->radius, c->center.y}; |
| 83 | EXPECT_NEAR(c->AngleOf(kZeroPoint), 0.0, 1e-5); |
| 84 | EXPECT_TRUE( |
| 85 | c->InAngleRange(kZeroPoint, -2.1 * 2.0 * M_PI, -1.9 * 2.0 * M_PI)); |
| 86 | EXPECT_TRUE( |
| 87 | c->InAngleRange(kZeroPoint, 1.9 * 2.0 * M_PI, 2.1 * 2.0 * M_PI)); |
| 88 | EXPECT_EQ(c->DistanceTo(kZeroPoint), 0.0); |
| 89 | |
| 90 | // Test the distance to another point |
| 91 | const cv::Point2d kDoubleDistPoint = { |
| 92 | c->center.x - (c->radius * 2.0) * std::sqrt(3.0) / 2.0, |
| 93 | c->center.y - (c->radius * 2.0) / 2.0}; |
| 94 | EXPECT_DOUBLE_EQ(c->DistanceTo(kDoubleDistPoint), c->radius); |
| 95 | |
| 96 | // Distance to center should be radius |
| 97 | EXPECT_DOUBLE_EQ(c->DistanceTo(c->center), c->radius); |
| 98 | } |
| 99 | // Test fitting an invalid circle (duplicate points) |
| 100 | { |
| 101 | auto c = Circle::Fit({{-6.0, 3.2}, {-3.0, 2.0}, {-6.0, 3.2}}); |
| 102 | EXPECT_FALSE(c.has_value()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } // namespace y2022::vision::testing |