miq.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>, Olga Diamanti <olga.diam@gmail.com>, Kevin Walliman <wkevin@student.ethz.ch>
  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 "miq.h"
  9. #include "../../local_basis.h"
  10. #include "../../triangle_triangle_adjacency.h"
  11. #include "../../cut_mesh.h"
  12. #include "../../LinSpaced.h"
  13. // includes for VertexIndexing
  14. #include "../../HalfEdgeIterator.h"
  15. #include "../../is_border_vertex.h"
  16. #include "../../vertex_triangle_adjacency.h"
  17. // includes for PoissonSolver
  18. #include "../../slice_into.h"
  19. #include "../../grad.h"
  20. #include "../../cotmatrix.h"
  21. #include "../../doublearea.h"
  22. #include <gmm/gmm.h>
  23. #include <CoMISo/Solver/ConstrainedSolver.hh>
  24. #include <CoMISo/Solver/MISolver.hh>
  25. #include <CoMISo/Solver/GMM_Tools.hh>
  26. //
  27. #include "../../cross_field_missmatch.h"
  28. #include "../../comb_frame_field.h"
  29. #include "../../comb_cross_field.h"
  30. #include "../../cut_mesh_from_singularities.h"
  31. #include "../../find_cross_field_singularities.h"
  32. #include "../../compute_frame_field_bisectors.h"
  33. #include "../../rotate_vectors.h"
  34. #ifndef NDEBUG
  35. #include <fstream>
  36. #endif
  37. #include <iostream>
  38. #include "../../matlab_format.h"
  39. #define DEBUGPRINT 0
  40. namespace igl {
  41. namespace copyleft {
  42. namespace comiso {
  43. struct SeamInfo
  44. {
  45. int v0,v0p;
  46. int integerVar;
  47. unsigned char MMatch;
  48. IGL_INLINE SeamInfo(int _v0,
  49. int _v0p,
  50. int _MMatch,
  51. int _integerVar);
  52. IGL_INLINE SeamInfo(const SeamInfo &S1);
  53. };
  54. struct MeshSystemInfo
  55. {
  56. ////number of vertices variables
  57. int num_vert_variables;
  58. ///num of integer for cuts
  59. int num_integer_cuts;
  60. ///this are used for drawing purposes
  61. std::vector<SeamInfo> EdgeSeamInfo;
  62. };
  63. template <typename DerivedV, typename DerivedF>
  64. class VertexIndexing
  65. {
  66. public:
  67. // Input:
  68. const Eigen::PlainObjectBase<DerivedV> &V;
  69. const Eigen::PlainObjectBase<DerivedF> &F;
  70. const Eigen::PlainObjectBase<DerivedV> &Vcut;
  71. const Eigen::PlainObjectBase<DerivedF> &Fcut;
  72. const Eigen::PlainObjectBase<DerivedF> &TT;
  73. const Eigen::PlainObjectBase<DerivedF> &TTi;
  74. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_MMatch;
  75. const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_Singular; // bool
  76. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_Seams; // 3 bool
  77. ///this handle for mesh TODO: move with the other global variables
  78. MeshSystemInfo Handle_SystemInfo;
  79. IGL_INLINE VertexIndexing(const Eigen::PlainObjectBase<DerivedV> &_V,
  80. const Eigen::PlainObjectBase<DerivedF> &_F,
  81. const Eigen::PlainObjectBase<DerivedV> &_Vcut,
  82. const Eigen::PlainObjectBase<DerivedF> &_Fcut,
  83. const Eigen::PlainObjectBase<DerivedF> &_TT,
  84. const Eigen::PlainObjectBase<DerivedF> &_TTi,
  85. const Eigen::Matrix<int, Eigen::Dynamic, 3> &_Handle_MMatch,
  86. const Eigen::Matrix<int, Eigen::Dynamic, 1> &_Handle_Singular,
  87. const Eigen::Matrix<int, Eigen::Dynamic, 3> &_Handle_Seams
  88. );
  89. // provide information about every vertex per seam
  90. IGL_INLINE void InitSeamInfo();
  91. private:
  92. struct VertexInfo{
  93. int v; // vertex index (according to V)
  94. int f0, k0; // face and local edge information of the edge that connects this vertex to the previous vertex (previous in the vector)
  95. int f1, k1; // face and local edge information of the other face corresponding to the same edge
  96. VertexInfo(int _v, int _f0, int _k0, int _f1, int _k1) :
  97. v(_v), f0(_f0), k0(_k0), f1(_f1), k1(_k1){}
  98. bool operator==(VertexInfo const& other){
  99. return other.v == v;
  100. }
  101. };
  102. IGL_INLINE void GetSeamInfo(const int f0,
  103. const int f1,
  104. const int indexE,
  105. int &v0,int &v1,
  106. int &v0p,int &v1p,
  107. unsigned char &_MMatch);
  108. IGL_INLINE std::vector<std::vector<VertexInfo> > GetVerticesPerSeam();
  109. };
  110. template <typename DerivedV, typename DerivedF>
  111. class PoissonSolver
  112. {
  113. public:
  114. IGL_INLINE void SolvePoisson(Eigen::VectorXd Stiffness,
  115. double vector_field_scale=0.1f,
  116. double grid_res=1.f,
  117. bool direct_round=true,
  118. int localIter=0,
  119. bool _integer_rounding=true,
  120. bool _singularity_rounding=true,
  121. std::vector<int> roundVertices = std::vector<int>(),
  122. std::vector<std::vector<int> > hardFeatures = std::vector<std::vector<int> >());
  123. IGL_INLINE PoissonSolver(const Eigen::PlainObjectBase<DerivedV> &_V,
  124. const Eigen::PlainObjectBase<DerivedF> &_F,
  125. const Eigen::PlainObjectBase<DerivedV> &_Vcut,
  126. const Eigen::PlainObjectBase<DerivedF> &_Fcut,
  127. const Eigen::PlainObjectBase<DerivedF> &_TT,
  128. const Eigen::PlainObjectBase<DerivedF> &_TTi,
  129. const Eigen::PlainObjectBase<DerivedV> &_PD1,
  130. const Eigen::PlainObjectBase<DerivedV> &_PD2,
  131. const Eigen::Matrix<int, Eigen::Dynamic, 1>&_Handle_Singular,
  132. const MeshSystemInfo &_Handle_SystemInfo
  133. );
  134. const Eigen::PlainObjectBase<DerivedV> &V;
  135. const Eigen::PlainObjectBase<DerivedF> &F;
  136. const Eigen::PlainObjectBase<DerivedV> &Vcut;
  137. const Eigen::PlainObjectBase<DerivedF> &Fcut;
  138. const Eigen::PlainObjectBase<DerivedF> &TT;
  139. const Eigen::PlainObjectBase<DerivedF> &TTi;
  140. const Eigen::PlainObjectBase<DerivedV> &PD1;
  141. const Eigen::PlainObjectBase<DerivedV> &PD2;
  142. const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_Singular; // bool
  143. const MeshSystemInfo &Handle_SystemInfo;
  144. // Internal:
  145. Eigen::VectorXd Handle_Stiffness;
  146. std::vector<std::vector<int> > VF;
  147. std::vector<std::vector<int> > VFi;
  148. Eigen::MatrixXd UV; // this is probably useless
  149. // Output:
  150. // per wedge UV coordinates, 6 coordinates (1 face) per row
  151. Eigen::MatrixXd WUV;
  152. // per vertex UV coordinates, Vcut.rows() x 2
  153. Eigen::MatrixXd UV_out;
  154. // Matrices
  155. Eigen::SparseMatrix<double> Lhs;
  156. Eigen::SparseMatrix<double> Constraints;
  157. Eigen::VectorXd rhs;
  158. Eigen::VectorXd constraints_rhs;
  159. ///vector of unknowns
  160. std::vector< double > X;
  161. ////REAL PART
  162. ///number of fixed vertex
  163. unsigned int n_fixed_vars;
  164. ///the number of REAL variables for vertices
  165. unsigned int n_vert_vars;
  166. ///total number of variables of the system,
  167. ///do not consider constraints, but consider integer vars
  168. unsigned int num_total_vars;
  169. //////INTEGER PART
  170. ///the total number of integer variables
  171. unsigned int n_integer_vars;
  172. ///CONSTRAINT PART
  173. ///number of cuts constraints
  174. unsigned int num_cut_constraint;
  175. // number of user-defined constraints
  176. unsigned int num_userdefined_constraint;
  177. ///total number of constraints equations
  178. unsigned int num_constraint_equations;
  179. ///vector of blocked vertices
  180. std::vector<int> Hard_constraints;
  181. ///vector of indexes to round
  182. std::vector<int> ids_to_round;
  183. ///vector of indexes to round
  184. std::vector<std::vector<int > > userdefined_constraints;
  185. ///boolean that is true if rounding to integer is needed
  186. bool integer_rounding;
  187. ///START COMMON MATH FUNCTIONS
  188. ///return the complex encoding the rotation
  189. ///for a given missmatch interval
  190. IGL_INLINE std::complex<double> GetRotationComplex(int interval);
  191. ///END COMMON MATH FUNCTIONS
  192. ///START FIXING VERTICES
  193. ///set a given vertex as fixed
  194. IGL_INLINE void AddFixedVertex(int v);
  195. ///find vertex to fix in case we're using
  196. ///a vector field NB: multiple components not handled
  197. IGL_INLINE void FindFixedVertField();
  198. ///find hard constraint depending if using or not
  199. ///a vector field
  200. IGL_INLINE void FindFixedVert();
  201. IGL_INLINE int GetFirstVertexIndex(int v);
  202. ///fix the vertices which are flagged as fixed
  203. IGL_INLINE void FixBlockedVertex();
  204. ///END FIXING VERTICES
  205. ///HANDLING SINGULARITY
  206. //set the singularity round to integer location
  207. IGL_INLINE void AddSingularityRound();
  208. IGL_INLINE void AddToRoundVertices(std::vector<int> ids);
  209. ///START GENERIC SYSTEM FUNCTIONS
  210. //build the laplacian matrix cyclyng over all rangemaps
  211. //and over all faces
  212. IGL_INLINE void BuildLaplacianMatrix(double vfscale=1);
  213. ///find different sized of the system
  214. IGL_INLINE void FindSizes();
  215. IGL_INLINE void AllocateSystem();
  216. ///intitialize the whole matrix
  217. IGL_INLINE void InitMatrix();
  218. ///map back coordinates after that
  219. ///the system has been solved
  220. IGL_INLINE void MapCoords();
  221. ///END GENERIC SYSTEM FUNCTIONS
  222. ///set the constraints for the inter-range cuts
  223. IGL_INLINE void BuildSeamConstraintsExplicitTranslation();
  224. ///set the constraints for the inter-range cuts
  225. IGL_INLINE void BuildUserDefinedConstraints();
  226. ///call of the mixed integer solver
  227. IGL_INLINE void MixedIntegerSolve(double cone_grid_res=1,
  228. bool direct_round=true,
  229. int localIter=0);
  230. IGL_INLINE void clearUserConstraint();
  231. IGL_INLINE void addSharpEdgeConstraint(int fid, int vid);
  232. };
  233. template <typename DerivedV, typename DerivedF, typename DerivedU>
  234. class MIQ_class
  235. {
  236. private:
  237. const Eigen::PlainObjectBase<DerivedV> &V;
  238. const Eigen::PlainObjectBase<DerivedF> &F;
  239. DerivedV Vcut;
  240. DerivedF Fcut;
  241. Eigen::MatrixXd UV_out;
  242. DerivedF FUV_out;
  243. // internal
  244. DerivedF TT;
  245. DerivedF TTi;
  246. // Stiffness per face
  247. Eigen::VectorXd Handle_Stiffness;
  248. DerivedV B1, B2, B3;
  249. public:
  250. IGL_INLINE MIQ_class(const Eigen::PlainObjectBase<DerivedV> &V_,
  251. const Eigen::PlainObjectBase<DerivedF> &F_,
  252. const Eigen::PlainObjectBase<DerivedV> &PD1_combed,
  253. const Eigen::PlainObjectBase<DerivedV> &PD2_combed,
  254. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_MMatch,
  255. const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_Singular,
  256. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_Seams,
  257. Eigen::PlainObjectBase<DerivedU> &UV,
  258. Eigen::PlainObjectBase<DerivedF> &FUV,
  259. double GradientSize = 30.0,
  260. double Stiffness = 5.0,
  261. bool DirectRound = false,
  262. int iter = 5,
  263. int localIter = 5,
  264. bool DoRound = true,
  265. bool SingularityRound=true,
  266. std::vector<int> roundVertices = std::vector<int>(),
  267. std::vector<std::vector<int> > hardFeatures = std::vector<std::vector<int> >());
  268. IGL_INLINE void extractUV(Eigen::PlainObjectBase<DerivedU> &UV_out,
  269. Eigen::PlainObjectBase<DerivedF> &FUV_out);
  270. private:
  271. IGL_INLINE int NumFlips(const Eigen::MatrixXd& WUV);
  272. IGL_INLINE double Distortion(int f, double h, const Eigen::MatrixXd& WUV);
  273. IGL_INLINE double LaplaceDistortion(const int f, double h, const Eigen::MatrixXd& WUV);
  274. IGL_INLINE bool updateStiffeningJacobianDistorsion(double grad_size, const Eigen::MatrixXd& WUV);
  275. IGL_INLINE bool IsFlipped(const Eigen::Vector2d &uv0,
  276. const Eigen::Vector2d &uv1,
  277. const Eigen::Vector2d &uv2);
  278. IGL_INLINE bool IsFlipped(const int i, const Eigen::MatrixXd& WUV);
  279. };
  280. };
  281. };
  282. }
  283. IGL_INLINE igl::copyleft::comiso::SeamInfo::SeamInfo(int _v0,
  284. int _v0p,
  285. int _MMatch,
  286. int _integerVar)
  287. {
  288. v0=_v0;
  289. v0p=_v0p;
  290. integerVar=_integerVar;
  291. MMatch=_MMatch;
  292. }
  293. IGL_INLINE igl::copyleft::comiso::SeamInfo::SeamInfo(const SeamInfo &S1)
  294. {
  295. v0=S1.v0;
  296. v0p=S1.v0p;
  297. integerVar=S1.integerVar;
  298. MMatch=S1.MMatch;
  299. }
  300. template <typename DerivedV, typename DerivedF>
  301. IGL_INLINE igl::copyleft::comiso::VertexIndexing<DerivedV, DerivedF>::VertexIndexing(const Eigen::PlainObjectBase<DerivedV> &_V,
  302. const Eigen::PlainObjectBase<DerivedF> &_F,
  303. const Eigen::PlainObjectBase<DerivedV> &_Vcut,
  304. const Eigen::PlainObjectBase<DerivedF> &_Fcut,
  305. const Eigen::PlainObjectBase<DerivedF> &_TT,
  306. const Eigen::PlainObjectBase<DerivedF> &_TTi,
  307. const Eigen::Matrix<int, Eigen::Dynamic, 3> &_Handle_MMatch,
  308. const Eigen::Matrix<int, Eigen::Dynamic, 1> &_Handle_Singular,
  309. const Eigen::Matrix<int, Eigen::Dynamic, 3> &_Handle_Seams
  310. ):
  311. V(_V),
  312. F(_F),
  313. Vcut(_Vcut),
  314. Fcut(_Fcut),
  315. TT(_TT),
  316. TTi(_TTi),
  317. Handle_MMatch(_Handle_MMatch),
  318. Handle_Singular(_Handle_Singular),
  319. Handle_Seams(_Handle_Seams)
  320. {
  321. #ifdef DEBUG_PRINT
  322. cerr<<igl::matlab_format(Handle_Seams,"Handle_Seams");
  323. #endif
  324. Handle_SystemInfo.num_vert_variables=Vcut.rows();
  325. Handle_SystemInfo.num_integer_cuts=0;
  326. }
  327. template <typename DerivedV, typename DerivedF>
  328. IGL_INLINE void igl::copyleft::comiso::VertexIndexing<DerivedV, DerivedF>::GetSeamInfo(const int f0,
  329. const int f1,
  330. const int indexE,
  331. int &v0,int &v1,
  332. int &v0p,int &v1p,
  333. unsigned char &_MMatch)
  334. {
  335. int edgef0 = indexE;
  336. v0 = Fcut(f0,edgef0);
  337. v1 = Fcut(f0,(edgef0+1)%3);
  338. ////get the index on opposite side
  339. assert(TT(f0,edgef0) == f1);
  340. int edgef1 = TTi(f0,edgef0);
  341. v1p = Fcut(f1,edgef1);
  342. v0p = Fcut(f1,(edgef1+1)%3);
  343. _MMatch = Handle_MMatch(f0,edgef0);
  344. assert(F(f0,edgef0) == F(f1,((edgef1+1)%3)));
  345. assert(F(f0,((edgef0+1)%3)) == F(f1,edgef1));
  346. }
  347. template <typename DerivedV, typename DerivedF>
  348. IGL_INLINE std::vector<std::vector<typename igl::copyleft::comiso::VertexIndexing<DerivedV, DerivedF>::VertexInfo> > igl::copyleft::comiso::VertexIndexing<DerivedV, DerivedF>::GetVerticesPerSeam()
  349. {
  350. // Return value
  351. std::vector<std::vector<VertexInfo> >verticesPerSeam;
  352. // for every vertex, keep track of their adjacent vertices on seams.
  353. // regular vertices have two neighbors on a seam, start- and endvertices may have any other numbers of neighbors (e.g. 1 or 3)
  354. std::vector<std::list<VertexInfo> > VVSeam(V.rows());
  355. Eigen::MatrixXi F_hit = Eigen::MatrixXi::Zero(F.rows(), 3);
  356. for (unsigned int f=0; f<F.rows();f++)
  357. {
  358. int f0 = f;
  359. for(int k0=0; k0<3; k0++){
  360. int f1 = TT(f0,k0);
  361. if(f1 == -1)
  362. continue;
  363. bool seam = Handle_Seams(f0,k0);
  364. if (seam && F_hit(f0,k0) == 0)
  365. {
  366. int v0 = F(f0, k0);
  367. int v1 = F(f0, (k0+1)%3);
  368. int k1 = TTi(f0,k0);
  369. VVSeam[v0].push_back(VertexInfo(v1, f0, k0, f1, k1));
  370. VVSeam[v1].push_back(VertexInfo(v0, f0, k0, f1, k1));
  371. F_hit(f0, k0) = 1;
  372. F_hit(f1, k1) = 1;
  373. }
  374. }
  375. }
  376. // Find start vertices, i.e. vertices that start or end a seam branch
  377. std::vector<int> startVertexIndices;
  378. std::vector<bool> isStartVertex(V.rows());
  379. for (unsigned int i=0;i<V.rows();i++)
  380. {
  381. isStartVertex[i] = false;
  382. // vertices with two neighbors are regular vertices, unless the vertex is a singularity, in which case it qualifies as a start vertex
  383. if (VVSeam[i].size() > 0 && VVSeam[i].size() != 2 || Handle_Singular(i) == true)
  384. {
  385. startVertexIndices.push_back(i);
  386. isStartVertex[i] = true;
  387. }
  388. }
  389. // For each startVertex, walk along its seam
  390. for (unsigned int i=0;i<startVertexIndices.size();i++)
  391. {
  392. auto startVertexNeighbors = &VVSeam[startVertexIndices[i]];
  393. const int neighborSize = startVertexNeighbors->size();
  394. // explore every seam to which this vertex is a start vertex
  395. // note: a vertex can never be a start vertex and a regular vertex simultaneously
  396. for (unsigned int j=0;j<neighborSize;j++)
  397. {
  398. std::vector<VertexInfo> thisSeam; // temporary container
  399. // Create vertexInfo struct for start vertex
  400. auto startVertex = VertexInfo(startVertexIndices[i], -1, -1, -1, -1);// -1 values are arbitrary (will never be used)
  401. auto currentVertex = startVertex;
  402. // Add start vertex to the seam
  403. thisSeam.push_back(currentVertex);
  404. // advance on the seam
  405. auto currentVertexNeighbors = startVertexNeighbors;
  406. auto nextVertex = currentVertexNeighbors->front();
  407. currentVertexNeighbors->pop_front();
  408. auto prevVertex = startVertex; // bogus initialization to get the type
  409. while (true)
  410. {
  411. // move to the next vertex
  412. prevVertex = currentVertex;
  413. currentVertex = nextVertex;
  414. currentVertexNeighbors = &VVSeam[nextVertex.v];
  415. // add current vertex to this seam
  416. thisSeam.push_back(currentVertex);
  417. // remove the previous vertex
  418. auto it = std::find(currentVertexNeighbors->begin(), currentVertexNeighbors->end(), prevVertex);
  419. assert(it != currentVertexNeighbors->end());
  420. currentVertexNeighbors->erase(it);
  421. if (currentVertexNeighbors->size() == 1 && !isStartVertex[currentVertex.v])
  422. {
  423. nextVertex = currentVertexNeighbors->front();
  424. currentVertexNeighbors->pop_front();
  425. }
  426. else
  427. break;
  428. }
  429. verticesPerSeam.push_back(thisSeam);
  430. }
  431. }
  432. return verticesPerSeam;
  433. }
  434. template <typename DerivedV, typename DerivedF>
  435. IGL_INLINE void igl::copyleft::comiso::VertexIndexing<DerivedV, DerivedF>::InitSeamInfo()
  436. {
  437. auto verticesPerSeam = GetVerticesPerSeam();
  438. Handle_SystemInfo.EdgeSeamInfo.clear();
  439. int integerVar = 0;
  440. // Loop over each seam
  441. for(auto seam : verticesPerSeam){
  442. //choose initial side of the seam such that the start vertex corresponds to Fcut(f, k) and the end vertex corresponds to Fcut(f, (k+1)%3) and not vice versa.
  443. int priorVertexIdx;
  444. if(seam.size() > 2){
  445. auto v1 = seam[1];
  446. auto v2 = seam[2];
  447. if(Fcut(v1.f0, (v1.k0+1) % 3) == Fcut(v2.f0, v2.k0) || Fcut(v1.f0, (v1.k0+1) % 3) == Fcut(v2.f1, v2.k1)){
  448. priorVertexIdx = Fcut(v1.f0, v1.k0);
  449. }
  450. else{
  451. priorVertexIdx = Fcut(v1.f1, v1.k1);
  452. assert(Fcut(v1.f1, (v1.k1+1) % 3) == Fcut(v2.f0, v2.k0) || Fcut(v1.f1, (v1.k1+1) % 3) == Fcut(v2.f1, v2.k1));
  453. }
  454. }
  455. else{
  456. auto v1 = seam[1];
  457. priorVertexIdx = Fcut(v1.f0, v1.k0);
  458. }
  459. // Loop over each vertex of the seam
  460. for(auto it=seam.begin()+1; it != seam.end(); ++it){
  461. auto vertex = *it;
  462. // choose the correct side of the seam
  463. int f,k,ff,kk;
  464. if(priorVertexIdx == Fcut(vertex.f0, vertex.k0)){
  465. f = vertex.f0; ff = vertex.f1;
  466. k = vertex.k0; kk = vertex.k1;
  467. }
  468. else{
  469. f = vertex.f1; ff = vertex.f0;
  470. k = vertex.k1; kk = vertex.k0;
  471. assert(priorVertexIdx == Fcut(vertex.f1, vertex.k1));
  472. }
  473. int vtx0,vtx0p,vtx1,vtx1p;
  474. unsigned char MM;
  475. GetSeamInfo(f,ff,k,vtx0,vtx1,vtx0p,vtx1p,MM);
  476. Handle_SystemInfo.EdgeSeamInfo.push_back(SeamInfo(vtx0,vtx0p,MM,integerVar));
  477. if(it == seam.end() -1){
  478. Handle_SystemInfo.EdgeSeamInfo.push_back(SeamInfo(vtx1,vtx1p,MM,integerVar));
  479. }
  480. priorVertexIdx = vtx1;
  481. }
  482. // use the same integer for each seam
  483. integerVar++;
  484. }
  485. Handle_SystemInfo.num_integer_cuts = integerVar;
  486. #ifndef NDEBUG
  487. int totalNVerticesOnSeams = 0;
  488. for(auto seam : verticesPerSeam){
  489. totalNVerticesOnSeams += seam.size();
  490. }
  491. assert(Handle_SystemInfo.EdgeSeamInfo.size() == totalNVerticesOnSeams);
  492. #endif
  493. }
  494. template <typename DerivedV, typename DerivedF>
  495. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::SolvePoisson(Eigen::VectorXd Stiffness,
  496. double vector_field_scale,
  497. double grid_res,
  498. bool direct_round,
  499. int localIter,
  500. bool _integer_rounding,
  501. bool _singularity_rounding,
  502. std::vector<int> roundVertices,
  503. std::vector<std::vector<int> > hardFeatures)
  504. {
  505. Handle_Stiffness = Stiffness;
  506. //initialization of flags and data structures
  507. integer_rounding=_integer_rounding;
  508. ids_to_round.clear();
  509. clearUserConstraint();
  510. // copy the user constraints number
  511. for (size_t i = 0; i < hardFeatures.size(); ++i)
  512. {
  513. addSharpEdgeConstraint(hardFeatures[i][0],hardFeatures[i][1]);
  514. }
  515. ///Initializing Matrix
  516. int t0=clock();
  517. ///initialize the matrix ALLOCATING SPACE
  518. InitMatrix();
  519. if (DEBUGPRINT)
  520. printf("\n ALLOCATED THE MATRIX \n");
  521. ///build the laplacian system
  522. BuildLaplacianMatrix(vector_field_scale);
  523. // add seam constraints
  524. BuildSeamConstraintsExplicitTranslation();
  525. // add user defined constraints
  526. BuildUserDefinedConstraints();
  527. ////add the lagrange multiplier
  528. FixBlockedVertex();
  529. if (DEBUGPRINT)
  530. printf("\n BUILT THE MATRIX \n");
  531. if (integer_rounding)
  532. AddToRoundVertices(roundVertices);
  533. if (_singularity_rounding)
  534. AddSingularityRound();
  535. int t1=clock();
  536. if (DEBUGPRINT) printf("\n time:%d \n",t1-t0);
  537. if (DEBUGPRINT) printf("\n SOLVING \n");
  538. MixedIntegerSolve(grid_res,direct_round,localIter);
  539. int t2=clock();
  540. if (DEBUGPRINT) printf("\n time:%d \n",t2-t1);
  541. if (DEBUGPRINT) printf("\n ASSIGNING COORDS \n");
  542. MapCoords();
  543. int t3=clock();
  544. if (DEBUGPRINT) printf("\n time:%d \n",t3-t2);
  545. if (DEBUGPRINT) printf("\n FINISHED \n");
  546. }
  547. template <typename DerivedV, typename DerivedF>
  548. IGL_INLINE igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>
  549. ::PoissonSolver(const Eigen::PlainObjectBase<DerivedV> &_V,
  550. const Eigen::PlainObjectBase<DerivedF> &_F,
  551. const Eigen::PlainObjectBase<DerivedV> &_Vcut,
  552. const Eigen::PlainObjectBase<DerivedF> &_Fcut,
  553. const Eigen::PlainObjectBase<DerivedF> &_TT,
  554. const Eigen::PlainObjectBase<DerivedF> &_TTi,
  555. const Eigen::PlainObjectBase<DerivedV> &_PD1,
  556. const Eigen::PlainObjectBase<DerivedV> &_PD2,
  557. const Eigen::Matrix<int, Eigen::Dynamic, 1>&_Handle_Singular,
  558. const MeshSystemInfo &_Handle_SystemInfo
  559. ):
  560. V(_V),
  561. F(_F),
  562. Vcut(_Vcut),
  563. Fcut(_Fcut),
  564. TT(_TT),
  565. TTi(_TTi),
  566. PD1(_PD1),
  567. PD2(_PD2),
  568. Handle_Singular(_Handle_Singular),
  569. Handle_SystemInfo(_Handle_SystemInfo)
  570. {
  571. UV = Eigen::MatrixXd(V.rows(),2);
  572. WUV = Eigen::MatrixXd(F.rows(),6);
  573. UV_out = Eigen::MatrixXd(Vcut.rows(),2);
  574. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  575. }
  576. ///START COMMON MATH FUNCTIONS
  577. ///return the complex encoding the rotation
  578. ///for a given missmatch interval
  579. template <typename DerivedV, typename DerivedF>
  580. IGL_INLINE std::complex<double> igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::GetRotationComplex(int interval)
  581. {
  582. assert((interval>=0)&&(interval<4));
  583. switch(interval)
  584. {
  585. case 0:return std::complex<double>(1,0);
  586. case 1:return std::complex<double>(0,1);
  587. case 2:return std::complex<double>(-1,0);
  588. default:return std::complex<double>(0,-1);
  589. }
  590. }
  591. ///END COMMON MATH FUNCTIONS
  592. ///START FIXING VERTICES
  593. ///set a given vertex as fixed
  594. template <typename DerivedV, typename DerivedF>
  595. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::AddFixedVertex(int v)
  596. {
  597. n_fixed_vars++;
  598. Hard_constraints.push_back(v);
  599. }
  600. ///find vertex to fix in case we're using
  601. ///a vector field NB: multiple components not handled
  602. template <typename DerivedV, typename DerivedF>
  603. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::FindFixedVertField()
  604. {
  605. Hard_constraints.clear();
  606. n_fixed_vars=0;
  607. //fix the first singularity
  608. for (unsigned int v=0;v<V.rows();v++)
  609. {
  610. if (Handle_Singular(v))
  611. {
  612. AddFixedVertex(v);
  613. UV.row(v) << 0,0;
  614. return;
  615. }
  616. }
  617. ///if anything fixed fix the first
  618. AddFixedVertex(0);
  619. UV.row(0) << 0,0;
  620. std::cerr << "No vertices to fix, I am fixing the first vertex to the origin!" << std::endl;
  621. }
  622. ///find hard constraint depending if using or not
  623. ///a vector field
  624. template <typename DerivedV, typename DerivedF>
  625. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::FindFixedVert()
  626. {
  627. Hard_constraints.clear();
  628. FindFixedVertField();
  629. }
  630. template <typename DerivedV, typename DerivedF>
  631. IGL_INLINE int igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::GetFirstVertexIndex(int v)
  632. {
  633. return Fcut(VF[v][0],VFi[v][0]);
  634. }
  635. ///fix the vertices which are flagged as fixed
  636. template <typename DerivedV, typename DerivedF>
  637. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::FixBlockedVertex()
  638. {
  639. int offset_row = num_cut_constraint*2;
  640. unsigned int constr_num = 0;
  641. for (unsigned int i=0;i<Hard_constraints.size();i++)
  642. {
  643. int v = Hard_constraints[i];
  644. ///get first index of the vertex that must blocked
  645. //int index=v->vertex_index[0];
  646. int index = GetFirstVertexIndex(v);
  647. ///multiply times 2 because of uv
  648. int indexvert = index*2;
  649. ///find the first free row to add the constraint
  650. int indexRow = (offset_row+constr_num*2);
  651. int indexCol = indexRow;
  652. ///add fixing constraint LHS
  653. Constraints.coeffRef(indexRow, indexvert) += 1;
  654. Constraints.coeffRef(indexRow+1,indexvert+1) += 1;
  655. ///add fixing constraint RHS
  656. constraints_rhs[indexCol] = UV(v,0);
  657. constraints_rhs[indexCol+1] = UV(v,1);
  658. constr_num++;
  659. }
  660. assert(constr_num==n_fixed_vars);
  661. }
  662. ///END FIXING VERTICES
  663. ///HANDLING SINGULARITY
  664. //set the singularity round to integer location
  665. template <typename DerivedV, typename DerivedF>
  666. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::AddSingularityRound()
  667. {
  668. for (unsigned int v=0;v<V.rows();v++)
  669. {
  670. if (Handle_Singular(v))
  671. {
  672. int index0=GetFirstVertexIndex(v);
  673. ids_to_round.push_back( index0*2 );
  674. ids_to_round.push_back((index0*2)+1);
  675. }
  676. }
  677. }
  678. template <typename DerivedV, typename DerivedF>
  679. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::AddToRoundVertices(std::vector<int> ids)
  680. {
  681. for (size_t i = 0; i < ids.size(); ++i)
  682. {
  683. if (ids[i] < 0 || ids[i] >= V.rows())
  684. std::cerr << "WARNING: Ignored round vertex constraint, vertex " << ids[i] << " does not exist in the mesh." << std::endl;
  685. int index0 = GetFirstVertexIndex(ids[i]);
  686. ids_to_round.push_back( index0*2 );
  687. ids_to_round.push_back((index0*2)+1);
  688. }
  689. }
  690. ///START GENERIC SYSTEM FUNCTIONS
  691. template <typename DerivedV, typename DerivedF>
  692. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::BuildLaplacianMatrix(double vfscale)
  693. {
  694. Eigen::VectorXi idx = igl::LinSpaced<Eigen::VectorXi >(Vcut.rows(), 0, 2*Vcut.rows()-2);
  695. Eigen::VectorXi idx2 = igl::LinSpaced<Eigen::VectorXi >(Vcut.rows(), 1, 2*Vcut.rows()-1);
  696. // get gradient matrix
  697. Eigen::SparseMatrix<double> G(Fcut.rows() * 3, Vcut.rows());
  698. igl::grad(Vcut, Fcut, G);
  699. // get triangle weights
  700. Eigen::VectorXd dblA(Fcut.rows());
  701. igl::doublearea(Vcut, Fcut, dblA);
  702. // compute intermediate result
  703. Eigen::SparseMatrix<double> G2;
  704. G2 = G.transpose() * dblA.replicate<3,1>().asDiagonal() * Handle_Stiffness.replicate<3,1>().asDiagonal();
  705. /// Compute LHS
  706. Eigen::SparseMatrix<double> Cotmatrix;
  707. Cotmatrix = 0.5 * G2 * G;
  708. igl::slice_into(Cotmatrix, idx, idx, Lhs);
  709. igl::slice_into(Cotmatrix, idx2, idx2, Lhs);
  710. /// Compute RHS
  711. // reshape nrosy vectors
  712. const Eigen::MatrixXd u = Eigen::Map<const Eigen::MatrixXd>(PD1.data(),Fcut.rows()*3,1); // this mimics a reshape at the cost of a copy.
  713. const Eigen::MatrixXd v = Eigen::Map<const Eigen::MatrixXd>(PD2.data(),Fcut.rows()*3,1); // this mimics a reshape at the cost of a copy.
  714. // multiply with weights
  715. Eigen::VectorXd rhs1 = G2 * u * 0.5 * vfscale;
  716. Eigen::VectorXd rhs2 = -G2 * v * 0.5 * vfscale;
  717. igl::slice_into(rhs1, idx, 1, rhs);
  718. igl::slice_into(rhs2, idx2, 1, rhs);
  719. }
  720. ///find different sized of the system
  721. template <typename DerivedV, typename DerivedF>
  722. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::FindSizes()
  723. {
  724. ///find the vertex that need to be fixed
  725. FindFixedVert();
  726. ///REAL PART
  727. n_vert_vars = Handle_SystemInfo.num_vert_variables;
  728. ///INTEGER PART
  729. ///the total number of integer variables
  730. n_integer_vars = Handle_SystemInfo.num_integer_cuts;
  731. ///CONSTRAINT PART
  732. num_cut_constraint = Handle_SystemInfo.EdgeSeamInfo.size();
  733. num_constraint_equations = num_cut_constraint * 2 + n_fixed_vars * 2 + num_userdefined_constraint;
  734. ///total variable of the system
  735. num_total_vars = (n_vert_vars+n_integer_vars) * 2;
  736. ///initialize matrix size
  737. if (DEBUGPRINT) printf("\n*** SYSTEM VARIABLES *** \n");
  738. if (DEBUGPRINT) printf("* NUM REAL VERTEX VARIABLES %d \n",n_vert_vars);
  739. if (DEBUGPRINT) printf("\n*** INTEGER VARIABLES *** \n");
  740. if (DEBUGPRINT) printf("* NUM INTEGER VARIABLES %d \n",(int)n_integer_vars);
  741. if (DEBUGPRINT) printf("\n*** CONSTRAINTS *** \n ");
  742. if (DEBUGPRINT) printf("* NUM FIXED CONSTRAINTS %d\n",n_fixed_vars);
  743. if (DEBUGPRINT) printf("* NUM CUTS CONSTRAINTS %d\n",num_cut_constraint);
  744. if (DEBUGPRINT) printf("* NUM USER DEFINED CONSTRAINTS %d\n",num_userdefined_constraint);
  745. if (DEBUGPRINT) printf("\n*** TOTAL SIZE *** \n");
  746. if (DEBUGPRINT) printf("* TOTAL VARIABLE SIZE (WITH INTEGER TRASL) %d \n",num_total_vars);
  747. if (DEBUGPRINT) printf("* TOTAL CONSTRAINTS %d \n",num_constraint_equations);
  748. }
  749. template <typename DerivedV, typename DerivedF>
  750. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::AllocateSystem()
  751. {
  752. Lhs.resize(n_vert_vars * 2, n_vert_vars * 2);
  753. Constraints.resize(num_constraint_equations, num_total_vars);
  754. rhs.resize(n_vert_vars * 2);
  755. constraints_rhs.resize(num_constraint_equations);
  756. printf("\n INITIALIZED SPARSE MATRIX OF %d x %d \n",n_vert_vars*2, n_vert_vars*2);
  757. printf("\n INITIALIZED SPARSE MATRIX OF %d x %d \n",num_constraint_equations, num_total_vars);
  758. printf("\n INITIALIZED VECTOR OF %d x 1 \n",n_vert_vars*2);
  759. printf("\n INITIALIZED VECTOR OF %d x 1 \n",num_constraint_equations);
  760. }
  761. ///intitialize the whole matrix
  762. template <typename DerivedV, typename DerivedF>
  763. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::InitMatrix()
  764. {
  765. FindSizes();
  766. AllocateSystem();
  767. }
  768. ///map back coordinates after that
  769. ///the system has been solved
  770. template <typename DerivedV, typename DerivedF>
  771. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::MapCoords()
  772. {
  773. ///map coords to faces
  774. for (unsigned int f=0;f<Fcut.rows();f++)
  775. {
  776. for (int k=0;k<3;k++)
  777. {
  778. //get the index of the variable in the system
  779. int indexUV = Fcut(f,k);
  780. ///then get U and V coords
  781. double U=X[indexUV*2];
  782. double V=X[indexUV*2+1];
  783. WUV(f,k*2 + 0) = U;
  784. WUV(f,k*2 + 1) = V;
  785. }
  786. }
  787. for(int i = 0; i < Vcut.rows(); i++){
  788. UV_out(i,0) = X[i*2];
  789. UV_out(i,1) = X[i*2+1];
  790. }
  791. }
  792. ///END GENERIC SYSTEM FUNCTIONS
  793. ///set the constraints for the inter-range cuts
  794. template <typename DerivedV, typename DerivedF>
  795. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::BuildSeamConstraintsExplicitTranslation()
  796. {
  797. ///current constraint row
  798. int constr_row = 0;
  799. for (unsigned int i=0; i<num_cut_constraint; i++)
  800. {
  801. unsigned char interval = Handle_SystemInfo.EdgeSeamInfo[i].MMatch;
  802. if (interval==1)
  803. interval=3;
  804. else
  805. if(interval==3)
  806. interval=1;
  807. int p0 = Handle_SystemInfo.EdgeSeamInfo[i].v0;
  808. int p0p = Handle_SystemInfo.EdgeSeamInfo[i].v0p;
  809. std::complex<double> rot = GetRotationComplex(interval);
  810. ///get the integer variable
  811. int integerVar = n_vert_vars + Handle_SystemInfo.EdgeSeamInfo[i].integerVar;
  812. if (integer_rounding)
  813. {
  814. ids_to_round.push_back(integerVar*2);
  815. ids_to_round.push_back(integerVar*2+1);
  816. }
  817. // cross boundary compatibility conditions
  818. Constraints.coeffRef(constr_row, 2*p0) += rot.real();
  819. Constraints.coeffRef(constr_row, 2*p0+1) += -rot.imag();
  820. Constraints.coeffRef(constr_row+1, 2*p0) += rot.imag();
  821. Constraints.coeffRef(constr_row+1, 2*p0+1) += rot.real();
  822. Constraints.coeffRef(constr_row, 2*p0p) += -1;
  823. Constraints.coeffRef(constr_row+1, 2*p0p+1) += -1;
  824. Constraints.coeffRef(constr_row, 2*integerVar) += 1;
  825. Constraints.coeffRef(constr_row+1, 2*integerVar+1) += 1;
  826. constraints_rhs[constr_row] = 0;
  827. constraints_rhs[constr_row+1] = 0;
  828. constr_row += 2;
  829. }
  830. }
  831. ///set the constraints for the inter-range cuts
  832. template <typename DerivedV, typename DerivedF>
  833. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::BuildUserDefinedConstraints()
  834. {
  835. /// the user defined constraints are at the end
  836. int offset_row = num_cut_constraint*2 + n_fixed_vars*2;
  837. ///current constraint row
  838. int constr_row = offset_row;
  839. assert(num_userdefined_constraint == userdefined_constraints.size());
  840. for (unsigned int i=0; i<num_userdefined_constraint; i++)
  841. {
  842. for (unsigned int j=0; j<userdefined_constraints[i].size()-1; ++j)
  843. {
  844. Constraints.coeffRef(constr_row, j) = userdefined_constraints[i][j];
  845. }
  846. constraints_rhs[constr_row] = userdefined_constraints[i][userdefined_constraints[i].size()-1];
  847. constr_row +=1;
  848. }
  849. }
  850. ///call of the mixed integer solver
  851. template <typename DerivedV, typename DerivedF>
  852. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::MixedIntegerSolve(double cone_grid_res,
  853. bool direct_round,
  854. int localIter)
  855. {
  856. X = std::vector<double>((n_vert_vars+n_integer_vars)*2);
  857. if (DEBUGPRINT)
  858. printf("\n ALLOCATED X \n");
  859. ///variables part
  860. int ScalarSize = n_vert_vars*2;
  861. int SizeMatrix = (n_vert_vars+n_integer_vars)*2;
  862. ///matrix A
  863. gmm::col_matrix< gmm::wsvector< double > > A(SizeMatrix,SizeMatrix); // lhs matrix variables
  864. ///constraints part
  865. int CsizeX = num_constraint_equations;
  866. int CsizeY = SizeMatrix+1;
  867. gmm::row_matrix< gmm::wsvector< double > > C(CsizeX,CsizeY); // constraints
  868. if (DEBUGPRINT)
  869. printf("\n ALLOCATED QMM STRUCTURES \n");
  870. std::vector<double> B(SizeMatrix,0); // rhs
  871. if (DEBUGPRINT)
  872. printf("\n ALLOCATED RHS STRUCTURES \n");
  873. //// copy LHS
  874. for (int k=0; k < Lhs.outerSize(); ++k){
  875. for (Eigen::SparseMatrix<double>::InnerIterator it(Lhs,k); it; ++it){
  876. int row = it.row();
  877. int col = it.col();
  878. A(row, col) += it.value();
  879. }
  880. }
  881. //// copy Constraints
  882. for (int k=0; k < Constraints.outerSize(); ++k){
  883. for (Eigen::SparseMatrix<double>::InnerIterator it(Constraints,k); it; ++it){
  884. int row = it.row();
  885. int col = it.col();
  886. C(row, col) += it.value();
  887. }
  888. }
  889. if (DEBUGPRINT)
  890. printf("\n SET %d INTEGER VALUES \n",n_integer_vars);
  891. ///add penalization term for integer variables
  892. double penalization = 0.000001;
  893. int offline_index = ScalarSize;
  894. for(unsigned int i = 0; i < (n_integer_vars)*2; ++i)
  895. {
  896. int index=offline_index+i;
  897. A(index,index)=penalization;
  898. }
  899. if (DEBUGPRINT)
  900. printf("\n SET RHS \n");
  901. // copy RHS
  902. for(int i = 0; i < (int)ScalarSize; ++i)
  903. {
  904. B[i] = rhs[i] * cone_grid_res;
  905. }
  906. // copy constraint RHS
  907. if (DEBUGPRINT)
  908. printf("\n SET %d CONSTRAINTS \n",num_constraint_equations);
  909. for(unsigned int i = 0; i < num_constraint_equations; ++i)
  910. {
  911. C(i, SizeMatrix) = -constraints_rhs[i] * cone_grid_res;
  912. }
  913. COMISO::ConstrainedSolver solver;
  914. solver.misolver().set_local_iters(localIter);
  915. solver.misolver().set_direct_rounding(direct_round);
  916. std::sort(ids_to_round.begin(),ids_to_round.end());
  917. std::vector<int>::iterator new_end=std::unique(ids_to_round.begin(),ids_to_round.end());
  918. int dist=distance(ids_to_round.begin(),new_end);
  919. ids_to_round.resize(dist);
  920. solver.solve( C, A, X, B, ids_to_round, 0.0, false, false);
  921. }
  922. template <typename DerivedV, typename DerivedF>
  923. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::clearUserConstraint()
  924. {
  925. num_userdefined_constraint = 0;
  926. userdefined_constraints.clear();
  927. }
  928. template <typename DerivedV, typename DerivedF>
  929. IGL_INLINE void igl::copyleft::comiso::PoissonSolver<DerivedV, DerivedF>::addSharpEdgeConstraint(int fid, int vid)
  930. {
  931. // prepare constraint
  932. std::vector<int> c(Handle_SystemInfo.num_vert_variables*2 + 1);
  933. for (size_t i = 0; i < c.size(); ++i)
  934. {
  935. c[i] = 0;
  936. }
  937. int v1 = Fcut(fid,vid);
  938. int v2 = Fcut(fid,(vid+1)%3);
  939. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> e = Vcut.row(v2) - Vcut.row(v1);
  940. e = e.normalized();
  941. double d1 = fabs(e.dot(PD1.row(fid).normalized()));
  942. double d2 = fabs(e.dot(PD2.row(fid).normalized()));
  943. int offset = 0;
  944. if (d1>d2)
  945. offset = 1;
  946. ids_to_round.push_back((v1 * 2) + offset);
  947. ids_to_round.push_back((v2 * 2) + offset);
  948. // add constraint
  949. c[(v1 * 2) + offset] = 1;
  950. c[(v2 * 2) + offset] = -1;
  951. // add to the user-defined constraints
  952. num_userdefined_constraint++;
  953. userdefined_constraints.push_back(c);
  954. }
  955. template <typename DerivedV, typename DerivedF, typename DerivedU>
  956. IGL_INLINE igl::copyleft::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::MIQ_class(const Eigen::PlainObjectBase<DerivedV> &V_,
  957. const Eigen::PlainObjectBase<DerivedF> &F_,
  958. const Eigen::PlainObjectBase<DerivedV> &PD1_combed,
  959. const Eigen::PlainObjectBase<DerivedV> &PD2_combed,
  960. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_MMatch,
  961. const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_Singular,
  962. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_Seams,
  963. Eigen::PlainObjectBase<DerivedU> &UV,
  964. Eigen::PlainObjectBase<DerivedF> &FUV,
  965. double GradientSize,
  966. double Stiffness,
  967. bool DirectRound,
  968. int iter,
  969. int localIter,
  970. bool DoRound,
  971. bool SingularityRound,
  972. std::vector<int> roundVertices,
  973. std::vector<std::vector<int> > hardFeatures):
  974. V(V_),
  975. F(F_)
  976. {
  977. igl::cut_mesh(V, F, Handle_Seams, Vcut, Fcut);
  978. igl::local_basis(V,F,B1,B2,B3);
  979. igl::triangle_triangle_adjacency(F,TT,TTi);
  980. // Prepare indexing for the linear system
  981. VertexIndexing<DerivedV, DerivedF> VInd(V, F, Vcut, Fcut, TT, TTi, Handle_MMatch, Handle_Singular, Handle_Seams);
  982. VInd.InitSeamInfo();
  983. // Assemble the system and solve
  984. PoissonSolver<DerivedV, DerivedF> PSolver(V,
  985. F,
  986. Vcut,
  987. Fcut,
  988. TT,
  989. TTi,
  990. PD1_combed,
  991. PD2_combed,
  992. Handle_Singular,
  993. VInd.Handle_SystemInfo);
  994. Handle_Stiffness = Eigen::VectorXd::Constant(F.rows(),1);
  995. if (iter > 0) // do stiffening
  996. {
  997. for (int i=0;i<iter;i++)
  998. {
  999. PSolver.SolvePoisson(Handle_Stiffness, GradientSize,1.f,DirectRound,localIter,DoRound,SingularityRound,roundVertices,hardFeatures);
  1000. int nflips=NumFlips(PSolver.WUV);
  1001. bool folded = updateStiffeningJacobianDistorsion(GradientSize,PSolver.WUV);
  1002. printf("ITERATION %d FLIPS %d \n",i,nflips);
  1003. if (!folded)break;
  1004. }
  1005. }
  1006. else
  1007. {
  1008. PSolver.SolvePoisson(Handle_Stiffness,GradientSize,1.f,DirectRound,localIter,DoRound,SingularityRound,roundVertices,hardFeatures);
  1009. }
  1010. int nflips=NumFlips(PSolver.WUV);
  1011. printf("**** END OPTIMIZING #FLIPS %d ****\n",nflips);
  1012. UV_out = PSolver.UV_out;
  1013. FUV_out = PSolver.Fcut;
  1014. fflush(stdout);
  1015. }
  1016. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1017. IGL_INLINE void igl::copyleft::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::extractUV(Eigen::PlainObjectBase<DerivedU> &UV_out,
  1018. Eigen::PlainObjectBase<DerivedF> &FUV_out)
  1019. {
  1020. UV_out = this->UV_out;
  1021. FUV_out = this->FUV_out;
  1022. }
  1023. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1024. IGL_INLINE int igl::copyleft::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::NumFlips(const Eigen::MatrixXd& WUV)
  1025. {
  1026. int numFl=0;
  1027. for (unsigned int i=0;i<F.rows();i++)
  1028. {
  1029. if (IsFlipped(i, WUV))
  1030. numFl++;
  1031. }
  1032. return numFl;
  1033. }
  1034. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1035. IGL_INLINE double igl::copyleft::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::Distortion(int f, double h, const Eigen::MatrixXd& WUV)
  1036. {
  1037. assert(h > 0);
  1038. Eigen::Vector2d uv0,uv1,uv2;
  1039. uv0 << WUV(f,0), WUV(f,1);
  1040. uv1 << WUV(f,2), WUV(f,3);
  1041. uv2 << WUV(f,4), WUV(f,5);
  1042. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> p0 = Vcut.row(Fcut(f,0));
  1043. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> p1 = Vcut.row(Fcut(f,1));
  1044. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> p2 = Vcut.row(Fcut(f,2));
  1045. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> norm = (p1 - p0).cross(p2 - p0);
  1046. double area2 = norm.norm();
  1047. double area2_inv = 1.0 / area2;
  1048. norm *= area2_inv;
  1049. if (area2 > 0)
  1050. {
  1051. // Singular values of the Jacobian
  1052. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> neg_t0 = norm.cross(p2 - p1);
  1053. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> neg_t1 = norm.cross(p0 - p2);
  1054. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> neg_t2 = norm.cross(p1 - p0);
  1055. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> diffu = (neg_t0 * uv0(0) +neg_t1 *uv1(0) + neg_t2 * uv2(0) )*area2_inv;
  1056. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> diffv = (neg_t0 * uv0(1) + neg_t1*uv1(1) + neg_t2*uv2(1) )*area2_inv;
  1057. // first fundamental form
  1058. double I00 = diffu.dot(diffu); // guaranteed non-neg
  1059. double I01 = diffu.dot(diffv); // I01 = I10
  1060. double I11 = diffv.dot(diffv); // guaranteed non-neg
  1061. // eigenvalues of a 2x2 matrix
  1062. // [a00 a01]
  1063. // [a10 a11]
  1064. // 1/2 * [ (a00 + a11) +/- sqrt((a00 - a11)^2 + 4 a01 a10) ]
  1065. double trI = I00 + I11; // guaranteed non-neg
  1066. double diffDiag = I00 - I11; // guaranteed non-neg
  1067. double sqrtDet = sqrt(std::max(0.0, diffDiag*diffDiag +
  1068. 4 * I01 * I01)); // guaranteed non-neg
  1069. double sig1 = 0.5 * (trI + sqrtDet); // higher singular value
  1070. double sig2 = 0.5 * (trI - sqrtDet); // lower singular value
  1071. // Avoid sig2 < 0 due to numerical error
  1072. if (fabs(sig2) < 1.0e-8)
  1073. sig2 = 0;
  1074. assert(sig1 >= 0);
  1075. assert(sig2 >= 0);
  1076. if (sig2 < 0) {
  1077. printf("Distortion will be NaN! sig1^2 is negative (%lg)\n",
  1078. sig2);
  1079. }
  1080. // The singular values of the Jacobian are the sqrts of the
  1081. // eigenvalues of the first fundamental form.
  1082. sig1 = sqrt(sig1);
  1083. sig2 = sqrt(sig2);
  1084. // distortion
  1085. double tao = IsFlipped(f,WUV) ? -1 : 1;
  1086. double factor = tao / h;
  1087. double lam = fabs(factor * sig1 - 1) + fabs(factor * sig2 - 1);
  1088. return lam;
  1089. }
  1090. else {
  1091. return 10; // something "large"
  1092. }
  1093. }
  1094. ////////////////////////////////////////////////////////////////////////////
  1095. // Approximate the distortion laplacian using a uniform laplacian on
  1096. // the dual mesh:
  1097. // ___________
  1098. // \-1 / \-1 /
  1099. // \ / 3 \ /
  1100. // \-----/
  1101. // \-1 /
  1102. // \ /
  1103. //
  1104. // @param[in] f facet on which to compute distortion laplacian
  1105. // @param[in] h scaling factor applied to cross field
  1106. // @return distortion laplacian for f
  1107. ///////////////////////////////////////////////////////////////////////////
  1108. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1109. IGL_INLINE double igl::copyleft::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::LaplaceDistortion(const int f, double h, const Eigen::MatrixXd& WUV)
  1110. {
  1111. double mydist = Distortion(f, h, WUV);
  1112. double lapl=0;
  1113. for (int i=0;i<3;i++)
  1114. {
  1115. if (TT(f,i) != -1)
  1116. lapl += (mydist - Distortion(TT(f,i), h, WUV));
  1117. }
  1118. return lapl;
  1119. }
  1120. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1121. IGL_INLINE bool igl::copyleft::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::updateStiffeningJacobianDistorsion(double grad_size, const Eigen::MatrixXd& WUV)
  1122. {
  1123. bool flipped = NumFlips(WUV)>0;
  1124. if (!flipped)
  1125. return false;
  1126. double maxL=0;
  1127. double maxD=0;
  1128. if (flipped)
  1129. {
  1130. const double c = 1.0;
  1131. const double d = 5.0;
  1132. for (unsigned int i = 0; i < Fcut.rows(); ++i)
  1133. {
  1134. double dist=Distortion(i,grad_size,WUV);
  1135. if (dist > maxD)
  1136. maxD=dist;
  1137. double absLap=fabs(LaplaceDistortion(i, grad_size,WUV));
  1138. if (absLap > maxL)
  1139. maxL = absLap;
  1140. double stiffDelta = std::min(c * absLap, d);
  1141. Handle_Stiffness[i]+=stiffDelta;
  1142. }
  1143. }
  1144. printf("Maximum Distorsion %4.4f \n",maxD);
  1145. printf("Maximum Laplacian %4.4f \n",maxL);
  1146. return flipped;
  1147. }
  1148. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1149. IGL_INLINE bool igl::copyleft::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::IsFlipped(const Eigen::Vector2d &uv0,
  1150. const Eigen::Vector2d &uv1,
  1151. const Eigen::Vector2d &uv2)
  1152. {
  1153. Eigen::Vector2d e0 = (uv1-uv0);
  1154. Eigen::Vector2d e1 = (uv2-uv0);
  1155. double Area = e0(0)*e1(1) - e0(1)*e1(0);
  1156. return (Area<=0);
  1157. }
  1158. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1159. IGL_INLINE bool igl::copyleft::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::IsFlipped(
  1160. const int i, const Eigen::MatrixXd& WUV)
  1161. {
  1162. Eigen::Vector2d uv0,uv1,uv2;
  1163. uv0 << WUV(i,0), WUV(i,1);
  1164. uv1 << WUV(i,2), WUV(i,3);
  1165. uv2 << WUV(i,4), WUV(i,5);
  1166. return (IsFlipped(uv0,uv1,uv2));
  1167. }
  1168. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1169. IGL_INLINE void igl::copyleft::comiso::miq(
  1170. const Eigen::PlainObjectBase<DerivedV> &V,
  1171. const Eigen::PlainObjectBase<DerivedF> &F,
  1172. const Eigen::PlainObjectBase<DerivedV> &PD1_combed,
  1173. const Eigen::PlainObjectBase<DerivedV> &PD2_combed,
  1174. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_MMatch,
  1175. const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_Singular,
  1176. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_Seams,
  1177. Eigen::PlainObjectBase<DerivedU> &UV,
  1178. Eigen::PlainObjectBase<DerivedF> &FUV,
  1179. double GradientSize,
  1180. double Stiffness,
  1181. bool DirectRound,
  1182. int iter,
  1183. int localIter,
  1184. bool DoRound,
  1185. bool SingularityRound,
  1186. std::vector<int> roundVertices,
  1187. std::vector<std::vector<int> > hardFeatures)
  1188. {
  1189. GradientSize = GradientSize/(V.colwise().maxCoeff()-V.colwise().minCoeff()).norm();
  1190. igl::copyleft::comiso::MIQ_class<DerivedV, DerivedF, DerivedU> miq(V,
  1191. F,
  1192. PD1_combed,
  1193. PD2_combed,
  1194. Handle_MMatch,
  1195. Handle_Singular,
  1196. Handle_Seams,
  1197. UV,
  1198. FUV,
  1199. GradientSize,
  1200. Stiffness,
  1201. DirectRound,
  1202. iter,
  1203. localIter,
  1204. DoRound,
  1205. SingularityRound,
  1206. roundVertices,
  1207. hardFeatures);
  1208. miq.extractUV(UV,FUV);
  1209. }
  1210. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1211. IGL_INLINE void igl::copyleft::comiso::miq(
  1212. const Eigen::PlainObjectBase<DerivedV> &V,
  1213. const Eigen::PlainObjectBase<DerivedF> &F,
  1214. const Eigen::PlainObjectBase<DerivedV> &PD1,
  1215. const Eigen::PlainObjectBase<DerivedV> &PD2,
  1216. Eigen::PlainObjectBase<DerivedU> &UV,
  1217. Eigen::PlainObjectBase<DerivedF> &FUV,
  1218. double GradientSize,
  1219. double Stiffness,
  1220. bool DirectRound,
  1221. int iter,
  1222. int localIter,
  1223. bool DoRound,
  1224. bool SingularityRound,
  1225. std::vector<int> roundVertices,
  1226. std::vector<std::vector<int> > hardFeatures)
  1227. {
  1228. DerivedV BIS1, BIS2;
  1229. igl::compute_frame_field_bisectors(V, F, PD1, PD2, BIS1, BIS2);
  1230. DerivedV BIS1_combed, BIS2_combed;
  1231. igl::comb_cross_field(V, F, BIS1, BIS2, BIS1_combed, BIS2_combed);
  1232. DerivedF Handle_MMatch;
  1233. igl::cross_field_missmatch(V, F, BIS1_combed, BIS2_combed, true, Handle_MMatch);
  1234. Eigen::Matrix<int, Eigen::Dynamic, 1> isSingularity, singularityIndex;
  1235. igl::find_cross_field_singularities(V, F, Handle_MMatch, isSingularity, singularityIndex);
  1236. Eigen::Matrix<int, Eigen::Dynamic, 3> Handle_Seams;
  1237. igl::cut_mesh_from_singularities(V, F, Handle_MMatch, Handle_Seams);
  1238. DerivedV PD1_combed, PD2_combed;
  1239. igl::comb_frame_field(V, F, PD1, PD2, BIS1_combed, BIS2_combed, PD1_combed, PD2_combed);
  1240. igl::copyleft::comiso::miq(V,
  1241. F,
  1242. PD1_combed,
  1243. PD2_combed,
  1244. Handle_MMatch,
  1245. isSingularity,
  1246. Handle_Seams,
  1247. UV,
  1248. FUV,
  1249. GradientSize,
  1250. Stiffness,
  1251. DirectRound,
  1252. iter,
  1253. localIter,
  1254. DoRound,
  1255. SingularityRound,
  1256. roundVertices,
  1257. hardFeatures);
  1258. }
  1259. #ifdef IGL_STATIC_LIBRARY
  1260. // Explicit template instantiation
  1261. template void igl::copyleft::comiso::miq<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&, double, double, bool, int, int, bool, bool, std::vector<int, std::allocator<int> >, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >);
  1262. template void igl::copyleft::comiso::miq<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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, 3, 0, -1, 3> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 3, 0, -1, 3> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, double, double, bool, int, int, bool, bool, std::vector<int, std::allocator<int> >, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >);
  1263. template void igl::copyleft::comiso::miq<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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, double, double, bool, int, int, bool, bool, std::vector<int, std::allocator<int> >, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >);
  1264. #endif