Advertisement
Guest User

Untitled

a guest
May 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import sys
  2. import random
  3. from tkinter import *
  4. import numpy as np
  5. import math
  6. from _operator import matmul
  7.  
  8. WIDTH = 400 # width of canvas
  9. HEIGHT = 400 # height of canvas
  10.  
  11. HPSIZE = 1.5 # double of point size (must be integer)
  12. COLOR = "#0000FF" # blue
  13.  
  14. angle = math.pi / 20 # Winkel fuer Drehung
  15. NOPOINTS = 1000
  16.  
  17. pointList = [] # list of points (used by Canvas.delete(...))
  18.  
  19. def quit(root=None):
  20. """ quit programm """
  21. if root==None:
  22. sys.exit(0)
  23. root._root().quit()
  24. root._root().destroy()
  25.  
  26. def draw():
  27. """ draw points """
  28. for point in can.find_all():
  29. can.delete(point)
  30. for point in viewPortTransfo(pointList):
  31. x,y = point
  32. can.create_oval(x-HPSIZE, y-HPSIZE, x+HPSIZE, y+HPSIZE, fill=COLOR, outline=COLOR)
  33.  
  34.  
  35. def viewPortTransfo(pointList):
  36. # Camera und viewport aspect ratio gleich - keine verzerrung
  37. return [[x[0] * WIDTH/2 + WIDTH/2 ,
  38. HEIGHT - (x[1] * HEIGHT/2.0 + HEIGHT/2.0)]
  39. for x in pointList]
  40.  
  41.  
  42. def rotYp():
  43. """ rotate counterclockwise around y axis """
  44. global angle
  45. global pointList
  46. pointList = rotate(angle, pointList)
  47. draw()
  48.  
  49.  
  50. def rotYn():
  51. """ rotate clockwise around y axis """
  52. global angle
  53. global pointList
  54. pointList = rotate(-angle, pointList)
  55. draw()
  56.  
  57. def rotate(alpha,points):
  58.  
  59. yaw = np.array( [ [np.cos(alpha),0,np.sin(alpha),0],
  60. [0,1,0,0],
  61. [-np.sin(alpha),0, np.cos(alpha),0],
  62. [0,0,0,1]
  63. ])
  64.  
  65. yawned = [np.matmul(yaw,x) for x in points]
  66. return yawned
  67.  
  68. def orthographic_projection(lst):
  69.  
  70.  
  71. x_l = min([x[0] for x in lst])
  72. y_b = min([x[1] for x in lst])
  73. N = min([x[2] for x in lst])
  74. x_r = max([x[0] for x in lst])
  75. y_t = max([x[1] for x in lst])
  76. F = max([x[2] for x in lst])
  77.  
  78.  
  79. trans_matrix = np.array([ [ 2.0/(x_r-x_l) , 0 , 0 , (-1)*((x_r + x_l) / (x_r-x_l)) ],
  80. [ 0, 2.0/(y_t-y_b), 0, (-1)*((y_t+y_b)/(y_t-y_b)) ],
  81. [ 0, 0, (-2.0)/(F-N), (-1)*((F+N)/ (F-N) ) ],
  82. [ 0, 0, 0, 1 ]
  83.  
  84.  
  85. ])
  86. trans_ls = [np.matmul(trans_matrix,x) for x in lst]
  87. res = [x/x[3] for x in trans_ls]
  88. return res
  89.  
  90.  
  91.  
  92. if __name__ == "__main__":
  93. #check parameters
  94. if len(sys.argv) == 1:
  95. print("pointViewerTemplate.py")
  96. sys.exit(-1)
  97.  
  98. print("Dateiname: ", sys.argv[1])
  99. file = open(sys.argv[1]).readlines()
  100. lst = []
  101. for i in file: lst.append(list(map(float,i.split())))
  102. for ele in lst: np.array(ele.append(1))
  103. pointList = orthographic_projection(lst)
  104. for ele in pointList[:3]:
  105. print(ele)
  106.  
  107.  
  108.  
  109.  
  110. # create main window
  111. mw = Tk()
  112.  
  113. # create and position canvas and buttons
  114. cFr = Frame(mw, width=WIDTH, height=HEIGHT, relief="sunken", bd=1)
  115. cFr.pack(side="top")
  116. can = Canvas(cFr, width=WIDTH, height=HEIGHT)
  117. can.pack()
  118. bFr = Frame(mw)
  119. bFr.pack(side="left")
  120. bRotYn = Button(bFr, text="<-", command=rotYn)
  121. bRotYn.pack(side="left")
  122. bRotYp = Button(bFr, text="->", command=rotYp)
  123. bRotYp.pack(side="left")
  124. eFr = Frame(mw)
  125. eFr.pack(side="right")
  126. bExit = Button(eFr, text="Quit", command=(lambda root=mw: quit(root)))
  127. bExit.pack()
  128.  
  129. # draw points
  130. draw()
  131.  
  132. # start
  133. mw.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement