miq.cpp 59 KB

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