principal_curvature.cpp 26 KB

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