principal_curvature.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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(std::vector<Eigen::Vector3d> &VV, bool zeroDetCheck, bool svd)
  92. {
  93. using namespace std;
  94. assert(VV.size() >= 5);
  95. if (VV.size() < 5)
  96. {
  97. cerr << "ASSERT FAILED!" << endl;
  98. exit(0);
  99. }
  100. Eigen::MatrixXd A(VV.size(),5);
  101. Eigen::MatrixXd b(VV.size(),1);
  102. Eigen::MatrixXd sol(5,1);
  103. for(unsigned int c=0; c < VV.size(); ++c)
  104. {
  105. double u = VV[c][0];
  106. double v = VV[c][1];
  107. double n = VV[c][2];
  108. A(c,0) = u*u;
  109. A(c,1) = u*v;
  110. A(c,2) = v*v;
  111. A(c,3) = u;
  112. A(c,4) = v;
  113. b(c) = n;
  114. }
  115. sol=A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
  116. return Quadric(sol(0),sol(1),sol(2),sol(3),sol(4));
  117. }
  118. };
  119. public:
  120. Eigen::MatrixXd vertices;
  121. // Face list of current mesh (#F x 3) or (#F x 4)
  122. // The i-th row contains the indices of the vertices that forms the i-th face in ccw order
  123. Eigen::MatrixXi faces;
  124. std::vector<std::vector<int> > vertex_to_vertices;
  125. std::vector<std::vector<int> > vertex_to_faces;
  126. std::vector<std::vector<int> > vertex_to_faces_index;
  127. Eigen::MatrixXd face_normals;
  128. Eigen::MatrixXd vertex_normals;
  129. /* Size of the neighborhood */
  130. double sphereRadius;
  131. int kRing;
  132. bool localMode; /* Use local mode */
  133. bool projectionPlaneCheck; /* Check collected vertices on tangent plane */
  134. bool montecarlo;
  135. bool svd; /* Use svd calculation instead of pseudoinverse */
  136. bool zeroDetCheck; /* Check if the determinant is close to zero */
  137. unsigned int montecarloN;
  138. searchType st; /* Use either a sphere search or a k-ring search */
  139. normalType nt;
  140. double lastRadius;
  141. double scaledRadius;
  142. std::string lastMeshName;
  143. /* Benchmark related variables */
  144. bool expStep; /* True if we want the radius to increase exponentially */
  145. int step; /* If expStep==false, by how much rhe radius increases on every step */
  146. int maxSize; /* The maximum limit of the radius in the benchmark */
  147. IGL_INLINE CurvatureCalculator();
  148. IGL_INLINE void init(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  149. IGL_INLINE void finalEigenStuff (int, std::vector<Eigen::Vector3d>, Quadric );
  150. IGL_INLINE void fitQuadric (Eigen::Vector3d, std::vector<Eigen::Vector3d> ref, const std::vector<int>& , Quadric *);
  151. IGL_INLINE void applyProjOnPlane(Eigen::Vector3d, std::vector<int>, std::vector<int>&);
  152. IGL_INLINE void getSphere(const int, const double, std::vector<int>&, int min);
  153. IGL_INLINE void getKRing(const int, const double,std::vector<int>&);
  154. IGL_INLINE Eigen::Vector3d project(Eigen::Vector3d, Eigen::Vector3d, Eigen::Vector3d);
  155. IGL_INLINE void computeReferenceFrame(int, Eigen::Vector3d, std::vector<Eigen::Vector3d>&);
  156. IGL_INLINE void getAverageNormal(int, std::vector<int>, Eigen::Vector3d&);
  157. IGL_INLINE void getProjPlane(int, std::vector<int>, Eigen::Vector3d&);
  158. IGL_INLINE void applyMontecarlo(std::vector<int>&,std::vector<int>*);
  159. IGL_INLINE void computeCurvature();
  160. IGL_INLINE void printCurvature(std::string outpath);
  161. IGL_INLINE double getAverageEdge();
  162. IGL_INLINE static int rotateForward (float *v0, float *v1, float *v2)
  163. {
  164. float t;
  165. if (std::abs(*v2) >= std::abs(*v1) && std::abs(*v2) >= std::abs(*v0))
  166. return 0;
  167. t = *v0;
  168. *v0 = *v2;
  169. *v2 = *v1;
  170. *v1 = t;
  171. return 1 + rotateForward (v0, v1, v2);
  172. }
  173. IGL_INLINE static void rotateBackward (int nr, float *v0, float *v1, float *v2)
  174. {
  175. float t;
  176. if (nr == 0)
  177. return;
  178. t = *v2;
  179. *v2 = *v0;
  180. *v0 = *v1;
  181. *v1 = t;
  182. rotateBackward (nr - 1, v0, v1, v2);
  183. }
  184. IGL_INLINE static Eigen::Vector3d chooseMax (Eigen::Vector3d n, Eigen::Vector3d abc, float ab)
  185. {
  186. int i, max_i;
  187. float max_sp;
  188. Eigen::Vector3d nt[8];
  189. n.normalize ();
  190. abc.normalize ();
  191. max_sp = - std::numeric_limits<float>::max();
  192. for (i = 0; i < 4; i++)
  193. {
  194. nt[i] = n;
  195. if (ab > 0)
  196. {
  197. switch (i)
  198. {
  199. case 0:
  200. break;
  201. case 1:
  202. nt[i][2] = -n[2];
  203. break;
  204. case 2:
  205. nt[i][0] = -n[0];
  206. nt[i][1] = -n[1];
  207. break;
  208. case 3:
  209. nt[i][0] = -n[0];
  210. nt[i][1] = -n[1];
  211. nt[i][2] = -n[2];
  212. break;
  213. }
  214. }
  215. else
  216. {
  217. switch (i)
  218. {
  219. case 0:
  220. nt[i][0] = -n[0];
  221. break;
  222. case 1:
  223. nt[i][1] = -n[1];
  224. break;
  225. case 2:
  226. nt[i][0] = -n[0];
  227. nt[i][2] = -n[2];
  228. break;
  229. case 3:
  230. nt[i][1] = -n[1];
  231. nt[i][2] = -n[2];
  232. break;
  233. }
  234. }
  235. if (nt[i].dot(abc) > max_sp)
  236. {
  237. max_sp = nt[i].dot(abc);
  238. max_i = i;
  239. }
  240. }
  241. return nt[max_i];
  242. }
  243. };
  244. class comparer
  245. {
  246. public:
  247. IGL_INLINE bool operator() (const std::pair<int, double>& lhs, const std::pair<int, double>&rhs) const
  248. {
  249. return lhs.second>rhs.second;
  250. }
  251. };
  252. IGL_INLINE CurvatureCalculator::CurvatureCalculator()
  253. {
  254. this->localMode=true;
  255. this->projectionPlaneCheck=true;
  256. this->sphereRadius=5;
  257. this->st=SPHERE_SEARCH;
  258. this->nt=AVERAGE;
  259. this->montecarlo=false;
  260. this->montecarloN=0;
  261. this->kRing=3;
  262. this->svd=true;
  263. this->zeroDetCheck=true;
  264. this->curvatureComputed=false;
  265. this->expStep=true;
  266. }
  267. IGL_INLINE void CurvatureCalculator::init(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F)
  268. {
  269. // Normalize vertices
  270. vertices = V;
  271. // vertices = vertices.array() - vertices.minCoeff();
  272. // vertices = vertices.array() / vertices.maxCoeff();
  273. // vertices = vertices.array() * (1.0/igl::avg_edge_length(V,F));
  274. faces = F;
  275. igl::adjacency_list(F, vertex_to_vertices);
  276. igl::vertex_triangle_adjacency(V, F, vertex_to_faces, vertex_to_faces_index);
  277. igl::per_face_normals(V, F, face_normals);
  278. igl::per_vertex_normals(V, F, face_normals, vertex_normals);
  279. }
  280. IGL_INLINE void CurvatureCalculator::fitQuadric (Eigen::Vector3d v, std::vector<Eigen::Vector3d> ref, const std::vector<int>& vv, Quadric *q)
  281. {
  282. std::vector<Eigen::Vector3d> points;
  283. points.reserve (vv.size());
  284. for (unsigned int i = 0; i < vv.size(); ++i) {
  285. Eigen::Vector3d cp = vertices.row(vv[i]);
  286. // vtang non e` il v tangente!!!
  287. Eigen::Vector3d vTang = cp - v;
  288. double x = vTang.dot(ref[0]);
  289. double y = vTang.dot(ref[1]);
  290. double z = vTang.dot(ref[2]);
  291. points.push_back(Eigen::Vector3d (x,y,z));
  292. }
  293. *q = Quadric::fit (points, zeroDetCheck, svd);
  294. }
  295. IGL_INLINE void CurvatureCalculator::finalEigenStuff (int i, std::vector<Eigen::Vector3d> ref, Quadric q)
  296. {
  297. double a = q.a();
  298. double b = q.b();
  299. double c = q.c();
  300. double d = q.d();
  301. double e = q.e();
  302. // if (fabs(a) < 10e-8 || fabs(b) < 10e-8)
  303. // {
  304. // std::cout << "Degenerate quadric: " << i << std::endl;
  305. // }
  306. double E = 1.0 + d*d;
  307. double F = d*e;
  308. double G = 1.0 + e*e;
  309. Eigen::Vector3d n = Eigen::Vector3d(-d,-e,1.0).normalized();
  310. double L = 2.0 * a * n[2];
  311. double M = b * n[2];
  312. double N = 2 * c * n[2];
  313. // ----------------- Eigen stuff
  314. Eigen::Matrix2d m;
  315. m << L*G - M*F, M*E-L*F, M*E-L*F, N*E-M*F;
  316. m = m / (E*G-F*F);
  317. Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> eig(m);
  318. Eigen::Vector2d c_val = eig.eigenvalues();
  319. Eigen::Matrix2d c_vec = eig.eigenvectors();
  320. // std::cerr << "c_val:" << c_val << std::endl;
  321. // std::cerr << "c_vec:" << c_vec << std::endl;
  322. // std::cerr << "c_vec:" << c_vec(0) << " " << c_vec(1) << std::endl;
  323. c_val = -c_val;
  324. Eigen::Vector3d v1, v2;
  325. v1[0] = c_vec(0);
  326. v1[1] = c_vec(1);
  327. v1[2] = 0; //d * v1[0] + e * v1[1];
  328. v2[0] = c_vec(2);
  329. v2[1] = c_vec(3);
  330. v2[2] = 0; //d * v2[0] + e * v2[1];
  331. // v1 = v1.normalized();
  332. // v2 = v2.normalized();
  333. Eigen::Vector3d v1global = ref[0] * v1[0] + ref[1] * v1[1] + ref[2] * v1[2];
  334. Eigen::Vector3d v2global = ref[0] * v2[0] + ref[1] * v2[1] + ref[2] * v2[2];
  335. v1global.normalize();
  336. v2global.normalize();
  337. v1global *= c_val(0);
  338. v2global *= c_val(1);
  339. if (c_val[0] > c_val[1])
  340. {
  341. curv[i]=std::vector<double>(2);
  342. curv[i][0]=c_val(1);
  343. curv[i][1]=c_val(0);
  344. curvDir[i]=std::vector<Eigen::Vector3d>(2);
  345. curvDir[i][0]=v2global;
  346. curvDir[i][1]=v1global;
  347. }
  348. else
  349. {
  350. curv[i]=std::vector<double>(2);
  351. curv[i][0]=c_val(0);
  352. curv[i][1]=c_val(1);
  353. curvDir[i]=std::vector<Eigen::Vector3d>(2);
  354. curvDir[i][0]=v1global;
  355. curvDir[i][1]=v2global;
  356. }
  357. // ---- end Eigen stuff
  358. }
  359. IGL_INLINE void CurvatureCalculator::getKRing(const int start, const double r, std::vector<int>&vv)
  360. {
  361. int bufsize=vertices.rows();
  362. vv.reserve(bufsize);
  363. std::list<std::pair<int,int> > queue;
  364. bool* visited = (bool*)calloc(bufsize,sizeof(bool));
  365. queue.push_back(std::pair<int,int>(start,0));
  366. visited[start]=true;
  367. while (!queue.empty())
  368. {
  369. int toVisit=queue.front().first;
  370. int distance=queue.front().second;
  371. queue.pop_front();
  372. vv.push_back(toVisit);
  373. if (distance<(int)r)
  374. {
  375. for (unsigned int i=0; i<vertex_to_vertices[toVisit].size(); i++)
  376. {
  377. int neighbor=vertex_to_vertices[toVisit][i];
  378. if (!visited[neighbor])
  379. {
  380. queue.push_back(std::pair<int,int> (neighbor,distance+1));
  381. visited[neighbor]=true;
  382. }
  383. }
  384. }
  385. }
  386. free(visited);
  387. return;
  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= new std::list<int>();
  394. bool* visited = (bool*)calloc(bufsize,sizeof(bool));
  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= new std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, comparer >();
  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. float 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. float distance=(me-neigh).norm();
  431. extra_candidates->push(std::pair<int,double>(neighbor,distance));
  432. visited[neighbor]=true;
  433. }
  434. }
  435. }
  436. free(extra_candidates);
  437. free(queue);
  438. free(visited);
  439. }
  440. IGL_INLINE Eigen::Vector3d CurvatureCalculator::project(Eigen::Vector3d v, Eigen::Vector3d vp, Eigen::Vector3d ppn)
  441. {
  442. return (vp - (ppn * ((vp - v).dot(ppn))));
  443. }
  444. IGL_INLINE void CurvatureCalculator::computeReferenceFrame(int i, Eigen::Vector3d normal, std::vector<Eigen::Vector3d>& ref )
  445. {
  446. Eigen::Vector3d longest_v=Eigen::Vector3d::Zero();
  447. longest_v=Eigen::Vector3d(vertices.row(vertex_to_vertices[i][0]));
  448. longest_v=(project(vertices.row(i),longest_v,normal)-Eigen::Vector3d(vertices.row(i))).normalized();
  449. /* L'ultimo asse si ottiene come prodotto vettoriale tra i due
  450. * calcolati */
  451. Eigen::Vector3d y_axis=(normal.cross(longest_v)).normalized();
  452. ref[0]=longest_v;
  453. ref[1]=y_axis;
  454. ref[2]=normal;
  455. }
  456. IGL_INLINE void CurvatureCalculator::getAverageNormal(int j, std::vector<int> vv, Eigen::Vector3d& normal)
  457. {
  458. normal=(vertex_normals.row(j)).normalized();
  459. if (localMode)
  460. return;
  461. for (unsigned int i=0; i<vv.size(); i++)
  462. {
  463. normal+=vertex_normals.row(vv[i]).normalized();
  464. }
  465. normal.normalize();
  466. }
  467. IGL_INLINE void CurvatureCalculator::getProjPlane(int j, std::vector<int> vv, Eigen::Vector3d& ppn)
  468. {
  469. int nr;
  470. float a, b, c;
  471. float nx, ny, nz;
  472. float abcq;
  473. a = b = c = 0;
  474. if (localMode)
  475. {
  476. for (unsigned int i=0; i<vertex_to_faces.at(j).size(); ++i)
  477. {
  478. Eigen::Vector3d faceNormal=face_normals.row(vertex_to_faces.at(j).at(i));
  479. a += faceNormal[0];
  480. b += faceNormal[1];
  481. c += faceNormal[2];
  482. }
  483. }
  484. else
  485. {
  486. for (unsigned int i=0; i<vv.size(); ++i)
  487. {
  488. a+= vertex_normals.row(vv[i])[0];
  489. b+= vertex_normals.row(vv[i])[1];
  490. c+= vertex_normals.row(vv[i])[2];
  491. }
  492. }
  493. nr = rotateForward (&a, &b, &c);
  494. abcq = a*a + b*b + c*c;
  495. nx = sqrt (a*a / abcq);
  496. ny = sqrt (b*b / abcq);
  497. nz = sqrt (1 - nx*nx - ny*ny);
  498. rotateBackward (nr, &a, &b, &c);
  499. rotateBackward (nr, &nx, &ny, &nz);
  500. ppn = chooseMax (Eigen::Vector3d(nx, ny, nz), Eigen::Vector3d (a, b, c), a * b);
  501. ppn.normalize();
  502. }
  503. IGL_INLINE double CurvatureCalculator::getAverageEdge()
  504. {
  505. double sum = 0;
  506. int count = 0;
  507. for (int i = 0; i<faces.rows(); i++)
  508. {
  509. for (short unsigned j=0; j<3; j++)
  510. {
  511. Eigen::Vector3d p1=vertices.row(faces.row(i)[j]);
  512. Eigen::Vector3d p2=vertices.row(faces.row(i)[(j+1)%3]);
  513. double l = (p1-p2).norm();
  514. sum+=l;
  515. ++count;
  516. }
  517. }
  518. return (sum/(double)count);
  519. }
  520. IGL_INLINE void CurvatureCalculator::applyProjOnPlane(Eigen::Vector3d ppn, std::vector<int> vin, std::vector<int> &vout)
  521. {
  522. for (std::vector<int>::iterator vpi = vin.begin(); vpi != vin.end(); ++vpi)
  523. if (vertex_normals.row(*vpi) * ppn > 0.0f)
  524. vout.push_back (*vpi);
  525. }
  526. IGL_INLINE void CurvatureCalculator::applyMontecarlo(std::vector<int>& vin, std::vector<int> *vout)
  527. {
  528. if (montecarloN >= vin.size ())
  529. {
  530. *vout = vin;
  531. return;
  532. }
  533. float p = ((float) montecarloN) / (float) vin.size();
  534. for (std::vector<int>::iterator vpi = vin.begin(); vpi != vin.end(); ++vpi)
  535. {
  536. float r;
  537. if ((r = ((float)rand () / RAND_MAX)) < p)
  538. {
  539. vout->push_back (*vpi);
  540. }
  541. }
  542. }
  543. IGL_INLINE void CurvatureCalculator::computeCurvature()
  544. {
  545. using namespace std;
  546. //CHECK che esista la mesh
  547. size_t vertices_count=vertices.rows() ;
  548. if (vertices_count <=0)
  549. return;
  550. curvDir=std::vector< std::vector<Eigen::Vector3d> >(vertices_count);
  551. curv=std::vector<std::vector<double> >(vertices_count);
  552. scaledRadius=getAverageEdge()*sphereRadius;
  553. std::vector<int> vv;
  554. std::vector<int> vvtmp;
  555. Eigen::Vector3d normal;
  556. //double time_spent;
  557. //double searchtime=0, ref_time=0, fit_time=0, final_time=0;
  558. for (size_t i=0; i<vertices_count; ++i)
  559. {
  560. vv.clear();
  561. vvtmp.clear();
  562. Eigen::Vector3d me=vertices.row(i);
  563. switch (st)
  564. {
  565. case SPHERE_SEARCH:
  566. getSphere(i,scaledRadius,vv,6);
  567. break;
  568. case K_RING_SEARCH:
  569. getKRing(i,kRing,vv);
  570. break;
  571. default:
  572. fprintf(stderr,"Error: search type not recognized");
  573. return;
  574. }
  575. std::vector<Eigen::Vector3d> ref(3);
  576. if (vv.size()<6)
  577. {
  578. std::cerr << "Could not compute curvature of radius " << scaledRadius << endl;
  579. return;
  580. }
  581. if (projectionPlaneCheck)
  582. {
  583. vvtmp.reserve (vv.size ());
  584. applyProjOnPlane (vertex_normals.row(i), vv, vvtmp);
  585. if (vvtmp.size() >= 6 && vvtmp.size()<vv.size())
  586. vv = vvtmp;
  587. }
  588. switch (nt)
  589. {
  590. case AVERAGE:
  591. getAverageNormal(i,vv,normal);
  592. break;
  593. case PROJ_PLANE:
  594. getProjPlane(i,vv,normal);
  595. break;
  596. default:
  597. fprintf(stderr,"Error: normal type not recognized");
  598. return;
  599. }
  600. if (vv.size()<6)
  601. {
  602. std::cerr << "Could not compute curvature of radius " << scaledRadius << endl;
  603. return;
  604. }
  605. if (montecarlo)
  606. {
  607. if(montecarloN<6)
  608. break;
  609. vvtmp.reserve(vv.size());
  610. applyMontecarlo(vv,&vvtmp);
  611. vv=vvtmp;
  612. }
  613. if (vv.size()<6)
  614. return;
  615. computeReferenceFrame(i,normal,ref);
  616. Quadric q;
  617. fitQuadric (me, ref, vv, &q);
  618. finalEigenStuff(i,ref,q);
  619. }
  620. lastRadius=sphereRadius;
  621. curvatureComputed=true;
  622. }
  623. IGL_INLINE void CurvatureCalculator::printCurvature(std::string outpath)
  624. {
  625. using namespace std;
  626. if (!curvatureComputed)
  627. return;
  628. std::ofstream of;
  629. of.open(outpath.c_str());
  630. if (!of)
  631. {
  632. fprintf(stderr, "Error: could not open output file %s\n", outpath.c_str());
  633. return;
  634. }
  635. int vertices_count=vertices.rows();
  636. of << vertices_count << endl;
  637. for (int i=0; i<vertices_count; i++)
  638. {
  639. of << curv[i][0] << " " << curv[i][1] << " " << curvDir[i][0][0] << " " << curvDir[i][0][1] << " " << curvDir[i][0][2] << " " <<
  640. curvDir[i][1][0] << " " << curvDir[i][1][1] << " " << curvDir[i][1][2] << endl;
  641. }
  642. of.close();
  643. }
  644. template <
  645. typename DerivedV,
  646. typename DerivedF,
  647. typename DerivedPD1,
  648. typename DerivedPD2,
  649. typename DerivedPV1,
  650. typename DerivedPV2>
  651. IGL_INLINE void igl::principal_curvature(
  652. const Eigen::PlainObjectBase<DerivedV>& V,
  653. const Eigen::PlainObjectBase<DerivedF>& F,
  654. Eigen::PlainObjectBase<DerivedPD1>& PD1,
  655. Eigen::PlainObjectBase<DerivedPD2>& PD2,
  656. Eigen::PlainObjectBase<DerivedPV1>& PV1,
  657. Eigen::PlainObjectBase<DerivedPV2>& PV2,
  658. unsigned radius,
  659. bool useKring)
  660. {
  661. using namespace std;
  662. if (radius < 2)
  663. {
  664. radius = 2;
  665. cout << "WARNING: igl::principal_curvature needs a radius >= 2, fixing it to 2." << endl;
  666. }
  667. // Preallocate memory
  668. PD1.resize(V.rows(),3);
  669. PD2.resize(V.rows(),3);
  670. // Preallocate memory
  671. PV1.resize(V.rows(),1);
  672. PV2.resize(V.rows(),1);
  673. // Precomputation
  674. CurvatureCalculator cc;
  675. cc.init(V.template cast<double>(),F.template cast<int>());
  676. cc.sphereRadius = radius;
  677. if (useKring)
  678. {
  679. cc.kRing = radius;
  680. cc.st = K_RING_SEARCH;
  681. }
  682. // Compute
  683. cc.computeCurvature();
  684. // Copy it back
  685. for (unsigned i=0; i<V.rows(); i++)
  686. {
  687. Eigen::Vector3d d1;
  688. Eigen::Vector3d d2;
  689. PD1.row(i) << cc.curvDir[i][0][0], cc.curvDir[i][0][1], cc.curvDir[i][0][2];
  690. PD2.row(i) << cc.curvDir[i][1][0], cc.curvDir[i][1][1], cc.curvDir[i][1][2];
  691. PD1.row(i).normalize();
  692. PD2.row(i).normalize();
  693. 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)))
  694. {
  695. PD1.row(i) << 0,0,0;
  696. PD2.row(i) << 0,0,0;
  697. }
  698. PV1(i) = cc.curv[i][0];
  699. PV2(i) = cc.curv[i][1];
  700. if (PD1.row(i) * PD2.row(i).transpose() > 10e-6)
  701. {
  702. cerr << "PRINCIPAL_CURVATURE: Something is wrong with vertex: i" << endl;
  703. PD1.row(i) *= 0;
  704. PD2.row(i) *= 0;
  705. }
  706. }
  707. }
  708. #ifdef IGL_STATIC_LIBRARY
  709. // Explicit template specialization
  710. // generated by autoexplicit.sh
  711. 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);
  712. 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);
  713. 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);
  714. #endif