304_LinearEqualityConstraints.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Add the igl library to the modules search path
  2. import sys, os
  3. sys.path.insert(0, os.getcwd() + "/../")
  4. import pyigl as igl
  5. V = igl.eigen.MatrixXd()
  6. F = igl.eigen.MatrixXi()
  7. igl.readOFF("../../tutorial/shared/cheburashka.off",V,F)
  8. # Two fixed points
  9. # Left hand, left foot
  10. b = igl.eigen.MatrixXi([[4331],[5957]])
  11. bc = igl.eigen.MatrixXd([[1],[-1]])
  12. # Construct Laplacian and mass matrix
  13. L = igl.eigen.SparseMatrixd()
  14. M = igl.eigen.SparseMatrixd()
  15. Minv = igl.eigen.SparseMatrixd()
  16. Q = igl.eigen.SparseMatrixd()
  17. igl.cotmatrix(V,F,L)
  18. igl.massmatrix(V,F,igl.MASSMATRIX_TYPE_VORONOI,M)
  19. igl.invert_diag(M,Minv)
  20. # Bi-Laplacian
  21. Q = L * (Minv * L);
  22. # Zero linear term
  23. B = igl.eigen.MatrixXd.Zero(V.rows(),1);
  24. Z = igl.eigen.MatrixXd()
  25. Z_const = igl.eigen.MatrixXd()
  26. # Alternative, short hand
  27. mqwf = igl.min_quad_with_fixed_data()
  28. # Empty constraints
  29. Beq = igl.eigen.MatrixXd()
  30. Aeq = igl.eigen.SparseMatrixd()
  31. igl.min_quad_with_fixed_precompute(Q,b,Aeq,True,mqwf)
  32. igl.min_quad_with_fixed_solve(mqwf,B,bc,Beq,Z)
  33. # Constraint forcing difference of two points to be 0
  34. Aeq = igl.eigen.SparseMatrixd(1,V.rows())
  35. # Right hand, right foot
  36. Aeq.insert(0,6074,1)
  37. Aeq.insert(0,6523,-1)
  38. Aeq.makeCompressed()
  39. Beq = igl.eigen.MatrixXd([[0]])
  40. igl.min_quad_with_fixed_precompute(Q,b,Aeq,True,mqwf)
  41. igl.min_quad_with_fixed_solve(mqwf,B,bc,Beq,Z_const)
  42. # Pseudo-color based on solution
  43. global C
  44. C = igl.eigen.MatrixXd()
  45. global C_const
  46. C_const = igl.eigen.MatrixXd()
  47. global toggle
  48. toggle = True
  49. # Use same color axes
  50. min_z = min(Z.minCoeff(),Z_const.minCoeff())
  51. max_z = max(Z.maxCoeff(),Z_const.maxCoeff())
  52. igl.jet( Z,min_z,max_z,C);
  53. igl.jet(Z_const,min_z,max_z,C_const);
  54. # Plot the mesh with pseudocolors
  55. viewer = igl.viewer.Viewer()
  56. viewer.data.set_mesh(V, F)
  57. viewer.core.show_lines = False
  58. viewer.data.set_colors(C)
  59. def key_down(viewer,key,mode):
  60. if key == ord(' '):
  61. global toggle
  62. global C
  63. global C_const
  64. if toggle:
  65. viewer.data.set_colors(C)
  66. else:
  67. viewer.data.set_colors(C_const)
  68. toggle = not toggle;
  69. return True
  70. return False
  71. viewer.callback_key_down = key_down
  72. print("Press [space] to toggle between unconstrained and constrained.")
  73. viewer.launch()