609_Boolean.py 2.9 KB

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