ikd_Tree.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. #include "ikd_Tree.h"
  2. /*
  3. Description: ikd-Tree: an incremental k-d tree for robotic applications
  4. Author: Yixi Cai
  5. email: yixicai@connect.hku.hk
  6. */
  7. KD_TREE::KD_TREE(float delete_param, float balance_param, float box_length) {
  8. delete_criterion_param = delete_param;
  9. balance_criterion_param = balance_param;
  10. downsample_size = box_length;
  11. Rebuild_Logger.clear();
  12. termination_flag = false;
  13. start_thread();
  14. }
  15. KD_TREE::~KD_TREE()
  16. {
  17. stop_thread();
  18. Delete_Storage_Disabled = true;
  19. delete_tree_nodes(&Root_Node);
  20. PointVector ().swap(PCL_Storage);
  21. Rebuild_Logger.clear();
  22. }
  23. void KD_TREE::Set_delete_criterion_param(float delete_param){
  24. delete_criterion_param = delete_param;
  25. }
  26. void KD_TREE::Set_balance_criterion_param(float balance_param){
  27. balance_criterion_param = balance_param;
  28. }
  29. void KD_TREE::set_downsample_param(float downsample_param){
  30. downsample_size = downsample_param;
  31. }
  32. void KD_TREE::InitializeKDTree(float delete_param, float balance_param, float box_length){
  33. Set_delete_criterion_param(delete_param);
  34. Set_balance_criterion_param(balance_param);
  35. set_downsample_param(box_length);
  36. }
  37. void KD_TREE::InitTreeNode(KD_TREE_NODE * root){
  38. root->point.x = 0.0f;
  39. root->point.y = 0.0f;
  40. root->point.z = 0.0f;
  41. root->node_range_x[0] = 0.0f;
  42. root->node_range_x[1] = 0.0f;
  43. root->node_range_y[0] = 0.0f;
  44. root->node_range_y[1] = 0.0f;
  45. root->node_range_z[0] = 0.0f;
  46. root->node_range_z[1] = 0.0f;
  47. root->division_axis = 0;
  48. root->father_ptr = nullptr;
  49. root->left_son_ptr = nullptr;
  50. root->right_son_ptr = nullptr;
  51. root->TreeSize = 0;
  52. root->invalid_point_num = 0;
  53. root->down_del_num = 0;
  54. root->point_deleted = false;
  55. root->tree_deleted = false;
  56. root->need_push_down_to_left = false;
  57. root->need_push_down_to_right = false;
  58. root->point_downsample_deleted = false;
  59. root->working_flag = false;
  60. pthread_mutex_init(&(root->push_down_mutex_lock),NULL);
  61. }
  62. int KD_TREE::size(){
  63. int s = 0;
  64. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != Root_Node){
  65. if (Root_Node != nullptr) {
  66. return Root_Node->TreeSize;
  67. } else {
  68. return 0;
  69. }
  70. } else {
  71. if (!pthread_mutex_trylock(&working_flag_mutex)){
  72. s = Root_Node->TreeSize;
  73. pthread_mutex_unlock(&working_flag_mutex);
  74. return s;
  75. } else {
  76. return Treesize_tmp;
  77. }
  78. }
  79. }
  80. BoxPointType KD_TREE::tree_range(){
  81. BoxPointType range;
  82. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != Root_Node){
  83. if (Root_Node != nullptr) {
  84. range.vertex_min[0] = Root_Node->node_range_x[0];
  85. range.vertex_min[1] = Root_Node->node_range_y[0];
  86. range.vertex_min[2] = Root_Node->node_range_z[0];
  87. range.vertex_max[0] = Root_Node->node_range_x[1];
  88. range.vertex_max[1] = Root_Node->node_range_y[1];
  89. range.vertex_max[2] = Root_Node->node_range_z[1];
  90. } else {
  91. memset(&range, 0, sizeof(range));
  92. }
  93. } else {
  94. if (!pthread_mutex_trylock(&working_flag_mutex)){
  95. range.vertex_min[0] = Root_Node->node_range_x[0];
  96. range.vertex_min[1] = Root_Node->node_range_y[0];
  97. range.vertex_min[2] = Root_Node->node_range_z[0];
  98. range.vertex_max[0] = Root_Node->node_range_x[1];
  99. range.vertex_max[1] = Root_Node->node_range_y[1];
  100. range.vertex_max[2] = Root_Node->node_range_z[1];
  101. pthread_mutex_unlock(&working_flag_mutex);
  102. } else {
  103. memset(&range, 0, sizeof(range));
  104. }
  105. }
  106. return range;
  107. }
  108. int KD_TREE::validnum(){
  109. int s = 0;
  110. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != Root_Node){
  111. if (Root_Node != nullptr)
  112. return (Root_Node->TreeSize - Root_Node->invalid_point_num);
  113. else
  114. return 0;
  115. } else {
  116. if (!pthread_mutex_trylock(&working_flag_mutex)){
  117. s = Root_Node->TreeSize-Root_Node->invalid_point_num;
  118. pthread_mutex_unlock(&working_flag_mutex);
  119. return s;
  120. } else {
  121. return -1;
  122. }
  123. }
  124. }
  125. void KD_TREE::root_alpha(float &alpha_bal, float &alpha_del){
  126. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != Root_Node){
  127. alpha_bal = Root_Node->alpha_bal;
  128. alpha_del = Root_Node->alpha_del;
  129. return;
  130. } else {
  131. if (!pthread_mutex_trylock(&working_flag_mutex)){
  132. alpha_bal = Root_Node->alpha_bal;
  133. alpha_del = Root_Node->alpha_del;
  134. pthread_mutex_unlock(&working_flag_mutex);
  135. return;
  136. } else {
  137. alpha_bal = alpha_bal_tmp;
  138. alpha_del = alpha_del_tmp;
  139. return;
  140. }
  141. }
  142. }
  143. void KD_TREE::start_thread(){
  144. pthread_mutex_init(&termination_flag_mutex_lock, NULL);
  145. pthread_mutex_init(&rebuild_ptr_mutex_lock, NULL);
  146. pthread_mutex_init(&rebuild_logger_mutex_lock, NULL);
  147. pthread_mutex_init(&points_deleted_rebuild_mutex_lock, NULL);
  148. pthread_mutex_init(&working_flag_mutex, NULL);
  149. pthread_mutex_init(&search_flag_mutex, NULL);
  150. pthread_create(&rebuild_thread, NULL, multi_thread_ptr, (void*) this);
  151. printf("Multi thread started \n");
  152. }
  153. void KD_TREE::stop_thread(){
  154. pthread_mutex_lock(&termination_flag_mutex_lock);
  155. termination_flag = true;
  156. pthread_mutex_unlock(&termination_flag_mutex_lock);
  157. if (rebuild_thread) pthread_join(rebuild_thread, NULL);
  158. pthread_mutex_destroy(&termination_flag_mutex_lock);
  159. pthread_mutex_destroy(&rebuild_logger_mutex_lock);
  160. pthread_mutex_destroy(&rebuild_ptr_mutex_lock);
  161. pthread_mutex_destroy(&points_deleted_rebuild_mutex_lock);
  162. pthread_mutex_destroy(&working_flag_mutex);
  163. pthread_mutex_destroy(&search_flag_mutex);
  164. }
  165. void * KD_TREE::multi_thread_ptr(void * arg){
  166. KD_TREE * handle = (KD_TREE*) arg;
  167. handle->multi_thread_rebuild();
  168. return nullptr;
  169. }
  170. void KD_TREE::multi_thread_rebuild(){
  171. bool terminated = false;
  172. KD_TREE_NODE * father_ptr, ** new_node_ptr;
  173. pthread_mutex_lock(&termination_flag_mutex_lock);
  174. terminated = termination_flag;
  175. pthread_mutex_unlock(&termination_flag_mutex_lock);
  176. while (!terminated){
  177. pthread_mutex_lock(&rebuild_ptr_mutex_lock);
  178. pthread_mutex_lock(&working_flag_mutex);
  179. if (Rebuild_Ptr != nullptr ){
  180. /* Traverse and copy */
  181. if (!Rebuild_Logger.empty()){
  182. printf("\n\n\n\n\n\n\n\n\n\n\n ERROR!!! \n\n\n\n\n\n\n\n\n");
  183. }
  184. rebuild_flag = true;
  185. if (*Rebuild_Ptr == Root_Node) {
  186. Treesize_tmp = Root_Node->TreeSize;
  187. Validnum_tmp = Root_Node->TreeSize - Root_Node->invalid_point_num;
  188. alpha_bal_tmp = Root_Node->alpha_bal;
  189. alpha_del_tmp = Root_Node->alpha_del;
  190. }
  191. KD_TREE_NODE * old_root_node = (*Rebuild_Ptr);
  192. father_ptr = (*Rebuild_Ptr)->father_ptr;
  193. PointVector ().swap(Rebuild_PCL_Storage);
  194. // Lock Search
  195. pthread_mutex_lock(&search_flag_mutex);
  196. while (search_mutex_counter != 0){
  197. pthread_mutex_unlock(&search_flag_mutex);
  198. usleep(1);
  199. pthread_mutex_lock(&search_flag_mutex);
  200. }
  201. search_mutex_counter = -1;
  202. pthread_mutex_unlock(&search_flag_mutex);
  203. // Lock deleted points cache
  204. pthread_mutex_lock(&points_deleted_rebuild_mutex_lock);
  205. flatten(*Rebuild_Ptr, Rebuild_PCL_Storage, MULTI_THREAD_REC);
  206. // Unlock deleted points cache
  207. pthread_mutex_unlock(&points_deleted_rebuild_mutex_lock);
  208. // Unlock Search
  209. pthread_mutex_lock(&search_flag_mutex);
  210. search_mutex_counter = 0;
  211. pthread_mutex_unlock(&search_flag_mutex);
  212. pthread_mutex_unlock(&working_flag_mutex);
  213. /* Rebuild and update missed operations*/
  214. Operation_Logger_Type Operation;
  215. KD_TREE_NODE * new_root_node = nullptr;
  216. if (int(Rebuild_PCL_Storage.size()) > 0){
  217. BuildTree(&new_root_node, 0, Rebuild_PCL_Storage.size()-1, Rebuild_PCL_Storage);
  218. // Rebuild has been done. Updates the blocked operations into the new tree
  219. pthread_mutex_lock(&working_flag_mutex);
  220. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  221. int tmp_counter = 0;
  222. while (!Rebuild_Logger.empty()){
  223. Operation = Rebuild_Logger.front();
  224. max_queue_size = max(max_queue_size, Rebuild_Logger.size());
  225. Rebuild_Logger.pop();
  226. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  227. pthread_mutex_unlock(&working_flag_mutex);
  228. run_operation(&new_root_node, Operation);
  229. tmp_counter ++;
  230. if (tmp_counter % 10 == 0) usleep(1);
  231. pthread_mutex_lock(&working_flag_mutex);
  232. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  233. }
  234. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  235. }
  236. /* Replace to original tree*/
  237. // pthread_mutex_lock(&working_flag_mutex);
  238. pthread_mutex_lock(&search_flag_mutex);
  239. while (search_mutex_counter != 0){
  240. pthread_mutex_unlock(&search_flag_mutex);
  241. usleep(1);
  242. pthread_mutex_lock(&search_flag_mutex);
  243. }
  244. search_mutex_counter = -1;
  245. pthread_mutex_unlock(&search_flag_mutex);
  246. if (father_ptr->left_son_ptr == *Rebuild_Ptr) {
  247. father_ptr->left_son_ptr = new_root_node;
  248. } else if (father_ptr->right_son_ptr == *Rebuild_Ptr){
  249. father_ptr->right_son_ptr = new_root_node;
  250. } else {
  251. throw "Error: Father ptr incompatible with current node\n";
  252. }
  253. if (new_root_node != nullptr) new_root_node->father_ptr = father_ptr;
  254. (*Rebuild_Ptr) = new_root_node;
  255. int valid_old = old_root_node->TreeSize-old_root_node->invalid_point_num;
  256. int valid_new = new_root_node->TreeSize-new_root_node->invalid_point_num;
  257. if (father_ptr == STATIC_ROOT_NODE) Root_Node = STATIC_ROOT_NODE->left_son_ptr;
  258. KD_TREE_NODE * update_root = *Rebuild_Ptr;
  259. while (update_root != nullptr && update_root != Root_Node){
  260. update_root = update_root->father_ptr;
  261. if (update_root->working_flag) break;
  262. if (update_root == update_root->father_ptr->left_son_ptr && update_root->father_ptr->need_push_down_to_left) break;
  263. if (update_root == update_root->father_ptr->right_son_ptr && update_root->father_ptr->need_push_down_to_right) break;
  264. Update(update_root);
  265. }
  266. pthread_mutex_lock(&search_flag_mutex);
  267. search_mutex_counter = 0;
  268. pthread_mutex_unlock(&search_flag_mutex);
  269. Rebuild_Ptr = nullptr;
  270. pthread_mutex_unlock(&working_flag_mutex);
  271. rebuild_flag = false;
  272. /* Delete discarded tree nodes */
  273. delete_tree_nodes(&old_root_node);
  274. } else {
  275. pthread_mutex_unlock(&working_flag_mutex);
  276. }
  277. pthread_mutex_unlock(&rebuild_ptr_mutex_lock);
  278. pthread_mutex_lock(&termination_flag_mutex_lock);
  279. terminated = termination_flag;
  280. pthread_mutex_unlock(&termination_flag_mutex_lock);
  281. usleep(100);
  282. }
  283. printf("Rebuild thread terminated normally\n");
  284. }
  285. void KD_TREE::run_operation(KD_TREE_NODE ** root, Operation_Logger_Type operation){
  286. switch (operation.op)
  287. {
  288. case ADD_POINT:
  289. Add_by_point(root, operation.point, false, (*root)->division_axis);
  290. break;
  291. case ADD_BOX:
  292. Add_by_range(root, operation.boxpoint, false);
  293. break;
  294. case DELETE_POINT:
  295. Delete_by_point(root, operation.point, false);
  296. break;
  297. case DELETE_BOX:
  298. Delete_by_range(root, operation.boxpoint, false, false);
  299. break;
  300. case DOWNSAMPLE_DELETE:
  301. Delete_by_range(root, operation.boxpoint, false, true);
  302. break;
  303. case PUSH_DOWN:
  304. (*root)->tree_downsample_deleted |= operation.tree_downsample_deleted;
  305. (*root)->point_downsample_deleted |= operation.tree_downsample_deleted;
  306. (*root)->tree_deleted = operation.tree_deleted || (*root)->tree_downsample_deleted;
  307. (*root)->point_deleted = (*root)->tree_deleted || (*root)->point_downsample_deleted;
  308. if (operation.tree_downsample_deleted) (*root)->down_del_num = (*root)->TreeSize;
  309. if (operation.tree_deleted) (*root)->invalid_point_num = (*root)->TreeSize;
  310. else (*root)->invalid_point_num = (*root)->down_del_num;
  311. (*root)->need_push_down_to_left = true;
  312. (*root)->need_push_down_to_right = true;
  313. break;
  314. default:
  315. break;
  316. }
  317. }
  318. void KD_TREE::Build(PointVector point_cloud){
  319. if (Root_Node != nullptr){
  320. delete_tree_nodes(&Root_Node);
  321. }
  322. if (point_cloud.size() == 0) return;
  323. STATIC_ROOT_NODE = new KD_TREE_NODE;
  324. InitTreeNode(STATIC_ROOT_NODE);
  325. BuildTree(&STATIC_ROOT_NODE->left_son_ptr, 0, point_cloud.size()-1, point_cloud);
  326. Update(STATIC_ROOT_NODE);
  327. STATIC_ROOT_NODE->TreeSize = 0;
  328. Root_Node = STATIC_ROOT_NODE->left_son_ptr;
  329. }
  330. void KD_TREE::Nearest_Search(PointType point, int k_nearest, PointVector& Nearest_Points, vector<float> & Point_Distance, double max_dist){
  331. MANUAL_HEAP q(2*k_nearest);
  332. q.clear();
  333. vector<float> ().swap(Point_Distance);
  334. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != Root_Node){
  335. Search(Root_Node, k_nearest, point, q, max_dist);
  336. } else {
  337. pthread_mutex_lock(&search_flag_mutex);
  338. while (search_mutex_counter == -1)
  339. {
  340. pthread_mutex_unlock(&search_flag_mutex);
  341. usleep(1);
  342. pthread_mutex_lock(&search_flag_mutex);
  343. }
  344. search_mutex_counter += 1;
  345. pthread_mutex_unlock(&search_flag_mutex);
  346. Search(Root_Node, k_nearest, point, q, max_dist);
  347. pthread_mutex_lock(&search_flag_mutex);
  348. search_mutex_counter -= 1;
  349. pthread_mutex_unlock(&search_flag_mutex);
  350. }
  351. int k_found = min(k_nearest,int(q.size()));
  352. PointVector ().swap(Nearest_Points);
  353. vector<float> ().swap(Point_Distance);
  354. for (int i=0;i < k_found;i++){
  355. Nearest_Points.insert(Nearest_Points.begin(), q.top().point);
  356. Point_Distance.insert(Point_Distance.begin(), q.top().dist);
  357. q.pop();
  358. }
  359. return;
  360. }
  361. int KD_TREE::Add_Points(PointVector & PointToAdd, bool downsample_on){
  362. int NewPointSize = PointToAdd.size();
  363. int tree_size = size();
  364. BoxPointType Box_of_Point;
  365. PointType downsample_result, mid_point;
  366. bool downsample_switch = downsample_on && DOWNSAMPLE_SWITCH;
  367. float min_dist, tmp_dist;
  368. int tmp_counter = 0;
  369. for (int i=0; i<PointToAdd.size();i++){
  370. if (downsample_switch){
  371. Box_of_Point.vertex_min[0] = floor(PointToAdd[i].x/downsample_size)*downsample_size;
  372. Box_of_Point.vertex_max[0] = Box_of_Point.vertex_min[0]+downsample_size;
  373. Box_of_Point.vertex_min[1] = floor(PointToAdd[i].y/downsample_size)*downsample_size;
  374. Box_of_Point.vertex_max[1] = Box_of_Point.vertex_min[1]+downsample_size;
  375. Box_of_Point.vertex_min[2] = floor(PointToAdd[i].z/downsample_size)*downsample_size;
  376. Box_of_Point.vertex_max[2] = Box_of_Point.vertex_min[2]+downsample_size;
  377. mid_point.x = Box_of_Point.vertex_min[0] + (Box_of_Point.vertex_max[0]-Box_of_Point.vertex_min[0])/2.0;
  378. mid_point.y = Box_of_Point.vertex_min[1] + (Box_of_Point.vertex_max[1]-Box_of_Point.vertex_min[1])/2.0;
  379. mid_point.z = Box_of_Point.vertex_min[2] + (Box_of_Point.vertex_max[2]-Box_of_Point.vertex_min[2])/2.0;
  380. PointVector ().swap(Downsample_Storage);
  381. Search_by_range(Root_Node, Box_of_Point, Downsample_Storage);
  382. min_dist = calc_dist(PointToAdd[i],mid_point);
  383. downsample_result = PointToAdd[i];
  384. for (int index = 0; index < Downsample_Storage.size(); index++){
  385. tmp_dist = calc_dist(Downsample_Storage[index], mid_point);
  386. if (tmp_dist < min_dist){
  387. min_dist = tmp_dist;
  388. downsample_result = Downsample_Storage[index];
  389. }
  390. }
  391. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != Root_Node){
  392. if (Downsample_Storage.size() > 1 || same_point(PointToAdd[i], downsample_result)){
  393. if (Downsample_Storage.size() > 0) Delete_by_range(&Root_Node, Box_of_Point, true, true);
  394. Add_by_point(&Root_Node, downsample_result, true, Root_Node->division_axis);
  395. tmp_counter ++;
  396. }
  397. } else {
  398. if (Downsample_Storage.size() > 1 || same_point(PointToAdd[i], downsample_result)){
  399. Operation_Logger_Type operation_delete, operation;
  400. operation_delete.boxpoint = Box_of_Point;
  401. operation_delete.op = DOWNSAMPLE_DELETE;
  402. operation.point = downsample_result;
  403. operation.op = ADD_POINT;
  404. pthread_mutex_lock(&working_flag_mutex);
  405. if (Downsample_Storage.size() > 0) Delete_by_range(&Root_Node, Box_of_Point, false , true);
  406. Add_by_point(&Root_Node, downsample_result, false, Root_Node->division_axis);
  407. tmp_counter ++;
  408. if (rebuild_flag){
  409. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  410. if (Downsample_Storage.size() > 0) Rebuild_Logger.push(operation_delete);
  411. Rebuild_Logger.push(operation);
  412. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  413. }
  414. pthread_mutex_unlock(&working_flag_mutex);
  415. };
  416. }
  417. } else {
  418. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != Root_Node){
  419. Add_by_point(&Root_Node, PointToAdd[i], true, Root_Node->division_axis);
  420. } else {
  421. Operation_Logger_Type operation;
  422. operation.point = PointToAdd[i];
  423. operation.op = ADD_POINT;
  424. pthread_mutex_lock(&working_flag_mutex);
  425. Add_by_point(&Root_Node, PointToAdd[i], false, Root_Node->division_axis);
  426. if (rebuild_flag){
  427. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  428. Rebuild_Logger.push(operation);
  429. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  430. }
  431. pthread_mutex_unlock(&working_flag_mutex);
  432. }
  433. }
  434. }
  435. return tmp_counter;
  436. }
  437. void KD_TREE::Add_Point_Boxes(vector<BoxPointType> & BoxPoints){
  438. for (int i=0;i < BoxPoints.size();i++){
  439. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != Root_Node){
  440. Add_by_range(&Root_Node ,BoxPoints[i], true);
  441. } else {
  442. Operation_Logger_Type operation;
  443. operation.boxpoint = BoxPoints[i];
  444. operation.op = ADD_BOX;
  445. pthread_mutex_lock(&working_flag_mutex);
  446. Add_by_range(&Root_Node ,BoxPoints[i], false);
  447. if (rebuild_flag){
  448. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  449. Rebuild_Logger.push(operation);
  450. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  451. }
  452. pthread_mutex_unlock(&working_flag_mutex);
  453. }
  454. }
  455. return;
  456. }
  457. void KD_TREE::Delete_Points(PointVector & PointToDel){
  458. for (int i=0;i<PointToDel.size();i++){
  459. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != Root_Node){
  460. Delete_by_point(&Root_Node, PointToDel[i], true);
  461. } else {
  462. Operation_Logger_Type operation;
  463. operation.point = PointToDel[i];
  464. operation.op = DELETE_POINT;
  465. pthread_mutex_lock(&working_flag_mutex);
  466. Delete_by_point(&Root_Node, PointToDel[i], false);
  467. if (rebuild_flag){
  468. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  469. Rebuild_Logger.push(operation);
  470. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  471. }
  472. pthread_mutex_unlock(&working_flag_mutex);
  473. }
  474. }
  475. return;
  476. }
  477. int KD_TREE::Delete_Point_Boxes(vector<BoxPointType> & BoxPoints){
  478. int tmp_counter = 0;
  479. for (int i=0;i < BoxPoints.size();i++){
  480. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != Root_Node){
  481. tmp_counter += Delete_by_range(&Root_Node ,BoxPoints[i], true, false);
  482. } else {
  483. Operation_Logger_Type operation;
  484. operation.boxpoint = BoxPoints[i];
  485. operation.op = DELETE_BOX;
  486. pthread_mutex_lock(&working_flag_mutex);
  487. tmp_counter += Delete_by_range(&Root_Node ,BoxPoints[i], false, false);
  488. if (rebuild_flag){
  489. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  490. Rebuild_Logger.push(operation);
  491. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  492. }
  493. pthread_mutex_unlock(&working_flag_mutex);
  494. }
  495. }
  496. return tmp_counter;
  497. }
  498. void KD_TREE::acquire_removed_points(PointVector & removed_points){
  499. pthread_mutex_lock(&points_deleted_rebuild_mutex_lock);
  500. for (int i = 0; i < Points_deleted.size();i++){
  501. removed_points.push_back(Points_deleted[i]);
  502. }
  503. for (int i = 0; i < Multithread_Points_deleted.size();i++){
  504. removed_points.push_back(Multithread_Points_deleted[i]);
  505. }
  506. Points_deleted.clear();
  507. Multithread_Points_deleted.clear();
  508. pthread_mutex_unlock(&points_deleted_rebuild_mutex_lock);
  509. return;
  510. }
  511. void KD_TREE::BuildTree(KD_TREE_NODE ** root, int l, int r, PointVector & Storage){
  512. if (l>r) return;
  513. *root = new KD_TREE_NODE;
  514. InitTreeNode(*root);
  515. int mid = (l+r)>>1;
  516. int div_axis = 0;
  517. int i;
  518. // Find the best division Axis
  519. float min_value[3] = {INFINITY, INFINITY, INFINITY};
  520. float max_value[3] = {-INFINITY, -INFINITY, -INFINITY};
  521. float dim_range[3] = {0,0,0};
  522. for (i=l;i<=r;i++){
  523. min_value[0] = min(min_value[0], Storage[i].x);
  524. min_value[1] = min(min_value[1], Storage[i].y);
  525. min_value[2] = min(min_value[2], Storage[i].z);
  526. max_value[0] = max(max_value[0], Storage[i].x);
  527. max_value[1] = max(max_value[1], Storage[i].y);
  528. max_value[2] = max(max_value[2], Storage[i].z);
  529. }
  530. // Select the longest dimension as division axis
  531. for (i=0;i<3;i++) dim_range[i] = max_value[i] - min_value[i];
  532. for (i=1;i<3;i++) if (dim_range[i] > dim_range[div_axis]) div_axis = i;
  533. // Divide by the division axis and recursively build.
  534. (*root)->division_axis = div_axis;
  535. switch (div_axis)
  536. {
  537. case 0:
  538. nth_element(begin(Storage)+l, begin(Storage)+mid, begin(Storage)+r+1, point_cmp_x);
  539. break;
  540. case 1:
  541. nth_element(begin(Storage)+l, begin(Storage)+mid, begin(Storage)+r+1, point_cmp_y);
  542. break;
  543. case 2:
  544. nth_element(begin(Storage)+l, begin(Storage)+mid, begin(Storage)+r+1, point_cmp_z);
  545. break;
  546. default:
  547. nth_element(begin(Storage)+l, begin(Storage)+mid, begin(Storage)+r+1, point_cmp_x);
  548. break;
  549. }
  550. (*root)->point = Storage[mid];
  551. KD_TREE_NODE * left_son = nullptr, * right_son = nullptr;
  552. BuildTree(&left_son, l, mid-1, Storage);
  553. BuildTree(&right_son, mid+1, r, Storage);
  554. (*root)->left_son_ptr = left_son;
  555. (*root)->right_son_ptr = right_son;
  556. Update((*root));
  557. return;
  558. }
  559. void KD_TREE::Rebuild(KD_TREE_NODE ** root){
  560. KD_TREE_NODE * father_ptr;
  561. if ((*root)->TreeSize >= Multi_Thread_Rebuild_Point_Num) {
  562. if (!pthread_mutex_trylock(&rebuild_ptr_mutex_lock)){
  563. if (Rebuild_Ptr == nullptr || ((*root)->TreeSize > (*Rebuild_Ptr)->TreeSize)) {
  564. Rebuild_Ptr = root;
  565. }
  566. pthread_mutex_unlock(&rebuild_ptr_mutex_lock);
  567. }
  568. } else {
  569. father_ptr = (*root)->father_ptr;
  570. int size_rec = (*root)->TreeSize;
  571. PCL_Storage.clear();
  572. flatten(*root, PCL_Storage, DELETE_POINTS_REC);
  573. delete_tree_nodes(root);
  574. BuildTree(root, 0, PCL_Storage.size()-1, PCL_Storage);
  575. if (*root != nullptr) (*root)->father_ptr = father_ptr;
  576. if (*root == Root_Node) STATIC_ROOT_NODE->left_son_ptr = *root;
  577. }
  578. return;
  579. }
  580. int KD_TREE::Delete_by_range(KD_TREE_NODE ** root, BoxPointType boxpoint, bool allow_rebuild, bool is_downsample){
  581. if ((*root) == nullptr || (*root)->tree_deleted) return 0;
  582. (*root)->working_flag = true;
  583. Push_Down(*root);
  584. int tmp_counter = 0;
  585. if (boxpoint.vertex_max[0] <= (*root)->node_range_x[0] || boxpoint.vertex_min[0] > (*root)->node_range_x[1]) return 0;
  586. if (boxpoint.vertex_max[1] <= (*root)->node_range_y[0] || boxpoint.vertex_min[1] > (*root)->node_range_y[1]) return 0;
  587. if (boxpoint.vertex_max[2] <= (*root)->node_range_z[0] || boxpoint.vertex_min[2] > (*root)->node_range_z[1]) return 0;
  588. if (boxpoint.vertex_min[0] <= (*root)->node_range_x[0] && boxpoint.vertex_max[0] > (*root)->node_range_x[1] && boxpoint.vertex_min[1] <= (*root)->node_range_y[0] && boxpoint.vertex_max[1] > (*root)->node_range_y[1] && boxpoint.vertex_min[2] <= (*root)->node_range_z[0] && boxpoint.vertex_max[2] > (*root)->node_range_z[1]){
  589. (*root)->tree_deleted = true;
  590. (*root)->point_deleted = true;
  591. (*root)->need_push_down_to_left = true;
  592. (*root)->need_push_down_to_right = true;
  593. tmp_counter = (*root)->TreeSize - (*root)->invalid_point_num;
  594. (*root)->invalid_point_num = (*root)->TreeSize;
  595. if (is_downsample){
  596. (*root)->tree_downsample_deleted = true;
  597. (*root)->point_downsample_deleted = true;
  598. (*root)->down_del_num = (*root)->TreeSize;
  599. }
  600. return tmp_counter;
  601. }
  602. if (!(*root)->point_deleted && boxpoint.vertex_min[0] <= (*root)->point.x && boxpoint.vertex_max[0] > (*root)->point.x && boxpoint.vertex_min[1] <= (*root)->point.y && boxpoint.vertex_max[1] > (*root)->point.y && boxpoint.vertex_min[2] <= (*root)->point.z && boxpoint.vertex_max[2] > (*root)->point.z){
  603. (*root)->point_deleted = true;
  604. tmp_counter += 1;
  605. if (is_downsample) (*root)->point_downsample_deleted = true;
  606. }
  607. Operation_Logger_Type delete_box_log;
  608. struct timespec Timeout;
  609. if (is_downsample) delete_box_log.op = DOWNSAMPLE_DELETE;
  610. else delete_box_log.op = DELETE_BOX;
  611. delete_box_log.boxpoint = boxpoint;
  612. if ((Rebuild_Ptr == nullptr) || (*root)->left_son_ptr != *Rebuild_Ptr){
  613. tmp_counter += Delete_by_range(&((*root)->left_son_ptr), boxpoint, allow_rebuild, is_downsample);
  614. } else {
  615. pthread_mutex_lock(&working_flag_mutex);
  616. tmp_counter += Delete_by_range(&((*root)->left_son_ptr), boxpoint, false, is_downsample);
  617. if (rebuild_flag){
  618. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  619. Rebuild_Logger.push(delete_box_log);
  620. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  621. }
  622. pthread_mutex_unlock(&working_flag_mutex);
  623. }
  624. if ((Rebuild_Ptr == nullptr) || (*root)->right_son_ptr != *Rebuild_Ptr){
  625. tmp_counter += Delete_by_range(&((*root)->right_son_ptr), boxpoint, allow_rebuild, is_downsample);
  626. } else {
  627. pthread_mutex_lock(&working_flag_mutex);
  628. tmp_counter += Delete_by_range(&((*root)->right_son_ptr), boxpoint, false, is_downsample);
  629. if (rebuild_flag){
  630. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  631. Rebuild_Logger.push(delete_box_log);
  632. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  633. }
  634. pthread_mutex_unlock(&working_flag_mutex);
  635. }
  636. Update(*root);
  637. if (Rebuild_Ptr != nullptr && *Rebuild_Ptr == *root && (*root)->TreeSize < Multi_Thread_Rebuild_Point_Num) Rebuild_Ptr = nullptr;
  638. bool need_rebuild = allow_rebuild & Criterion_Check((*root));
  639. if (need_rebuild) Rebuild(root);
  640. if ((*root) != nullptr) (*root)->working_flag = false;
  641. return tmp_counter;
  642. }
  643. void KD_TREE::Delete_by_point(KD_TREE_NODE ** root, PointType point, bool allow_rebuild){
  644. if ((*root) == nullptr || (*root)->tree_deleted) return;
  645. (*root)->working_flag = true;
  646. Push_Down(*root);
  647. if (same_point((*root)->point, point) && !(*root)->point_deleted) {
  648. (*root)->point_deleted = true;
  649. (*root)->invalid_point_num += 1;
  650. if ((*root)->invalid_point_num == (*root)->TreeSize) (*root)->tree_deleted = true;
  651. return;
  652. }
  653. Operation_Logger_Type delete_log;
  654. struct timespec Timeout;
  655. delete_log.op = DELETE_POINT;
  656. delete_log.point = point;
  657. if (((*root)->division_axis == 0 && point.x < (*root)->point.x) || ((*root)->division_axis == 1 && point.y < (*root)->point.y) || ((*root)->division_axis == 2 && point.z < (*root)->point.z)){
  658. if ((Rebuild_Ptr == nullptr) || (*root)->left_son_ptr != *Rebuild_Ptr){
  659. Delete_by_point(&(*root)->left_son_ptr, point, allow_rebuild);
  660. } else {
  661. pthread_mutex_lock(&working_flag_mutex);
  662. Delete_by_point(&(*root)->left_son_ptr, point,false);
  663. if (rebuild_flag){
  664. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  665. Rebuild_Logger.push(delete_log);
  666. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  667. }
  668. pthread_mutex_unlock(&working_flag_mutex);
  669. }
  670. } else {
  671. if ((Rebuild_Ptr == nullptr) || (*root)->right_son_ptr != *Rebuild_Ptr){
  672. Delete_by_point(&(*root)->right_son_ptr, point, allow_rebuild);
  673. } else {
  674. pthread_mutex_lock(&working_flag_mutex);
  675. Delete_by_point(&(*root)->right_son_ptr, point, false);
  676. if (rebuild_flag){
  677. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  678. Rebuild_Logger.push(delete_log);
  679. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  680. }
  681. pthread_mutex_unlock(&working_flag_mutex);
  682. }
  683. }
  684. Update(*root);
  685. if (Rebuild_Ptr != nullptr && *Rebuild_Ptr == *root && (*root)->TreeSize < Multi_Thread_Rebuild_Point_Num) Rebuild_Ptr = nullptr;
  686. bool need_rebuild = allow_rebuild & Criterion_Check((*root));
  687. if (need_rebuild) Rebuild(root);
  688. if ((*root) != nullptr) (*root)->working_flag = false;
  689. return;
  690. }
  691. void KD_TREE::Add_by_range(KD_TREE_NODE ** root, BoxPointType boxpoint, bool allow_rebuild){
  692. if ((*root) == nullptr) return;
  693. (*root)->working_flag = true;
  694. Push_Down(*root);
  695. if (boxpoint.vertex_max[0] <= (*root)->node_range_x[0] || boxpoint.vertex_min[0] > (*root)->node_range_x[1]) return;
  696. if (boxpoint.vertex_max[1] <= (*root)->node_range_y[0] || boxpoint.vertex_min[1] > (*root)->node_range_y[1]) return;
  697. if (boxpoint.vertex_max[2] <= (*root)->node_range_z[0] || boxpoint.vertex_min[2] > (*root)->node_range_z[1]) return;
  698. if (boxpoint.vertex_min[0] <= (*root)->node_range_x[0] && boxpoint.vertex_max[0] > (*root)->node_range_x[1] && boxpoint.vertex_min[1] <= (*root)->node_range_y[0] && boxpoint.vertex_max[1]> (*root)->node_range_y[1] && boxpoint.vertex_min[2] <= (*root)->node_range_z[0] && boxpoint.vertex_max[2] > (*root)->node_range_z[1]){
  699. (*root)->tree_deleted = false || (*root)->tree_downsample_deleted;
  700. (*root)->point_deleted = false || (*root)->point_downsample_deleted;
  701. (*root)->need_push_down_to_left = true;
  702. (*root)->need_push_down_to_right = true;
  703. (*root)->invalid_point_num = (*root)->down_del_num;
  704. return;
  705. }
  706. if (boxpoint.vertex_min[0] <= (*root)->point.x && boxpoint.vertex_max[0] > (*root)->point.x && boxpoint.vertex_min[1] <= (*root)->point.y && boxpoint.vertex_max[1] > (*root)->point.y && boxpoint.vertex_min[2] <= (*root)->point.z && boxpoint.vertex_max[2] > (*root)->point.z){
  707. (*root)->point_deleted = (*root)->point_downsample_deleted;
  708. }
  709. Operation_Logger_Type add_box_log;
  710. struct timespec Timeout;
  711. add_box_log.op = ADD_BOX;
  712. add_box_log.boxpoint = boxpoint;
  713. if ((Rebuild_Ptr == nullptr) || (*root)->left_son_ptr != *Rebuild_Ptr){
  714. Add_by_range(&((*root)->left_son_ptr), boxpoint, allow_rebuild);
  715. } else {
  716. pthread_mutex_lock(&working_flag_mutex);
  717. Add_by_range(&((*root)->left_son_ptr), boxpoint, false);
  718. if (rebuild_flag){
  719. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  720. Rebuild_Logger.push(add_box_log);
  721. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  722. }
  723. pthread_mutex_unlock(&working_flag_mutex);
  724. }
  725. if ((Rebuild_Ptr == nullptr) || (*root)->right_son_ptr != *Rebuild_Ptr){
  726. Add_by_range(&((*root)->right_son_ptr), boxpoint, allow_rebuild);
  727. } else {
  728. pthread_mutex_lock(&working_flag_mutex);
  729. Add_by_range(&((*root)->right_son_ptr), boxpoint, false);
  730. if (rebuild_flag){
  731. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  732. Rebuild_Logger.push(add_box_log);
  733. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  734. }
  735. pthread_mutex_unlock(&working_flag_mutex);
  736. }
  737. Update(*root);
  738. if (Rebuild_Ptr != nullptr && *Rebuild_Ptr == *root && (*root)->TreeSize < Multi_Thread_Rebuild_Point_Num) Rebuild_Ptr = nullptr;
  739. bool need_rebuild = allow_rebuild & Criterion_Check((*root));
  740. if (need_rebuild) Rebuild(root);
  741. if ((*root) != nullptr) (*root)->working_flag = false;
  742. return;
  743. }
  744. void KD_TREE::Add_by_point(KD_TREE_NODE ** root, PointType point, bool allow_rebuild, int father_axis){
  745. if (*root == nullptr){
  746. *root = new KD_TREE_NODE;
  747. InitTreeNode(*root);
  748. (*root)->point = point;
  749. (*root)->division_axis = (father_axis + 1) % 3;
  750. Update(*root);
  751. return;
  752. }
  753. (*root)->working_flag = true;
  754. Operation_Logger_Type add_log;
  755. struct timespec Timeout;
  756. add_log.op = ADD_POINT;
  757. add_log.point = point;
  758. Push_Down(*root);
  759. if (((*root)->division_axis == 0 && point.x < (*root)->point.x) || ((*root)->division_axis == 1 && point.y < (*root)->point.y) || ((*root)->division_axis == 2 && point.z < (*root)->point.z)){
  760. if ((Rebuild_Ptr == nullptr) || (*root)->left_son_ptr != *Rebuild_Ptr){
  761. Add_by_point(&(*root)->left_son_ptr, point, allow_rebuild, (*root)->division_axis);
  762. } else {
  763. pthread_mutex_lock(&working_flag_mutex);
  764. Add_by_point(&(*root)->left_son_ptr, point, false,(*root)->division_axis);
  765. if (rebuild_flag){
  766. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  767. Rebuild_Logger.push(add_log);
  768. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  769. }
  770. pthread_mutex_unlock(&working_flag_mutex);
  771. }
  772. } else {
  773. if ((Rebuild_Ptr == nullptr) || (*root)->right_son_ptr != *Rebuild_Ptr){
  774. Add_by_point(&(*root)->right_son_ptr, point, allow_rebuild,(*root)->division_axis);
  775. } else {
  776. pthread_mutex_lock(&working_flag_mutex);
  777. Add_by_point(&(*root)->right_son_ptr, point, false,(*root)->division_axis);
  778. if (rebuild_flag){
  779. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  780. Rebuild_Logger.push(add_log);
  781. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  782. }
  783. pthread_mutex_unlock(&working_flag_mutex);
  784. }
  785. }
  786. Update(*root);
  787. if (Rebuild_Ptr != nullptr && *Rebuild_Ptr == *root && (*root)->TreeSize < Multi_Thread_Rebuild_Point_Num) Rebuild_Ptr = nullptr;
  788. bool need_rebuild = allow_rebuild & Criterion_Check((*root));
  789. if (need_rebuild) Rebuild(root);
  790. if ((*root) != nullptr) (*root)->working_flag = false;
  791. return;
  792. }
  793. void KD_TREE::Search(KD_TREE_NODE * root, int k_nearest, PointType point, MANUAL_HEAP &q, double max_dist){
  794. if (root == nullptr || root->tree_deleted) return;
  795. double cur_dist = calc_box_dist(root, point);
  796. if (cur_dist > max_dist) return;
  797. int retval;
  798. if (root->need_push_down_to_left || root->need_push_down_to_right) {
  799. retval = pthread_mutex_trylock(&(root->push_down_mutex_lock));
  800. if (retval == 0){
  801. Push_Down(root);
  802. pthread_mutex_unlock(&(root->push_down_mutex_lock));
  803. } else {
  804. pthread_mutex_lock(&(root->push_down_mutex_lock));
  805. pthread_mutex_unlock(&(root->push_down_mutex_lock));
  806. }
  807. }
  808. if (!root->point_deleted){
  809. float dist = calc_dist(point, root->point);
  810. if (dist <= max_dist && (q.size() < k_nearest || dist < q.top().dist)){
  811. if (q.size() >= k_nearest) q.pop();
  812. PointType_CMP current_point{root->point, dist};
  813. q.push(current_point);
  814. }
  815. }
  816. int cur_search_counter;
  817. float dist_left_node = calc_box_dist(root->left_son_ptr, point);
  818. float dist_right_node = calc_box_dist(root->right_son_ptr, point);
  819. if (q.size()< k_nearest || dist_left_node < q.top().dist && dist_right_node < q.top().dist){
  820. if (dist_left_node <= dist_right_node) {
  821. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != root->left_son_ptr){
  822. Search(root->left_son_ptr, k_nearest, point, q, max_dist);
  823. } else {
  824. pthread_mutex_lock(&search_flag_mutex);
  825. while (search_mutex_counter == -1)
  826. {
  827. pthread_mutex_unlock(&search_flag_mutex);
  828. usleep(1);
  829. pthread_mutex_lock(&search_flag_mutex);
  830. }
  831. search_mutex_counter += 1;
  832. pthread_mutex_unlock(&search_flag_mutex);
  833. Search(root->left_son_ptr, k_nearest, point, q, max_dist);
  834. pthread_mutex_lock(&search_flag_mutex);
  835. search_mutex_counter -= 1;
  836. pthread_mutex_unlock(&search_flag_mutex);
  837. }
  838. if (q.size() < k_nearest || dist_right_node < q.top().dist) {
  839. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != root->right_son_ptr){
  840. Search(root->right_son_ptr, k_nearest, point, q, max_dist);
  841. } else {
  842. pthread_mutex_lock(&search_flag_mutex);
  843. while (search_mutex_counter == -1)
  844. {
  845. pthread_mutex_unlock(&search_flag_mutex);
  846. usleep(1);
  847. pthread_mutex_lock(&search_flag_mutex);
  848. }
  849. search_mutex_counter += 1;
  850. pthread_mutex_unlock(&search_flag_mutex);
  851. Search(root->right_son_ptr, k_nearest, point, q, max_dist);
  852. pthread_mutex_lock(&search_flag_mutex);
  853. search_mutex_counter -= 1;
  854. pthread_mutex_unlock(&search_flag_mutex);
  855. }
  856. }
  857. } else {
  858. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != root->right_son_ptr){
  859. Search(root->right_son_ptr, k_nearest, point, q, max_dist);
  860. } else {
  861. pthread_mutex_lock(&search_flag_mutex);
  862. while (search_mutex_counter == -1)
  863. {
  864. pthread_mutex_unlock(&search_flag_mutex);
  865. usleep(1);
  866. pthread_mutex_lock(&search_flag_mutex);
  867. }
  868. search_mutex_counter += 1;
  869. pthread_mutex_unlock(&search_flag_mutex);
  870. Search(root->right_son_ptr, k_nearest, point, q, max_dist);
  871. pthread_mutex_lock(&search_flag_mutex);
  872. search_mutex_counter -= 1;
  873. pthread_mutex_unlock(&search_flag_mutex);
  874. }
  875. if (q.size() < k_nearest || dist_left_node < q.top().dist) {
  876. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != root->left_son_ptr){
  877. Search(root->left_son_ptr, k_nearest, point, q, max_dist);
  878. } else {
  879. pthread_mutex_lock(&search_flag_mutex);
  880. while (search_mutex_counter == -1)
  881. {
  882. pthread_mutex_unlock(&search_flag_mutex);
  883. usleep(1);
  884. pthread_mutex_lock(&search_flag_mutex);
  885. }
  886. search_mutex_counter += 1;
  887. pthread_mutex_unlock(&search_flag_mutex);
  888. Search(root->left_son_ptr, k_nearest, point, q, max_dist);
  889. pthread_mutex_lock(&search_flag_mutex);
  890. search_mutex_counter -= 1;
  891. pthread_mutex_unlock(&search_flag_mutex);
  892. }
  893. }
  894. }
  895. } else {
  896. if (dist_left_node < q.top().dist) {
  897. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != root->left_son_ptr){
  898. Search(root->left_son_ptr, k_nearest, point, q, max_dist);
  899. } else {
  900. pthread_mutex_lock(&search_flag_mutex);
  901. while (search_mutex_counter == -1)
  902. {
  903. pthread_mutex_unlock(&search_flag_mutex);
  904. usleep(1);
  905. pthread_mutex_lock(&search_flag_mutex);
  906. }
  907. search_mutex_counter += 1;
  908. pthread_mutex_unlock(&search_flag_mutex);
  909. Search(root->left_son_ptr, k_nearest, point, q, max_dist);
  910. pthread_mutex_lock(&search_flag_mutex);
  911. search_mutex_counter -= 1;
  912. pthread_mutex_unlock(&search_flag_mutex);
  913. }
  914. }
  915. if (dist_right_node < q.top().dist) {
  916. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != root->right_son_ptr){
  917. Search(root->right_son_ptr, k_nearest, point, q, max_dist);
  918. } else {
  919. pthread_mutex_lock(&search_flag_mutex);
  920. while (search_mutex_counter == -1)
  921. {
  922. pthread_mutex_unlock(&search_flag_mutex);
  923. usleep(1);
  924. pthread_mutex_lock(&search_flag_mutex);
  925. }
  926. search_mutex_counter += 1;
  927. pthread_mutex_unlock(&search_flag_mutex);
  928. Search(root->right_son_ptr, k_nearest, point, q, max_dist);
  929. pthread_mutex_lock(&search_flag_mutex);
  930. search_mutex_counter -= 1;
  931. pthread_mutex_unlock(&search_flag_mutex);
  932. }
  933. }
  934. }
  935. return;
  936. }
  937. void KD_TREE::Search_by_range(KD_TREE_NODE *root, BoxPointType boxpoint, PointVector & Storage){
  938. if (root == nullptr) return;
  939. Push_Down(root);
  940. if (boxpoint.vertex_max[0] <= root->node_range_x[0] || boxpoint.vertex_min[0] > root->node_range_x[1]) return;
  941. if (boxpoint.vertex_max[1] <= root->node_range_y[0] || boxpoint.vertex_min[1] > root->node_range_y[1]) return;
  942. if (boxpoint.vertex_max[2] <= root->node_range_z[0] || boxpoint.vertex_min[2] > root->node_range_z[1]) return;
  943. if (boxpoint.vertex_min[0] <= root->node_range_x[0] && boxpoint.vertex_max[0] > root->node_range_x[1] && boxpoint.vertex_min[1] <= root->node_range_y[0] && boxpoint.vertex_max[1] > root->node_range_y[1] && boxpoint.vertex_min[2] <= root->node_range_z[0] && boxpoint.vertex_max[2] > root->node_range_z[1]){
  944. flatten(root, Storage, NOT_RECORD);
  945. return;
  946. }
  947. if (boxpoint.vertex_min[0] <= root->point.x && boxpoint.vertex_max[0] > root->point.x && boxpoint.vertex_min[1] <= root->point.y && boxpoint.vertex_max[1] > root->point.y && boxpoint.vertex_min[2] <= root->point.z && boxpoint.vertex_max[2] > root->point.z){
  948. if (!root->point_deleted) Storage.push_back(root->point);
  949. }
  950. if ((Rebuild_Ptr == nullptr) || root->left_son_ptr != *Rebuild_Ptr){
  951. Search_by_range(root->left_son_ptr, boxpoint, Storage);
  952. } else {
  953. pthread_mutex_lock(&search_flag_mutex);
  954. Search_by_range(root->left_son_ptr, boxpoint, Storage);
  955. pthread_mutex_unlock(&search_flag_mutex);
  956. }
  957. if ((Rebuild_Ptr == nullptr) || root->right_son_ptr != *Rebuild_Ptr){
  958. Search_by_range(root->right_son_ptr, boxpoint, Storage);
  959. } else {
  960. pthread_mutex_lock(&search_flag_mutex);
  961. Search_by_range(root->right_son_ptr, boxpoint, Storage);
  962. pthread_mutex_unlock(&search_flag_mutex);
  963. }
  964. return;
  965. }
  966. bool KD_TREE::Criterion_Check(KD_TREE_NODE * root){
  967. if (root->TreeSize <= Minimal_Unbalanced_Tree_Size){
  968. return false;
  969. }
  970. float balance_evaluation = 0.0f;
  971. float delete_evaluation = 0.0f;
  972. KD_TREE_NODE * son_ptr = root->left_son_ptr;
  973. if (son_ptr == nullptr) son_ptr = root->right_son_ptr;
  974. delete_evaluation = float(root->invalid_point_num)/ root->TreeSize;
  975. balance_evaluation = float(son_ptr->TreeSize) / (root->TreeSize-1);
  976. if (delete_evaluation > delete_criterion_param){
  977. return true;
  978. }
  979. if (balance_evaluation > balance_criterion_param || balance_evaluation < 1-balance_criterion_param){
  980. return true;
  981. }
  982. return false;
  983. }
  984. void KD_TREE::Push_Down(KD_TREE_NODE *root){
  985. if (root == nullptr) return;
  986. Operation_Logger_Type operation;
  987. operation.op = PUSH_DOWN;
  988. operation.tree_deleted = root->tree_deleted;
  989. operation.tree_downsample_deleted = root->tree_downsample_deleted;
  990. if (root->need_push_down_to_left && root->left_son_ptr != nullptr){
  991. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != root->left_son_ptr){
  992. root->left_son_ptr->tree_downsample_deleted |= root->tree_downsample_deleted;
  993. root->left_son_ptr->point_downsample_deleted |= root->tree_downsample_deleted;
  994. root->left_son_ptr->tree_deleted = root->tree_deleted || root->left_son_ptr->tree_downsample_deleted;
  995. root->left_son_ptr->point_deleted = root->left_son_ptr->tree_deleted || root->left_son_ptr->point_downsample_deleted;
  996. if (root->tree_downsample_deleted) root->left_son_ptr->down_del_num = root->left_son_ptr->TreeSize;
  997. if (root->tree_deleted) root->left_son_ptr->invalid_point_num = root->left_son_ptr->TreeSize;
  998. else root->left_son_ptr->invalid_point_num = root->left_son_ptr->down_del_num;
  999. root->left_son_ptr->need_push_down_to_left = true;
  1000. root->left_son_ptr->need_push_down_to_right = true;
  1001. root->need_push_down_to_left = false;
  1002. } else {
  1003. pthread_mutex_lock(&working_flag_mutex);
  1004. root->left_son_ptr->tree_downsample_deleted |= root->tree_downsample_deleted;
  1005. root->left_son_ptr->point_downsample_deleted |= root->tree_downsample_deleted;
  1006. root->left_son_ptr->tree_deleted = root->tree_deleted || root->left_son_ptr->tree_downsample_deleted;
  1007. root->left_son_ptr->point_deleted = root->left_son_ptr->tree_deleted || root->left_son_ptr->point_downsample_deleted;
  1008. if (root->tree_downsample_deleted) root->left_son_ptr->down_del_num = root->left_son_ptr->TreeSize;
  1009. if (root->tree_deleted) root->left_son_ptr->invalid_point_num = root->left_son_ptr->TreeSize;
  1010. else root->left_son_ptr->invalid_point_num = root->left_son_ptr->down_del_num;
  1011. root->left_son_ptr->need_push_down_to_left = true;
  1012. root->left_son_ptr->need_push_down_to_right = true;
  1013. if (rebuild_flag){
  1014. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  1015. Rebuild_Logger.push(operation);
  1016. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  1017. }
  1018. root->need_push_down_to_left = false;
  1019. pthread_mutex_unlock(&working_flag_mutex);
  1020. }
  1021. }
  1022. if (root->need_push_down_to_right && root->right_son_ptr != nullptr){
  1023. if (Rebuild_Ptr == nullptr || *Rebuild_Ptr != root->right_son_ptr){
  1024. root->right_son_ptr->tree_downsample_deleted |= root->tree_downsample_deleted;
  1025. root->right_son_ptr->point_downsample_deleted |= root->tree_downsample_deleted;
  1026. root->right_son_ptr->tree_deleted = root->tree_deleted || root->right_son_ptr->tree_downsample_deleted;
  1027. root->right_son_ptr->point_deleted = root->right_son_ptr->tree_deleted || root->right_son_ptr->point_downsample_deleted;
  1028. if (root->tree_downsample_deleted) root->right_son_ptr->down_del_num = root->right_son_ptr->TreeSize;
  1029. if (root->tree_deleted) root->right_son_ptr->invalid_point_num = root->right_son_ptr->TreeSize;
  1030. else root->right_son_ptr->invalid_point_num = root->right_son_ptr->down_del_num;
  1031. root->right_son_ptr->need_push_down_to_left = true;
  1032. root->right_son_ptr->need_push_down_to_right = true;
  1033. root->need_push_down_to_right = false;
  1034. } else {
  1035. pthread_mutex_lock(&working_flag_mutex);
  1036. root->right_son_ptr->tree_downsample_deleted |= root->tree_downsample_deleted;
  1037. root->right_son_ptr->point_downsample_deleted |= root->tree_downsample_deleted;
  1038. root->right_son_ptr->tree_deleted = root->tree_deleted || root->right_son_ptr->tree_downsample_deleted;
  1039. root->right_son_ptr->point_deleted = root->right_son_ptr->tree_deleted || root->right_son_ptr->point_downsample_deleted;
  1040. if (root->tree_downsample_deleted) root->right_son_ptr->down_del_num = root->right_son_ptr->TreeSize;
  1041. if (root->tree_deleted) root->right_son_ptr->invalid_point_num = root->right_son_ptr->TreeSize;
  1042. else root->right_son_ptr->invalid_point_num = root->right_son_ptr->down_del_num;
  1043. root->right_son_ptr->need_push_down_to_left = true;
  1044. root->right_son_ptr->need_push_down_to_right = true;
  1045. if (rebuild_flag){
  1046. pthread_mutex_lock(&rebuild_logger_mutex_lock);
  1047. Rebuild_Logger.push(operation);
  1048. pthread_mutex_unlock(&rebuild_logger_mutex_lock);
  1049. }
  1050. root->need_push_down_to_right = false;
  1051. pthread_mutex_unlock(&working_flag_mutex);
  1052. }
  1053. }
  1054. return;
  1055. }
  1056. void KD_TREE::Update(KD_TREE_NODE * root){
  1057. KD_TREE_NODE * left_son_ptr = root->left_son_ptr;
  1058. KD_TREE_NODE * right_son_ptr = root->right_son_ptr;
  1059. float tmp_range_x[2] = {INFINITY, -INFINITY};
  1060. float tmp_range_y[2] = {INFINITY, -INFINITY};
  1061. float tmp_range_z[2] = {INFINITY, -INFINITY};
  1062. // Update Tree Size
  1063. if (left_son_ptr != nullptr && right_son_ptr != nullptr){
  1064. root->TreeSize = left_son_ptr->TreeSize + right_son_ptr->TreeSize + 1;
  1065. root->invalid_point_num = left_son_ptr->invalid_point_num + right_son_ptr->invalid_point_num + (root->point_deleted? 1:0);
  1066. root->down_del_num = left_son_ptr->down_del_num + right_son_ptr->down_del_num + (root->point_downsample_deleted? 1:0);
  1067. root->tree_downsample_deleted = left_son_ptr->tree_downsample_deleted & right_son_ptr->tree_downsample_deleted & root->point_downsample_deleted;
  1068. root->tree_deleted = left_son_ptr->tree_deleted && right_son_ptr->tree_deleted && root->point_deleted;
  1069. if (root->tree_deleted || (!left_son_ptr->tree_deleted && !right_son_ptr->tree_deleted && !root->point_deleted)){
  1070. tmp_range_x[0] = min(min(left_son_ptr->node_range_x[0],right_son_ptr->node_range_x[0]),root->point.x);
  1071. tmp_range_x[1] = max(max(left_son_ptr->node_range_x[1],right_son_ptr->node_range_x[1]),root->point.x);
  1072. tmp_range_y[0] = min(min(left_son_ptr->node_range_y[0],right_son_ptr->node_range_y[0]),root->point.y);
  1073. tmp_range_y[1] = max(max(left_son_ptr->node_range_y[1],right_son_ptr->node_range_y[1]),root->point.y);
  1074. tmp_range_z[0] = min(min(left_son_ptr->node_range_z[0],right_son_ptr->node_range_z[0]),root->point.z);
  1075. tmp_range_z[1] = max(max(left_son_ptr->node_range_z[1],right_son_ptr->node_range_z[1]),root->point.z);
  1076. } else {
  1077. if (!left_son_ptr->tree_deleted){
  1078. tmp_range_x[0] = min(tmp_range_x[0], left_son_ptr->node_range_x[0]);
  1079. tmp_range_x[1] = max(tmp_range_x[1], left_son_ptr->node_range_x[1]);
  1080. tmp_range_y[0] = min(tmp_range_y[0], left_son_ptr->node_range_y[0]);
  1081. tmp_range_y[1] = max(tmp_range_y[1], left_son_ptr->node_range_y[1]);
  1082. tmp_range_z[0] = min(tmp_range_z[0], left_son_ptr->node_range_z[0]);
  1083. tmp_range_z[1] = max(tmp_range_z[1], left_son_ptr->node_range_z[1]);
  1084. }
  1085. if (!right_son_ptr->tree_deleted){
  1086. tmp_range_x[0] = min(tmp_range_x[0], right_son_ptr->node_range_x[0]);
  1087. tmp_range_x[1] = max(tmp_range_x[1], right_son_ptr->node_range_x[1]);
  1088. tmp_range_y[0] = min(tmp_range_y[0], right_son_ptr->node_range_y[0]);
  1089. tmp_range_y[1] = max(tmp_range_y[1], right_son_ptr->node_range_y[1]);
  1090. tmp_range_z[0] = min(tmp_range_z[0], right_son_ptr->node_range_z[0]);
  1091. tmp_range_z[1] = max(tmp_range_z[1], right_son_ptr->node_range_z[1]);
  1092. }
  1093. if (!root->point_deleted){
  1094. tmp_range_x[0] = min(tmp_range_x[0], root->point.x);
  1095. tmp_range_x[1] = max(tmp_range_x[1], root->point.x);
  1096. tmp_range_y[0] = min(tmp_range_y[0], root->point.y);
  1097. tmp_range_y[1] = max(tmp_range_y[1], root->point.y);
  1098. tmp_range_z[0] = min(tmp_range_z[0], root->point.z);
  1099. tmp_range_z[1] = max(tmp_range_z[1], root->point.z);
  1100. }
  1101. }
  1102. } else if (left_son_ptr != nullptr){
  1103. root->TreeSize = left_son_ptr->TreeSize + 1;
  1104. root->invalid_point_num = left_son_ptr->invalid_point_num + (root->point_deleted?1:0);
  1105. root->down_del_num = left_son_ptr->down_del_num + (root->point_downsample_deleted?1:0);
  1106. root->tree_downsample_deleted = left_son_ptr->tree_downsample_deleted & root->point_downsample_deleted;
  1107. root->tree_deleted = left_son_ptr->tree_deleted && root->point_deleted;
  1108. if (root->tree_deleted || (!left_son_ptr->tree_deleted && !root->point_deleted)){
  1109. tmp_range_x[0] = min(left_son_ptr->node_range_x[0],root->point.x);
  1110. tmp_range_x[1] = max(left_son_ptr->node_range_x[1],root->point.x);
  1111. tmp_range_y[0] = min(left_son_ptr->node_range_y[0],root->point.y);
  1112. tmp_range_y[1] = max(left_son_ptr->node_range_y[1],root->point.y);
  1113. tmp_range_z[0] = min(left_son_ptr->node_range_z[0],root->point.z);
  1114. tmp_range_z[1] = max(left_son_ptr->node_range_z[1],root->point.z);
  1115. } else {
  1116. if (!left_son_ptr->tree_deleted){
  1117. tmp_range_x[0] = min(tmp_range_x[0], left_son_ptr->node_range_x[0]);
  1118. tmp_range_x[1] = max(tmp_range_x[1], left_son_ptr->node_range_x[1]);
  1119. tmp_range_y[0] = min(tmp_range_y[0], left_son_ptr->node_range_y[0]);
  1120. tmp_range_y[1] = max(tmp_range_y[1], left_son_ptr->node_range_y[1]);
  1121. tmp_range_z[0] = min(tmp_range_z[0], left_son_ptr->node_range_z[0]);
  1122. tmp_range_z[1] = max(tmp_range_z[1], left_son_ptr->node_range_z[1]);
  1123. }
  1124. if (!root->point_deleted){
  1125. tmp_range_x[0] = min(tmp_range_x[0], root->point.x);
  1126. tmp_range_x[1] = max(tmp_range_x[1], root->point.x);
  1127. tmp_range_y[0] = min(tmp_range_y[0], root->point.y);
  1128. tmp_range_y[1] = max(tmp_range_y[1], root->point.y);
  1129. tmp_range_z[0] = min(tmp_range_z[0], root->point.z);
  1130. tmp_range_z[1] = max(tmp_range_z[1], root->point.z);
  1131. }
  1132. }
  1133. } else if (right_son_ptr != nullptr){
  1134. root->TreeSize = right_son_ptr->TreeSize + 1;
  1135. root->invalid_point_num = right_son_ptr->invalid_point_num + (root->point_deleted? 1:0);
  1136. root->down_del_num = right_son_ptr->down_del_num + (root->point_downsample_deleted? 1:0);
  1137. root->tree_downsample_deleted = right_son_ptr->tree_downsample_deleted & root->point_downsample_deleted;
  1138. root->tree_deleted = right_son_ptr->tree_deleted && root->point_deleted;
  1139. if (root->tree_deleted || (!right_son_ptr->tree_deleted && !root->point_deleted)){
  1140. tmp_range_x[0] = min(right_son_ptr->node_range_x[0],root->point.x);
  1141. tmp_range_x[1] = max(right_son_ptr->node_range_x[1],root->point.x);
  1142. tmp_range_y[0] = min(right_son_ptr->node_range_y[0],root->point.y);
  1143. tmp_range_y[1] = max(right_son_ptr->node_range_y[1],root->point.y);
  1144. tmp_range_z[0] = min(right_son_ptr->node_range_z[0],root->point.z);
  1145. tmp_range_z[1] = max(right_son_ptr->node_range_z[1],root->point.z);
  1146. } else {
  1147. if (!right_son_ptr->tree_deleted){
  1148. tmp_range_x[0] = min(tmp_range_x[0], right_son_ptr->node_range_x[0]);
  1149. tmp_range_x[1] = max(tmp_range_x[1], right_son_ptr->node_range_x[1]);
  1150. tmp_range_y[0] = min(tmp_range_y[0], right_son_ptr->node_range_y[0]);
  1151. tmp_range_y[1] = max(tmp_range_y[1], right_son_ptr->node_range_y[1]);
  1152. tmp_range_z[0] = min(tmp_range_z[0], right_son_ptr->node_range_z[0]);
  1153. tmp_range_z[1] = max(tmp_range_z[1], right_son_ptr->node_range_z[1]);
  1154. }
  1155. if (!root->point_deleted){
  1156. tmp_range_x[0] = min(tmp_range_x[0], root->point.x);
  1157. tmp_range_x[1] = max(tmp_range_x[1], root->point.x);
  1158. tmp_range_y[0] = min(tmp_range_y[0], root->point.y);
  1159. tmp_range_y[1] = max(tmp_range_y[1], root->point.y);
  1160. tmp_range_z[0] = min(tmp_range_z[0], root->point.z);
  1161. tmp_range_z[1] = max(tmp_range_z[1], root->point.z);
  1162. }
  1163. }
  1164. } else {
  1165. root->TreeSize = 1;
  1166. root->invalid_point_num = (root->point_deleted? 1:0);
  1167. root->down_del_num = (root->point_downsample_deleted? 1:0);
  1168. root->tree_downsample_deleted = root->point_downsample_deleted;
  1169. root->tree_deleted = root->point_deleted;
  1170. tmp_range_x[0] = root->point.x;
  1171. tmp_range_x[1] = root->point.x;
  1172. tmp_range_y[0] = root->point.y;
  1173. tmp_range_y[1] = root->point.y;
  1174. tmp_range_z[0] = root->point.z;
  1175. tmp_range_z[1] = root->point.z;
  1176. }
  1177. memcpy(root->node_range_x,tmp_range_x,sizeof(tmp_range_x));
  1178. memcpy(root->node_range_y,tmp_range_y,sizeof(tmp_range_y));
  1179. memcpy(root->node_range_z,tmp_range_z,sizeof(tmp_range_z));
  1180. if (left_son_ptr != nullptr) left_son_ptr -> father_ptr = root;
  1181. if (right_son_ptr != nullptr) right_son_ptr -> father_ptr = root;
  1182. if (root == Root_Node && root->TreeSize > 3){
  1183. KD_TREE_NODE * son_ptr = root->left_son_ptr;
  1184. if (son_ptr == nullptr) son_ptr = root->right_son_ptr;
  1185. float tmp_bal = float(son_ptr->TreeSize) / (root->TreeSize-1);
  1186. root->alpha_del = float(root->invalid_point_num)/ root->TreeSize;
  1187. root->alpha_bal = (tmp_bal>=0.5-EPSS)?tmp_bal:1-tmp_bal;
  1188. }
  1189. return;
  1190. }
  1191. void KD_TREE::flatten(KD_TREE_NODE * root, PointVector &Storage, delete_point_storage_set storage_type){
  1192. if (root == nullptr) return;
  1193. Push_Down(root);
  1194. if (!root->point_deleted) {
  1195. Storage.push_back(root->point);
  1196. }
  1197. flatten(root->left_son_ptr, Storage, storage_type);
  1198. flatten(root->right_son_ptr, Storage, storage_type);
  1199. switch (storage_type)
  1200. {
  1201. case NOT_RECORD:
  1202. break;
  1203. case DELETE_POINTS_REC:
  1204. if (root->point_deleted && !root->point_downsample_deleted) {
  1205. Points_deleted.push_back(root->point);
  1206. }
  1207. break;
  1208. case MULTI_THREAD_REC:
  1209. if (root->point_deleted && !root->point_downsample_deleted) {
  1210. Multithread_Points_deleted.push_back(root->point);
  1211. }
  1212. break;
  1213. default:
  1214. break;
  1215. }
  1216. return;
  1217. }
  1218. void KD_TREE::delete_tree_nodes(KD_TREE_NODE ** root){
  1219. if (*root == nullptr) return;
  1220. Push_Down(*root);
  1221. delete_tree_nodes(&(*root)->left_son_ptr);
  1222. delete_tree_nodes(&(*root)->right_son_ptr);
  1223. delete *root;
  1224. *root = nullptr;
  1225. return;
  1226. }
  1227. bool KD_TREE::same_point(PointType a, PointType b){
  1228. return (fabs(a.x-b.x) < EPSS && fabs(a.y-b.y) < EPSS && fabs(a.z-b.z) < EPSS );
  1229. }
  1230. float KD_TREE::calc_dist(PointType a, PointType b){
  1231. float dist = 0.0f;
  1232. dist = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + (a.z-b.z)*(a.z-b.z);
  1233. return dist;
  1234. }
  1235. float KD_TREE::calc_box_dist(KD_TREE_NODE * node, PointType point){
  1236. if (node == nullptr) return INFINITY;
  1237. float min_dist = 0.0;
  1238. if (point.x < node->node_range_x[0]) min_dist += (point.x - node->node_range_x[0])*(point.x - node->node_range_x[0]);
  1239. if (point.x > node->node_range_x[1]) min_dist += (point.x - node->node_range_x[1])*(point.x - node->node_range_x[1]);
  1240. if (point.y < node->node_range_y[0]) min_dist += (point.y - node->node_range_y[0])*(point.y - node->node_range_y[0]);
  1241. if (point.y > node->node_range_y[1]) min_dist += (point.y - node->node_range_y[1])*(point.y - node->node_range_y[1]);
  1242. if (point.z < node->node_range_z[0]) min_dist += (point.z - node->node_range_z[0])*(point.z - node->node_range_z[0]);
  1243. if (point.z > node->node_range_z[1]) min_dist += (point.z - node->node_range_z[1])*(point.z - node->node_range_z[1]);
  1244. return min_dist;
  1245. }
  1246. bool KD_TREE::point_cmp_x(PointType a, PointType b) { return a.x < b.x;}
  1247. bool KD_TREE::point_cmp_y(PointType a, PointType b) { return a.y < b.y;}
  1248. bool KD_TREE::point_cmp_z(PointType a, PointType b) { return a.z < b.z;}
  1249. /**
  1250. * @brief 删除当前所有的点云缓存,根据输入的点云,重新构造ikdtree
  1251. * @param point_cloud 输入的点云
  1252. */
  1253. void KD_TREE::reconstruct(PointVector point_cloud){
  1254. Delete_Storage_Disabled = true;
  1255. delete_tree_nodes(&Root_Node);
  1256. PointVector ().swap(PCL_Storage);
  1257. Rebuild_Logger.clear();
  1258. if(Root_Node == nullptr){
  1259. Build(point_cloud);
  1260. } else {
  1261. Add_Points(point_cloud, true);
  1262. }
  1263. }
  1264. // Manual heap
  1265. MANUAL_HEAP::MANUAL_HEAP(int max_capacity){
  1266. cap = max_capacity;
  1267. heap = new PointType_CMP[max_capacity];
  1268. heap_size = 0;
  1269. }
  1270. MANUAL_HEAP::~MANUAL_HEAP(){
  1271. delete[] heap;
  1272. }
  1273. void MANUAL_HEAP::pop(){
  1274. if (heap_size == 0) return;
  1275. heap[0] = heap[heap_size-1];
  1276. heap_size--;
  1277. MoveDown(0);
  1278. return;
  1279. }
  1280. PointType_CMP MANUAL_HEAP::top(){
  1281. return heap[0];
  1282. }
  1283. void MANUAL_HEAP::push(PointType_CMP point){
  1284. if (heap_size >= cap) return;
  1285. heap[heap_size] = point;
  1286. FloatUp(heap_size);
  1287. heap_size++;
  1288. return;
  1289. }
  1290. int MANUAL_HEAP::size(){
  1291. return heap_size;
  1292. }
  1293. void MANUAL_HEAP::clear(){
  1294. heap_size = 0;
  1295. return;
  1296. }
  1297. void MANUAL_HEAP::MoveDown(int heap_index){
  1298. int l = heap_index * 2 + 1;
  1299. PointType_CMP tmp = heap[heap_index];
  1300. while (l < heap_size){
  1301. if (l + 1 < heap_size && heap[l] < heap[l+1]) l++;
  1302. if (tmp < heap[l]){
  1303. heap[heap_index] = heap[l];
  1304. heap_index = l;
  1305. l = heap_index * 2 + 1;
  1306. } else break;
  1307. }
  1308. heap[heap_index] = tmp;
  1309. return;
  1310. }
  1311. void MANUAL_HEAP::FloatUp(int heap_index){
  1312. int ancestor = (heap_index-1)/2;
  1313. PointType_CMP tmp = heap[heap_index];
  1314. while (heap_index > 0){
  1315. if (heap[ancestor] < tmp){
  1316. heap[heap_index] = heap[ancestor];
  1317. heap_index = ancestor;
  1318. ancestor = (heap_index-1)/2;
  1319. } else break;
  1320. }
  1321. heap[heap_index] = tmp;
  1322. return;
  1323. }
  1324. // manual queue
  1325. void MANUAL_Q::clear(){
  1326. head = 0;
  1327. tail = 0;
  1328. counter = 0;
  1329. is_empty = true;
  1330. return;
  1331. }
  1332. void MANUAL_Q::pop(){
  1333. if (counter == 0) return;
  1334. head ++;
  1335. head %= Q_LEN;
  1336. counter --;
  1337. if (counter == 0) is_empty = true;
  1338. return;
  1339. }
  1340. Operation_Logger_Type MANUAL_Q::front(){
  1341. return q[head];
  1342. }
  1343. Operation_Logger_Type MANUAL_Q::back(){
  1344. return q[tail];
  1345. }
  1346. void MANUAL_Q::push(Operation_Logger_Type op){
  1347. q[tail] = op;
  1348. counter ++;
  1349. if (is_empty) is_empty = false;
  1350. tail ++;
  1351. tail %= Q_LEN;
  1352. }
  1353. bool MANUAL_Q::empty(){
  1354. return is_empty;
  1355. }
  1356. int MANUAL_Q::size(){
  1357. return counter;
  1358. }