principal_curvature.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Daniele Panozzo <daniele.panozzo@gmail.com>
  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 <igl/adjacency_list.h>
  20. #include <igl/per_face_normals.h>
  21. #include <igl/per_vertex_normals.h>
  22. #include <igl/avg_edge_length.h>
  23. #include <igl/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] : K2
  39. curv[i][1] : K1
  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. IGL_INLINE double duv(double u, double v)
  80. {
  81. return b();
  82. }
  83. IGL_INLINE double duu(double u, double v)
  84. {
  85. return 2.0*a();
  86. }
  87. IGL_INLINE double dvv(double u, double v)
  88. {
  89. return 2.0*c();
  90. }
  91. IGL_INLINE static Quadric fit(const std::vector<Eigen::Vector3d> &VV, bool zeroDetCheck)
  92. {
  93. assert(VV.size() >= 5);
  94. if (VV.size() < 5)
  95. {
  96. std::cerr << "ASSERT FAILED! fit function requires at least 5 points: Only " << VV.size() << " were given." << std::endl;
  97. exit(0);
  98. }
  99. Eigen::MatrixXd A(VV.size(),5);
  100. Eigen::MatrixXd b(VV.size(),1);
  101. Eigen::MatrixXd sol(5,1);
  102. for(unsigned int c=0; c < VV.size(); ++c)
  103. {
  104. double u = VV[c][0];
  105. double v = VV[c][1];
  106. double n = VV[c][2];
  107. A(c,0) = u*u;
  108. A(c,1) = u*v;
  109. A(c,2) = v*v;
  110. A(c,3) = u;
  111. A(c,4) = v;
  112. b(c) = n;
  113. }
  114. sol=A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
  115. return Quadric(sol(0),sol(1),sol(2),sol(3),sol(4));
  116. }
  117. };
  118. public:
  119. Eigen::MatrixXd vertices;
  120. // Face list of current mesh (#F x 3) or (#F x 4)
  121. // The i-th row contains the indices of the vertices that forms the i-th face in ccw order
  122. Eigen::MatrixXi faces;
  123. std::vector<std::vector<int> > vertex_to_vertices;
  124. std::vector<std::vector<int> > vertex_to_faces;
  125. std::vector<std::vector<int> > vertex_to_faces_index;
  126. Eigen::MatrixXd face_normals;
  127. Eigen::MatrixXd vertex_normals;
  128. /* Size of the neighborhood */
  129. double sphereRadius;
  130. int kRing;
  131. bool localMode; /* Use local mode */
  132. bool projectionPlaneCheck; /* Check collected vertices on tangent plane */
  133. bool montecarlo;
  134. bool zeroDetCheck; /* Check if the determinant is close to zero */
  135. unsigned int montecarloN;
  136. searchType st; /* Use either a sphere search or a k-ring search */
  137. normalType nt;
  138. double lastRadius;
  139. double scaledRadius;
  140. std::string lastMeshName;
  141. /* Benchmark related variables */
  142. bool expStep; /* True if we want the radius to increase exponentially */
  143. int step; /* If expStep==false, by how much rhe radius increases on every step */
  144. int maxSize; /* The maximum limit of the radius in the benchmark */
  145. IGL_INLINE CurvatureCalculator();
  146. IGL_INLINE void init(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  147. IGL_INLINE void finalEigenStuff(int, const std::vector<Eigen::Vector3d>&, Quadric&);
  148. IGL_INLINE void fitQuadric(const Eigen::Vector3d&, const std::vector<Eigen::Vector3d>& ref, const std::vector<int>& , Quadric *);
  149. IGL_INLINE void applyProjOnPlane(const Eigen::Vector3d&, const std::vector<int>&, std::vector<int>&);
  150. IGL_INLINE void getSphere(const int, const double, std::vector<int>&, int min);
  151. IGL_INLINE void getKRing(const int, const double,std::vector<int>&);
  152. IGL_INLINE Eigen::Vector3d project(const Eigen::Vector3d&, const Eigen::Vector3d&, const Eigen::Vector3d&);
  153. IGL_INLINE void computeReferenceFrame(int, const Eigen::Vector3d&, std::vector<Eigen::Vector3d>&);
  154. IGL_INLINE void getAverageNormal(int, const std::vector<int>&, Eigen::Vector3d&);
  155. IGL_INLINE void getProjPlane(int, const std::vector<int>&, Eigen::Vector3d&);
  156. IGL_INLINE void applyMontecarlo(const std::vector<int>&,std::vector<int>*);
  157. IGL_INLINE void computeCurvature();
  158. IGL_INLINE void printCurvature(const std::string& outpath);
  159. IGL_INLINE double getAverageEdge();
  160. IGL_INLINE static int rotateForward (double *v0, double *v1, double *v2)
  161. {
  162. double t;
  163. if (std::abs(*v2) >= std::abs(*v1) && std::abs(*v2) >= std::abs(*v0))
  164. return 0;
  165. t = *v0;
  166. *v0 = *v2;
  167. *v2 = *v1;
  168. *v1 = t;
  169. return 1 + rotateForward (v0, v1, v2);
  170. }
  171. IGL_INLINE static void rotateBackward (int nr, double *v0, double *v1, double *v2)
  172. {
  173. double t;
  174. if (nr == 0)
  175. return;
  176. t = *v2;
  177. *v2 = *v0;
  178. *v0 = *v1;
  179. *v1 = t;
  180. rotateBackward (nr - 1, v0, v1, v2);
  181. }
  182. IGL_INLINE static Eigen::Vector3d chooseMax (Eigen::Vector3d n, Eigen::Vector3d abc, double ab)
  183. {
  184. int max_i;
  185. double max_sp;
  186. Eigen::Vector3d nt[8];
  187. n.normalize ();
  188. abc.normalize ();
  189. max_sp = - std::numeric_limits<double>::max();
  190. for (int i = 0; i < 4; ++i)
  191. {
  192. nt[i] = n;
  193. if (ab > 0)
  194. {
  195. switch (i)
  196. {
  197. case 0:
  198. break;
  199. case 1:
  200. nt[i][2] = -n[2];
  201. break;
  202. case 2:
  203. nt[i][0] = -n[0];
  204. nt[i][1] = -n[1];
  205. break;
  206. case 3:
  207. nt[i][0] = -n[0];
  208. nt[i][1] = -n[1];
  209. nt[i][2] = -n[2];
  210. break;
  211. }
  212. }
  213. else
  214. {
  215. switch (i)
  216. {
  217. case 0:
  218. nt[i][0] = -n[0];
  219. break;
  220. case 1:
  221. nt[i][1] = -n[1];
  222. break;
  223. case 2:
  224. nt[i][0] = -n[0];
  225. nt[i][2] = -n[2];
  226. break;
  227. case 3:
  228. nt[i][1] = -n[1];
  229. nt[i][2] = -n[2];
  230. break;
  231. }
  232. }
  233. if (nt[i].dot(abc) > max_sp)
  234. {
  235. max_sp = nt[i].dot(abc);
  236. max_i = i;
  237. }
  238. }
  239. return nt[max_i];
  240. }
  241. };
  242. class comparer
  243. {
  244. public:
  245. IGL_INLINE bool operator() (const std::pair<int, double>& lhs, const std::pair<int, double>&rhs) const
  246. {
  247. return lhs.second>rhs.second;
  248. }
  249. };
  250. IGL_INLINE CurvatureCalculator::CurvatureCalculator()
  251. {
  252. this->localMode=true;
  253. this->projectionPlaneCheck=true;
  254. this->sphereRadius=5;
  255. this->st=SPHERE_SEARCH;
  256. this->nt=AVERAGE;
  257. this->montecarlo=false;
  258. this->montecarloN=0;
  259. this->kRing=3;
  260. this->zeroDetCheck=true;
  261. this->curvatureComputed=false;
  262. this->expStep=true;
  263. }
  264. IGL_INLINE void CurvatureCalculator::init(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F)
  265. {
  266. // Normalize vertices
  267. vertices = V;
  268. // vertices = vertices.array() - vertices.minCoeff();
  269. // vertices = vertices.array() / vertices.maxCoeff();
  270. // vertices = vertices.array() * (1.0/igl::avg_edge_length(V,F));
  271. faces = F;
  272. igl::adjacency_list(F, vertex_to_vertices);
  273. igl::vertex_triangle_adjacency(V, F, vertex_to_faces, vertex_to_faces_index);
  274. igl::per_face_normals(V, F, face_normals);
  275. igl::per_vertex_normals(V, F, face_normals, vertex_normals);
  276. }
  277. IGL_INLINE void CurvatureCalculator::fitQuadric(const Eigen::Vector3d& v, const std::vector<Eigen::Vector3d>& ref, const std::vector<int>& vv, Quadric *q)
  278. {
  279. std::vector<Eigen::Vector3d> points;
  280. points.reserve (vv.size());
  281. for (unsigned int i = 0; i < vv.size(); ++i) {
  282. Eigen::Vector3d cp = vertices.row(vv[i]);
  283. // vtang non e` il v tangente!!!
  284. Eigen::Vector3d vTang = cp - v;
  285. double x = vTang.dot(ref[0]);
  286. double y = vTang.dot(ref[1]);
  287. double z = vTang.dot(ref[2]);
  288. points.push_back(Eigen::Vector3d (x,y,z));
  289. }
  290. *q = Quadric::fit (points, zeroDetCheck);
  291. }
  292. IGL_INLINE void CurvatureCalculator::finalEigenStuff(int i, const std::vector<Eigen::Vector3d>& ref, Quadric& q)
  293. {
  294. const double a = q.a();
  295. const double b = q.b();
  296. const double c = q.c();
  297. const double d = q.d();
  298. const double e = q.e();
  299. // if (fabs(a) < 10e-8 || fabs(b) < 10e-8)
  300. // {
  301. // std::cout << "Degenerate quadric: " << i << std::endl;
  302. // }
  303. double E = 1.0 + d*d;
  304. double F = d*e;
  305. double G = 1.0 + e*e;
  306. Eigen::Vector3d n = Eigen::Vector3d(-d,-e,1.0).normalized();
  307. double L = 2.0 * a * n[2];
  308. double M = b * n[2];
  309. double N = 2 * c * n[2];
  310. // ----------------- Eigen stuff
  311. Eigen::Matrix2d m;
  312. m << L*G - M*F, M*E-L*F, M*E-L*F, N*E-M*F;
  313. m = m / (E*G-F*F);
  314. Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> eig(m);
  315. Eigen::Vector2d c_val = eig.eigenvalues();
  316. Eigen::Matrix2d c_vec = eig.eigenvectors();
  317. // std::cerr << "c_val:" << c_val << std::endl;
  318. // std::cerr << "c_vec:" << c_vec << std::endl;
  319. // std::cerr << "c_vec:" << c_vec(0) << " " << c_vec(1) << std::endl;
  320. c_val = -c_val;
  321. Eigen::Vector3d v1, v2;
  322. v1[0] = c_vec(0);
  323. v1[1] = c_vec(1);
  324. v1[2] = 0; //d * v1[0] + e * v1[1];
  325. v2[0] = c_vec(2);
  326. v2[1] = c_vec(3);
  327. v2[2] = 0; //d * v2[0] + e * v2[1];
  328. // v1 = v1.normalized();
  329. // v2 = v2.normalized();
  330. Eigen::Vector3d v1global = ref[0] * v1[0] + ref[1] * v1[1] + ref[2] * v1[2];
  331. Eigen::Vector3d v2global = ref[0] * v2[0] + ref[1] * v2[1] + ref[2] * v2[2];
  332. v1global.normalize();
  333. v2global.normalize();
  334. v1global *= c_val(0);
  335. v2global *= c_val(1);
  336. if (c_val[0] > c_val[1])
  337. {
  338. curv[i]=std::vector<double>(2);
  339. curv[i][0]=c_val(1);
  340. curv[i][1]=c_val(0);
  341. curvDir[i]=std::vector<Eigen::Vector3d>(2);
  342. curvDir[i][0]=v2global;
  343. curvDir[i][1]=v1global;
  344. }
  345. else
  346. {
  347. curv[i]=std::vector<double>(2);
  348. curv[i][0]=c_val(0);
  349. curv[i][1]=c_val(1);
  350. curvDir[i]=std::vector<Eigen::Vector3d>(2);
  351. curvDir[i][0]=v1global;
  352. curvDir[i][1]=v2global;
  353. }
  354. // ---- end Eigen stuff
  355. }
  356. IGL_INLINE void CurvatureCalculator::getKRing(const int start, const double r, std::vector<int>&vv)
  357. {
  358. int bufsize=vertices.rows();
  359. vv.reserve(bufsize);
  360. std::list<std::pair<int,int> > queue;
  361. bool* visited = (bool*)calloc(bufsize,sizeof(bool));
  362. queue.push_back(std::pair<int,int>(start,0));
  363. visited[start]=true;
  364. while (!queue.empty())
  365. {
  366. int toVisit=queue.front().first;
  367. int distance=queue.front().second;
  368. queue.pop_front();
  369. vv.push_back(toVisit);
  370. if (distance<(int)r)
  371. {
  372. for (unsigned int i=0; i<vertex_to_vertices[toVisit].size(); ++i)
  373. {
  374. int neighbor=vertex_to_vertices[toVisit][i];
  375. if (!visited[neighbor])
  376. {
  377. queue.push_back(std::pair<int,int> (neighbor,distance+1));
  378. visited[neighbor]=true;
  379. }
  380. }
  381. }
  382. }
  383. free(visited);
  384. return;
  385. }
  386. IGL_INLINE void CurvatureCalculator::getSphere(const int start, const double r, std::vector<int> &vv, int min)
  387. {
  388. int bufsize=vertices.rows();
  389. vv.reserve(bufsize);
  390. std::list<int>* queue= new std::list<int>();
  391. bool* visited = (bool*)calloc(bufsize,sizeof(bool));
  392. queue->push_back(start);
  393. visited[start]=true;
  394. Eigen::Vector3d me=vertices.row(start);
  395. std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, comparer >* extra_candidates= new std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, comparer >();
  396. while (!queue->empty())
  397. {
  398. int toVisit=queue->front();
  399. queue->pop_front();
  400. vv.push_back(toVisit);
  401. for (unsigned int i=0; i<vertex_to_vertices[toVisit].size(); ++i)
  402. {
  403. int neighbor=vertex_to_vertices[toVisit][i];
  404. if (!visited[neighbor])
  405. {
  406. Eigen::Vector3d neigh=vertices.row(neighbor);
  407. double distance=(me-neigh).norm();
  408. if (distance<r)
  409. queue->push_back(neighbor);
  410. else if ((int)vv.size()<min)
  411. extra_candidates->push(std::pair<int,double>(neighbor,distance));
  412. visited[neighbor]=true;
  413. }
  414. }
  415. }
  416. while (!extra_candidates->empty() && (int)vv.size()<min)
  417. {
  418. std::pair<int, double> cand=extra_candidates->top();
  419. extra_candidates->pop();
  420. vv.push_back(cand.first);
  421. for (unsigned int i=0; i<vertex_to_vertices[cand.first].size(); ++i)
  422. {
  423. int neighbor=vertex_to_vertices[cand.first][i];
  424. if (!visited[neighbor])
  425. {
  426. Eigen::Vector3d neigh=vertices.row(neighbor);
  427. double distance=(me-neigh).norm();
  428. extra_candidates->push(std::pair<int,double>(neighbor,distance));
  429. visited[neighbor]=true;
  430. }
  431. }
  432. }
  433. free(extra_candidates);
  434. free(queue);
  435. free(visited);
  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. return;
  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. return;
  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. using namespace std;
  621. if (!curvatureComputed)
  622. return;
  623. std::ofstream of;
  624. of.open(outpath.c_str());
  625. if (!of)
  626. {
  627. fprintf(stderr, "Error: could not open output file %s\n", outpath.c_str());
  628. return;
  629. }
  630. int vertices_count=vertices.rows();
  631. of << vertices_count << endl;
  632. for (int i=0; i<vertices_count; ++i)
  633. {
  634. of << curv[i][0] << " " << curv[i][1] << " " << curvDir[i][0][0] << " " << curvDir[i][0][1] << " " << curvDir[i][0][2] << " " <<
  635. curvDir[i][1][0] << " " << curvDir[i][1][1] << " " << curvDir[i][1][2] << endl;
  636. }
  637. of.close();
  638. }
  639. template <
  640. typename DerivedV,
  641. typename DerivedF,
  642. typename DerivedPD1,
  643. typename DerivedPD2,
  644. typename DerivedPV1,
  645. typename DerivedPV2>
  646. IGL_INLINE void igl::principal_curvature(
  647. const Eigen::PlainObjectBase<DerivedV>& V,
  648. const Eigen::PlainObjectBase<DerivedF>& F,
  649. Eigen::PlainObjectBase<DerivedPD1>& PD1,
  650. Eigen::PlainObjectBase<DerivedPD2>& PD2,
  651. Eigen::PlainObjectBase<DerivedPV1>& PV1,
  652. Eigen::PlainObjectBase<DerivedPV2>& PV2,
  653. unsigned radius,
  654. bool useKring)
  655. {
  656. if (radius < 2)
  657. {
  658. radius = 2;
  659. std::cout << "WARNING: igl::principal_curvature needs a radius >= 2, fixing it to 2." << std::endl;
  660. }
  661. // Preallocate memory
  662. PD1.resize(V.rows(),3);
  663. PD2.resize(V.rows(),3);
  664. // Preallocate memory
  665. PV1.resize(V.rows(),1);
  666. PV2.resize(V.rows(),1);
  667. // Precomputation
  668. CurvatureCalculator cc;
  669. cc.init(V.template cast<double>(),F.template cast<int>());
  670. cc.sphereRadius = radius;
  671. if (useKring)
  672. {
  673. cc.kRing = radius;
  674. cc.st = K_RING_SEARCH;
  675. }
  676. // Compute
  677. cc.computeCurvature();
  678. // Copy it back
  679. for (unsigned i=0; i<V.rows(); ++i)
  680. {
  681. PD1.row(i) << cc.curvDir[i][0][0], cc.curvDir[i][0][1], cc.curvDir[i][0][2];
  682. PD2.row(i) << cc.curvDir[i][1][0], cc.curvDir[i][1][1], cc.curvDir[i][1][2];
  683. PD1.row(i).normalize();
  684. PD2.row(i).normalize();
  685. 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)))
  686. {
  687. PD1.row(i) << 0,0,0;
  688. PD2.row(i) << 0,0,0;
  689. }
  690. PV1(i) = cc.curv[i][0];
  691. PV2(i) = cc.curv[i][1];
  692. if (PD1.row(i) * PD2.row(i).transpose() > 10e-6)
  693. {
  694. std::cerr << "PRINCIPAL_CURVATURE: Something is wrong with vertex: i" << std::endl;
  695. PD1.row(i) *= 0;
  696. PD2.row(i) *= 0;
  697. }
  698. }
  699. }
  700. #ifdef IGL_STATIC_LIBRARY
  701. // Explicit template instantiation
  702. // generated by autoexplicit.sh
  703. 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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<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);
  704. 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::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<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);
  705. 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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<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);
  706. #endif