bounding_box.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "bounding_box.h"
  9. #include <iostream>
  10. template <typename DerivedV, typename DerivedBV, typename DerivedBF>
  11. IGL_INLINE void igl::bounding_box(
  12. const Eigen::MatrixBase<DerivedV>& V,
  13. Eigen::PlainObjectBase<DerivedBV>& BV,
  14. Eigen::PlainObjectBase<DerivedBF>& BF)
  15. {
  16. return bounding_box(V,0.,BV,BF);
  17. }
  18. template <typename DerivedV, typename DerivedBV, typename DerivedBF>
  19. IGL_INLINE void igl::bounding_box(
  20. const Eigen::MatrixBase<DerivedV>& V,
  21. const typename DerivedV::Scalar pad,
  22. Eigen::PlainObjectBase<DerivedBV>& BV,
  23. Eigen::PlainObjectBase<DerivedBF>& BF)
  24. {
  25. const int dim = V.cols();
  26. const auto & minV = V.colwise().minCoeff().array()-pad;
  27. const auto & maxV = V.colwise().maxCoeff().array()+pad;
  28. // 2^n vertices
  29. BV.resize((1ull<<dim),dim);
  30. // Recursive lambda to generate all 2^n combinations
  31. const std::function<void(const int,const int,int*,int)> combos =
  32. [&BV,&minV,&maxV,&combos](
  33. const int dim,
  34. const int i,
  35. int * X,
  36. const int pre_index)
  37. {
  38. for(X[i] = 0;X[i]<2;X[i]++)
  39. {
  40. int index = pre_index*2+X[i];
  41. if((i+1)<dim)
  42. {
  43. combos(dim,i+1,X,index);
  44. }else
  45. {
  46. for(int d = 0;d<dim;d++)
  47. {
  48. BV(index,d) = (X[d]?minV[d]:maxV[d]);
  49. }
  50. }
  51. }
  52. };
  53. Eigen::VectorXi X(dim);
  54. combos(dim,0,X.data(),0);
  55. switch(dim)
  56. {
  57. case 2:
  58. BF.resize(4,2);
  59. BF<<
  60. 3,1,
  61. 1,0,
  62. 0,2,
  63. 2,3;
  64. break;
  65. case 3:
  66. BF.resize(12,3);
  67. BF<<
  68. 2,0,6,
  69. 0,4,6,
  70. 5,4,0,
  71. 5,0,1,
  72. 6,4,5,
  73. 5,7,6,
  74. 3,0,2,
  75. 1,0,3,
  76. 3,2,6,
  77. 6,7,3,
  78. 5,1,3,
  79. 3,7,5;
  80. break;
  81. default:
  82. assert(false && "Unsupported dimension.");
  83. break;
  84. }
  85. }
  86. #ifdef IGL_STATIC_LIBRARY
  87. // Explicit template instantiation
  88. // generated by autoexplicit.sh
  89. template void igl::bounding_box<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  90. // generated by autoexplicit.sh
  91. template void igl::bounding_box<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  92. // generated by autoexplicit.sh
  93. template void igl::bounding_box<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::Matrix<double, -1, -1, 1, -1, -1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  94. // generated by autoexplicit.sh
  95. template void igl::bounding_box<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  96. template void igl::bounding_box<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  97. template void igl::bounding_box<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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  98. #endif