miq.cpp 75 KB

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