miq.cpp 59 KB

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