504_NRosyDesign.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. from math import atan2,pi,cos,sin
  6. # Mesh
  7. V = igl.eigen.MatrixXd()
  8. F = igl.eigen.MatrixXi()
  9. # Constrained faces id
  10. b = igl.eigen.MatrixXi()
  11. # Constrained faces representative vector
  12. bc = igl.eigen.MatrixXd()
  13. # Degree of the N-RoSy field
  14. N = 4;
  15. # Converts a representative vector per face in the full set of vectors that describe
  16. # an N-RoSy field
  17. def representative_to_nrosy(V, F, R, N, Y):
  18. B1 = igl.eigen.MatrixXd()
  19. B2 = igl.eigen.MatrixXd()
  20. B3 = igl.eigen.MatrixXd()
  21. igl.local_basis(V,F,B1,B2,B3)
  22. Y.resize(F.rows()*N, 3)
  23. for i in range (0,F.rows()):
  24. x = R.row(i) * B1.row(i).transpose()
  25. y = R.row(i) * B2.row(i).transpose()
  26. angle = atan2(y[0],x[0])
  27. for j in range(0,N):
  28. anglej = angle + 2*pi*j/float(N)
  29. xj = cos(anglej)
  30. yj = sin(anglej)
  31. Y.setRow(i*N+j, xj * B1.row(i) + yj * B2.row(i))
  32. # Plots the mesh with an N-RoSy field and its singularities on top
  33. # The constrained faces (b) are colored in red.
  34. def plot_mesh_nrosy(viewer, V, F, N, PD1, S, b):
  35. # Clear the mesh
  36. viewer.data.clear()
  37. viewer.data.set_mesh(V,F)
  38. # Expand the representative vectors in the full vector set and plot them as lines
  39. avg = igl.avg_edge_length(V, F)
  40. Y = igl.eigen.MatrixXd()
  41. representative_to_nrosy(V, F, PD1, N, Y)
  42. B = igl.eigen.MatrixXd()
  43. igl.barycenter(V,F,B)
  44. Be = igl.eigen.MatrixXd(B.rows()*N,3)
  45. for i in range(0,B.rows()):
  46. for j in range(0,N):
  47. Be.setRow(i*N+j,B.row(i))
  48. viewer.data.add_edges(Be,Be+Y*(avg/2),igl.eigen.MatrixXd([[0,0,1]]))
  49. # Plot the singularities as colored dots (red for negative, blue for positive)
  50. for i in range(0,S.size()):
  51. if S[i] < -0.001:
  52. viewer.data.add_points(V.row(i),igl.eigen.MatrixXd([[1,0,0]]))
  53. elif S[i] > 0.001:
  54. viewer.data.add_points(V.row(i),igl.eigen.MatrixXd([[0,1,0]]));
  55. # Highlight in red the constrained faces
  56. C = igl.eigen.MatrixXd.Constant(F.rows(),3,1)
  57. for i in range(0,b.size()):
  58. C.setRow(b[i], igl.eigen.MatrixXd([[1, 0, 0]]))
  59. viewer.data.set_colors(C)
  60. # It allows to change the degree of the field when a number is pressed
  61. def key_down(viewer, key, modifier):
  62. global N
  63. if key >= ord('1') and key <= ord('9'):
  64. N = key - ord('0')
  65. R = igl.eigen.MatrixXd()
  66. S = igl.eigen.MatrixXd()
  67. igl.comiso.nrosy(V,F,b,bc,igl.eigen.MatrixXi(),igl.eigen.MatrixXd(),igl.eigen.MatrixXd(),N,0.5,R,S)
  68. plot_mesh_nrosy(viewer,V,F,N,R,S,b)
  69. return False
  70. # Load a mesh in OFF format
  71. igl.readOFF("../../tutorial/shared/bumpy.off", V, F);
  72. # Threshold faces with high anisotropy
  73. b = igl.eigen.MatrixXi([[0]])
  74. bc = igl.eigen.MatrixXd([[1,1,1]])
  75. viewer = igl.viewer.Viewer()
  76. # Interpolate the field and plot
  77. key_down(viewer, ord('4'), 0)
  78. # Plot the mesh
  79. viewer.data.set_mesh(V, F)
  80. viewer.callback_key_down = key_down
  81. # Disable wireframe
  82. viewer.core.show_lines = False
  83. # Launch the viewer
  84. viewer.launch()