Sun, 10 Dec 2017 15:45:50 +0200
Parsing function complete
geometry.py | file | annotate | diff | comparison | revisions | |
ldraw.py | file | annotate | diff | comparison | revisions | |
linetypes.py | file | annotate | diff | comparison | revisions |
--- a/geometry.py Sun Dec 10 15:37:26 2017 +0200 +++ b/geometry.py Sun Dec 10 15:45:50 2017 +0200 @@ -18,6 +18,12 @@ def __repr__(self): return str.format('LineSegment({!r}, {!r})', self.v1, self.v2) +class Polygon: + def __init__(self, vertices): + self.vertices = vertices + def __repr__(self): + return str.format('Polygon({!r})', self.vertices) + def is_real(number): return isinstance(number, int) or isinstance(number, float)
--- a/ldraw.py Sun Dec 10 15:37:26 2017 +0200 +++ b/ldraw.py Sun Dec 10 15:45:50 2017 +0200 @@ -10,15 +10,22 @@ line = line.strip() if not line: return linetypes.EmptyLine() - if line == '0': + elif line == '0': return linetypes.Comment('') - if line.startswith('0 '): + elif line.startswith('0 '): return linetypes.Comment(line[2:].strip()) - if line.startswith('1 '): + elif line.startswith('1 '): return parse_ldraw_subfile_reference(line) - if line.startswith('2 '): + elif line.startswith('2 '): return parse_ldraw_line(line) - ... + elif line.startswith('3 '): + return parse_ldraw_triangle(line) + elif line.startswith('4 '): + return parse_ldraw_quadrilateral(line) + elif line.startswith('5 '): + return parse_ldraw_contour(line) + else: + raise BadLdrawLine('unknown line type') def parse_ldraw_subfile_reference(line): pattern = r'^1\s+(\d+)' + r'\s+([^ ]+)' * (3 + 9 + 1) + r'\s*$' @@ -72,10 +79,32 @@ def parse_ldraw_line(line): parse_result = generic_parse_polygon(line, type_code = 2, vertex_count = 2) - line_segment = LineSegment(*parse_result['vertices']) return linetypes.LineSegment( color = parse_result['color'], - geometry = line_segment) + geometry = LineSegment(*parse_result['vertices']), + ) + +def parse_ldraw_triangle(line): + parse_result = generic_parse_polygon(line, type_code = 3, vertex_count = 3) + return linetypes.Triangle( + color = parse_result['color'], + geometry = Polygon(parse_result['vertices']), + ) + +def parse_ldraw_quadrilateral(line): + parse_result = generic_parse_polygon(line, type_code = 4, vertex_count = 4) + return linetypes.Quadrilateral( + color = parse_result['color'], + geometry = Polygon(parse_result['vertices']), + ) + +def parse_ldraw_contour(line): + parse_result = generic_parse_polygon(line, type_code = 5, vertex_count = 4) + return linetypes.Contour( + color = parse_result['color'], + geometry = LineSegment(*parse_result['vertices'][0:2]), + control_points = parse_result['vertices'][2:], + ) def read_ldraw(file, *, libraries): result = list()
--- a/linetypes.py Sun Dec 10 15:37:26 2017 +0200 +++ b/linetypes.py Sun Dec 10 15:45:50 2017 +0200 @@ -44,3 +44,14 @@ class Quadrilateral(LineSegment): pass + +class Contour(LineSegment): + def __init__(self, *, color, geometry, control_points): + super().__init__(color = color, geometry = geometry) + self.control_points = control_points + assert(len(self.control_points) == 2) + def __repr__(self): + return str.format('linetypes.Contour(' \ + 'color = {color!r}, ' \ + 'geometry = {geometry!r}, ' \ + 'control_points = {control_points!r})', **self.__dict__)