TestArrange.py 11 KB

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