Bratmobile
sensor.h
1 #ifndef SENSOR_H
2 #define SENSOR_H
3 #include "CloCK_math.h"
4 
5 class ConfiguratorInterface;
6 class Configurator;
7 
12 class Pointf: public cv::Point2f{
13  public:
14 
15  Pointf(){}
16 
17  Pointf(float _x, float _y){
18  x=_x;
19  y=_y;
20  }
21 
22  Pointf operator+(const Pointf p){
23  Pointf result;
24  result.x = x + p.x;
25  result.y = y+ p.y;
26  return result;
27  }
28 
29  Pointf operator-(const Pointf p){
30  Pointf result;
31  result.x = x - p.x;
32  result.y = y- p.y;
33  return result;
34  }
35 
36  bool isin(Pointf, Pointf);
37 
38 };
39 
44 template<>
45 struct cv::traits::Depth<Pointf> {enum {value = Depth<cv::Point2f>::value};};
46 
47 template<>
48 struct cv::traits::Type<Pointf> {enum {value = CV_MAKETYPE(cv::traits::Depth<Pointf>::value, 2)};};
49 
50 float length(cv::Point2f const& p);
51 
52 float angle(cv::Point2f const&);
53 
54 bool operator <(Pointf const &, Pointf const&);
55 
56 bool operator >(const Pointf&, const Pointf&);
57 
62 typedef std::set<Pointf> CoordinateContainer;
68 b2Vec2 getb2Vec2(cv::Point2f );
69 
77 template <typename T>
78 Pointf getPointf(T v){
79  return Pointf(v.x, v.y);
80 }
81 
89 Pointf Polar2f(float radius, float angle);
90 
98 template <typename T>
99 std::vector<T> set2vec(std::set<T> s){
100  std::vector <T> vec;
101  for (T t:s){
102  vec.emplace_back(t);
103  }
104  return vec;
105 }
106 
114 template <typename T>
115 std::vector<cv::Point2f> set2vec2f(std::set<T> s){
116  std::vector <cv::Point2f> vec;
117  for (T t:s){
118  vec.push_back(cv::Point2f(t.x, t.y));
119  }
120  return vec;
121 }
122 
130 template <typename T> inline
131 std::vector<b2Vec2> cast_b2Vec2(const std::vector<T>& v){
132  std::vector<b2Vec2> result;
133  for (const T & t:v){
134  result.push_back(b2Vec2(t.x, t.y));
135  }
136  return result;
137 }
138 
146 template <typename T> inline
147 std::vector<cv::Point2f> cast_Point2f(const std::vector<T>& v){
148  std::vector<cv::Point2f> result;
149  for (const T & t:v){
150  result.push_back(cv::Point2f(t.x, t.y));
151  }
152  return result;
153 
154 }
155 
163 template <typename T>
164 std::set<T> vec2set(std::vector<T> vec){
165  std::set <T> set;
166  for (T t:vec){
167  set.emplace(t);
168  }
169  return set;
170 }
171 
178 std::pair <bool, BodyFeatures> bounding_rotated_box(std::vector <cv::Point2f>nb);
179 
180 template <typename Pt>
181 static b2PolygonShape sensor_box(const std::vector <Pt> &all_points_pt, b2Transform robot_pose, const Disturbance * dist){
182  b2PolygonShape shape;
183  b2Vec2 centroid(2.0, 2.0), center=centroid, center_local=b2Vec2_zero;
184  float halfHeight=0, halfWidth=0;
185  if (dist->isValid()){
186  std::vector <b2Vec2> d_vertices=dist->vertices();
187  std::vector <cv::Point2f> all_points=cast_Point2f(all_points_pt);
188  for (b2Vec2 p: d_vertices){
189  p=b2MulT(robot_pose, p); //get local point
190  all_points.push_back(cv::Point2f(p.x, p.y));
191  }
192  float minx=(std::min_element(all_points.begin(),all_points.end(), CompareX())).base()->x;
193  float miny=(std::min_element(all_points.begin(), all_points.end(), CompareY())).base()->y;
194  float maxx=(std::max_element(all_points.begin(), all_points.end(), CompareX())).base()->x;
195  float maxy=(std::max_element(all_points.begin(), all_points.end(), CompareY())).base()->y;
196  halfHeight=(fabs(maxy-miny))/2; //
197  halfWidth=(fabs(maxx-minx))/2;
198  center.x=maxx-halfWidth;
199  center.y=maxy-halfHeight;
200  centroid=center-center_local;
201  }
202  shape.SetAsBox(halfWidth, halfHeight,centroid, 0);
203  return shape;
204 
205 }
206 
214 template <class Pt>
215 std::pair<bool,BodyFeatures> bounding_box( std::vector <Pt >&nb){//gets bounding box of points
216  float l=(0.0005*2), w=(0.0005*2) ;
217  float x_glob=0.0f, y_glob=0.0f;
218  std::pair <bool, BodyFeatures> result(0, BodyFeatures());
219  if (nb.empty()){
220  return result;
221  }
222  CompareX compareX;
223  CompareY compareY;
224  typename std::vector<Pt>::iterator maxx=std::max_element(nb.begin(), nb.end(), compareX);
225  typename std::vector<Pt>::iterator miny=std::min_element(nb.begin(), nb.end(), compareY);
226  typename std::vector<Pt>::iterator minx=std::min_element(nb.begin(), nb.end(), compareX);
227  typename std::vector<Pt>::iterator maxy=std::max_element(nb.begin(), nb.end(), compareY);
228  if (minx->x!=maxx->x){
229  w= fabs((*maxx).x-(*minx).x);
230  }
231  if (miny->y!=maxy->y){
232  l=fabs((*maxy).y-(*miny).y);
233  }
234  x_glob= ((*maxx).x+(*minx).x)/2;
235  y_glob= ((*maxy).y+(*miny).y)/2;
236  result.second.halfLength=l/2;
237  result.second.halfWidth=w/2;
238  result.second.pose.p=b2Vec2(x_glob, y_glob);
239  result.first=true;
240  return result;
241 }
242 
243 
244 #endif
Definition: disturbance.h:45
Definition: configurator.h:17
Wrapper around cv::Point2f for customisation purposes.
Definition: sensor.h:12
Definition: disturbance.h:22
Definition: disturbance.h:15
Definition: disturbance.h:112