305_QuadraticProgramming.py 2.6 KB

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