203_CurvatureDirections.py 1.4 KB

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