509_Planarization.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. # Add the igl library to the modules search path
  10. sys.path.insert(0, os.getcwd() + "/../")
  11. import pyigl as igl
  12. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  13. dependencies = ["viewer"]
  14. check_dependencies(dependencies)
  15. viewer = igl.viewer.Viewer()
  16. # Quad mesh generated from conjugate field
  17. VQC = igl.eigen.MatrixXd()
  18. FQC = igl.eigen.MatrixXi()
  19. FQCtri = igl.eigen.MatrixXi()
  20. PQC0 = igl.eigen.MatrixXd()
  21. PQC1 = igl.eigen.MatrixXd()
  22. PQC2 = igl.eigen.MatrixXd()
  23. PQC3 = igl.eigen.MatrixXd()
  24. # Planarized quad mesh
  25. VQCplan = igl.eigen.MatrixXd()
  26. FQCtriplan = igl.eigen.MatrixXi()
  27. PQC0plan = igl.eigen.MatrixXd()
  28. PQC1plan = igl.eigen.MatrixXd()
  29. PQC2plan = igl.eigen.MatrixXd()
  30. PQC3plan = igl.eigen.MatrixXd()
  31. def key_down(viewer, key, modifier):
  32. if key == ord('1'):
  33. # Draw the triangulated quad mesh
  34. viewer.data.set_mesh(VQC, FQCtri)
  35. # Assign a color to each quad that corresponds to its planarity
  36. planarity = igl.eigen.MatrixXd()
  37. igl.quad_planarity(VQC, FQC, planarity)
  38. Ct = igl.eigen.MatrixXd()
  39. igl.jet(planarity, 0, 0.01, Ct)
  40. C = igl.eigen.MatrixXd(FQCtri.rows(), 3)
  41. C.setTopRows(Ct.rows(), Ct)
  42. C.setBottomRows(Ct.rows(), Ct)
  43. viewer.data.set_colors(C)
  44. # Plot a line for each edge of the quad mesh
  45. viewer.data.add_edges(PQC0, PQC1, igl.eigen.MatrixXd([[0, 0, 0]]))
  46. viewer.data.add_edges(PQC1, PQC2, igl.eigen.MatrixXd([[0, 0, 0]]))
  47. viewer.data.add_edges(PQC2, PQC3, igl.eigen.MatrixXd([[0, 0, 0]]))
  48. viewer.data.add_edges(PQC3, PQC0, igl.eigen.MatrixXd([[0, 0, 0]]))
  49. elif key == ord('2'):
  50. # Draw the planar quad mesh
  51. viewer.data.set_mesh(VQCplan, FQCtri)
  52. # Assign a color to each quad that corresponds to its planarity
  53. planarity = igl.eigen.MatrixXd()
  54. igl.quad_planarity(VQCplan, FQC, planarity)
  55. Ct = igl.eigen.MatrixXd()
  56. igl.jet(planarity, 0, 0.01, Ct)
  57. C = igl.eigen.MatrixXd(FQCtri.rows(), 3)
  58. C.setTopRows(Ct.rows(), Ct)
  59. C.setBottomRows(Ct.rows(), Ct)
  60. viewer.data.set_colors(C)
  61. # Plot a line for each edge of the quad mesh
  62. viewer.data.add_edges(PQC0plan, PQC1plan, igl.eigen.MatrixXd([[0, 0, 0]]))
  63. viewer.data.add_edges(PQC1plan, PQC2plan, igl.eigen.MatrixXd([[0, 0, 0]]))
  64. viewer.data.add_edges(PQC2plan, PQC3plan, igl.eigen.MatrixXd([[0, 0, 0]]))
  65. viewer.data.add_edges(PQC3plan, PQC0plan, igl.eigen.MatrixXd([[0, 0, 0]]))
  66. else:
  67. return False
  68. return True
  69. # Load a quad mesh generated by a conjugate field
  70. igl.readOFF(TUTORIAL_SHARED_PATH + "inspired_mesh_quads_Conjugate.off", VQC, FQC)
  71. # Convert it to a triangle mesh
  72. FQCtri.resize(2 * FQC.rows(), 3)
  73. FQCtriUpper = igl.eigen.MatrixXi(FQC.rows(), 3)
  74. FQCtriLower = igl.eigen.MatrixXi(FQC.rows(), 3)
  75. FQCtriUpper.setCol(0, FQC.col(0))
  76. FQCtriUpper.setCol(1, FQC.col(1))
  77. FQCtriUpper.setCol(2, FQC.col(2))
  78. FQCtriLower.setCol(0, FQC.col(2))
  79. FQCtriLower.setCol(1, FQC.col(3))
  80. FQCtriLower.setCol(2, FQC.col(0))
  81. FQCtri.setTopRows(FQCtriUpper.rows(), FQCtriUpper)
  82. FQCtri.setBottomRows(FQCtriLower.rows(), FQCtriLower)
  83. igl.slice(VQC, FQC.col(0), 1, PQC0)
  84. igl.slice(VQC, FQC.col(1), 1, PQC1)
  85. igl.slice(VQC, FQC.col(2), 1, PQC2)
  86. igl.slice(VQC, FQC.col(3), 1, PQC3)
  87. # Planarize it
  88. igl.planarize_quad_mesh(VQC, FQC, 100, 0.005, VQCplan)
  89. # Convert the planarized mesh to triangles
  90. igl.slice(VQCplan, FQC.col(0), 1, PQC0plan)
  91. igl.slice(VQCplan, FQC.col(1), 1, PQC1plan)
  92. igl.slice(VQCplan, FQC.col(2), 1, PQC2plan)
  93. igl.slice(VQCplan, FQC.col(3), 1, PQC3plan)
  94. # Launch the viewer
  95. key_down(viewer, ord('2'), 0)
  96. viewer.core.invert_normals = True
  97. viewer.core.show_lines = False
  98. viewer.callback_key_down = key_down
  99. viewer.launch()