507_Planarization.py 4.1 KB

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