minkowski_sum.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <[email protected]>
  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 "../../LinSpaced.h"
  11. #include "../../unique_rows.h"
  12. #include "../../placeholders.h"
  13. #include "../../find.h"
  14. #include "../../get_seconds.h"
  15. #include "../../edges.h"
  16. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  17. #include <cassert>
  18. #include <vector>
  19. #include <iostream>
  20. template <
  21. typename DerivedVA,
  22. typename DerivedFA,
  23. typename DerivedVB,
  24. typename DerivedFB,
  25. typename DerivedW,
  26. typename DerivedG,
  27. typename DerivedJ>
  28. IGL_INLINE void igl::copyleft::cgal::minkowski_sum(
  29. const Eigen::MatrixBase<DerivedVA> & VA,
  30. const Eigen::MatrixBase<DerivedFA> & FA,
  31. const Eigen::MatrixBase<DerivedVB> & VB,
  32. const Eigen::MatrixBase<DerivedFB> & FB,
  33. const bool resolve_overlaps,
  34. Eigen::PlainObjectBase<DerivedW> & W,
  35. Eigen::PlainObjectBase<DerivedG> & G,
  36. Eigen::PlainObjectBase<DerivedJ> & J)
  37. {
  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. Eigen::Matrix<typename DerivedFB::Scalar ,Eigen::Dynamic,2> EB;
  51. edges(FB,EB);
  52. Eigen::Matrix<typename DerivedFA::Scalar ,Eigen::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. std::vector<DerivedW> vW(n_ab + n_ba);
  62. std::vector<DerivedG> vG(n_ab + n_ba);
  63. std::vector<DerivedJ> vJ(n_ab + n_ba);
  64. std::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. Eigen::Matrix<typename DerivedJ::Scalar ,Eigen::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. Eigen::Matrix<typename DerivedJ::Scalar ,Eigen::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. offsets[ee+1] = offsets[ee] + vW[ee].rows();
  104. }
  105. // Combine meshes
  106. int n=0,m=0;
  107. std::for_each(vW.begin(),vW.end(),[&n](const DerivedW & w){n+=w.rows();});
  108. std::for_each(vG.begin(),vG.end(),[&m](const DerivedG & g){m+=g.rows();});
  109. assert(n == offsets.back());
  110. W.resize(n,3);
  111. G.resize(m,3);
  112. J.resize(m,2);
  113. {
  114. int m_off = 0,n_off = 0;
  115. for(int i = 0;i<vG.size();i++)
  116. {
  117. W.block(n_off,0,vW[i].rows(),3) = vW[i];
  118. G.block(m_off,0,vG[i].rows(),3) = vG[i].array()+offsets[i];
  119. J.block(m_off,0,vJ[i].rows(),2) = vJ[i];
  120. n_off += vW[i].rows();
  121. m_off += vG[i].rows();
  122. }
  123. assert(n == n_off);
  124. assert(m == m_off);
  125. }
  126. if(resolve_overlaps)
  127. {
  128. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1> SJ;
  129. mesh_boolean(
  130. DerivedW(W),
  131. DerivedG(G),
  132. Eigen::Matrix<typename DerivedW::Scalar ,Eigen::Dynamic ,Eigen::Dynamic>(),
  133. Eigen::Matrix<typename DerivedG::Scalar ,Eigen::Dynamic ,Eigen::Dynamic>(),
  134. MESH_BOOLEAN_TYPE_UNION,
  135. W,
  136. G,
  137. SJ);
  138. J = J(SJ).eval();
  139. }
  140. }
  141. template <
  142. typename DerivedVA,
  143. typename DerivedFA,
  144. typename sType, int sCols, int sOptions,
  145. typename dType, int dCols, int dOptions,
  146. typename DerivedW,
  147. typename DerivedG,
  148. typename DerivedJ>
  149. IGL_INLINE void igl::copyleft::cgal::minkowski_sum(
  150. const Eigen::MatrixBase<DerivedVA> & VA,
  151. const Eigen::MatrixBase<DerivedFA> & FA,
  152. const Eigen::Matrix<sType,1,sCols,sOptions> & s,
  153. const Eigen::Matrix<dType,1,dCols,dOptions> & d,
  154. const bool resolve_overlaps,
  155. Eigen::PlainObjectBase<DerivedW> & W,
  156. Eigen::PlainObjectBase<DerivedG> & G,
  157. Eigen::PlainObjectBase<DerivedJ> & J)
  158. {
  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. Eigen::Array<bool ,Eigen::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 Eigen::Matrix<typename DerivedG::Scalar ,Eigen::Dynamic ,Eigen::Dynamic> MatrixXI;
  220. typedef Eigen::Matrix<typename DerivedG::Scalar ,Eigen::Dynamic,1> VectorXI;
  221. MatrixXI GT(mp+mn,3);
  222. GT<<
  223. FA(igl::find(N),igl::placeholders::all),
  224. (FA.array()+n).eval()(igl::find(P),igl::placeholders::all);
  225. // J indexes FA for parts at s and m+FA for parts at d
  226. J.derived() = igl::LinSpaced<DerivedJ >(m,0,m-1);
  227. DerivedJ JT(mp+mn);
  228. JT <<
  229. J(igl::find(P),igl::placeholders::all),
  230. J(igl::find(N),igl::placeholders::all);
  231. JT.block(mp,0,mn,1).array()+=m;
  232. // Original non-co-planar faces with positively oriented reversed
  233. MatrixXI BA(mp+mn,3);
  234. BA <<
  235. FA(igl::find(P),igl::placeholders::all).rowwise().reverse(),
  236. FA(igl::find(N),igl::placeholders::all);
  237. // Quads along **all** sides
  238. MatrixXI GQ((mp+mn)*3,4);
  239. GQ<<
  240. BA.col(1), BA.col(0), BA.col(0).array()+n, BA.col(1).array()+n,
  241. BA.col(2), BA.col(1), BA.col(1).array()+n, BA.col(2).array()+n,
  242. BA.col(0), BA.col(2), BA.col(2).array()+n, BA.col(0).array()+n;
  243. MatrixXI uGQ;
  244. VectorXI S,sI,sJ;
  245. // Inputs:
  246. // F #F by d list of polygons
  247. // Outputs:
  248. // S #uF list of signed incidences for each unique face
  249. // uF #uF by d list of unique faces
  250. // I #uF index vector so that uF = sort(F,2)(I,:)
  251. // J #F index vector so that sort(F,2) = uF(J,:)
  252. [](
  253. const MatrixXI & F,
  254. VectorXI & S,
  255. MatrixXI & uF,
  256. VectorXI & I,
  257. VectorXI & J)
  258. {
  259. const int m = F.rows();
  260. const int d = F.cols();
  261. MatrixXI sF = F;
  262. const auto MN = sF.rowwise().minCoeff().eval();
  263. // rotate until smallest index is first
  264. for(int p = 0;p<d;p++)
  265. {
  266. for(int f = 0;f<m;f++)
  267. {
  268. if(sF(f,0) != MN(f))
  269. {
  270. for(int r = 0;r<d-1;r++)
  271. {
  272. std::swap(sF(f,r),sF(f,r+1));
  273. }
  274. }
  275. }
  276. }
  277. // swap orienation so that last index is greater than first
  278. for(int f = 0;f<m;f++)
  279. {
  280. if(sF(f,d-1) < sF(f,1))
  281. {
  282. sF.block(f,1,1,d-1) = sF.block(f,1,1,d-1).reverse().eval();
  283. }
  284. }
  285. Eigen::Array<bool ,Eigen::Dynamic,1> M = Eigen::Array<bool ,Eigen::Dynamic,1>::Zero(m,1);
  286. {
  287. VectorXI P = igl::LinSpaced<VectorXI >(d,0,d-1);
  288. for(int p = 0;p<d;p++)
  289. {
  290. for(int f = 0;f<m;f++)
  291. {
  292. bool all = true;
  293. for(int r = 0;r<d;r++)
  294. {
  295. all = all && (sF(f,P(r)) == F(f,r));
  296. }
  297. M(f) = M(f) || all;
  298. }
  299. for(int r = 0;r<d-1;r++)
  300. {
  301. std::swap(P(r),P(r+1));
  302. }
  303. }
  304. }
  305. unique_rows(sF,uF,I,J);
  306. S = VectorXI::Zero(uF.rows(),1);
  307. assert(m == J.rows());
  308. for(int f = 0;f<m;f++)
  309. {
  310. S(J(f)) += M(f) ? 1 : -1;
  311. }
  312. }(MatrixXI(GQ),S,uGQ,sI,sJ);
  313. assert(S.rows() == uGQ.rows());
  314. const int nq = (S.array().abs()==2).count();
  315. GQ.resize(nq,4);
  316. {
  317. int k = 0;
  318. for(int q = 0;q<uGQ.rows();q++)
  319. {
  320. switch(S(q))
  321. {
  322. case -2:
  323. GQ.row(k++) = uGQ.row(q).reverse().eval();
  324. break;
  325. case 2:
  326. GQ.row(k++) = uGQ.row(q);
  327. break;
  328. default:
  329. // do not add
  330. break;
  331. }
  332. }
  333. assert(nq == k);
  334. }
  335. G.resize(GT.rows()+2*GQ.rows(),3);
  336. G<<
  337. GT,
  338. GQ.col(0), GQ.col(1), GQ.col(2),
  339. GQ.col(0), GQ.col(2), GQ.col(3);
  340. J.resize(JT.rows()+2*GQ.rows(),1);
  341. J<<JT,DerivedJ::Constant(2*GQ.rows(),1,2*m+1);
  342. if(resolve_overlaps)
  343. {
  344. Eigen::Matrix<typename DerivedJ::Scalar, Eigen::Dynamic,1> SJ;
  345. mesh_boolean(
  346. DerivedW(W),DerivedG(G),
  347. Eigen::Matrix<typename DerivedVA::Scalar ,Eigen::Dynamic ,Eigen::Dynamic>(),MatrixXI(),
  348. MESH_BOOLEAN_TYPE_UNION,
  349. W,G,SJ);
  350. J = J(SJ).eval();
  351. }
  352. }
  353. template <
  354. typename DerivedVA,
  355. typename DerivedFA,
  356. typename sType, int sCols, int sOptions,
  357. typename dType, int dCols, int dOptions,
  358. typename DerivedW,
  359. typename DerivedG,
  360. typename DerivedJ>
  361. IGL_INLINE void igl::copyleft::cgal::minkowski_sum(
  362. const Eigen::MatrixBase<DerivedVA> & VA,
  363. const Eigen::MatrixBase<DerivedFA> & FA,
  364. const Eigen::Matrix<sType,1,sCols,sOptions> & s,
  365. const Eigen::Matrix<dType,1,dCols,dOptions> & d,
  366. Eigen::PlainObjectBase<DerivedW> & W,
  367. Eigen::PlainObjectBase<DerivedG> & G,
  368. Eigen::PlainObjectBase<DerivedJ> & J)
  369. {
  370. return minkowski_sum(VA,FA,s,d,true,W,G,J);
  371. }
  372. #ifdef IGL_STATIC_LIBRARY
  373. // Explicit template instantiation
  374. template void igl::copyleft::cgal::minkowski_sum<Eigen::Matrix<CGAL::Epeck::FT, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, CGAL::Epeck::FT, 3, 1, CGAL::Epeck::FT, 3, 1, Eigen::Matrix<CGAL::Epeck::FT, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<CGAL::Epeck::FT, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::Matrix<CGAL::Epeck::FT, 1, 3, 1, 1, 3> const&, Eigen::Matrix<CGAL::Epeck::FT, 1, 3, 1, 1, 3> const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Epeck::FT, -1, -1, 1, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  375. template void igl::copyleft::cgal::minkowski_sum<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  376. template void igl::copyleft::cgal::minkowski_sum<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, double, 3, 1, double, 3, 1, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, 1, 3, 1, 1, 3> const&, Eigen::Matrix<double, 1, 3, 1, 1, 3> const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  377. #endif