half_space_box.cpp 5.3 KB

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