minkowski_sum.cpp 13 KB

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