305_QuadraticProgramming.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. b = igl.eigen.MatrixXi()
  16. B = igl.eigen.MatrixXd()
  17. bc = igl.eigen.MatrixXd()
  18. lx = igl.eigen.MatrixXd()
  19. ux = igl.eigen.MatrixXd()
  20. Beq = igl.eigen.MatrixXd()
  21. Bieq = igl.eigen.MatrixXd()
  22. Z = igl.eigen.MatrixXd()
  23. Q = igl.eigen.SparseMatrixd()
  24. Aeq = igl.eigen.SparseMatrixd()
  25. Aieq = igl.eigen.SparseMatrixd()
  26. def solve(viewer):
  27. global Q, B, b, bc, Aeq, Beq, Aieq, Bieq, lx, ux, Z
  28. params = igl.active_set_params()
  29. params.max_iter = 8
  30. igl.active_set(Q, B, b, bc, Aeq, Beq, Aieq, Bieq, lx, ux, params, Z)
  31. C = igl.eigen.MatrixXd()
  32. igl.jet(Z, 0, 1, C)
  33. viewer.data.set_colors(C)
  34. def key_down(viewer, key, mod):
  35. global Beq, solve
  36. if key == ord('.'):
  37. Beq[0, 0] = Beq[0, 0] * 2.0
  38. solve(viewer)
  39. return True
  40. elif key == ord(','):
  41. Beq[0, 0] = Beq[0, 0] / 2.0
  42. solve(viewer)
  43. return True
  44. elif key == ord(' '):
  45. solve(viewer)
  46. return True
  47. return False
  48. V = igl.eigen.MatrixXd()
  49. F = igl.eigen.MatrixXi()
  50. igl.readOFF(TUTORIAL_SHARED_PATH + "cheburashka.off", V, F)
  51. # Plot the mesh
  52. viewer = igl.viewer.Viewer()
  53. viewer.data.set_mesh(V, F)
  54. viewer.core.show_lines = False
  55. viewer.callback_key_down = key_down
  56. # One fixed point on belly
  57. b = igl.eigen.MatrixXi([[2556]])
  58. bc = igl.eigen.MatrixXd([[1]])
  59. # Construct Laplacian and mass matrix
  60. L = igl.eigen.SparseMatrixd()
  61. M = igl.eigen.SparseMatrixd()
  62. Minv = igl.eigen.SparseMatrixd()
  63. igl.cotmatrix(V, F, L)
  64. igl.massmatrix(V, F, igl.MASSMATRIX_TYPE_VORONOI, M)
  65. igl.invert_diag(M, Minv)
  66. # Bi-Laplacian
  67. Q = L.transpose() * (Minv * L)
  68. # Zero linear term
  69. B = igl.eigen.MatrixXd.Zero(V.rows(), 1)
  70. # Lower and upper bound
  71. lx = igl.eigen.MatrixXd.Zero(V.rows(), 1)
  72. ux = igl.eigen.MatrixXd.Ones(V.rows(), 1)
  73. # Equality constraint constrain solution to sum to 1
  74. Beq = igl.eigen.MatrixXd([[0.08]])
  75. Aeq = M.diagonal().sparseView().transpose()
  76. # (Empty inequality constraints)
  77. solve(viewer)
  78. print("Press '.' to increase scale and resolve.")
  79. print("Press ',' to decrease scale and resolve.")
  80. viewer.launch()