Arrange.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  2. from UM.Logger import Logger
  3. from UM.Math.Vector import Vector
  4. from cura.Arranging.ShapeArray import ShapeArray
  5. from cura.Scene import ZOffsetDecorator
  6. from collections import namedtuple
  7. import numpy
  8. import copy
  9. ## Return object for bestSpot
  10. LocationSuggestion = namedtuple("LocationSuggestion", ["x", "y", "penalty_points", "priority"])
  11. ## The Arrange classed is used together with ShapeArray. Use it to find
  12. # good locations for objects that you try to put on a build place.
  13. # Different priority schemes can be defined so it alters the behavior while using
  14. # the same logic.
  15. #
  16. # Note: Make sure the scale is the same between ShapeArray objects and the Arrange instance.
  17. class Arrange:
  18. build_volume = None
  19. def __init__(self, x, y, offset_x, offset_y, scale= 0.5):
  20. self._scale = scale # convert input coordinates to arrange coordinates
  21. world_x, world_y = int(x * self._scale), int(y * self._scale)
  22. self._shape = (world_y, world_x)
  23. self._priority = numpy.zeros((world_y, world_x), dtype=numpy.int32) # beware: these are indexed (y, x)
  24. self._priority_unique_values = []
  25. self._occupied = numpy.zeros((world_y, world_x), dtype=numpy.int32) # beware: these are indexed (y, x)
  26. self._offset_x = int(offset_x * self._scale)
  27. self._offset_y = int(offset_y * self._scale)
  28. self._last_priority = 0
  29. self._is_empty = True
  30. ## Helper to create an Arranger instance
  31. #
  32. # Either fill in scene_root and create will find all sliceable nodes by itself,
  33. # or use fixed_nodes to provide the nodes yourself.
  34. # \param scene_root Root for finding all scene nodes
  35. # \param fixed_nodes Scene nodes to be placed
  36. @classmethod
  37. def create(cls, scene_root = None, fixed_nodes = None, scale = 0.5, x = 350, y = 250):
  38. arranger = Arrange(x, y, x // 2, y // 2, scale = scale)
  39. arranger.centerFirst()
  40. if fixed_nodes is None:
  41. fixed_nodes = []
  42. for node_ in DepthFirstIterator(scene_root):
  43. # Only count sliceable objects
  44. if node_.callDecoration("isSliceable"):
  45. fixed_nodes.append(node_)
  46. # Place all objects fixed nodes
  47. for fixed_node in fixed_nodes:
  48. vertices = fixed_node.callDecoration("getConvexHull")
  49. if not vertices:
  50. continue
  51. points = copy.deepcopy(vertices._points)
  52. shape_arr = ShapeArray.fromPolygon(points, scale = scale)
  53. arranger.place(0, 0, shape_arr)
  54. # If a build volume was set, add the disallowed areas
  55. if Arrange.build_volume:
  56. disallowed_areas = Arrange.build_volume.getDisallowedAreasNoBrim()
  57. for area in disallowed_areas:
  58. points = copy.deepcopy(area._points)
  59. shape_arr = ShapeArray.fromPolygon(points, scale = scale)
  60. arranger.place(0, 0, shape_arr, update_empty = False)
  61. return arranger
  62. ## This resets the optimization for finding location based on size
  63. def resetLastPriority(self):
  64. self._last_priority = 0
  65. ## Find placement for a node (using offset shape) and place it (using hull shape)
  66. # return the nodes that should be placed
  67. # \param node
  68. # \param offset_shape_arr ShapeArray with offset, used to find location
  69. # \param hull_shape_arr ShapeArray without offset, for placing the shape
  70. def findNodePlacement(self, node, offset_shape_arr, hull_shape_arr, step = 1):
  71. new_node = copy.deepcopy(node)
  72. best_spot = self.bestSpot(
  73. offset_shape_arr, start_prio = self._last_priority, step = step)
  74. x, y = best_spot.x, best_spot.y
  75. # Save the last priority.
  76. self._last_priority = best_spot.priority
  77. # Ensure that the object is above the build platform
  78. new_node.removeDecorator(ZOffsetDecorator.ZOffsetDecorator)
  79. if new_node.getBoundingBox():
  80. center_y = new_node.getWorldPosition().y - new_node.getBoundingBox().bottom
  81. else:
  82. center_y = 0
  83. if x is not None: # We could find a place
  84. new_node.setPosition(Vector(x, center_y, y))
  85. found_spot = True
  86. self.place(x, y, hull_shape_arr) # place the object in arranger
  87. else:
  88. Logger.log("d", "Could not find spot!"),
  89. found_spot = False
  90. new_node.setPosition(Vector(200, center_y, 100))
  91. return new_node, found_spot
  92. ## Fill priority, center is best. Lower value is better
  93. # This is a strategy for the arranger.
  94. def centerFirst(self):
  95. # Square distance: creates a more round shape
  96. self._priority = numpy.fromfunction(
  97. lambda j, i: (self._offset_x - i) ** 2 + (self._offset_y - j) ** 2, self._shape, dtype=numpy.int32)
  98. self._priority_unique_values = numpy.unique(self._priority)
  99. self._priority_unique_values.sort()
  100. ## Fill priority, back is best. Lower value is better
  101. # This is a strategy for the arranger.
  102. def backFirst(self):
  103. self._priority = numpy.fromfunction(
  104. lambda j, i: 10 * j + abs(self._offset_x - i), self._shape, dtype=numpy.int32)
  105. self._priority_unique_values = numpy.unique(self._priority)
  106. self._priority_unique_values.sort()
  107. ## Return the amount of "penalty points" for polygon, which is the sum of priority
  108. # None if occupied
  109. # \param x x-coordinate to check shape
  110. # \param y y-coordinate
  111. # \param shape_arr the ShapeArray object to place
  112. def checkShape(self, x, y, shape_arr):
  113. x = int(self._scale * x)
  114. y = int(self._scale * y)
  115. offset_x = x + self._offset_x + shape_arr.offset_x
  116. offset_y = y + self._offset_y + shape_arr.offset_y
  117. if offset_x < 0 or offset_y < 0:
  118. return None # out of bounds in self._occupied
  119. occupied_x_max = offset_x + shape_arr.arr.shape[1]
  120. occupied_y_max = offset_y + shape_arr.arr.shape[0]
  121. if occupied_x_max > self._occupied.shape[1] + 1 or occupied_y_max > self._occupied.shape[0] + 1:
  122. return None # out of bounds in self._occupied
  123. occupied_slice = self._occupied[
  124. offset_y:occupied_y_max,
  125. offset_x:occupied_x_max]
  126. try:
  127. if numpy.any(occupied_slice[numpy.where(shape_arr.arr == 1)]):
  128. return None
  129. except IndexError: # out of bounds if you try to place an object outside
  130. return None
  131. prio_slice = self._priority[
  132. offset_y:offset_y + shape_arr.arr.shape[0],
  133. offset_x:offset_x + shape_arr.arr.shape[1]]
  134. return numpy.sum(prio_slice[numpy.where(shape_arr.arr == 1)])
  135. ## Find "best" spot for ShapeArray
  136. # Return namedtuple with properties x, y, penalty_points, priority.
  137. # \param shape_arr ShapeArray
  138. # \param start_prio Start with this priority value (and skip the ones before)
  139. # \param step Slicing value, higher = more skips = faster but less accurate
  140. def bestSpot(self, shape_arr, start_prio = 0, step = 1):
  141. start_idx_list = numpy.where(self._priority_unique_values == start_prio)
  142. if start_idx_list:
  143. start_idx = start_idx_list[0][0]
  144. else:
  145. start_idx = 0
  146. for priority in self._priority_unique_values[start_idx::step]:
  147. tryout_idx = numpy.where(self._priority == priority)
  148. for idx in range(len(tryout_idx[0])):
  149. x = tryout_idx[1][idx]
  150. y = tryout_idx[0][idx]
  151. projected_x = int((x - self._offset_x) / self._scale)
  152. projected_y = int((y - self._offset_y) / self._scale)
  153. penalty_points = self.checkShape(projected_x, projected_y, shape_arr)
  154. if penalty_points is not None:
  155. return LocationSuggestion(x = projected_x, y = projected_y, penalty_points = penalty_points, priority = priority)
  156. return LocationSuggestion(x = None, y = None, penalty_points = None, priority = priority) # No suitable location found :-(
  157. ## Place the object.
  158. # Marks the locations in self._occupied and self._priority
  159. # \param x x-coordinate
  160. # \param y y-coordinate
  161. # \param shape_arr ShapeArray object
  162. # \param update_empty updates the _is_empty, used when adding disallowed areas
  163. def place(self, x, y, shape_arr, update_empty = True):
  164. x = int(self._scale * x)
  165. y = int(self._scale * y)
  166. offset_x = x + self._offset_x + shape_arr.offset_x
  167. offset_y = y + self._offset_y + shape_arr.offset_y
  168. shape_y, shape_x = self._occupied.shape
  169. min_x = min(max(offset_x, 0), shape_x - 1)
  170. min_y = min(max(offset_y, 0), shape_y - 1)
  171. max_x = min(max(offset_x + shape_arr.arr.shape[1], 0), shape_x - 1)
  172. max_y = min(max(offset_y + shape_arr.arr.shape[0], 0), shape_y - 1)
  173. occupied_slice = self._occupied[min_y:max_y, min_x:max_x]
  174. # we use a slice of shape because it can be out of bounds
  175. new_occupied = numpy.where(shape_arr.arr[
  176. min_y - offset_y:max_y - offset_y, min_x - offset_x:max_x - offset_x] == 1)
  177. if update_empty and new_occupied:
  178. self._is_empty = False
  179. occupied_slice[new_occupied] = 1
  180. # Set priority to low (= high number), so it won't get picked at trying out.
  181. prio_slice = self._priority[min_y:max_y, min_x:max_x]
  182. prio_slice[new_occupied] = 999
  183. # If you want to see how the rasterized arranger build plate looks like, uncomment this code
  184. # numpy.set_printoptions(linewidth=500, edgeitems=200)
  185. # print(self._occupied.shape)
  186. # print(self._occupied)
  187. @property
  188. def isEmpty(self):
  189. return self._is_empty