123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- //
- // Created by zx on 2020/1/10.
- //
- #include "Railing.h"
- /*
- * 构造函数
- * a,b,c代表栏杆所在直线方程, ax+by+c=0
- * width:表示栏杆的宽
- */
- Railing::Railing(float a,float b,float c,float width)
- {
- m_a=a;
- m_b=b;
- m_c=c;
- m_width=width;
- }
- /*
- * 检验结果框,是否会与栏杆冲突
- */
- Error_manager Railing::verify(cv::RotatedRect rotate_rect)
- {
- //检验内部参数是否合法
- if(m_a==0 && m_b==0)
- {
- return Error_manager(HARDWARE_LIMIT_RAILING_PARAMETER_ERROR,NORMAL,"railing parameter a&b = 0");
- }
- //第一步,获取旋转矩形四个顶角
- cv::Point2f vertice[4];
- rotate_rect.points(vertice);
- //第二步.判断四个顶点是否在栏杆不同侧面,且与直线距离<m_width
- bool limit_left=false;
- bool limit_right=false;
- for(int i=0;i<4;++i)
- {
- //计算顶点与直线的距离
- cv::Point2f point=vertice[i];
- float x=point.x;
- float y=point.y;
- float distance=fabs(m_a*x+m_b*y+m_c)/sqrt(m_a*m_a+m_b*m_b);
- if(distance<m_width)
- {
- //顶点在栏杆上,必定会碰撞
- return Error_manager(HARDWARE_LIMIT_RAILING_ERROR,NORMAL,"railing error");
- }
- else
- {
- //顶点在直线右边
- if(m_a*x+m_b*y+m_c>0)
- limit_right=true;
- else if(m_a*x+m_b*y+m_c<0)
- limit_left=true;
- }
- }
- //如果左右都有顶点, 则与栏杆碰撞
- if(limit_left==true && limit_right==true)
- {
- return Error_manager(HARDWARE_LIMIT_RAILING_ERROR,NORMAL,"railing error");
- }
- return SUCCESS;
- }
|