main.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include <igl/pathinfo.h>
  2. #include <igl/readOBJ.h>
  3. #include <igl/readOFF.h>
  4. #include <igl/readMESH.h>
  5. #include <igl/tetgen/mesh_with_skeleton.h>
  6. #include <igl/faces_first.h>
  7. #include <igl/readTGF.h>
  8. #include <igl/launch_medit.h>
  9. #include <igl/boundary_conditions.h>
  10. #include <igl/mosek/bbw.h>
  11. #include <igl/writeDMAT.h>
  12. #include <igl/writeMESH.h>
  13. #include <Eigen/Dense>
  14. #include <iostream>
  15. #include <string>
  16. // Whether medit program is install
  17. //#define WITH_MEDIT
  18. const char * USAGE=
  19. "Usage:\n"
  20. " ./bbw_demo shape{.obj|.off|.mesh} skeleton{.tgf|.bf}\n"
  21. ;
  22. // Read a surface mesh from a {.obj|.off|.mesh} files
  23. // Inputs:
  24. // mesh_filename path to {.obj|.off|.mesh} file
  25. // Outputs:
  26. // V #V by 3 list of mesh vertex positions
  27. // F #F by 3 list of triangle indices
  28. // Returns true only if successfuly able to read file
  29. bool load_mesh_from_file(
  30. const std::string mesh_filename,
  31. Eigen::MatrixXd & V,
  32. Eigen::MatrixXi & F)
  33. {
  34. using namespace std;
  35. using namespace igl;
  36. using namespace Eigen;
  37. string dirname, basename, extension, filename;
  38. pathinfo(mesh_filename,dirname,basename,extension,filename);
  39. transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  40. bool success = false;
  41. if(extension == "obj")
  42. {
  43. success = readOBJ(mesh_filename,V,F);
  44. }else if(extension == "off")
  45. {
  46. success = readOFF(mesh_filename,V,F);
  47. }else if(extension == "mesh")
  48. {
  49. // Unused Tets read from .mesh file
  50. MatrixXi Tets;
  51. success = readMESH(mesh_filename,V,Tets,F);
  52. // We're not going to use any input tets. Only the surface
  53. if(Tets.size() > 0 && F.size() == 0)
  54. {
  55. // If Tets read, but no faces then use surface of tet volume
  56. }else
  57. {
  58. // Rearrange vertices so that faces come first
  59. VectorXi IM;
  60. faces_first(V,F,IM);
  61. // Dont' bother reordering Tets, but this is how one would:
  62. //Tets =
  63. // Tets.unaryExpr(bind1st(mem_fun( static_cast<VectorXi::Scalar&
  64. // (VectorXi::*)(VectorXi::Index)>(&VectorXi::operator())),
  65. // &IM)).eval();
  66. // Don't throw away any interior vertices, since user may want weights
  67. // there
  68. }
  69. }else
  70. {
  71. cerr<<"Error: Unknown shape file format extension: ."<<extension<<endl;
  72. return false;
  73. }
  74. return success;
  75. }
  76. // Load a skeleton (bones, points and cage edges) from a {.bf|.tgf} file
  77. //
  78. // Inputs:
  79. // skel_filename path to skeleton {.bf|.tgf} file
  80. // Outputs:
  81. // C # vertices by 3 list of vertex positions
  82. // P # point-handles list of point handle indices
  83. // BE # bone-edges by 2 list of bone-edge indices
  84. // CE # cage-edges by 2 list of cage-edge indices
  85. bool load_skeleton_from_file(
  86. const std::string skel_filename,
  87. Eigen::MatrixXd & C,
  88. Eigen::VectorXi & P,
  89. Eigen::MatrixXi & BE,
  90. Eigen::MatrixXi & CE)
  91. {
  92. using namespace std;
  93. using namespace igl;
  94. using namespace Eigen;
  95. string dirname, basename, extension, filename;
  96. pathinfo(skel_filename,dirname,basename,extension,filename);
  97. transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  98. bool success = false;
  99. if(extension == "tgf")
  100. {
  101. // Phony space for unused all edges and pseudo edges
  102. MatrixXi E;
  103. MatrixXi PE;
  104. success = readTGF(skel_filename,C,E,P,BE,CE,PE);
  105. }else
  106. {
  107. cerr<<"Error: Unknown skeleton file format extension: ."<<extension<<endl;
  108. return false;
  109. }
  110. return success;
  111. }
  112. // Writes output files to /path/to/input/mesh-skeleton.dmat,
  113. // mesh-volume.dmat, mesh-volume.mesh if input mesh was
  114. // located at /path/to/input/mesh.obj and input skeleton was at
  115. // /other/path/to/input/skel.tgf
  116. //
  117. // Writes:
  118. //// mesh.dmat dense weights matrix corresponding to original input
  119. //// vertices V
  120. // mesh-volume.dmat dense weights matrix corresponding to all
  121. // vertices in tet mesh used for computation VV
  122. // mesh-volume.mesh Tet mesh used for computation
  123. //
  124. // Inputs:
  125. // mesh_filename path to {.obj|.off|.mesh} file
  126. // skel_filename path to skeleton {.bf|.tgf} file
  127. // V #V by 3 list of original mesh vertex positions
  128. // F #F by 3 list of original triangle indices
  129. // VV #VV by 3 list of tet-mesh vertex positions
  130. // TT #TT by 4 list of tetrahedra indices
  131. // FF #FF by 3 list of surface triangle indices
  132. // W #VV by #W weights matrix
  133. // Returns true on success
  134. bool save_output(
  135. const std::string mesh_filename,
  136. const std::string /*skel_filename*/,
  137. const Eigen::MatrixXd & V,
  138. const Eigen::MatrixXi & /*F*/,
  139. const Eigen::MatrixXd & VV,
  140. const Eigen::MatrixXi & TT,
  141. const Eigen::MatrixXi & FF,
  142. const Eigen::MatrixXd & W)
  143. {
  144. using namespace std;
  145. using namespace igl;
  146. using namespace Eigen;
  147. // build filename prefix out of input base names
  148. string prefix = "";
  149. {
  150. string dirname, basename, extension, filename;
  151. pathinfo(mesh_filename,dirname,basename,extension,filename);
  152. transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  153. prefix += dirname + "/" + filename;
  154. }
  155. //{
  156. // string dirname, basename, extension, filename;
  157. // pathinfo(skel_filename,dirname,basename,extension,filename);
  158. // transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  159. // prefix += "-" + filename;
  160. //}
  161. // Keep track if any fail
  162. bool success = true;
  163. //// Weights matrix for just V. Assumes V prefaces VV
  164. //MatrixXd WV = W.block(0,0,V.rows(),W.cols());
  165. //// write dmat
  166. //success &= writeDMAT(prefix + ".dmat",WV);
  167. // write volume weights dmat
  168. success &= writeDMAT(prefix + "-volume.dmat",W);
  169. // write volume mesh
  170. success &= writeMESH(prefix + "-volume.mesh",VV,TT,FF);
  171. //// write surface OBJ with pseudocolor
  172. return success;
  173. }
  174. int main(int argc, char * argv[])
  175. {
  176. using namespace std;
  177. using namespace Eigen;
  178. using namespace igl;
  179. if(argc<3)
  180. {
  181. cerr<<USAGE<<endl;
  182. return 1;
  183. }
  184. // #V by 3 list of mesh vertex positions
  185. MatrixXd V;
  186. // #F by 3 list of triangle indices
  187. MatrixXi F;
  188. // load mesh from .obj, .off or .mesh
  189. if(!load_mesh_from_file(argv[1],V,F))
  190. {
  191. return 1;
  192. }
  193. // "Skeleton" (handles) descriptors:
  194. // List of control and joint (bone endpoint) positions
  195. MatrixXd C;
  196. // List of point handles indexing C
  197. VectorXi P;
  198. // List of bone edges indexing C
  199. MatrixXi BE;
  200. // List of cage edges indexing *P*
  201. MatrixXi CE;
  202. // load skeleton (.tgf or .bf)
  203. if(!load_skeleton_from_file(argv[2],C,P,BE,CE))
  204. {
  205. return 1;
  206. }
  207. // Mesh with samples on skeleton
  208. // New vertices of tet mesh, V prefaces VV
  209. MatrixXd VV;
  210. // Tetrahedra
  211. MatrixXi TT;
  212. // New surface faces FF
  213. MatrixXi FF;
  214. if(!mesh_with_skeleton(V,F,C,P,BE,CE,10,VV,TT,FF))
  215. {
  216. return 1;
  217. }
  218. #ifdef WITH_MEDIT
  219. // If you have medit installed then it's convenient to visualize the tet mesh
  220. // at this point
  221. launch_medit(VV,TT,FF,false);
  222. #endif
  223. // Compute boundary conditions (aka fixed value constraints)
  224. // List of boundary indices (aka fixed value indices into VV)
  225. VectorXi b;
  226. // List of boundary conditions of each weight function
  227. MatrixXd bc;
  228. if(!boundary_conditions(VV,TT,C,P,BE,CE,b,bc))
  229. {
  230. return 1;
  231. }
  232. cout<<"b=["<<b<<"];"<<endl;
  233. cout<<"bc=["<<bc<<"];"<<endl;
  234. // compute BBW
  235. // Default bbw data and flags
  236. BBWData bbw_data;
  237. // Weights matrix
  238. MatrixXd W;
  239. if(!bbw(VV,TT,b,bc,bbw_data,W))
  240. {
  241. return 1;
  242. }
  243. // Save output
  244. save_output(argv[1],argv[2],V,F,VV,TT,FF,W);
  245. return 0;
  246. }