half_space_box.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "half_space_box.h"
  2. #include "assign_scalar.h"
  3. #include <CGAL/Point_3.h>
  4. #include <CGAL/Vector_3.h>
  5. template <typename DerivedV>
  6. IGL_INLINE void igl::copyleft::cgal::half_space_box(
  7. const CGAL::Plane_3<CGAL::Epeck> & P,
  8. const Eigen::PlainObjectBase<DerivedV> & V,
  9. Eigen::Matrix<CGAL::Epeck::FT,8,3> & BV,
  10. Eigen::Matrix<int,12,3> & BF)
  11. {
  12. typedef CGAL::Plane_3<CGAL::Epeck> Plane;
  13. typedef CGAL::Point_3<CGAL::Epeck> Point;
  14. typedef CGAL::Vector_3<CGAL::Epeck> Vector;
  15. typedef CGAL::Epeck::FT EScalar;
  16. Eigen::RowVector3d avg = V.colwise().mean();
  17. Point o3(avg(0),avg(1),avg(2));
  18. Point o2 = P.projection(o3);
  19. Vector u;
  20. EScalar max_sqrd = -1;
  21. for(int v = 0;v<V.rows();v++)
  22. {
  23. Vector v2 = P.projection(Point(V(v,0),V(v,1),V(v,2))) - o2;
  24. const EScalar sqrd = v2.squared_length();
  25. if(max_sqrd<0 || sqrd < max_sqrd)
  26. {
  27. u = v2;
  28. max_sqrd = sqrd;
  29. }
  30. }
  31. // L1 bbd
  32. const EScalar bbd =
  33. (EScalar(V.col(0).maxCoeff())- EScalar(V.col(0).minCoeff())) +
  34. (EScalar(V.col(1).maxCoeff())- EScalar(V.col(1).minCoeff())) +
  35. (EScalar(V.col(2).maxCoeff())- EScalar(V.col(2).minCoeff()));
  36. Vector n = P.orthogonal_vector();
  37. // now we have a center o2 and a vector u to the farthest point on the plane
  38. std::vector<Point> vBV;vBV.reserve(8);
  39. Vector v = CGAL::cross_product(u,n);
  40. // Scale u,v,n to be longer than bbd
  41. const auto & longer_than = [](const EScalar min_sqr, Vector & x)
  42. {
  43. assert(x.squared_length() > 0);
  44. while(x.squared_length() < min_sqr)
  45. {
  46. x = 2.*x;
  47. }
  48. };
  49. longer_than(bbd*bbd,u);
  50. longer_than(bbd*bbd,v);
  51. longer_than(bbd*bbd,n);
  52. vBV.emplace_back( o2 + u + v);
  53. vBV.emplace_back( o2 - u + v);
  54. vBV.emplace_back( o2 - u - v);
  55. vBV.emplace_back( o2 + u - v);
  56. vBV.emplace_back( o2 + u + v - n);
  57. vBV.emplace_back( o2 - u + v - n);
  58. vBV.emplace_back( o2 - u - v - n);
  59. vBV.emplace_back( o2 + u - v - n);
  60. BV.resize(8,3);
  61. for(int b = 0;b<8;b++)
  62. {
  63. igl::copyleft::cgal::assign_scalar(vBV[b].x(),BV(b,0));
  64. igl::copyleft::cgal::assign_scalar(vBV[b].y(),BV(b,1));
  65. igl::copyleft::cgal::assign_scalar(vBV[b].z(),BV(b,2));
  66. }
  67. BF.resize(12,3);
  68. BF<<
  69. 1,0,2,
  70. 2,0,3,
  71. 4,5,6,
  72. 4,6,7,
  73. 0,1,4,
  74. 4,1,5,
  75. 1,2,5,
  76. 5,2,6,
  77. 2,3,6,
  78. 6,3,7,
  79. 3,0,7,
  80. 7,0,4;
  81. }
  82. template <typename Derivedp, typename Derivedn, typename DerivedV>
  83. IGL_INLINE void igl::copyleft::cgal::half_space_box(
  84. const Eigen::PlainObjectBase<Derivedp> & p,
  85. const Eigen::PlainObjectBase<Derivedn> & n,
  86. const Eigen::PlainObjectBase<DerivedV> & V,
  87. Eigen::Matrix<CGAL::Epeck::FT,8,3> & BV,
  88. Eigen::Matrix<int,12,3> & BF)
  89. {
  90. typedef CGAL::Plane_3<CGAL::Epeck> Plane;
  91. typedef CGAL::Point_3<CGAL::Epeck> Point;
  92. typedef CGAL::Vector_3<CGAL::Epeck> Vector;
  93. Plane P(Point(p(0),p(1),p(2)),Vector(n(0),n(1),n(2)));
  94. return half_space_box(P,V,BV,BF);
  95. }
  96. template <typename Derivedequ, typename DerivedV>
  97. IGL_INLINE void igl::copyleft::cgal::half_space_box(
  98. const Eigen::PlainObjectBase<Derivedequ> & equ,
  99. const Eigen::PlainObjectBase<DerivedV> & V,
  100. Eigen::Matrix<CGAL::Epeck::FT,8,3> & BV,
  101. Eigen::Matrix<int,12,3> & BF)
  102. {
  103. typedef CGAL::Plane_3<CGAL::Epeck> Plane;
  104. Plane P(equ(0),equ(1),equ(2),equ(3));
  105. return half_space_box(P,V,BV,BF);
  106. }
  107. #ifdef IGL_STATIC_LIBRARY
  108. // Explicit template specialization
  109. template void igl::copyleft::cgal::half_space_box<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, 8, 3, 0, 8, 3>&, Eigen::Matrix<int, 12, 3, 0, 12, 3>&);
  110. #endif