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