principal_curvature.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. #include "principal_curvature.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <iostream>
  6. #include <queue>
  7. #include <list>
  8. #include <cmath>
  9. #include <limits>
  10. #include <Eigen/SparseCholesky>
  11. // Lib IGL includes
  12. #include <igl/adjacency_list.h>
  13. #include <igl/per_face_normals.h>
  14. #include <igl/per_vertex_normals.h>
  15. #include <igl/avg_edge_length.h>
  16. #include <igl/vf.h>
  17. typedef enum
  18. {
  19. SPHERE_SEARCH,
  20. K_RING_SEARCH
  21. } searchType;
  22. typedef enum
  23. {
  24. AVERAGE,
  25. PROJ_PLANE
  26. } normalType;
  27. class CurvatureCalculator
  28. {
  29. public:
  30. /* Row number i represents the i-th vertex, whose columns are:
  31. curv[i][0] : K2
  32. curv[i][1] : K1
  33. curvDir[i][0] : PD1
  34. curvDir[i][1] : PD2
  35. */
  36. std::vector< std::vector<double> > curv;
  37. std::vector< std::vector<Eigen::Vector3d> > curvDir;
  38. bool curvatureComputed;
  39. class Quadric
  40. {
  41. public:
  42. IGL_INLINE Quadric ()
  43. {
  44. a() = b() = c() = d() = e() = 1.0;
  45. }
  46. IGL_INLINE Quadric(double av, double bv, double cv, double dv, double ev)
  47. {
  48. a() = av;
  49. b() = bv;
  50. c() = cv;
  51. d() = dv;
  52. e() = ev;
  53. }
  54. IGL_INLINE double& a() { return data[0];}
  55. IGL_INLINE double& b() { return data[1];}
  56. IGL_INLINE double& c() { return data[2];}
  57. IGL_INLINE double& d() { return data[3];}
  58. IGL_INLINE double& e() { return data[4];}
  59. double data[5];
  60. IGL_INLINE double evaluate(double u, double v)
  61. {
  62. return a()*u*u + b()*u*v + c()*v*v + d()*u + e()*v;
  63. }
  64. IGL_INLINE double du(double u, double v)
  65. {
  66. return 2.0*a()*u + b()*v + d();
  67. }
  68. IGL_INLINE double dv(double u, double v)
  69. {
  70. return 2.0*c()*v + b()*u + e();
  71. }
  72. IGL_INLINE double duv(double u, double v)
  73. {
  74. return b();
  75. }
  76. IGL_INLINE double duu(double u, double v)
  77. {
  78. return 2.0*a();
  79. }
  80. IGL_INLINE double dvv(double u, double v)
  81. {
  82. return 2.0*c();
  83. }
  84. IGL_INLINE static Quadric fit(std::vector<Eigen::Vector3d> &VV, bool zeroDetCheck, bool svd)
  85. {
  86. using namespace std;
  87. assert(VV.size() >= 5);
  88. if (VV.size() < 5)
  89. {
  90. cerr << "ASSERT FAILED!" << endl;
  91. exit(0);
  92. }
  93. Eigen::MatrixXd A(VV.size(),5);
  94. Eigen::MatrixXd b(VV.size(),1);
  95. Eigen::MatrixXd sol(5,1);
  96. for(unsigned int c=0; c < VV.size(); ++c)
  97. {
  98. double u = VV[c][0];
  99. double v = VV[c][1];
  100. double n = VV[c][2];
  101. A(c,0) = u*u;
  102. A(c,1) = u*v;
  103. A(c,2) = v*v;
  104. A(c,3) = u;
  105. A(c,4) = v;
  106. b(c) = n;
  107. }
  108. sol=A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
  109. return Quadric(sol(0),sol(1),sol(2),sol(3),sol(4));
  110. }
  111. };
  112. public:
  113. Eigen::MatrixXd vertices;
  114. // Face list of current mesh (#F x 3) or (#F x 4)
  115. // The i-th row contains the indices of the vertices that forms the i-th face in ccw order
  116. Eigen::MatrixXi faces;
  117. std::vector<std::vector<int> > vertex_to_vertices;
  118. std::vector<std::vector<int> > vertex_to_faces;
  119. std::vector<std::vector<int> > vertex_to_faces_index;
  120. Eigen::MatrixXd face_normals;
  121. Eigen::MatrixXd vertex_normals;
  122. /* Size of the neighborhood */
  123. double sphereRadius;
  124. int kRing;
  125. bool localMode; /* Use local mode */
  126. bool projectionPlaneCheck; /* Check collected vertices on tangent plane */
  127. bool montecarlo;
  128. bool svd; /* Use svd calculation instead of pseudoinverse */
  129. bool zeroDetCheck; /* Check if the determinant is close to zero */
  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, std::vector<Eigen::Vector3d>, Quadric );
  143. IGL_INLINE void fitQuadric (Eigen::Vector3d, std::vector<Eigen::Vector3d> ref, const std::vector<int>& , Quadric *);
  144. IGL_INLINE void applyProjOnPlane(Eigen::Vector3d, 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(Eigen::Vector3d, Eigen::Vector3d, Eigen::Vector3d);
  148. IGL_INLINE void computeReferenceFrame(int, Eigen::Vector3d, std::vector<Eigen::Vector3d>&);
  149. IGL_INLINE void getAverageNormal(int, std::vector<int>, Eigen::Vector3d&);
  150. IGL_INLINE void getProjPlane(int, std::vector<int>, Eigen::Vector3d&);
  151. IGL_INLINE void applyMontecarlo(std::vector<int>&,std::vector<int>*);
  152. IGL_INLINE void computeCurvature();
  153. IGL_INLINE void printCurvature(std::string outpath);
  154. IGL_INLINE double getAverageEdge();
  155. IGL_INLINE static int rotateForward (float *v0, float *v1, float *v2)
  156. {
  157. float t;
  158. if (abs(*v2) >= abs(*v1) && abs(*v2) >= 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, float *v0, float *v1, float *v2)
  167. {
  168. float 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, float ab)
  178. {
  179. int i, max_i;
  180. float max_sp;
  181. Eigen::Vector3d nt[8];
  182. n.normalize ();
  183. abc.normalize ();
  184. max_sp = - std::numeric_limits<float>::max();
  185. for (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=false;
  248. this->projectionPlaneCheck=false;
  249. this->sphereRadius=5;
  250. this->st=SPHERE_SEARCH;
  251. this->nt=PROJ_PLANE;
  252. this->montecarlo=false;
  253. this->montecarloN=0;
  254. this->kRing=3;
  255. this->svd=true;
  256. this->zeroDetCheck=true;
  257. this->curvatureComputed=false;
  258. this->expStep=true;
  259. }
  260. IGL_INLINE void CurvatureCalculator::init(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F)
  261. {
  262. // Normalize vertices
  263. vertices = V;
  264. // vertices = vertices.array() - vertices.minCoeff();
  265. // vertices = vertices.array() / vertices.maxCoeff();
  266. // vertices = vertices.array() * (1.0/igl::avg_edge_length(V,F));
  267. faces = F;
  268. igl::adjacency_list(F, vertex_to_vertices);
  269. igl::vf(V, F, vertex_to_faces, vertex_to_faces_index);
  270. igl::per_face_normals(V, F, face_normals);
  271. igl::per_vertex_normals(V, F, face_normals, vertex_normals);
  272. }
  273. IGL_INLINE void CurvatureCalculator::fitQuadric (Eigen::Vector3d v, std::vector<Eigen::Vector3d> ref, const std::vector<int>& vv, Quadric *q)
  274. {
  275. std::vector<Eigen::Vector3d> points;
  276. points.reserve (vv.size());
  277. for (unsigned int i = 0; i < vv.size(); ++i) {
  278. Eigen::Vector3d cp = vertices.row(vv[i]);
  279. // vtang non e` il v tangente!!!
  280. Eigen::Vector3d vTang = cp - v;
  281. double x = vTang.dot(ref[0]);
  282. double y = vTang.dot(ref[1]);
  283. double z = vTang.dot(ref[2]);
  284. points.push_back(Eigen::Vector3d (x,y,z));
  285. }
  286. *q = Quadric::fit (points, zeroDetCheck, svd);
  287. }
  288. IGL_INLINE void CurvatureCalculator::finalEigenStuff (int i, std::vector<Eigen::Vector3d> ref, Quadric q)
  289. {
  290. double a = q.a();
  291. double b = q.b();
  292. double c = q.c();
  293. double d = q.d();
  294. double e = q.e();
  295. if (fabs(a) < 10e-8 || fabs(b) < 10e-8)
  296. {
  297. std::cout << "Degenerate quadric: " << i << std::endl;
  298. }
  299. double E = 1.0 + d*d;
  300. double F = d*e;
  301. double G = 1.0 + e*e;
  302. Eigen::Vector3d n = Eigen::Vector3d(-d,-e,1.0).normalized();
  303. double L = 2.0 * a * n[2];
  304. double M = b * n[2];
  305. double N = 2 * c * n[2];
  306. // ----------------- Eigen stuff
  307. Eigen::Matrix2d m;
  308. m << L*G - M*F, M*E-L*F, M*E-L*F, N*E-M*F;
  309. m = m / (E*G-F*F);
  310. Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> eig(m);
  311. Eigen::Vector2d c_val = eig.eigenvalues();
  312. Eigen::Matrix2d c_vec = eig.eigenvectors();
  313. // std::cerr << "c_val:" << c_val << std::endl;
  314. // std::cerr << "c_vec:" << c_vec << std::endl;
  315. // std::cerr << "c_vec:" << c_vec(0) << " " << c_vec(1) << std::endl;
  316. c_val = -c_val;
  317. Eigen::Vector3d v1, v2;
  318. v1[0] = c_vec(0);
  319. v1[1] = c_vec(1);
  320. v1[2] = 0; //d * v1[0] + e * v1[1];
  321. v2[0] = c_vec(2);
  322. v2[1] = c_vec(3);
  323. v2[2] = 0; //d * v2[0] + e * v2[1];
  324. // v1 = v1.normalized();
  325. // v2 = v2.normalized();
  326. Eigen::Vector3d v1global = ref[0] * v1[0] + ref[1] * v1[1] + ref[2] * v1[2];
  327. Eigen::Vector3d v2global = ref[0] * v2[0] + ref[1] * v2[1] + ref[2] * v2[2];
  328. v1global.normalize();
  329. v2global.normalize();
  330. v1global *= c_val(0);
  331. v2global *= c_val(1);
  332. if (c_val[0] > c_val[1])
  333. {
  334. curv[i]=std::vector<double>(2);
  335. curv[i][0]=c_val(1);
  336. curv[i][1]=c_val(0);
  337. curvDir[i]=std::vector<Eigen::Vector3d>(2);
  338. curvDir[i][0]=v2global;
  339. curvDir[i][1]=v1global;
  340. }
  341. else
  342. {
  343. curv[i]=std::vector<double>(2);
  344. curv[i][0]=c_val(0);
  345. curv[i][1]=c_val(1);
  346. curvDir[i]=std::vector<Eigen::Vector3d>(2);
  347. curvDir[i][0]=v1global;
  348. curvDir[i][1]=v2global;
  349. }
  350. // ---- end Eigen stuff
  351. }
  352. IGL_INLINE void CurvatureCalculator::getKRing(const int start, const double r, std::vector<int>&vv)
  353. {
  354. int bufsize=vertices.rows();
  355. vv.reserve(bufsize);
  356. std::list<std::pair<int,int> > queue;
  357. bool* visited = (bool*)calloc(bufsize,sizeof(bool));
  358. queue.push_back(std::pair<int,int>(start,0));
  359. visited[start]=true;
  360. while (!queue.empty())
  361. {
  362. int toVisit=queue.front().first;
  363. int distance=queue.front().second;
  364. queue.pop_front();
  365. vv.push_back(toVisit);
  366. if (distance<(int)r)
  367. {
  368. for (unsigned int i=0; i<vertex_to_vertices[toVisit].size(); i++)
  369. {
  370. int neighbor=vertex_to_vertices[toVisit][i];
  371. if (!visited[neighbor])
  372. {
  373. queue.push_back(std::pair<int,int> (neighbor,distance+1));
  374. visited[neighbor]=true;
  375. }
  376. }
  377. }
  378. }
  379. free(visited);
  380. return;
  381. }
  382. IGL_INLINE void CurvatureCalculator::getSphere(const int start, const double r, std::vector<int> &vv, int min)
  383. {
  384. int bufsize=vertices.rows();
  385. vv.reserve(bufsize);
  386. std::list<int>* queue= new std::list<int>();
  387. bool* visited = (bool*)calloc(bufsize,sizeof(bool));
  388. queue->push_back(start);
  389. visited[start]=true;
  390. Eigen::Vector3d me=vertices.row(start);
  391. 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 >();
  392. while (!queue->empty())
  393. {
  394. int toVisit=queue->front();
  395. queue->pop_front();
  396. vv.push_back(toVisit);
  397. for (unsigned int i=0; i<vertex_to_vertices[toVisit].size(); i++)
  398. {
  399. int neighbor=vertex_to_vertices[toVisit][i];
  400. if (!visited[neighbor])
  401. {
  402. Eigen::Vector3d neigh=vertices.row(neighbor);
  403. float distance=(me-neigh).norm();
  404. if (distance<r)
  405. queue->push_back(neighbor);
  406. else if (vv.size()<min)
  407. extra_candidates->push(std::pair<int,double>(neighbor,distance));
  408. visited[neighbor]=true;
  409. }
  410. }
  411. }
  412. while (!extra_candidates->empty() && vv.size()<min)
  413. {
  414. std::pair<int, double> cand=extra_candidates->top();
  415. extra_candidates->pop();
  416. vv.push_back(cand.first);
  417. for (unsigned int i=0; i<vertex_to_vertices[cand.first].size(); i++)
  418. {
  419. int neighbor=vertex_to_vertices[cand.first][i];
  420. if (!visited[neighbor])
  421. {
  422. Eigen::Vector3d neigh=vertices.row(neighbor);
  423. float distance=(me-neigh).norm();
  424. extra_candidates->push(std::pair<int,double>(neighbor,distance));
  425. visited[neighbor]=true;
  426. }
  427. }
  428. }
  429. free(extra_candidates);
  430. free(queue);
  431. free(visited);
  432. }
  433. IGL_INLINE Eigen::Vector3d CurvatureCalculator::project(Eigen::Vector3d v, Eigen::Vector3d vp, Eigen::Vector3d ppn)
  434. {
  435. return (vp - (ppn * ((vp - v).dot(ppn))));
  436. }
  437. IGL_INLINE void CurvatureCalculator::computeReferenceFrame(int i, Eigen::Vector3d normal, std::vector<Eigen::Vector3d>& ref )
  438. {
  439. Eigen::Vector3d longest_v=Eigen::Vector3d::Zero();
  440. longest_v=Eigen::Vector3d(vertices.row(vertex_to_vertices[i][0]));
  441. longest_v=(project(vertices.row(i),longest_v,normal)-Eigen::Vector3d(vertices.row(i))).normalized();
  442. /* L'ultimo asse si ottiene come prodotto vettoriale tra i due
  443. * calcolati */
  444. Eigen::Vector3d y_axis=(normal.cross(longest_v)).normalized();
  445. ref[0]=longest_v;
  446. ref[1]=y_axis;
  447. ref[2]=normal;
  448. }
  449. IGL_INLINE void CurvatureCalculator::getAverageNormal(int j, std::vector<int> vv, Eigen::Vector3d& normal)
  450. {
  451. normal=(vertex_normals.row(j)).normalized();
  452. if (localMode)
  453. return;
  454. for (unsigned int i=0; i<vv.size(); i++)
  455. {
  456. normal+=vertex_normals.row(vv[i]).normalized();
  457. }
  458. normal.normalize();
  459. }
  460. IGL_INLINE void CurvatureCalculator::getProjPlane(int j, std::vector<int> vv, Eigen::Vector3d& ppn)
  461. {
  462. int nr;
  463. float a, b, c;
  464. float nx, ny, nz;
  465. float abcq;
  466. a = b = c = 0;
  467. if (localMode)
  468. {
  469. for (unsigned int i=0; i<vertex_to_faces.at(j).size(); ++i)
  470. {
  471. Eigen::Vector3d faceNormal=face_normals.row(vertex_to_faces.at(j).at(i));
  472. a += faceNormal[0];
  473. b += faceNormal[1];
  474. c += faceNormal[2];
  475. }
  476. }
  477. else
  478. {
  479. for (unsigned int i=0; i<vv.size(); ++i)
  480. {
  481. a+= vertex_normals.row(vv[i])[0];
  482. b+= vertex_normals.row(vv[i])[1];
  483. c+= vertex_normals.row(vv[i])[2];
  484. }
  485. }
  486. nr = rotateForward (&a, &b, &c);
  487. abcq = a*a + b*b + c*c;
  488. nx = sqrt (a*a / abcq);
  489. ny = sqrt (b*b / abcq);
  490. nz = sqrt (1 - nx*nx - ny*ny);
  491. rotateBackward (nr, &a, &b, &c);
  492. rotateBackward (nr, &nx, &ny, &nz);
  493. ppn = chooseMax (Eigen::Vector3d(nx, ny, nz), Eigen::Vector3d (a, b, c), a * b);
  494. ppn.normalize();
  495. }
  496. IGL_INLINE double CurvatureCalculator::getAverageEdge()
  497. {
  498. double sum = 0;
  499. int count = 0;
  500. for (int i = 0; i<faces.rows(); i++)
  501. {
  502. for (short unsigned j=0; j<3; j++)
  503. {
  504. Eigen::Vector3d p1=vertices.row(faces.row(i)[j]);
  505. Eigen::Vector3d p2=vertices.row(faces.row(i)[(j+1)%3]);
  506. double l = (p1-p2).norm();
  507. sum+=l;
  508. ++count;
  509. }
  510. }
  511. return (sum/(double)count);
  512. }
  513. IGL_INLINE void CurvatureCalculator::applyProjOnPlane(Eigen::Vector3d ppn, std::vector<int> vin, std::vector<int> &vout)
  514. {
  515. for (std::vector<int>::iterator vpi = vin.begin(); vpi != vin.end(); ++vpi)
  516. if (vertex_normals.row(*vpi) * ppn > 0.0f)
  517. vout.push_back (*vpi);
  518. }
  519. IGL_INLINE void CurvatureCalculator::applyMontecarlo(std::vector<int>& vin, std::vector<int> *vout)
  520. {
  521. if (montecarloN >= vin.size ())
  522. {
  523. *vout = vin;
  524. return;
  525. }
  526. float p = ((float) montecarloN) / (float) vin.size();
  527. for (std::vector<int>::iterator vpi = vin.begin(); vpi != vin.end(); ++vpi)
  528. {
  529. float r;
  530. if ((r = ((float)rand () / RAND_MAX)) < p)
  531. {
  532. vout->push_back (*vpi);
  533. }
  534. }
  535. }
  536. IGL_INLINE void CurvatureCalculator::computeCurvature()
  537. {
  538. using namespace std;
  539. //CHECK che esista la mesh
  540. size_t vertices_count=vertices.rows() ;
  541. if (vertices_count <=0)
  542. return;
  543. curvDir=std::vector< std::vector<Eigen::Vector3d> >(vertices_count);
  544. curv=std::vector<std::vector<double> >(vertices_count);
  545. scaledRadius=getAverageEdge()*sphereRadius;
  546. std::vector<int> vv;
  547. std::vector<int> vvtmp;
  548. Eigen::Vector3d normal;
  549. double time_spent;
  550. double searchtime=0, ref_time=0, fit_time=0, final_time=0;
  551. for (size_t i=0; i<vertices_count; ++i)
  552. {
  553. vv.clear();
  554. vvtmp.clear();
  555. Eigen::Vector3d me=vertices.row(i);
  556. switch (st)
  557. {
  558. case SPHERE_SEARCH:
  559. getSphere(i,scaledRadius,vv,6);
  560. break;
  561. case K_RING_SEARCH:
  562. getKRing(i,kRing,vv);
  563. break;
  564. default:
  565. fprintf(stderr,"Error: search type not recognized");
  566. return;
  567. }
  568. std::vector<Eigen::Vector3d> ref(3);
  569. if (vv.size()<6)
  570. {
  571. std::cerr << "Could not compute curvature of radius " << scaledRadius << endl;
  572. return;
  573. }
  574. switch (nt)
  575. {
  576. case AVERAGE:
  577. getAverageNormal(i,vv,normal);
  578. break;
  579. case PROJ_PLANE:
  580. getProjPlane(i,vv,normal);
  581. break;
  582. default:
  583. fprintf(stderr,"Error: normal type not recognized");
  584. return;
  585. }
  586. if (projectionPlaneCheck)
  587. {
  588. vvtmp.reserve (vv.size ());
  589. applyProjOnPlane (normal, vv, vvtmp);
  590. if (vvtmp.size() >= 6)
  591. vv = vvtmp;
  592. }
  593. if (vv.size()<6)
  594. {
  595. std::cerr << "Could not compute curvature of radius " << scaledRadius << endl;
  596. return;
  597. }
  598. if (montecarlo)
  599. {
  600. if(montecarloN<6)
  601. break;
  602. vvtmp.reserve(vv.size());
  603. applyMontecarlo(vv,&vvtmp);
  604. vv=vvtmp;
  605. }
  606. if (vv.size()<6)
  607. return;
  608. computeReferenceFrame(i,normal,ref);
  609. Quadric q;
  610. fitQuadric (me, ref, vv, &q);
  611. finalEigenStuff(i,ref,q);
  612. }
  613. lastRadius=sphereRadius;
  614. curvatureComputed=true;
  615. }
  616. IGL_INLINE void CurvatureCalculator::printCurvature(std::string outpath)
  617. {
  618. using namespace std;
  619. if (!curvatureComputed)
  620. return;
  621. std::ofstream of;
  622. of.open(outpath.c_str());
  623. if (!of)
  624. {
  625. fprintf(stderr, "Error: could not open output file %s\n", outpath.c_str());
  626. return;
  627. }
  628. int vertices_count=vertices.rows();
  629. of << vertices_count << endl;
  630. for (int i=0; i<vertices_count; i++)
  631. {
  632. of << curv[i][0] << " " << curv[i][1] << " " << curvDir[i][0][0] << " " << curvDir[i][0][1] << " " << curvDir[i][0][2] << " " <<
  633. curvDir[i][1][0] << " " << curvDir[i][1][1] << " " << curvDir[i][1][2] << endl;
  634. }
  635. of.close();
  636. }
  637. template <typename DerivedV, typename DerivedF>
  638. IGL_INLINE void igl::principal_curvature(
  639. const Eigen::PlainObjectBase<DerivedV>& V,
  640. const Eigen::PlainObjectBase<DerivedF>& F,
  641. Eigen::PlainObjectBase<DerivedV>& PD1,
  642. Eigen::PlainObjectBase<DerivedV>& PD2,
  643. unsigned radius
  644. )
  645. {
  646. using namespace std;
  647. // Preallocate memory
  648. PD1.resize(V.rows(),3);
  649. PD2.resize(V.rows(),3);
  650. // Precomputation
  651. CurvatureCalculator cc;
  652. cc.init(V.template cast<double>(),F.template cast<int>());
  653. cc.sphereRadius = radius;
  654. // Compute
  655. cc.computeCurvature();
  656. // Copy it back
  657. for (unsigned i=0; i<V.rows(); i++)
  658. {
  659. Eigen::Vector3d d1;
  660. Eigen::Vector3d d2;
  661. d1 << cc.curvDir[i][0][0], cc.curvDir[i][0][1], cc.curvDir[i][0][2];
  662. d2 << cc.curvDir[i][1][0], cc.curvDir[i][1][1], cc.curvDir[i][1][2];
  663. d1.normalize();
  664. d2.normalize();
  665. d1 *= cc.curv[i][0];
  666. d2 *= cc.curv[i][1];
  667. PD1.row(i) = d1;
  668. PD2.row(i) = d2;
  669. if (PD1.row(i) * PD2.row(i).transpose() > 10e-6)
  670. {
  671. cerr << "Something is wrong, vertex: i" << endl;
  672. PD1.row(i) *= 0;
  673. PD2.row(i) *= 0;
  674. }
  675. }
  676. }