def test_contains(self): "" P0,vL=Point(0,0),Vector(1,1) hl=Halfline(P0,vL) # point self.assertTrue(hl.contains(P0)) self.assertTrue(hl.contains(P0+vL)) self.assertTrue(hl.contains(P0+vL*10)) self.assertFalse(hl.contains(P0+vL.opposite)) self.assertFalse(hl.contains(P0+vL.perp_vector)) # segment self.assertTrue(hl.contains(Segment(P0,P0+vL))) self.assertTrue(hl.contains(Segment(P0+vL,P0+vL*2))) self.assertFalse(hl.contains(Segment(P0+vL*-1,P0))) self.assertFalse(hl.contains(Segment(P0+vL*-2,P0+vL*-1))) self.assertFalse(hl.contains(Segment(P0+vL.perp_vector,P0+vL+vL.perp_vector))) P0,vL=Point(0,0,0),Vector(1,1,1) hl=Halfline(P0,vL) # point self.assertTrue(hl.contains(P0)) self.assertTrue(hl.contains(P0+vL)) self.assertTrue(hl.contains(P0+vL*10)) self.assertFalse(hl.contains(P0+vL.opposite)) self.assertFalse(hl.contains(P0+Vector(1,-1,0))) # segment self.assertTrue(hl.contains(Segment(P0,P0+vL))) self.assertTrue(hl.contains(Segment(P0+vL,P0+vL*2))) self.assertFalse(hl.contains(Segment(P0+vL*-1,P0))) self.assertFalse(hl.contains(Segment(P0+vL*-2,P0+vL*-1))) self.assertFalse(hl.contains(Segment(P0+Vector(1,-1,0),P0+vL+Vector(1,-1,0))))
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_line(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) self.assertEqual(s.line, Line(P0, s.line.vL)) P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) self.assertEqual(s.line, Line(P0, P1 - P0))
def test_order(self): "" P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) self.assertEqual(s.order.points, s.points) s = Segment(P1, P0) self.assertEqual(s.order.points, Points(Point(0, 0, 0), Point(1, 1, 1)))
def test_points(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) self.assertEqual(s.points, Points(P0, P1)) P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) self.assertEqual(s.points, Points(P0, P1))
def reverse(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) self.assertEqual(s.reverse, Segment(P1, P0)) P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) self.assertEqual(s.reverse, Segment(P1, P0))
def test___eq__(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) self.assertTrue(s == s) self.assertFalse(Segment(P0, P0 + s.line.vL * 0.5) == s) P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) self.assertTrue(s == s) self.assertFalse(Segment(P0, P0 + s.line.vL * 0.5) == s)
def test___init__(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) self.assertIsInstance(s, Segment) self.assertEqual(s.P0, Point(0, 0)) self.assertEqual(s.P1, Point(1, 1)) P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) self.assertIsInstance(s, Segment) self.assertEqual(s.P0, Point(0, 0, 0)) self.assertEqual(s.P1, Point(1, 1, 1))
def test_intersect_halfline(self): "" P0,vL=Point(0,0),Vector(1,1) hl=Halfline(P0,vL) # same self.assertEqual(hl.intersect_halfline(hl), hl) # codirectional self.assertEqual(hl.intersect_halfline(Halfline(P0+vL,vL)), Halfline(P0+vL,vL)) self.assertEqual(hl.intersect_halfline(Halfline(P0+vL*-1,vL)), hl) # parallel self.assertEqual(hl.intersect_halfline(Halfline(P0+vL.perp_vector,vL)), None) # collinear but not codirectinal self.assertEqual(hl.intersect_halfline(Halfline(P0,vL*-1)), hl.P0) self.assertEqual(hl.intersect_halfline(Halfline(P0+vL,vL*-1)), Segment(P0,P0+vL)) # skew self.assertEqual(hl.intersect_halfline(Halfline(P0,vL.perp_vector)), hl.P0) self.assertEqual(hl.intersect_halfline(Halfline(P0+vL,vL.perp_vector)), P0+vL) self.assertEqual(hl.intersect_halfline(Halfline(P0+vL*-1,vL.perp_vector)), None)
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_distance_to_point(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) self.assertEqual(s.distance_to_point(Point(-2, 0)), 2) self.assertEqual(s.distance_to_point(Point(2, 1)), 1) self.assertEqual(s.distance_to_point(Point(0, 1)), 0.5**0.5) self.assertEqual(s.distance_to_point(Point(1, 0)), 0.5**0.5) P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) self.assertEqual(s.distance_to_point(Point(-2, 0, 0)), 2) self.assertEqual(s.distance_to_point(Point(1, 1, 2)), 1) self.assertEqual(s.distance_to_point(Point(0, 0, 0)), 0) self.assertEqual(s.distance_to_point(Point(1, -1, 0)), (s.P0 - Point(1, -1, 0)).length)
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_distance_to_segment(self): "" P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) self.assertEqual(s.distance_to_segment(s), 0) self.assertEqual( s.distance_to_segment( Segment(P0 + Vector(1, -1, 0), P1 + Vector(1, -1, 0))), Vector(1, -1, 0).length) self.assertEqual(s.distance_to_segment(Segment(P0, Point(1, -1, 0))), 0) self.assertEqual( s.distance_to_segment(Segment(Point(-2, -2, -2), Point(-1, -1, -1))), Vector(1, 1, 1).length)
def test_plot(self): "" if plot: P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) fig, ax = plt.subplots() s.plot(ax) if plot: P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') s.plot(ax)
def test_intersect_halfline(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) # collinear - same start point self.assertEqual(s.intersect_halfline(Halfline(P0, s.line.vL)), s) # collinear - halfline start point is segment end point self.assertEqual(s.intersect_halfline(Halfline(P1, s.line.vL)), P1) # collinear - halfline start point is segment mid point self.assertEqual( s.intersect_halfline(Halfline(P0 + s.line.vL * 0.5, s.line.vL)), Segment(P0 + s.line.vL * 0.5, P1)) self.assertEqual( s.intersect_halfline(Halfline(P0 + s.line.vL * 0.5, s.line.vL * -1)), Segment(P0, P0 + s.line.vL * 0.5))
def test_calculate_point(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) self.assertEqual(s.calculate_point(0.5), P0 + s.line.vL * 0.5) P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) self.assertEqual(s.calculate_point(0.5), P0 + s.line.vL * 0.5)
def test___add__(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) s1 = Segment(Point(1, 1), Point(2, 2)) self.assertEqual(s + s1, Segment(Point(0, 0), Point(2, 2))) self.assertEqual( Segment(Point(1, 1), Point(1, 2)) + Segment(Point(1, 0), Point(1, 1)), Segment(Point(1.0, 0.0), Point(1.0, 2.0)))
def test_contains(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) # point self.assertTrue(l.contains(P0)) self.assertTrue(l.contains(P0 + vL)) self.assertFalse(l.contains(P0 + vL.perp_vector)) # # halfline self.assertTrue(l.contains(Halfline(P0, vL))) self.assertTrue(l.contains(Halfline(P0 + vL, vL))) self.assertFalse(l.contains(Halfline(P0 + vL.perp_vector, vL))) self.assertFalse(l.contains(Halfline(P0, vL.perp_vector))) # segment self.assertTrue(l.contains(Segment(P0, P0 + vL))) self.assertTrue(l.contains(Segment(P0, P0 + vL * 10))) self.assertFalse(l.contains(Segment(P0 + vL.perp_vector, P0 + vL))) self.assertFalse( l.contains(Segment(P0 + vL.perp_vector, P0 + vL + vL.perp_vector))) P0, vL = Point(0, 0, 0), Vector(1, 1, 1) l = Line(P0, vL) # point self.assertTrue(l.contains(P0)) self.assertTrue(l.contains(P0 + vL)) self.assertFalse(l.contains(P0 + Vector(1, -1, 0))) # halfline self.assertTrue(l.contains(Halfline(P0, vL))) self.assertTrue(l.contains(Halfline(P0 + vL, vL))) self.assertFalse(l.contains(Halfline(P0 + Vector(1, -1, 0), vL))) self.assertFalse(l.contains(Halfline(P0, Vector(1, -1, 0)))) # segment self.assertTrue(l.contains(Segment(P0, P0 + vL))) self.assertTrue(l.contains(Segment(P0, P0 + vL * 10))) self.assertFalse(l.contains(Segment(P0 + Vector(1, -1, 0), P0 + vL))) self.assertFalse( l.contains( Segment(P0 + Vector(1, -1, 0), P0 + vL + Vector(1, -1, 0))))
def test_intersect_line(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) # collinear self.assertEqual(s.intersect_line(Line(P0, s.line.vL)), s) # parallel self.assertEqual( s.intersect_line(Line(P0 + s.line.vL.perp_vector, s.line.vL)), None) # skew - same P0s self.assertEqual(s.intersect_line(Line(P0, s.line.vL.perp_vector)), P0) # skew - different P0s self.assertEqual( s.intersect_line(Line(Point(0.5, 0), s.line.vL.perp_vector)), Point(0.25, 0.25)) # skew - no intersection self.assertEqual( s.intersect_line(Line(P0 + s.line.vL * -1, s.line.vL.perp_vector)), None)
def test_intersect_segment(self): "" pl = Plane(P0, N) # segment in plane self.assertEqual(pl.intersect_segment(Segment(P0, Point(1, 0, 0))), Segment(P0, Point(1, 0, 0))) # parallel segment not in plane self.assertEqual( pl.intersect_segment(Segment(P0 + N, Point(1, 0, 0) + N)), None) # perpendicular segment passing through P0 self.assertEqual(pl.intersect_segment(Segment(P0, P0 + N)), P0) self.assertEqual(pl.intersect_segment(Segment(P0 - N, P0)), P0) # perpendicular segment not passing through plane self.assertEqual(pl.intersect_segment(Segment(P0 + N, P0 + N * 2)), None) self.assertEqual(pl.intersect_segment(Segment(P0 - N, P0 - N * 2)), None)
def test_intersect_segment(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) # collinear - same segment self.assertEqual(s.intersect_segment(s), s) # collinear - different start point inside segment self.assertEqual( s.intersect_segment(Segment(P0 + s.line.vL * 0.5, P1)), Segment(P0 + s.line.vL * 0.5, P1)) # collinear - different start point outside segment self.assertEqual( s.intersect_segment(Segment(P0 + s.line.vL * -0.5, P1)), s) # collinear - different end point inside segment self.assertEqual( s.intersect_segment(Segment(P0, P1 + s.line.vL * -0.5)), Segment(P0, P1 + s.line.vL * -0.5)) # collinear - different start point outside segment self.assertEqual( s.intersect_segment(Segment(P0, P1 + s.line.vL * 0.5)), s) # collinear - start point and end point inside segment self.assertEqual( s.intersect_segment( Segment(P0 + s.line.vL * 0.25, P1 + s.line.vL * -0.25)), Segment(P0 + s.line.vL * 0.25, P1 + s.line.vL * -0.25)) # collinear - start point and end point outside segment self.assertEqual( s.intersect_segment( Segment(P0 + s.line.vL * -0.25, P1 + s.line.vL * 0.25)), s) # collinear - but no intersection self.assertEqual( s.intersect_segment(Segment(P0 + s.line.vL * 2, P1 + s.line.vL * 2)), None) # parallel self.assertEqual( s.intersect_segment( Segment(P0 + s.line.vL.perp_vector, P1 + s.line.vL.perp_vector)), None) # skew - intersecting at start point self.assertEqual( s.intersect_segment(Segment(P0, P1 + s.line.vL.perp_vector)), P0) # skew - intersecting at end point self.assertEqual( s.intersect_segment( Segment(P1 + s.line.vL.perp_vector * -1, P1 + s.line.vL.perp_vector)), P1) # skew - intersecting at mid points self.assertEqual( s.intersect_segment( Segment(P0 + s.line.vL * 0.5 + s.line.vL.perp_vector * -0.5, P0 + s.line.vL * 0.5 + s.line.vL.perp_vector * 0.5)), P0 + s.line.vL * 0.5) # skew - no intersection self.assertEqual( s.intersect_segment( Segment(P0 + s.line.vL.perp_vector * 0.5, P0 + s.line.vL.perp_vector * 1.5)), None) s1 = Segment(Point(0, 0, 0), Point(0, 1, 0)) s2 = Segment(Point(0, 1, 0), Point(1, 1, 0)) self.assertEqual(s1.intersect_segment(s2), Point(0, 1, 0))
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_difference_segments(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) # self intersection - intersection first s1 = Segments(Segment(Point(0, 0), Point(1, 1)), Segment(Point(1, 1), Point(2, 1)), Segment(Point(4, 1), Point(5, 1))) self.assertEqual(s.difference_segments(s1), Segments()) # self intersection - intersection last s1 = Segments(Segment(Point(4, 1), Point(5, 1)), Segment(Point(1, 1), Point(2, 1)), Segment(Point(0, 0), Point(1, 1))) self.assertEqual(s.difference_segments(s1), Segments()) # no intersection s1 = Segments(Segment(Point(0, 0), Point(1, 0)), Segment(Point(1, 1), Point(2, 1))) self.assertEqual(s.difference_segments(s1), Segments(s)) # mid intersection s1 = Segments(Segment(Point(0, 0), Point(0.5, 0.5)), Segment(Point(1, 1), Point(2, 1))) self.assertEqual(s.difference_segments(s1), Segments(Segment(Point(0.5, 0.5), Point(1, 1)))) # full intersection using two segments s1 = Segments(Segment(Point(0, 0), Point(0.5, 0.5)), Segment(Point(0.5, 0.5), Point(1, 1))) self.assertEqual(s.difference_segments(s1), Segments()) # intersection inside original s1 = Segments(Segment(Point(0.25, 0.25), Point(0.75, 0.75)), Segment(Point(1, 1), Point(2, 2))) self.assertEqual( s.difference_segments(s1), Segments(Segment(Point(0, 0), Point(0.25, 0.25)), Segment(Point(0.75, 0.75), Point(1, 1)))) # intersection inside original s1 = Segments(Segment(Point(0.2, 0.2), Point(0.4, 0.4)), Segment(Point(0.6, 0.6), Point(0.8, 0.8))) self.assertEqual( s.difference_segments(s1), Segments(Segment(Point(0, 0), Point(0.2, 0.2)), Segment(Point(0.4, 0.4), Point(0.6, 0.6)), Segment(Point(0.8, 0.8), Point(1.0, 1.0))))
# 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_intersect_segments(self): "" s = Segments(*segments) self.assertEqual(s.intersect_segments(s), (Points(), s)) if __name__ == '__main__': segments = (Segment(Point(0, 0), Point(1, 0)), Segment(Point(1, 0), Point(1, 1))) unittest.main(Test_Segments())
def test_difference_segment(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) # no intersection, difference is self self.assertEqual( s.difference_segment(Segment(Point(5, 5), Point(6, 6))), Segments(s)) # point intersection, difference is self self.assertEqual( s.difference_segment(Segment(Point(1, 1), Point(2, 2))), Segments(s)) # segment intersection, difference is remaining segment part self.assertEqual( s.difference_segment(Segment(Point(0.5, 0.5), Point(2, 2))), Segments(Segment(Point(0, 0), Point(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(Segment(Point(0.25, 0.25), Point(0.75, 0.75))), Segments(Segment(Point(0, 0), Point(0.25, 0.25)), Segment(Point(0.75, 0.75), Point(1, 1)))) # segment intersection, outside both start and end point self.assertEqual( s.difference_segment(Segment(Point(-1, -1), Point(2, 2))), Segments())
def test_contains(self): "" P0, P1 = Point(0, 0), Point(1, 1) s = Segment(P0, P1) # point self.assertTrue(s.contains(P0)) self.assertTrue(s.contains(P1)) self.assertTrue(s.contains(P0 + s.line.vL * 0.5)) # segment midpoint self.assertFalse(s.contains(P0 + s.line.vL * -0.5)) self.assertFalse(s.contains(P0 + s.line.vL * 1.5)) # segment self.assertTrue(s.contains(s)) self.assertTrue(s.contains(Segment(P0, P0 + s.line.vL * 0.5))) self.assertTrue(s.contains(Segment(P0 + s.line.vL * 0.5, P1))) self.assertFalse(s.contains(Segment(P0 + s.line.vL * -0.5, P1))) self.assertFalse(s.contains(Segment(P0, P1 + s.line.vL * 0.5))) self.assertFalse(s.contains(Segment(P0, P0 + s.line.vL.perp_vector))) P0, P1 = Point(0, 0, 0), Point(1, 1, 1) s = Segment(P0, P1) # point self.assertTrue(s.contains(P0)) self.assertTrue(s.contains(P1)) self.assertTrue(s.contains(P0 + s.line.vL * 0.5)) # segment midpoint self.assertFalse(s.contains(P0 + s.line.vL * -0.5)) self.assertFalse(s.contains(P0 + s.line.vL * 1.5)) # segment self.assertTrue(s.contains(s)) self.assertTrue(s.contains(Segment(P0, P0 + s.line.vL * 0.5))) self.assertTrue(s.contains(Segment(P0 + s.line.vL * 0.5, P1))) self.assertFalse(s.contains(Segment(P0 + s.line.vL * -0.5, P1))) self.assertFalse(s.contains(Segment(P0, P1 + s.line.vL * 0.5))) self.assertFalse(s.contains(Segment(P0, P0 + Vector(1, -1, 0))))