def test_contains(self): "" s = Segments(*segments) # point self.assertTrue(s.contains(Point(0, 0))) self.assertTrue(s.contains(Point(0.5, 0))) self.assertFalse(s.contains(Point(-1, 0)))
def test___eq__(self): "" s = Segments(*segments) self.assertTrue(s == s) s1 = Segments(*segments) s1.append(Segment(Point(1, 1), Point(0, 1))) self.assertFalse(s == s1)
def test_add_first(self): "" s = Segments(*segments) self.assertEqual(s.add_first(Segment(Point(-1, 0), Point(0, 0))), (Segment(Point(-1.0, 0.0), Point(1.0, 0.0)), 0)) self.assertEqual(s.add_first(Segment(Point(1, 1), Point(1, 2))), (Segment(Point(1.0, 0.0), Point(1.0, 2.0)), 1))
def test_segments(self): "" pl = Polyline3D(*points) self.assertEqual( pl.segments, Segments(Segment3D(Point3D(0, 0, 0), Point3D(0, 1, 0)), Segment3D(Point3D(0, 1, 0), Point3D(1, 1, 0))))
def test_remove_points_in_segments(self): "" pts = Points(Point(0,0), Point(1,0)) segments = Segments(Segment(Point(0,0), Point(0,1))) pts.remove_points_in_segments(segments) self.assertEqual(pts, Points(Point(1.0,0.0)))
def test_intersect_segment(self): "" pg = Polygon2D(Point2D(0, 0), Point2D(2, 0), Point2D(1, 1), Point2D(2, 2), Point2D(0, 2)) pgs = pg._triangulate self.assertEqual( pgs.intersect_segment(Segment2D(Point2D(1.5, 0), Point2D(1.5, 10))), (Points(), Segments(Segment2D(Point2D(1.5, 0), Point2D(1.5, 0.5)), Segment2D(Point2D(1.5, 1.5), Point2D(1.5, 2)))))
def test_difference_segment(self): "" s = Segment2D(P0, P1) # no intersection, difference is self self.assertEqual( s.difference_segment(Segment2D(Point2D(5, 5), Point2D(6, 6))), Segments(s)) # point intersection, difference is self self.assertEqual( s.difference_segment(Segment2D(Point2D(1, 1), Point2D(2, 2))), Segments(s)) # segment intersection, difference is remaining segment part self.assertEqual( s.difference_segment(Segment2D(Point2D(0.5, 0.5), Point2D(2, 2))), Segments(Segment2D(Point2D(0, 0), Point2D(0.5, 0.5)), )) # self intersection, difference is None self.assertEqual(s.difference_segment(s), Segments()) # segment intersection, inside original # segment intersection, difference is remaining segment part self.assertEqual( s.difference_segment( Segment2D(Point2D(0.25, 0.25), Point2D(0.75, 0.75))), Segments(Segment2D(Point2D(0, 0), Point2D(0.25, 0.25)), Segment2D(Point2D(0.75, 0.75), Point2D(1, 1)))) # segment intersection, outside both start and end point self.assertEqual( s.difference_segment(Segment2D(Point2D(-1, -1), Point2D(2, 2))), Segments())
def test_add_all(self): "" # no additions s = Segments(*segments) s.add_all() self.assertEqual(s, Segments(*segments)) # an addition s = Segments(Segment(Point(0, 0), Point(1, 0)), Segment(Point(1, 0), Point(2, 0))) s.add_all() self.assertEqual(s, Segments(Segment(Point(0, 0), Point(2, 0)))) # reversed s = Segments(Segment(Point(1, 0), Point(2, 0)), Segment(Point(0, 0), Point(1, 0))) s.add_all() self.assertEqual(s, Segments(Segment(Point(0, 0), Point(2, 0)))) # gap s = Segments(Segment(Point(0, 0), Point(1, 0)), Segment(Point(2, 0), Point(3, 0))) s.add_all() self.assertEqual( s, Segments(Segment(Point(0, 0), Point(1, 0)), Segment(Point(2, 0), Point(3, 0)))) # an gap and an addition s = Segments(Segment(Point(0, 0), Point(1, 0)), Segment(Point(2, 0), Point(3, 0)), Segment(Point(3, 0), Point(4, 0))) s.add_all() self.assertEqual( s, Segments(Segment(Point(0, 0), Point(1, 0)), Segment(Point(2.0, 0.0), Point(4.0, 0.0))))
def _test_intersect_segments(self): "" s = Segments(*segments) self.assertEqual(s.intersect_segments(s), (Points(), s))
def test___init__(self): "" s = Segments(*segments) self.assertIsInstance(s, Segments) self.assertEqual(s._segments, list(segments))
def _test_intersect_segment(self): "" s = Segments(*segments) # no intersection self.assertEqual( s.intersect_segment(Segment2D(Point2D(-1, 0), Point2D(-0.5, 0))), (Points(), Segments())) # single point intersection self.assertEqual( s.intersect_segment(Segment2D(Point2D(-1, 0), Point2D(0, 0))), (Points(Point2D(0, 0)), Segments())) # two point intersection self.assertEqual( s.intersect_segment(Segment2D(Point2D(0, 0), Point2D(1, 1))), (Points(Point2D(0, 0), Point2D(1, 1)), Segments())) # segment intersection self.assertEqual( s.intersect_segment(Segment2D(Point2D(0, 0), Point2D(1, 0))), (Points(), Segments(Segment2D(Point2D(0, 0), Point2D(1, 0))))) self.assertEqual( s.intersect_segment(Segment2D(Point2D(-1, 0), Point2D(1, 0))), (Points(), Segments(Segment2D(Point2D(0, 0), Point2D(1, 0))))) self.assertEqual( s.intersect_segment(Segment2D(Point2D(0, 0), Point2D(2, 0))), (Points(), Segments(Segment2D(Point2D(0, 0), Point2D(1, 0))))) self.assertEqual( s.intersect_segment(Segment2D(Point2D(-1, 0), Point2D(2, 0))), (Points(), Segments(Segment2D(Point2D(0, 0), Point2D(1, 0)))))
def _test_difference_segments(self): "" s = Segments(*segments) # no intersection s1 = Segments(Segment2D(Point2D(0, 5), Point2D(0, 6))) self.assertEqual(s.difference_segments(s1), s) # edge intersection - single edge s1 = Segments(Segment2D(Point2D(0, 0), Point2D(1, 0))) self.assertEqual(s.difference_segments(s1), Segments(Segment2D(Point2D(1, 0), Point2D(1, 1)))) # edge intersection - mid to end single edge s1 = Segments(Segment2D(Point2D(0, 0), Point2D(0.5, 0))) self.assertEqual( s.difference_segments(s1), Segments(Segment2D(Point2D(0.5, 0), Point2D(1, 0)), Segment2D(Point2D(1, 0), Point2D(1, 1)))) # edge intersection - start to mid single edge s1 = Segments(Segment2D(Point2D(0.5, 0), Point2D(1, 0))) self.assertEqual( s.difference_segments(s1), Segments(Segment2D(Point2D(0, 0), Point2D(0.5, 0)), Segment2D(Point2D(1, 0), Point2D(1, 1)))) # self intersection self.assertEqual(s.difference_segments(s), Segments()) # s1 = Segments(Segment2D(Point2D(1.0, 1.0), Point2D(0.5, 1)), Segment2D(Point2D(0.5, 1), Point2D(0.5, 0.0)), Segment2D(Point2D(0.5, 0.0), Point2D(1.0, 0.0)), Segment2D(Point2D(1.0, 0.0), Point2D(1.0, 1.0))) s2 = Segments(Segment2D(Point2D(0, 0), Point2D(1, 0)), Segment2D(Point2D(1, 0), Point2D(1, 1)), Segment2D(Point2D(1, 1), Point2D(0, 1)), Segment2D(Point2D(0, 1), Point2D(0, 0))) self.assertEqual( s1.difference_segments(s2), Segments(Segment2D(Point2D(0.5, 1), Point2D(0.5, 0.0)))) # s1 = Segments(Segment2D(Point2D(0, 0), Point2D(2, 0)), Segment2D(Point2D(2, 0), Point2D(2, 1)), Segment2D(Point2D(2, 1), Point2D(0, 1)), Segment2D(Point2D(0, 1), Point2D(0, 0))) s2 = Segments(Segment2D(Point2D(0.5, 0), Point2D(1.5, 0)), Segment2D(Point2D(1.5, 0), Point2D(1.5, 1)), Segment2D(Point2D(1.5, 1), Point2D(0.5, 1)), Segment2D(Point2D(0.5, 1), Point2D(0.5, 0))) self.assertEqual( s1.difference_segments(s2), Segments(Segment2D(Point2D(0, 0), Point2D(0.5, 0.0)), Segment2D(Point2D(1.5, 0.0), Point2D(2, 0)), Segment2D(Point2D(2, 0), Point2D(2, 1)), Segment2D(Point2D(2, 1), Point2D(1.5, 1.0)), Segment2D(Point2D(0.5, 1.0), Point2D(0, 1)), Segment2D(Point2D(0, 1), Point2D(0, 0))))
def test_segments(self): "" pls=Polylines(*polylines) self.assertEqual(pls.segments, Segments(Segment2D(Point2D(0,0),Point2D(1,0)), Segment2D(Point2D(1,0),Point2D(1,1))))
def test_difference_segments(self): "" s = Segment2D(P0, P1) # self intersection - intersection first s1 = Segments(Segment2D(Point2D(0, 0), Point2D(1, 1)), Segment2D(Point2D(1, 1), Point2D(2, 1)), Segment2D(Point2D(4, 1), Point2D(5, 1))) self.assertEqual(s.difference_segments(s1), Segments()) # self intersection - intersection last s1 = Segments(Segment2D(Point2D(4, 1), Point2D(5, 1)), Segment2D(Point2D(1, 1), Point2D(2, 1)), Segment2D(Point2D(0, 0), Point2D(1, 1))) self.assertEqual(s.difference_segments(s1), Segments()) # no intersection s1 = Segments(Segment2D(Point2D(0, 0), Point2D(1, 0)), Segment2D(Point2D(1, 1), Point2D(2, 1))) self.assertEqual(s.difference_segments(s1), Segments(s)) # mid intersection s1 = Segments(Segment2D(Point2D(0, 0), Point2D(0.5, 0.5)), Segment2D(Point2D(1, 1), Point2D(2, 1))) self.assertEqual(s.difference_segments(s1), Segments(Segment2D(Point2D(0.5, 0.5), Point2D(1, 1)))) # full intersection using two segments s1 = Segments(Segment2D(Point2D(0, 0), Point2D(0.5, 0.5)), Segment2D(Point2D(0.5, 0.5), Point2D(1, 1))) self.assertEqual(s.difference_segments(s1), Segments()) # intersection inside original s1 = Segments(Segment2D(Point2D(0.25, 0.25), Point2D(0.75, 0.75)), Segment2D(Point2D(1, 1), Point2D(2, 2))) self.assertEqual( s.difference_segments(s1), Segments(Segment2D(Point2D(0, 0), Point2D(0.25, 0.25)), Segment2D(Point2D(0.75, 0.75), Point2D(1, 1)))) # intersection inside original s1 = Segments(Segment2D(Point2D(0.2, 0.2), Point2D(0.4, 0.4)), Segment2D(Point2D(0.6, 0.6), Point2D(0.8, 0.8))) self.assertEqual( s.difference_segments(s1), Segments(Segment2D(Point2D(0, 0), Point2D(0.2, 0.2)), Segment2D(Point2D(0.4, 0.4), Point2D(0.6, 0.6)), Segment2D(Point2D(0.8, 0.8), Point2D(1.0, 1.0))))