example.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  2. #define EIGEN_IM_MAD_AS_HELL_AND_IM_NOT_GOING_TO_TAKE_IT_ANYMORE
  3. #include <Eigen/Sparse>
  4. #include <Eigen/SparseExtra>
  5. using namespace Eigen;
  6. #include <cstdio>
  7. #include <iostream>
  8. using namespace std;
  9. #include "readOBJ.h"
  10. #include "print_ijv.h"
  11. #include "cotmatrix.h"
  12. #include "get_seconds.h"
  13. #include "EPS.h"
  14. using namespace igl;
  15. #include "SparseSolver.h"
  16. #include "IJV.h"
  17. // Build cotangent matrix by looping over triangles filling in eigen's dynamic
  18. // sparse matrix then casting to sparse matrix
  19. inline void cotmatrix_dyncast(
  20. const Eigen::MatrixXd & V,
  21. const Eigen::MatrixXi & F,
  22. Eigen::SparseMatrix<double>& L)
  23. {
  24. // Assumes vertices are 3D or 2D
  25. assert((V.cols() == 3) || (V.cols() == 2));
  26. int dim = V.cols();
  27. // Assumes faces are triangles
  28. assert(F.cols() == 3);
  29. Eigen::DynamicSparseMatrix<double, Eigen::RowMajor> dyn_L (V.rows(), V.rows());
  30. // THIS MAKES A HUGE DIFFERENCE 2x
  31. dyn_L.reserve(7*V.rows());
  32. // Loop over triangles
  33. for (unsigned i = 0; i < F.rows(); i++)
  34. {
  35. // Corner indices of this triangle
  36. int vi1 = F(i,0);
  37. int vi2 = F(i,1);
  38. int vi3 = F(i,2);
  39. // Grab corner positions of this triangle
  40. Eigen::Vector3d v1(V(vi1,0), V(vi1,1), V(vi1,2));
  41. Eigen::Vector3d v2(V(vi2,0), V(vi2,1), V(vi2,2));
  42. Eigen::Vector3d v3(V(vi3,0), V(vi3,1), V(vi3,2));
  43. // Compute cotangent of angles at each corner
  44. double cot1, cot2, cot3;
  45. computeCotWeights(v1, v2, v3, cot1, cot2, cot3);
  46. // Throw each corner's cotangent at opposite edge (in both directions)
  47. dyn_L.coeffRef(vi1, vi2) += cot3;
  48. dyn_L.coeffRef(vi2, vi1) += cot3;
  49. dyn_L.coeffRef(vi2, vi3) += cot1;
  50. dyn_L.coeffRef(vi3, vi2) += cot1;
  51. dyn_L.coeffRef(vi3, vi1) += cot2;
  52. dyn_L.coeffRef(vi1, vi3) += cot2;
  53. //dyn_L.coeffRef(vi1, vi1) -= cot3;
  54. //dyn_L.coeffRef(vi2, vi2) -= cot3;
  55. //dyn_L.coeffRef(vi2, vi2) -= cot1;
  56. //dyn_L.coeffRef(vi3, vi3) -= cot1;
  57. //dyn_L.coeffRef(vi3, vi3) -= cot2;
  58. //dyn_L.coeffRef(vi1, vi1) -= cot2;
  59. }
  60. for (int k=0; k < dyn_L.outerSize(); ++k)
  61. {
  62. double tmp = 0.0f;
  63. for(Eigen::DynamicSparseMatrix<double, Eigen::RowMajor>::InnerIterator it (dyn_L, k); it; ++it)
  64. {
  65. tmp += it.value();
  66. }
  67. dyn_L.coeffRef(k,k) = -tmp;
  68. }
  69. L = Eigen::SparseMatrix<double>(dyn_L);
  70. }
  71. inline bool safe_AddToIJV(SparseSolver & S, int i, int j, double v)
  72. {
  73. // Only add if not within double precision of zero
  74. if( v>DOUBLE_EPS|| v< -DOUBLE_EPS)
  75. {
  76. S.AddToIJV(i,j,v);
  77. return true;
  78. }
  79. //else
  80. return false;
  81. }
  82. inline void cotmatrix_sparsesolver(
  83. const Eigen::MatrixXd & V,
  84. const Eigen::MatrixXi & F,
  85. Eigen::SparseMatrix<double>& L)
  86. {
  87. SparseSolver ssL(V.rows(),V.rows());
  88. // loop over triangles
  89. // Loop over triangles
  90. for (int i = 0; i < F.rows(); i++)
  91. {
  92. // Corner indices of this triangle
  93. int vi1 = F(i,0);
  94. int vi2 = F(i,1);
  95. int vi3 = F(i,2);
  96. // Grab corner positions of this triangle
  97. Eigen::Vector3d v1(V(vi1,0), V(vi1,1), V(vi1,2));
  98. Eigen::Vector3d v2(V(vi2,0), V(vi2,1), V(vi2,2));
  99. Eigen::Vector3d v3(V(vi3,0), V(vi3,1), V(vi3,2));
  100. // Compute cotangent of angles at each corner
  101. double cot1, cot2, cot3;
  102. computeCotWeights(v1, v2, v3, cot1, cot2, cot3);
  103. // Throw each corner's cotangent at opposite edge (in both directions)
  104. safe_AddToIJV(ssL,vi1,vi2,cot3);
  105. safe_AddToIJV(ssL,vi2,vi1,cot3);
  106. safe_AddToIJV(ssL,vi2,vi3,cot1);
  107. safe_AddToIJV(ssL,vi3,vi2,cot1);
  108. safe_AddToIJV(ssL,vi3,vi1,cot2);
  109. safe_AddToIJV(ssL,vi1,vi3,cot2);
  110. safe_AddToIJV(ssL,vi1,vi1,-cot2);
  111. safe_AddToIJV(ssL,vi1,vi1,-cot3);
  112. safe_AddToIJV(ssL,vi2,vi2,-cot3);
  113. safe_AddToIJV(ssL,vi2,vi2,-cot1);
  114. safe_AddToIJV(ssL,vi3,vi3,-cot1);
  115. safe_AddToIJV(ssL,vi3,vi3,-cot2);
  116. }
  117. }
  118. // First build adjacency list then use it to build a cotan matrix loop over
  119. // vertices in order
  120. inline void cotmatrix_adjacencylist(
  121. const Eigen::MatrixXd & V,
  122. const Eigen::MatrixXi & F,
  123. Eigen::SparseMatrix<double>& L)
  124. {
  125. // adjacency list
  126. std::vector<std::vector<int> > A;
  127. //adjacency_list(F,A);
  128. }
  129. // Build a cotmatrix by building a list of values to add in an IJV list, then
  130. // sort the IJV list and *add* them to the sparsematrix in sorted order
  131. inline void cotmatrix_sort_ijv(
  132. const Eigen::MatrixXd & V,
  133. const Eigen::MatrixXi & F,
  134. Eigen::SparseMatrix<double>& L)
  135. {
  136. // Assumes vertices are 3D or 2D
  137. assert((V.cols() == 3) || (V.cols() == 2));
  138. int dim = V.cols();
  139. // Assumes faces are triangles
  140. assert(F.cols() == 3);
  141. vector<IJV<int,double> > Lijv;
  142. // We know that we will have 12 entries per face
  143. Lijv.reserve(6*F.rows());
  144. // Loop over triangles
  145. for (unsigned i = 0; i < F.rows(); i++)
  146. {
  147. // Corner indices of this triangle
  148. int vi1 = F(i,0);
  149. int vi2 = F(i,1);
  150. int vi3 = F(i,2);
  151. // Grab corner positions of this triangle
  152. Eigen::Vector3d v1(V(vi1,0), V(vi1,1), V(vi1,2));
  153. Eigen::Vector3d v2(V(vi2,0), V(vi2,1), V(vi2,2));
  154. Eigen::Vector3d v3(V(vi3,0), V(vi3,1), V(vi3,2));
  155. // Compute cotangent of angles at each corner
  156. double cot1, cot2, cot3;
  157. computeCotWeights(v1, v2, v3, cot1, cot2, cot3);
  158. // Throw each corner's cotangent at opposite edge (in both directions)
  159. Lijv.push_back(IJV<int,double>(vi1, vi2,cot3));
  160. //Lijv.push_back(IJV<int,double>(vi2, vi1,cot3));
  161. Lijv.push_back(IJV<int,double>(vi2, vi3,cot1));
  162. //Lijv.push_back(IJV<int,double>(vi3, vi2,cot1));
  163. Lijv.push_back(IJV<int,double>(vi3, vi1,cot2));
  164. //Lijv.push_back(IJV<int,double>(vi1, vi3,cot2));
  165. //// diagonal entries
  166. Lijv.push_back(IJV<int,double>(vi1, vi1,-cot3));
  167. //Lijv.push_back(IJV<int,double>(vi2, vi2,-cot3));
  168. Lijv.push_back(IJV<int,double>(vi2, vi2,-cot1));
  169. //Lijv.push_back(IJV<int,double>(vi3, vi3,-cot1));
  170. Lijv.push_back(IJV<int,double>(vi3, vi3,-cot2));
  171. //Lijv.push_back(IJV<int,double>(vi1, vi1,-cot2));
  172. }
  173. sort(Lijv.begin(),Lijv.end());
  174. L = Eigen::SparseMatrix<double>(V.rows(),V.rows());
  175. L.reserve(7*V.rows());
  176. // Now that all the (i,j,v) triplets are sorted we can add them to the
  177. // SparseMatrix directly
  178. int i = -1;
  179. int j = -1;
  180. double v = 0;
  181. vector<IJV<int,double> >::iterator last;
  182. for(
  183. vector<IJV<int,double> >::iterator Lit = Lijv.begin();
  184. Lit != Lijv.end();
  185. Lit++)
  186. {
  187. // Sum up non-unique entries
  188. if(i == Lit->i && j == Lit->j)
  189. {
  190. last->v += Lit->v;
  191. Lit->v = 0;
  192. }else
  193. {
  194. i = Lit->i;
  195. j = Lit->j;
  196. last = Lit;
  197. }
  198. }
  199. i = -1;
  200. j = -1;
  201. // Now our ijv list is sorted and we don't have dups
  202. for(
  203. vector<IJV<int,double> >::iterator Lit = Lijv.begin();
  204. Lit != Lijv.end();
  205. Lit++)
  206. {
  207. // only consider non-zeros
  208. if(Lit->v != 0)
  209. {
  210. // If new outer then need to start
  211. if(i != Lit->i)
  212. {
  213. i = Lit->i;
  214. L.startVec(i);
  215. }
  216. L.insertBack(Lit->j,Lit->i) = Lit->v;
  217. }
  218. }
  219. L.finalize();
  220. L = L + SparseMatrix<double>(L.transpose());
  221. }
  222. int main(int argc, char * argv[])
  223. {
  224. if(argc < 2)
  225. {
  226. printf("Usage:\n ./example mesh.obj\n");
  227. return 1;
  228. }
  229. // Read in a triangle mesh
  230. MatrixXd V;
  231. MatrixXi F;
  232. readOBJ(argv[1],V,F);
  233. // Should be 3D triangle mesh
  234. assert(V.cols() == 3);
  235. assert(F.cols() == 3);
  236. // Print info about the mesh
  237. printf("#vertices: %d\n",(int)V.rows());
  238. printf("#faces: %d\n",(int)F.rows());
  239. printf("min face index: %d\n",(int)F.minCoeff());
  240. printf("max face index: %d\n",(int)F.maxCoeff());
  241. double before;
  242. int trials;
  243. int max_trials = 10;
  244. SparseMatrix<double> L;
  245. printf("cotmatrix:\n ");
  246. before = get_seconds();
  247. for(trials = 0;trials<max_trials;trials++)
  248. {
  249. cotmatrix(V,F,L);
  250. }
  251. printf("%g\n",(get_seconds()-before)/(double)trials);
  252. if(L.rows() < 10)
  253. {
  254. print_ijv(L);
  255. }
  256. printf("cotmatrix_dyncast:\n ");
  257. before = get_seconds();
  258. for(trials = 0;trials<max_trials;trials++)
  259. {
  260. cotmatrix_dyncast(V,F,L);
  261. }
  262. printf("%g\n",(get_seconds()-before)/(double)trials);
  263. if(L.rows() < 10)
  264. {
  265. print_ijv(L);
  266. }
  267. //printf("cotmatrix_sparsesolver:\n ");
  268. //before = get_seconds();
  269. //for(trials = 0;trials<max_trials;trials++)
  270. //{
  271. // cotmatrix_sparsesolver(V,F,L);
  272. //}
  273. //printf("%g\n",(get_seconds()-before)/(double)trials);
  274. printf("cotmatrix_sort_ijv:\n ");
  275. before = get_seconds();
  276. for(trials = 0;trials<max_trials;trials++)
  277. {
  278. cotmatrix_sort_ijv(V,F,L);
  279. }
  280. printf("%g\n",(get_seconds()-before)/(double)trials);
  281. if(L.rows() < 10)
  282. {
  283. print_ijv(L);
  284. }
  285. return 0;
  286. }