readBF.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "readBF.h"
  2. #include "list_to_matrix.h"
  3. #include <vector>
  4. #include <cstdio>
  5. #include <fstream>
  6. #include <cassert>
  7. #include <functional>
  8. template <
  9. typename DerivedWI,
  10. typename DerivedP,
  11. typename DerivedO>
  12. IGL_INLINE bool igl::readBF(
  13. const std::string & filename,
  14. Eigen::PlainObjectBase<DerivedWI> & WI,
  15. Eigen::PlainObjectBase<DerivedP> & P,
  16. Eigen::PlainObjectBase<DerivedO> & O)
  17. {
  18. using namespace std;
  19. ifstream is(filename);
  20. if(!is.is_open())
  21. {
  22. return false;
  23. }
  24. string line;
  25. std::vector<typename DerivedWI::Scalar> vWI;
  26. std::vector<typename DerivedP::Scalar> vP;
  27. std::vector<std::vector<typename DerivedO::Scalar> > vO;
  28. while(getline(is, line))
  29. {
  30. int wi,p;
  31. double cx,cy,cz;
  32. if(sscanf(line.c_str(), "%d %d %lg %lg %lg",&wi,&p,&cx,&cy,&cz) != 5)
  33. {
  34. return false;
  35. }
  36. vWI.push_back(wi);
  37. vP.push_back(p);
  38. vO.push_back({cx,cy,cz});
  39. }
  40. list_to_matrix(vWI,WI);
  41. list_to_matrix(vP,P);
  42. list_to_matrix(vO,O);
  43. return true;
  44. }
  45. template <
  46. typename DerivedWI,
  47. typename DerivedbfP,
  48. typename DerivedO,
  49. typename DerivedC,
  50. typename DerivedBE,
  51. typename DerivedP>
  52. IGL_INLINE bool igl::readBF(
  53. const std::string & filename,
  54. Eigen::PlainObjectBase<DerivedWI> & WI,
  55. Eigen::PlainObjectBase<DerivedbfP> & bfP,
  56. Eigen::PlainObjectBase<DerivedO> & offsets,
  57. Eigen::PlainObjectBase<DerivedC> & C,
  58. Eigen::PlainObjectBase<DerivedBE> & BE,
  59. Eigen::PlainObjectBase<DerivedP> & P)
  60. {
  61. using namespace Eigen;
  62. using namespace std;
  63. if(!readBF(filename,WI,bfP,offsets))
  64. {
  65. return false;
  66. }
  67. C.resize(WI.rows(),3);
  68. vector<bool> computed(C.rows(),false);
  69. // better not be cycles in bfP
  70. std::function<Eigen::RowVector3d(const int)> locate_tip;
  71. locate_tip =
  72. [&offsets,&computed,&bfP,&locate_tip,&C](const int w)->Eigen::RowVector3d
  73. {
  74. if(w<0) return Eigen::RowVector3d(0,0,0);
  75. if(computed[w]) return C.row(w);
  76. computed[w] = true;
  77. return C.row(w) = locate_tip(bfP(w)) + offsets.row(w);
  78. };
  79. int num_roots = (bfP.array() == -1).count();
  80. BE.resize(WI.rows()-num_roots,2);
  81. P.resize(BE.rows());
  82. for(int c = 0;c<C.rows();c++)
  83. {
  84. locate_tip(c);
  85. assert(c>=0);
  86. // weight associated with this bone
  87. const int wi = WI(c);
  88. if(wi >= 0)
  89. {
  90. // index into C
  91. const int p = bfP(c);
  92. assert(p >= 0 && "No weights for roots allowed");
  93. // index into BE
  94. const int pwi = WI(p);
  95. P(wi) = pwi;
  96. BE(wi,0) = p;
  97. BE(wi,1) = c;
  98. }
  99. }
  100. return true;
  101. }
  102. #ifdef IGL_STATIC_LIBRARY
  103. template bool igl::readBF<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  104. #endif