ikd_Tree.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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. // Manual heap
  1250. MANUAL_HEAP::MANUAL_HEAP(int max_capacity){
  1251. cap = max_capacity;
  1252. heap = new PointType_CMP[max_capacity];
  1253. heap_size = 0;
  1254. }
  1255. MANUAL_HEAP::~MANUAL_HEAP(){
  1256. delete[] heap;
  1257. }
  1258. void MANUAL_HEAP::pop(){
  1259. if (heap_size == 0) return;
  1260. heap[0] = heap[heap_size-1];
  1261. heap_size--;
  1262. MoveDown(0);
  1263. return;
  1264. }
  1265. PointType_CMP MANUAL_HEAP::top(){
  1266. return heap[0];
  1267. }
  1268. void MANUAL_HEAP::push(PointType_CMP point){
  1269. if (heap_size >= cap) return;
  1270. heap[heap_size] = point;
  1271. FloatUp(heap_size);
  1272. heap_size++;
  1273. return;
  1274. }
  1275. int MANUAL_HEAP::size(){
  1276. return heap_size;
  1277. }
  1278. void MANUAL_HEAP::clear(){
  1279. heap_size = 0;
  1280. return;
  1281. }
  1282. void MANUAL_HEAP::MoveDown(int heap_index){
  1283. int l = heap_index * 2 + 1;
  1284. PointType_CMP tmp = heap[heap_index];
  1285. while (l < heap_size){
  1286. if (l + 1 < heap_size && heap[l] < heap[l+1]) l++;
  1287. if (tmp < heap[l]){
  1288. heap[heap_index] = heap[l];
  1289. heap_index = l;
  1290. l = heap_index * 2 + 1;
  1291. } else break;
  1292. }
  1293. heap[heap_index] = tmp;
  1294. return;
  1295. }
  1296. void MANUAL_HEAP::FloatUp(int heap_index){
  1297. int ancestor = (heap_index-1)/2;
  1298. PointType_CMP tmp = heap[heap_index];
  1299. while (heap_index > 0){
  1300. if (heap[ancestor] < tmp){
  1301. heap[heap_index] = heap[ancestor];
  1302. heap_index = ancestor;
  1303. ancestor = (heap_index-1)/2;
  1304. } else break;
  1305. }
  1306. heap[heap_index] = tmp;
  1307. return;
  1308. }
  1309. // manual queue
  1310. void MANUAL_Q::clear(){
  1311. head = 0;
  1312. tail = 0;
  1313. counter = 0;
  1314. is_empty = true;
  1315. return;
  1316. }
  1317. void MANUAL_Q::pop(){
  1318. if (counter == 0) return;
  1319. head ++;
  1320. head %= Q_LEN;
  1321. counter --;
  1322. if (counter == 0) is_empty = true;
  1323. return;
  1324. }
  1325. Operation_Logger_Type MANUAL_Q::front(){
  1326. return q[head];
  1327. }
  1328. Operation_Logger_Type MANUAL_Q::back(){
  1329. return q[tail];
  1330. }
  1331. void MANUAL_Q::push(Operation_Logger_Type op){
  1332. q[tail] = op;
  1333. counter ++;
  1334. if (is_empty) is_empty = false;
  1335. tail ++;
  1336. tail %= Q_LEN;
  1337. }
  1338. bool MANUAL_Q::empty(){
  1339. return is_empty;
  1340. }
  1341. int MANUAL_Q::size(){
  1342. return counter;
  1343. }