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