205_Laplacian.py 3.0 KB

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