706_FacetOrientation.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, print_usage
  15. dependencies = ["embree", "glfw"]
  16. check_dependencies(dependencies)
  17. def key_down(viewer, key, modifier):
  18. global facetwise, is_showing_reoriented, FF
  19. if key == ord('F') or key == ord('f'):
  20. facetwise = (facetwise + 1) % 2
  21. elif key == ord('S') or key == ord('s'):
  22. scramble_colors()
  23. elif key == ord(' '):
  24. is_showing_reoriented = ~is_showing_reoriented
  25. viewer.data().clear()
  26. viewer.data().set_mesh(V, FF[facetwise] if is_showing_reoriented else F)
  27. viewer.data().set_colors(RGBcolors[facetwise])
  28. return True
  29. def scramble_colors():
  30. global C, viewer, RGBcolors
  31. for p in range(2):
  32. R = igl.eigen.MatrixXi()
  33. igl.randperm(C[p].maxCoeff() + 1, R)
  34. C[p] = igl.slice(R, igl.eigen.MatrixXi(C[p]))
  35. HSV = igl.eigen.MatrixXd(C[p].rows(), 3)
  36. HSV.setCol(0, 360.0 * C[p].castdouble() / C[p].maxCoeff())
  37. HSVright = igl.eigen.MatrixXd(HSV.rows(), 2)
  38. HSVright.setConstant(1.0)
  39. HSV.setRightCols(2, HSVright)
  40. igl.hsv_to_rgb(HSV, RGBcolors[p])
  41. viewer.data().set_colors(RGBcolors[facetwise])
  42. if __name__ == "__main__":
  43. keys = {"space": "toggle between original and reoriented faces",
  44. "F,f": "toggle between patchwise and facetwise reorientation",
  45. "S,s": "scramble colors"}
  46. print_usage(keys)
  47. V = igl.eigen.MatrixXd()
  48. F = igl.eigen.MatrixXi()
  49. C = [igl.eigen.MatrixXi(), igl.eigen.MatrixXi()]
  50. RGBcolors = [igl.eigen.MatrixXd(), igl.eigen.MatrixXd()]
  51. FF = [igl.eigen.MatrixXi(), igl.eigen.MatrixXi()]
  52. is_showing_reoriented = False
  53. facetwise = 0
  54. igl.read_triangle_mesh(TUTORIAL_SHARED_PATH + "truck.obj", V, F)
  55. # Compute patches
  56. for p in range(2):
  57. I = igl.eigen.MatrixXi()
  58. igl.embree.reorient_facets_raycast(V, F, F.rows() * 100, 10, p == 1, False, False, I, C[p])
  59. # apply reorientation
  60. FF[p].conservativeResize(F.rows(), F.cols())
  61. for i in range(I.rows()):
  62. if I[i]:
  63. FF[p].setRow(i, F.row(i).rowwiseReverse())
  64. else:
  65. FF[p].setRow(i, F.row(i))
  66. # Plot the generated mesh
  67. viewer = igl.glfw.Viewer()
  68. viewer.data().set_mesh(V, FF[facetwise] if is_showing_reoriented else F)
  69. viewer.data().set_face_based(True)
  70. scramble_colors()
  71. viewer.callback_key_down = key_down
  72. viewer.launch()