-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
176 lines (151 loc) · 6.57 KB
/
Copy pathparser.py
File metadata and controls
176 lines (151 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from display import *
from matrix import *
from draw import *
import copy
"""
Goes through the file named filename and performs all of the actions listed in that file.
The file follows the following format:
Every command is a single character that takes up a line
Any command that requires arguments must have those arguments in the second line.
The commands are as follows:
push: push a copy of the current top of the coordinate system stack to the stack
pop: pop off the current top of the coordinate system stack
All the shape commands work as follows:
1) Add the shape to a temporary matrix
2) Multiply that matrix by the current top of the coordinate system stack
3) Draw the shape to the screen
4) Clear the temporary matrix
sphere: add a sphere to the POLYGON matrix -
takes 4 arguemnts (cx, cy, cz, r)
torus: add a torus to the POLYGON matrix -
takes 5 arguemnts (cx, cy, cz, r1, r2)
box: add a rectangular prism to the POLYGON matrix -
takes 6 arguemnts (x, y, z, width, height, depth)
clear: clears the edge and POLYGON matrices
circle: add a circle to the edge matrix -
takes 4 arguments (cx, cy, cz, r)
hermite: add a hermite curve to the edge matrix -
takes 8 arguments (x0, y0, x1, y1, rx0, ry0, rx1, ry1)
bezier: add a bezier curve to the edge matrix -
takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3)
line: add a line to the edge matrix -
takes 6 arguemnts (x0, y0, z0, x1, y1, z1)
ident: set the transform matrix to the identity matrix -
scale: create a scale matrix,
then multiply the transform matrix by the scale matrix -
takes 3 arguments (sx, sy, sz)
move: create a translation matrix,
then multiply the transform matrix by the translation matrix -
takes 3 arguments (tx, ty, tz)
rotate: create a rotation matrix,
then multiply the transform matrix by the rotation matrix -
takes 2 arguments (axis, theta) axis should be x y or z
apply: apply the current transformation matrix to the edge and POLYGON matrices
display: clear the screen, then
draw the lines of the edge and POLYGON matrices to the screen
display the screen
: clear the screen, then
draw the lines of the edge and POLYGON matrices to the screen
save the screen to a file -
takes 1 argument (file name)
quit: end parsing
See the file script for an example of the file format
"""
ARG_COMMANDS = [ 'push', 'pop', 'box', 'sphere', 'torus', 'circle', 'bezier', 'hermite', 'line', 'scale', 'move', 'rotate', 'save' ]
def parse_file( fname, edges, polygons, csystems, screen, color ):
f = open(fname)
lines = f.readlines()
step = 100
step_3d = 20
c = 0
while c < len(lines):
line = lines[c].strip()
#print ':' + line + ':'
if line in ARG_COMMANDS:
c+= 1
args = lines[c].strip().split(' ')
if line == 'push':
t=csystems[len(csystems)-1]
csystems.append(t)
c-=1
elif line == 'pop':
csystems.pop()
c-=1
elif line == 'sphere':
#print 'SPHERE\t' + str(args)
add_sphere(polygons,
float(args[0]), float(args[1]), float(args[2]),
float(args[3]), step_3d)
matrix_mult(csystems[-1],polygons)
draw_polygons(polygons, screen, color)
polygons=[]
elif line == 'torus':
#print 'TORUS\t' + str(args)
add_torus(polygons,
float(args[0]), float(args[1]), float(args[2]),
float(args[3]), float(args[4]), step_3d)
matrix_mult(csystems[-1],polygons)
draw_polygons(polygons, screen, color)
polygons=[]
elif line == 'box':
#print 'BOX\t' + str(args)
add_box(polygons,
float(args[0]), float(args[1]), float(args[2]),
float(args[3]), float(args[4]), float(args[5]))
matrix_mult(csystems[-1],polygons)
draw_polygons(polygons, screen, color)
polygons=[]
elif line == 'circle':
#print 'CIRCLE\t' + str(args)
add_circle(edges,
float(args[0]), float(args[1]), float(args[2]),
float(args[3]), step)
matrix_mult(csystems[-1],edges)
draw_lines(edges, screen, color)
edges=[]
elif line == 'hermite' or line == 'bezier':
#print 'curve\t' + line + ": " + str(args)
add_curve(edges,
float(args[0]), float(args[1]),
float(args[2]), float(args[3]),
float(args[4]), float(args[5]),
float(args[6]), float(args[7]),
step, line)
matrix_mult(csystems[-1],edges)
draw_lines(edges, screen, color)
edges=[]
elif line == 'line':
#print 'LINE\t' + str(args)
add_edge( edges,
float(args[0]), float(args[1]), float(args[2]),
float(args[3]), float(args[4]), float(args[5]) )
matrix_mult(csystems[-1],edges)
draw_lines(edges, screen, color)
edges=[]
elif line == 'scale':
#print 'SCALE\t' + str(args)
t = make_scale(float(args[0]), float(args[1]), float(args[2]))
matrix_mult(csystems[-1], t )
csystems[-1] = t
elif line == 'move':
#print 'MOVE\t' + str(args)
t = make_translate(float(args[0]), float(args[1]), float(args[2]))
matrix_mult(csystems[-1], t )
csystems[-1] = t
elif line == 'rotate':
#print 'ROTATE\t' + str(args)
theta = float(args[1]) * (math.pi / 180)
if args[0] == 'x':
t = make_rotX(theta)
elif args[0] == 'y':
t = make_rotY(theta)
else:
t = make_rotZ(theta)
matrix_mult(csystems[-1], t )
csystems[-1] = t
elif line == 'display' or line == 'save':
if line == 'display':
display(screen)
else:
save_extension(screen, args[0])
c+= 1