Arrange.py 10.0 KB

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