minkowski_sum.cpp 9.9 KB

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