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___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_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_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_distance_to_point(self): "" pt1 = Point(0, 0) pt2 = Point(1, 0) self.assertEqual(pt1.distance_to_point(pt2), 1) pt1 = Point(0, 0, 0) pt2 = Point(1, 0, 0) self.assertEqual(pt1.distance_to_point(pt2), 1)
def test_plot(self): "" return pg = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) pg.plot() pg = Polygon(Point(0, 0, 0), Point(1, 0, 0), Point(1, 1, 0), Point(0, 1, 0)) pg.plot()
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___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_plot(self): "" P0 = Point(0, 0, 0) N = Vector(1, 0, 0) pl = Plane(P0, N) import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') pl.plot(ax)
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_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)
def test___init__(self): "" pt=Point(0,0) pts=Points(pt) self.assertIsInstance(pts, Points) self.assertEqual(pts[0], pt) self.assertEqual(len(pts), 1) pt1=Point(1,1) pts.append(pt1) self.assertEqual(pts[1], pt1) del pts[0] self.assertEqual(pts[0], pt1)
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_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_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_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_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___eq__(self): "" pg = Polygon(Point(0, 0), Point(1, 0), Point(1, 1)) pg1 = Polygon(Point(2, 0), Point(1, 0), Point(1, 1)) pgs = Polygons(pg, pg1) self.assertEqual(pgs, pgs) self.assertFalse(pgs == Polygons(pg))
def test_distance_to_point(self): "" P0,vL=Point(0,0),Vector(1,1) hl=Halfline(P0,vL) self.assertEqual(hl.distance_to_point(P0), 0) self.assertEqual(hl.distance_to_point(P0-vL), vL.length) self.assertEqual(hl.distance_to_point(P0+vL), 0) self.assertEqual(hl.distance_to_point(P0+Vector(1,-1)), Vector(1,-1).length) P0,vL=Point(0,0,0),Vector(1,1,1) hl=Halfline(P0,vL) self.assertEqual(hl.distance_to_point(P0), 0) self.assertEqual(hl.distance_to_point(P0-vL), vL.length) self.assertEqual(hl.distance_to_point(P0+vL), 0) self.assertEqual(hl.distance_to_point(P0+Vector(1,-1,0)), Vector(1,-1,0).length)
def test__shapely(self): "" pgs = Polygons(Polygon(Point(0, 0), Point(1, 0), Point(1, 1)), Polygon(Point(2, 0), Point(1, 0), Point(1, 1))) #print(pgs._shapely); return self.assertEqual( pgs._shapely, shapely.geometry.MultiPolygon([ shapely.geometry.Polygon(((0, 0), (1, 0), (1, 1))), shapely.geometry.Polygon(((2, 0), (1, 0), (1, 1))) ]))
def test_polylines(self): "" ph = Polyhedron( Polygon(Point(0, 1, 0), Point(1, 1, 0), Point(0, 0, 0)), Polygon(Point(0, 0, 0), Point(1, 1, 0), Point(0, 1, 1)), Polygon(Point(0, 1, 1), Point(0, 1, 0), Point(0, 0, 0)), Polygon(Point(1, 1, 0), Point(0, 1, 0), Point(0, 1, 1))) self.assertEqual( ph.polylines, Polylines(Polyline(Point(0.0, 1.0, 0.0), Point(1.0, 1.0, 0.0)), Polyline(Point(1.0, 1.0, 0.0), Point(0.0, 0.0, 0.0)), Polyline(Point(0.0, 0.0, 0.0), Point(0.0, 1.0, 0.0)), Polyline(Point(1.0, 1.0, 0.0), Point(0.0, 1.0, 1.0)), Polyline(Point(0.0, 1.0, 1.0), Point(0.0, 0.0, 0.0)), Polyline(Point(0.0, 1.0, 1.0), Point(0.0, 1.0, 0.0))))
def test_plot(self): "" return pls = Polylines(Polyline(Point(0.5, 0.5), Point(1.5, 0.5)), Polyline(Point(0.5, 1.5), Point(1.5, 1.5))) pls.plot()
def test_plot(self): "" return pgs = Polygons(Polygon(Point(0, 0), Point(1, 0), Point(1, 1)), Polygon(Point(2, 0), Point(2, 1), Point(1, 0))) pgs.plot() pgs = Polygons(Polygon(Point(0, 0, 0), Point(1, 0, 0), Point(1, 1, 0)), Polygon(Point(0, 0, 1), Point(0, 1, 1), Point(1, 1, 1))) pgs.plot()
def test___init__(self): "" ph = Polyhedron( Polygon(Point(0, 1, 0), Point(1, 1, 0), Point(0, 0, 0)), Polygon(Point(0, 0, 0), Point(1, 1, 0), Point(0, 1, 1)), Polygon(Point(0, 1, 1), Point(0, 1, 0), Point(0, 0, 0)), Polygon(Point(1, 1, 0), Point(0, 1, 0), Point(0, 1, 1))) self.assertIsInstance(ph, Polyhedron)
def test_plot(self): "" P0, vL = Point(0, 0), Vector(1, 1) l = Line(P0, vL) fig, ax = plt.subplots() l.plot(ax)
# 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_plot(self): "" P0,vL=Point(0,0),Vector(-1,1) hl=Halfline(P0,vL) fig, ax = plt.subplots() hl.plot(ax)
def test_polyhedron_from_base_polygon_and_extrud_vector(self): "" ph = polyhedron_from_base_polygon_and_extrud_vector( Polygon(Point(0, 0, 0), Point(1, 0, 0), Point(1, 1, 0), Point(0, 1, 0)), Vector(0, 0, 1)) self.assertEqual( ph.polygons, Polygons( Polygon(Point(0, 1, 0), Point(1, 1, 0), Point(1, 0, 0), Point(0, 0, 0)), Polygon(Point(0, 0, 1), Point(1, 0, 1), Point(1, 1, 1), Point(0, 1, 1)), Polygon(Point(0, 1, 1), Point(1, 1, 1), Point(1, 1, 0), Point(0, 1, 0)), Polygon(Point(1, 1, 1), Point(1, 0, 1), Point(1, 0, 0), Point(1, 1, 0)), Polygon(Point(1, 0, 1), Point(0, 0, 1), Point(0, 0, 0), Point(1, 0, 0)), Polygon(Point(0, 0, 1), Point(0, 1, 1), Point(0, 1, 0), Point(0, 0, 0)))) self.assertEqual(len(ph.polygons[0].triangles), 2) self.assertEqual(len(ph.tetrahedrons), 6)
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_tetrahedron_from_points(self): "" ph = tetrahedron_from_points(Point(0, 0, 0), Point(1, 1, 0), Point(0, 1, 0), Point(0, 1, 1)) self.assertIsInstance(ph, Polyhedron) self.assertEqual( ph.polygons, Polygons(Polygon(Point(0, 1, 0), Point(1, 1, 0), Point(0, 0, 0)), Polygon(Point(0, 0, 0), Point(1, 1, 0), Point(0, 1, 1)), Polygon(Point(0, 1, 1), Point(0, 1, 0), Point(0, 0, 0)), Polygon(Point(1, 1, 0), Point(0, 1, 0), Point(0, 1, 1))))