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