miq.cpp 73 KB

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