TestArrange.py 3.8 KB

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