504_NRosyDesign.py 3.3 KB

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