504_NRosyDesign.py 3.6 KB

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