ldraw.py

Sun, 10 Dec 2017 15:45:50 +0200

author
Santeri Piippo
date
Sun, 10 Dec 2017 15:45:50 +0200
changeset 1
5411a25cfca7
parent 0
55b4c97d44c5
child 2
50d3086070df
permissions
-rwxr-xr-x

Parsing function complete

0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
1 #!/usr/bin/env python3
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
2 import linetypes
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
3 import re
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
4 from geometry import *
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
5
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
6 class BadLdrawLine(Exception):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
7 pass
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
8
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
9 def parse_ldraw_code(line):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
10 line = line.strip()
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
11 if not line:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
12 return linetypes.EmptyLine()
1
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
13 elif line == '0':
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
14 return linetypes.Comment('')
1
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
15 elif line.startswith('0 '):
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
16 return linetypes.Comment(line[2:].strip())
1
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
17 elif line.startswith('1 '):
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
18 return parse_ldraw_subfile_reference(line)
1
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
19 elif line.startswith('2 '):
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
20 return parse_ldraw_line(line)
1
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
21 elif line.startswith('3 '):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
22 return parse_ldraw_triangle(line)
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
23 elif line.startswith('4 '):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
24 return parse_ldraw_quadrilateral(line)
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
25 elif line.startswith('5 '):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
26 return parse_ldraw_contour(line)
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
27 else:
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
28 raise BadLdrawLine('unknown line type')
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
29
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
30 def parse_ldraw_subfile_reference(line):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
31 pattern = r'^1\s+(\d+)' + r'\s+([^ ]+)' * (3 + 9 + 1) + r'\s*$'
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
32 match = re.search(pattern, line)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
33 if not match:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
34 raise BadLdrawLine('unable to parse')
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
35 groups = list(match.groups())
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
36 indices = {
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
37 'color': 0,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
38 'anchor': slice(1, 4),
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
39 'matrix': slice(4, 13),
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
40 'subfile_path': 13
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
41 }
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
42 try:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
43 color = int(groups[indices['color']])
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
44 vertex_values = [float(x) for x in groups[indices['anchor']]]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
45 matrix_values = [float(x) for x in groups[indices['matrix']]]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
46 except ValueError:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
47 raise BadLdrawLine('bad numeric values')
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
48 return linetypes.SubfileReference(
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
49 color = color,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
50 anchor = Vertex(*vertex_values),
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
51 matrix = TransformationMatrix(matrix_values),
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
52 subfile_path = groups[indices['subfile_path']]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
53 )
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
54
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
55 def generic_parse_polygon(line, *, type_code, vertex_count):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
56 pattern = r'^' \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
57 + str(type_code) \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
58 + '\s+(\d+)' \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
59 + r'\s+([^ ]+)' * (vertex_count * 3) \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
60 + r'\s*$'
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
61 match = re.search(pattern, line)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
62 if not match:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
63 raise BadLdrawLine(str.format('cannot parse type-{} line', type_code))
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
64 vertices = []
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
65 for vertex_index in range(vertex_count):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
66 slice_begin = 1 + vertex_index * 3
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
67 slice_end = 1 + (vertex_index + 1) * 3
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
68 coordinates = match.groups()[slice_begin:slice_end]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
69 assert(len(coordinates) == 3)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
70 try:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
71 coordinates = [float(x) for x in coordinates]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
72 except ValueError:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
73 raise BadLdrawLine('bad numeric values')
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
74 vertices.append(Vertex(*coordinates))
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
75 return {
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
76 'color': int(match.group(1)),
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
77 'vertices': vertices,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
78 }
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
79
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
80 def parse_ldraw_line(line):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
81 parse_result = generic_parse_polygon(line, type_code = 2, vertex_count = 2)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
82 return linetypes.LineSegment(
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
83 color = parse_result['color'],
1
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
84 geometry = LineSegment(*parse_result['vertices']),
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
85 )
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
86
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
87 def parse_ldraw_triangle(line):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
88 parse_result = generic_parse_polygon(line, type_code = 3, vertex_count = 3)
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
89 return linetypes.Triangle(
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
90 color = parse_result['color'],
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
91 geometry = Polygon(parse_result['vertices']),
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
92 )
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
93
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
94 def parse_ldraw_quadrilateral(line):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
95 parse_result = generic_parse_polygon(line, type_code = 4, vertex_count = 4)
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
96 return linetypes.Quadrilateral(
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
97 color = parse_result['color'],
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
98 geometry = Polygon(parse_result['vertices']),
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
99 )
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
100
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
101 def parse_ldraw_contour(line):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
102 parse_result = generic_parse_polygon(line, type_code = 5, vertex_count = 4)
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
103 return linetypes.Contour(
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
104 color = parse_result['color'],
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
105 geometry = LineSegment(*parse_result['vertices'][0:2]),
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
106 control_points = parse_result['vertices'][2:],
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
107 )
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
108
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
109 def read_ldraw(file, *, libraries):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
110 result = list()
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
111 for line in file:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
112 result.append(parse_ldraw_code(line))
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
113 return result
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
114
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
115 if __name__ == '__main__':
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
116 from sys import argv
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
117 libraries = [{'path': '/home/teemu/ldraw', 'role': 'official'}]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
118 with open(argv[1], 'r') as file:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
119 model = read_ldraw(file, libraries = libraries)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
120 from pprint import pprint
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
121 pprint(model)

mercurial