readBF.cpp 3.5 KB

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