minkowski_sum.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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 "minkowski_sum.h"
  9. #include "mesh_boolean.h"
  10. #include "../../slice_mask.h"
  11. #include "../../unique.h"
  12. #include "../../get_seconds.h"
  13. #include "../../edges.h"
  14. #include "../cgal/assign_scalar.h"
  15. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  16. #include <cassert>
  17. #include <vector>
  18. template <
  19. typename DerivedVA,
  20. typename DerivedFA,
  21. typename DerivedVB,
  22. typename DerivedFB,
  23. typename DerivedW,
  24. typename DerivedG,
  25. typename DerivedJ>
  26. IGL_INLINE void igl::copyleft::boolean::minkowski_sum(
  27. const Eigen::PlainObjectBase<DerivedVA> & VA,
  28. const Eigen::PlainObjectBase<DerivedFA> & FA,
  29. const Eigen::PlainObjectBase<DerivedVB> & VB,
  30. const Eigen::PlainObjectBase<DerivedFB> & FB,
  31. const bool resolve_overlaps,
  32. Eigen::PlainObjectBase<DerivedW> & W,
  33. Eigen::PlainObjectBase<DerivedG> & G,
  34. Eigen::PlainObjectBase<DerivedJ> & J)
  35. {
  36. using namespace std;
  37. using namespace Eigen;
  38. assert(FA.cols() == 3 && "FA must contain a closed triangle mesh");
  39. assert(FB.cols() <= FA.cols() &&
  40. "FB must contain lower diemnsional simplices than FA");
  41. const auto tictoc = []()->double
  42. {
  43. static double t_start;
  44. double now = igl::get_seconds();
  45. double interval = now-t_start;
  46. t_start = now;
  47. return interval;
  48. };
  49. tictoc();
  50. Matrix<typename DerivedFB::Scalar,Dynamic,2> EB;
  51. edges(FB,EB);
  52. Matrix<typename DerivedFA::Scalar,Dynamic,2> EA(0,2);
  53. if(FB.cols() == 3)
  54. {
  55. edges(FA,EA);
  56. }
  57. // number of copies of A along edges of B
  58. const int n_ab = EB.rows();
  59. // number of copies of B along edges of A
  60. const int n_ba = EA.rows();
  61. vector<DerivedW> vW(n_ab + n_ba);
  62. vector<DerivedG> vG(n_ab + n_ba);
  63. vector<DerivedJ> vJ(n_ab + n_ba);
  64. vector<int> offsets(n_ab + n_ba + 1);
  65. offsets[0] = 0;
  66. // sweep A along edges of B
  67. for(int e = 0;e<n_ab;e++)
  68. {
  69. Matrix<typename DerivedJ::Scalar,Dynamic,1> eJ;
  70. minkowski_sum(
  71. VA,
  72. FA,
  73. VB.row(EB(e,0)).eval(),
  74. VB.row(EB(e,1)).eval(),
  75. false,
  76. vW[e],
  77. vG[e],
  78. eJ);
  79. assert(vG[e].rows() == eJ.rows());
  80. assert(eJ.cols() == 1);
  81. vJ[e].resize(vG[e].rows(),2);
  82. vJ[e].col(0) = eJ;
  83. vJ[e].col(1).setConstant(e);
  84. offsets[e+1] = offsets[e] + vW[e].rows();
  85. }
  86. // sweep B along edges of A
  87. for(int e = 0;e<n_ba;e++)
  88. {
  89. Matrix<typename DerivedJ::Scalar,Dynamic,1> eJ;
  90. const int ee = n_ab+e;
  91. minkowski_sum(
  92. VB,
  93. FB,
  94. VA.row(EA(e,0)).eval(),
  95. VA.row(EA(e,1)).eval(),
  96. false,
  97. vW[ee],
  98. vG[ee],
  99. eJ);
  100. vJ[ee].resize(vG[ee].rows(),2);
  101. vJ[ee].col(0) = eJ.array() + (FA.rows()+1);
  102. vJ[ee].col(1).setConstant(ee);
  103. }
  104. // Combine meshes
  105. int n=0,m=0;
  106. for_each(vW.begin(),vW.end(),[&n](const DerivedW & w){n+=w.rows();});
  107. for_each(vG.begin(),vG.end(),[&m](const DerivedG & g){m+=g.rows();});
  108. assert(n == offsets.back());
  109. W.resize(n,3);
  110. G.resize(m,3);
  111. J.resize(m,2);
  112. {
  113. int m_off = 0,n_off = 0;
  114. for(int i = 0;i<vG.size();i++)
  115. {
  116. W.block(n_off,0,vW[i].rows(),3) = vW[i];
  117. G.block(m_off,0,vG[i].rows(),3) = vG[i].array()+offsets[i];
  118. J.block(m_off,0,vJ[i].rows(),2) = vJ[i];
  119. n_off += vW[i].rows();
  120. m_off += vG[i].rows();
  121. }
  122. assert(n == n_off);
  123. assert(m == m_off);
  124. }
  125. if(resolve_overlaps)
  126. {
  127. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1> SJ;
  128. mesh_boolean(
  129. DerivedW(W),
  130. DerivedG(G),
  131. Matrix<typename DerivedW::Scalar,Dynamic,Dynamic>(),
  132. Matrix<typename DerivedG::Scalar,Dynamic,Dynamic>(),
  133. MESH_BOOLEAN_TYPE_UNION,
  134. W,
  135. G,
  136. SJ);
  137. J = slice(DerivedJ(J),SJ,1);
  138. }
  139. }
  140. template <
  141. typename DerivedVA,
  142. typename DerivedFA,
  143. typename sType, int sCols, int sOptions,
  144. typename dType, int dCols, int dOptions,
  145. typename DerivedW,
  146. typename DerivedG,
  147. typename DerivedJ>
  148. IGL_INLINE void igl::copyleft::boolean::minkowski_sum(
  149. const Eigen::PlainObjectBase<DerivedVA> & VA,
  150. const Eigen::PlainObjectBase<DerivedFA> & FA,
  151. const Eigen::Matrix<sType,1,sCols,sOptions> & s,
  152. const Eigen::Matrix<dType,1,dCols,dOptions> & d,
  153. const bool resolve_overlaps,
  154. Eigen::PlainObjectBase<DerivedW> & W,
  155. Eigen::PlainObjectBase<DerivedG> & G,
  156. Eigen::PlainObjectBase<DerivedJ> & J)
  157. {
  158. using namespace Eigen;
  159. using namespace std;
  160. assert(s.cols() == 3 && "s should be a 3d point");
  161. assert(d.cols() == 3 && "d should be a 3d point");
  162. // silly base case
  163. if(FA.size() == 0)
  164. {
  165. W.resize(0,3);
  166. G.resize(0,3);
  167. return;
  168. }
  169. const int dim = VA.cols();
  170. assert(dim == 3 && "dim must be 3D");
  171. assert(s.size() == 3 && "s must be 3D point");
  172. assert(d.size() == 3 && "d must be 3D point");
  173. // segment vector
  174. const CGAL::Vector_3<CGAL::Epeck> v(d(0)-s(0),d(1)-s(1),d(2)-s(2));
  175. // number of vertices
  176. const int n = VA.rows();
  177. // duplicate vertices at s and d, we'll remove unreferernced later
  178. W.resize(2*n,dim);
  179. for(int i = 0;i<n;i++)
  180. {
  181. for(int j = 0;j<dim;j++)
  182. {
  183. W (i,j) = VA(i,j) + s(j);
  184. W(i+n,j) = VA(i,j) + d(j);
  185. }
  186. }
  187. // number of faces
  188. const int m = FA.rows();
  189. // Mask whether positive dot product, or negative: because of exactly zero,
  190. // these are not necessarily complementary
  191. Matrix<bool,Dynamic,1> P(m,1),N(m,1);
  192. // loop over faces
  193. int mp = 0,mn = 0;
  194. for(int f = 0;f<m;f++)
  195. {
  196. const CGAL::Plane_3<CGAL::Epeck> plane(
  197. CGAL::Point_3<CGAL::Epeck>(VA(FA(f,0),0),VA(FA(f,0),1),VA(FA(f,0),2)),
  198. CGAL::Point_3<CGAL::Epeck>(VA(FA(f,1),0),VA(FA(f,1),1),VA(FA(f,1),2)),
  199. CGAL::Point_3<CGAL::Epeck>(VA(FA(f,2),0),VA(FA(f,2),1),VA(FA(f,2),2)));
  200. const auto normal = plane.orthogonal_vector();
  201. const auto dt = normal * v;
  202. if(dt > 0)
  203. {
  204. P(f) = true;
  205. N(f) = false;
  206. mp++;
  207. }else if(dt < 0)
  208. {
  209. P(f) = false;
  210. N(f) = true;
  211. mn++;
  212. }else
  213. {
  214. P(f) = false;
  215. N(f) = false;
  216. }
  217. }
  218. typedef Matrix<typename DerivedG::Scalar,Dynamic,Dynamic> MatrixXI;
  219. typedef Matrix<typename DerivedG::Scalar,Dynamic,1> VectorXI;
  220. MatrixXI GT(mp+mn,3);
  221. GT<< slice_mask(FA,N,1), slice_mask((FA.array()+n).eval(),P,1);
  222. // J indexes FA for parts at s and m+FA for parts at d
  223. J = DerivedJ::LinSpaced(m,0,m-1);
  224. DerivedJ JT(mp+mn);
  225. JT << slice_mask(J,P,1), slice_mask(J,N,1);
  226. JT.block(mp,0,mn,1).array()+=m;
  227. // Original non-co-planar faces with positively oriented reversed
  228. MatrixXI BA(mp+mn,3);
  229. BA << slice_mask(FA,P,1).rowwise().reverse(), slice_mask(FA,N,1);
  230. // Quads along **all** sides
  231. MatrixXI GQ((mp+mn)*3,4);
  232. GQ<<
  233. BA.col(1), BA.col(0), BA.col(0).array()+n, BA.col(1).array()+n,
  234. BA.col(2), BA.col(1), BA.col(1).array()+n, BA.col(2).array()+n,
  235. BA.col(0), BA.col(2), BA.col(2).array()+n, BA.col(0).array()+n;
  236. MatrixXI uGQ;
  237. VectorXI S,sI,sJ;
  238. //const auto & total_signed_distance =
  239. [](
  240. const MatrixXI & F,
  241. VectorXI & S,
  242. MatrixXI & uF,
  243. VectorXI & I,
  244. VectorXI & J)
  245. {
  246. const int m = F.rows();
  247. const int d = F.cols();
  248. MatrixXI sF = F;
  249. const auto MN = sF.rowwise().minCoeff().eval();
  250. // rotate until smallest index is first
  251. for(int p = 0;p<d;p++)
  252. {
  253. for(int f = 0;f<m;f++)
  254. {
  255. if(sF(f,0) != MN(f))
  256. {
  257. for(int r = 0;r<d-1;r++)
  258. {
  259. std::swap(sF(f,r),sF(f,r+1));
  260. }
  261. }
  262. }
  263. }
  264. // swap orienation
  265. for(int f = 0;f<m;f++)
  266. {
  267. if(sF(f,d-1) < sF(f,1))
  268. {
  269. sF.block(f,1,1,d-1) = sF.block(f,1,1,d-1).reverse().eval();
  270. }
  271. }
  272. Matrix<bool,Dynamic,1> M = Matrix<bool,Dynamic,1>::Zero(m,1);
  273. {
  274. VectorXI P = VectorXI::LinSpaced(d,0,d-1);
  275. for(int p = 0;p<d;p++)
  276. {
  277. for(int f = 0;f<m;f++)
  278. {
  279. bool all = true;
  280. for(int r = 0;r<d;r++)
  281. {
  282. all = all && (sF(f,P(r)) == F(f,r));
  283. }
  284. M(f) = M(f) || all;
  285. }
  286. for(int r = 0;r<d-1;r++)
  287. {
  288. std::swap(P(r),P(r+1));
  289. }
  290. }
  291. }
  292. unique_rows(sF,uF,I,J);
  293. S = VectorXI::Zero(uF.rows(),1);
  294. assert(m == J.rows());
  295. for(int f = 0;f<m;f++)
  296. {
  297. S(J(f)) += M(f) ? 1 : -1;
  298. }
  299. }(MatrixXI(GQ),S,uGQ,sI,sJ);
  300. assert(S.rows() == uGQ.rows());
  301. const int nq = (S.array().abs()==2).count();
  302. GQ.resize(nq,4);
  303. {
  304. int k = 0;
  305. for(int q = 0;q<uGQ.rows();q++)
  306. {
  307. switch(S(q))
  308. {
  309. case -2:
  310. GQ.row(k++) = uGQ.row(q).reverse().eval();
  311. break;
  312. case 2:
  313. GQ.row(k++) = uGQ.row(q);
  314. break;
  315. default:
  316. // do not add
  317. break;
  318. }
  319. }
  320. assert(nq == k);
  321. }
  322. G.resize(GT.rows()+2*GQ.rows(),3);
  323. G<<
  324. GT,
  325. GQ.col(0), GQ.col(1), GQ.col(2),
  326. GQ.col(0), GQ.col(2), GQ.col(3);
  327. J.resize(JT.rows()+2*GQ.rows(),1);
  328. J<<JT,DerivedJ::Constant(2*GQ.rows(),1,2*m+1);
  329. if(resolve_overlaps)
  330. {
  331. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1> SJ;
  332. mesh_boolean(
  333. DerivedW(W),DerivedG(G),
  334. Matrix<typename DerivedVA::Scalar,Dynamic,Dynamic>(),MatrixXI(),
  335. MESH_BOOLEAN_TYPE_UNION,
  336. W,G,SJ);
  337. J = slice(DerivedJ(J),SJ,1);
  338. }
  339. }
  340. template <
  341. typename DerivedVA,
  342. typename DerivedFA,
  343. typename sType, int sCols, int sOptions,
  344. typename dType, int dCols, int dOptions,
  345. typename DerivedW,
  346. typename DerivedG,
  347. typename DerivedJ>
  348. IGL_INLINE void igl::copyleft::boolean::minkowski_sum(
  349. const Eigen::PlainObjectBase<DerivedVA> & VA,
  350. const Eigen::PlainObjectBase<DerivedFA> & FA,
  351. const Eigen::Matrix<sType,1,sCols,sOptions> & s,
  352. const Eigen::Matrix<dType,1,dCols,dOptions> & d,
  353. Eigen::PlainObjectBase<DerivedW> & W,
  354. Eigen::PlainObjectBase<DerivedG> & G,
  355. Eigen::PlainObjectBase<DerivedJ> & J)
  356. {
  357. return minkowski_sum(VA,FA,s,d,true,W,G,J);
  358. }