test_pathconnect.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import unittest
  2. from shapely.geometry import LineString, Polygon
  3. from shapely.ops import unary_union
  4. from matplotlib.pyplot import plot, subplot, show, cla, clf, xlim, ylim, title
  5. from camlib import *
  6. from random import random
  7. def mkstorage(paths):
  8. def get_pts(o):
  9. return [o.coords[0], o.coords[-1]]
  10. storage = FlatCAMRTreeStorage()
  11. storage.get_points = get_pts
  12. for p in paths:
  13. storage.insert(p)
  14. return storage
  15. class PathConnectTest1(unittest.TestCase):
  16. def setUp(self):
  17. print("PathConnectTest1.setUp()")
  18. pass
  19. def test_simple_connect(self):
  20. paths = [
  21. LineString([[0, 0], [1, 1]]),
  22. LineString([[1, 1], [2, 1]])
  23. ]
  24. result = Geometry.path_connect(mkstorage(paths))
  25. result = list(result.get_objects())
  26. self.assertEqual(len(result), 1)
  27. self.assertTrue(result[0].equals(LineString([[0, 0], [1, 1], [2, 1]])))
  28. def test_interfere_connect(self):
  29. paths = [
  30. LineString([[0, 0], [1, 1]]),
  31. LineString([[1, 1], [2, 1]]),
  32. LineString([[-0.5, 0.5], [0.5, 0]])
  33. ]
  34. result = Geometry.path_connect(mkstorage(paths))
  35. result = list(result.get_objects())
  36. self.assertEqual(len(result), 2)
  37. matches = [p for p in result if p.equals(LineString([[0, 0], [1, 1], [2, 1]]))]
  38. self.assertEqual(len(matches), 1)
  39. def test_simple_connect_offset1(self):
  40. for i in range(20):
  41. offset_x = random()
  42. offset_y = random()
  43. paths = [
  44. LineString([[0 + offset_x, 0 + offset_y], [1 + offset_x, 1 + offset_y]]),
  45. LineString([[1 + offset_x, 1 + offset_y], [2 + offset_x, 1 + offset_y]])
  46. ]
  47. result = Geometry.path_connect(mkstorage(paths))
  48. result = list(result.get_objects())
  49. self.assertEqual(len(result), 1)
  50. self.assertTrue(result[0].equals(LineString([[0 + offset_x, 0 + offset_y],
  51. [1 + offset_x, 1 + offset_y],
  52. [2 + offset_x, 1 + offset_y]])))
  53. def test_ring_interfere_connect(self):
  54. print()
  55. print("TEST STARTING ...")
  56. paths = [
  57. LineString([[0, 0], [1, 1]]),
  58. LineString([[1, 1], [2, 1]]),
  59. LinearRing([[1, 1], [2, 2], [1, 3], [0, 2]])
  60. ]
  61. result = Geometry.path_connect(mkstorage(paths))
  62. result = list(result.get_objects())
  63. self.assertEqual(len(result), 2)
  64. matches = [p for p in result if p.equals(LineString([[0, 0], [1, 1], [2, 1]]))]
  65. self.assertEqual(len(matches), 1)
  66. if __name__ == "__main__":
  67. unittest.main()