203_CurvatureDirections.py 1.6 KB

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