test_pathconnect.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. from random import random
  7. class PathConnectTest1(unittest.TestCase):
  8. def setUp(self):
  9. pass
  10. def test_simple_connect(self):
  11. paths = [
  12. LineString([[0, 0], [1, 1]]),
  13. LineString([[1, 1], [2, 1]])
  14. ]
  15. result = Geometry.path_connect(paths)
  16. self.assertEqual(len(result), 1)
  17. self.assertTrue(result[0].equals(LineString([[0, 0], [1, 1], [2, 1]])))
  18. def test_interfere_connect(self):
  19. paths = [
  20. LineString([[0, 0], [1, 1]]),
  21. LineString([[1, 1], [2, 1]]),
  22. LineString([[-0.5, 0.5], [0.5, 0]])
  23. ]
  24. result = Geometry.path_connect(paths)
  25. self.assertEqual(len(result), 2)
  26. matches = [p for p in result if p.equals(LineString([[0, 0], [1, 1], [2, 1]]))]
  27. self.assertEqual(len(matches), 1)
  28. def test_simple_connect_offset1(self):
  29. for i in range(20):
  30. offset_x = random()
  31. offset_y = random()
  32. paths = [
  33. LineString([[0 + offset_x, 0 + offset_y], [1 + offset_x, 1 + offset_y]]),
  34. LineString([[1 + offset_x, 1 + offset_y], [2 + offset_x, 1 + offset_y]])
  35. ]
  36. result = Geometry.path_connect(paths)
  37. self.assertEqual(len(result), 1)
  38. self.assertTrue(result[0].equals(LineString([[0 + offset_x, 0 + offset_y],
  39. [1 + offset_x, 1 + offset_y],
  40. [2 + offset_x, 1 + offset_y]])))
  41. if __name__ == "__main__":
  42. unittest.main()