205_Laplacian.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  2. #
  3. # This file is part of libigl, a simple c++ geometry processing library.
  4. #
  5. # Copyright (C) 2017 Sebastian Koch <s.koch@tu-berlin.de> and Daniele Panozzo <daniele.panozzo@gmail.com>
  6. #
  7. # This Source Code Form is subject to the terms of the Mozilla Public License
  8. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. # obtain one at http://mozilla.org/MPL/2.0/.
  10. import sys, os
  11. import math
  12. # Add the igl library to the modules search path
  13. sys.path.insert(0, os.getcwd() + "/../")
  14. import pyigl as igl
  15. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  16. dependencies = ["glfw"]
  17. check_dependencies(dependencies)
  18. V = igl.eigen.MatrixXd()
  19. U = igl.eigen.MatrixXd()
  20. F = igl.eigen.MatrixXi()
  21. L = igl.eigen.SparseMatrixd()
  22. viewer = igl.glfw.Viewer()
  23. # Load a mesh in OFF format
  24. igl.readOFF(TUTORIAL_SHARED_PATH + "cow.off", V, F)
  25. # Compute Laplace-Beltrami operator: #V by #V
  26. igl.cotmatrix(V, F, L)
  27. # Alternative construction of same Laplacian
  28. G = igl.eigen.SparseMatrixd()
  29. K = igl.eigen.SparseMatrixd()
  30. # Gradient/Divergence
  31. igl.grad(V, F, G)
  32. # Diagonal per-triangle "mass matrix"
  33. dblA = igl.eigen.MatrixXd()
  34. igl.doublearea(V, F, dblA)
  35. # Place areas along diagonal #dim times
  36. T = (dblA.replicate(3, 1) * 0.5).asDiagonal() * 1
  37. # Laplacian K built as discrete divergence of gradient or equivalently
  38. # discrete Dirichelet energy Hessian
  39. temp = -G.transpose()
  40. K = -G.transpose() * T * G
  41. print("|K-L|: ", (K - L).norm())
  42. def key_pressed(viewer, key, modifier):
  43. global V, U, F, L
  44. if key == ord('r') or key == ord('R'):
  45. U = V
  46. print("RESET")
  47. elif key == ord(' '):
  48. # Recompute just mass matrix on each step
  49. M = igl.eigen.SparseMatrixd()
  50. igl.massmatrix(U, F, igl.MASSMATRIX_TYPE_BARYCENTRIC, M)
  51. # Solve (M-delta*L) U = M*U
  52. S = (M - 0.001 * L)
  53. solver = igl.eigen.SimplicialLLTsparse(S)
  54. U = solver.solve(M * U)
  55. # Compute centroid and subtract (also important for numerics)
  56. dblA = igl.eigen.MatrixXd()
  57. igl.doublearea(U, F, dblA)
  58. print(dblA.sum())
  59. area = 0.5 * dblA.sum()
  60. BC = igl.eigen.MatrixXd()
  61. igl.barycenter(U, F, BC)
  62. centroid = igl.eigen.MatrixXd([[0.0, 0.0, 0.0]])
  63. for i in range(0, BC.rows()):
  64. centroid += 0.5 * dblA[i, 0] / area * BC.row(i)
  65. U -= centroid.replicate(U.rows(), 1)
  66. # Normalize to unit surface area (important for numerics)
  67. U = U / math.sqrt(area)
  68. else:
  69. return False
  70. # Send new positions, update normals, recenter
  71. viewer.data().set_vertices(U)
  72. viewer.data().compute_normals()
  73. viewer.core.align_camera_center(U, F)
  74. return True
  75. # Use original normals as pseudo-colors
  76. N = igl.eigen.MatrixXd()
  77. igl.per_vertex_normals(V, F, N)
  78. C = N.rowwiseNormalized() * 0.5 + 0.5
  79. # Initialize smoothing with base mesh
  80. U = V
  81. viewer.data().set_mesh(U, F)
  82. viewer.data().set_colors(C)
  83. viewer.callback_key_pressed = key_pressed
  84. print("Press [space] to smooth.")
  85. print("Press [r] to reset.")
  86. viewer.launch()