minkowski_sum.cpp 13 KB

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