navigation.h
Go to the documentation of this file.
1 #pragma once
2 // std
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <vector>
7 #include <list>
8 #include <set>
9 
10 /*
11 // cgal
12 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
13 #include <CGAL/Triangulation_vertex_base_with_info_2.h>
14 #include <CGAL/Polygon_2.h>
15 #include <CGAL/Polygon_with_holes_2.h>
16 #include <CGAL/Boolean_set_operations_2.h>
17 #include <CGAL/Constrained_Delaunay_triangulation_2.h>
18 #include <CGAL/Triangulation_face_base_with_info_2.h>
19 #include <CGAL/intersections.h>
20 typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
21 typedef K::Point_2 Point_2;
22 typedef CGAL::Polygon_2<K> Polygon_2;
23 struct FaceInfo2
24 {
25  FaceInfo2(){}
26  int nesting_level;
27  bool in_domain()
28  {
29  return nesting_level % 2 == 1;
30  }
31 };
32 typedef CGAL::Triangulation_vertex_base_2<K> Vb;
33 typedef CGAL::Triangulation_face_base_with_info_2<FaceInfo2, K> Fbb;
34 typedef CGAL::Constrained_triangulation_face_base_2<K, Fbb> Fb;
35 typedef CGAL::Triangulation_data_structure_2<Vb, Fb> TDS;
36 typedef CGAL::Exact_predicates_tag Itag;
37 typedef CGAL::Constrained_Delaunay_triangulation_2<K, TDS, Itag> CDT;
38 typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes_2;
39 typedef std::list<Polygon_with_holes_2> Pwh_list_2;
40 //*/
41 
42 // headers
43 #include "global.h" // global variables
44 #include "data_engine.h" // process data
45 #include "distance.h" // distance cost metric
46 #include "omt_solver.h" // solve omt problem
47 #include "tsp/tsp.h" // tsp
48 #include "tsp/usage.h" // tsp
49 #include "tsp/twoOpt.h" // tsp
50 #include "tsp/MyThread.h" // tsp
51 #include "path_optimization.h" // solve path
52 #define CPS CLOCKS_PER_SEC
53 #define NUM_THREADS 1
54 
55 const int frontier_exploration_sample_range_pixel = 10; //10 pixel = 0.5m
56 const int offset_size = (int)(0.15/map_cellsize); // offset to avoid collision
57 // random site num of CDT
58 const int random_site_num = 200; // CDT resolution. 50~200 is OK.
59 
60 // next best view
62 {
64  Eigen::Vector3i target;
65  int type = -1; // 0-object 1-frontier
66  int index = -1; // index in objects/frontiers
68  {
69  pose = iro::SE2(0, 0, 0);
70  target = Eigen::Vector3i(0, 0, 0);
71  }
73  pose = p;
74  target = Eigen::Vector3i(0, 0, 0);
75  }
76  NextBestView(iro::SE2 p, Eigen::Vector3i t, int tp, int ind=-1){
77  pose = p;
78  target = t;
79  type = tp;
80  index = ind;
81  }
82  NextBestView(iro::SE2 p, int tp, int ind){
83  pose = p;
84  target = Eigen::Vector3i(0, 0, 0);
85  type = tp;
86  index = ind;
87  }
88  NextBestView(const NextBestView & other) // 2018-11-12.
89  {
90  pose = iro::SE2(other.pose);
91  target = Eigen::Vector3i(other.target);
92  type = other.type;
93  index = other.index;
94  //cerr << "constructor: input const NextBestView &" << endl;
95  }
96  NextBestView & operator = (const NextBestView & other) // 2018-11-12.
97  {
98  pose = iro::SE2(other.pose);
99  target = Eigen::Vector3i(other.target);
100  type = other.type;
101  index = other.index;
102  //cerr << "constructor: operator =" << endl;
103  return *this;
104  }
105 };
106 
108 {
109 public:
110  Eigen::Vector3d target_position; // se2 coordinate
111  int target_type = -1; // 0-quality 1-exploration
113 
115  {
116  // none paras
117  }
118 
120  {
121  view = v;
122  }
123 
125  {
126  this->target_type = type;
127  this->view = v;
128  }
129 
130  ScanningTask(Eigen::Vector3d posi, int type, NextBestView v)
131  {
132  this->target_position = posi;
133  this->target_type = type;
134  this->view = v;
135  }
136 
137  //vector<NextBestView> computeTaskView(Polygon_2 boundary, vector<Polygon_2> holes);
138 
139 };
140 
141 // frontiers
143 {
144  int id = -1;
147  {
148  position = p;
149  }
150 };
151 
153 {
154 public:
155 
156  int m_max_task_num = 30;
157 
158  std::vector<cv::Point> m_boundary;
159  std::vector<cv::Point> m_frontiers;
160  std::vector<Point_2> m_frontiers_p2;
161  std::vector<std::vector<cv::Point>> m_holes;
162  int rbt_num;
163  std::vector<Point_2> m_robot_sites;
164 
165  // data engine
167  // distance metric
169 
170  // todo: organize temp variables...
171  std::vector<FrontierElement> m_frontierList;
172  std::set<Point_2> task_maybe_invalid;
173  std::vector<NextBestView> m_valid_object_nbvs; // valid nbvs for all objects in objectList
174  std::vector<int> m_task_index_in_valid_object_nbvs; // task indexes
175 
176  std::vector<std::vector<iro::SE2>> m_robot_move_views; // 记录规划出的机器人移动路径,坐标系为OT坐标系,包括当前位置,所以至少有一个节点(机器人当前位置)
177  std::vector<std::vector<iro::SE2>> m_robot_move_nodes; // 机器人移动路径的每个节点,至少有一个节点(机器人当前位置)
178  std::vector<std::vector<bool>> m_robot_move_nodes_nbv_sign; // 机器人移动路径的每个节点,记录是否为NBV节点
179  std::vector<std::vector<iro::SE2>> m_sync_move_paths; // 同步控制机器人移动的每个节点
180 
181  // constructor
182  Navigation(DataEngine& de, int paramK=6)
183  {
184  m_p_de = &de;
185  rbt_num = m_p_de->rbt_num;
186  m_max_task_num = m_p_de->rbt_num * paramK;
187  return;
188  }
189 
190  // extract boundary, holes, and frontiers
191  void processCurrentScene();
192 
193  // polygon simplification. reduce number of vertexes.
194  bool simplifyPolygon(Polygon_2 & poly, const double colinearThresh, bool addNoise);
195 
196  // polygon difference. exact difference of two polygons. use CGAL exact kernel. sometimes CGAL doesn't work...
197  Pwh_list_2 differenceCGALExactKernel(Polygon_with_holes_2 domain, Polygon_2 hole);
198 
199  // load and check boundary
200  Polygon_2 load_and_check_boundary();
201 
202  // load and check holes
203  std::vector<Polygon_2> load_and_check_holes(Polygon_2 boundary, std::vector<Polygon_2> & origin_holes);
204 
205  // correction of boundary and holes: double check boundary&holes together, remove overlaps, then reset boundary and holes
206  void correct_boundary_holes(Polygon_2 & boundary, std::vector<Polygon_2> & holes);
207 
208  // generate robot move domain
209  bool robotMoveDomainProcess(Polygon_2 & boundary, std::vector<Polygon_2> & holes, std::vector<Polygon_2> & origin_holes);
210 
211  // CDT
212  bool generateMoveDomainCDT(Polygon_2 boundary, std::vector<Polygon_2> holes, std::vector<Polygon_2> origin_holes, std::vector<ScanningTask> tasks, CDT & cdt);
213 
214  // load and check frontiers, saved in the variable frontiers
215  std::vector<Point_2> load_and_check_frontiers(Polygon_2 & boundary, std::vector<Polygon_2> & holes);
216 
217  // compute location map
218  cv::Mat computeLoationMap();
219 
220  // remove invalid frontiers, save valid frontiers to frontier list.
221  std::vector<int> preprocess_frontiers(Polygon_2 outer, std::vector<Polygon_2> iner);
222 
223  // check view ray validness(without occlusion) 2018-09-12.
224  bool checkViewRayValidness(cv::Point source, cv::Point target);
225 
226  // frustum contour.
227  vector<cv::Point> get_frustum_contuor(iro::SE2 pose);
228 
229  // exe func: return nbvs
230  std::vector<NextBestView> extractQualityTaskViews(Polygon_2 boundary, std::vector<Polygon_2> holes);
231 
232  // NBV for frontiers. 2018-09-22.
233  std::vector<NextBestView> generateViewsFrontiers(bool enableSampling=false, double sampleRate=0.5);
234 
235  // tasks func
236  std::vector<NextBestView> compute_frontier_tasks(Polygon_2 boundary, std::vector<Polygon_2> holes);
237 
238  // extract tasks
239  bool extractTasks(Polygon_2 boundary, std::vector<Polygon_2> holes, std::vector<ScanningTask> & tasks);
240 
241  // TSP, include the first node for current pose
242  std::vector<int> TSP_path(std::vector<Point_2> points, std::vector<vector<double>> weights);
243 
244  // OMT with TSP
245  void OMT_TSP();
246 
247 /*
248  // check trojectories
249  void Navigation::checkTrojectories();
250 //*/
251 
252  // compute m_robot_move_nodes
253  bool compute_robot_move_nodes(std::vector<double> & distances);
254 
255  // path_occlusion_check
256  bool path_occlusion_check(cv::Mat background, cv::Point beg, cv::Point end);
257 
258  // check Point_2 equal
259  bool points_equal(Point_2 p1, Point_2 p2);
260 
261  // check se2 equal
262  bool se2_equal(iro::SE2 p1, iro::SE2 p2);
263 
264  // compute m_sync_move_paths
265  bool compute_sync_move_paths(std::vector<double> distances);
266 
267  // uniform sample nodes from nodes inside length
268  std::vector<iro::SE2> uniformSampleWithNBVInfo(const int robot_id, const double step, const double length);
269 
270  // camera trajectory interpolation: delta distance/angle constraints.
271  void trajectoryInterpolation(std::vector<iro::SE2> & path, double dAngle = PI / 12);
272 
273  // traj opt
274  void Trajectory_Optimization();
275 
276  // ask robot to move and scan
277  void moveRobotsAndScan();
278 
279  // visualization. 2018-11-16.
280  bool visualizeScan(std::vector<iro::SE2> current_views, int vid);
281 
282 /*
283  // test
284  void test()
285  {
286  Point_2 source(667.085, -562.966);
287  vector<Point_2> targets;
288  targets.push_back(Point_2(20, -20));
289  targets.push_back(Point_2(652.233, -636.267));
290  DistanceMetric dm;
291  vector<double> distances = dm.getGeodesicDistances(source, targets);
292  for (int i = 0; i < distances.size(); ++i)
293  {
294  cerr<<"distances "<<i<<" "<<distances[i]<<endl;
295  }
296 
297  return;
298  }
299 //*/
300 
301  // finished.
302  void scanFinished();
303 };
NextBestView(iro::SE2 p, Eigen::Vector3i t, int tp, int ind=-1)
Definition: navigation.h:76
K::Point_2 Point_2
Definition: global.h:32
NextBestView(iro::SE2 p)
Definition: navigation.h:72
FrontierElement(Point_2 p)
Definition: navigation.h:146
DataEngine * m_p_de
Definition: navigation.h:166
std::vector< std::vector< cv::Point > > m_holes
Definition: navigation.h:161
DistanceMetric m_metric
Definition: navigation.h:168
std::vector< Point_2 > m_frontiers_p2
Definition: navigation.h:160
Eigen::Vector3d target_position
Definition: navigation.h:110
Navigation(DataEngine &de, int paramK=6)
Definition: navigation.h:182
#define PI
Definition: global.h:13
NextBestView(const NextBestView &other)
Definition: navigation.h:88
std::vector< std::vector< iro::SE2 > > m_robot_move_views
Definition: navigation.h:176
std::vector< FrontierElement > m_frontierList
Definition: navigation.h:171
CGAL::Polygon_with_holes_2< K > Polygon_with_holes_2
Definition: global.h:49
ScanningTask(Eigen::Vector3d posi, int type, NextBestView v)
Definition: navigation.h:130
Eigen::Vector3i target
Definition: navigation.h:64
ScanningTask(int type, NextBestView v)
Definition: navigation.h:124
std::vector< cv::Point > m_frontiers
Definition: navigation.h:159
Point_2 position
Definition: navigation.h:145
NextBestView view
Definition: navigation.h:112
iro::SE2 pose
Definition: navigation.h:63
std::vector< Point_2 > m_robot_sites
Definition: navigation.h:163
std::vector< std::vector< iro::SE2 > > m_sync_move_paths
Definition: navigation.h:179
const float map_cellsize
Definition: data_engine.h:39
NextBestView(iro::SE2 p, int tp, int ind)
Definition: navigation.h:82
std::vector< NextBestView > m_valid_object_nbvs
Definition: navigation.h:173
std::list< Polygon_with_holes_2 > Pwh_list_2
Definition: global.h:50
std::vector< std::vector< iro::SE2 > > m_robot_move_nodes
Definition: navigation.h:177
Definition: se2.h:49
std::vector< std::vector< bool > > m_robot_move_nodes_nbv_sign
Definition: navigation.h:178
CGAL::Constrained_Delaunay_triangulation_2< K, TDS, Itag > CDT
Definition: global.h:48
NextBestView & operator=(const NextBestView &other)
Definition: navigation.h:96
std::vector< int > m_task_index_in_valid_object_nbvs
Definition: navigation.h:174
CGAL::Polygon_2< K > Polygon_2
Definition: global.h:33
std::vector< cv::Point > m_boundary
Definition: navigation.h:158
std::set< Point_2 > task_maybe_invalid
Definition: navigation.h:172
ScanningTask(NextBestView v)
Definition: navigation.h:119


co_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:00:45