OneAtATimeIterator.py 4.5 KB

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