203_CurvatureDirections.py 1.4 KB

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