def test_intersection_plane(self): "" P0, N = Point(0, 0, 0), Vector(0, 0, 1) pl = Plane(P0, N) # coplanar plane self.assertEqual(pl.intersection(pl), GeometryObjects(pl)) # parallel, non-coplanar planes self.assertEqual(pl.intersection(Plane(P0 + N, N)), GeometryObjects()) # intersecting planes - same P0 self.assertEqual( pl.intersection(Plane(P0, Vector(1, 0, 0))), GeometryObjects(Line(Point(0, 0, 0), Vector(0, 1, 0)))) self.assertEqual( pl.intersection(Plane(P0, Vector(0, 1, 0))), GeometryObjects(Line(Point(0, 0, 0), Vector(-1, 0, 0)))) self.assertEqual( pl.intersection(Plane(P0, Vector(1, 1, 0))), GeometryObjects(Line(Point(0, 0, 0), Vector(-1, 1, 0)))) self.assertEqual( pl.intersection(Plane(P0, Vector(0, 1, 1))), GeometryObjects(Line(Point(0, 0, 0), Vector(-1, 0, 0)))) # intersecting planes - different P0 self.assertEqual( pl.intersection(Plane(P0 + Vector(1, 0, 0), Vector(1, 0, 0))), GeometryObjects(Line(Point(1, 0, 0), Vector(0, 1, 0))))
def test_intersect_plane(self): "" pl = Plane(P0, N) # coplanar plane self.assertEqual(pl.intersect_plane(pl), pl) # parallel, non-coplanar planes self.assertEqual(pl.intersect_plane(Plane(P0 + N, N)), None) # intersecting planes - same P0 self.assertEqual(pl.intersect_plane(Plane(P0, Vector(1, 0, 0))), Line(Point(0, 0, 0), Vector(0, 1, 0))) self.assertEqual(pl.intersect_plane(Plane(P0, Vector(0, 1, 0))), Line(Point(0, 0, 0), Vector(1, 0, 0))) self.assertEqual(pl.intersect_plane(Plane(P0, Vector(1, 1, 0))), Line(Point(0, 0, 0), Vector(-1, 1, 0))) self.assertEqual(pl.intersect_plane(Plane(P0, Vector(0, 1, 1))), Line(Point(0, 0, 0), Vector(1, 0, 0))) # intersecting planes - different P0 self.assertEqual( pl.intersect_plane(Plane(P0 + Vector(1, 0, 0), Vector(1, 0, 0))), Line(Point(1, 0, 0), Vector(0, 1, 0)))
def test_split(self): "" # 2d pg = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) pl = Polyline(Point(0.5, 0), Point(0.5, 1)) self.assertEqual( pg.split(pl), GeometryObjects( Polygon(Point(0.5, 0.0), Point(0.0, 0.0), Point(0.0, 1.0), Point(0.5, 1.0)), Polygon(Point(0.5, 1.0), Point(1.0, 1.0), Point(1.0, 0.0), Point(0.5, 0.0)))) pg = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) l = Line(Point(0.5, 0), Vector(0, 1)) self.assertEqual( pg.split(l), GeometryObjects( Polygon(Point(0.5, 0.0), Point(0.0, 0.0), Point(0.0, 1.0), Point(0.5, 1.0)), Polygon(Point(0.5, 1.0), Point(1.0, 1.0), Point(1.0, 0.0), Point(0.5, 0.0)))) # 3d pg = Polygon(Point(0, 0, 1), Point(1, 0, 1), Point(1, 1, 1), Point(0, 1, 1)) l = Line(Point(0.5, 0, 1), Vector(0, 1, 0)) #print(pg.split(l)); return self.assertEqual( pg.split(l), GeometryObjects( Polygon(Point(0.5, 0.0, 1.0), Point(0.0, 0.0, 1.0), Point(0.0, 1.0, 1.0), Point(0.5, 1.0, 1.0)), Polygon(Point(0.5, 1.0, 1.0), Point(1.0, 1.0, 1.0), Point(1.0, 0.0, 1.0), Point(0.5, 0.0, 1.0))))
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_line(self): P0,vL=Point(0,0),Vector(1,1) hl=Halfline(P0,vL) self.assertEqual(hl.line, Line(P0,vL)) P0,vL=Point(0,0,0),Vector(1,1,1) hl=Halfline(P0,vL) self.assertEqual(hl.line, Line(P0,vL))
def test__intersect_line_skew(self): "" pl = Plane(P0, N) # perpendicular line passing through P0 self.assertEqual(pl._intersect_line_skew(Line(P0, N)), P0) self.assertEqual(pl._intersect_line_skew(Line(P0 + N, N)), P0) # non perpendicular line not passing through P0 self.assertEqual( pl._intersect_line_skew(Line(Point(0, 0, 1), Vector(1, 0, -1))), Point(1, 0, 0))
def test___contains__(self): "" pl = Plane(P0, N) # point self.assertTrue(pl.contains(P0)) self.assertTrue(pl.contains(Point(10, 10, 0))) self.assertFalse(pl.contains(Point(0, 0, 1))) # line self.assertTrue(pl.contains(Line(P0, Vector(1, 0, 0)))) self.assertFalse(pl.contains(Line(P0 + N, Vector(1, 0, 0)))) self.assertFalse(pl.contains(Line(P0, N)))
def test_intersect_line(self): "" P0,vL=Point(0,0),Vector(1,1) hl=Halfline(P0,vL) # collinear self.assertEqual(hl.intersect_line(Line(P0,vL)), hl) # parallel self.assertEqual(hl.intersect_line(Line(Point(0,1),vL)), None) # skew - same P0s self.assertEqual(hl.intersect_line(Line(P0,Vector(0,1))), P0) # skew - different P0s self.assertEqual(hl.intersect_line(Line(Point(0.5,0),Vector(0,1))), Point(0.5,0.5))
def test___init__(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) self.assertIsInstance(l, Line) self.assertEqual(l.P0, P0) self.assertEqual(l.vL, vL)
def test_distance_to_point(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) self.assertEqual(l.distance_to_point(P0), 0) self.assertEqual(l.distance_to_point(P0 + vL), 0) self.assertEqual(l.distance_to_point(P0 + vL.perp_vector), vL.length) P0, vL = Point(0, 0, 0), Vector(1, 1, 1) l = Line(P0, vL) self.assertEqual(l.distance_to_point(P0), 0) self.assertEqual(l.distance_to_point(P0 + Vector(1, -1, 0)), Vector(1, -1, 0).length)
def test_project_2D(self): "" P0, vL = Point(0, 0, 0), Vector(1, 1, 1) l = Line(P0, vL) self.assertEqual(l.project_2D(0), Line(Point(0, 0), Vector(1, 1))) self.assertEqual(l.project_2D(1), Line(Point(0, 0), Vector(1, 1))) self.assertEqual(l.project_2D(2), Line(Point(0, 0), Vector(1, 1)))
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_calculate_t_from_coordinates(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) self.assertEqual(l.calculate_t_from_coordinates(*(P0 + vL * 2)), 2) self.assertEqual(l.calculate_t_from_coordinates(2, 0), 2) P0, vL = Point(0, 0, 0), Vector(1, 1, 1) l = Line(P0, vL) self.assertEqual(l.calculate_t_from_coordinates(*(P0 + vL * 2)), 2)
def test__intersect_line_skew_2D(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) self.assertEqual(l._intersect_line_skew_2D(Line(P0, vL.perp_vector)), P0) self.assertEqual( l._intersect_line_skew_2D(Line(P0 + vL, vL.perp_vector)), P0 + vL)
def test_calculate_point(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) self.assertEqual(l.calculate_point(2), P0 + vL * 2) P0, vL = Point(0, 0, 0), Vector(1, 1, 1) l = Line(P0, vL) self.assertEqual(l.calculate_point(2), P0 + vL * 2)
def test__intersect_line_skew_3D(self): "" P0, vL = Point(0, 0, 0), Vector(1, 1, 1) l = Line(P0, vL) self.assertEqual(l._intersect_line_skew_3D(Line(P0, Vector(1, -1, 0))), P0) self.assertEqual( l._intersect_line_skew_3D(Line(Point(0, 0, 1), Vector(1, -1, 0))), None)
def test_intersect_line(self): "" pl = Plane(P0, N) # line in plane self.assertEqual(pl.intersect_line(Line(P0, Vector(1, 0, 0))), Line(P0, Vector(1, 0, 0))) # parallel line not in plane self.assertEqual(pl.intersect_line(Line(P0 + N, Vector(1, 0, 0))), None) # perpendicular line passing through P0 self.assertEqual(pl.intersect_line(Line(P0, N)), P0) self.assertEqual(pl.intersect_line(Line(P0 + N, N)), P0) # non perpendicular line not passing through P0 self.assertEqual( pl.intersect_line(Line(Point(0, 0, 1), Vector(1, 0, -1))), Point(1, 0, 0))
def test___eq__(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) self.assertTrue(l == Line(P0, vL * 2)) self.assertTrue(l == Line(P0, vL.opposite)) self.assertTrue(l == Line(P0 + vL, vL)) self.assertFalse(l == Line(P0, vL.perp_vector)) self.assertFalse(l == Line(P0 + vL.perp_vector, vL)) P0, vL = Point(0, 0, 0), Vector(1, 1, 1) l = Line(P0, vL) self.assertTrue(l == Line(P0, vL * 2)) self.assertTrue(l == Line(P0, vL.opposite)) self.assertTrue(l == Line(P0 + vL, vL)) self.assertFalse(l == Line(P0, Vector(1, -1, 0))) self.assertFalse(l == Line(P0 + Vector(1, -1, 0), vL))
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_plot(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) fig, ax = plt.subplots() l.plot(ax)
def test_is_parallel(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) self.assertTrue(l.is_parallel(l)) self.assertTrue(l.is_parallel(Line(P0, vL.opposite))) self.assertTrue(l.is_parallel(Line(P0 + vL.perp_vector, vL))) P0, vL = Point(0, 0, 0), Vector(1, 1, 1) l = Line(P0, vL) self.assertTrue(l.is_parallel(l)) self.assertTrue(l.is_parallel(Line(P0, vL.opposite))) self.assertTrue(l.is_parallel(Line(P0 + Vector(1, -1, 0), vL)))
def test_intersect_line(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) # collinear self.assertEqual(l.intersect_line(l), l) # parallel self.assertEqual(l.intersect_line(Line(P0 + vL.perp_vector, vL)), None) # skew self.assertEqual(l.intersect_line(Line(P0, vL.perp_vector)), P0) # skew - different P0s self.assertEqual(l.intersect_line(Line(P0 + vL, vL.perp_vector)), P0 + vL) P0, vL = Point(0, 0, 0), Vector(1, 1, 1) l = Line(P0, vL) # collinear self.assertEqual(l.intersect_line(l), l) # parallel self.assertEqual(l.intersect_line(Line(P0 + Vector(1, -1, 0), vL)), None) #skew self.assertEqual(l.intersect_line(Line(P0, Vector(1, -1, 0))), P0) self.assertEqual(l.intersect_line(Line(P0 + vL, Vector(1, -1, 0))), P0 + vL) self.assertEqual( l.intersect_line(Line(Point(0, 0, 1), Vector(1, -1, 0))), None)
def test_distance_to_line(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) self.assertEqual(l.distance_to_line(l), 0) self.assertEqual(l.distance_to_line(Line(P0 + vL.perp_vector, vL)), vL.length) self.assertEqual(l.distance_to_line(Line(P0, vL.perp_vector)), 0) P0, vL = Point(0, 0, 0), Vector(1, 1, 1) l = Line(P0, vL) self.assertEqual(l.distance_to_line(l), 0) self.assertEqual(l.distance_to_line(Line(P0 + Vector(1, -1, 0), vL)), Vector(1, -1, 0).length) self.assertEqual(l.distance_to_line(Line(P0, Vector(1, -1, 0))), 0) self.assertEqual( Line(Point(0, 0, 0), Vector(1, 0, 0)).distance_to_line( Line(Point(0, 0, 1), Vector(0, 1, 0))), 1)