Bratmobile
sensor.h
Go to the documentation of this file.
1 #ifndef SENSOR_H
2 #define SENSOR_H
3 #include "brat_math.h"
4 
5 class ConfiguratorInterface;
6 class Configurator;
7 
12 struct Pointf: public cv::Point2f{
13 
14  Pointf(){}
15 
16  Pointf(float _x, float _y){
17  x=_x;
18  y=_y;
19  }
20 
21  Pointf operator+(const Pointf p){
22  Pointf result;
23  result.x = x + p.x;
24  result.y = y+ p.y;
25  return result;
26  }
27 
28  Pointf operator-(const Pointf p){
29  Pointf result;
30  result.x = x - p.x;
31  result.y = y- p.y;
32  return result;
33  }
34 
35  bool isin(Pointf, Pointf);
36 
37 };
38 
43 template<>
44 struct cv::traits::Depth<Pointf> {enum {value = Depth<cv::Point2f>::value};};
45 
46 template<>
47 struct cv::traits::Type<Pointf> {enum {value = CV_MAKETYPE(cv::traits::Depth<Pointf>::value, 2)};};
48 
49 float length(cv::Point2f const& p);
50 
51 float angle(cv::Point2f const&);
52 
53 bool operator <(Pointf const &, Pointf const&);
54 
55 bool operator >(const Pointf&, const Pointf&);
56 
61 typedef std::set<Pointf> CoordinateContainer;
67 b2Vec2 getb2Vec2(cv::Point2f );
68 
76 template <typename T>
78  return Pointf(v.x, v.y);
79 }
80 
88 Pointf Polar2f(float radius, float angle);
89 
97 template <typename T>
98 std::vector<T> set2vec(std::set<T> s){
99  std::vector <T> vec;
100  for (T t:s){
101  vec.emplace_back(t);
102  }
103  return vec;
104 }
105 
113 template <typename T>
114 std::vector<cv::Point2f> set2vec2f(std::set<T> s){
115  std::vector <cv::Point2f> vec;
116  for (T t:s){
117  vec.push_back(cv::Point2f(t.x, t.y));
118  }
119  return vec;
120 }
121 
129 template <typename T> inline
130 std::vector<b2Vec2> cast_b2Vec2(const std::vector<T>& v){
131  std::vector<b2Vec2> result;
132  for (const T & t:v){
133  result.push_back(b2Vec2(t.x, t.y));
134  }
135  return result;
136 }
137 
145 template <typename T> inline
146 std::vector<cv::Point2f> cast_Point2f(const std::vector<T>& v){
147  std::vector<cv::Point2f> result;
148  for (const T & t:v){
149  result.push_back(cv::Point2f(t.x, t.y));
150  }
151  return result;
152 
153 }
154 
162 template <typename T>
163 std::set<T> vec2set(std::vector<T> vec){
164  std::set <T> set;
165  for (T t:vec){
166  set.emplace(t);
167  }
168  return set;
169 }
170 
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:47
Definition: configurator.h:17
std::vector< b2Vec2 > cast_b2Vec2(const std::vector< T > &v)
Casts a vector of 2d points to a vector of box2d b2Vec2.
Definition: sensor.h:130
b2Vec2 getb2Vec2(cv::Point2f)
Gets an opencv point in b2VEc2 format.
Definition: sensor.cpp:30
std::set< Pointf > CoordinateContainer
container for LIDAR coordinates
Definition: sensor.h:61
std::vector< cv::Point2f > cast_Point2f(const std::vector< T > &v)
Casts a vector of 2d points to a vector of cv::Point2f.
Definition: sensor.h:146
Pointf Polar2f(float radius, float angle)
Gets Pointf from polar coordinates.
Definition: sensor.cpp:36
Pointf getPointf(T v)
Get the Pointf object from a 2d point/vector.
Definition: sensor.h:77
std::vector< cv::Point2f > set2vec2f(std::set< T > s)
Casts a set of 2d points/vectors to a vector of cv::Point2f.
Definition: sensor.h:114
std::set< T > vec2set(std::vector< T > vec)
Casts a vector to set.
Definition: sensor.h:163
std::vector< T > set2vec(std::set< T > s)
Casts a set to vector.
Definition: sensor.h:98
std::pair< bool, BodyFeatures > bounding_box(std::vector< Pt > &nb)
Makes an upright bounding box around points.
Definition: sensor.h:215
Definition: disturbance.h:24
Definition: disturbance.h:17
Definition: disturbance.h:114
Wrapper around cv::Point2f for customisation purposes.
Definition: sensor.h:12