Bratmobile
disturbance.h
1 #ifndef DISTURBANCE_H
2 #include "robot.h"
3 #include "threshold.h"
4 #include "box2d_helpers.h"
5 #include <algorithm>
6 #include <stdexcept>
7 #include <opencv2/core.hpp>
8 #include <opencv2/imgproc.hpp> //useful down the line! (graphTools)
9 #include <opencv2/tracking.hpp>
10 #include <opencv2/video/tracking.hpp> //kalman filter
11 
12 
13 typedef unsigned int AffordanceIndex; //was thinking of this being a character but doesn't have to be maybe enum is fine
14 
15 
16 
17 struct CompareY{
18  template <typename T>
19  bool operator() ( T a, T b ){ //
20  return a.y <=b.y;
21  }
22 };
23 
24 struct CompareX{
25  template <typename T>
26  bool operator()(T a, T b){
27  return a.x<=b.x;
28  }
29 };
30 
31 template <typename C>
32 std::vector <C> arrayToVec(C* c, int ct){
33  std::vector <C> result;
34  for (int i=0; i<ct; i++){
35  result.push_back(*c);
36  c++;
37  }
38  return result;
39 }
40 
41 
42 class ControlInterface;
43 class Bundle;
48  public:
49  b2Transform pose {b2Transform(b2Vec2(0,0), b2Rot(0))} ;
50  float halfLength=MIN_BODY_DIMENSION;//x
51  float halfWidth=MIN_BODY_DIMENSION; //y
52  float shift=0.0f;
53  b2BodyType bodyType = b2_dynamicBody;
54 
55  b2Shape::Type shape = b2Shape::e_polygon;
56  bool attention=false; //attention is used to indicate that the disturbance is the initial disturbance of a task and is being tracked by the robot using the attention window
57 
58  BodyFeatures(){}
59 
60  BodyFeatures(b2Transform _pose):pose(_pose){}
61 
62  void setHalfLength(const float & f){
63  if (f<MIN_BODY_DIMENSION){
64  halfLength=MIN_BODY_DIMENSION;
65  }
66  else{
67  halfLength=f;
68  }
69  }
70 
71  void setHalfWidth(const float & f){
72  if (f<MIN_BODY_DIMENSION){
73  halfWidth=MIN_BODY_DIMENSION;
74  }
75  else{
76  halfWidth=f;
77  }
78  }
79 
86  bool match(const BodyFeatures&, Bundle * bundle=NULL, b2Transform t=b2Transform_zero)const;
87 
88  float width()const{
89  return halfWidth*2;
90  }
91 
92  float length()const{
93  return halfLength*2;
94  }
95 
96  float area()const{
97  return width()*length();
98  }
99 
100  std::vector <b2Vec2> vertices()const;
101 
102  std::vector <cv::Point2f> vertices_cv()const; //global vertices
103 
104  bool is_point()const{
105  return halfWidth==MIN_BODY_DIMENSION && halfLength==MIN_BODY_DIMENSION;
106  }
107 
108  Bundle get_bundle(){
109  return Bundle(pose.p.x, pose.p.y, pose.q.GetAngle(), halfWidth, halfLength);
110  }
111 };
112 
113 
114 struct Disturbance{
115 
116 protected:
117 friend class ControlInterface;
118 friend struct StateMatcher;
119  AffordanceIndex affordanceIndex = NONE; //not using the enum because in the future we might want to add more affordances
120  bool valid= 0;
121  bool rotation_valid=0;
122 
123  void setOrientation(float f){ //returns orientation (angle) of a point, in order
124  rotation_valid=1;
125  bf.pose.q.Set(f);
126  }
127 
128  void addToOrientation(float dtheta){
129  if (rotation_valid){
130  setOrientation(bf.pose.q.GetAngle()+dtheta);
131  }
132  else{
133  setOrientation(dtheta);
134  }
135 }
136 
137 public:
138  BodyFeatures bf=BodyFeatures(b2Transform(b2Vec2(10000, 10000), b2Rot(M_PI)));
139 
140  Disturbance(){};
141  Disturbance(AffordanceIndex i){
142  affordanceIndex = i;
143  }
144 
145  Disturbance(AffordanceIndex i, b2Vec2 p){
146  affordanceIndex = i;
147  bf.pose.Set(p, 0);
148  valid =1;
149  }
150 
151  Disturbance(AffordanceIndex i, b2Vec2 p, float a){
152  affordanceIndex = i;
153  bf.pose.Set(p,a);
154  valid =1;
155  }
156 
157  Disturbance(BodyFeatures _bf): bf(_bf){
158  affordanceIndex=AVOID;
159  }
160 
161  Disturbance(b2Body* b){
162  bf.pose=b->GetTransform(); //global
163  b2Fixture * fixture =b->GetFixtureList();
164  bf.shape=(fixture->GetShape()->GetType());
165  valid=1;
166  if (bf.shape==b2Shape::e_polygon){
167  b2PolygonShape * poly=(b2PolygonShape*)fixture->GetShape();
168  std::vector <b2Vec2> local_vertices=arrayToVec(poly->m_vertices, poly->m_count);
169  CompareX compareX;
170  CompareY compareY;
171  float minx=(std::min_element(local_vertices.begin(), local_vertices.end(), compareX)).base()->x;
172  float miny=(std::min_element(local_vertices.begin(), local_vertices.end(), compareY)).base()->y;
173  float maxx=(std::max_element(local_vertices.begin(), local_vertices.end(), compareX)).base()->x;
174  float maxy=(std::max_element(local_vertices.begin(), local_vertices.end(), compareY)).base()->y;
175  bf.halfLength=(fabs(maxy-miny))/2; //local coordinates
176  bf.halfWidth=(fabs(maxx-minx))/2;
177  }
178  bf.attention=true;
179  affordanceIndex=1;
180  }
181 
182  float getAngle(b2Transform);
183 
184  float getAngle(b2Body* b){
185  return getAngle(b->GetTransform());
186  }
187 
188  void setPosition(b2Vec2 pos){
189  bf.pose.p.Set(pos.x, pos.y);
190  }
191 
192  void setPosition(float x, float y){
193  bf.pose.p.Set(x, y);
194  }
195 
196  b2Vec2 getPosition()const{
197  return bf.pose.p;
198  }
199 
200 
201  bool isValid()const{
202  return valid;
203  }
204 
205  AffordanceIndex getAffIndex()const{
206  return affordanceIndex;
207  }
208 
209  void set_affordance(AffordanceIndex a){
210  affordanceIndex=a;
211  }
212 
213  void invalidate(){
214  valid =0;
215  }
216 
217  void validate(){
218  valid=1;
219  }
220 
221 
222  std::pair<bool, float> getOrientation(){
223 
224  return std::pair<bool, float>(rotation_valid, bf.pose.q.GetAngle());
225  }
226 
227  b2Transform pose()const{
228  return bf.pose;
229  }
230 
231  void setPose(b2Transform t){
232  bf.pose=t;
233  }
234 
235  void setAsBox(float w, float l){
236  bf.halfLength=l;
237  bf.halfWidth=w;
238  }
239 
240  BodyFeatures bodyFeatures()const{
241  return bf;
242  }
243 
244 
245  void subtractPose(b2Transform dPose){
246  bf.pose.p.x-=dPose.p.x;
247  bf.pose.p.y-=dPose.p.y;
248  addToOrientation(-dPose.q.GetAngle());
249  }
250 
251  float halfLength(){
252  return bf.halfLength;
253  }
254 
255  float halfWidth(){
256  return bf.halfWidth;
257  }
258 
259  void setOrientation(float, float);
260 
261  std::vector <b2Vec2> vertices()const; //global vertices
262 
263  // bool operator==(const Disturbance & d);
264 
265  bool operator==(const Disturbance & d)const;
266 
267 };
268 
269 
270 struct simResult{
271  enum resultType {successful =0, crashed =1, safeForNow=2}; //successful =0, crashed =1, safeForNow=2
272  resultType resultCode= resultType::successful;
273  Disturbance collision;
274  //bool valid = 0;
275  b2Transform endPose = b2Transform(b2Vec2(0.0, 0.0), b2Rot(0));
276  int step=0;
277 
278 
279  simResult(){}
280 
281  simResult(resultType code): resultCode(code){
282  // valid =1;
283  }
284 
285  simResult(resultType code, Disturbance obst): resultCode(code), collision(obst){
286  // valid =1;
287  }
288 };
289 
290 std::vector <b2Vec2> GetLocalPoints( std::vector <b2Vec2>, const b2Body *);
291 
292 #endif
Definition: disturbance.h:47
bool match(const BodyFeatures &, Bundle *bundle=NULL, b2Transform t=b2Transform_zero) const
Definition: disturbance.cpp:3
Definition: threshold.h:10
Definition: graphTools.h:453
Definition: disturbance.h:24
Definition: disturbance.h:17
Definition: disturbance.h:114
Definition: disturbance.h:270