NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
LineSegment.hpp
1 //LineSegment.h
2 // Created on: Apr 10, 2015
3 // Author: Hafez Farazi <farazi@ais.uni-bonn.de>
4 #pragma once
5 #include <opencv2/opencv.hpp>
6 #include <ros/ros.h>
7 #include <math.h>
8 #include <algorithm> // std::max
9 using namespace std;
10 using namespace cv;
11 #define DEFFLEQEPSILON 0.001
12 
18 {
19 private:
20  bool Within(float fl, float flLow, float flHi, float flEp =
21  DEFFLEQEPSILON);
22  double probability;
23 public:
24  bool SortbyDistance(const Point2f & a, const Point2f &b);
25  bool IsOnThis(const Point2f& ptTest, float flEp =
26  DEFFLEQEPSILON);
27  LineSegment(const Point2d p1, const Point2d p2,double _probability=0.0);
28  LineSegment(const Point2d center, double angle,double length,double _probability=0.0);
29  LineSegment(const LineSegment &l);
30  LineSegment();
31  Point2d P1, P2;
32  inline void setProbability(double _in)
33  {
34  probability=std::max(0.0,std::min(_in,1.0));
35  }
36  inline double getProbability() const
37  {
38  return probability;
39  }
40  double GetLength() const;
41  void Clip(Rect boundry);
42  void GetDirection(Point2d &res) const;
43  float GetYByX(float x) const;
44  int GetSide(const Point2d point) const;
45  double GetExteriorAngleDegree(const LineSegment otherLine) const;
46  double GetAbsMinAngleDegree(const LineSegment otherLine) const;
47  virtual ~LineSegment();
48  Point2f GetClosestPointOnLineSegment(Point2f p);
49  Point2f GetMiddle();
50  float DistanceFromLine(Point2f p);
51  bool Intersect(LineSegment L, Point2d &res);
52  bool IntersectLineForm(LineSegment L, Point2d &res);
53  LineSegment PerpendicularLineSegment(double scale = 1);
54  LineSegment PerpendicularLineSegment(double len, cv::Point2d mid);
55  bool GetSlope(double &slope)
56  {
57  if (abs(P2.x - P1.x) < 0.00001)
58  {
59  return false;
60  }
61  slope = (P2.y - P1.y) / (P2.x - P1.x);
62  return true;
63  }
64  double GetRadianFromX()
65  {
66  double res = atan2f((P2.y - P1.y), (P2.x - P1.x));
67  return res;
68  }
69  double GetDegreeFromX()
70  {
71  return (GetRadianFromX() / M_PI) * (180);;
72  }
73  LineSegment scale(double _in)
74  {
75  double angle=GetRadianFromX();
76  Point2d mid=GetMiddle();
77  double len=GetLength()/2;
78  len*=_in;
79  double x2=mid.x+cos(angle)*len;
80  double y2=mid.y+sin(angle)*len;
81  double x1=mid.x-cos(angle)*len;
82  double y1=mid.y-sin(angle)*len;
83  return LineSegment(Point2d(x1,y1),Point2d(x2,y2));
84  }
85 
86  vector<LineSegment> GetMidLineSegments(
87  int count /*Means that lines count will be 2^count*/)
88  {
89  vector<LineSegment> lsList, lsListTmp;
90  lsList.push_back(LineSegment(P1, P2));
91 
92  for (int counter = 0; counter < count; counter++)
93  {
94  lsListTmp.clear();
95  for (size_t i = 0; i < lsList.size(); i++)
96  {
97  LineSegment tmp = lsList[i];
98  Point2d midPoint = tmp.GetMiddle();
99  lsListTmp.push_back(LineSegment(tmp.P1, midPoint));
100  lsListTmp.push_back(LineSegment(tmp.P2, midPoint));
101  }
102  lsList = lsListTmp;
103  }
104  return lsList;
105  }
106 
107  vector<Point2d> GetMidPoints(
108  int count /*Means that mid count will be (2^count)+1*/, bool sortthis =
109  true)
110  {
111  //int resCount=pow (2, count)+1;
112  vector<LineSegment> lsList, lsListTmp;
113  lsList.push_back(LineSegment(P1, P2));
114  vector<Point2d> res;
115  res.push_back(P1);
116  res.push_back(P2);
117  for (int counter = 0; counter < count; counter++)
118  {
119  lsListTmp.clear();
120  for (size_t i = 0; i < lsList.size(); i++)
121  {
122  LineSegment tmp = lsList[i];
123  Point2d midPoint = tmp.GetMiddle();
124  res.push_back(midPoint);
125  lsListTmp.push_back(LineSegment(tmp.P1, midPoint));
126  lsListTmp.push_back(LineSegment(tmp.P2, midPoint));
127  }
128  lsList = lsListTmp;
129  }
130 
131  if (sortthis)
132  {
133  sort(res.begin(), res.end(),
134  bind(&LineSegment::SortbyDistance, this, _1, _2));
135  }
136 
137  return res;
138  }
139 };
A class representing line segments.
Definition: LineSegment.hpp:17