609_Boolean.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if key == ord('.'):
  26. boolean_type = boolean_types[(boolean_types.index(boolean_type) + 1) % (len(boolean_types))]
  27. elif key == ord(','):
  28. boolean_type = boolean_types[(boolean_types.index(boolean_type) + len(boolean_types) - 1) % len(boolean_types)]
  29. elif key == ord('['):
  30. viewer.core.camera_dnear -= 0.1
  31. elif key == ord(']'):
  32. viewer.core.camera_dnear += 0.1
  33. else:
  34. return False
  35. update(viewer)
  36. return False
  37. if __name__ == "__main__":
  38. VA = igl.eigen.MatrixXd()
  39. FA = igl.eigen.MatrixXi()
  40. VB = igl.eigen.MatrixXd()
  41. FB = igl.eigen.MatrixXi()
  42. VC = igl.eigen.MatrixXd()
  43. FC = igl.eigen.MatrixXi()
  44. J = igl.eigen.MatrixXi()
  45. Red = igl.eigen.MatrixXd([[1, 0, 0]])
  46. Green = igl.eigen.MatrixXd([[0, 1, 0]])
  47. # Load meshes in OFF format
  48. igl.readOFF(TUTORIAL_SHARED_PATH + "cheburashka.off", VA, FA)
  49. igl.readOFF(TUTORIAL_SHARED_PATH + "decimated-knight.off", VB, FB)
  50. boolean_type = igl.MESH_BOOLEAN_TYPE_UNION
  51. viewer = igl.viewer.Viewer()
  52. update(viewer)
  53. print(
  54. "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")
  55. viewer.core.show_lines = True
  56. viewer.callback_key_down = key_down
  57. viewer.core.camera_dnear = 3.9
  58. viewer.launch()