示例#1
0
文件: Parser.py 项目: stuydw/3d
 def parse(self, screen, fill_color, line_color):
     screen.fill(fill_color)
     m = Edge_Matrix()
     t = m.get_identity()
     i = 0
     while i < len(self.lines):
         if self.lines[i] == "c":
             i += 1
             m.add_circle(*self.lines[i])
         elif self.lines[i] == "p":
             i += 1
             m.add_rectangular_prism(*self.lines[i])
         elif self.lines[i] == "m":
             i += 1
             m.add_sphere(*self.lines[i])
         elif self.lines[i] == "d":
             i += 1
             m.add_torus(*self.lines[i])
         elif self.lines[i] == "h":
             i += 1
             args = self.lines[i]
             m.add_curve(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], 0)
         elif self.lines[i] == "b":
             i += 1
             args = self.lines[i]
             m.add_curve(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], 1)
         elif self.lines[i] == "l":
             i += 1
             m.add_edge(*self.lines[i])
         elif self.lines[i] == "i":
             t = m.get_identity()
         elif self.lines[i] == "s":
             i += 1
             #print("s", self.lines[i])
             sm = m.get_scale_matrix(*self.lines[i])
             #print(sm)
             t = t.matrix_mult(sm)
             #print(t)
         elif self.lines[i] == "t":
             i += 1
             tm = m.get_translation_matrix(*self.lines[i])
             t = t.matrix_mult(tm)
         elif self.lines[i] == "x":
             i += 1
             xr = m.get_rotation_matrix_x(*self.lines[i])
             t = t.matrix_mult(xr)
         elif self.lines[i] == "y":
             i += 1
             yr = m.get_rotation_matrix_y(*self.lines[i])
             t = t.matrix_mult(yr)
         elif self.lines[i] == "z":
             i += 1
             zr = m.get_rotation_matrix_z(*self.lines[i])
             t = t.matrix_mult(zr)
         elif self.lines[i] == "a":
             #print(str(m) + "\n")
             #print(str(t) + "\n")
             m = m.matrix_mult_by(t)
             #print(str(m) + "\n")
         elif self.lines[i] == "v":
             temp_name = "/var/tmp/temp.ppm"
             with open(temp_name, "w") as f:
                 draw_matrix(m, screen, line_color)
                 f.write(screen.gen_ppm())
             os.system("display %s" %(temp_name, ))
             os.remove(temp_name)
             screen.fill(fill_color)
         elif self.lines[i] == "g":
             i += 1
             with open(self.lines[i], "w") as f:
                 draw_matrix(m, screen, line_color)
                 f.write(screen.gen_ppm())
             os.system("convert %s %s" %(str(self.lines[i]), str(self.lines[i])))
             screen.fill(fill_color)
         elif self.lines[i] == "q":
             return
         else:
             print("line %i: %s" %(i, str(self.lines[i])))
             raise Exception
         i += 1