blob: 0a5a75980117c31ac1c8c010cb45aaf44f006309 [file] [log] [blame]
milind-udb98afa2022-03-01 19:54:57 -08001#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
9namespace y2022::vision::testing {
10
11TEST(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
40TEST(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
65TEST(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);
Milind Upadhyay8e2582b2022-03-06 15:14:15 -080084 EXPECT_TRUE(c->InAngleRange(kZeroPoint, (2.0 * M_PI) - 0.1, 0.1));
milind-udb98afa2022-03-01 19:54:57 -080085 EXPECT_EQ(c->DistanceTo(kZeroPoint), 0.0);
86
87 // Test the distance to another point
88 const cv::Point2d kDoubleDistPoint = {
89 c->center.x - (c->radius * 2.0) * std::sqrt(3.0) / 2.0,
90 c->center.y - (c->radius * 2.0) / 2.0};
91 EXPECT_DOUBLE_EQ(c->DistanceTo(kDoubleDistPoint), c->radius);
92
93 // Distance to center should be radius
94 EXPECT_DOUBLE_EQ(c->DistanceTo(c->center), c->radius);
95 }
96 // Test fitting an invalid circle (duplicate points)
97 {
98 auto c = Circle::Fit({{-6.0, 3.2}, {-3.0, 2.0}, {-6.0, 3.2}});
99 EXPECT_FALSE(c.has_value());
100 }
Milind Upadhyay8e2582b2022-03-06 15:14:15 -0800101 // Test if angles are in ranges
102 {
103 EXPECT_TRUE(Circle::AngleInRange(0.5, 0.4, 0.6));
104 EXPECT_TRUE(Circle::AngleInRange(0, (2.0 * M_PI) - 0.2, 0.2));
105 EXPECT_FALSE(
106 Circle::AngleInRange(0, (2.0 * M_PI) - 0.2, (2.0 * M_PI) - 0.1));
107 EXPECT_TRUE(Circle::AngleInRange(0.5, (2.0 * M_PI) - 0.1, 0.6));
108 }
milind-udb98afa2022-03-01 19:54:57 -0800109}
110
111} // namespace y2022::vision::testing