609_Boolean.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 = ["cgal", "glfw"]
  16. check_dependencies(dependencies)
  17. 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"}
  18. boolean_types = list(boolean_type_names.keys())
  19. def update(viewer):
  20. print("Calculating A %s B..." % boolean_type_names[boolean_type])
  21. igl.cgal.mesh_boolean(VA, FA, VB, FB, boolean_type, VC, FC, J)
  22. C = igl.eigen.MatrixXd(FC.rows(), 3)
  23. for f in range(C.rows()):
  24. if J[f] < FA.rows():
  25. C.setRow(f, Red)
  26. else:
  27. C.setRow(f, Green)
  28. viewer.data().clear()
  29. viewer.data().set_mesh(VC, FC)
  30. viewer.data().set_colors(C)
  31. print("Done.")
  32. def key_down(viewer, key, modifier):
  33. global boolean_type
  34. if key == ord('.'):
  35. boolean_type = boolean_types[(boolean_types.index(boolean_type) + 1) % (len(boolean_types))]
  36. elif key == ord(','):
  37. boolean_type = boolean_types[(boolean_types.index(boolean_type) + len(boolean_types) - 1) % len(boolean_types)]
  38. elif key == ord('['):
  39. viewer.core.camera_dnear -= 0.1
  40. elif key == ord(']'):
  41. viewer.core.camera_dnear += 0.1
  42. else:
  43. return False
  44. update(viewer)
  45. return False
  46. if __name__ == "__main__":
  47. VA = igl.eigen.MatrixXd()
  48. FA = igl.eigen.MatrixXi()
  49. VB = igl.eigen.MatrixXd()
  50. FB = igl.eigen.MatrixXi()
  51. VC = igl.eigen.MatrixXd()
  52. FC = igl.eigen.MatrixXi()
  53. J = igl.eigen.MatrixXi()
  54. Red = igl.eigen.MatrixXd([[1, 0, 0]])
  55. Green = igl.eigen.MatrixXd([[0, 1, 0]])
  56. # Load meshes in OFF format
  57. igl.readOFF(TUTORIAL_SHARED_PATH + "cheburashka.off", VA, FA)
  58. igl.readOFF(TUTORIAL_SHARED_PATH + "decimated-knight.off", VB, FB)
  59. boolean_type = igl.MESH_BOOLEAN_TYPE_UNION
  60. viewer = igl.glfw.Viewer()
  61. update(viewer)
  62. print(
  63. "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")
  64. viewer.data().show_lines = True
  65. viewer.callback_key_down = key_down
  66. viewer.core.camera_dnear = 3.9
  67. viewer.launch()