OneAtATimeIterator.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Scene.Iterator import Iterator
  4. from UM.Scene.SceneNode import SceneNode
  5. from functools import cmp_to_key
  6. ## Iterator that returns a list of nodes in the order that they need to be printed
  7. # If there is no solution an empty list is returned.
  8. # Take note that the list of nodes can have children (that may or may not contain mesh data)
  9. class OneAtATimeIterator(Iterator.Iterator):
  10. def __init__(self, scene_node):
  11. super().__init__(scene_node) # Call super to make multiple inheritence work.
  12. self._hit_map = [[]]
  13. self._original_node_list = []
  14. def _fillStack(self):
  15. node_list = []
  16. for node in self._scene_node.getChildren():
  17. if not issubclass(type(node), SceneNode):
  18. continue
  19. if node.callDecoration("getConvexHull"):
  20. node_list.append(node)
  21. if len(node_list) < 2:
  22. self._node_stack = node_list[:]
  23. return
  24. # Copy the list
  25. self._original_node_list = node_list[:]
  26. ## Initialise the hit map (pre-compute all hits between all objects)
  27. self._hit_map = [[self._checkHit(i,j) for i in node_list] for j in node_list]
  28. # Check if we have to files that block eachother. If this is the case, there is no solution!
  29. for a in range(0,len(node_list)):
  30. for b in range(0,len(node_list)):
  31. if a != b and self._hit_map[a][b] and self._hit_map[b][a]:
  32. return
  33. # Sort the original list so that items that block the most other objects are at the beginning.
  34. # This does not decrease the worst case running time, but should improve it in most cases.
  35. sorted(node_list, key = cmp_to_key(self._calculateScore))
  36. todo_node_list = [_ObjectOrder([], node_list)]
  37. while len(todo_node_list) > 0:
  38. current = todo_node_list.pop()
  39. for node in current.todo:
  40. # Check if the object can be placed with what we have and still allows for a solution in the future
  41. if not self._checkHitMultiple(node, current.order) and not self._checkBlockMultiple(node, current.todo):
  42. # We found a possible result. Create new todo & order list.
  43. new_todo_list = current.todo[:]
  44. new_todo_list.remove(node)
  45. new_order = current.order[:] + [node]
  46. if len(new_todo_list) == 0:
  47. # We have no more nodes to check, so quit looking.
  48. todo_node_list = None
  49. self._node_stack = new_order
  50. return
  51. todo_node_list.append(_ObjectOrder(new_order, new_todo_list))
  52. self._node_stack = [] #No result found!
  53. # Check if first object can be printed before the provided list (using the hit map)
  54. def _checkHitMultiple(self, node, other_nodes):
  55. node_index = self._original_node_list.index(node)
  56. for other_node in other_nodes:
  57. other_node_index = self._original_node_list.index(other_node)
  58. if self._hit_map[node_index][other_node_index]:
  59. return True
  60. return False
  61. def _checkBlockMultiple(self, node, other_nodes):
  62. node_index = self._original_node_list.index(node)
  63. for other_node in other_nodes:
  64. other_node_index = self._original_node_list.index(other_node)
  65. if self._hit_map[other_node_index][node_index] and node_index != other_node_index:
  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("getConvexHullHeadFull"))
  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