OneAtATimeIterator.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Scene.Iterator import Iterator
  4. from UM.Scene.SceneNode import SceneNode
  5. from functools import cmp_to_key
  6. from UM.Application import Application
  7. ## Iterator that returns a list of nodes in the order that they need to be printed
  8. # If there is no solution an empty list is returned.
  9. # Take note that the list of nodes can have children (that may or may not contain mesh data)
  10. class OneAtATimeIterator(Iterator.Iterator):
  11. def __init__(self, scene_node):
  12. super().__init__(scene_node) # Call super to make multiple inheritence work.
  13. self._hit_map = [[]]
  14. self._original_node_list = []
  15. def _fillStack(self):
  16. node_list = []
  17. for node in self._scene_node.getChildren():
  18. if not type(node) is SceneNode:
  19. continue
  20. if node.getBoundingBox().height > Application.getInstance().getMachineManager().getActiveProfile().getSettingValue("gantry_height"):
  21. return
  22. if node.callDecoration("getConvexHull"):
  23. node_list.append(node)
  24. if len(node_list) < 2:
  25. self._node_stack = node_list[:]
  26. return
  27. self._original_node_list = node_list[:]
  28. ## Initialise the hit map (pre-compute all hits between all objects)
  29. self._hit_map = [[self._checkHit(j,i) for i in node_list] for j in node_list]
  30. # Check if we have to files that block eachother. If this is the case, there is no solution!
  31. for a in range(0,len(node_list)):
  32. for b in range(0,len(node_list)):
  33. if a != b and self._hit_map[a][b] and self._hit_map[b][a]:
  34. return
  35. # Sort the original list so that items that block the most other objects are at the beginning.
  36. # This does not decrease the worst case running time, but should improve it in most cases.
  37. sorted(node_list, key = cmp_to_key(self._calculateScore))
  38. todo_node_list = [_ObjectOrder([], node_list)]
  39. while len(todo_node_list) > 0:
  40. current = todo_node_list.pop()
  41. for node in current.todo:
  42. # Check if the object can be placed with what we have and still allows for a solution in the future
  43. if not self._checkHitMultiple(node, current.order) and not self._checkBlockMultiple(node, current.todo):
  44. # We found a possible result. Create new todo & order list.
  45. new_todo_list = current.todo[:]
  46. new_todo_list.remove(node)
  47. new_order = current.order[:] + [node]
  48. if len(new_todo_list) == 0:
  49. # We have no more nodes to check, so quit looking.
  50. todo_node_list = None
  51. self._node_stack = new_order
  52. return
  53. todo_node_list.append(_ObjectOrder(new_order, new_todo_list))
  54. self._node_stack = [] #No result found!
  55. # Check if first object can be printed before the provided list (using the hit map)
  56. def _checkHitMultiple(self, node, other_nodes):
  57. node_index = self._original_node_list.index(node)
  58. for other_node in other_nodes:
  59. if self._hit_map[node_index][self._original_node_list.index(other_node)]:
  60. return True
  61. return False
  62. def _checkBlockMultiple(self, node, other_nodes):
  63. node_index = self._original_node_list.index(node)
  64. for other_node in other_nodes:
  65. if self._hit_map[self._original_node_list.index(other_node)][node_index] and node_index != self._original_node_list.index(other_node):
  66. return True
  67. return False
  68. ## Calculate score simply sums the number of other objects it 'blocks'
  69. def _calculateScore(self, a, b):
  70. score_a = sum(self._hit_map[self._original_node_list.index(a)])
  71. score_b = sum(self._hit_map[self._original_node_list.index(b)])
  72. return score_a - score_b
  73. # Checks if A can be printed before B
  74. def _checkHit(self, a, b):
  75. if a == b:
  76. return False
  77. overlap = a.callDecoration("getConvexHullBoundary").intersectsPolygon(b.callDecoration("getConvexHullHead"))
  78. if overlap:
  79. return True
  80. else:
  81. return False
  82. ## Internal object used to keep track of a possible order in which to print objects.
  83. class _ObjectOrder():
  84. def __init__(self, order, todo):
  85. """
  86. :param order: List of indexes in which to print objects, ordered by printing order.
  87. :param todo: List of indexes which are not yet inserted into the order list.
  88. """
  89. self.order = order
  90. self.todo = todo