octree.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "octree.h"
  2. #include <vector>
  3. #include <queue>
  4. namespace igl {
  5. template <typename DerivedP, typename IndexType, typename DerivedCH,
  6. typename DerivedCN, typename DerivedW>
  7. IGL_INLINE void octree(const Eigen::MatrixBase<DerivedP>& P,
  8. std::vector<std::vector<IndexType> > & point_indices,
  9. Eigen::PlainObjectBase<DerivedCH>& CH,
  10. Eigen::PlainObjectBase<DerivedCN>& CN,
  11. Eigen::PlainObjectBase<DerivedW>& W)
  12. {
  13. const int MAX_DEPTH = 30000;
  14. typedef typename DerivedCH::Scalar ChildrenType;
  15. typedef typename DerivedCN::Scalar CentersType;
  16. typedef typename DerivedW::Scalar WidthsType;
  17. typedef Eigen::Matrix<ChildrenType,8,1> Vector8i;
  18. typedef Eigen::Matrix<typename DerivedP::Scalar, 1, 3> RowVector3PType;
  19. typedef Eigen::Matrix<CentersType, 1, 3> RowVector3CentersType;
  20. std::vector<Eigen::Matrix<ChildrenType,8,1>,
  21. Eigen::aligned_allocator<Eigen::Matrix<ChildrenType,8,1> > > children;
  22. std::vector<Eigen::Matrix<CentersType,1,3>,
  23. Eigen::aligned_allocator<Eigen::Matrix<CentersType,1,3> > > centers;
  24. std::vector<WidthsType> widths;
  25. auto get_octant = [](RowVector3PType location,
  26. RowVector3CentersType center){
  27. // We use a binary numbering of children. Treating the parent cell's
  28. // center as the origin, we number the octants in the following manner:
  29. // The first bit is 1 iff the octant's x coordinate is positive
  30. // The second bit is 1 iff the octant's y coordinate is positive
  31. // The third bit is 1 iff the octant's z coordinate is positive
  32. //
  33. // For example, the octant with negative x, positive y, positive z is:
  34. // 110 binary = 6 decimal
  35. IndexType index = 0;
  36. if( location(0) >= center(0)){
  37. index = index + 1;
  38. }
  39. if( location(1) >= center(1)){
  40. index = index + 2;
  41. }
  42. if( location(2) >= center(2)){
  43. index = index + 4;
  44. }
  45. return index;
  46. };
  47. std::function< RowVector3CentersType(const RowVector3CentersType,
  48. const CentersType,
  49. const ChildrenType) >
  50. translate_center =
  51. [](const RowVector3CentersType & parent_center,
  52. const CentersType h,
  53. const ChildrenType child_index){
  54. RowVector3CentersType change_vector;
  55. change_vector << -h,-h,-h;
  56. //positive x chilren are 1,3,4,7
  57. if(child_index % 2){
  58. change_vector(0) = h;
  59. }
  60. //positive y children are 2,3,6,7
  61. if(child_index == 2 || child_index == 3 ||
  62. child_index == 6 || child_index == 7){
  63. change_vector(1) = h;
  64. }
  65. //positive z children are 4,5,6,7
  66. if(child_index > 3){
  67. change_vector(2) = h;
  68. }
  69. RowVector3CentersType output = parent_center + change_vector;
  70. return output;
  71. };
  72. // How many cells do we have so far?
  73. IndexType m = 0;
  74. // Useful list of number 0..7
  75. const Vector8i zero_to_seven = (Vector8i()<<0,1,2,3,4,5,6,7).finished();
  76. const Vector8i neg_ones = (Vector8i()<<-1,-1,-1,-1,-1,-1,-1,-1).finished();
  77. std::function< void(const ChildrenType, const int) > helper;
  78. helper = [&helper,&translate_center,&get_octant,&m,
  79. &zero_to_seven,&neg_ones,&P,
  80. &point_indices,&children,&centers,&widths,&MAX_DEPTH]
  81. (const ChildrenType index, const int depth)-> void
  82. {
  83. if(point_indices.at(index).size() > 1 && depth < MAX_DEPTH){
  84. //give the parent access to the children
  85. children.at(index) = zero_to_seven.array() + m;
  86. //make the children's data in our arrays
  87. //Add the children to the lists, as default children
  88. CentersType h = widths.at(index)/2;
  89. RowVector3CentersType curr_center = centers.at(index);
  90. for(ChildrenType i = 0; i < 8; i++){
  91. children.emplace_back(neg_ones);
  92. point_indices.emplace_back(std::vector<IndexType>());
  93. centers.emplace_back(translate_center(curr_center,h/2,i));
  94. widths.emplace_back(h);
  95. }
  96. //Split up the points into the corresponding children
  97. for(int j = 0; j < point_indices.at(index).size(); j++){
  98. IndexType curr_point_index = point_indices.at(index).at(j);
  99. IndexType cell_of_curr_point =
  100. get_octant(P.row(curr_point_index),curr_center)+m;
  101. point_indices.at(cell_of_curr_point).emplace_back(curr_point_index);
  102. }
  103. //Now increase m
  104. m += 8;
  105. // Look ma, I'm calling myself.
  106. for(int i = 0; i < 8; i++){
  107. helper(children.at(index)(i),depth+1);
  108. }
  109. }
  110. };
  111. {
  112. std::vector<IndexType> all(P.rows());
  113. for(IndexType i = 0;i<all.size();i++) all[i]=i;
  114. point_indices.emplace_back(all);
  115. }
  116. children.emplace_back(neg_ones);
  117. //Get the minimum AABB for the points
  118. RowVector3PType backleftbottom(P.col(0).minCoeff(),
  119. P.col(1).minCoeff(),
  120. P.col(2).minCoeff());
  121. RowVector3PType frontrighttop(P.col(0).maxCoeff(),
  122. P.col(1).maxCoeff(),
  123. P.col(2).maxCoeff());
  124. RowVector3CentersType aabb_center = (backleftbottom+frontrighttop)/2.0;
  125. WidthsType aabb_width = std::max(std::max(
  126. frontrighttop(0) - backleftbottom(0),
  127. frontrighttop(1) - backleftbottom(1)),
  128. frontrighttop(2) - backleftbottom(2));
  129. centers.emplace_back( aabb_center );
  130. //Widths are the side length of the cube, (not half the side length):
  131. widths.emplace_back( aabb_width );
  132. m++;
  133. // then you have to actually call the function
  134. helper(0,0);
  135. //Now convert from vectors to Eigen matricies:
  136. CH.resize(children.size(),8);
  137. CN.resize(centers.size(),3);
  138. W.resize(widths.size(),1);
  139. for(int i = 0; i < children.size(); i++){
  140. CH.row(i) = children.at(i);
  141. }
  142. for(int i = 0; i < centers.size(); i++){
  143. CN.row(i) = centers.at(i);
  144. }
  145. for(int i = 0; i < widths.size(); i++){
  146. W(i) = widths.at(i);
  147. }
  148. }
  149. }
  150. #ifdef IGL_STATIC_LIBRARY
  151. // Explicit template instantiation
  152. template void igl::octree<Eigen::Matrix<double, -1, -1, 0, -1, -1>, int, Eigen::Matrix<int, -1, 8, 0, -1, 8>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 8, 0, -1, 8> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  153. #endif