203_CurvatureDirections.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import igl
  2. V = igl.eigen.MatrixXd();
  3. F = igl.eigen.MatrixXi();
  4. igl.read_triangle_mesh("../tutorial/shared/fertility.off", V, F);
  5. # Alternative discrete mean curvature
  6. HN = igl.eigen.MatrixXd()
  7. L = igl.eigen.SparseMatrixd()
  8. M = igl.eigen.SparseMatrixd()
  9. Minv = igl.eigen.SparseMatrixd()
  10. igl.cotmatrix(V,F,L)
  11. igl.massmatrix(V,F,igl.MASSMATRIX_TYPE_VORONOI,M)
  12. igl.invert_diag(M,Minv)
  13. # Laplace-Beltrami of position
  14. HN = -Minv*(L*V)
  15. # Extract magnitude as mean curvature
  16. H = HN.rowwiseNorm()
  17. # Compute curvature directions via quadric fitting
  18. PD1 = igl.eigen.MatrixXd()
  19. PD2 = igl.eigen.MatrixXd()
  20. PV1 = igl.eigen.MatrixXd()
  21. PV2 = igl.eigen.MatrixXd()
  22. igl.principal_curvature(V,F,PD1,PD2,PV1,PV2)
  23. # Mean curvature
  24. H = 0.5*(PV1+PV2)
  25. viewer = igl.viewer.Viewer()
  26. viewer.data.set_mesh(V, F)
  27. # Compute pseudocolor
  28. C = igl.eigen.MatrixXd()
  29. igl.parula(H,True,C)
  30. viewer.data.set_colors(C)
  31. # Average edge length for sizing
  32. avg = igl.avg_edge_length(V,F)
  33. # Draw a blue segment parallel to the minimal curvature direction
  34. red = igl.eigen.MatrixXd([[0.8,0.2,0.2]])
  35. blue = igl.eigen.MatrixXd([[0.2,0.2,0.8]])
  36. viewer.data.add_edges(V + PD1*avg, V - PD1*avg, blue)
  37. # Draw a red segment parallel to the maximal curvature direction
  38. viewer.data.add_edges(V + PD2*avg, V - PD2*avg, red)
  39. # Hide wireframe
  40. viewer.core.show_lines = False
  41. viewer.launch();