TestArrange.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import numpy
  4. from cura.Arranging.Arrange import Arrange
  5. from cura.Arranging.ShapeArray import ShapeArray
  6. ## Triangle of area 12
  7. def gimmeTriangle():
  8. return numpy.array([[-3, 1], [3, 1], [0, -3]], dtype=numpy.int32)
  9. ## Boring square
  10. def gimmeSquare():
  11. return numpy.array([[-2, -2], [2, -2], [2, 2], [-2, 2]], dtype=numpy.int32)
  12. ## Triangle of area 12
  13. def gimmeShapeArray(scale = 1.0):
  14. vertices = gimmeTriangle()
  15. shape_arr = ShapeArray.fromPolygon(vertices, scale = scale)
  16. return shape_arr
  17. ## Boring square
  18. def gimmeShapeArraySquare(scale = 1.0):
  19. vertices = gimmeSquare()
  20. shape_arr = ShapeArray.fromPolygon(vertices, scale = scale)
  21. return shape_arr
  22. ## Smoke test for Arrange
  23. def test_smoke_arrange():
  24. Arrange.create(fixed_nodes = [])
  25. ## Smoke test for ShapeArray
  26. def test_smoke_ShapeArray():
  27. gimmeShapeArray()
  28. ## Test ShapeArray
  29. def test_ShapeArray():
  30. scale = 1
  31. ar = Arrange(16, 16, 8, 8, scale = scale)
  32. ar.centerFirst()
  33. shape_arr = gimmeShapeArray(scale)
  34. count = len(numpy.where(shape_arr.arr == 1)[0])
  35. assert count >= 10 # should approach 12
  36. ## Test ShapeArray with scaling
  37. def test_ShapeArray_scaling():
  38. scale = 2
  39. ar = Arrange(16, 16, 8, 8, scale = scale)
  40. ar.centerFirst()
  41. shape_arr = gimmeShapeArray(scale)
  42. count = len(numpy.where(shape_arr.arr == 1)[0])
  43. assert count >= 40 # should approach 2*2*12 = 48
  44. ## Test ShapeArray with scaling
  45. def test_ShapeArray_scaling2():
  46. scale = 0.5
  47. ar = Arrange(16, 16, 8, 8, scale = scale)
  48. ar.centerFirst()
  49. shape_arr = gimmeShapeArray(scale)
  50. count = len(numpy.where(shape_arr.arr == 1)[0])
  51. assert count >= 1 # should approach 3, but it can be inaccurate due to pixel rounding
  52. ## Test centerFirst
  53. def test_centerFirst():
  54. ar = Arrange(300, 300, 150, 150, scale = 1)
  55. ar.centerFirst()
  56. assert ar._priority[150][150] < ar._priority[170][150]
  57. assert ar._priority[150][150] < ar._priority[150][170]
  58. assert ar._priority[150][150] < ar._priority[170][170]
  59. assert ar._priority[150][150] < ar._priority[130][150]
  60. assert ar._priority[150][150] < ar._priority[150][130]
  61. assert ar._priority[150][150] < ar._priority[130][130]
  62. ## Test centerFirst
  63. def test_centerFirst_rectangular():
  64. ar = Arrange(400, 300, 200, 150, scale = 1)
  65. ar.centerFirst()
  66. assert ar._priority[150][200] < ar._priority[150][220]
  67. assert ar._priority[150][200] < ar._priority[170][200]
  68. assert ar._priority[150][200] < ar._priority[170][220]
  69. assert ar._priority[150][200] < ar._priority[180][150]
  70. assert ar._priority[150][200] < ar._priority[130][200]
  71. assert ar._priority[150][200] < ar._priority[130][180]
  72. ## Test centerFirst
  73. def test_centerFirst_rectangular2():
  74. ar = Arrange(10, 20, 5, 10, scale = 1)
  75. ar.centerFirst()
  76. assert ar._priority[10][5] < ar._priority[10][7]
  77. ## Test backFirst
  78. def test_backFirst():
  79. ar = Arrange(300, 300, 150, 150, scale = 1)
  80. ar.backFirst()
  81. assert ar._priority[150][150] < ar._priority[170][150]
  82. assert ar._priority[150][150] < ar._priority[170][170]
  83. assert ar._priority[150][150] > ar._priority[130][150]
  84. assert ar._priority[150][150] > ar._priority[130][130]
  85. ## See if the result of bestSpot has the correct form
  86. def test_smoke_bestSpot():
  87. ar = Arrange(30, 30, 15, 15, scale = 1)
  88. ar.centerFirst()
  89. shape_arr = gimmeShapeArray()
  90. best_spot = ar.bestSpot(shape_arr)
  91. assert hasattr(best_spot, "x")
  92. assert hasattr(best_spot, "y")
  93. assert hasattr(best_spot, "penalty_points")
  94. assert hasattr(best_spot, "priority")
  95. ## Real life test
  96. def test_bestSpot():
  97. ar = Arrange(16, 16, 8, 8, scale = 1)
  98. ar.centerFirst()
  99. shape_arr = gimmeShapeArray()
  100. best_spot = ar.bestSpot(shape_arr)
  101. assert best_spot.x == 0
  102. assert best_spot.y == 0
  103. ar.place(best_spot.x, best_spot.y, shape_arr)
  104. # Place object a second time
  105. best_spot = ar.bestSpot(shape_arr)
  106. assert best_spot.x is not None # we found a location
  107. assert best_spot.x != 0 or best_spot.y != 0 # it can't be on the same location
  108. ar.place(best_spot.x, best_spot.y, shape_arr)
  109. ## Real life test rectangular build plate
  110. def test_bestSpot_rectangular_build_plate():
  111. ar = Arrange(16, 40, 8, 20, scale = 1)
  112. ar.centerFirst()
  113. shape_arr = gimmeShapeArray()
  114. best_spot = ar.bestSpot(shape_arr)
  115. ar.place(best_spot.x, best_spot.y, shape_arr)
  116. assert best_spot.x == 0
  117. assert best_spot.y == 0
  118. # Place object a second time
  119. best_spot2 = ar.bestSpot(shape_arr)
  120. assert best_spot2.x is not None # we found a location
  121. assert best_spot2.x != 0 or best_spot2.y != 0 # it can't be on the same location
  122. ar.place(best_spot2.x, best_spot2.y, shape_arr)
  123. # Place object a 3rd time
  124. best_spot3 = ar.bestSpot(shape_arr)
  125. assert best_spot3.x is not None # we found a location
  126. assert best_spot3.x != best_spot.x or best_spot3.y != best_spot.y # it can't be on the same location
  127. assert best_spot3.x != best_spot2.x or best_spot3.y != best_spot2.y # it can't be on the same location
  128. ar.place(best_spot3.x, best_spot3.y, shape_arr)
  129. best_spot_x = ar.bestSpot(shape_arr)
  130. ar.place(best_spot_x.x, best_spot_x.y, shape_arr)
  131. best_spot_x = ar.bestSpot(shape_arr)
  132. ar.place(best_spot_x.x, best_spot_x.y, shape_arr)
  133. best_spot_x = ar.bestSpot(shape_arr)
  134. ar.place(best_spot_x.x, best_spot_x.y, shape_arr)
  135. ## Real life test
  136. def test_bestSpot_scale():
  137. scale = 0.5
  138. ar = Arrange(16, 16, 8, 8, scale = scale)
  139. ar.centerFirst()
  140. shape_arr = gimmeShapeArray(scale)
  141. best_spot = ar.bestSpot(shape_arr)
  142. assert best_spot.x == 0
  143. assert best_spot.y == 0
  144. ar.place(best_spot.x, best_spot.y, shape_arr)
  145. # Place object a second time
  146. best_spot = ar.bestSpot(shape_arr)
  147. assert best_spot.x is not None # we found a location
  148. assert best_spot.x != 0 or best_spot.y != 0 # it can't be on the same location
  149. ar.place(best_spot.x, best_spot.y, shape_arr)
  150. ## Real life test
  151. def test_bestSpot_scale_rectangular():
  152. scale = 0.5
  153. ar = Arrange(16, 40, 8, 20, scale = scale)
  154. ar.centerFirst()
  155. shape_arr = gimmeShapeArray(scale)
  156. shape_arr_square = gimmeShapeArraySquare(scale)
  157. best_spot = ar.bestSpot(shape_arr_square)
  158. assert best_spot.x == 0
  159. assert best_spot.y == 0
  160. ar.place(best_spot.x, best_spot.y, shape_arr_square)
  161. # Place object a second time
  162. best_spot = ar.bestSpot(shape_arr)
  163. assert best_spot.x is not None # we found a location
  164. assert best_spot.x != 0 or best_spot.y != 0 # it can't be on the same location
  165. ar.place(best_spot.x, best_spot.y, shape_arr)
  166. best_spot = ar.bestSpot(shape_arr_square)
  167. ar.place(best_spot.x, best_spot.y, shape_arr_square)
  168. ## Try to place an object and see if something explodes
  169. def test_smoke_place():
  170. ar = Arrange(30, 30, 15, 15)
  171. ar.centerFirst()
  172. shape_arr = gimmeShapeArray()
  173. assert not numpy.any(ar._occupied)
  174. ar.place(0, 0, shape_arr)
  175. assert numpy.any(ar._occupied)
  176. ## See of our center has less penalty points than out of the center
  177. def test_checkShape():
  178. ar = Arrange(30, 30, 15, 15)
  179. ar.centerFirst()
  180. shape_arr = gimmeShapeArray()
  181. points = ar.checkShape(0, 0, shape_arr)
  182. points2 = ar.checkShape(5, 0, shape_arr)
  183. points3 = ar.checkShape(0, 5, shape_arr)
  184. assert points2 > points
  185. assert points3 > points
  186. ## See of our center has less penalty points than out of the center
  187. def test_checkShape_rectangular():
  188. ar = Arrange(20, 30, 10, 15)
  189. ar.centerFirst()
  190. shape_arr = gimmeShapeArray()
  191. points = ar.checkShape(0, 0, shape_arr)
  192. points2 = ar.checkShape(5, 0, shape_arr)
  193. points3 = ar.checkShape(0, 5, shape_arr)
  194. assert points2 > points
  195. assert points3 > points
  196. ## Check that placing an object on occupied place returns None.
  197. def test_checkShape_place():
  198. ar = Arrange(30, 30, 15, 15)
  199. ar.centerFirst()
  200. shape_arr = gimmeShapeArray()
  201. ar.checkShape(3, 6, shape_arr)
  202. ar.place(3, 6, shape_arr)
  203. points2 = ar.checkShape(3, 6, shape_arr)
  204. assert points2 is None
  205. ## Test the whole sequence
  206. def test_smoke_place_objects():
  207. ar = Arrange(20, 20, 10, 10, scale = 1)
  208. ar.centerFirst()
  209. shape_arr = gimmeShapeArray()
  210. for i in range(5):
  211. best_spot_x, best_spot_y, score, prio = ar.bestSpot(shape_arr)
  212. ar.place(best_spot_x, best_spot_y, shape_arr)
  213. # Test some internals
  214. def test_compare_occupied_and_priority_tables():
  215. ar = Arrange(10, 15, 5, 7)
  216. ar.centerFirst()
  217. assert ar._priority.shape == ar._occupied.shape
  218. ## Polygon -> array
  219. def test_arrayFromPolygon():
  220. vertices = numpy.array([[-3, 1], [3, 1], [0, -3]])
  221. array = ShapeArray.arrayFromPolygon([5, 5], vertices)
  222. assert numpy.any(array)
  223. ## Polygon -> array
  224. def test_arrayFromPolygon2():
  225. vertices = numpy.array([[-3, 1], [3, 1], [2, -3]])
  226. array = ShapeArray.arrayFromPolygon([5, 5], vertices)
  227. assert numpy.any(array)
  228. ## Polygon -> array
  229. def test_fromPolygon():
  230. vertices = numpy.array([[0, 0.5], [0, 0], [0.5, 0]])
  231. array = ShapeArray.fromPolygon(vertices, scale=0.5)
  232. assert numpy.any(array.arr)
  233. ## Line definition -> array with true/false
  234. def test_check():
  235. base_array = numpy.zeros([5, 5], dtype=float)
  236. p1 = numpy.array([0, 0])
  237. p2 = numpy.array([4, 4])
  238. check_array = ShapeArray._check(p1, p2, base_array)
  239. assert numpy.any(check_array)
  240. assert check_array[3][0]
  241. assert not check_array[0][3]
  242. ## Line definition -> array with true/false
  243. def test_check2():
  244. base_array = numpy.zeros([5, 5], dtype=float)
  245. p1 = numpy.array([0, 3])
  246. p2 = numpy.array([4, 3])
  247. check_array = ShapeArray._check(p1, p2, base_array)
  248. assert numpy.any(check_array)
  249. assert not check_array[3][0]
  250. assert check_array[3][4]
  251. ## Just adding some stuff to ensure fromNode works as expected. Some parts should actually be in UM
  252. def test_parts_of_fromNode():
  253. from UM.Math.Polygon import Polygon
  254. p = Polygon(numpy.array([[-2, -2], [2, -2], [2, 2], [-2, 2]], dtype=numpy.int32))
  255. offset = 1
  256. p_offset = p.getMinkowskiHull(Polygon.approximatedCircle(offset))
  257. assert len(numpy.where(p_offset._points[:, 0] >= 2.9)) > 0
  258. assert len(numpy.where(p_offset._points[:, 0] <= -2.9)) > 0
  259. assert len(numpy.where(p_offset._points[:, 1] >= 2.9)) > 0
  260. assert len(numpy.where(p_offset._points[:, 1] <= -2.9)) > 0
  261. def test_parts_of_fromNode2():
  262. from UM.Math.Polygon import Polygon
  263. p = Polygon(numpy.array([[-2, -2], [2, -2], [2, 2], [-2, 2]], dtype=numpy.int32) * 2) # 4x4
  264. offset = 13.3
  265. scale = 0.5
  266. p_offset = p.getMinkowskiHull(Polygon.approximatedCircle(offset))
  267. shape_arr1 = ShapeArray.fromPolygon(p._points, scale = scale)
  268. shape_arr2 = ShapeArray.fromPolygon(p_offset._points, scale = scale)
  269. assert shape_arr1.arr.shape[0] >= (4 * scale) - 1 # -1 is to account for rounding errors
  270. assert shape_arr2.arr.shape[0] >= (2 * offset + 4) * scale - 1