PlatformPhysics.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from PyQt5.QtCore import QTimer
  4. from UM.Scene.SceneNode import SceneNode
  5. from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
  6. from UM.Math.Vector import Vector
  7. from UM.Math.AxisAlignedBox import AxisAlignedBox
  8. from UM.Scene.Selection import Selection
  9. from UM.Preferences import Preferences
  10. from cura.ConvexHullDecorator import ConvexHullDecorator
  11. from . import PlatformPhysicsOperation
  12. from . import ZOffsetDecorator
  13. class PlatformPhysics:
  14. def __init__(self, controller, volume):
  15. super().__init__()
  16. self._controller = controller
  17. self._controller.getScene().sceneChanged.connect(self._onSceneChanged)
  18. self._controller.toolOperationStarted.connect(self._onToolOperationStarted)
  19. self._controller.toolOperationStopped.connect(self._onToolOperationStopped)
  20. self._build_volume = volume
  21. self._enabled = True
  22. self._change_timer = QTimer()
  23. self._change_timer.setInterval(100)
  24. self._change_timer.setSingleShot(True)
  25. self._change_timer.timeout.connect(self._onChangeTimerFinished)
  26. Preferences.getInstance().addPreference("physics/automatic_push_free", True)
  27. def _onSceneChanged(self, source):
  28. self._change_timer.start()
  29. def _onChangeTimerFinished(self):
  30. if not self._enabled:
  31. return
  32. root = self._controller.getScene().getRoot()
  33. for node in BreadthFirstIterator(root):
  34. if node is root or type(node) is not SceneNode or node.getBoundingBox() is None:
  35. continue
  36. bbox = node.getBoundingBox()
  37. # Ignore intersections with the bottom
  38. build_volume_bounding_box = self._build_volume.getBoundingBox().set(bottom=-9001)
  39. node._outside_buildarea = False
  40. # Mark the node as outside the build volume if the bounding box test fails.
  41. if build_volume_bounding_box.intersectsBox(bbox) != AxisAlignedBox.IntersectionResult.FullIntersection:
  42. node._outside_buildarea = True
  43. # Move it downwards if bottom is above platform
  44. move_vector = Vector()
  45. if not (node.getParent() and node.getParent().callDecoration("isGroup")): #If an object is grouped, don't move it down
  46. z_offset = node.callDecoration("getZOffset") if node.getDecorator(ZOffsetDecorator.ZOffsetDecorator) else 0
  47. if bbox.bottom > 0:
  48. move_vector = move_vector.set(y=-bbox.bottom + z_offset)
  49. elif bbox.bottom < z_offset:
  50. move_vector = move_vector.set(y=(-bbox.bottom) - z_offset)
  51. # If there is no convex hull for the node, start calculating it and continue.
  52. if not node.getDecorator(ConvexHullDecorator):
  53. node.addDecorator(ConvexHullDecorator())
  54. node.callDecoration("recomputeConvexHull")
  55. if Preferences.getInstance().getValue("physics/automatic_push_free"):
  56. # Check for collisions between convex hulls
  57. for other_node in BreadthFirstIterator(root):
  58. # Ignore root, ourselves and anything that is not a normal SceneNode.
  59. if other_node is root or type(other_node) is not SceneNode or other_node is node:
  60. continue
  61. # Ignore collisions of a group with it's own children
  62. if other_node in node.getAllChildren() or node in other_node.getAllChildren():
  63. continue
  64. # Ignore collisions within a group
  65. if other_node.getParent().callDecoration("isGroup") is not None or node.getParent().callDecoration("isGroup") is not None:
  66. continue
  67. # Ignore nodes that do not have the right properties set.
  68. if not other_node.callDecoration("getConvexHull") or not other_node.getBoundingBox():
  69. continue
  70. # Get the overlap distance for both convex hulls. If this returns None, there is no intersection.
  71. head_hull = node.callDecoration("getConvexHullHead")
  72. if head_hull:
  73. overlap = head_hull.intersectsPolygon(other_node.callDecoration("getConvexHull"))
  74. if not overlap:
  75. other_head_hull = other_node.callDecoration("getConvexHullHead")
  76. if other_head_hull:
  77. overlap = node.callDecoration("getConvexHull").intersectsPolygon(other_head_hull)
  78. else:
  79. own_convex_hull = node.callDecoration("getConvexHull")
  80. other_convex_hull = other_node.callDecoration("getConvexHull")
  81. if own_convex_hull and other_convex_hull:
  82. overlap = own_convex_hull.intersectsPolygon(other_convex_hull)
  83. else:
  84. # This can happen in some cases if the object is not yet done with being loaded.
  85. # Simply waiting for the next tick seems to resolve this correctly.
  86. overlap = None
  87. if overlap is None:
  88. continue
  89. move_vector = move_vector.set(x=overlap[0] * 1.1, z=overlap[1] * 1.1)
  90. convex_hull = node.callDecoration("getConvexHull")
  91. if convex_hull:
  92. if not convex_hull.isValid():
  93. return
  94. # Check for collisions between disallowed areas and the object
  95. for area in self._build_volume.getDisallowedAreas():
  96. overlap = convex_hull.intersectsPolygon(area)
  97. if overlap is None:
  98. continue
  99. node._outside_buildarea = True
  100. if not Vector.Null.equals(move_vector, epsilon=1e-5):
  101. op = PlatformPhysicsOperation.PlatformPhysicsOperation(node, move_vector)
  102. op.push()
  103. def _onToolOperationStarted(self, tool):
  104. self._enabled = False
  105. def _onToolOperationStopped(self, tool):
  106. if tool.getPluginId() == "TranslateTool":
  107. for node in Selection.getAllSelectedObjects():
  108. if node.getBoundingBox().bottom < 0:
  109. if not node.getDecorator(ZOffsetDecorator.ZOffsetDecorator):
  110. node.addDecorator(ZOffsetDecorator.ZOffsetDecorator())
  111. node.callDecoration("setZOffset", node.getBoundingBox().bottom)
  112. else:
  113. if node.getDecorator(ZOffsetDecorator.ZOffsetDecorator):
  114. node.removeDecorator(ZOffsetDecorator.ZOffsetDecorator)
  115. self._enabled = True
  116. self._onChangeTimerFinished()