principal_curvature.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Daniele Panozzo <[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 "principal_curvature.h"
  9. #include <iostream>
  10. #include <fstream>
  11. #include <iomanip>
  12. #include <iostream>
  13. #include <queue>
  14. #include <list>
  15. #include <cmath>
  16. #include <limits>
  17. #include <Eigen/SparseCholesky>
  18. // Lib IGL includes
  19. #include "adjacency_list.h"
  20. #include "per_face_normals.h"
  21. #include "per_vertex_normals.h"
  22. #include "avg_edge_length.h"
  23. #include "vertex_triangle_adjacency.h"
  24. typedef enum
  25. {
  26. SPHERE_SEARCH,
  27. K_RING_SEARCH
  28. } searchType;
  29. typedef enum
  30. {
  31. AVERAGE,
  32. PROJ_PLANE
  33. } normalType;
  34. class CurvatureCalculator
  35. {
  36. public:
  37. /* Row number i represents the i-th vertex, whose columns are:
  38. curv[i][0] : K1
  39. curv[i][1] : K2
  40. curvDir[i][0] : PD1
  41. curvDir[i][1] : PD2
  42. */
  43. std::vector< std::vector<double> > curv;
  44. std::vector< std::vector<Eigen::Vector3d> > curvDir;
  45. bool curvatureComputed;
  46. class Quadric
  47. {
  48. public:
  49. IGL_INLINE Quadric ()
  50. {
  51. a() = b() = c() = d() = e() = 1.0;
  52. }
  53. IGL_INLINE Quadric(double av, double bv, double cv, double dv, double ev)
  54. {
  55. a() = av;
  56. b() = bv;
  57. c() = cv;
  58. d() = dv;
  59. e() = ev;
  60. }
  61. IGL_INLINE double& a() { return data[0];}
  62. IGL_INLINE double& b() { return data[1];}
  63. IGL_INLINE double& c() { return data[2];}
  64. IGL_INLINE double& d() { return data[3];}
  65. IGL_INLINE double& e() { return data[4];}
  66. double data[5];
  67. IGL_INLINE double evaluate(double u, double v)
  68. {
  69. return a()*u*u + b()*u*v + c()*v*v + d()*u + e()*v;
  70. }
  71. IGL_INLINE double du(double u, double v)
  72. {
  73. return 2.0*a()*u + b()*v + d();
  74. }
  75. IGL_INLINE double dv(double u, double v)
  76. {
  77. return 2.0*c()*v + b()*u + e();
  78. }
  79. // Why do these take u,v arguments if they're not used?
  80. IGL_INLINE double duv(double , double )
  81. {
  82. return b();
  83. }
  84. IGL_INLINE double duu(double , double )
  85. {
  86. return 2.0*a();
  87. }
  88. IGL_INLINE double dvv(double , double )
  89. {
  90. return 2.0*c();
  91. }
  92. IGL_INLINE static Quadric fit(const std::vector<Eigen::Vector3d> &VV)
  93. {
  94. assert(VV.size() >= 5);
  95. Eigen::MatrixXd A(VV.size(),5);
  96. Eigen::MatrixXd b(VV.size(),1);
  97. Eigen::MatrixXd sol(5,1);
  98. for(unsigned int c=0; c < VV.size(); ++c)
  99. {
  100. double u = VV[c][0];
  101. double v = VV[c][1];
  102. double n = VV[c][2];
  103. A(c,0) = u*u;
  104. A(c,1) = u*v;
  105. A(c,2) = v*v;
  106. A(c,3) = u;
  107. A(c,4) = v;
  108. b(c) = n;
  109. }
  110. sol=A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
  111. return Quadric(sol(0),sol(1),sol(2),sol(3),sol(4));
  112. }
  113. };
  114. public:
  115. Eigen::MatrixXd vertices;
  116. // Face list of current mesh (#F x 3) or (#F x 4)
  117. // The i-th row contains the indices of the vertices that forms the i-th face in ccw order
  118. Eigen::MatrixXi faces;
  119. std::vector<std::vector<int> > vertex_to_vertices;
  120. std::vector<std::vector<int> > vertex_to_faces;
  121. std::vector<std::vector<int> > vertex_to_faces_index;
  122. Eigen::MatrixXd face_normals;
  123. Eigen::MatrixXd vertex_normals;
  124. /* Size of the neighborhood */
  125. double sphereRadius;
  126. int kRing;
  127. bool localMode; /* Use local mode */
  128. bool projectionPlaneCheck; /* Check collected vertices on tangent plane */
  129. bool montecarlo;
  130. unsigned int montecarloN;
  131. searchType st; /* Use either a sphere search or a k-ring search */
  132. normalType nt;
  133. double lastRadius;
  134. double scaledRadius;
  135. std::string lastMeshName;
  136. /* Benchmark related variables */
  137. bool expStep; /* True if we want the radius to increase exponentially */
  138. int step; /* If expStep==false, by how much rhe radius increases on every step */
  139. int maxSize; /* The maximum limit of the radius in the benchmark */
  140. IGL_INLINE CurvatureCalculator();
  141. IGL_INLINE void init(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  142. IGL_INLINE void finalEigenStuff(int, const std::vector<Eigen::Vector3d>&, Quadric&);
  143. IGL_INLINE void fitQuadric(const Eigen::Vector3d&, const std::vector<Eigen::Vector3d>& ref, const std::vector<int>& , Quadric *);
  144. IGL_INLINE void applyProjOnPlane(const Eigen::Vector3d&, const std::vector<int>&, std::vector<int>&);
  145. IGL_INLINE void getSphere(const int, const double, std::vector<int>&, int min);
  146. IGL_INLINE void getKRing(const int, const double,std::vector<int>&);
  147. IGL_INLINE Eigen::Vector3d project(const Eigen::Vector3d&, const Eigen::Vector3d&, const Eigen::Vector3d&);
  148. IGL_INLINE void computeReferenceFrame(int, const Eigen::Vector3d&, std::vector<Eigen::Vector3d>&);
  149. IGL_INLINE void getAverageNormal(int, const std::vector<int>&, Eigen::Vector3d&);
  150. IGL_INLINE void getProjPlane(int, const std::vector<int>&, Eigen::Vector3d&);
  151. IGL_INLINE void applyMontecarlo(const std::vector<int>&,std::vector<int>*);
  152. IGL_INLINE void computeCurvature();
  153. IGL_INLINE void printCurvature(const std::string& outpath);
  154. IGL_INLINE double getAverageEdge();
  155. IGL_INLINE static int rotateForward (double *v0, double *v1, double *v2)
  156. {
  157. double t;
  158. if (std::abs(*v2) >= std::abs(*v1) && std::abs(*v2) >= std::abs(*v0))
  159. return 0;
  160. t = *v0;
  161. *v0 = *v2;
  162. *v2 = *v1;
  163. *v1 = t;
  164. return 1 + rotateForward (v0, v1, v2);
  165. }
  166. IGL_INLINE static void rotateBackward (int nr, double *v0, double *v1, double *v2)
  167. {
  168. double t;
  169. if (nr == 0)
  170. return;
  171. t = *v2;
  172. *v2 = *v0;
  173. *v0 = *v1;
  174. *v1 = t;
  175. rotateBackward (nr - 1, v0, v1, v2);
  176. }
  177. IGL_INLINE static Eigen::Vector3d chooseMax (Eigen::Vector3d n, Eigen::Vector3d abc, double ab)
  178. {
  179. int max_i;
  180. double max_sp;
  181. Eigen::Vector3d nt[8];
  182. n.normalize ();
  183. abc.normalize ();
  184. max_sp = - std::numeric_limits<double>::max();
  185. for (int i = 0; i < 4; ++i)
  186. {
  187. nt[i] = n;
  188. if (ab > 0)
  189. {
  190. switch (i)
  191. {
  192. case 0:
  193. break;
  194. case 1:
  195. nt[i][2] = -n[2];
  196. break;
  197. case 2:
  198. nt[i][0] = -n[0];
  199. nt[i][1] = -n[1];
  200. break;
  201. case 3:
  202. nt[i][0] = -n[0];
  203. nt[i][1] = -n[1];
  204. nt[i][2] = -n[2];
  205. break;
  206. }
  207. }
  208. else
  209. {
  210. switch (i)
  211. {
  212. case 0:
  213. nt[i][0] = -n[0];
  214. break;
  215. case 1:
  216. nt[i][1] = -n[1];
  217. break;
  218. case 2:
  219. nt[i][0] = -n[0];
  220. nt[i][2] = -n[2];
  221. break;
  222. case 3:
  223. nt[i][1] = -n[1];
  224. nt[i][2] = -n[2];
  225. break;
  226. }
  227. }
  228. if (nt[i].dot(abc) > max_sp)
  229. {
  230. max_sp = nt[i].dot(abc);
  231. max_i = i;
  232. }
  233. }
  234. return nt[max_i];
  235. }
  236. };
  237. class comparer
  238. {
  239. public:
  240. IGL_INLINE bool operator() (const std::pair<int, double>& lhs, const std::pair<int, double>&rhs) const
  241. {
  242. return lhs.second>rhs.second;
  243. }
  244. };
  245. IGL_INLINE CurvatureCalculator::CurvatureCalculator()
  246. {
  247. this->localMode=true;
  248. this->projectionPlaneCheck=true;
  249. this->sphereRadius=5;
  250. this->st=SPHERE_SEARCH;
  251. this->nt=AVERAGE;
  252. this->montecarlo=false;
  253. this->montecarloN=0;
  254. this->kRing=3;
  255. this->curvatureComputed=false;
  256. this->expStep=true;
  257. }
  258. IGL_INLINE void CurvatureCalculator::init(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F)
  259. {
  260. // Normalize vertices
  261. vertices = V;
  262. // vertices = vertices.array() - vertices.minCoeff();
  263. // vertices = vertices.array() / vertices.maxCoeff();
  264. // vertices = vertices.array() * (1.0/igl::avg_edge_length(V,F));
  265. faces = F;
  266. igl::adjacency_list(F, vertex_to_vertices);
  267. igl::vertex_triangle_adjacency(V, F, vertex_to_faces, vertex_to_faces_index);
  268. igl::per_face_normals(V, F, face_normals);
  269. igl::per_vertex_normals(V, F, face_normals, vertex_normals);
  270. }
  271. IGL_INLINE void CurvatureCalculator::fitQuadric(const Eigen::Vector3d& v, const std::vector<Eigen::Vector3d>& ref, const std::vector<int>& vv, Quadric *q)
  272. {
  273. std::vector<Eigen::Vector3d> points;
  274. points.reserve (vv.size());
  275. for (unsigned int i = 0; i < vv.size(); ++i) {
  276. Eigen::Vector3d cp = vertices.row(vv[i]);
  277. // vtang non e` il v tangente!!!
  278. Eigen::Vector3d vTang = cp - v;
  279. double x = vTang.dot(ref[0]);
  280. double y = vTang.dot(ref[1]);
  281. double z = vTang.dot(ref[2]);
  282. points.push_back(Eigen::Vector3d (x,y,z));
  283. }
  284. if (points.size() < 5)
  285. {
  286. assert(false && "fit function requires at least 5 points");
  287. *q = Quadric(0,0,0,0,0);
  288. }
  289. else
  290. {
  291. *q = Quadric::fit (points);
  292. }
  293. }
  294. IGL_INLINE void CurvatureCalculator::finalEigenStuff(int i, const std::vector<Eigen::Vector3d>& ref, Quadric& q)
  295. {
  296. const double a = q.a();
  297. const double b = q.b();
  298. const double c = q.c();
  299. const double d = q.d();
  300. const double e = q.e();
  301. // if (fabs(a) < 10e-8 || fabs(b) < 10e-8)
  302. // {
  303. // std::cout << "Degenerate quadric: " << i << std::endl;
  304. // }
  305. double E = 1.0 + d*d;
  306. double F = d*e;
  307. double G = 1.0 + e*e;
  308. Eigen::Vector3d n = Eigen::Vector3d(-d,-e,1.0).normalized();
  309. double L = 2.0 * a * n[2];
  310. double M = b * n[2];
  311. double N = 2 * c * n[2];
  312. // ----------------- Eigen stuff
  313. Eigen::Matrix2d m;
  314. m << L*G - M*F, M*E-L*F, M*E-L*F, N*E-M*F;
  315. m = m / (E*G-F*F);
  316. Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> eig(m);
  317. Eigen::Vector2d c_val = eig.eigenvalues();
  318. Eigen::Matrix2d c_vec = eig.eigenvectors();
  319. // std::cerr << "c_val:" << c_val << std::endl;
  320. // std::cerr << "c_vec:" << c_vec << std::endl;
  321. // std::cerr << "c_vec:" << c_vec(0) << " " << c_vec(1) << std::endl;
  322. c_val = -c_val;
  323. Eigen::Vector3d v1, v2;
  324. v1[0] = c_vec(0);
  325. v1[1] = c_vec(1);
  326. v1[2] = 0; //d * v1[0] + e * v1[1];
  327. v2[0] = c_vec(2);
  328. v2[1] = c_vec(3);
  329. v2[2] = 0; //d * v2[0] + e * v2[1];
  330. // v1 = v1.normalized();
  331. // v2 = v2.normalized();
  332. Eigen::Vector3d v1global = ref[0] * v1[0] + ref[1] * v1[1] + ref[2] * v1[2];
  333. Eigen::Vector3d v2global = ref[0] * v2[0] + ref[1] * v2[1] + ref[2] * v2[2];
  334. v1global.normalize();
  335. v2global.normalize();
  336. v1global *= c_val(0);
  337. v2global *= c_val(1);
  338. if (c_val[0] > c_val[1])
  339. {
  340. curv[i]=std::vector<double>(2);
  341. curv[i][0]=c_val(0);
  342. curv[i][1]=c_val(1);
  343. curvDir[i]=std::vector<Eigen::Vector3d>(2);
  344. curvDir[i][0]=v1global;
  345. curvDir[i][1]=v2global;
  346. }
  347. else
  348. {
  349. curv[i]=std::vector<double>(2);
  350. curv[i][0]=c_val(1);
  351. curv[i][1]=c_val(0);
  352. curvDir[i]=std::vector<Eigen::Vector3d>(2);
  353. curvDir[i][0]=v2global;
  354. curvDir[i][1]=v1global;
  355. }
  356. // ---- end Eigen stuff
  357. }
  358. IGL_INLINE void CurvatureCalculator::getKRing(const int start, const double r, std::vector<int>&vv)
  359. {
  360. int bufsize=vertices.rows();
  361. vv.reserve(bufsize);
  362. std::list<std::pair<int,int> > queue;
  363. std::vector<bool> visited(bufsize, false);
  364. queue.push_back(std::pair<int,int>(start,0));
  365. visited[start]=true;
  366. while (!queue.empty())
  367. {
  368. int toVisit=queue.front().first;
  369. int distance=queue.front().second;
  370. queue.pop_front();
  371. vv.push_back(toVisit);
  372. if(toVisit<vertex_to_vertices.size())
  373. {
  374. if (distance<(int)r)
  375. {
  376. for (unsigned int i=0; i<vertex_to_vertices[toVisit].size(); ++i)
  377. {
  378. int neighbor=vertex_to_vertices[toVisit][i];
  379. if (!visited[neighbor])
  380. {
  381. queue.push_back(std::pair<int,int> (neighbor,distance+1));
  382. visited[neighbor]=true;
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. IGL_INLINE void CurvatureCalculator::getSphere(const int start, const double r, std::vector<int> &vv, int min)
  390. {
  391. int bufsize=vertices.rows();
  392. vv.reserve(bufsize);
  393. std::list<int> queue;
  394. std::vector<bool> visited(bufsize, false);
  395. queue.push_back(start);
  396. visited[start]=true;
  397. Eigen::Vector3d me=vertices.row(start);
  398. std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, comparer > extra_candidates;
  399. while (!queue.empty())
  400. {
  401. int toVisit=queue.front();
  402. queue.pop_front();
  403. vv.push_back(toVisit);
  404. for (unsigned int i=0; i<vertex_to_vertices[toVisit].size(); ++i)
  405. {
  406. int neighbor=vertex_to_vertices[toVisit][i];
  407. if (!visited[neighbor])
  408. {
  409. Eigen::Vector3d neigh=vertices.row(neighbor);
  410. double distance=(me-neigh).norm();
  411. if (distance<r)
  412. queue.push_back(neighbor);
  413. else if ((int)vv.size()<min)
  414. extra_candidates.push(std::pair<int,double>(neighbor,distance));
  415. visited[neighbor]=true;
  416. }
  417. }
  418. }
  419. while (!extra_candidates.empty() && (int)vv.size()<min)
  420. {
  421. std::pair<int, double> cand=extra_candidates.top();
  422. extra_candidates.pop();
  423. vv.push_back(cand.first);
  424. for (unsigned int i=0; i<vertex_to_vertices[cand.first].size(); ++i)
  425. {
  426. int neighbor=vertex_to_vertices[cand.first][i];
  427. if (!visited[neighbor])
  428. {
  429. Eigen::Vector3d neigh=vertices.row(neighbor);
  430. double distance=(me-neigh).norm();
  431. extra_candidates.push(std::pair<int,double>(neighbor,distance));
  432. visited[neighbor]=true;
  433. }
  434. }
  435. }
  436. }
  437. IGL_INLINE Eigen::Vector3d CurvatureCalculator::project(const Eigen::Vector3d& v, const Eigen::Vector3d& vp, const Eigen::Vector3d& ppn)
  438. {
  439. return (vp - (ppn * ((vp - v).dot(ppn))));
  440. }
  441. IGL_INLINE void CurvatureCalculator::computeReferenceFrame(int i, const Eigen::Vector3d& normal, std::vector<Eigen::Vector3d>& ref )
  442. {
  443. Eigen::Vector3d longest_v=Eigen::Vector3d(vertices.row(vertex_to_vertices[i][0]));
  444. longest_v=(project(vertices.row(i),longest_v,normal)-Eigen::Vector3d(vertices.row(i))).normalized();
  445. /* L'ultimo asse si ottiene come prodotto vettoriale tra i due
  446. * calcolati */
  447. Eigen::Vector3d y_axis=(normal.cross(longest_v)).normalized();
  448. ref[0]=longest_v;
  449. ref[1]=y_axis;
  450. ref[2]=normal;
  451. }
  452. IGL_INLINE void CurvatureCalculator::getAverageNormal(int j, const std::vector<int>& vv, Eigen::Vector3d& normal)
  453. {
  454. normal=(vertex_normals.row(j)).normalized();
  455. if (localMode)
  456. return;
  457. for (unsigned int i=0; i<vv.size(); ++i)
  458. {
  459. normal+=vertex_normals.row(vv[i]).normalized();
  460. }
  461. normal.normalize();
  462. }
  463. IGL_INLINE void CurvatureCalculator::getProjPlane(int j, const std::vector<int>& vv, Eigen::Vector3d& ppn)
  464. {
  465. int nr;
  466. double a, b, c;
  467. double nx, ny, nz;
  468. double abcq;
  469. a = b = c = 0;
  470. if (localMode)
  471. {
  472. for (unsigned int i=0; i<vertex_to_faces.at(j).size(); ++i)
  473. {
  474. Eigen::Vector3d faceNormal=face_normals.row(vertex_to_faces.at(j).at(i));
  475. a += faceNormal[0];
  476. b += faceNormal[1];
  477. c += faceNormal[2];
  478. }
  479. }
  480. else
  481. {
  482. for (unsigned int i=0; i<vv.size(); ++i)
  483. {
  484. a+= vertex_normals.row(vv[i])[0];
  485. b+= vertex_normals.row(vv[i])[1];
  486. c+= vertex_normals.row(vv[i])[2];
  487. }
  488. }
  489. nr = rotateForward (&a, &b, &c);
  490. abcq = a*a + b*b + c*c;
  491. nx = sqrt (a*a / abcq);
  492. ny = sqrt (b*b / abcq);
  493. nz = sqrt (1 - nx*nx - ny*ny);
  494. rotateBackward (nr, &a, &b, &c);
  495. rotateBackward (nr, &nx, &ny, &nz);
  496. ppn = chooseMax (Eigen::Vector3d(nx, ny, nz), Eigen::Vector3d (a, b, c), a * b);
  497. ppn.normalize();
  498. }
  499. IGL_INLINE double CurvatureCalculator::getAverageEdge()
  500. {
  501. double sum = 0;
  502. int count = 0;
  503. for (int i = 0; i<faces.rows(); ++i)
  504. {
  505. for (short unsigned j=0; j<3; ++j)
  506. {
  507. Eigen::Vector3d p1=vertices.row(faces.row(i)[j]);
  508. Eigen::Vector3d p2=vertices.row(faces.row(i)[(j+1)%3]);
  509. double l = (p1-p2).norm();
  510. sum+=l;
  511. ++count;
  512. }
  513. }
  514. return (sum/(double)count);
  515. }
  516. IGL_INLINE void CurvatureCalculator::applyProjOnPlane(const Eigen::Vector3d& ppn, const std::vector<int>& vin, std::vector<int> &vout)
  517. {
  518. for (std::vector<int>::const_iterator vpi = vin.begin(); vpi != vin.end(); ++vpi)
  519. if (vertex_normals.row(*vpi) * ppn > 0.0)
  520. vout.push_back(*vpi);
  521. }
  522. IGL_INLINE void CurvatureCalculator::applyMontecarlo(const std::vector<int>& vin, std::vector<int> *vout)
  523. {
  524. if (montecarloN >= vin.size ())
  525. {
  526. *vout = vin;
  527. return;
  528. }
  529. float p = ((float) montecarloN) / (float) vin.size();
  530. for (std::vector<int>::const_iterator vpi = vin.begin(); vpi != vin.end(); ++vpi)
  531. {
  532. float r;
  533. if ((r = ((float)rand () / RAND_MAX)) < p)
  534. {
  535. vout->push_back(*vpi);
  536. }
  537. }
  538. }
  539. IGL_INLINE void CurvatureCalculator::computeCurvature()
  540. {
  541. //CHECK che esista la mesh
  542. const size_t vertices_count=vertices.rows();
  543. if (vertices_count ==0)
  544. return;
  545. curvDir=std::vector< std::vector<Eigen::Vector3d> >(vertices_count);
  546. curv=std::vector<std::vector<double> >(vertices_count);
  547. scaledRadius=getAverageEdge()*sphereRadius;
  548. std::vector<int> vv;
  549. std::vector<int> vvtmp;
  550. Eigen::Vector3d normal;
  551. //double time_spent;
  552. //double searchtime=0, ref_time=0, fit_time=0, final_time=0;
  553. for (size_t i=0; i<vertices_count; ++i)
  554. {
  555. vv.clear();
  556. vvtmp.clear();
  557. Eigen::Vector3d me=vertices.row(i);
  558. switch (st)
  559. {
  560. case SPHERE_SEARCH:
  561. getSphere(i,scaledRadius,vv,6);
  562. break;
  563. case K_RING_SEARCH:
  564. getKRing(i,kRing,vv);
  565. break;
  566. default:
  567. fprintf(stderr,"Error: search type not recognized");
  568. return;
  569. }
  570. if (vv.size()<6)
  571. {
  572. //std::cerr << "Could not compute curvature of radius " << scaledRadius << std::endl;
  573. continue;
  574. }
  575. if (projectionPlaneCheck)
  576. {
  577. vvtmp.reserve (vv.size ());
  578. applyProjOnPlane (vertex_normals.row(i), vv, vvtmp);
  579. if (vvtmp.size() >= 6 && vvtmp.size()<vv.size())
  580. vv = vvtmp;
  581. }
  582. switch (nt)
  583. {
  584. case AVERAGE:
  585. getAverageNormal(i,vv,normal);
  586. break;
  587. case PROJ_PLANE:
  588. getProjPlane(i,vv,normal);
  589. break;
  590. default:
  591. fprintf(stderr,"Error: normal type not recognized");
  592. return;
  593. }
  594. if (vv.size()<6)
  595. {
  596. //std::cerr << "Could not compute curvature of radius " << scaledRadius << std::endl;
  597. continue;
  598. }
  599. if (montecarlo)
  600. {
  601. if(montecarloN<6)
  602. break;
  603. vvtmp.reserve(vv.size());
  604. applyMontecarlo(vv,&vvtmp);
  605. vv=vvtmp;
  606. }
  607. if (vv.size()<6)
  608. return;
  609. std::vector<Eigen::Vector3d> ref(3);
  610. computeReferenceFrame(i,normal,ref);
  611. Quadric q;
  612. fitQuadric (me, ref, vv, &q);
  613. finalEigenStuff(i,ref,q);
  614. }
  615. lastRadius=sphereRadius;
  616. curvatureComputed=true;
  617. }
  618. IGL_INLINE void CurvatureCalculator::printCurvature(const std::string& outpath)
  619. {
  620. if (!curvatureComputed)
  621. return;
  622. std::ofstream of;
  623. of.open(outpath.c_str());
  624. if (!of)
  625. {
  626. fprintf(stderr, "Error: could not open output file %s\n", outpath.c_str());
  627. return;
  628. }
  629. int vertices_count=vertices.rows();
  630. of << vertices_count << std::endl;
  631. for (int i=0; i<vertices_count; ++i)
  632. {
  633. of << curv[i][0] << " " << curv[i][1] << " " << curvDir[i][0][0] << " " << curvDir[i][0][1] << " " << curvDir[i][0][2] << " " <<
  634. curvDir[i][1][0] << " " << curvDir[i][1][1] << " " << curvDir[i][1][2] << std::endl;
  635. }
  636. of.close();
  637. }
  638. template <
  639. typename DerivedV,
  640. typename DerivedF,
  641. typename DerivedPD1,
  642. typename DerivedPD2,
  643. typename DerivedPV1,
  644. typename DerivedPV2,
  645. typename Index>
  646. IGL_INLINE void igl::principal_curvature(
  647. const Eigen::MatrixBase<DerivedV>& V,
  648. const Eigen::MatrixBase<DerivedF>& F,
  649. Eigen::PlainObjectBase<DerivedPD1>& PD1,
  650. Eigen::PlainObjectBase<DerivedPD2>& PD2,
  651. Eigen::PlainObjectBase<DerivedPV1>& PV1,
  652. Eigen::PlainObjectBase<DerivedPV2>& PV2,
  653. std::vector<Index>& bad_vertices,
  654. unsigned radius,
  655. bool useKring)
  656. {
  657. if (radius < 2)
  658. {
  659. radius = 2;
  660. std::cout << "WARNING: igl::principal_curvature needs a radius >= 2, fixing it to 2." << std::endl;
  661. }
  662. // Preallocate memory
  663. PD1.resize(V.rows(),3);
  664. PD2.resize(V.rows(),3);
  665. // Preallocate memory
  666. PV1.resize(V.rows(),1);
  667. PV2.resize(V.rows(),1);
  668. // Precomputation
  669. CurvatureCalculator cc;
  670. cc.init(V.template cast<double>(),F.template cast<int>());
  671. cc.sphereRadius = radius;
  672. if (useKring)
  673. {
  674. cc.kRing = radius;
  675. cc.st = K_RING_SEARCH;
  676. }
  677. // Compute
  678. cc.computeCurvature();
  679. // Copy it back
  680. for (unsigned i=0; i<V.rows(); ++i)
  681. {
  682. if (!cc.curv[i].empty())
  683. {
  684. PD1.row(i) << cc.curvDir[i][0][0], cc.curvDir[i][0][1], cc.curvDir[i][0][2];
  685. PD2.row(i) << cc.curvDir[i][1][0], cc.curvDir[i][1][1], cc.curvDir[i][1][2];
  686. PD1.row(i).normalize();
  687. PD2.row(i).normalize();
  688. if (std::isnan(PD1(i,0)) || std::isnan(PD1(i,1)) || std::isnan(PD1(i,2)) || std::isnan(PD2(i,0)) || std::isnan(PD2(i,1)) || std::isnan(PD2(i,2)))
  689. {
  690. PD1.row(i) << 0,0,0;
  691. PD2.row(i) << 0,0,0;
  692. }
  693. PV1(i) = cc.curv[i][0];
  694. PV2(i) = cc.curv[i][1];
  695. if (PD1.row(i) * PD2.row(i).transpose() > 10e-6)
  696. {
  697. bad_vertices.push_back((Index)i);
  698. PD1.row(i) *= 0;
  699. PD2.row(i) *= 0;
  700. }
  701. } else {
  702. bad_vertices.push_back((Index)i);
  703. PV1(i) = 0;
  704. PV2(i) = 0;
  705. PD1.row(i) << 0,0,0;
  706. PD2.row(i) << 0,0,0;
  707. }
  708. }
  709. }
  710. template <
  711. typename DerivedV,
  712. typename DerivedF,
  713. typename DerivedPD1,
  714. typename DerivedPD2,
  715. typename DerivedPV1,
  716. typename DerivedPV2>
  717. IGL_INLINE void igl::principal_curvature(
  718. const Eigen::MatrixBase<DerivedV>& V,
  719. const Eigen::MatrixBase<DerivedF>& F,
  720. Eigen::PlainObjectBase<DerivedPD1>& PD1,
  721. Eigen::PlainObjectBase<DerivedPD2>& PD2,
  722. Eigen::PlainObjectBase<DerivedPV1>& PV1,
  723. Eigen::PlainObjectBase<DerivedPV2>& PV2,
  724. unsigned radius,
  725. bool useKring)
  726. {
  727. if (radius < 2)
  728. {
  729. radius = 2;
  730. std::cout << "WARNING: igl::principal_curvature needs a radius >= 2, fixing it to 2." << std::endl;
  731. }
  732. // Preallocate memory
  733. PD1.resize(V.rows(),3);
  734. PD2.resize(V.rows(),3);
  735. // Preallocate memory
  736. PV1.resize(V.rows(),1);
  737. PV2.resize(V.rows(),1);
  738. // Precomputation
  739. CurvatureCalculator cc;
  740. cc.init(V.template cast<double>(),F.template cast<int>());
  741. cc.sphereRadius = radius;
  742. if (useKring)
  743. {
  744. cc.kRing = radius;
  745. cc.st = K_RING_SEARCH;
  746. }
  747. // Compute
  748. cc.computeCurvature();
  749. // Copy it back
  750. for (unsigned i=0; i<V.rows(); ++i)
  751. {
  752. PD1.row(i) << cc.curvDir[i][0][0], cc.curvDir[i][0][1], cc.curvDir[i][0][2];
  753. PD2.row(i) << cc.curvDir[i][1][0], cc.curvDir[i][1][1], cc.curvDir[i][1][2];
  754. PD1.row(i).normalize();
  755. PD2.row(i).normalize();
  756. if (std::isnan(PD1(i,0)) || std::isnan(PD1(i,1)) || std::isnan(PD1(i,2)) || std::isnan(PD2(i,0)) || std::isnan(PD2(i,1)) || std::isnan(PD2(i,2)))
  757. {
  758. PD1.row(i) << 0,0,0;
  759. PD2.row(i) << 0,0,0;
  760. }
  761. PV1(i) = cc.curv[i][0];
  762. PV2(i) = cc.curv[i][1];
  763. if (PD1.row(i) * PD2.row(i).transpose() > 10e-6)
  764. {
  765. assert(false && "PRINCIPAL_CURVATURE: Something is wrong with vertex");
  766. PD1.row(i) *= 0;
  767. PD2.row(i) *= 0;
  768. }
  769. }
  770. }
  771. #ifdef IGL_STATIC_LIBRARY
  772. // Explicit template instantiation
  773. // generated by autoexplicit.sh
  774. template void igl::principal_curvature<Eigen::Matrix<double, -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<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -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<double, -1, 1, 0, -1, 1> >&, unsigned int, bool);
  775. template void igl::principal_curvature<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, unsigned int, bool);
  776. template void igl::principal_curvature<Eigen::Matrix<double, -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<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -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<double, -1, -1, 0, -1, -1> >&, unsigned int, bool);
  777. template void igl::principal_curvature<Eigen::Matrix<double, -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<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, int>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -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<double, -1, 1, 0, -1, 1> >&, std::vector<int, std::allocator<int> >&, unsigned int, bool);
  778. #endif