TestArrange.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import numpy
  2. from cura.Arranging.Arrange import Arrange
  3. from cura.Arranging.ShapeArray import ShapeArray
  4. def gimmeShapeArray():
  5. vertices = numpy.array([[-3, 1], [3, 1], [0, -3]])
  6. shape_arr = ShapeArray.fromPolygon(vertices)
  7. return shape_arr
  8. ## Smoke test for Arrange
  9. def test_smoke_arrange():
  10. ar = Arrange.create(fixed_nodes = [])
  11. ## Smoke test for ShapeArray
  12. def test_smoke_ShapeArray():
  13. shape_arr = gimmeShapeArray()
  14. ## Test centerFirst
  15. def test_centerFirst():
  16. ar = Arrange(300, 300, 150, 150)
  17. ar.centerFirst()
  18. assert ar._priority[150][150] < ar._priority[170][150]
  19. assert ar._priority[150][150] < ar._priority[150][170]
  20. assert ar._priority[150][150] < ar._priority[170][170]
  21. assert ar._priority[150][150] < ar._priority[130][150]
  22. assert ar._priority[150][150] < ar._priority[150][130]
  23. assert ar._priority[150][150] < ar._priority[130][130]
  24. ## Test backFirst
  25. def test_backFirst():
  26. ar = Arrange(300, 300, 150, 150)
  27. ar.backFirst()
  28. assert ar._priority[150][150] < ar._priority[150][170]
  29. assert ar._priority[150][150] < ar._priority[170][170]
  30. assert ar._priority[150][150] > ar._priority[150][130]
  31. assert ar._priority[150][150] > ar._priority[130][130]
  32. ## See if the result of bestSpot has the correct form
  33. def test_smoke_bestSpot():
  34. ar = Arrange(30, 30, 15, 15)
  35. ar.centerFirst()
  36. shape_arr = gimmeShapeArray()
  37. best_spot = ar.bestSpot(shape_arr)
  38. assert hasattr(best_spot, "x")
  39. assert hasattr(best_spot, "y")
  40. assert hasattr(best_spot, "penalty_points")
  41. assert hasattr(best_spot, "priority")
  42. ## Try to place an object and see if something explodes
  43. def test_smoke_place():
  44. ar = Arrange(30, 30, 15, 15)
  45. ar.centerFirst()
  46. shape_arr = gimmeShapeArray()
  47. assert not numpy.any(ar._occupied)
  48. ar.place(0, 0, shape_arr)
  49. assert numpy.any(ar._occupied)
  50. ## See of our center has less penalty points than out of the center
  51. def test_checkShape():
  52. ar = Arrange(30, 30, 15, 15)
  53. ar.centerFirst()
  54. shape_arr = gimmeShapeArray()
  55. points = ar.checkShape(0, 0, shape_arr)
  56. points2 = ar.checkShape(5, 0, shape_arr)
  57. points3 = ar.checkShape(0, 5, shape_arr)
  58. assert points2 > points
  59. assert points3 > points
  60. ## Check that placing an object on occupied place returns None.
  61. def test_checkShape_place():
  62. ar = Arrange(30, 30, 15, 15)
  63. ar.centerFirst()
  64. shape_arr = gimmeShapeArray()
  65. points = ar.checkShape(3, 6, shape_arr)
  66. ar.place(3, 6, shape_arr)
  67. points2 = ar.checkShape(3, 6, shape_arr)
  68. assert points2 is None
  69. ## Test the whole sequence
  70. def test_smoke_place_objects():
  71. ar = Arrange(20, 20, 10, 10)
  72. ar.centerFirst()
  73. shape_arr = gimmeShapeArray()
  74. for i in range(5):
  75. best_spot_x, best_spot_y, score, prio = ar.bestSpot(shape_arr)
  76. ar.place(best_spot_x, best_spot_y, shape_arr)
  77. ## Polygon -> array
  78. def test_arrayFromPolygon():
  79. vertices = numpy.array([[-3, 1], [3, 1], [0, -3]])
  80. array = ShapeArray.arrayFromPolygon([5, 5], vertices)
  81. assert numpy.any(array)
  82. ## Polygon -> array
  83. def test_arrayFromPolygon2():
  84. vertices = numpy.array([[-3, 1], [3, 1], [2, -3]])
  85. array = ShapeArray.arrayFromPolygon([5, 5], vertices)
  86. assert numpy.any(array)
  87. ## Polygon -> array
  88. def test_fromPolygon():
  89. vertices = numpy.array([[0, 0.5], [0, 0], [0.5, 0]])
  90. array = ShapeArray.fromPolygon(vertices, scale=0.5)
  91. assert numpy.any(array.arr)
  92. ## Line definition -> array with true/false
  93. def test_check():
  94. base_array = numpy.zeros([5, 5], dtype=float)
  95. p1 = numpy.array([0, 0])
  96. p2 = numpy.array([4, 4])
  97. check_array = ShapeArray._check(p1, p2, base_array)
  98. assert numpy.any(check_array)
  99. assert check_array[3][0]
  100. assert not check_array[0][3]
  101. ## Line definition -> array with true/false
  102. def test_check2():
  103. base_array = numpy.zeros([5, 5], dtype=float)
  104. p1 = numpy.array([0, 3])
  105. p2 = numpy.array([4, 3])
  106. check_array = ShapeArray._check(p1, p2, base_array)
  107. assert numpy.any(check_array)
  108. assert not check_array[3][0]
  109. assert check_array[3][4]