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