TestArrange.py 9.8 KB

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