mixed_integer_quadrangulate.cpp 71 KB

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