bounding_box.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
  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. using namespace std;
  26. const int dim = V.cols();
  27. const auto & minV = V.colwise().minCoeff().array()-pad;
  28. const auto & maxV = V.colwise().maxCoeff().array()+pad;
  29. // 2^n vertices
  30. BV.resize((1<<dim),dim);
  31. // Recursive lambda to generate all 2^n combinations
  32. const std::function<void(const int,const int,int*,int)> combos =
  33. [&BV,&minV,&maxV,&combos](
  34. const int dim,
  35. const int i,
  36. int * X,
  37. const int pre_index)
  38. {
  39. for(X[i] = 0;X[i]<2;X[i]++)
  40. {
  41. int index = pre_index*2+X[i];
  42. if((i+1)<dim)
  43. {
  44. combos(dim,i+1,X,index);
  45. }else
  46. {
  47. for(int d = 0;d<dim;d++)
  48. {
  49. BV(index,d) = (X[d]?minV[d]:maxV[d]);
  50. }
  51. }
  52. }
  53. };
  54. Eigen::VectorXi X(dim);
  55. combos(dim,0,X.data(),0);
  56. switch(dim)
  57. {
  58. case 2:
  59. BF.resize(4,2);
  60. BF<<
  61. 3,1,
  62. 1,0,
  63. 0,2,
  64. 2,3;
  65. break;
  66. case 3:
  67. BF.resize(12,3);
  68. BF<<
  69. 2,0,6,
  70. 0,4,6,
  71. 5,4,0,
  72. 5,0,1,
  73. 6,4,5,
  74. 5,7,6,
  75. 3,0,2,
  76. 1,0,3,
  77. 3,2,6,
  78. 6,7,3,
  79. 5,1,3,
  80. 3,7,5;
  81. break;
  82. default:
  83. assert(false && "Unsupported dimension.");
  84. break;
  85. }
  86. }
  87. #ifdef IGL_STATIC_LIBRARY
  88. // Explicit template instantiation
  89. // generated by autoexplicit.sh
  90. 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> >&);
  91. // generated by autoexplicit.sh
  92. 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> >&);
  93. // generated by autoexplicit.sh
  94. 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> >&);
  95. 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> >&);
  96. 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> >&);
  97. #endif