Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.cpp 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "utils.h"
  2. LFRPoint::LFRPoint(/* args */) : x(0.0), y(0.0)
  3. {
  4. }
  5. LFRPoint::LFRPoint(double x, double y): x(x), y(y)
  6. {
  7. }
  8. LFRPoint::~LFRPoint()
  9. {
  10. }
  11. LFRVector::LFRVector(/* args */) : LFRPoint()
  12. {
  13. }
  14. LFRVector::LFRVector(double x, double y) : LFRPoint(x, y)
  15. {
  16. }
  17. LFRVector::LFRVector(const LFRPoint& pt) : LFRPoint(pt)
  18. {
  19. }
  20. LFRVector::~LFRVector()
  21. {
  22. }
  23. LFRLine::LFRLine(/* args */) : start(), dir()
  24. {
  25. }
  26. LFRLine::LFRLine(LFRPoint start, LFRVector dir) : start(start), dir(dir)
  27. {
  28. }
  29. LFRLine::LFRLine(LFRPoint start, LFRPoint end) : start(start)
  30. {
  31. dir = end - start;
  32. }
  33. LFRLine::~LFRLine()
  34. {
  35. }
  36. VectorOfLines::VectorOfLines()
  37. {
  38. }
  39. VectorOfLines::~VectorOfLines()
  40. {
  41. }
  42. double VectorOfLines::calcGradient(Point p0, Point p1)
  43. {
  44. double gradient = (p1.y - p0.y)/(p1.x - p0.x + 1e-10);
  45. return p1.x > p0.x ? gradient : - gradient;
  46. }
  47. float VectorOfLines::calcZeroPoint(cv::Point x, float m)
  48. {
  49. return 0.0;
  50. }
  51. double VectorOfLines::calcDistance(Point p0, Point p1)
  52. {
  53. return sqrt(pow(p1.y - p0.y, 2) + pow(p1.x - p0.x, 2));
  54. }
  55. vector<Vec4i> VectorOfLines::findMiddleLine(vector<Vec4i> &lines){
  56. Point point11;
  57. Point point12;
  58. Point point21;
  59. Point point22;
  60. vector<Vec4i> middleLines;
  61. for( size_t i = 0; i < (lines.size() - 1); i++ )
  62. {
  63. point11 = Point(lines[i][0], lines[i][1]);
  64. point12 = Point( lines[i][2], lines[i][3]);
  65. double gradient1 = VectorOfLines::calcGradient(point11, point12);
  66. //Compare every Line with the other
  67. for( size_t j = 0; j < (lines.size()); j++ )
  68. {
  69. if(j != i)
  70. {
  71. point21 = Point(lines[j][0], lines[j][1]);
  72. point22 = Point(lines[j][2], lines[j][3]);
  73. double gradient2 = VectorOfLines::calcGradient(point21, point22);
  74. if(norm(gradient1 - gradient2) < 0.15)
  75. {
  76. middleLines.push_back(Vec4i((point11.x+point21.x)/2, (point11.y+point21.y)/2, (point12.x+point22.x)/2, (point12.y+point22.y)/2));
  77. }
  78. }
  79. }
  80. }
  81. return middleLines;
  82. }