304_LinearEqualityConstraints.py 2.1 KB

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