609_Boolean.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import sys, os
  2. # Add the igl library to the modules search path
  3. sys.path.insert(0, os.getcwd() + "/../")
  4. import pyigl as igl
  5. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  6. dependencies = ["cgal", "viewer"]
  7. check_dependencies(dependencies)
  8. boolean_type_names = {igl.MESH_BOOLEAN_TYPE_UNION: "Union", igl.MESH_BOOLEAN_TYPE_INTERSECT: "Intersect", igl.MESH_BOOLEAN_TYPE_MINUS: "Minus", igl.MESH_BOOLEAN_TYPE_XOR: "XOR", igl.MESH_BOOLEAN_TYPE_RESOLVE: "Resolve"}
  9. boolean_types = list(boolean_type_names.keys())
  10. def update(viewer):
  11. print("Calculating A %s B..." % boolean_type_names[boolean_type])
  12. igl.cgal.mesh_boolean(VA, FA, VB, FB, boolean_type, VC, FC, J)
  13. C = igl.eigen.MatrixXd(FC.rows(), 3)
  14. for f in range(C.rows()):
  15. if J[f] < FA.rows():
  16. C.setRow(f, Red)
  17. else:
  18. C.setRow(f, Green)
  19. viewer.data.clear()
  20. viewer.data.set_mesh(VC, FC)
  21. viewer.data.set_colors(C)
  22. print("Done.")
  23. def key_down(viewer, key, modifier):
  24. global boolean_type
  25. # print(boolean_types)
  26. if key == ord('.'):
  27. boolean_type = boolean_types[(boolean_types.index(boolean_type) + 1) % (len(boolean_types))]
  28. elif key == ord(','):
  29. boolean_type = boolean_types[(boolean_types.index(boolean_type) + len(boolean_types) - 1) % len(boolean_types)]
  30. elif key == ord('['):
  31. viewer.core.camera_dnear -= 0.1
  32. elif key == ord(']'):
  33. viewer.core.camera_dnear += 0.1
  34. else:
  35. return False
  36. #igl.cgal.mesh_boolean(VA, FA, VB, FB, boolean_type, VC, FC)
  37. update(viewer)
  38. return False
  39. if __name__ == "__main__":
  40. # Mesh with per-face color
  41. VA = igl.eigen.MatrixXd()
  42. FA = igl.eigen.MatrixXi()
  43. VB = igl.eigen.MatrixXd()
  44. FB = igl.eigen.MatrixXi()
  45. VC = igl.eigen.MatrixXd()
  46. FC = igl.eigen.MatrixXi()
  47. J = igl.eigen.MatrixXi()
  48. Red = igl.eigen.MatrixXd([[1, 0, 0]])
  49. Green = igl.eigen.MatrixXd([[0, 1, 0]])
  50. # Load meshes in OFF format
  51. igl.readOFF(TUTORIAL_SHARED_PATH + "cheburashka.off", VA, FA)
  52. igl.readOFF(TUTORIAL_SHARED_PATH + "decimated-knight.off", VB, FB)
  53. boolean_type = igl.MESH_BOOLEAN_TYPE_UNION
  54. viewer = igl.viewer.Viewer()
  55. update(viewer)
  56. print(
  57. "Usage: Press '.' to switch to next boolean operation type. \nPress ',' to switch to previous boolean operation type. \nPress ']' to push near cutting plane away from camera. \nPress '[' to pull near cutting plane closer to camera. \nHint: investigate _inside_ the model to see orientation changes. \n")
  58. viewer.core.show_lines = True
  59. viewer.callback_key_down = key_down
  60. viewer.core.camera_dnear = 3.9
  61. viewer.launch()