ikd_Tree.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #pragma once
  2. #include <pcl/point_types.h>
  3. #include <Eigen/StdVector>
  4. #include <Eigen/Geometry>
  5. #include <stdio.h>
  6. #include <queue>
  7. #include <pthread.h>
  8. #include <chrono>
  9. #include <time.h>
  10. #define EPSS 1e-6
  11. #define Minimal_Unbalanced_Tree_Size 10
  12. #define Multi_Thread_Rebuild_Point_Num 1500
  13. #define DOWNSAMPLE_SWITCH true
  14. #define ForceRebuildPercentage 0.2
  15. #define Q_LEN 1000000
  16. using namespace std;
  17. typedef pcl::PointXYZINormal PointType;
  18. typedef vector<PointType, Eigen::aligned_allocator<PointType>> PointVector;
  19. const PointType ZeroP;
  20. struct KD_TREE_NODE
  21. {
  22. PointType point;
  23. int division_axis;
  24. int TreeSize = 1;
  25. int invalid_point_num = 0;
  26. int down_del_num = 0;
  27. bool point_deleted = false;
  28. bool tree_deleted = false;
  29. bool point_downsample_deleted = false;
  30. bool tree_downsample_deleted = false;
  31. bool need_push_down_to_left = false;
  32. bool need_push_down_to_right = false;
  33. bool working_flag = false;
  34. pthread_mutex_t push_down_mutex_lock;
  35. float node_range_x[2], node_range_y[2], node_range_z[2];
  36. KD_TREE_NODE *left_son_ptr = nullptr;
  37. KD_TREE_NODE *right_son_ptr = nullptr;
  38. KD_TREE_NODE *father_ptr = nullptr;
  39. // For paper data record
  40. float alpha_del;
  41. float alpha_bal;
  42. };
  43. struct PointType_CMP{
  44. PointType point;
  45. float dist = 0.0;
  46. PointType_CMP (PointType p = ZeroP, float d = INFINITY){
  47. this->point = p;
  48. this->dist = d;
  49. };
  50. bool operator < (const PointType_CMP &a)const{
  51. if (fabs(dist - a.dist) < 1e-10) return point.x < a.point.x;
  52. else return dist < a.dist;
  53. }
  54. };
  55. struct BoxPointType{
  56. float vertex_min[3];
  57. float vertex_max[3];
  58. };
  59. enum operation_set {ADD_POINT, DELETE_POINT, DELETE_BOX, ADD_BOX, DOWNSAMPLE_DELETE, PUSH_DOWN};
  60. enum delete_point_storage_set {NOT_RECORD, DELETE_POINTS_REC, MULTI_THREAD_REC};
  61. struct Operation_Logger_Type{
  62. PointType point;
  63. BoxPointType boxpoint;
  64. bool tree_deleted, tree_downsample_deleted;
  65. operation_set op;
  66. };
  67. class MANUAL_Q{
  68. private:
  69. int head = 0,tail = 0, counter = 0;
  70. Operation_Logger_Type q[Q_LEN];
  71. bool is_empty;
  72. public:
  73. void pop();
  74. Operation_Logger_Type front();
  75. Operation_Logger_Type back();
  76. void clear();
  77. void push(Operation_Logger_Type op);
  78. bool empty();
  79. int size();
  80. };
  81. class MANUAL_HEAP
  82. {
  83. public:
  84. MANUAL_HEAP(int max_capacity = 100);
  85. ~MANUAL_HEAP();
  86. void pop();
  87. PointType_CMP top();
  88. void push(PointType_CMP point);
  89. int size();
  90. void clear();
  91. private:
  92. PointType_CMP * heap;
  93. void MoveDown(int heap_index);
  94. void FloatUp(int heap_index);
  95. int heap_size = 0;
  96. int cap = 0;
  97. };
  98. class KD_TREE
  99. {
  100. private:
  101. // Multi-thread Tree Rebuild
  102. bool termination_flag = false;
  103. bool rebuild_flag = false;
  104. pthread_t rebuild_thread;
  105. pthread_mutex_t termination_flag_mutex_lock, rebuild_ptr_mutex_lock, working_flag_mutex, search_flag_mutex;
  106. pthread_mutex_t rebuild_logger_mutex_lock, points_deleted_rebuild_mutex_lock;
  107. // queue<Operation_Logger_Type> Rebuild_Logger;
  108. MANUAL_Q Rebuild_Logger;
  109. PointVector Rebuild_PCL_Storage;
  110. KD_TREE_NODE ** Rebuild_Ptr = nullptr;
  111. int search_mutex_counter = 0;
  112. static void * multi_thread_ptr(void *arg);
  113. void multi_thread_rebuild();
  114. void start_thread();
  115. void stop_thread();
  116. void run_operation(KD_TREE_NODE ** root, Operation_Logger_Type operation);
  117. // KD Tree Functions and augmented variables
  118. int Treesize_tmp = 0, Validnum_tmp = 0;
  119. float alpha_bal_tmp = 0.5, alpha_del_tmp = 0.0;
  120. float delete_criterion_param = 0.5f;
  121. float balance_criterion_param = 0.7f;
  122. float downsample_size = 0.2f;
  123. bool Delete_Storage_Disabled = false;
  124. KD_TREE_NODE * STATIC_ROOT_NODE = nullptr;
  125. PointVector Points_deleted;
  126. PointVector Downsample_Storage;
  127. PointVector Multithread_Points_deleted;
  128. void InitTreeNode(KD_TREE_NODE * root);
  129. void Test_Lock_States(KD_TREE_NODE *root);
  130. void BuildTree(KD_TREE_NODE ** root, int l, int r, PointVector & Storage);
  131. void Rebuild(KD_TREE_NODE ** root);
  132. int Delete_by_range(KD_TREE_NODE ** root, BoxPointType boxpoint, bool allow_rebuild, bool is_downsample);
  133. void Delete_by_point(KD_TREE_NODE ** root, PointType point, bool allow_rebuild);
  134. void Add_by_point(KD_TREE_NODE ** root, PointType point, bool allow_rebuild, int father_axis);
  135. void Add_by_range(KD_TREE_NODE ** root, BoxPointType boxpoint, bool allow_rebuild);
  136. void Search(KD_TREE_NODE * root, int k_nearest, PointType point, MANUAL_HEAP &q, double max_dist);//priority_queue<PointType_CMP>
  137. void Search_by_range(KD_TREE_NODE *root, BoxPointType boxpoint, PointVector &Storage);
  138. bool Criterion_Check(KD_TREE_NODE * root);
  139. void Push_Down(KD_TREE_NODE * root);
  140. void Update(KD_TREE_NODE * root);
  141. void delete_tree_nodes(KD_TREE_NODE ** root);
  142. void downsample(KD_TREE_NODE ** root);
  143. bool same_point(PointType a, PointType b);
  144. float calc_dist(PointType a, PointType b);
  145. float calc_box_dist(KD_TREE_NODE * node, PointType point);
  146. static bool point_cmp_x(PointType a, PointType b);
  147. static bool point_cmp_y(PointType a, PointType b);
  148. static bool point_cmp_z(PointType a, PointType b);
  149. public:
  150. KD_TREE(float delete_param = 0.5, float balance_param = 0.6 , float box_length = 0.2);
  151. ~KD_TREE();
  152. void Set_delete_criterion_param(float delete_param);
  153. void Set_balance_criterion_param(float balance_param);
  154. void set_downsample_param(float box_length);
  155. void InitializeKDTree(float delete_param = 0.5, float balance_param = 0.7, float box_length = 0.2);
  156. int size();
  157. int validnum();
  158. void root_alpha(float &alpha_bal, float &alpha_del);
  159. void Build(PointVector point_cloud);
  160. void Nearest_Search(PointType point, int k_nearest, PointVector &Nearest_Points, vector<float> & Point_Distance, double max_dist = INFINITY);
  161. int Add_Points(PointVector & PointToAdd, bool downsample_on);
  162. void Add_Point_Boxes(vector<BoxPointType> & BoxPoints);
  163. void Delete_Points(PointVector & PointToDel);
  164. int Delete_Point_Boxes(vector<BoxPointType> & BoxPoints);
  165. void flatten(KD_TREE_NODE * root, PointVector &Storage, delete_point_storage_set storage_type);
  166. void acquire_removed_points(PointVector & removed_points);
  167. BoxPointType tree_range();
  168. PointVector PCL_Storage;
  169. KD_TREE_NODE * Root_Node = nullptr;
  170. int max_queue_size = 0;
  171. };