706_FacetOrientation.py 2.9 KB

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