def test_add_ellipse(): from ezdxf.math import ConstructionEllipse ellipse = ConstructionEllipse(center=(3, 0), major_axis=(1, 0), ratio=0.5, start_param=0, end_param=math.pi) path = Path() tools.add_ellipse(path, ellipse) assert path.start == (4, 0) assert path.end == (2, 0) # set start point to end of ellipse path = Path(start=(2, 0)) # add reversed ellipse, by default the start of # an empty path is set to the ellipse start tools.add_ellipse(path, ellipse, reset=False) assert path.start == (2, 0) assert path.end == (4, 0) path = Path() # add a line segment from (0, 0) to start of ellipse tools.add_ellipse(path, ellipse, reset=False) assert path.start == (0, 0) assert path.end == (2, 0)
def test_append_empty_path(): path = Path((1, 0, 0)) path.line_to((2, 0, 0)) start = path.start end = path.end path.append_path(Path()) assert start == path.start and end == path.end, "path should be unchanged"
def test_to_multi_path_ignores_empty_paths(): p0 = Path((1, 0, 0)) p0.line_to((2, 0, 0)) empty = Path((100, 0, 0)) path = to_multi_path([p0, empty]) assert len(path) == 1 assert path.has_sub_paths is False assert path.end.isclose((2, 0, 0))
def test_append_path_with_a_gap(): p1 = Path((1, 0, 0)) p1.line_to((2, 0, 0)) p2 = Path((3, 0, 0)) p2.line_to((4, 0, 0)) p1.append_path(p2) assert p1.start == (1, 0) assert p1.end == (4, 0) assert len(p1) == 3
def test_extend_path_by_another_single_path(): path = Path((1, 0, 0)) path.line_to((2, 0, 0)) p1 = Path((3, 0, 0)) p1.line_to((4, 0, 0)) path.extend_multi_path(p1) assert path.has_sub_paths is True assert path.start == (1, 0, 0) assert path.end == (4, 0, 0)
def test_extend_path_by_another_none_empty_path(): p0 = Path((1, 0, 0)) p0.line_to((2, 0, 0)) p1 = Path((3, 0, 0)) p1.line_to((3, 0, 0)) p0.extend_multi_path(p1) assert p0.has_sub_paths is True assert p0.start == (1, 0, 0) assert p0.end == (3, 0, 0)
def test_to_multi_path(): p0 = Path((1, 0, 0)) p0.line_to((2, 0, 0)) p0.move_to((3, 0, 0)) # will be replaced by move_to(4, 0, 0) p1 = Path((4, 0, 0)) p1.line_to((5, 0, 0)) p1.move_to((6, 0, 0)) path = to_multi_path([p0, p1]) assert path.has_sub_paths is True assert path.start == (1, 0, 0) assert path.end == (6, 0, 0) assert path[1].type == Command.MOVE_TO assert path[1].end == (4, 0, 0)
def test_multiple_segments(self): p = Path() p.curve4_to((4, 0), (1, 2), (3, 2)) p.line_to((6, 0)) p.curve3_to((8, 0), (7, 1)) result = list(to_bsplines_and_vertices(p)) assert len(result) == 3, "expected three segments"
def test_for_very_short_line_segments(self, start, delta): path = Path((start, 0, 0)) path.line_to((start + delta, 0, 0)) path = lines_to_curve4(path) assert len(path) == 1 assert path[0].type == Command.CURVE4_TO assert len(list(path.flattening(1))) > 3
def test_two_paths_one_command(self): path_a = Path() path_a.line_to((1, 0)) path_b = Path((2, 0)) path_b.line_to((3, 0)) result = transform_paths([path_a, path_b], Matrix44()) path0 = result[0] assert path0[0].type == Command.LINE_TO assert path0.start == (0, 0) assert path0.end == (1, 0) path1 = result[1] assert path1[0].type == Command.LINE_TO assert path1.start == (2, 0) assert path1.end == (3, 0)
def test_close_last_sub_path(self): p = Path() p.line_to((1, 0, 0)) p.move_to((2, 0, 0)) p.line_to((3, 0, 0)) p.close_sub_path() assert p.end == (2, 0, 0)
def test_add_curves4(): path = Path() c1 = Bezier4P(((0, 0), (0, 1), (2, 1), (2, 0))) c2 = Bezier4P(((2, 0), (2, -1), (0, -1), (0, 0))) tools.add_bezier4p(path, [c1, c2]) assert len(path) == 2 assert path.end == (0, 0)
def test_move_to_creates_a_multi_path_object(self): path = Path(start=(1, 0, 0)) path.line_to((2, 0, 0)) path.move_to((3, 0, 0)) assert len(path) == 2, "should add a MOVETO cmd as last cmd" assert path.has_sub_paths is True, "should be a multi path object" assert path.end.isclose((3, 0, 0)), "should end at the MOVETO location"
def test_add_curves4_with_gap(): path = Path() c1 = Bezier4P(((0, 0, 0), (0, 1, 0), (2, 1, 0), (2, 0, 0))) c2 = Bezier4P(((2, -1, 0), (2, -2, 0), (0, -2, 0), (0, -1, 0))) tools.add_bezier4p(path, [c1, c2]) assert len(path) == 3 # added a line segment between curves assert path.end == (0, -1, 0)
def test_single_paths_from_a_multi_path_object(): p = Path((1, 0, 0)) p.line_to((2, 0, 0)) # 1st sub-path p.move_to((3, 0, 0)) # 2nd sub-path p.line_to((4, 0, 0)) p.move_to((5, 0, 0)) # 3rd sub-path paths = list(single_paths([p])) assert len(paths) == 3
def test_does_nothing_if_last_sub_path_is_empty(self): p = Path() p.line_to((1, 0, 0)) p.move_to((2, 0, 0)) assert len(p) == 2 p.close_sub_path() assert len(p) == 2 assert p.end == (2, 0, 0)
def test_one_path_line_to(self): path = Path() path.line_to((1, 0)) result = transform_paths([path], Matrix44()) path0 = result[0] assert path0[0].type == Command.LINE_TO assert path0.start == (0, 0) assert path0.end == (1, 0)
def test_to_ocs(self): p = Path((0, 1, 1)) p.line_to((0, 1, 3)) ocs = OCS((1, 0, 0)) # x-Axis result = list(transform_paths_to_ocs([p], ocs)) p0 = result[0] assert ocs.from_wcs((0, 1, 1)) == p0.start assert ocs.from_wcs((0, 1, 3)) == p0[0].end
def test_multiple_first_move_to(self): path = Path(start=(1, 0, 0)) path.move_to((2, 0, 0)) path.move_to((3, 0, 0)) path.move_to((4, 0, 0)) assert path.start.isclose((4, 0, 0)), "should reset the start point" assert len(path) == 0, "should not add a MOVETO cmd as first cmd" assert path.has_sub_paths is False
def test_control_vertices(p1): vertices = list(p1.control_vertices()) assert vertices == Vec3.list([(0, 0), (2, 0), (2, 1), (4, 1), (4, 0), (5, -1), (6, 0)]) path = Path() assert len(list(path.control_vertices())) == 0 path = converter.from_vertices([(0, 0), (1, 0)]) assert len(list(path.control_vertices())) == 2
def test_approximate_lines(): path = Path() path.line_to((1, 1)) path.line_to((2, 0)) vertices = list(path.approximate()) assert len(vertices) == 3 assert vertices[0] == path.start == (0, 0) assert vertices[2] == path.end == (2, 0)
def test_approximate_curves(): path = Path() path.curve3_to((2, 0), (1, 1)) path.curve4_to((3, 0), (2, 1), (3, 1)) vertices = list(path.approximate(20)) assert len(vertices) == 41 assert vertices[0] == (0, 0) assert vertices[-1] == (3, 0)
def test_sub_paths_from_single_path_object(self): path = Path(start=(1, 2, 3)) paths = list(path.sub_paths()) assert len(paths) == 1 s0 = paths[0] assert s0.start == (1, 2, 3) assert s0.end == (1, 2, 3) assert s0.has_sub_paths is False assert len(s0) == 0
def test_control_vertices(p1): vertices = list(p1.control_vertices()) assert close_vectors(vertices, [(0, 0), (2, 0), (2, 1), (4, 1), (4, 0), (5, -1), (6, 0)]) path = Path() assert len(list(path.control_vertices())) == 0 assert list(path.control_vertices()) == list(path.approximate(2)) path = converter.from_vertices([(0, 0), (1, 0)]) assert len(list(path.control_vertices())) == 2
def test_multi_path_to_lwpolylines(self): path = Path() path.line_to((1, 0, 0)) path.move_to((2, 0, 0)) path.line_to((3, 0, 0)) polylines = list(to_lwpolylines(path)) assert len(polylines) == 2 assert len(polylines[0]) == 2 assert len(polylines[1]) == 2
def test_which_length_is_too_short_to_create_a_curve(self, start, delta): path = Path((start, 0, 0)) path.line_to((start + delta, 0, 0)) path = lines_to_curve4(path) assert len(path) == 1 assert ( path[0].type == Command.LINE_TO ), "should not remove a single line segment representing a point" assert len(list(path.flattening(1))) == 2
def test_sub_paths_inherit_parent_user_data(): path = Path() path.user_data = "data" path.line_to((1, 2, 3)) path.move_to((7, 8, 9)) path.line_to((7, 8, 9)) assert path.has_sub_paths is True for p in path.sub_paths(): assert p.user_data == "data"
def test_one_path_curve4_to(self): path = Path() path.curve4_to((2, 0), (0, 1), (2, 1)) result = transform_paths([path], Matrix44()) path0 = result[0] assert path0[0].type == Command.CURVE4_TO assert len(path0[0]) == 3 assert path0.start == (0, 0) assert path0.end == (2, 0)
def test_remove_line_segments_of_zero_length_at_the_end(self): # CURVE3_TO and CURVE4_TO can not process zero length segments path = Path() path.line_to((1, 0)) path.line_to((1, 0)) # line segment of length==0 should be removed path = lines_to_curve4(path) assert len(path) == 1 assert path.start == (0, 0) assert path[0].type == Command.CURVE4_TO assert path[0].end == (1, 0)
def test_has_clockwise_orientation(): # basic has_clockwise_orientation() function is tested in: # test_617_clockwise_orientation path = converter.from_vertices([(0, 0), (1, 0), (1, 1), (0, 1)]) assert path.has_clockwise_orientation() is False path = Path() path.line_to((2, 0)) path.curve4_to((4, 0), (2, 1), (4, 1)) # end, ctrl1, ctrl2 assert path.has_clockwise_orientation() is True