blob: c27eb4ddd07c46c3903f97b975e96e06169c091d [file] [log] [blame]
Milind Upadhyayda042bb2022-03-26 16:01:45 -07001#ifndef Y2022_VISION_GEOMETRY_H_
2#define Y2022_VISION_GEOMETRY_H_
3
milind-udb98afa2022-03-01 19:54:57 -08004#include "aos/util/math.h"
5#include "glog/logging.h"
6#include "opencv2/core/types.hpp"
7
8namespace y2022::vision {
9
10// Linear equation in the form y = mx + b
11struct SlopeInterceptLine {
12 double m, b;
13
14 inline SlopeInterceptLine(cv::Point2d p, cv::Point2d q) {
15 if (p.x == q.x) {
16 CHECK_EQ(p.y, q.y) << "Can't fit line to infinite slope";
17
18 // If two identical points were passed in, give the slope 0,
19 // with it passing the point.
20 m = 0.0;
21 } else {
22 m = (p.y - q.y) / (p.x - q.x);
23 }
24 // y = mx + b -> b = y - mx
25 b = p.y - (m * p.x);
26 }
27
28 inline double operator()(double x) const { return (m * x) + b; }
29};
30
31// Linear equation in the form ax + by = c
32struct StdFormLine {
33 public:
34 double a, b, c;
35
36 inline std::optional<cv::Point2d> Intersection(const StdFormLine &l) const {
37 // Use Cramer's rule to solve for the intersection
38 const double denominator = Determinant(a, b, l.a, l.b);
39 const double numerator_x = Determinant(c, b, l.c, l.b);
40 const double numerator_y = Determinant(a, c, l.a, l.c);
41
42 std::optional<cv::Point2d> intersection = std::nullopt;
43 // Return nullopt if the denominator is 0, meaning the same slopes
44 if (denominator != 0) {
45 intersection =
46 cv::Point2d(numerator_x / denominator, numerator_y / denominator);
47 }
48
49 return intersection;
50 }
51
52 private: // Determinant of [[a, b], [c, d]]
53 static inline double Determinant(double a, double b, double c, double d) {
54 return (a * d) - (b * c);
55 }
56};
57
58struct Circle {
59 public:
60 cv::Point2d center;
61 double radius;
62
63 static inline std::optional<Circle> Fit(std::vector<cv::Point2d> points) {
64 CHECK_EQ(points.size(), 3ul);
65 // For the 3 points, we have 3 equations in the form
66 // (x - h)^2 + (y - k)^2 = r^2
67 // Manipulate them to solve for the center and radius
68 // (x1 - h)^2 + (y1 - k)^2 = r^2 ->
69 // x1^2 + h^2 - 2x1h + y1^2 + k^2 - 2y1k = r^2
70 // Also, (x2 - h)^2 + (y2 - k)^2 = r^2
71 // Subtracting these two, we get
72 // x1^2 - x2^2 - 2h(x1 - x2) + y1^2 - y2^2 - 2k(y1 - y2) = 0 ->
73 // h(x1 - x2) + k(y1 - y2) = (-x1^2 + x2^2 - y1^2 + y2^2) / -2
74 // Doing the same with equations 1 and 3, we get the second linear equation
75 // h(x1 - x3) + k(y1 - y3) = (-x1^2 + x3^2 - y1^2 + y3^2) / -2
76 // Now, we can solve for their intersection and find the center
77 const auto l =
78 StdFormLine{points[0].x - points[1].x, points[0].y - points[1].y,
79 (-std::pow(points[0].x, 2) + std::pow(points[1].x, 2) -
80 std::pow(points[0].y, 2) + std::pow(points[1].y, 2)) /
81 -2.0};
82 const auto m =
83 StdFormLine{points[0].x - points[2].x, points[0].y - points[2].y,
84 (-std::pow(points[0].x, 2) + std::pow(points[2].x, 2) -
85 std::pow(points[0].y, 2) + std::pow(points[2].y, 2)) /
86 -2.0};
87 const auto center = l.Intersection(m);
88
89 std::optional<Circle> circle = std::nullopt;
90 if (center) {
91 // Now find the radius
92 const double radius = cv::norm(points[0] - *center);
93 circle = Circle{*center, radius};
94 }
95 return circle;
96 }
97
98 inline double DistanceTo(cv::Point2d p) const {
99 const auto p_prime = TranslateToOrigin(p);
100 // Now, the distance is simply the difference between distance from the
101 // origin to p' and the radius.
102 return std::abs(cv::norm(p_prime) - radius);
103 }
104
105 inline double AngleOf(cv::Point2d p) const {
106 auto p_prime = TranslateToOrigin(p);
107 // Flip the y because y values go downwards.
108 p_prime.y *= -1;
109 return std::atan2(p_prime.y, p_prime.x);
110 }
111
Milind Upadhyay8e2582b2022-03-06 15:14:15 -0800112 // Expects all angles to be from 0 to 2pi
113 // TODO(milind): handle wrapping
114 static inline bool AngleInRange(double theta, double theta_min,
115 double theta_max) {
116 return (
117 (theta >= theta_min && theta <= theta_max) ||
118 (theta_min > theta_max && (theta >= theta_min || theta <= theta_max)));
119 }
120
milind-udb98afa2022-03-01 19:54:57 -0800121 inline bool InAngleRange(cv::Point2d p, double theta_min,
122 double theta_max) const {
Milind Upadhyay8e2582b2022-03-06 15:14:15 -0800123 return AngleInRange(AngleOf(p), theta_min, theta_max);
milind-udb98afa2022-03-01 19:54:57 -0800124 }
125
126 private:
127 // Translate the point on the circle
128 // as if the circle's center is the origin (0,0)
129 inline cv::Point2d TranslateToOrigin(cv::Point2d p) const {
130 return cv::Point2d(p.x - center.x, p.y - center.y);
131 }
132};
133
134} // namespace y2022::vision
Milind Upadhyayda042bb2022-03-26 16:01:45 -0700135
136#endif // Y2022_VISION_GEOMETRY_H_