miq.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>, Olga Diamanti <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include <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. using namespace std;
  36. using namespace Eigen;
  37. #define DEBUGPRINT 0
  38. namespace igl {
  39. namespace comiso {
  40. class SparseMatrixData{
  41. protected:
  42. unsigned int m_nrows;
  43. unsigned int m_ncols;
  44. std::vector<unsigned int> m_rowind;
  45. std::vector<unsigned int> m_colind;
  46. std::vector<double> m_vals;
  47. public:
  48. unsigned int nrows() { return m_nrows ; }
  49. unsigned int ncols() { return m_ncols ; }
  50. unsigned int nentries() { return m_vals.size(); }
  51. std::vector<unsigned int>& rowind() { return m_rowind ; }
  52. std::vector<unsigned int>& colind() { return m_colind ; }
  53. std::vector<double>& vals() { return m_vals ; }
  54. // create an empty matrix with a fixed number of rows
  55. IGL_INLINE SparseMatrixData()
  56. {
  57. initialize(0,0);
  58. }
  59. // create an empty matrix with a fixed number of rows
  60. IGL_INLINE void initialize(int nr, int nc) {
  61. assert(nr >= 0 && nc >=0);
  62. m_nrows = nr;
  63. m_ncols = nc;
  64. m_rowind.resize(0);
  65. m_colind.resize(0);
  66. m_vals.resize(0);
  67. }
  68. // add a nonzero entry to the matrix
  69. // no checks are done for coinciding entries
  70. // the interpretation of the repeated entries (replace or add)
  71. // depends on how the actual sparse matrix datastructure is constructed
  72. IGL_INLINE void addEntryCmplx(unsigned int i, unsigned int j, std::complex<double> val) {
  73. m_rowind.push_back(2*i); m_colind.push_back(2*j); m_vals.push_back( val.real());
  74. m_rowind.push_back(2*i); m_colind.push_back(2*j+1); m_vals.push_back(-val.imag());
  75. m_rowind.push_back(2*i+1); m_colind.push_back(2*j); m_vals.push_back( val.imag());
  76. m_rowind.push_back(2*i+1); m_colind.push_back(2*j+1); m_vals.push_back( val.real());
  77. }
  78. IGL_INLINE void addEntryReal(unsigned int i, unsigned int j, double val) {
  79. m_rowind.push_back(i); m_colind.push_back(j); m_vals.push_back(val);
  80. }
  81. IGL_INLINE virtual ~SparseMatrixData() {
  82. }
  83. };
  84. // a small class to manage storage for matrix data
  85. // not using stl vectors: want to make all memory management
  86. // explicit to avoid hidden automatic reallocation
  87. // TODO: redo with STL vectors but with explicit mem. management
  88. class SparseSystemData {
  89. private:
  90. // matrix representation, A[rowind[i],colind[i]] = vals[i]
  91. // right-hand side
  92. SparseMatrixData m_A;
  93. double *m_b;
  94. double *m_x;
  95. public:
  96. IGL_INLINE SparseMatrixData& A() { return m_A; }
  97. IGL_INLINE double* b() { return m_b ; }
  98. IGL_INLINE double* x() { return m_x ; }
  99. IGL_INLINE unsigned int nrows() { return m_A.nrows(); }
  100. public:
  101. IGL_INLINE SparseSystemData(): m_A(), m_b(NULL), m_x(NULL){ }
  102. IGL_INLINE void initialize(unsigned int nr, unsigned int nc) {
  103. m_A.initialize(nr,nc);
  104. m_b = new double[nr];
  105. m_x = new double[nr];
  106. assert(m_b);
  107. std::fill( m_b, m_b+nr, 0.);
  108. }
  109. IGL_INLINE void addRHSCmplx(unsigned int i, std::complex<double> val) {
  110. assert( 2*i+1 < m_A.nrows());
  111. m_b[2*i] += val.real(); m_b[2*i+1] += val.imag();
  112. }
  113. IGL_INLINE void setRHSCmplx(unsigned int i, std::complex<double> val) {
  114. assert( 2*i+1 < m_A.nrows());
  115. m_b[2*i] = val.real(); m_b[2*i+1] = val.imag();
  116. }
  117. IGL_INLINE std::complex<double> getRHSCmplx(unsigned int i) {
  118. assert( 2*i+1 < m_A.nrows());
  119. return std::complex<double>( m_b[2*i], m_b[2*i+1]);
  120. }
  121. IGL_INLINE double getRHSReal(unsigned int i) {
  122. assert( i < m_A.nrows());
  123. return m_b[i];
  124. }
  125. IGL_INLINE std::complex<double> getXCmplx(unsigned int i) {
  126. assert( 2*i+1 < m_A.nrows());
  127. return std::complex<double>( m_x[2*i], m_x[2*i+1]);
  128. }
  129. IGL_INLINE void cleanMem() {
  130. //m_A.cleanup();
  131. delete [] m_b;
  132. delete [] m_x;
  133. }
  134. IGL_INLINE virtual ~SparseSystemData() {
  135. delete [] m_b;
  136. delete [] m_x;
  137. }
  138. };
  139. struct SeamInfo
  140. {
  141. int v0,v0p,v1,v1p;
  142. int integerVar;
  143. unsigned char MMatch;
  144. IGL_INLINE SeamInfo(int _v0,
  145. int _v1,
  146. int _v0p,
  147. int _v1p,
  148. int _MMatch,
  149. int _integerVar);
  150. IGL_INLINE SeamInfo(const SeamInfo &S1);
  151. };
  152. struct MeshSystemInfo
  153. {
  154. ///total number of scalar variables
  155. int num_scalar_variables;
  156. ////number of vertices variables
  157. int num_vert_variables;
  158. ///num of integer for cuts
  159. int num_integer_cuts;
  160. ///this are used for drawing purposes
  161. std::vector<SeamInfo> EdgeSeamInfo;
  162. #if 0
  163. ///this are values of integer variables after optimization
  164. std::vector<int> IntegerValues;
  165. #endif
  166. };
  167. template <typename DerivedV, typename DerivedF>
  168. class VertexIndexing
  169. {
  170. public:
  171. // Input:
  172. const Eigen::PlainObjectBase<DerivedV> &V;
  173. const Eigen::PlainObjectBase<DerivedF> &F;
  174. const Eigen::PlainObjectBase<DerivedF> &TT;
  175. const Eigen::PlainObjectBase<DerivedF> &TTi;
  176. // const Eigen::PlainObjectBase<DerivedV> &PD1;
  177. // const Eigen::PlainObjectBase<DerivedV> &PD2;
  178. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_MMatch;
  179. // const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_Singular; // bool
  180. // const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_SingularDegree; // vertex;
  181. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_Seams; // 3 bool
  182. ///this handle for mesh TODO: move with the other global variables
  183. MeshSystemInfo Handle_SystemInfo;
  184. // Output:
  185. ///this maps the integer for edge - face
  186. Eigen::MatrixXi Handle_Integer; // TODO: remove it is useless
  187. ///per face indexes of vertex in the solver
  188. Eigen::MatrixXi HandleS_Index;
  189. ///per vertex variable indexes
  190. std::vector<std::vector<int> > HandleV_Integer;
  191. // internal
  192. std::vector<std::vector<int> > VF, VFi;
  193. std::vector<bool> V_border; // bool
  194. IGL_INLINE VertexIndexing(const Eigen::PlainObjectBase<DerivedV> &_V,
  195. const Eigen::PlainObjectBase<DerivedF> &_F,
  196. const Eigen::PlainObjectBase<DerivedF> &_TT,
  197. const Eigen::PlainObjectBase<DerivedF> &_TTi,
  198. // const Eigen::PlainObjectBase<DerivedV> &_PD1,
  199. // const Eigen::PlainObjectBase<DerivedV> &_PD2,
  200. const Eigen::Matrix<int, Eigen::Dynamic, 3> &_Handle_MMatch,
  201. // const Eigen::Matrix<int, Eigen::Dynamic, 1> &_Handle_Singular,
  202. // const Eigen::Matrix<int, Eigen::Dynamic, 1> &_Handle_SingularDegree,
  203. const Eigen::Matrix<int, Eigen::Dynamic, 3> &_Handle_Seams
  204. );
  205. ///vertex to variable mapping
  206. IGL_INLINE void InitMapping();
  207. IGL_INLINE void InitFaceIntegerVal();
  208. IGL_INLINE void InitSeamInfo();
  209. private:
  210. ///this maps back index to vertices
  211. std::vector<int> IndexToVert; // TODO remove it is useless
  212. ///this is used for drawing purposes
  213. std::vector<int> duplicated; // TODO remove it is useless
  214. IGL_INLINE void FirstPos(const int v, int &f, int &edge);
  215. IGL_INLINE int AddNewIndex(const int v0);
  216. IGL_INLINE bool HasIndex(int indexVert,int indexVar);
  217. IGL_INLINE void GetSeamInfo(const int f0,
  218. const int f1,
  219. const int indexE,
  220. int &v0,int &v1,
  221. int &v0p,int &v1p,
  222. unsigned char &_MMatch,
  223. int &integerVar);
  224. IGL_INLINE bool IsSeam(const int f0, const int f1);
  225. ///find initial position of the pos to
  226. // assing face to vert inxex correctly
  227. IGL_INLINE void FindInitialPos(const int vert, int &edge, int &face);
  228. ///intialize the mapping given an initial pos
  229. ///whih must be initialized with FindInitialPos
  230. IGL_INLINE void MapIndexes(const int vert, const int edge_init, const int f_init);
  231. ///intialize the mapping for a given vertex
  232. IGL_INLINE void InitMappingSeam(const int vert);
  233. ///intialize the mapping for a given sampled mesh
  234. IGL_INLINE void InitMappingSeam();
  235. ///test consistency of face variables per vert mapping
  236. IGL_INLINE void TestSeamMappingFace(const int f);
  237. ///test consistency of face variables per vert mapping
  238. IGL_INLINE void TestSeamMappingVertex(int indexVert);
  239. ///check consistency of variable mapping across seams
  240. IGL_INLINE void TestSeamMapping();
  241. };
  242. template <typename DerivedV, typename DerivedF>
  243. class PoissonSolver
  244. {
  245. public:
  246. IGL_INLINE void SolvePoisson(Eigen::VectorXd Stiffness,
  247. double vector_field_scale=0.1f,
  248. double grid_res=1.f,
  249. bool direct_round=true,
  250. int localIter=0,
  251. bool _integer_rounding=true,
  252. bool _singularity_rounding=true,
  253. std::vector<int> roundVertices = std::vector<int>(),
  254. std::vector<std::vector<int> > hardFeatures = std::vector<std::vector<int> >());
  255. IGL_INLINE PoissonSolver(const Eigen::PlainObjectBase<DerivedV> &_V,
  256. const Eigen::PlainObjectBase<DerivedF> &_F,
  257. const Eigen::PlainObjectBase<DerivedF> &_TT,
  258. const Eigen::PlainObjectBase<DerivedF> &_TTi,
  259. const Eigen::PlainObjectBase<DerivedV> &_PD1,
  260. const Eigen::PlainObjectBase<DerivedV> &_PD2,
  261. const Eigen::MatrixXi &_HandleS_Index,
  262. const Eigen::Matrix<int, Eigen::Dynamic, 1>&_Handle_Singular,
  263. const MeshSystemInfo &_Handle_SystemInfo
  264. );
  265. const Eigen::PlainObjectBase<DerivedV> &V;
  266. const Eigen::PlainObjectBase<DerivedF> &F;
  267. const Eigen::PlainObjectBase<DerivedF> &TT;
  268. const Eigen::PlainObjectBase<DerivedF> &TTi;
  269. const Eigen::PlainObjectBase<DerivedV> &PD1;
  270. const Eigen::PlainObjectBase<DerivedV> &PD2;
  271. const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_Singular; // bool
  272. const Eigen::MatrixXi &HandleS_Index; //todo
  273. const Eigen::MatrixXi &Handle_Seams;
  274. const MeshSystemInfo &Handle_SystemInfo;
  275. // Internal:
  276. Eigen::MatrixXd doublearea;
  277. Eigen::VectorXd Handle_Stiffness;
  278. Eigen::PlainObjectBase<DerivedV> N;
  279. std::vector<std::vector<int> > VF;
  280. std::vector<std::vector<int> > VFi;
  281. Eigen::MatrixXd UV; // this is probably useless
  282. // Output:
  283. // per wedge UV coordinates, 6 coordinates (1 face) per row
  284. Eigen::MatrixXd WUV;
  285. // Matrices
  286. Eigen::SparseMatrix<double> Lhs;
  287. Eigen::SparseMatrix<double> Constraints;
  288. Eigen::VectorXd rhs;
  289. Eigen::VectorXd constraints_rhs;
  290. ///vector of unknowns
  291. std::vector< double > X;
  292. ////REAL PART
  293. ///number of fixed vertex
  294. unsigned int n_fixed_vars;
  295. ///the number of REAL variables for vertices
  296. unsigned int n_vert_vars;
  297. ///total number of variables of the system,
  298. ///do not consider constraints, but consider integer vars
  299. unsigned int num_total_vars;
  300. //////INTEGER PART
  301. ///the total number of integer variables
  302. unsigned int n_integer_vars;
  303. ///CONSTRAINT PART
  304. ///number of cuts constraints
  305. unsigned int num_cut_constraint;
  306. // number of user-defined constraints
  307. unsigned int num_userdefined_constraint;
  308. ///total number of constraints equations
  309. unsigned int num_constraint_equations;
  310. ///total size of the system including constraints
  311. unsigned int system_size;
  312. ///if you intend to make integer rotation
  313. ///and translations
  314. bool integer_jumps_bary;
  315. ///vector of blocked vertices
  316. std::vector<int> Hard_constraints;
  317. ///vector of indexes to round
  318. std::vector<int> ids_to_round;
  319. ///vector of indexes to round
  320. std::vector<std::vector<int > > userdefined_constraints;
  321. ///boolean that is true if rounding to integer is needed
  322. bool integer_rounding;
  323. ///START SYSTEM ACCESS METHODS
  324. ///add an entry to the LHS
  325. IGL_INLINE void AddValA(int Xindex,
  326. int Yindex,
  327. double val);
  328. ///add a complex entry to the LHS
  329. IGL_INLINE void AddComplexA(int VarXindex,
  330. int VarYindex,
  331. std::complex<double> val);
  332. ///add a velue to the RHS
  333. IGL_INLINE void AddValB(int Xindex,
  334. double val);
  335. ///add the area term, scalefactor is used to sum up
  336. ///and normalize on the overlap zones
  337. IGL_INLINE void AddAreaTerm(int index[3][3][2],double ScaleFactor);
  338. ///given a vector of scalar values and
  339. ///a vector of indexes add such values
  340. ///as specified by the indexes
  341. IGL_INLINE void AddRHS(double b[6],
  342. int index[3]);
  343. ///add a 3x3 block matrix to the system matrix...
  344. ///indexes are specified in the 3x3 matrix of x,y pairs
  345. ///indexes must be multiplied by 2 cause u and v
  346. IGL_INLINE void Add33Block(double val[3][3], int index[3][3][2]);
  347. ///add a 3x3 block matrix to the system matrix...
  348. ///indexes are specified in the 3x3 matrix of x,y pairs
  349. ///indexes must be multiplied by 2 cause u and v
  350. IGL_INLINE void Add44Block(double val[4][4],int index[4][4][2]);
  351. ///END SYSTEM ACCESS METHODS
  352. ///START COMMON MATH FUNCTIONS
  353. ///return the complex encoding the rotation
  354. ///for a given missmatch interval
  355. IGL_INLINE std::complex<double> GetRotationComplex(int interval);
  356. ///END COMMON MATH FUNCTIONS
  357. ///START FIXING VERTICES
  358. ///set a given vertex as fixed
  359. IGL_INLINE void AddFixedVertex(int v);
  360. ///find vertex to fix in case we're using
  361. ///a vector field NB: multiple components not handled
  362. IGL_INLINE void FindFixedVertField();
  363. ///find hard constraint depending if using or not
  364. ///a vector field
  365. IGL_INLINE void FindFixedVert();
  366. IGL_INLINE int GetFirstVertexIndex(int v);
  367. ///fix the vertices which are flagged as fixed
  368. IGL_INLINE void FixBlockedVertex();
  369. ///END FIXING VERTICES
  370. ///HANDLING SINGULARITY
  371. //set the singularity round to integer location
  372. IGL_INLINE void AddSingularityRound();
  373. IGL_INLINE void AddToRoundVertices(std::vector<int> ids);
  374. ///START GENERIC SYSTEM FUNCTIONS
  375. //build the laplacian matrix cyclyng over all rangemaps
  376. //and over all faces
  377. IGL_INLINE void BuildLaplacianMatrix(double vfscale=1);
  378. ///find different sized of the system
  379. IGL_INLINE void FindSizes();
  380. IGL_INLINE void AllocateSystem();
  381. ///intitialize the whole matrix
  382. IGL_INLINE void InitMatrix();
  383. ///map back coordinates after that
  384. ///the system has been solved
  385. IGL_INLINE void MapCoords();
  386. ///END GENERIC SYSTEM FUNCTIONS
  387. ///set the constraints for the inter-range cuts
  388. IGL_INLINE void BuildSeamConstraintsExplicitTranslation();
  389. ///set the constraints for the inter-range cuts
  390. IGL_INLINE void BuildUserDefinedConstraints();
  391. ///call of the mixed integer solver
  392. IGL_INLINE void MixedIntegerSolve(double cone_grid_res=1,
  393. bool direct_round=true,
  394. int localIter=0);
  395. IGL_INLINE void clearUserConstraint();
  396. IGL_INLINE void addSharpEdgeConstraint(int fid, int vid);
  397. };
  398. template <typename DerivedV, typename DerivedF, typename DerivedU>
  399. class MIQ_class
  400. {
  401. private:
  402. const Eigen::PlainObjectBase<DerivedV> &V;
  403. const Eigen::PlainObjectBase<DerivedF> &F;
  404. Eigen::MatrixXd WUV;
  405. // internal
  406. Eigen::PlainObjectBase<DerivedF> TT;
  407. Eigen::PlainObjectBase<DerivedF> TTi;
  408. // Stiffness per face
  409. Eigen::VectorXd Handle_Stiffness;
  410. Eigen::PlainObjectBase<DerivedV> B1, B2, B3;
  411. public:
  412. IGL_INLINE MIQ_class(const Eigen::PlainObjectBase<DerivedV> &V_,
  413. const Eigen::PlainObjectBase<DerivedF> &F_,
  414. const Eigen::PlainObjectBase<DerivedV> &PD1_combed,
  415. const Eigen::PlainObjectBase<DerivedV> &PD2_combed,
  416. // const Eigen::PlainObjectBase<DerivedV> &BIS1_combed,
  417. // const Eigen::PlainObjectBase<DerivedV> &BIS2_combed,
  418. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_MMatch,
  419. const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_Singular,
  420. // const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_SingularDegree,
  421. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_Seams,
  422. Eigen::PlainObjectBase<DerivedU> &UV,
  423. Eigen::PlainObjectBase<DerivedF> &FUV,
  424. double GradientSize = 30.0,
  425. double Stiffness = 5.0,
  426. bool DirectRound = false,
  427. int iter = 5,
  428. int localIter = 5,
  429. bool DoRound = true,
  430. bool SingularityRound=true,
  431. std::vector<int> roundVertices = std::vector<int>(),
  432. std::vector<std::vector<int> > hardFeatures = std::vector<std::vector<int> >());
  433. IGL_INLINE void extractUV(Eigen::PlainObjectBase<DerivedU> &UV_out,
  434. Eigen::PlainObjectBase<DerivedF> &FUV_out);
  435. private:
  436. IGL_INLINE int NumFlips(const Eigen::MatrixXd& WUV);
  437. IGL_INLINE double Distortion(int f, double h, const Eigen::MatrixXd& WUV);
  438. IGL_INLINE double LaplaceDistortion(const int f, double h, const Eigen::MatrixXd& WUV);
  439. IGL_INLINE bool updateStiffeningJacobianDistorsion(double grad_size, const Eigen::MatrixXd& WUV);
  440. IGL_INLINE bool IsFlipped(const Eigen::Vector2d &uv0,
  441. const Eigen::Vector2d &uv1,
  442. const Eigen::Vector2d &uv2);
  443. IGL_INLINE bool IsFlipped(const int i, const Eigen::MatrixXd& WUV);
  444. };
  445. };
  446. }
  447. IGL_INLINE igl::comiso::SeamInfo::SeamInfo(int _v0,
  448. int _v1,
  449. int _v0p,
  450. int _v1p,
  451. int _MMatch,
  452. int _integerVar)
  453. {
  454. v0=_v0;
  455. v1=_v1;
  456. v0p=_v0p;
  457. v1p=_v1p;
  458. integerVar=_integerVar;
  459. MMatch=_MMatch;
  460. }
  461. IGL_INLINE igl::comiso::SeamInfo::SeamInfo(const SeamInfo &S1)
  462. {
  463. v0=S1.v0;
  464. v1=S1.v1;
  465. v0p=S1.v0p;
  466. v1p=S1.v1p;
  467. integerVar=S1.integerVar;
  468. MMatch=S1.MMatch;
  469. }
  470. template <typename DerivedV, typename DerivedF>
  471. IGL_INLINE igl::comiso::VertexIndexing<DerivedV, DerivedF>::VertexIndexing(const Eigen::PlainObjectBase<DerivedV> &_V,
  472. const Eigen::PlainObjectBase<DerivedF> &_F,
  473. const Eigen::PlainObjectBase<DerivedF> &_TT,
  474. const Eigen::PlainObjectBase<DerivedF> &_TTi,
  475. // const Eigen::PlainObjectBase<DerivedV> &_PD1,
  476. // const Eigen::PlainObjectBase<DerivedV> &_PD2,
  477. const Eigen::Matrix<int, Eigen::Dynamic, 3> &_Handle_MMatch,
  478. // const Eigen::Matrix<int, Eigen::Dynamic, 1> &_Handle_Singular,
  479. // const Eigen::Matrix<int, Eigen::Dynamic, 1> &_Handle_SingularDegree,
  480. const Eigen::Matrix<int, Eigen::Dynamic, 3> &_Handle_Seams
  481. ):
  482. V(_V),
  483. F(_F),
  484. TT(_TT),
  485. TTi(_TTi),
  486. // PD1(_PD1),
  487. // PD2(_PD2),
  488. Handle_MMatch(_Handle_MMatch),
  489. // Handle_Singular(_Handle_Singular),
  490. // Handle_SingularDegree(_Handle_SingularDegree),
  491. Handle_Seams(_Handle_Seams)
  492. {
  493. #ifdef DEBUG_PRINT
  494. cerr<<igl::matlab_format(Handle_Seams,"Handle_Seams");
  495. #endif
  496. V_border = igl::is_border_vertex(V,F);
  497. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  498. IndexToVert.clear();
  499. Handle_SystemInfo.num_scalar_variables=0;
  500. Handle_SystemInfo.num_vert_variables=0;
  501. Handle_SystemInfo.num_integer_cuts=0;
  502. duplicated.clear();
  503. HandleS_Index = Eigen::MatrixXi::Constant(F.rows(),3,-1);
  504. Handle_Integer = Eigen::MatrixXi::Constant(F.rows(),3,-1);
  505. HandleV_Integer.resize(V.rows());
  506. }
  507. template <typename DerivedV, typename DerivedF>
  508. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::FirstPos(const int v, int &f, int &edge)
  509. {
  510. f = VF[v][0]; // f=v->cVFp();
  511. edge = VFi[v][0]; // edge=v->cVFi();
  512. }
  513. template <typename DerivedV, typename DerivedF>
  514. IGL_INLINE int igl::comiso::VertexIndexing<DerivedV, DerivedF>::AddNewIndex(const int v0)
  515. {
  516. Handle_SystemInfo.num_scalar_variables++;
  517. HandleV_Integer[v0].push_back(Handle_SystemInfo.num_scalar_variables);
  518. IndexToVert.push_back(v0);
  519. return Handle_SystemInfo.num_scalar_variables;
  520. }
  521. template <typename DerivedV, typename DerivedF>
  522. IGL_INLINE bool igl::comiso::VertexIndexing<DerivedV, DerivedF>::HasIndex(int indexVert,int indexVar)
  523. {
  524. for (unsigned int i=0;i<HandleV_Integer[indexVert].size();i++)
  525. if (HandleV_Integer[indexVert][i]==indexVar)return true;
  526. return false;
  527. }
  528. template <typename DerivedV, typename DerivedF>
  529. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::GetSeamInfo(const int f0,
  530. const int f1,
  531. const int indexE,
  532. int &v0,int &v1,
  533. int &v0p,int &v1p,
  534. unsigned char &_MMatch,
  535. int &integerVar)
  536. {
  537. int edgef0 = indexE;
  538. v0 = HandleS_Index(f0,edgef0);
  539. v1 = HandleS_Index(f0,(edgef0+1)%3);
  540. ////get the index on opposite side
  541. assert(TT(f0,edgef0) == f1);
  542. int edgef1 = TTi(f0,edgef0);
  543. v1p = HandleS_Index(f1,edgef1);
  544. v0p = HandleS_Index(f1,(edgef1+1)%3);
  545. integerVar = Handle_Integer(f0,edgef0);
  546. _MMatch = Handle_MMatch(f0,edgef0);
  547. assert(F(f0,edgef0) == F(f1,((edgef1+1)%3)));
  548. assert(F(f0,((edgef0+1)%3)) == F(f1,edgef1));
  549. }
  550. template <typename DerivedV, typename DerivedF>
  551. IGL_INLINE bool igl::comiso::VertexIndexing<DerivedV, DerivedF>::IsSeam(const int f0, const int f1)
  552. {
  553. for (int i=0;i<3;i++)
  554. {
  555. int f_clos = TT(f0,i);
  556. if (f_clos == -1)
  557. continue; ///border
  558. if (f_clos == f1)
  559. return(Handle_Seams(f0,i));
  560. }
  561. assert(0);
  562. return false;
  563. }
  564. ///find initial position of the pos to
  565. // assing face to vert inxex correctly
  566. template <typename DerivedV, typename DerivedF>
  567. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::FindInitialPos(const int vert,
  568. int &edge,
  569. int &face)
  570. {
  571. int f_init;
  572. int edge_init;
  573. FirstPos(vert,f_init,edge_init); // todo manually IGL_INLINE the function
  574. igl::HalfEdgeIterator<DerivedF> VFI(F,TT,TTi,f_init,edge_init);
  575. #ifdef DEBUG_PRINT
  576. cerr<<"--FindInitialPos--"<<endl;
  577. #endif
  578. bool vertexB = V_border[vert];
  579. bool possible_split=false;
  580. bool complete_turn=false;
  581. do
  582. {
  583. int curr_f = VFI.Fi();
  584. int curr_edge=VFI.Ei();
  585. #ifdef DEBUG_PRINT
  586. cerr<<"@ face "<<curr_f<<", edge "<< F(curr_f,curr_edge)<<" - "<< F(curr_f,(curr_edge+1)%3)<<endl;
  587. #endif
  588. VFI.NextFE();
  589. int next_f=VFI.Fi();
  590. #ifdef DEBUG_PRINT
  591. cerr<<"next face "<<next_f<<", edge "<< F(next_f,VFI.Ei())<<" - "<< F(next_f,(VFI.Ei()+1)%3)<<endl;
  592. #endif
  593. ///test if I've just crossed a border
  594. bool on_border=(TT(curr_f,curr_edge)==-1);
  595. #ifdef DEBUG_PRINT
  596. cerr<<"on_border: "<<on_border<<endl;
  597. #endif
  598. //bool mismatch=false;
  599. bool seam=false;
  600. #ifdef DEBUG_PRINT
  601. cerr<<igl::matlab_format(Handle_Seams,"Handle_Seams");
  602. #endif
  603. ///or if I've just crossed a seam
  604. ///if I'm on a border I MUST start from the one next t othe border
  605. if (!vertexB)
  606. //seam=curr_f->IsSeam(next_f);
  607. seam=IsSeam(curr_f,next_f);
  608. // if (vertexB)
  609. // assert(!Handle_Singular(vert));
  610. // ;
  611. //assert(!vert->IsSingular());
  612. #ifdef DEBUG_PRINT
  613. cerr<<"seam: "<<seam<<endl;
  614. #endif
  615. possible_split=((on_border)||(seam));
  616. #ifdef DEBUG_PRINT
  617. cerr<<"possible_split: "<<possible_split<<endl;
  618. #endif
  619. complete_turn = next_f == f_init;
  620. #ifdef DEBUG_PRINT
  621. cerr<<"complete_turn: "<<complete_turn<<endl;
  622. #endif
  623. } while ((!possible_split)&&(!complete_turn));
  624. face=VFI.Fi();
  625. edge=VFI.Ei();
  626. #ifdef DEBUG_PRINT
  627. cerr<<"FindInitialPos done. Face: "<<face<<", edge: "<< F(face,edge)<<" - "<< F(face,(edge+1)%3)<<endl;
  628. #endif
  629. ///test that is not on a border
  630. //assert(face->FFp(edge)!=face);
  631. }
  632. ///intialize the mapping given an initial pos
  633. ///whih must be initialized with FindInitialPos
  634. template <typename DerivedV, typename DerivedF>
  635. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::MapIndexes(const int vert,
  636. const int edge_init,
  637. const int f_init)
  638. {
  639. ///check that is not on border..
  640. ///in such case maybe it's non manyfold
  641. ///insert an initial index
  642. int curr_index=AddNewIndex(vert);
  643. #ifdef DEBUG_PRINT
  644. cerr<<"--MapIndexes--"<<endl;
  645. #endif
  646. #ifdef DEBUG_PRINT
  647. cerr<<"adding vertex for "<<vert<<endl;
  648. #endif
  649. ///and initialize the jumping pos
  650. igl::HalfEdgeIterator<DerivedF> VFI(F,TT,TTi,f_init,edge_init);
  651. bool complete_turn=false;
  652. do
  653. {
  654. int curr_f = VFI.Fi();
  655. int curr_edge = VFI.Ei();
  656. #ifdef DEBUG_PRINT
  657. cerr<<"Adding vertex "<<curr_index<<" to face "<<curr_f<<", edge "<< F(curr_f,curr_edge)<<" - "<< F(curr_f,(curr_edge+1)%3)<<endl;
  658. #endif
  659. ///assing the current index
  660. HandleS_Index(curr_f,curr_edge) = curr_index;
  661. #ifdef DEBUG_PRINT
  662. cerr<<igl::matlab_format(HandleS_Index,"HandleS_Index")<<endl;
  663. #endif
  664. VFI.NextFE();
  665. int next_f = VFI.Fi();
  666. #ifdef DEBUG_PRINT
  667. cerr<<"next face "<<next_f<<", edge "<< F(next_f,VFI.Ei())<<" - "<< F(next_f,(VFI.Ei()+1)%3)<<endl;
  668. #endif
  669. ///test if I've finiseh with the face exploration
  670. complete_turn = (next_f==f_init);
  671. #ifdef DEBUG_PRINT
  672. cerr<<"complete_turn: "<<complete_turn<<endl;
  673. #endif
  674. ///or if I've just crossed a mismatch
  675. if (!complete_turn)
  676. {
  677. bool seam=false;
  678. //seam=curr_f->IsSeam(next_f);
  679. seam=IsSeam(curr_f,next_f);
  680. if (seam)
  681. {
  682. ///then add a new index
  683. curr_index=AddNewIndex(vert);
  684. #ifdef DEBUG_PRINT
  685. cerr<<"Found a seam, adding vertex for "<<vert<<endl;
  686. #endif
  687. }
  688. }
  689. } while (!complete_turn);
  690. }
  691. ///intialize the mapping for a given vertex
  692. template <typename DerivedV, typename DerivedF>
  693. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::InitMappingSeam(const int vert)
  694. {
  695. ///first rotate until find the first pos after a mismatch
  696. ///or a border or return to the first position...
  697. int f_init = VF[vert][0];
  698. int indexE = VFi[vert][0];
  699. igl::HalfEdgeIterator<DerivedF> VFI(F,TT,TTi,f_init,indexE);
  700. int edge_init;
  701. int face_init;
  702. #ifdef DEBUG_PRINT
  703. cerr<<"---Vertex: "<<vert<<"---"<<endl;
  704. #endif
  705. FindInitialPos(vert,edge_init,face_init);
  706. MapIndexes(vert,edge_init,face_init);
  707. }
  708. ///intialize the mapping for a given sampled mesh
  709. template <typename DerivedV, typename DerivedF>
  710. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::InitMappingSeam()
  711. {
  712. //num_scalar_variables=-1;
  713. Handle_SystemInfo.num_scalar_variables=-1;
  714. for (unsigned int i=0;i<V.rows();i++)
  715. InitMappingSeam(i);
  716. for (unsigned int j=0;j<V.rows();j++)
  717. {
  718. assert(HandleV_Integer[j].size()>0);
  719. if (HandleV_Integer[j].size()>1)
  720. duplicated.push_back(j);
  721. }
  722. }
  723. ///test consistency of face variables per vert mapping
  724. template <typename DerivedV, typename DerivedF>
  725. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::TestSeamMappingFace(const int f)
  726. {
  727. for (int k=0;k<3;k++)
  728. {
  729. int indexV=HandleS_Index(f,k);
  730. int v = F(f,k);
  731. bool has_index=HasIndex(v,indexV);
  732. assert(has_index);
  733. }
  734. }
  735. ///test consistency of face variables per vert mapping
  736. template <typename DerivedV, typename DerivedF>
  737. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::TestSeamMappingVertex(int indexVert)
  738. {
  739. for (unsigned int k=0;k<HandleV_Integer[indexVert].size();k++)
  740. {
  741. int indexV=HandleV_Integer[indexVert][k];
  742. ///get faces sharing vertex
  743. std::vector<int> faces = VF[indexVert];
  744. std::vector<int> indexes = VFi[indexVert];
  745. for (unsigned int j=0;j<faces.size();j++)
  746. {
  747. int f = faces[j];
  748. int index = indexes[j];
  749. assert(F(f,index) == indexVert);
  750. assert((index>=0)&&(index<3));
  751. if (HandleS_Index(f,index) == indexV)
  752. return;
  753. }
  754. }
  755. assert(0);
  756. }
  757. ///check consistency of variable mapping across seams
  758. template <typename DerivedV, typename DerivedF>
  759. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::TestSeamMapping()
  760. {
  761. printf("\n TESTING SEAM INDEXES \n");
  762. ///test F-V mapping
  763. for (unsigned int j=0;j<F.rows();j++)
  764. TestSeamMappingFace(j);
  765. ///TEST V-F MAPPING
  766. for (unsigned int j=0;j<V.rows();j++)
  767. TestSeamMappingVertex(j);
  768. }
  769. ///vertex to variable mapping
  770. template <typename DerivedV, typename DerivedF>
  771. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::InitMapping()
  772. {
  773. //use_direction_field=_use_direction_field;
  774. IndexToVert.clear();
  775. duplicated.clear();
  776. InitMappingSeam();
  777. Handle_SystemInfo.num_vert_variables=Handle_SystemInfo.num_scalar_variables+1;
  778. ///end testing...
  779. TestSeamMapping();
  780. }
  781. template <typename DerivedV, typename DerivedF>
  782. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::InitFaceIntegerVal()
  783. {
  784. Handle_SystemInfo.num_integer_cuts=0;
  785. for (unsigned int j=0;j<F.rows();j++)
  786. {
  787. for (int k=0;k<3;k++)
  788. {
  789. if (Handle_Seams(j,k))
  790. {
  791. Handle_Integer(j,k) = Handle_SystemInfo.num_integer_cuts;
  792. Handle_SystemInfo.num_integer_cuts++;
  793. }
  794. else
  795. Handle_Integer(j,k)=-1;
  796. }
  797. }
  798. }
  799. template <typename DerivedV, typename DerivedF>
  800. IGL_INLINE void igl::comiso::VertexIndexing<DerivedV, DerivedF>::InitSeamInfo()
  801. {
  802. std::vector<std::vector<int> >lEdgeSeamInfo; //tmp
  803. // for every vertex, keep track of their adjacent vertices on seams.
  804. std::vector<std::list<int> > VVSeam(V.rows());
  805. Eigen::MatrixXi EV, FE, EF;
  806. igl::edge_topology(V, F, EV, FE, EF);
  807. for (unsigned int e=0;e<EF.rows();e++)
  808. {
  809. int f0 = EF(e,0);
  810. int f1 = EF(e,1);
  811. if (f1 == -1)
  812. continue;
  813. int k=0;
  814. while(k<3)
  815. {
  816. if(FE(f0,k) == e)
  817. break;
  818. k++;
  819. }
  820. bool seam = Handle_Seams(f0,k);
  821. if (seam)
  822. {
  823. int v0 = F(f0, k);
  824. int v1 = F(f0, (k+1)%3);
  825. VVSeam[v0].push_back(v1);
  826. VVSeam[v1].push_back(v0);
  827. }
  828. }
  829. // Find start vertices
  830. std::vector<int> startVertices;
  831. std::vector<bool> isStartVertex(V.rows());
  832. for (unsigned int i=0;i<V.rows();i++)
  833. {
  834. isStartVertex[i] = false;
  835. if (VVSeam[i].size() > 0 && VVSeam[i].size() != 2)
  836. {
  837. startVertices.push_back(i);
  838. isStartVertex[i] = true;
  839. }
  840. }
  841. for (unsigned int i=0;i<startVertices.size();i++)
  842. {
  843. auto startVertex = &VVSeam[startVertices[i]];
  844. for (unsigned int j=0;j<startVertex->size();j++)
  845. {
  846. auto currentVertex = startVertex;
  847. int currentVertexIndex = startVertices[i];
  848. std::vector<int> thisSeam;
  849. thisSeam.push_back(currentVertexIndex);
  850. // walk along the seam
  851. int nextVertexIndex = currentVertex->front();
  852. currentVertex->pop_front();
  853. int prevVertexIndex;
  854. while (true)
  855. {
  856. // update indices (move to the next vertex)
  857. prevVertexIndex = currentVertexIndex;
  858. currentVertexIndex = nextVertexIndex;
  859. currentVertex = &VVSeam[nextVertexIndex];
  860. // add current vertex to this seam
  861. thisSeam.push_back(currentVertexIndex);
  862. // remove the previous vertex
  863. auto it = std::find(currentVertex->begin(), currentVertex->end(), prevVertexIndex);
  864. currentVertex->erase(it);
  865. if (currentVertex->size() == 1 && !isStartVertex[currentVertexIndex])
  866. {
  867. nextVertexIndex = currentVertex->front();
  868. currentVertex->pop_front();
  869. }
  870. else
  871. break;
  872. }
  873. lEdgeSeamInfo.push_back(thisSeam);
  874. }
  875. }
  876. Handle_SystemInfo.EdgeSeamInfo.clear();
  877. for (unsigned int f0=0;f0<F.rows();f0++)
  878. {
  879. for (int k=0;k<3;k++)
  880. {
  881. int f1 = TT(f0,k);
  882. if (f1 == -1)
  883. continue;
  884. bool seam = Handle_Seams(f0,k);
  885. if (seam)
  886. {
  887. int v0,v0p,v1,v1p;
  888. unsigned char MM;
  889. int integerVar;
  890. GetSeamInfo(f0,f1,k,v0,v1,v0p,v1p,MM,integerVar);
  891. Handle_SystemInfo.EdgeSeamInfo.push_back(SeamInfo(v0,v1,v0p,v1p,MM,integerVar));
  892. }
  893. }
  894. }
  895. }
  896. template <typename DerivedV, typename DerivedF>
  897. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::SolvePoisson(Eigen::VectorXd Stiffness,
  898. double vector_field_scale,
  899. double grid_res,
  900. bool direct_round,
  901. int localIter,
  902. bool _integer_rounding,
  903. bool _singularity_rounding,
  904. std::vector<int> roundVertices,
  905. std::vector<std::vector<int> > hardFeatures)
  906. {
  907. Handle_Stiffness = Stiffness;
  908. //initialization of flags and data structures
  909. integer_rounding=_integer_rounding;
  910. ids_to_round.clear();
  911. clearUserConstraint();
  912. // copy the user constraints number
  913. for (size_t i = 0; i < hardFeatures.size(); ++i)
  914. {
  915. addSharpEdgeConstraint(hardFeatures[i][0],hardFeatures[i][1]);
  916. }
  917. ///Initializing Matrix
  918. int t0=clock();
  919. ///initialize the matrix ALLOCATING SPACE
  920. InitMatrix();
  921. if (DEBUGPRINT)
  922. printf("\n ALLOCATED THE MATRIX \n");
  923. ///build the laplacian system
  924. BuildLaplacianMatrix(vector_field_scale);
  925. // add seam constraints
  926. BuildSeamConstraintsExplicitTranslation();
  927. // add user defined constraints
  928. BuildUserDefinedConstraints();
  929. ////add the lagrange multiplier
  930. FixBlockedVertex();
  931. if (DEBUGPRINT)
  932. printf("\n BUILT THE MATRIX \n");
  933. if (integer_rounding)
  934. AddToRoundVertices(roundVertices);
  935. if (_singularity_rounding)
  936. AddSingularityRound();
  937. int t1=clock();
  938. if (DEBUGPRINT) printf("\n time:%d \n",t1-t0);
  939. if (DEBUGPRINT) printf("\n SOLVING \n");
  940. MixedIntegerSolve(grid_res,direct_round,localIter);
  941. int t2=clock();
  942. if (DEBUGPRINT) printf("\n time:%d \n",t2-t1);
  943. if (DEBUGPRINT) printf("\n ASSIGNING COORDS \n");
  944. MapCoords();
  945. int t3=clock();
  946. if (DEBUGPRINT) printf("\n time:%d \n",t3-t2);
  947. if (DEBUGPRINT) printf("\n FINISHED \n");
  948. }
  949. template <typename DerivedV, typename DerivedF>
  950. IGL_INLINE igl::comiso::PoissonSolver<DerivedV, DerivedF>
  951. ::PoissonSolver(const Eigen::PlainObjectBase<DerivedV> &_V,
  952. const Eigen::PlainObjectBase<DerivedF> &_F,
  953. const Eigen::PlainObjectBase<DerivedF> &_TT,
  954. const Eigen::PlainObjectBase<DerivedF> &_TTi,
  955. const Eigen::PlainObjectBase<DerivedV> &_PD1,
  956. const Eigen::PlainObjectBase<DerivedV> &_PD2,
  957. const Eigen::MatrixXi &_HandleS_Index,
  958. const Eigen::Matrix<int, Eigen::Dynamic, 1>&_Handle_Singular,
  959. const MeshSystemInfo &_Handle_SystemInfo, //todo: const?
  960. const Eigen::MatrixXi &_Handle_Seams
  961. ):
  962. V(_V),
  963. F(_F),
  964. TT(_TT),
  965. TTi(_TTi),
  966. PD1(_PD1),
  967. PD2(_PD2),
  968. HandleS_Index(_HandleS_Index),
  969. Handle_Singular(_Handle_Singular),
  970. Handle_SystemInfo(_Handle_SystemInfo),
  971. Handle_Seams(_Handle_Seams)
  972. {
  973. UV = Eigen::MatrixXd(V.rows(),2);
  974. WUV = Eigen::MatrixXd(F.rows(),6);
  975. igl::doublearea(V,F,doublearea);
  976. igl::per_face_normals(V,F,N);
  977. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  978. }
  979. ///START SYSTEM ACCESS METHODS
  980. ///add an entry to the LHS
  981. template <typename DerivedV, typename DerivedF>
  982. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::AddValA(int Xindex,
  983. int Yindex,
  984. double val)
  985. {
  986. int size=(int)S.nrows();
  987. assert(0 <= Xindex && Xindex < size);
  988. assert(0 <= Yindex && Yindex < size);
  989. S.A().addEntryReal(Xindex,Yindex,val);
  990. }
  991. ///add a complex entry to the LHS
  992. template <typename DerivedV, typename DerivedF>
  993. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::AddComplexA(int VarXindex,
  994. int VarYindex,
  995. std::complex<double> val)
  996. {
  997. int size=(int)S.nrows()/2;
  998. assert(0 <= VarXindex && VarXindex < size);
  999. assert(0 <= VarYindex && VarYindex < size);
  1000. S.A().addEntryCmplx(VarXindex,VarYindex,val);
  1001. }
  1002. ///add a velue to the RHS
  1003. template <typename DerivedV, typename DerivedF>
  1004. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::AddValB(int Xindex,
  1005. double val)
  1006. {
  1007. int size=(int)S.nrows();
  1008. assert(0 <= Xindex && Xindex < size);
  1009. S.b()[Xindex] += val;
  1010. }
  1011. ///add the area term, scalefactor is used to sum up
  1012. ///and normalize on the overlap zones
  1013. template <typename DerivedV, typename DerivedF>
  1014. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::AddAreaTerm(int index[3][3][2],double ScaleFactor)
  1015. {
  1016. const double entry = 0.5*ScaleFactor;
  1017. double val[3][3]= {
  1018. {0, entry, -entry},
  1019. {-entry, 0, entry},
  1020. {entry, -entry, 0}
  1021. };
  1022. for (int i=0;i<3;i++)
  1023. for (int j=0;j<3;j++)
  1024. {
  1025. ///add for both u and v
  1026. int Xindex=index[i][j][0]*2;
  1027. int Yindex=index[i][j][1]*2;
  1028. AddValA(Xindex+1,Yindex,-val[i][j]);
  1029. AddValA(Xindex,Yindex+1,val[i][j]);
  1030. }
  1031. }
  1032. ///given a vector of scalar values and
  1033. ///a vector of indexes add such values
  1034. ///as specified by the indexes
  1035. template <typename DerivedV, typename DerivedF>
  1036. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::AddRHS(double b[6],
  1037. int index[3])
  1038. {
  1039. for (int i=0;i<3;i++)
  1040. {
  1041. double valU=b[i*2];
  1042. double valV=b[(i*2)+1];
  1043. AddValB((index[i]*2),valU);
  1044. AddValB((index[i]*2)+1,valV);
  1045. }
  1046. }
  1047. ///add a 3x3 block matrix to the system matrix...
  1048. ///indexes are specified in the 3x3 matrix of x,y pairs
  1049. ///indexes must be multiplied by 2 cause u and v
  1050. template <typename DerivedV, typename DerivedF>
  1051. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::Add33Block(double val[3][3], int index[3][3][2])
  1052. {
  1053. for (int i=0;i<3;i++)
  1054. for (int j=0;j<3;j++)
  1055. {
  1056. ///add for both u and v
  1057. int Xindex=index[i][j][0]*2;
  1058. int Yindex=index[i][j][1]*2;
  1059. assert((unsigned)Xindex<(n_vert_vars*2));
  1060. assert((unsigned)Yindex<(n_vert_vars*2));
  1061. AddValA(Xindex,Yindex,val[i][j]);
  1062. AddValA(Xindex+1,Yindex+1,val[i][j]);
  1063. }
  1064. }
  1065. ///add a 3x3 block matrix to the system matrix...
  1066. ///indexes are specified in the 3x3 matrix of x,y pairs
  1067. ///indexes must be multiplied by 2 cause u and v
  1068. template <typename DerivedV, typename DerivedF>
  1069. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::Add44Block(double val[4][4],int index[4][4][2])
  1070. {
  1071. for (int i=0;i<4;i++)
  1072. for (int j=0;j<4;j++)
  1073. {
  1074. ///add for both u and v
  1075. int Xindex=index[i][j][0]*2;
  1076. int Yindex=index[i][j][1]*2;
  1077. assert((unsigned)Xindex<(n_vert_vars*2));
  1078. assert((unsigned)Yindex<(n_vert_vars*2));
  1079. AddValA(Xindex,Yindex,val[i][j]);
  1080. AddValA(Xindex+1,Yindex+1,val[i][j]);
  1081. }
  1082. }
  1083. ///END SYSTEM ACCESS METHODS
  1084. ///START COMMON MATH FUNCTIONS
  1085. ///return the complex encoding the rotation
  1086. ///for a given missmatch interval
  1087. template <typename DerivedV, typename DerivedF>
  1088. IGL_INLINE std::complex<double> igl::comiso::PoissonSolver<DerivedV, DerivedF>::GetRotationComplex(int interval)
  1089. {
  1090. assert((interval>=0)&&(interval<4));
  1091. switch(interval)
  1092. {
  1093. case 0:return std::complex<double>(1,0);
  1094. case 1:return std::complex<double>(0,1);
  1095. case 2:return std::complex<double>(-1,0);
  1096. default:return std::complex<double>(0,-1);
  1097. }
  1098. }
  1099. ///END COMMON MATH FUNCTIONS
  1100. ///START FIXING VERTICES
  1101. ///set a given vertex as fixed
  1102. template <typename DerivedV, typename DerivedF>
  1103. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::AddFixedVertex(int v)
  1104. {
  1105. n_fixed_vars++;
  1106. Hard_constraints.push_back(v);
  1107. }
  1108. ///find vertex to fix in case we're using
  1109. ///a vector field NB: multiple components not handled
  1110. template <typename DerivedV, typename DerivedF>
  1111. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::FindFixedVertField()
  1112. {
  1113. Hard_constraints.clear();
  1114. n_fixed_vars=0;
  1115. //fix the first singularity
  1116. for (unsigned int v=0;v<V.rows();v++)
  1117. {
  1118. if (Handle_Singular(v))
  1119. {
  1120. AddFixedVertex(v);
  1121. UV.row(v) << 0,0;
  1122. return;
  1123. }
  1124. }
  1125. ///if anything fixed fix the first
  1126. AddFixedVertex(0); // TODO HERE IT ISSSSSS
  1127. UV.row(0) << 0,0;
  1128. std::cerr << "No vertices to fix, I am fixing the first vertex to the origin!" << std::endl;
  1129. }
  1130. ///find hard constraint depending if using or not
  1131. ///a vector field
  1132. template <typename DerivedV, typename DerivedF>
  1133. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::FindFixedVert()
  1134. {
  1135. Hard_constraints.clear();
  1136. FindFixedVertField();
  1137. }
  1138. template <typename DerivedV, typename DerivedF>
  1139. IGL_INLINE int igl::comiso::PoissonSolver<DerivedV, DerivedF>::GetFirstVertexIndex(int v)
  1140. {
  1141. return HandleS_Index(VF[v][0],VFi[v][0]);
  1142. }
  1143. ///fix the vertices which are flagged as fixed
  1144. template <typename DerivedV, typename DerivedF>
  1145. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::FixBlockedVertex()
  1146. {
  1147. int offset_row = n_vert_vars*2 + num_cut_constraint*2;
  1148. unsigned int constr_num = 0;
  1149. for (unsigned int i=0;i<Hard_constraints.size();i++)
  1150. {
  1151. int v = Hard_constraints[i];
  1152. ///get first index of the vertex that must blocked
  1153. //int index=v->vertex_index[0];
  1154. int index = GetFirstVertexIndex(v);
  1155. ///multiply times 2 because of uv
  1156. int indexvert = index*2;
  1157. ///find the first free row to add the constraint
  1158. int indexRow = (offset_row+constr_num*2);
  1159. int indexCol = indexRow;
  1160. ///add fixing constraint LHS
  1161. AddValA(indexRow,indexvert,1);
  1162. AddValA(indexRow+1,indexvert+1,1);
  1163. ///add fixing constraint RHS
  1164. AddValB(indexCol, UV(v,0));
  1165. AddValB(indexCol+1,UV(v,1));
  1166. constr_num++;
  1167. }
  1168. assert(constr_num==n_fixed_vars);
  1169. }
  1170. ///END FIXING VERTICES
  1171. ///HANDLING SINGULARITY
  1172. //set the singularity round to integer location
  1173. template <typename DerivedV, typename DerivedF>
  1174. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::AddSingularityRound()
  1175. {
  1176. for (unsigned int v=0;v<V.rows();v++)
  1177. {
  1178. if (Handle_Singular(v))
  1179. {
  1180. int index0=GetFirstVertexIndex(v);
  1181. ids_to_round.push_back( index0*2 );
  1182. ids_to_round.push_back((index0*2)+1);
  1183. }
  1184. }
  1185. }
  1186. template <typename DerivedV, typename DerivedF>
  1187. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::AddToRoundVertices(std::vector<int> ids)
  1188. {
  1189. for (size_t i = 0; i < ids.size(); ++i)
  1190. {
  1191. if (ids[i] < 0 || ids[i] >= V.rows())
  1192. std::cerr << "WARNING: Ignored round vertex constraint, vertex " << ids[i] << " does not exist in the mesh." << std::endl;
  1193. int index0 = GetFirstVertexIndex(ids[i]);
  1194. ids_to_round.push_back( index0*2 );
  1195. ids_to_round.push_back((index0*2)+1);
  1196. }
  1197. }
  1198. ///START GENERIC SYSTEM FUNCTIONS
  1199. //build the laplacian matrix cyclyng over all rangemaps
  1200. //and over all faces
  1201. template <typename DerivedV, typename DerivedF>
  1202. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::BuildLaplacianMatrix(double vfscale)
  1203. {
  1204. Eigen::MatrixXd Vcut;
  1205. Eigen::MatrixXi Fcut;
  1206. igl::cut_mesh(V, F, Handle_Seams, Vcut, Fcut);
  1207. Eigen::VectorXd idx = Eigen::VectorXd::LinSpace(2, 0, 2*Vcut.size()-2);
  1208. Eigen::VectorXd idx2 = Eigen::VectorXd::LinSpace(2, 1, 2*Vcut.size()-1);
  1209. /// Compute LHS
  1210. Eigen::SparseMatrix<double> C;
  1211. igl::cotmatrix(Vcut, Fcut, C);
  1212. C = -C * Handle_Stiffness.asDiagonal();
  1213. igl::slice_into(Lhs, idx, idx, C);
  1214. igl::slice_into(Lhs, idx2, idx2, C);
  1215. /// Compute RHS
  1216. // get gradient matrix
  1217. Eigen::SparseMatrix<double> G(Fcut.rows() * 3, Vcut.rows());
  1218. igl::grad(Vcut, Fcut, G);
  1219. // get triangle weights
  1220. Eigen::VectorXd dblA(Fcut.rows());
  1221. igl::doublearea(Vcut, Fcut, dblA);
  1222. // reshape nrosy vectors
  1223. Eigen::MatrixXd u = Eigen::Map<Eigen::MatrixXd>(PD1.data(),Fcut.rows()*3,1); // this mimics a reshape at the cost of a copy.
  1224. Eigen::MatrixXd v = Eigen::Map<Eigen::MatrixXd>(PD2.data(),Fcut.rows()*3,1); // this mimics a reshape at the cost of a copy.
  1225. // multiply with weights
  1226. igl::slice_into(rhs, idx, 1, G.transpose() * dblA.replicate<3,1>().asDiagonal() * Handle_Stiffness.asDiagonal() * u * 0.5 * vfscale);
  1227. igl::slice_into(rhs, idx2, 1, G.transpose() * dblA.replicate<3,1>().asDiagonal() * Handle_Stiffness.asDiagonal() * v * 0.5 * vfscale);
  1228. }
  1229. ///find different sized of the system
  1230. template <typename DerivedV, typename DerivedF>
  1231. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::FindSizes()
  1232. {
  1233. ///find the vertex that need to be fixed
  1234. FindFixedVert();
  1235. ///REAL PART
  1236. n_vert_vars = Handle_SystemInfo.num_vert_variables;
  1237. ///INTEGER PART
  1238. ///the total number of integer variables
  1239. n_integer_vars = Handle_SystemInfo.num_integer_cuts;
  1240. ///CONSTRAINT PART
  1241. num_cut_constraint = Handle_SystemInfo.EdgeSeamInfo.size()*2;
  1242. num_constraint_equations = num_cut_constraint * 2 + n_fixed_vars * 2 + num_userdefined_constraint;
  1243. ///total variable of the system
  1244. num_total_vars = (n_vert_vars+n_integer_vars) * 2;
  1245. ///initialize matrix size
  1246. system_size = num_total_vars + num_constraint_equations;
  1247. if (DEBUGPRINT) printf("\n*** SYSTEM VARIABLES *** \n");
  1248. if (DEBUGPRINT) printf("* NUM REAL VERTEX VARIABLES %d \n",n_vert_vars);
  1249. if (DEBUGPRINT) printf("\n*** SINGULARITY *** \n ");
  1250. if (DEBUGPRINT) printf("* NUM SINGULARITY %d\n",(int)ids_to_round.size()/2);
  1251. if (DEBUGPRINT) printf("\n*** INTEGER VARIABLES *** \n");
  1252. if (DEBUGPRINT) printf("* NUM INTEGER VARIABLES %d \n",(int)n_integer_vars);
  1253. if (DEBUGPRINT) printf("\n*** CONSTRAINTS *** \n ");
  1254. if (DEBUGPRINT) printf("* NUM FIXED CONSTRAINTS %d\n",n_fixed_vars);
  1255. if (DEBUGPRINT) printf("* NUM CUTS CONSTRAINTS %d\n",num_cut_constraint);
  1256. if (DEBUGPRINT) printf("* NUM USER DEFINED CONSTRAINTS %d\n",num_userdefined_constraint);
  1257. if (DEBUGPRINT) printf("\n*** TOTAL SIZE *** \n");
  1258. if (DEBUGPRINT) printf("* TOTAL VARIABLE SIZE (WITH INTEGER TRASL) %d \n",num_total_vars);
  1259. if (DEBUGPRINT) printf("* TOTAL CONSTRAINTS %d \n",num_constraint_equations);
  1260. if (DEBUGPRINT) printf("* MATRIX SIZE %d \n",system_size);
  1261. }
  1262. template <typename DerivedV, typename DerivedF>
  1263. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::AllocateSystem()
  1264. {
  1265. Lhs.resize(system_size, system_size);
  1266. Constraints.resize(num_constraint_equations, system_size);
  1267. rhs.resize(system_size);
  1268. constraints_rhs.resize(num_constraint_equations);
  1269. printf("\n INITIALIZED SPARSE MATRIX OF %d x %d \n",system_size, system_size);
  1270. printf("\n INITIALIZED SPARSE MATRIX OF %d x %d \n",num_constraint_equations, system_size);
  1271. printf("\n INITIALIZED VECTOR OF %d x 1 \n",system_size);
  1272. printf("\n INITIALIZED VECTOR OF %d x 1 \n",num_constraint_equations);
  1273. }
  1274. ///intitialize the whole matrix
  1275. template <typename DerivedV, typename DerivedF>
  1276. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::InitMatrix()
  1277. {
  1278. FindSizes();
  1279. AllocateSystem();
  1280. }
  1281. ///map back coordinates after that
  1282. ///the system has been solved
  1283. template <typename DerivedV, typename DerivedF>
  1284. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::MapCoords()
  1285. {
  1286. ///map coords to faces
  1287. for (unsigned int f=0;f<F.rows();f++)
  1288. {
  1289. for (int k=0;k<3;k++)
  1290. {
  1291. //get the index of the variable in the system
  1292. int indexUV = HandleS_Index(f,k);
  1293. ///then get U and V coords
  1294. double U=X[indexUV*2];
  1295. double V=X[indexUV*2+1];
  1296. WUV(f,k*2 + 0) = U;
  1297. WUV(f,k*2 + 1) = V;
  1298. }
  1299. }
  1300. #if 0
  1301. ///initialize the vector of integer variables to return their values
  1302. Handle_SystemInfo.IntegerValues.resize(n_integer_vars*2);
  1303. int baseIndex = (n_vert_vars)*2;
  1304. int endIndex = baseIndex+n_integer_vars*2;
  1305. int index = 0;
  1306. for (int i=baseIndex; i<endIndex; i++)
  1307. {
  1308. ///assert that the value is an integer value
  1309. double value=X[i];
  1310. double diff = value-(int)floor(value+0.5);
  1311. assert(diff<0.00000001);
  1312. Handle_SystemInfo.IntegerValues[index] = value;
  1313. index++;
  1314. }
  1315. #endif
  1316. }
  1317. ///END GENERIC SYSTEM FUNCTIONS
  1318. ///set the constraints for the inter-range cuts
  1319. template <typename DerivedV, typename DerivedF>
  1320. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::BuildSeamConstraintsExplicitTranslation()
  1321. {
  1322. ///add constraint(s) for every seam edge (not halfedge)
  1323. int offset_row = n_vert_vars;
  1324. ///current constraint row
  1325. int constr_row = offset_row;
  1326. ///current constraint
  1327. unsigned int constr_num = 0;
  1328. for (unsigned int i=0; i<num_cut_constraint/2; i++)
  1329. {
  1330. unsigned char interval = Handle_SystemInfo.EdgeSeamInfo[i].MMatch;
  1331. if (interval==1)
  1332. interval=3;
  1333. else
  1334. if(interval==3)
  1335. interval=1;
  1336. int p0 = Handle_SystemInfo.EdgeSeamInfo[i].v0;
  1337. int p1 = Handle_SystemInfo.EdgeSeamInfo[i].v1;
  1338. int p0p = Handle_SystemInfo.EdgeSeamInfo[i].v0p;
  1339. int p1p = Handle_SystemInfo.EdgeSeamInfo[i].v1p;
  1340. std::complex<double> rot = GetRotationComplex(interval);
  1341. ///get the integer variable
  1342. int integerVar = offset_row+Handle_SystemInfo.EdgeSeamInfo[i].integerVar;
  1343. if (integer_rounding)
  1344. {
  1345. ids_to_round.push_back(integerVar*2);
  1346. ids_to_round.push_back(integerVar*2+1);
  1347. }
  1348. AddComplexA(constr_row, p0 , rot);
  1349. AddComplexA(constr_row, p0p, -1);
  1350. ///then translation...considering the rotation
  1351. ///due to substitution
  1352. AddComplexA(constr_row, integerVar, 1);
  1353. AddValB(2*constr_row ,0);
  1354. AddValB(2*constr_row+1,0);
  1355. constr_row +=1;
  1356. constr_num++;
  1357. AddComplexA(constr_row, p1, rot);
  1358. AddComplexA(constr_row, p1p, -1);
  1359. ///other translation
  1360. AddComplexA(constr_row, integerVar , 1);
  1361. AddValB(2*constr_row,0);
  1362. AddValB(2*constr_row+1,0);
  1363. constr_row +=1;
  1364. constr_num++;
  1365. }
  1366. }
  1367. ///set the constraints for the inter-range cuts
  1368. template <typename DerivedV, typename DerivedF>
  1369. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::BuildUserDefinedConstraints()
  1370. {
  1371. /// the user defined constraints are at the end
  1372. int offset_row = n_vert_vars*2 + num_cut_constraint*2 + n_fixed_vars*2;
  1373. ///current constraint row
  1374. int constr_row = offset_row;
  1375. assert(num_userdefined_constraint == userdefined_constraints.size());
  1376. for (unsigned int i=0; i<num_userdefined_constraint; i++)
  1377. {
  1378. for (unsigned int j=0; j<userdefined_constraints[i].size()-1; ++j)
  1379. {
  1380. AddValA(constr_row, j , userdefined_constraints[i][j]);
  1381. }
  1382. AddValB(constr_row,userdefined_constraints[i][userdefined_constraints[i].size()-1]);
  1383. constr_row +=1;
  1384. }
  1385. }
  1386. ///call of the mixed integer solver
  1387. template <typename DerivedV, typename DerivedF>
  1388. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::MixedIntegerSolve(double cone_grid_res,
  1389. bool direct_round,
  1390. int localIter)
  1391. {
  1392. X = std::vector<double>((n_vert_vars+n_integer_vars)*2);
  1393. ///variables part
  1394. int ScalarSize = n_vert_vars*2;
  1395. int SizeMatrix = (n_vert_vars+n_integer_vars)*2;
  1396. if (DEBUGPRINT)
  1397. printf("\n ALLOCATED X \n");
  1398. ///matrix A
  1399. gmm::col_matrix< gmm::wsvector< double > > A(SizeMatrix,SizeMatrix); // lhs matrix variables +
  1400. ///constraints part
  1401. int CsizeX = num_constraint_equations;
  1402. int CsizeY = SizeMatrix+1;
  1403. gmm::row_matrix< gmm::wsvector< double > > C(CsizeX,CsizeY); // constraints
  1404. if (DEBUGPRINT)
  1405. printf("\n ALLOCATED QMM STRUCTURES \n");
  1406. std::vector<double> rhs(SizeMatrix,0); // rhs
  1407. if (DEBUGPRINT)
  1408. printf("\n ALLOCATED RHS STRUCTURES \n");
  1409. //// copy LHS
  1410. for(int i = 0; i < (int)S.A().nentries(); ++i)
  1411. {
  1412. int row = S.A().rowind()[i];
  1413. int col = S.A().colind()[i];
  1414. int size =(int)S.nrows();
  1415. assert(0 <= row && row < size);
  1416. assert(0 <= col && col < size);
  1417. // it's either part of the matrix
  1418. if (row < ScalarSize)
  1419. {
  1420. A(row, col) += S.A().vals()[i];
  1421. }
  1422. // or it's a part of the constraint
  1423. else
  1424. {
  1425. assert ((unsigned int)row < (n_vert_vars+num_constraint_equations)*2);
  1426. int r = row - ScalarSize;
  1427. assert(r < CsizeX);
  1428. assert(col < CsizeY);
  1429. C(r , col ) += S.A().vals()[i];
  1430. }
  1431. }
  1432. if (DEBUGPRINT)
  1433. printf("\n SET %d INTEGER VALUES \n",n_integer_vars);
  1434. ///add penalization term for integer variables
  1435. double penalization = 0.000001;
  1436. int offline_index = ScalarSize;
  1437. for(unsigned int i = 0; i < (n_integer_vars)*2; ++i)
  1438. {
  1439. int index=offline_index+i;
  1440. A(index,index)=penalization;
  1441. }
  1442. if (DEBUGPRINT)
  1443. printf("\n SET RHS \n");
  1444. // copy RHS
  1445. for(int i = 0; i < (int)ScalarSize; ++i)
  1446. {
  1447. rhs[i] = S.getRHSReal(i) * cone_grid_res;
  1448. }
  1449. // copy constraint RHS
  1450. if (DEBUGPRINT)
  1451. printf("\n SET %d CONSTRAINTS \n",num_constraint_equations);
  1452. for(unsigned int i = 0; i < num_constraint_equations; ++i)
  1453. {
  1454. C(i, SizeMatrix) = -S.getRHSReal(ScalarSize + i) * cone_grid_res;
  1455. }
  1456. ///copy values back into S
  1457. COMISO::ConstrainedSolver solver;
  1458. solver.misolver().set_local_iters(localIter);
  1459. solver.misolver().set_direct_rounding(direct_round);
  1460. std::sort(ids_to_round.begin(),ids_to_round.end());
  1461. std::vector<int>::iterator new_end=std::unique(ids_to_round.begin(),ids_to_round.end());
  1462. int dist=distance(ids_to_round.begin(),new_end);
  1463. ids_to_round.resize(dist);
  1464. solver.solve( C, A, X, rhs, ids_to_round, 0.0, false, false);
  1465. }
  1466. template <typename DerivedV, typename DerivedF>
  1467. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::clearUserConstraint()
  1468. {
  1469. num_userdefined_constraint = 0;
  1470. userdefined_constraints.clear();
  1471. }
  1472. template <typename DerivedV, typename DerivedF>
  1473. IGL_INLINE void igl::comiso::PoissonSolver<DerivedV, DerivedF>::addSharpEdgeConstraint(int fid, int vid)
  1474. {
  1475. // prepare constraint
  1476. std::vector<int> c(Handle_SystemInfo.num_vert_variables*2 + 1);
  1477. for (size_t i = 0; i < c.size(); ++i)
  1478. {
  1479. c[i] = 0;
  1480. }
  1481. int v1 = F(fid,vid);
  1482. int v2 = F(fid,(vid+1)%3);
  1483. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> e = V.row(v2) - V.row(v1);
  1484. e = e.normalized();
  1485. int v1i = HandleS_Index(fid,vid);//GetFirstVertexIndex(v1);
  1486. int v2i = HandleS_Index(fid,(vid+1)%3);//GetFirstVertexIndex(v2);
  1487. double d1 = fabs(e.dot(PD1.row(fid).normalized()));
  1488. double d2 = fabs(e.dot(PD2.row(fid).normalized()));
  1489. int offset = 0;
  1490. if (d1>d2)
  1491. offset = 1;
  1492. ids_to_round.push_back((v1i * 2) + offset);
  1493. ids_to_round.push_back((v2i * 2) + offset);
  1494. // add constraint
  1495. c[(v1i * 2) + offset] = 1;
  1496. c[(v2i * 2) + offset] = -1;
  1497. // add to the user-defined constraints
  1498. num_userdefined_constraint++;
  1499. userdefined_constraints.push_back(c);
  1500. }
  1501. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1502. IGL_INLINE igl::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::MIQ_class(const Eigen::PlainObjectBase<DerivedV> &V_,
  1503. const Eigen::PlainObjectBase<DerivedF> &F_,
  1504. const Eigen::PlainObjectBase<DerivedV> &PD1_combed,
  1505. const Eigen::PlainObjectBase<DerivedV> &PD2_combed,
  1506. // const Eigen::PlainObjectBase<DerivedV> &BIS1_combed,
  1507. // const Eigen::PlainObjectBase<DerivedV> &BIS2_combed,
  1508. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_MMatch,
  1509. const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_Singular,
  1510. // const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_SingularDegree,
  1511. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_Seams,
  1512. Eigen::PlainObjectBase<DerivedU> &UV,
  1513. Eigen::PlainObjectBase<DerivedF> &FUV,
  1514. double GradientSize,
  1515. double Stiffness,
  1516. bool DirectRound,
  1517. int iter,
  1518. int localIter,
  1519. bool DoRound,
  1520. bool SingularityRound,
  1521. std::vector<int> roundVertices,
  1522. std::vector<std::vector<int> > hardFeatures):
  1523. V(V_),
  1524. F(F_)
  1525. {
  1526. igl::local_basis(V,F,B1,B2,B3);
  1527. igl::triangle_triangle_adjacency(V,F,TT,TTi);
  1528. // Prepare indexing for the linear system
  1529. VertexIndexing<DerivedV, DerivedF> VInd(V, F, TT, TTi, /*BIS1_combed, BIS2_combed,*/ Handle_MMatch, /*Handle_Singular, Handle_SingularDegree,*/ Handle_Seams);
  1530. VInd.InitMapping();
  1531. VInd.InitFaceIntegerVal();
  1532. VInd.InitSeamInfo();
  1533. // Eigen::PlainObjectBase<DerivedV> PD1_combed_for_poisson, PD2_combed_for_poisson;
  1534. // // Rotate by 90 degrees CCW
  1535. // PD1_combed_for_poisson.setZero(PD1_combed.rows(),3);
  1536. // PD2_combed_for_poisson.setZero(PD2_combed.rows(),3);
  1537. // for (unsigned i=0; i<PD1_combed.rows();++i)
  1538. // {
  1539. // double n1 = PD1_combed.row(i).norm();
  1540. // double n2 = PD2_combed.row(i).norm();
  1541. //
  1542. // double a1 = atan2(B2.row(i).dot(PD1_combed.row(i)),B1.row(i).dot(PD1_combed.row(i)));
  1543. // double a2 = atan2(B2.row(i).dot(PD2_combed.row(i)),B1.row(i).dot(PD2_combed.row(i)));
  1544. //
  1545. // // a1 += M_PI/2;
  1546. // // a2 += M_PI/2;
  1547. //
  1548. //
  1549. // PD1_combed_for_poisson.row(i) = cos(a1) * B1.row(i) + sin(a1) * B2.row(i);
  1550. // PD2_combed_for_poisson.row(i) = cos(a2) * B1.row(i) + sin(a2) * B2.row(i);
  1551. //
  1552. // PD1_combed_for_poisson.row(i) = PD1_combed_for_poisson.row(i).normalized() * n1;
  1553. // PD2_combed_for_poisson.row(i) = PD2_combed_for_poisson.row(i).normalized() * n2;
  1554. // }
  1555. // Assemble the system and solve
  1556. PoissonSolver<DerivedV, DerivedF> PSolver(V,
  1557. F,
  1558. TT,
  1559. TTi,
  1560. PD1_combed,
  1561. PD2_combed,
  1562. VInd.HandleS_Index,
  1563. /*VInd.Handle_Singular*/Handle_Singular,
  1564. VInd.Handle_SystemInfo,
  1565. VInd.Handle_Seams);
  1566. Handle_Stiffness = Eigen::VectorXd::Constant(F.rows(),1);
  1567. if (iter > 0) // do stiffening
  1568. {
  1569. for (int i=0;i<iter;i++)
  1570. {
  1571. PSolver.SolvePoisson(Handle_Stiffness, GradientSize,1.f,DirectRound,localIter,DoRound,SingularityRound,roundVertices,hardFeatures);
  1572. int nflips=NumFlips(PSolver.WUV);
  1573. bool folded = updateStiffeningJacobianDistorsion(GradientSize,PSolver.WUV);
  1574. printf("ITERATION %d FLIPS %d \n",i,nflips);
  1575. if (!folded)break;
  1576. }
  1577. }
  1578. else
  1579. {
  1580. PSolver.SolvePoisson(Handle_Stiffness,GradientSize,1.f,DirectRound,localIter,DoRound,SingularityRound,roundVertices,hardFeatures);
  1581. }
  1582. int nflips=NumFlips(PSolver.WUV);
  1583. printf("**** END OPTIMIZING #FLIPS %d ****\n",nflips);
  1584. fflush(stdout);
  1585. WUV = PSolver.WUV;
  1586. }
  1587. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1588. IGL_INLINE void igl::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::extractUV(Eigen::PlainObjectBase<DerivedU> &UV_out,
  1589. Eigen::PlainObjectBase<DerivedF> &FUV_out)
  1590. {
  1591. // int f = F.rows();
  1592. int f = WUV.rows();
  1593. unsigned vtfaceid[f*3];
  1594. std::vector<double> vtu;
  1595. std::vector<double> vtv;
  1596. std::vector<std::vector<double> > listUV;
  1597. unsigned counter = 0;
  1598. for (unsigned i=0; i<f; ++i)
  1599. {
  1600. for (unsigned j=0; j<3; ++j)
  1601. {
  1602. std::vector<double> t(3);
  1603. t[0] = WUV(i,j*2 + 0);
  1604. t[1] = WUV(i,j*2 + 1);
  1605. t[2] = counter++;
  1606. listUV.push_back(t);
  1607. }
  1608. }
  1609. std::sort(listUV.begin(),listUV.end());
  1610. counter = 0;
  1611. unsigned k = 0;
  1612. while (k < f*3)
  1613. {
  1614. double u = listUV[k][0];
  1615. double v = listUV[k][1];
  1616. unsigned id = round(listUV[k][2]);
  1617. vtfaceid[id] = counter;
  1618. vtu.push_back(u);
  1619. vtv.push_back(v);
  1620. unsigned j=1;
  1621. while(k+j < f*3 && u == listUV[k+j][0] && v == listUV[k+j][1])
  1622. {
  1623. unsigned tid = round(listUV[k+j][2]);
  1624. vtfaceid[tid] = counter;
  1625. ++j;
  1626. }
  1627. k = k+j;
  1628. counter++;
  1629. }
  1630. UV_out.resize(vtu.size(),2);
  1631. for (unsigned i=0; i<vtu.size(); ++i)
  1632. {
  1633. UV_out(i,0) = vtu[i];
  1634. UV_out(i,1) = vtv[i];
  1635. }
  1636. FUV_out.resize(f,3);
  1637. unsigned vcounter = 0;
  1638. for (unsigned i=0; i<f; ++i)
  1639. {
  1640. FUV_out(i,0) = vtfaceid[vcounter++];
  1641. FUV_out(i,1) = vtfaceid[vcounter++];
  1642. FUV_out(i,2) = vtfaceid[vcounter++];
  1643. }
  1644. }
  1645. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1646. IGL_INLINE int igl::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::NumFlips(const Eigen::MatrixXd& WUV)
  1647. {
  1648. int numFl=0;
  1649. for (unsigned int i=0;i<F.rows();i++)
  1650. {
  1651. if (IsFlipped(i, WUV))
  1652. numFl++;
  1653. }
  1654. return numFl;
  1655. }
  1656. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1657. IGL_INLINE double igl::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::Distortion(int f, double h, const Eigen::MatrixXd& WUV)
  1658. {
  1659. assert(h > 0);
  1660. Eigen::Vector2d uv0,uv1,uv2;
  1661. uv0 << WUV(f,0), WUV(f,1);
  1662. uv1 << WUV(f,2), WUV(f,3);
  1663. uv2 << WUV(f,4), WUV(f,5);
  1664. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> p0 = V.row(F(f,0));
  1665. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> p1 = V.row(F(f,1));
  1666. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> p2 = V.row(F(f,2));
  1667. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> norm = (p1 - p0).cross(p2 - p0);
  1668. double area2 = norm.norm();
  1669. double area2_inv = 1.0 / area2;
  1670. norm *= area2_inv;
  1671. if (area2 > 0)
  1672. {
  1673. // Singular values of the Jacobian
  1674. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> neg_t0 = norm.cross(p2 - p1);
  1675. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> neg_t1 = norm.cross(p0 - p2);
  1676. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> neg_t2 = norm.cross(p1 - p0);
  1677. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> diffu = (neg_t0 * uv0(0) +neg_t1 *uv1(0) + neg_t2 * uv2(0) )*area2_inv;
  1678. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> diffv = (neg_t0 * uv0(1) + neg_t1*uv1(1) + neg_t2*uv2(1) )*area2_inv;
  1679. // first fundamental form
  1680. double I00 = diffu.dot(diffu); // guaranteed non-neg
  1681. double I01 = diffu.dot(diffv); // I01 = I10
  1682. double I11 = diffv.dot(diffv); // guaranteed non-neg
  1683. // eigenvalues of a 2x2 matrix
  1684. // [a00 a01]
  1685. // [a10 a11]
  1686. // 1/2 * [ (a00 + a11) +/- sqrt((a00 - a11)^2 + 4 a01 a10) ]
  1687. double trI = I00 + I11; // guaranteed non-neg
  1688. double diffDiag = I00 - I11; // guaranteed non-neg
  1689. double sqrtDet = sqrt(std::max(0.0, diffDiag*diffDiag +
  1690. 4 * I01 * I01)); // guaranteed non-neg
  1691. double sig1 = 0.5 * (trI + sqrtDet); // higher singular value
  1692. double sig2 = 0.5 * (trI - sqrtDet); // lower singular value
  1693. // Avoid sig2 < 0 due to numerical error
  1694. if (fabs(sig2) < 1.0e-8)
  1695. sig2 = 0;
  1696. assert(sig1 >= 0);
  1697. assert(sig2 >= 0);
  1698. if (sig2 < 0) {
  1699. printf("Distortion will be NaN! sig1^2 is negative (%lg)\n",
  1700. sig2);
  1701. }
  1702. // The singular values of the Jacobian are the sqrts of the
  1703. // eigenvalues of the first fundamental form.
  1704. sig1 = sqrt(sig1);
  1705. sig2 = sqrt(sig2);
  1706. // distortion
  1707. double tao = IsFlipped(f,WUV) ? -1 : 1;
  1708. double factor = tao / h;
  1709. double lam = fabs(factor * sig1 - 1) + fabs(factor * sig2 - 1);
  1710. return lam;
  1711. }
  1712. else {
  1713. return 10; // something "large"
  1714. }
  1715. }
  1716. ////////////////////////////////////////////////////////////////////////////
  1717. // Approximate the distortion laplacian using a uniform laplacian on
  1718. // the dual mesh:
  1719. // ___________
  1720. // \-1 / \-1 /
  1721. // \ / 3 \ /
  1722. // \-----/
  1723. // \-1 /
  1724. // \ /
  1725. //
  1726. // @param[in] f facet on which to compute distortion laplacian
  1727. // @param[in] h scaling factor applied to cross field
  1728. // @return distortion laplacian for f
  1729. ///////////////////////////////////////////////////////////////////////////
  1730. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1731. IGL_INLINE double igl::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::LaplaceDistortion(const int f, double h, const Eigen::MatrixXd& WUV)
  1732. {
  1733. double mydist = Distortion(f, h, WUV);
  1734. double lapl=0;
  1735. for (int i=0;i<3;i++)
  1736. {
  1737. if (TT(f,i) != -1)
  1738. lapl += (mydist - Distortion(TT(f,i), h, WUV));
  1739. }
  1740. return lapl;
  1741. }
  1742. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1743. IGL_INLINE bool igl::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::updateStiffeningJacobianDistorsion(double grad_size, const Eigen::MatrixXd& WUV)
  1744. {
  1745. bool flipped = NumFlips(WUV)>0;
  1746. if (!flipped)
  1747. return false;
  1748. double maxL=0;
  1749. double maxD=0;
  1750. if (flipped)
  1751. {
  1752. const double c = 1.0;
  1753. const double d = 5.0;
  1754. for (unsigned int i = 0; i < F.rows(); ++i)
  1755. {
  1756. double dist=Distortion(i,grad_size,WUV);
  1757. if (dist > maxD)
  1758. maxD=dist;
  1759. double absLap=fabs(LaplaceDistortion(i, grad_size,WUV));
  1760. if (absLap > maxL)
  1761. maxL = absLap;
  1762. double stiffDelta = std::min(c * absLap, d);
  1763. Handle_Stiffness[i]+=stiffDelta;
  1764. }
  1765. }
  1766. printf("Maximum Distorsion %4.4f \n",maxD);
  1767. printf("Maximum Laplacian %4.4f \n",maxL);
  1768. return flipped;
  1769. }
  1770. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1771. IGL_INLINE bool igl::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::IsFlipped(const Eigen::Vector2d &uv0,
  1772. const Eigen::Vector2d &uv1,
  1773. const Eigen::Vector2d &uv2)
  1774. {
  1775. Eigen::Vector2d e0 = (uv1-uv0);
  1776. Eigen::Vector2d e1 = (uv2-uv0);
  1777. double Area = e0(0)*e1(1) - e0(1)*e1(0);
  1778. return (Area<=0);
  1779. }
  1780. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1781. IGL_INLINE bool igl::comiso::MIQ_class<DerivedV, DerivedF, DerivedU>::IsFlipped(
  1782. const int i, const Eigen::MatrixXd& WUV)
  1783. {
  1784. Eigen::Vector2d uv0,uv1,uv2;
  1785. uv0 << WUV(i,0), WUV(i,1);
  1786. uv1 << WUV(i,2), WUV(i,3);
  1787. uv2 << WUV(i,4), WUV(i,5);
  1788. return (IsFlipped(uv0,uv1,uv2));
  1789. }
  1790. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1791. IGL_INLINE void igl::comiso::miq(
  1792. const Eigen::PlainObjectBase<DerivedV> &V,
  1793. const Eigen::PlainObjectBase<DerivedF> &F,
  1794. const Eigen::PlainObjectBase<DerivedV> &PD1_combed,
  1795. const Eigen::PlainObjectBase<DerivedV> &PD2_combed,
  1796. // const Eigen::PlainObjectBase<DerivedV> &BIS1_combed,
  1797. // const Eigen::PlainObjectBase<DerivedV> &BIS2_combed,
  1798. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_MMatch,
  1799. const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_Singular,
  1800. // const Eigen::Matrix<int, Eigen::Dynamic, 1> &Handle_SingularDegree,
  1801. const Eigen::Matrix<int, Eigen::Dynamic, 3> &Handle_Seams,
  1802. Eigen::PlainObjectBase<DerivedU> &UV,
  1803. Eigen::PlainObjectBase<DerivedF> &FUV,
  1804. double GradientSize,
  1805. double Stiffness,
  1806. bool DirectRound,
  1807. int iter,
  1808. int localIter,
  1809. bool DoRound,
  1810. bool SingularityRound,
  1811. std::vector<int> roundVertices,
  1812. std::vector<std::vector<int> > hardFeatures)
  1813. {
  1814. GradientSize = GradientSize/(V.colwise().maxCoeff()-V.colwise().minCoeff()).norm();
  1815. igl::comiso::MIQ_class<DerivedV, DerivedF, DerivedU> miq(V,
  1816. F,
  1817. PD1_combed,
  1818. PD2_combed,
  1819. // BIS1_combed,
  1820. // BIS2_combed,
  1821. Handle_MMatch,
  1822. Handle_Singular,
  1823. // Handle_SingularDegree,
  1824. Handle_Seams,
  1825. UV,
  1826. FUV,
  1827. GradientSize,
  1828. Stiffness,
  1829. DirectRound,
  1830. iter,
  1831. localIter,
  1832. DoRound,
  1833. SingularityRound,
  1834. roundVertices,
  1835. hardFeatures);
  1836. miq.extractUV(UV,FUV);
  1837. }
  1838. template <typename DerivedV, typename DerivedF, typename DerivedU>
  1839. IGL_INLINE void igl::comiso::miq(
  1840. const Eigen::PlainObjectBase<DerivedV> &V,
  1841. const Eigen::PlainObjectBase<DerivedF> &F,
  1842. const Eigen::PlainObjectBase<DerivedV> &PD1,
  1843. const Eigen::PlainObjectBase<DerivedV> &PD2,
  1844. Eigen::PlainObjectBase<DerivedU> &UV,
  1845. Eigen::PlainObjectBase<DerivedF> &FUV,
  1846. double GradientSize,
  1847. double Stiffness,
  1848. bool DirectRound,
  1849. int iter,
  1850. int localIter,
  1851. bool DoRound,
  1852. bool SingularityRound,
  1853. std::vector<int> roundVertices,
  1854. std::vector<std::vector<int> > hardFeatures)
  1855. {
  1856. // Eigen::MatrixXd PD2i = PD2;
  1857. // if (PD2i.size() == 0)
  1858. // {
  1859. // Eigen::MatrixXd B1, B2, B3;
  1860. // igl::local_basis(V,F,B1,B2,B3);
  1861. // PD2i = igl::rotate_vectors(V,Eigen::VectorXd::Constant(1,M_PI/2),B1,B2);
  1862. // }
  1863. Eigen::PlainObjectBase<DerivedV> BIS1, BIS2;
  1864. igl::compute_frame_field_bisectors(V, F, PD1, PD2, BIS1, BIS2);
  1865. Eigen::PlainObjectBase<DerivedV> BIS1_combed, BIS2_combed;
  1866. igl::comb_cross_field(V, F, BIS1, BIS2, BIS1_combed, BIS2_combed);
  1867. Eigen::PlainObjectBase<DerivedF> Handle_MMatch;
  1868. igl::cross_field_missmatch(V, F, BIS1_combed, BIS2_combed, true, Handle_MMatch);
  1869. Eigen::Matrix<int, Eigen::Dynamic, 1> isSingularity, singularityIndex;
  1870. igl::find_cross_field_singularities(V, F, Handle_MMatch, isSingularity, singularityIndex);
  1871. Eigen::Matrix<int, Eigen::Dynamic, 3> Handle_Seams;
  1872. igl::cut_mesh_from_singularities(V, F, Handle_MMatch, Handle_Seams);
  1873. Eigen::PlainObjectBase<DerivedV> PD1_combed, PD2_combed;
  1874. igl::comb_frame_field(V, F, PD1, PD2, BIS1_combed, BIS2_combed, PD1_combed, PD2_combed);
  1875. igl::comiso::miq(V,
  1876. F,
  1877. PD1_combed,
  1878. PD2_combed,
  1879. // BIS1_combed,
  1880. // BIS2_combed,
  1881. Handle_MMatch,
  1882. isSingularity,
  1883. // singularityIndex,
  1884. Handle_Seams,
  1885. UV,
  1886. FUV,
  1887. GradientSize,
  1888. Stiffness,
  1889. DirectRound,
  1890. iter,
  1891. localIter,
  1892. DoRound,
  1893. SingularityRound,
  1894. roundVertices,
  1895. hardFeatures);
  1896. }
  1897. #ifdef IGL_STATIC_LIBRARY
  1898. // Explicit template specialization
  1899. 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> > > >);
  1900. 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> > > >);
  1901. 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> > > >);
  1902. #endif