bounding_box.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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::PlainObjectBase<DerivedV>& V,
  13. Eigen::PlainObjectBase<DerivedBV>& BV,
  14. Eigen::PlainObjectBase<DerivedBF>& BF)
  15. {
  16. using namespace std;
  17. const int dim = V.cols();
  18. const auto & minV = V.colwise().minCoeff();
  19. const auto & maxV = V.colwise().maxCoeff();
  20. // 2^n vertices
  21. BV.resize((1<<dim),dim);
  22. // Recursive lambda to generate all 2^n combinations
  23. const std::function<void(const int,const int,int*,int)> combos =
  24. [&BV,&minV,&maxV,&combos](
  25. const int dim,
  26. const int i,
  27. int * X,
  28. const int pre_index)
  29. {
  30. for(X[i] = 0;X[i]<2;X[i]++)
  31. {
  32. int index = pre_index*2+X[i];
  33. if((i+1)<dim)
  34. {
  35. combos(dim,i+1,X,index);
  36. }else
  37. {
  38. for(int d = 0;d<dim;d++)
  39. {
  40. BV(index,d) = (X[d]?minV[d]:maxV[d]);
  41. }
  42. }
  43. }
  44. };
  45. Eigen::VectorXi X(dim);
  46. combos(dim,0,X.data(),0);
  47. switch(dim)
  48. {
  49. case 2:
  50. BF.resize(4,2);
  51. BF<<
  52. 3,1,
  53. 1,0,
  54. 0,2,
  55. 2,3;
  56. break;
  57. case 3:
  58. BF.resize(12,3);
  59. BF<<
  60. 2,0,6,
  61. 0,4,6,
  62. 5,4,0,
  63. 5,0,1,
  64. 6,4,5,
  65. 5,7,6,
  66. 3,0,2,
  67. 1,0,3,
  68. 3,2,6,
  69. 6,7,3,
  70. 5,1,3,
  71. 3,7,5;
  72. break;
  73. default:
  74. assert(false && "Unsupported dimension.");
  75. break;
  76. }
  77. }
  78. #ifdef IGL_STATIC_LIBRARY
  79. // Explicit template specialization
  80. 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::PlainObjectBase<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> >&);
  81. 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::PlainObjectBase<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> >&);
  82. #endif