505_MIQ.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #!/usr/bin/env python
  2. #
  3. # This file is part of libigl, a simple c++ geometry processing library.
  4. #
  5. # Copyright (C) 2017 Sebastian Koch <s.koch@tu-berlin.de> and Daniele Panozzo <daniele.panozzo@gmail.com>
  6. #
  7. # This Source Code Form is subject to the terms of the Mozilla Public License
  8. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. # obtain one at http://mozilla.org/MPL/2.0/.
  10. import sys, os
  11. from math import pi
  12. # Add the igl library to the modules search path
  13. sys.path.insert(0, os.getcwd() + "/../")
  14. import pyigl as igl
  15. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  16. dependencies = ["comiso", "glfw"]
  17. check_dependencies(dependencies)
  18. V = igl.eigen.MatrixXd()
  19. F = igl.eigen.MatrixXi()
  20. # Face barycenters
  21. B = igl.eigen.MatrixXd()
  22. # Scale for visualizing the fields
  23. global_scale = 1
  24. extend_arrows = False
  25. # Cross field
  26. X1 = igl.eigen.MatrixXd()
  27. X2 = igl.eigen.MatrixXd()
  28. # Bisector field
  29. BIS1 = igl.eigen.MatrixXd()
  30. BIS2 = igl.eigen.MatrixXd()
  31. # Combed bisector
  32. BIS1_combed = igl.eigen.MatrixXd()
  33. BIS2_combed = igl.eigen.MatrixXd()
  34. # Per-corner, integer mismatches
  35. MMatch = igl.eigen.MatrixXi()
  36. # Field singularities
  37. isSingularity = igl.eigen.MatrixXi()
  38. singularityIndex = igl.eigen.MatrixXi()
  39. # Per corner seams
  40. Seams = igl.eigen.MatrixXi()
  41. # Combed field
  42. X1_combed = igl.eigen.MatrixXd()
  43. X2_combed = igl.eigen.MatrixXd()
  44. # Global parametrization (with seams)
  45. UV_seams = igl.eigen.MatrixXd()
  46. FUV_seams = igl.eigen.MatrixXi()
  47. # Global parametrization
  48. UV = igl.eigen.MatrixXd()
  49. FUV = igl.eigen.MatrixXi()
  50. # Texture
  51. texture_R = igl.eigen.MatrixXuc()
  52. texture_G = igl.eigen.MatrixXuc()
  53. texture_B = igl.eigen.MatrixXuc()
  54. # Create a texture that hides the integer translation in the parametrization
  55. def line_texture():
  56. size = 128
  57. size2 = int(size / 2)
  58. lineWidth = 3
  59. texture_R.setConstant(size, size, 255)
  60. for i in range(0, size):
  61. for j in range(size2 - lineWidth, size2 + lineWidth + 1):
  62. texture_R[i, j] = 0
  63. for i in range(size2 - lineWidth, size2 + lineWidth + 1):
  64. for j in range(0, size):
  65. texture_R[i, j] = 0
  66. texture_G = texture_R.copy()
  67. texture_B = texture_R.copy()
  68. return (texture_R, texture_G, texture_B)
  69. def key_down(viewer, key, modifier):
  70. global extend_arrows, texture_R, texture_G, texture_B
  71. if key == ord('E'):
  72. extend_arrows = not extend_arrows
  73. if key < ord('1') or key > ord('8'):
  74. return False
  75. viewer.data().clear()
  76. viewer.data().show_lines = False
  77. viewer.data().show_texture = False
  78. if key == ord('1'):
  79. # Cross field
  80. viewer.data().set_mesh(V, F)
  81. viewer.data().add_edges(B - global_scale * X1 if extend_arrows else B, B + global_scale * X1,
  82. igl.eigen.MatrixXd([[1, 0, 0]]))
  83. viewer.data().add_edges(B - global_scale * X2 if extend_arrows else B, B + global_scale * X2,
  84. igl.eigen.MatrixXd([[0, 0, 1]]))
  85. if key == ord('2'):
  86. # Bisector field
  87. viewer.data().set_mesh(V, F)
  88. viewer.data().add_edges(B - global_scale * BIS1 if extend_arrows else B, B + global_scale * BIS1,
  89. igl.eigen.MatrixXd([[1, 0, 0]]))
  90. viewer.data().add_edges(B - global_scale * BIS2 if extend_arrows else B, B + global_scale * BIS2,
  91. igl.eigen.MatrixXd([[0, 0, 1]]))
  92. if key == ord('3'):
  93. # Bisector field combed
  94. viewer.data().set_mesh(V, F)
  95. viewer.data().add_edges(B - global_scale * BIS1_combed if extend_arrows else B, B + global_scale * BIS1_combed,
  96. igl.eigen.MatrixXd([[1, 0, 0]]))
  97. viewer.data().add_edges(B - global_scale * BIS2_combed if extend_arrows else B, B + global_scale * BIS2_combed,
  98. igl.eigen.MatrixXd([[0, 0, 1]]))
  99. if key == ord('4'):
  100. # Singularities and cuts
  101. viewer.data().set_mesh(V, F)
  102. # Plot cuts
  103. l_count = Seams.sum()
  104. P1 = igl.eigen.MatrixXd(l_count, 3)
  105. P2 = igl.eigen.MatrixXd(l_count, 3)
  106. for i in range(0, Seams.rows()):
  107. for j in range(0, Seams.cols()):
  108. if Seams[i, j] != 0:
  109. P1.setRow(l_count - 1, V.row(F[i, j]))
  110. P2.setRow(l_count - 1, V.row(F[i, (j + 1) % 3]))
  111. l_count -= 1
  112. viewer.data().add_edges(P1, P2, igl.eigen.MatrixXd([[1, 0, 0]]))
  113. # Plot the singularities as colored dots (red for negative, blue for positive)
  114. for i in range(0, singularityIndex.size()):
  115. if 2 > singularityIndex[i] > 0:
  116. viewer.data().add_points(V.row(i), igl.eigen.MatrixXd([[1, 0, 0]]))
  117. elif singularityIndex[i] > 2:
  118. viewer.data().add_points(V.row(i), igl.eigen.MatrixXd([[1, 0, 0]]))
  119. if key == ord('5'):
  120. # Singularities and cuts, original field
  121. # Singularities and cuts
  122. viewer.data().set_mesh(V, F)
  123. viewer.data().add_edges(B - global_scale * X1_combed if extend_arrows else B, B + global_scale * X1_combed,
  124. igl.eigen.MatrixXd([[1, 0, 0]]))
  125. viewer.data().add_edges(B - global_scale * X2_combed if extend_arrows else B, B + global_scale * X2_combed,
  126. igl.eigen.MatrixXd([[0, 0, 1]]))
  127. # Plot cuts
  128. l_count = Seams.sum()
  129. P1 = igl.eigen.MatrixXd(l_count, 3)
  130. P2 = igl.eigen.MatrixXd(l_count, 3)
  131. for i in range(0, Seams.rows()):
  132. for j in range(0, Seams.cols()):
  133. if Seams[i, j] != 0:
  134. P1.setRow(l_count - 1, V.row(F[i, j]))
  135. P2.setRow(l_count - 1, V.row(F[i, (j + 1) % 3]))
  136. l_count -= 1
  137. viewer.data().add_edges(P1, P2, igl.eigen.MatrixXd([[1, 0, 0]]))
  138. # Plot the singularities as colored dots (red for negative, blue for positive)
  139. for i in range(0, singularityIndex.size()):
  140. if 2 > singularityIndex[i] > 0:
  141. viewer.data().add_points(V.row(i), igl.eigen.MatrixXd([[1, 0, 0]]))
  142. elif singularityIndex[i] > 2:
  143. viewer.data().add_points(V.row(i), igl.eigen.MatrixXd([[0, 1, 0]]))
  144. if key == ord('6'):
  145. # Global parametrization UV
  146. viewer.data().set_mesh(UV, FUV)
  147. viewer.data().set_uv(UV)
  148. viewer.data().show_lines = True
  149. if key == ord('7'):
  150. # Global parametrization in 3D
  151. viewer.data().set_mesh(V, F)
  152. viewer.data().set_uv(UV, FUV)
  153. viewer.data().show_texture = True
  154. if key == ord('8'):
  155. # Global parametrization in 3D with seams
  156. viewer.data().set_mesh(V, F)
  157. viewer.data().set_uv(UV_seams, FUV_seams)
  158. viewer.data().show_texture = True
  159. viewer.data().set_colors(igl.eigen.MatrixXd([[1, 1, 1]]))
  160. viewer.data().set_texture(texture_R, texture_B, texture_G)
  161. viewer.core.align_camera_center(viewer.data().V, viewer.data().F)
  162. return False
  163. # Load a mesh in OFF format
  164. igl.readOFF(TUTORIAL_SHARED_PATH + "3holes.off", V, F)
  165. # Compute face barycenters
  166. igl.barycenter(V, F, B)
  167. # Compute scale for visualizing fields
  168. global_scale = .5 * igl.avg_edge_length(V, F)
  169. # Contrain one face
  170. b = igl.eigen.MatrixXd([[0]]).castint()
  171. bc = igl.eigen.MatrixXd([[1, 0, 0]])
  172. # Create a smooth 4-RoSy field
  173. S = igl.eigen.MatrixXd()
  174. igl.comiso.nrosy(V, F, b, bc, igl.eigen.MatrixXi(), igl.eigen.MatrixXd(), igl.eigen.MatrixXd(), 4, 0.5, X1, S)
  175. # Find the orthogonal vector
  176. B1 = igl.eigen.MatrixXd()
  177. B2 = igl.eigen.MatrixXd()
  178. B3 = igl.eigen.MatrixXd()
  179. igl.local_basis(V, F, B1, B2, B3)
  180. X2 = igl.rotate_vectors(X1, igl.eigen.MatrixXd.Constant(1, 1, pi / 2), B1, B2)
  181. gradient_size = 50
  182. iterations = 0
  183. stiffness = 5.0
  184. direct_round = False
  185. # Always work on the bisectors, it is more general
  186. igl.compute_frame_field_bisectors(V, F, X1, X2, BIS1, BIS2)
  187. # Comb the field, implicitly defining the seams
  188. igl.comb_cross_field(V, F, BIS1, BIS2, BIS1_combed, BIS2_combed)
  189. # Find the integer mismatches
  190. igl.cross_field_missmatch(V, F, BIS1_combed, BIS2_combed, True, MMatch)
  191. # Find the singularities
  192. igl.find_cross_field_singularities(V, F, MMatch, isSingularity, singularityIndex)
  193. # Cut the mesh, duplicating all vertices on the seams
  194. igl.cut_mesh_from_singularities(V, F, MMatch, Seams)
  195. # Comb the frame-field accordingly
  196. igl.comb_frame_field(V, F, X1, X2, BIS1_combed, BIS2_combed, X1_combed, X2_combed)
  197. # Global parametrization
  198. igl.comiso.miq(V, F, X1_combed, X2_combed, MMatch, isSingularity, Seams, UV, FUV, gradient_size, stiffness,
  199. direct_round, iterations, 5, True, True)
  200. # Global parametrization (with seams, only for demonstration)
  201. igl.comiso.miq(V, F, X1_combed, X2_combed, MMatch, isSingularity, Seams, UV_seams, FUV_seams, gradient_size,
  202. stiffness, direct_round, iterations, 5, False)
  203. # Plot the mesh
  204. viewer = igl.glfw.Viewer()
  205. # Replace the standard texture with an integer shift invariant texture
  206. (texture_R, texture_G, texture_B) = line_texture()
  207. # Plot the original mesh with a texture parametrization
  208. key_down(viewer, ord('7'), 0)
  209. # Launch the viewer
  210. viewer.callback_key_down = key_down
  211. viewer.launch()