203_CurvatureDirections.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # This file is part of libigl, a simple c++ geometry processing library.
  2. #
  3. # Copyright (C) 2017 Sebastian Koch <[email protected]> and Daniele Panozzo <[email protected]>
  4. #
  5. # This Source Code Form is subject to the terms of the Mozilla Public License
  6. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. # obtain one at http://mozilla.org/MPL/2.0/.
  8. import sys, os
  9. # Add the igl library to the modules search path
  10. sys.path.insert(0, os.getcwd() + "/../")
  11. import pyigl as igl
  12. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  13. dependencies = ["viewer"]
  14. check_dependencies(dependencies)
  15. V = igl.eigen.MatrixXd()
  16. F = igl.eigen.MatrixXi()
  17. igl.read_triangle_mesh(TUTORIAL_SHARED_PATH + "fertility.off", V, F)
  18. # Alternative discrete mean curvature
  19. HN = igl.eigen.MatrixXd()
  20. L = igl.eigen.SparseMatrixd()
  21. M = igl.eigen.SparseMatrixd()
  22. Minv = igl.eigen.SparseMatrixd()
  23. igl.cotmatrix(V, F, L)
  24. igl.massmatrix(V, F, igl.MASSMATRIX_TYPE_VORONOI, M)
  25. igl.invert_diag(M, Minv)
  26. # Laplace-Beltrami of position
  27. HN = -Minv * (L * V)
  28. # Extract magnitude as mean curvature
  29. H = HN.rowwiseNorm()
  30. # Compute curvature directions via quadric fitting
  31. PD1 = igl.eigen.MatrixXd()
  32. PD2 = igl.eigen.MatrixXd()
  33. PV1 = igl.eigen.MatrixXd()
  34. PV2 = igl.eigen.MatrixXd()
  35. igl.principal_curvature(V, F, PD1, PD2, PV1, PV2)
  36. # Mean curvature
  37. H = 0.5 * (PV1 + PV2)
  38. viewer = igl.viewer.Viewer()
  39. viewer.data.set_mesh(V, F)
  40. # Compute pseudocolor
  41. C = igl.eigen.MatrixXd()
  42. igl.parula(H, True, C)
  43. viewer.data.set_colors(C)
  44. # Average edge length for sizing
  45. avg = igl.avg_edge_length(V, F)
  46. # Draw a blue segment parallel to the minimal curvature direction
  47. red = igl.eigen.MatrixXd([[0.8, 0.2, 0.2]])
  48. blue = igl.eigen.MatrixXd([[0.2, 0.2, 0.8]])
  49. viewer.data.add_edges(V + PD1 * avg, V - PD1 * avg, blue)
  50. # Draw a red segment parallel to the maximal curvature direction
  51. viewer.data.add_edges(V + PD2 * avg, V - PD2 * avg, red)
  52. # Hide wireframe
  53. viewer.core.show_lines = False
  54. viewer.launch()