main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include <igl/pathinfo.h>
  2. #include <igl/readOBJ.h>
  3. #include <igl/readOFF.h>
  4. #include <igl/readMESH.h>
  5. #include <igl/sample_edges.h>
  6. #include <igl/cat.h>
  7. #include <igl/faces_first.h>
  8. #include <igl/readTGF.h>
  9. #include <igl/tetgen/tetrahedralize.h>
  10. #include <igl/launch_medit.h>
  11. #include <igl/boundary_conditions.h>
  12. #include <igl/mosek/bbw.h>
  13. #include <igl/writeDMAT.h>
  14. #include <igl/writeMESH.h>
  15. #include <Eigen/Dense>
  16. #include <iostream>
  17. #include <string>
  18. // Whether medit program is install
  19. const bool WITH_MEDIT = true;
  20. const char * USAGE=
  21. "Usage:\n"
  22. " ./bbw_demo shape{.obj|.off|.mesh} skeleton{.tgf|.bf}\n"
  23. ;
  24. // Read a surface mesh from a {.obj|.off|.mesh} files
  25. // Inputs:
  26. // mesh_filename path to {.obj|.off|.mesh} file
  27. // Outputs:
  28. // V #V by 3 list of mesh vertex positions
  29. // F #F by 3 list of triangle indices
  30. // Returns true only if successfuly able to read file
  31. bool load_mesh_from_file(
  32. const std::string mesh_filename,
  33. Eigen::MatrixXd & V,
  34. Eigen::MatrixXi & F)
  35. {
  36. using namespace std;
  37. using namespace igl;
  38. using namespace Eigen;
  39. string dirname, basename, extension, filename;
  40. pathinfo(mesh_filename,dirname,basename,extension,filename);
  41. transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  42. bool success = false;
  43. if(extension == "obj")
  44. {
  45. success = readOBJ(mesh_filename,V,F);
  46. }else if(extension == "off")
  47. {
  48. success = readOFF(mesh_filename,V,F);
  49. }else if(extension == "mesh")
  50. {
  51. // Unused Tets read from .mesh file
  52. MatrixXi Tets;
  53. success = readMESH(mesh_filename,V,Tets,F);
  54. // We're not going to use any input tets. Only the surface
  55. if(Tets.size() > 0 && F.size() == 0)
  56. {
  57. // If Tets read, but no faces then use surface of tet volume
  58. }else
  59. {
  60. // Rearrange vertices so that faces come first
  61. VectorXi IM;
  62. faces_first(V,F,IM);
  63. // Dont' bother reordering Tets, but this is how one would:
  64. //Tets =
  65. // Tets.unaryExpr(bind1st(mem_fun( static_cast<VectorXi::Scalar&
  66. // (VectorXi::*)(VectorXi::Index)>(&VectorXi::operator())),
  67. // &IM)).eval();
  68. // Don't throw away any interior vertices, since user may want weights
  69. // there
  70. }
  71. }else
  72. {
  73. cerr<<"Error: Unknown shape file format extension: ."<<extension<<endl;
  74. return false;
  75. }
  76. return success;
  77. }
  78. // Load a skeleton (bones, points and cage edges) from a {.bf|.tgf} file
  79. //
  80. // Inputs:
  81. // skel_filename path to skeleton {.bf|.tgf} file
  82. // Outputs:
  83. // C # vertices by 3 list of vertex positions
  84. // P # point-handles list of point handle indices
  85. // BE # bone-edges by 2 list of bone-edge indices
  86. // CE # cage-edges by 2 list of cage-edge indices
  87. bool load_skeleton_from_file(
  88. const std::string skel_filename,
  89. Eigen::MatrixXd & C,
  90. Eigen::VectorXi & P,
  91. Eigen::MatrixXi & BE,
  92. Eigen::MatrixXi & CE)
  93. {
  94. using namespace std;
  95. using namespace igl;
  96. using namespace Eigen;
  97. string dirname, basename, extension, filename;
  98. pathinfo(skel_filename,dirname,basename,extension,filename);
  99. transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  100. bool success = false;
  101. if(extension == "tgf")
  102. {
  103. // Phony space for unused all edges and pseudo edges
  104. MatrixXi E;
  105. MatrixXi PE;
  106. success = readTGF(skel_filename,C,E,P,BE,CE,PE);
  107. }else
  108. {
  109. cerr<<"Error: Unknown skeleton file format extension: ."<<extension<<endl;
  110. return false;
  111. }
  112. return success;
  113. }
  114. // Mesh the interior of a given surface with tetrahedra which are graded (tend
  115. // to be small near the surface and large inside) and conform to the given
  116. // handles and samplings thereof.
  117. //
  118. // Inputs:
  119. // V #V by 3 list of mesh vertex positions
  120. // F #F by 3 list of triangle indices
  121. // C #C by 3 list of vertex positions
  122. // P #P list of point handle indices
  123. // BE #BE by 2 list of bone-edge indices
  124. // CE #CE by 2 list of cage-edge indices
  125. // Outputs:
  126. // VV #VV by 3 list of tet-mesh vertex positions
  127. // TT #TT by 4 list of tetrahedra indices
  128. // FF #FF by 3 list of surface triangle indices
  129. // Returns true only on success
  130. bool mesh_with_skeleton(
  131. const Eigen::MatrixXd & V,
  132. const Eigen::MatrixXi & F,
  133. const Eigen::MatrixXd & C,
  134. const Eigen::VectorXi & /*P*/,
  135. const Eigen::MatrixXi & BE,
  136. const Eigen::MatrixXi & CE,
  137. Eigen::MatrixXd & VV,
  138. Eigen::MatrixXi & TT,
  139. Eigen::MatrixXi & FF)
  140. {
  141. using namespace Eigen;
  142. using namespace igl;
  143. using namespace std;
  144. // Collect all edges that need samples:
  145. MatrixXi BECE = cat(1,BE,CE);
  146. MatrixXd S;
  147. // Sample each edge with 10 samples. (Choice of 10 doesn't seem to matter so
  148. // much, but could under some circumstances)
  149. sample_edges(C,BECE,10,S);
  150. // Vertices we'll constrain tet mesh to meet
  151. MatrixXd VS = cat(1,V,S);
  152. // Boundary faces
  153. MatrixXi BF;
  154. // Use tetgen to mesh the interior of surface, this assumes surface:
  155. // * has no holes
  156. // * has no non-manifold edges or vertices
  157. // * has consistent orientation
  158. // * has no self-intersections
  159. // * has no 0-volume pieces
  160. // Default settings pq100 tell tetgen to mesh interior of triangle mesh and
  161. // to produce a graded tet mesh
  162. cerr<<"tetgen begin()"<<endl;
  163. int status = tetrahedralize( VS,F,"pq100",VV,TT,FF);
  164. cerr<<"tetgen end()"<<endl;
  165. if(FF.rows() != F.rows())
  166. {
  167. // Issue a warning if the surface has changed
  168. cerr<<"mesh_with_skeleton: Warning: boundary faces != input faces"<<endl;
  169. }
  170. if(status != 0)
  171. {
  172. cerr<<
  173. "***************************************************************"<<endl<<
  174. "***************************************************************"<<endl<<
  175. "***************************************************************"<<endl<<
  176. "***************************************************************"<<endl<<
  177. "* mesh_with_skeleton: tetgen failed. Just meshing convex hull *"<<endl<<
  178. "***************************************************************"<<endl<<
  179. "***************************************************************"<<endl<<
  180. "***************************************************************"<<endl<<
  181. "***************************************************************"<<endl;
  182. // If meshing convex hull then use more regular mesh
  183. status = tetrahedralize(VS,F,"q1.414",VV,TT,FF);
  184. // I suppose this will fail if the skeleton is outside the mesh
  185. assert(FF.maxCoeff() < VV.rows());
  186. if(status != 0)
  187. {
  188. cerr<<"mesh_with_skeleton: tetgen failed again."<<endl;
  189. return false;
  190. }
  191. }
  192. // If you have medit installed then it's convenient to visualize the tet mesh
  193. // at this point
  194. if(WITH_MEDIT)
  195. {
  196. launch_medit(VV,TT,FF,false);
  197. }
  198. return true;
  199. }
  200. // Writes output files to /path/to/input/mesh-skeleton.dmat,
  201. // mesh-volume.dmat, mesh-volume.mesh if input mesh was
  202. // located at /path/to/input/mesh.obj and input skeleton was at
  203. // /other/path/to/input/skel.tgf
  204. //
  205. // Writes:
  206. //// mesh.dmat dense weights matrix corresponding to original input
  207. //// vertices V
  208. // mesh-volume.dmat dense weights matrix corresponding to all
  209. // vertices in tet mesh used for computation VV
  210. // mesh-volume.mesh Tet mesh used for computation
  211. //
  212. // Inputs:
  213. // mesh_filename path to {.obj|.off|.mesh} file
  214. // skel_filename path to skeleton {.bf|.tgf} file
  215. // V #V by 3 list of original mesh vertex positions
  216. // F #F by 3 list of original triangle indices
  217. // VV #VV by 3 list of tet-mesh vertex positions
  218. // TT #TT by 4 list of tetrahedra indices
  219. // FF #FF by 3 list of surface triangle indices
  220. // W #VV by #W weights matrix
  221. // Returns true on success
  222. bool save_output(
  223. const std::string mesh_filename,
  224. const std::string /*skel_filename*/,
  225. const Eigen::MatrixXd & V,
  226. const Eigen::MatrixXi & /*F*/,
  227. const Eigen::MatrixXd & VV,
  228. const Eigen::MatrixXi & TT,
  229. const Eigen::MatrixXi & FF,
  230. const Eigen::MatrixXd & W)
  231. {
  232. using namespace std;
  233. using namespace igl;
  234. using namespace Eigen;
  235. // build filename prefix out of input base names
  236. string prefix = "";
  237. {
  238. string dirname, basename, extension, filename;
  239. pathinfo(mesh_filename,dirname,basename,extension,filename);
  240. transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  241. prefix += dirname + "/" + filename;
  242. }
  243. //{
  244. // string dirname, basename, extension, filename;
  245. // pathinfo(skel_filename,dirname,basename,extension,filename);
  246. // transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  247. // prefix += "-" + filename;
  248. //}
  249. // Keep track if any fail
  250. bool success = true;
  251. //// Weights matrix for just V. Assumes V prefaces VV
  252. //MatrixXd WV = W.block(0,0,V.rows(),W.cols());
  253. //// write dmat
  254. //success &= writeDMAT(prefix + ".dmat",WV);
  255. // write volume weights dmat
  256. success &= writeDMAT(prefix + "-volume.dmat",W);
  257. // write volume mesh
  258. success &= writeMESH(prefix + "-volume.mesh",VV,TT,FF);
  259. //// write surface OBJ with pseudocolor
  260. return success;
  261. }
  262. int main(int argc, char * argv[])
  263. {
  264. using namespace std;
  265. using namespace Eigen;
  266. using namespace igl;
  267. if(argc<3)
  268. {
  269. cerr<<USAGE<<endl;
  270. return 1;
  271. }
  272. // #V by 3 list of mesh vertex positions
  273. MatrixXd V;
  274. // #F by 3 list of triangle indices
  275. MatrixXi F;
  276. // load mesh from .obj, .off or .mesh
  277. if(!load_mesh_from_file(argv[1],V,F))
  278. {
  279. return 1;
  280. }
  281. // "Skeleton" (handles) descriptors:
  282. // List of control and joint (bone endpoint) positions
  283. MatrixXd C;
  284. // List of point handles indexing C
  285. VectorXi P;
  286. // List of bone edges indexing C
  287. MatrixXi BE;
  288. // List of cage edges indexing *P*
  289. MatrixXi CE;
  290. // load skeleton (.tgf or .bf)
  291. if(!load_skeleton_from_file(argv[2],C,P,BE,CE))
  292. {
  293. return 1;
  294. }
  295. // Mesh with samples on skeleton
  296. // New vertices of tet mesh, V prefaces VV
  297. MatrixXd VV;
  298. // Tetrahedra
  299. MatrixXi TT;
  300. // New surface faces FF
  301. MatrixXi FF;
  302. if(!mesh_with_skeleton(V,F,C,P,BE,CE,VV,TT,FF))
  303. {
  304. return 1;
  305. }
  306. // Compute boundary conditions (aka fixed value constraints)
  307. // List of boundary indices (aka fixed value indices into VV)
  308. VectorXi b;
  309. // List of boundary conditions of each weight function
  310. MatrixXd bc;
  311. if(!boundary_conditions(VV,TT,C,P,BE,CE,b,bc))
  312. {
  313. return 1;
  314. }
  315. cout<<"b=["<<b<<"];"<<endl;
  316. cout<<"bc=["<<bc<<"];"<<endl;
  317. // compute BBW
  318. // Default bbw data and flags
  319. BBWData bbw_data;
  320. // Weights matrix
  321. MatrixXd W;
  322. if(!bbw(VV,TT,b,bc,bbw_data,W))
  323. {
  324. return 1;
  325. }
  326. // Save output
  327. save_output(argv[1],argv[2],V,F,VV,TT,FF,W);
  328. return 0;
  329. }