box_faces.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "box_faces.h"
  2. #include "AABB.h"
  3. #include <vector>
  4. #include <utility>
  5. template <typename DerivedV, typename DerivedQ>
  6. IGL_INLINE void igl::box_faces(
  7. const Eigen::AlignedBox<typename DerivedV::Scalar,3> & box,
  8. const typename DerivedV::Scalar shrink,
  9. Eigen::PlainObjectBase<DerivedV> & P,
  10. Eigen::PlainObjectBase<DerivedQ> & Q)
  11. {
  12. auto min_corner = box.min();
  13. auto max_corner = box.max();
  14. // shrink by 3%
  15. min_corner = min_corner + shrink*(max_corner-min_corner);
  16. max_corner = max_corner - shrink*(max_corner-min_corner);
  17. P.resize(8,3);
  18. Q.resize(6,4);
  19. int p = 0;
  20. int q = 0;
  21. Q.row(q++) << p+0,p+1,p+2,p+3;
  22. Q.row(q++) << p+0,p+1,p+5,p+4;
  23. Q.row(q++) << p+1,p+2,p+6,p+5;
  24. Q.row(q++) << p+2,p+3,p+7,p+6;
  25. Q.row(q++) << p+3,p+0,p+4,p+7;
  26. Q.row(q++) << p+4,p+5,p+6,p+7;
  27. P.row(p++) = min_corner;
  28. P.row(p++) = Eigen::RowVector3d(max_corner[0],min_corner[1],min_corner[2]);
  29. P.row(p++) = Eigen::RowVector3d(max_corner[0],max_corner[1],min_corner[2]);
  30. P.row(p++) = Eigen::RowVector3d(min_corner[0],max_corner[1],min_corner[2]);
  31. P.row(p++) = Eigen::RowVector3d(min_corner[0],min_corner[1],max_corner[2]);
  32. P.row(p++) = Eigen::RowVector3d(max_corner[0],min_corner[1],max_corner[2]);
  33. P.row(p++) = max_corner;
  34. P.row(p++) = Eigen::RowVector3d(min_corner[0],max_corner[1],max_corner[2]);
  35. }
  36. template <
  37. typename DerivedV,
  38. typename DerivedP,
  39. typename DerivedQ,
  40. typename DerivedD >
  41. IGL_INLINE void igl::box_faces(
  42. const igl::AABB<DerivedV,3> & tree,
  43. Eigen::PlainObjectBase<DerivedP> & P,
  44. Eigen::PlainObjectBase<DerivedQ> & Q,
  45. Eigen::PlainObjectBase<DerivedD> & D)
  46. {
  47. const int num_nodes = tree.size();
  48. P.resize(8*num_nodes,3);
  49. Q.resize(6*num_nodes,4);
  50. D.resize(6*num_nodes);
  51. int d = 0;
  52. int p = 0;
  53. int q = 0;
  54. std::vector<std::pair<const igl::AABB<DerivedV,3> *,int> > stack;
  55. stack.push_back({&tree,0});
  56. while(!stack.empty())
  57. {
  58. const auto pair = stack.back();
  59. const auto * node = pair.first;
  60. const int depth = pair.second;
  61. D(d++) = depth;
  62. D(d++) = depth;
  63. D(d++) = depth;
  64. D(d++) = depth;
  65. D(d++) = depth;
  66. D(d++) = depth;
  67. stack.pop_back();
  68. const auto & box = node->m_box;
  69. DerivedP Pi;
  70. DerivedQ Qi;
  71. box_faces(box,0.03,Pi,Qi);
  72. P.block(p,0,8,3) = Pi;
  73. Q.block(q,0,6,4) = Qi.array()+p;
  74. p += 8;
  75. q += 6;
  76. if(node->m_left)
  77. {
  78. stack.push_back({node->m_left,depth+1});
  79. }
  80. if(node->m_right)
  81. {
  82. stack.push_back({node->m_right,depth+1});
  83. }
  84. }
  85. }
  86. #ifdef IGL_STATIC_LIBRARY
  87. // Explicit template instantiation
  88. // generated by autoexplicit.sh
  89. template void igl::box_faces<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(igl::AABB<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 3> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  90. #endif