minkowski_sum.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "minkowski_sum.h"
  2. #include "mesh_boolean.h"
  3. #include "../../slice_mask.h"
  4. #include "../../unique.h"
  5. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  6. template <
  7. typename DerivedV,
  8. typename DerivedF,
  9. typename Deriveds,
  10. typename Derivedd,
  11. typename DerivedW,
  12. typename DerivedG,
  13. typename DerivedJ>
  14. IGL_INLINE void igl::copyleft::boolean::minkowski_sum(
  15. const Eigen::PlainObjectBase<DerivedV> & V,
  16. const Eigen::PlainObjectBase<DerivedF> & F,
  17. const Eigen::PlainObjectBase<Deriveds> & s,
  18. const Eigen::PlainObjectBase<Derivedd> & d,
  19. Eigen::PlainObjectBase<DerivedW> & W,
  20. Eigen::PlainObjectBase<DerivedG> & G,
  21. Eigen::PlainObjectBase<DerivedJ> & J)
  22. {
  23. using namespace Eigen;
  24. using namespace std;
  25. // silly base case
  26. if(F.size() == 0)
  27. {
  28. W.resize(0,3);
  29. G.resize(0,3);
  30. return;
  31. }
  32. const int dim = V.cols();
  33. assert(dim == 3 && "dim must be 3D");
  34. assert(s.size() == 3 && "s must be 3D point");
  35. assert(d.size() == 3 && "d must be 3D point");
  36. // segment vector
  37. const CGAL::Vector_3<CGAL::Epeck> v(d(0)-s(0),d(1)-s(1),d(2)-s(2));
  38. // number of vertices
  39. const int n = V.rows();
  40. // duplicate vertices at s and d, we'll remove unreferernced later
  41. DerivedW WW(2*n,dim);
  42. for(int i = 0;i<n;i++)
  43. {
  44. for(int j = 0;j<dim;j++)
  45. {
  46. WW (i,j) = V(i,j) + s(j);
  47. WW(i+n,j) = V(i,j) + d(j);
  48. }
  49. }
  50. // number of faces
  51. const int m = F.rows();
  52. // Mask whether positive dot product, or negative: because of exactly zero,
  53. // these are not necessarily complementary
  54. Matrix<bool,Dynamic,1> P(m,1),N(m,1);
  55. // loop over faces
  56. int mp = 0,mn = 0;
  57. for(int f = 0;f<m;f++)
  58. {
  59. const CGAL::Plane_3<CGAL::Epeck> plane(
  60. CGAL::Point_3<CGAL::Epeck>(V(F(f,0),0),V(F(f,0),1),V(F(f,0),2)),
  61. CGAL::Point_3<CGAL::Epeck>(V(F(f,1),0),V(F(f,1),1),V(F(f,1),2)),
  62. CGAL::Point_3<CGAL::Epeck>(V(F(f,2),0),V(F(f,2),1),V(F(f,2),2)));
  63. const auto normal = plane.orthogonal_vector();
  64. const auto dt = normal * v;
  65. if(dt > 0)
  66. {
  67. P(f) = true;
  68. N(f) = false;
  69. mp++;
  70. }else if(dt < 0)
  71. {
  72. P(f) = false;
  73. N(f) = true;
  74. mn++;
  75. }else
  76. {
  77. P(f) = false;
  78. N(f) = false;
  79. }
  80. }
  81. typedef Matrix<typename DerivedG::Scalar,Dynamic,Dynamic> MatrixXI;
  82. typedef Matrix<typename DerivedG::Scalar,Dynamic,1> VectorXI;
  83. MatrixXI GT(mp+mn,3);
  84. GT<< slice_mask(F,N,1), slice_mask((F.array()+n).eval(),P,1);
  85. // J indexes F for parts at s and m+F for parts at d
  86. J = DerivedJ::LinSpaced(m,0,m-1);
  87. DerivedJ JT(mp+mn);
  88. JT << slice_mask(J,P,1), slice_mask(J,N,1);
  89. JT.block(mp,0,mn,1).array()+=m;
  90. // Original non-co-planar faces with positively oriented reversed
  91. MatrixXI BA(mp+mn,3);
  92. BA << slice_mask(F,P,1).rowwise().reverse(), slice_mask(F,N,1);
  93. // Quads along **all** sides
  94. MatrixXI GQ((mp+mn)*3,4);
  95. GQ<<
  96. BA.col(1), BA.col(0), BA.col(0).array()+n, BA.col(1).array()+n,
  97. BA.col(2), BA.col(1), BA.col(1).array()+n, BA.col(2).array()+n,
  98. BA.col(0), BA.col(2), BA.col(2).array()+n, BA.col(0).array()+n;
  99. MatrixXI uGQ;
  100. VectorXI S,sI,sJ;
  101. //const auto & total_signed_distance =
  102. [](
  103. const MatrixXI & F,
  104. VectorXI & S,
  105. MatrixXI & uF,
  106. VectorXI & I,
  107. VectorXI & J)
  108. {
  109. const int m = F.rows();
  110. const int d = F.cols();
  111. MatrixXI sF = F;
  112. const auto MN = sF.rowwise().minCoeff().eval();
  113. // rotate until smallest index is first
  114. for(int p = 0;p<d;p++)
  115. {
  116. for(int f = 0;f<m;f++)
  117. {
  118. if(sF(f,0) != MN(f))
  119. {
  120. for(int r = 0;r<d-1;r++)
  121. {
  122. std::swap(sF(f,r),sF(f,r+1));
  123. }
  124. }
  125. }
  126. }
  127. // swap orienation
  128. for(int f = 0;f<m;f++)
  129. {
  130. if(sF(f,d-1) < sF(f,1))
  131. {
  132. sF.block(f,1,1,d-1) = sF.block(f,1,1,d-1).reverse().eval();
  133. }
  134. }
  135. Matrix<bool,Dynamic,1> M = Matrix<bool,Dynamic,1>::Zero(m,1);
  136. {
  137. VectorXI P = VectorXI::LinSpaced(d,0,d-1);
  138. for(int p = 0;p<d;p++)
  139. {
  140. for(int f = 0;f<m;f++)
  141. {
  142. bool all = true;
  143. for(int r = 0;r<d;r++)
  144. {
  145. all = all && (sF(f,P(r)) == F(f,r));
  146. }
  147. M(f) = M(f) || all;
  148. }
  149. for(int r = 0;r<d-1;r++)
  150. {
  151. std::swap(P(r),P(r+1));
  152. }
  153. }
  154. }
  155. unique_rows(sF,uF,I,J);
  156. S = VectorXI::Zero(uF.rows(),1);
  157. assert(m == J.rows());
  158. for(int f = 0;f<m;f++)
  159. {
  160. S(J(f)) += M(f) ? 1 : -1;
  161. }
  162. }(MatrixXI(GQ),S,uGQ,sI,sJ);
  163. assert(S.rows() == uGQ.rows());
  164. const int nq = (S.array().abs()==2).count();
  165. GQ.resize(nq,4);
  166. {
  167. int k = 0;
  168. for(int q = 0;q<uGQ.rows();q++)
  169. {
  170. switch(S(q))
  171. {
  172. #warning "If (V,F) is non-manifold, does this handle all cases?"
  173. case -2:
  174. GQ.row(k++) = uGQ.row(q).reverse().eval();
  175. break;
  176. case 2:
  177. GQ.row(k++) = uGQ.row(q);
  178. break;
  179. default:
  180. // do not add
  181. break;
  182. }
  183. }
  184. assert(nq == k);
  185. }
  186. MatrixXI GG(GT.rows()+2*GQ.rows(),3);
  187. GG<<
  188. GT,
  189. GQ.col(0), GQ.col(1), GQ.col(2),
  190. GQ.col(0), GQ.col(2), GQ.col(3);
  191. J.resize(JT.rows()+2*GQ.rows(),1);
  192. J<<JT,DerivedJ::Constant(2*GQ.rows(),1,2*m+1);
  193. {
  194. DerivedJ SJ;
  195. mesh_boolean(
  196. WW,GG,
  197. Matrix<typename DerivedV::Scalar,Dynamic,Dynamic>(),MatrixXI(),
  198. MESH_BOOLEAN_TYPE_UNION,
  199. W,G,SJ);
  200. J = slice(DerivedJ(J),SJ,1);
  201. }
  202. }