Bratmobile
worldbuilder.h
1 #ifndef WORLDBUILDER_H
2 #define WORLDBUILDER_H
3 #include "sensor.h"
4 
6  protected:
7  int iteration=0;
8  char bodyFile[100];
9  float simulationStep=BOX2DRANGE;
10  int bodies=0;
11  std::vector <BodyFeatures> world_objects;
12  friend class Configurator;
13  public:
14  enum CLUSTERING{BOX=0, KMEANS=1, PARTITION=2}; //BOX: bounding box around points
16  CompareCluster()=default;
17 
18  bool operator()(const BodyFeatures & b1, const BodyFeatures & b2){ //distances of centre from start
19  bool result=false;
20  if (fabs(atan2(b1.pose.p.y, b1.pose.p.x))< fabs(atan2(b2.pose.p.y, b2.pose.p.x)) && b1.pose.p.Length()<= b2.pose.p.Length()){
21  result=true;
22  }
23  return result;
24  }
25  };
26 
27  static std::pair <CoordinateContainer, bool> salientPoints(b2Transform, const CoordinateContainer &, std::pair <Pointf, Pointf>); //gets points from the raw data that are relevant to the task based on bounding boxes
28 
37  b2Body* makeBody(b2World& w, const BodyFeatures& features);
38 
39 
45  std::vector <BodyFeatures> processData(const CoordinateContainer&, const b2Transform&);
46 
54  std::vector <BodyFeatures> cluster_data(const CoordinateContainer &pts, const b2Transform& start, CLUSTERING clustering=PARTITION);
55 
56  bool checkDisturbance(Pointf, bool&,Task * curr =NULL, float range=0.025);
57 
64  virtual std::vector <BodyFeatures> getFeatures(const CoordinateContainer &current, b2Transform start, CLUSTERING clustering=PARTITION);
65 
74  virtual void buildWorld(b2World&,b2Transform, Direction, Disturbance disturbance=Disturbance(), float halfWindowWidth=0.15, CLUSTERING clustering=CLUSTERING::PARTITION, Task * task=NULL);
75 
76  //returns top and bottom of rotated rectangle (not side-specific)
77  static std::pair <Pointf, Pointf> bounds(Direction, b2Transform t, float boxLength, float halfWindowWidth,std::vector <Pointf> *_bounds=NULL); //returns bottom and top of bounding box
78 
88  b2PolygonShape object_filtering_box(float halfWindowWidth, float boxLength, b2Transform start, Direction d);
89 
90 
91  std::pair <bool, BodyFeatures> bounding_approx_poly(std::vector <cv::Point2f>nb);
92 
98  std::vector <std::vector<cv::Point2f>> kmeans_clusters( std::vector <cv::Point2f>, std::vector <cv::Point2f>&);
99 
105  std::vector <std::vector<cv::Point2f>> partition_clusters( std::vector <cv::Point2f>);
106 
107  b2Vec2 averagePoint(const CoordinateContainer &, Disturbance &, float rad = 0.025); //finds centroid of a poitn cluster, return position vec difference
108 
109  int getBodies(){
110  return bodies;
111  }
112 
113  void add_body_count(){
114  bodies++;
115  }
116 
117  void resetBodies(){
118  bodies =0;
119  }
120 
121  void add_iteration(int i=1){
122  iteration+=1;
123  }
124 
125  b2Body * get_robot(b2World *);
126 
127  b2Fixture * get_chassis(b2Body *);
128 
136  b2AABB makeRobotSensor(b2Body* const robotBody, const Disturbance & focus)const; //returns bounding box in world coord
137 
138 
139  std::vector <BodyFeatures>& get_world_objects(){
140  return world_objects;
141  }
142 
143  void set_world_objects(const std::vector <BodyFeatures>& wo){
144  world_objects=wo;
145  }
146 
147  void setSimulationStep(float f){simulationStep=f;}
148 
149 
150 };
151 
160  protected:
161  virtual std::vector <BodyFeatures> getFeatures(const CoordinateContainer & current, b2Transform start, CLUSTERING clustering)override;
162 };
163 
168 class EverythingBuilder: public virtual WorldPointBuilder{
169  protected:
170  virtual void buildWorld(b2World & w, b2Transform start, Direction d, Disturbance disturbance, float halfWindowWidth, CLUSTERING clustering, Task * task)override;
171 };
172 
178  protected:
179  std::vector <BodyFeatures> getFeatures(const CoordinateContainer & current, b2Transform start, CLUSTERING clustering)override;
180 };
181 
186  protected:
187  std::vector <BodyFeatures> getFeatures(const CoordinateContainer & current, b2Transform start, CLUSTERING clustering)override{
188  return EveryOtherFeatureBuilder::getFeatures(current, start, clustering);
189  }
190 
191  void buildWorld(b2World & w, b2Transform start, Direction d, Disturbance disturbance, float halfWindowWidth, CLUSTERING clustering, Task * task)override{
192  EverythingBuilder::buildWorld(w, start, d, disturbance, halfWindowWidth, clustering, task);
193  }
194 };
195 
200 class LaserFocus: public virtual WorldBuilder{ //legacy
201  protected:
202  CoordinateContainer m_current;
203  public:
204  std::vector <BodyFeatures> getFeatures(const CoordinateContainer & current, b2Transform start, CLUSTERING clustering)override;
205 
206  virtual void buildWorld(b2World & w, b2Transform start, Direction d, Disturbance disturbance, float halfWindowWidth, CLUSTERING clustering, Task * task)override;
207 
208 };
209 
210 #endif
Definition: configurator.h:17
Makes a feature for every other point and builds only those in the way of task.
Definition: worldbuilder.h:177
std::vector< BodyFeatures > getFeatures(const CoordinateContainer &current, b2Transform start, CLUSTERING clustering) override
clusters point clouds into objects and returns a vector of body features
Definition: worldbuilder.cpp:328
Makes a feature for every other point and builds all points.
Definition: worldbuilder.h:185
void buildWorld(b2World &w, b2Transform start, Direction d, Disturbance disturbance, float halfWindowWidth, CLUSTERING clustering, Task *task) override
Creates bodies (objects) in the box2d world.
Definition: worldbuilder.h:191
std::vector< BodyFeatures > getFeatures(const CoordinateContainer &current, b2Transform start, CLUSTERING clustering) override
clusters point clouds into objects and returns a vector of body features
Definition: worldbuilder.h:187
Builds all points for each task.
Definition: worldbuilder.h:168
virtual void buildWorld(b2World &w, b2Transform start, Direction d, Disturbance disturbance, float halfWindowWidth, CLUSTERING clustering, Task *task) override
Creates bodies (objects) in the box2d world.
Definition: worldbuilder.cpp:322
Gets all points in the way of the task and makes a body which is a bounding upright box around all po...
Definition: worldbuilder.h:200
std::vector< BodyFeatures > getFeatures(const CoordinateContainer &current, b2Transform start, CLUSTERING clustering) override
clusters point clouds into objects and returns a vector of body features
Definition: worldbuilder.cpp:340
virtual void buildWorld(b2World &w, b2Transform start, Direction d, Disturbance disturbance, float halfWindowWidth, CLUSTERING clustering, Task *task) override
Creates bodies (objects) in the box2d world.
Definition: worldbuilder.cpp:346
A closed hybrid control loop. It has an initial disturbance representing the continuous state and a d...
Definition: task.h:47
Definition: worldbuilder.h:5
std::vector< std::vector< cv::Point2f > > partition_clusters(std::vector< cv::Point2f >)
Clusters points using the partition algorithm.
Definition: worldbuilder.cpp:71
std::vector< std::vector< cv::Point2f > > kmeans_clusters(std::vector< cv::Point2f >, std::vector< cv::Point2f > &)
Clusters points using k-means algorithm.
Definition: worldbuilder.cpp:55
virtual std::vector< BodyFeatures > getFeatures(const CoordinateContainer &current, b2Transform start, CLUSTERING clustering=PARTITION)
clusters point clouds into objects and returns a vector of body features
Definition: worldbuilder.cpp:187
std::vector< BodyFeatures > processData(const CoordinateContainer &, const b2Transform &)
returns a bounding box encompassing all points provided
Definition: worldbuilder.cpp:90
b2PolygonShape object_filtering_box(float halfWindowWidth, float boxLength, b2Transform start, Direction d)
Makes a box.
Definition: worldbuilder.cpp:38
b2Body * makeBody(b2World &w, const BodyFeatures &features)
Creates a body in the box2d world, and if the features represent a disturbance to which the attention...
Definition: worldbuilder.cpp:123
std::vector< BodyFeatures > cluster_data(const CoordinateContainer &pts, const b2Transform &start, CLUSTERING clustering=PARTITION)
Cluster point cloud data using a custom algorithm.
Definition: worldbuilder.cpp:101
virtual void buildWorld(b2World &, b2Transform, Direction, Disturbance disturbance=Disturbance(), float halfWindowWidth=0.15, CLUSTERING clustering=CLUSTERING::PARTITION, Task *task=NULL)
Creates bodies (objects) in the box2d world.
Definition: worldbuilder.cpp:206
b2AABB makeRobotSensor(b2Body *const robotBody, const Disturbance &focus) const
Makes the robot attention window, i.e. a distal sensor which is comprised between the extremes of the...
Definition: worldbuilder.cpp:295
Each point is an object but each time the world is built, only the points in the way of the task are ...
Definition: worldbuilder.h:159
virtual std::vector< BodyFeatures > getFeatures(const CoordinateContainer &current, b2Transform start, CLUSTERING clustering) override
clusters point clouds into objects and returns a vector of body features
Definition: worldbuilder.cpp:314
Given points, makes rotated bounding box.
std::set< Pointf > CoordinateContainer
container for LIDAR coordinates
Definition: sensor.h:61
Definition: disturbance.h:47
Definition: disturbance.h:113
Wrapper around cv::Point2f for customisation purposes.
Definition: sensor.h:12
Definition: worldbuilder.h:15