Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import random
- from tkinter import *
- import numpy as np
- import math
- from _operator import matmul
- WIDTH = 400 # width of canvas
- HEIGHT = 400 # height of canvas
- HPSIZE = 1.5 # double of point size (must be integer)
- COLOR = "#0000FF" # blue
- angle = math.pi / 20 # Winkel fuer Drehung
- NOPOINTS = 1000
- pointList = [] # list of points (used by Canvas.delete(...))
- def quit(root=None):
- """ quit programm """
- if root==None:
- sys.exit(0)
- root._root().quit()
- root._root().destroy()
- def draw():
- """ draw points """
- for point in can.find_all():
- can.delete(point)
- for point in viewPortTransfo(pointList):
- x,y = point
- can.create_oval(x-HPSIZE, y-HPSIZE, x+HPSIZE, y+HPSIZE, fill=COLOR, outline=COLOR)
- def viewPortTransfo(pointList):
- # Camera und viewport aspect ratio gleich - keine verzerrung
- return [[x[0] * WIDTH/2 + WIDTH/2 ,
- HEIGHT - (x[1] * HEIGHT/2.0 + HEIGHT/2.0)]
- for x in pointList]
- def rotYp():
- """ rotate counterclockwise around y axis """
- global angle
- global pointList
- pointList = rotate(angle, pointList)
- draw()
- def rotYn():
- """ rotate clockwise around y axis """
- global angle
- global pointList
- pointList = rotate(-angle, pointList)
- draw()
- def rotate(alpha,points):
- yaw = np.array( [ [np.cos(alpha),0,np.sin(alpha),0],
- [0,1,0,0],
- [-np.sin(alpha),0, np.cos(alpha),0],
- [0,0,0,1]
- ])
- yawned = [np.matmul(yaw,x) for x in points]
- return yawned
- def orthographic_projection(lst):
- x_l = min([x[0] for x in lst])
- y_b = min([x[1] for x in lst])
- N = min([x[2] for x in lst])
- x_r = max([x[0] for x in lst])
- y_t = max([x[1] for x in lst])
- F = max([x[2] for x in lst])
- trans_matrix = np.array([ [ 2.0/(x_r-x_l) , 0 , 0 , (-1)*((x_r + x_l) / (x_r-x_l)) ],
- [ 0, 2.0/(y_t-y_b), 0, (-1)*((y_t+y_b)/(y_t-y_b)) ],
- [ 0, 0, (-2.0)/(F-N), (-1)*((F+N)/ (F-N) ) ],
- [ 0, 0, 0, 1 ]
- ])
- trans_ls = [np.matmul(trans_matrix,x) for x in lst]
- res = [x/x[3] for x in trans_ls]
- return res
- if __name__ == "__main__":
- #check parameters
- if len(sys.argv) == 1:
- print("pointViewerTemplate.py")
- sys.exit(-1)
- print("Dateiname: ", sys.argv[1])
- file = open(sys.argv[1]).readlines()
- lst = []
- for i in file: lst.append(list(map(float,i.split())))
- for ele in lst: np.array(ele.append(1))
- pointList = orthographic_projection(lst)
- for ele in pointList[:3]:
- print(ele)
- # create main window
- mw = Tk()
- # create and position canvas and buttons
- cFr = Frame(mw, width=WIDTH, height=HEIGHT, relief="sunken", bd=1)
- cFr.pack(side="top")
- can = Canvas(cFr, width=WIDTH, height=HEIGHT)
- can.pack()
- bFr = Frame(mw)
- bFr.pack(side="left")
- bRotYn = Button(bFr, text="<-", command=rotYn)
- bRotYn.pack(side="left")
- bRotYp = Button(bFr, text="->", command=rotYp)
- bRotYp.pack(side="left")
- eFr = Frame(mw)
- eFr.pack(side="right")
- bExit = Button(eFr, text="Quit", command=(lambda root=mw: quit(root)))
- bExit.pack()
- # draw points
- draw()
- # start
- mw.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement