geometry.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <iostream>
  2. #ifdef HAS_OPENCV3
  3. #include <opencv2/core.hpp> //Any OPENCV3 code
  4. #else
  5. #include <opencv2/core/core.hpp> //Any Opencv2 code
  6. #endif
  7. using namespace cv;
  8. #include "geometry.h"
  9. double dist(cv::Point a, cv::Point b)
  10. {
  11. return sqrt((double)pow(a.x-b.x,2)+pow(a.y-b.y,2));
  12. }
  13. double inner_angle(cv::Point a, cv::Point middle, cv::Point b)
  14. {
  15. cv::Point v1 = a - middle;
  16. cv::Point v2 = b - middle;
  17. double v1_length = dist(cv::Point(0, 0), v1);
  18. double v2_length = dist(cv::Point(0, 0), v2);
  19. return acos( (v1.x*v2.x + v1.y*v2.y) / (v1_length*v2_length) );
  20. }
  21. cv::Point
  22. map_point(double theta, cv::Point origin, cv::Point tomap)
  23. {
  24. //that's the algorithm: res = Rt*(tomap - origin)
  25. cv::Vec<double,2> p_as_vector;
  26. cv::Point p;
  27. cv::Matx<double,2,2> Rt;
  28. p = tomap - origin;
  29. p_as_vector[0] = p.x;
  30. p_as_vector[1] = p.y;
  31. Rt(0,0) = cos(theta);
  32. Rt(0,1) = sin(theta);
  33. Rt(1,1) = Rt(0,0);
  34. Rt(1,0) = -Rt(0,1);
  35. cv::Vec<double,2> res_as_vector = Rt*p_as_vector;
  36. return cv::Point(res_as_vector);
  37. }
  38. cv::Vec<double,3>
  39. get_line(Point p, Point q)
  40. {
  41. Vec<double,3> line; //the three positions are a,b,c as in: ax + by + c = 0
  42. Point vector = q-p;
  43. line(0) = - vector.y;
  44. line(1) = vector.x;
  45. line(2) = vector.y * p.x - vector.x * p.y;
  46. assert(is_in_line(line, p));
  47. assert(is_in_line(line, q));
  48. return line;
  49. }
  50. bool
  51. is_in_line(cv::Vec<double,3> line, Point p)
  52. {
  53. return line(0)*p.x + line(1)*p.y + line(2) == 0;
  54. }