304_LinearEqualityConstraints.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. # 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.readOFF(TUTORIAL_SHARED_PATH + "cheburashka.off", V, F)
  18. # Two fixed points
  19. # Left hand, left foot
  20. b = igl.eigen.MatrixXi([[4331], [5957]])
  21. bc = igl.eigen.MatrixXd([[1], [-1]])
  22. # Construct Laplacian and mass matrix
  23. L = igl.eigen.SparseMatrixd()
  24. M = igl.eigen.SparseMatrixd()
  25. Minv = igl.eigen.SparseMatrixd()
  26. Q = igl.eigen.SparseMatrixd()
  27. igl.cotmatrix(V, F, L)
  28. igl.massmatrix(V, F, igl.MASSMATRIX_TYPE_VORONOI, M)
  29. igl.invert_diag(M, Minv)
  30. # Bi-Laplacian
  31. Q = L * (Minv * L)
  32. # Zero linear term
  33. B = igl.eigen.MatrixXd.Zero(V.rows(), 1)
  34. Z = igl.eigen.MatrixXd()
  35. Z_const = igl.eigen.MatrixXd()
  36. # Alternative, short hand
  37. mqwf = igl.min_quad_with_fixed_data()
  38. # Empty constraints
  39. Beq = igl.eigen.MatrixXd()
  40. Aeq = igl.eigen.SparseMatrixd()
  41. igl.min_quad_with_fixed_precompute(Q, b, Aeq, True, mqwf)
  42. igl.min_quad_with_fixed_solve(mqwf, B, bc, Beq, Z)
  43. # Constraint forcing difference of two points to be 0
  44. Aeq = igl.eigen.SparseMatrixd(1, V.rows())
  45. # Right hand, right foot
  46. Aeq.insert(0, 6074, 1)
  47. Aeq.insert(0, 6523, -1)
  48. Aeq.makeCompressed()
  49. Beq = igl.eigen.MatrixXd([[0]])
  50. igl.min_quad_with_fixed_precompute(Q, b, Aeq, True, mqwf)
  51. igl.min_quad_with_fixed_solve(mqwf, B, bc, Beq, Z_const)
  52. # Global definitions for viewer
  53. # Pseudo-color based on solution
  54. C = igl.eigen.MatrixXd()
  55. C_const = igl.eigen.MatrixXd()
  56. toggle = True
  57. # Use same color axes
  58. min_z = min(Z.minCoeff(), Z_const.minCoeff())
  59. max_z = max(Z.maxCoeff(), Z_const.maxCoeff())
  60. igl.jet(Z, min_z, max_z, C)
  61. igl.jet(Z_const, min_z, max_z, C_const)
  62. # Plot the mesh with pseudocolors
  63. viewer = igl.viewer.Viewer()
  64. viewer.data.set_mesh(V, F)
  65. viewer.core.show_lines = False
  66. viewer.data.set_colors(C)
  67. def key_down(viewer, key, mode):
  68. if key == ord(' '):
  69. global toggle, C, C_const
  70. if toggle:
  71. viewer.data.set_colors(C)
  72. else:
  73. viewer.data.set_colors(C_const)
  74. toggle = not toggle
  75. return True
  76. return False
  77. viewer.callback_key_down = key_down
  78. print("Press [space] to toggle between unconstrained and constrained.")
  79. viewer.launch()