arap_linear_block.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "arap_linear_block.h"
  9. #include "verbose.h"
  10. #include "cotmatrix_entries.h"
  11. #include <Eigen/Dense>
  12. template <typename MatV, typename MatF, typename MatK>
  13. IGL_INLINE void igl::arap_linear_block(
  14. const MatV & V,
  15. const MatF & F,
  16. const int d,
  17. const igl::ARAPEnergyType energy,
  18. MatK & Kd)
  19. {
  20. switch(energy)
  21. {
  22. case ARAP_ENERGY_TYPE_SPOKES:
  23. return igl::arap_linear_block_spokes(V,F,d,Kd);
  24. break;
  25. case ARAP_ENERGY_TYPE_SPOKES_AND_RIMS:
  26. return igl::arap_linear_block_spokes_and_rims(V,F,d,Kd);
  27. break;
  28. case ARAP_ENERGY_TYPE_ELEMENTS:
  29. return igl::arap_linear_block_elements(V,F,d,Kd);
  30. break;
  31. default:
  32. verbose("Unsupported energy type: %d\n",energy);
  33. assert(false);
  34. }
  35. }
  36. template <typename MatV, typename MatF, typename MatK>
  37. IGL_INLINE void igl::arap_linear_block_spokes(
  38. const MatV & V,
  39. const MatF & F,
  40. const int d,
  41. MatK & Kd)
  42. {
  43. typedef typename MatK::Scalar Scalar;
  44. // simplex size (3: triangles, 4: tetrahedra)
  45. int simplex_size = F.cols();
  46. // Number of elements
  47. int m = F.rows();
  48. // Temporary output
  49. Eigen::Matrix<int ,Eigen::Dynamic,2> edges;
  50. Kd.resize(V.rows(), V.rows());
  51. std::vector<Eigen::Triplet<Scalar> > Kd_IJV;
  52. if(simplex_size == 3)
  53. {
  54. // triangles
  55. Kd.reserve(7*V.rows());
  56. Kd_IJV.reserve(7*V.rows());
  57. edges.resize(3,2);
  58. edges <<
  59. 1,2,
  60. 2,0,
  61. 0,1;
  62. }else if(simplex_size == 4)
  63. {
  64. // tets
  65. Kd.reserve(17*V.rows());
  66. Kd_IJV.reserve(17*V.rows());
  67. edges.resize(6,2);
  68. edges <<
  69. 1,2,
  70. 2,0,
  71. 0,1,
  72. 3,0,
  73. 3,1,
  74. 3,2;
  75. }
  76. // gather cotangent weights
  77. Eigen::Matrix<Scalar ,Eigen::Dynamic ,Eigen::Dynamic> C;
  78. cotmatrix_entries(V,F,C);
  79. // should have weights for each edge
  80. assert(C.cols() == edges.rows());
  81. // loop over elements
  82. for(int i = 0;i<m;i++)
  83. {
  84. // loop over edges of element
  85. for(int e = 0;e<edges.rows();e++)
  86. {
  87. int source = F(i,edges(e,0));
  88. int dest = F(i,edges(e,1));
  89. double v = 0.5*C(i,e)*(V(source,d)-V(dest,d));
  90. Kd_IJV.push_back(Eigen::Triplet<Scalar>(source,dest,v));
  91. Kd_IJV.push_back(Eigen::Triplet<Scalar>(dest,source,-v));
  92. Kd_IJV.push_back(Eigen::Triplet<Scalar>(source,source,v));
  93. Kd_IJV.push_back(Eigen::Triplet<Scalar>(dest,dest,-v));
  94. }
  95. }
  96. Kd.setFromTriplets(Kd_IJV.begin(),Kd_IJV.end());
  97. Kd.makeCompressed();
  98. }
  99. template <typename MatV, typename MatF, typename MatK>
  100. IGL_INLINE void igl::arap_linear_block_spokes_and_rims(
  101. const MatV & V,
  102. const MatF & F,
  103. const int d,
  104. MatK & Kd)
  105. {
  106. typedef typename MatK::Scalar Scalar;
  107. // simplex size (3: triangles, 4: tetrahedra)
  108. int simplex_size = F.cols();
  109. // Number of elements
  110. int m = F.rows();
  111. // Temporary output
  112. Kd.resize(V.rows(), V.rows());
  113. std::vector<Eigen::Triplet<Scalar> > Kd_IJV;
  114. Eigen::Matrix<int ,Eigen::Dynamic,2> edges;
  115. if(simplex_size == 3)
  116. {
  117. // triangles
  118. Kd.reserve(7*V.rows());
  119. Kd_IJV.reserve(7*V.rows());
  120. edges.resize(3,2);
  121. edges <<
  122. 1,2,
  123. 2,0,
  124. 0,1;
  125. }else if(simplex_size == 4)
  126. {
  127. // tets
  128. Kd.reserve(17*V.rows());
  129. Kd_IJV.reserve(17*V.rows());
  130. edges.resize(6,2);
  131. edges <<
  132. 1,2,
  133. 2,0,
  134. 0,1,
  135. 3,0,
  136. 3,1,
  137. 3,2;
  138. // Not implemented yet for tets
  139. assert(false);
  140. }
  141. // gather cotangent weights
  142. Eigen::Matrix<Scalar ,Eigen::Dynamic ,Eigen::Dynamic> C;
  143. cotmatrix_entries(V,F,C);
  144. // should have weights for each edge
  145. assert(C.cols() == edges.rows());
  146. // loop over elements
  147. for(int i = 0;i<m;i++)
  148. {
  149. // loop over edges of element
  150. for(int e = 0;e<edges.rows();e++)
  151. {
  152. int source = F(i,edges(e,0));
  153. int dest = F(i,edges(e,1));
  154. double v = C(i,e)*(V(source,d)-V(dest,d))/3.0;
  155. // loop over edges again
  156. for(int f = 0;f<edges.rows();f++)
  157. {
  158. int Rs = F(i,edges(f,0));
  159. int Rd = F(i,edges(f,1));
  160. if(Rs == source && Rd == dest)
  161. {
  162. Kd_IJV.push_back(Eigen::Triplet<Scalar>(Rs,Rd,v));
  163. Kd_IJV.push_back(Eigen::Triplet<Scalar>(Rd,Rs,-v));
  164. }else if(Rd == source)
  165. {
  166. Kd_IJV.push_back(Eigen::Triplet<Scalar>(Rd,Rs,v));
  167. }else if(Rs == dest)
  168. {
  169. Kd_IJV.push_back(Eigen::Triplet<Scalar>(Rs,Rd,-v));
  170. }
  171. }
  172. Kd_IJV.push_back(Eigen::Triplet<Scalar>(source,source,v));
  173. Kd_IJV.push_back(Eigen::Triplet<Scalar>(dest,dest,-v));
  174. }
  175. }
  176. Kd.setFromTriplets(Kd_IJV.begin(),Kd_IJV.end());
  177. Kd.makeCompressed();
  178. }
  179. template <typename MatV, typename MatF, typename MatK>
  180. IGL_INLINE void igl::arap_linear_block_elements(
  181. const MatV & V,
  182. const MatF & F,
  183. const int d,
  184. MatK & Kd)
  185. {
  186. typedef typename MatK::Scalar Scalar;
  187. // simplex size (3: triangles, 4: tetrahedra)
  188. int simplex_size = F.cols();
  189. // Number of elements
  190. int m = F.rows();
  191. // Temporary output
  192. Kd.resize(V.rows(), F.rows());
  193. std::vector<Eigen::Triplet<Scalar> > Kd_IJV;
  194. Eigen::Matrix<int ,Eigen::Dynamic,2> edges;
  195. if(simplex_size == 3)
  196. {
  197. // triangles
  198. Kd.reserve(7*V.rows());
  199. Kd_IJV.reserve(7*V.rows());
  200. edges.resize(3,2);
  201. edges <<
  202. 1,2,
  203. 2,0,
  204. 0,1;
  205. }else if(simplex_size == 4)
  206. {
  207. // tets
  208. Kd.reserve(17*V.rows());
  209. Kd_IJV.reserve(17*V.rows());
  210. edges.resize(6,2);
  211. edges <<
  212. 1,2,
  213. 2,0,
  214. 0,1,
  215. 3,0,
  216. 3,1,
  217. 3,2;
  218. }
  219. // gather cotangent weights
  220. Eigen::Matrix<Scalar ,Eigen::Dynamic ,Eigen::Dynamic> C;
  221. cotmatrix_entries(V,F,C);
  222. // should have weights for each edge
  223. assert(C.cols() == edges.rows());
  224. // loop over elements
  225. for(int i = 0;i<m;i++)
  226. {
  227. // loop over edges of element
  228. for(int e = 0;e<edges.rows();e++)
  229. {
  230. int source = F(i,edges(e,0));
  231. int dest = F(i,edges(e,1));
  232. double v = C(i,e)*(V(source,d)-V(dest,d));
  233. Kd_IJV.push_back(Eigen::Triplet<Scalar>(source,i,v));
  234. Kd_IJV.push_back(Eigen::Triplet<Scalar>(dest,i,-v));
  235. }
  236. }
  237. Kd.setFromTriplets(Kd_IJV.begin(),Kd_IJV.end());
  238. Kd.makeCompressed();
  239. }
  240. #ifdef IGL_STATIC_LIBRARY
  241. // Explicit template instantiation
  242. template void igl::arap_linear_block<Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >, Eigen::SparseMatrix<double, 0, int> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, int, igl::ARAPEnergyType, Eigen::SparseMatrix<double, 0, int>&);
  243. template void igl::arap_linear_block<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::SparseMatrix<double, 0, int> >(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, int, igl::ARAPEnergyType, Eigen::SparseMatrix<double, 0, int>&);
  244. #endif