test_paint.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import unittest
  2. from shapely.geometry import LineString, Polygon
  3. from shapely.ops import cascaded_union, unary_union
  4. from matplotlib.pyplot import plot, subplot, show, cla, clf, xlim, ylim, title
  5. from camlib import *
  6. def plotg2(geo, solid_poly=False, color="black", linestyle='solid'):
  7. try:
  8. for sub_geo in geo:
  9. plotg2(sub_geo, solid_poly=solid_poly, color=color, linestyle=linestyle)
  10. except TypeError:
  11. if type(geo) == Polygon:
  12. if solid_poly:
  13. patch = PolygonPatch(geo,
  14. #facecolor="#BBF268",
  15. facecolor=color,
  16. edgecolor="#006E20",
  17. alpha=0.5,
  18. zorder=2)
  19. ax = subplot(111)
  20. ax.add_patch(patch)
  21. else:
  22. x, y = geo.exterior.coords.xy
  23. plot(x, y, color=color, linestyle=linestyle)
  24. for ints in geo.interiors:
  25. x, y = ints.coords.xy
  26. plot(x, y, color=color, linestyle=linestyle)
  27. if type(geo) == LineString or type(geo) == LinearRing:
  28. x, y = geo.coords.xy
  29. plot(x, y, color=color, linestyle=linestyle)
  30. if type(geo) == Point:
  31. x, y = geo.coords.xy
  32. plot(x, y, 'o')
  33. class PaintTestCase(unittest.TestCase):
  34. # def __init__(self):
  35. # super(PaintTestCase, self).__init__()
  36. # self.boundary = None
  37. # self.descr = None
  38. def plot_summary_A(self, paths, tooldia, result, msg):
  39. plotg2(self.boundary, solid_poly=True, color="green")
  40. plotg2(paths, color="red")
  41. plotg2([r.buffer(tooldia / 2) for r in result], solid_poly=True, color="blue")
  42. plotg2(result, color="black", linestyle='dashed')
  43. title(msg)
  44. xlim(0, 5)
  45. ylim(0, 5)
  46. show()
  47. class PaintConnectTest(PaintTestCase):
  48. """
  49. Simple rectangular boundary and paths inside.
  50. """
  51. def setUp(self):
  52. self.boundary = Polygon([[0, 0], [0, 5], [5, 5], [5, 0]])
  53. def test_jump(self):
  54. print "Test: WALK Expected"
  55. paths = [
  56. LineString([[0.5, 2], [2, 4.5]]),
  57. LineString([[2, 0.5], [4.5, 2]])
  58. ]
  59. for p in paths:
  60. print p
  61. tooldia = 1.0
  62. print "--"
  63. result = Geometry.paint_connect(paths, self.boundary, tooldia)
  64. for r in result:
  65. print r
  66. self.assertEqual(len(result), 1)
  67. # plotg(self.boundary, solid_poly=True)
  68. # plotg(paths, color="red")
  69. # plotg([r.buffer(tooldia / 2) for r in result], solid_poly=True)
  70. # show()
  71. # #cla()
  72. # clf()
  73. self.plot_summary_A(paths, tooldia, result, "WALK expected.")
  74. def test_no_jump1(self):
  75. print "Test: FLY Expected"
  76. paths = [
  77. LineString([[0, 2], [2, 5]]),
  78. LineString([[2, 0], [5, 2]])
  79. ]
  80. for p in paths:
  81. print p
  82. tooldia = 1.0
  83. print "--"
  84. result = Geometry.paint_connect(paths, self.boundary, tooldia)
  85. for r in result:
  86. print r
  87. self.assertEqual(len(result), len(paths))
  88. self.plot_summary_A(paths, tooldia, result, "FLY Expected")
  89. def test_no_jump2(self):
  90. print "Test: FLY Expected"
  91. paths = [
  92. LineString([[0.5, 2], [2, 4.5]]),
  93. LineString([[2, 0.5], [4.5, 2]])
  94. ]
  95. for p in paths:
  96. print p
  97. tooldia = 1.1
  98. print "--"
  99. result = Geometry.paint_connect(paths, self.boundary, tooldia)
  100. for r in result:
  101. print r
  102. self.assertEqual(len(result), len(paths))
  103. self.plot_summary_A(paths, tooldia, result, "FLY Expected")
  104. class PaintConnectTest2(PaintTestCase):
  105. """
  106. Boundary with an internal cutout.
  107. """
  108. def setUp(self):
  109. self.boundary = Polygon([[0, 0], [0, 5], [5, 5], [5, 0]])
  110. self.boundary = self.boundary.difference(
  111. Polygon([[2, 1], [3, 1], [3, 4], [2, 4]])
  112. )
  113. def test_no_jump3(self):
  114. print "TEST: No jump expected"
  115. paths = [
  116. LineString([[0.5, 1], [1.5, 3]]),
  117. LineString([[4, 1], [4, 4]])
  118. ]
  119. for p in paths:
  120. print p
  121. tooldia = 1.0
  122. print "--"
  123. result = Geometry.paint_connect(paths, self.boundary, tooldia)
  124. for r in result:
  125. print r
  126. self.assertEqual(len(result), len(paths))
  127. self.plot_summary_A(paths, tooldia, result, "FLY Expected")
  128. class PaintConnectTest3(PaintTestCase):
  129. """
  130. Tests with linerings among elements.
  131. """
  132. def setUp(self):
  133. self.boundary = Polygon([[0, 0], [0, 5], [5, 5], [5, 0]])
  134. def test_jump2(self):
  135. print "Test: WALK Expected"
  136. paths = [
  137. LineString([[0.5, 2], [2, 4.5]]),
  138. LineString([[2, 0.5], [4.5, 2]]),
  139. self.boundary.buffer(-0.5).exterior
  140. ]
  141. for p in paths:
  142. print p
  143. tooldia = 1.0
  144. print "--"
  145. result = Geometry.paint_connect(paths, self.boundary, tooldia)
  146. for r in result:
  147. print r
  148. self.assertEqual(len(result), 1)
  149. self.plot_summary_A(paths, tooldia, result, "WALK Expected")
  150. if __name__ == '__main__':
  151. unittest.main()