Railing.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // Created by zx on 2020/1/10.
  3. //
  4. #include "Railing.h"
  5. /*
  6. * 构造函数
  7. * a,b,c代表栏杆所在直线方程, ax+by+c=0
  8. * width:表示栏杆的宽
  9. */
  10. Railing::Railing(float a,float b,float c,float width)
  11. {
  12. m_a=a;
  13. m_b=b;
  14. m_c=c;
  15. m_width=width;
  16. }
  17. /*
  18. * 检验结果框,是否会与栏杆冲突
  19. */
  20. Error_manager Railing::verify(cv::RotatedRect rotate_rect)
  21. {
  22. //检验内部参数是否合法
  23. if(m_a==0 && m_b==0)
  24. {
  25. return Error_manager(HARDWARE_LIMIT_RAILING_PARAMETER_ERROR,NORMAL,"railing parameter a&b = 0");
  26. }
  27. //第一步,获取旋转矩形四个顶角
  28. cv::Point2f vertice[4];
  29. rotate_rect.points(vertice);
  30. //第二步.判断四个顶点是否在栏杆不同侧面,且与直线距离<m_width
  31. bool limit_left=false;
  32. bool limit_right=false;
  33. for(int i=0;i<4;++i)
  34. {
  35. //计算顶点与直线的距离
  36. cv::Point2f point=vertice[i];
  37. float x=point.x;
  38. float y=point.y;
  39. float distance=fabs(m_a*x+m_b*y+m_c)/sqrt(m_a*m_a+m_b*m_b);
  40. if(distance<m_width)
  41. {
  42. //顶点在栏杆上,必定会碰撞
  43. return Error_manager(HARDWARE_LIMIT_RAILING_ERROR,NORMAL,"railing error");
  44. }
  45. else
  46. {
  47. //顶点在直线右边
  48. if(m_a*x+m_b*y+m_c>0)
  49. limit_right=true;
  50. else if(m_a*x+m_b*y+m_c<0)
  51. limit_left=true;
  52. }
  53. }
  54. //如果左右都有顶点, 则与栏杆碰撞
  55. if(limit_left==true && limit_right==true)
  56. {
  57. return Error_manager(HARDWARE_LIMIT_RAILING_ERROR,NORMAL,"railing error");
  58. }
  59. return SUCCESS;
  60. }