proj3d.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # 3dproj.py
  2. #
  3. """
  4. Various transforms used for by the 3D code
  5. """
  6. from __future__ import (absolute_import, division, print_function,
  7. unicode_literals)
  8. import six
  9. from six.moves import zip
  10. import numpy as np
  11. import numpy.linalg as linalg
  12. def line2d(p0, p1):
  13. """
  14. Return 2D equation of line in the form ax+by+c = 0
  15. """
  16. # x + x1 = 0
  17. x0, y0 = p0[:2]
  18. x1, y1 = p1[:2]
  19. #
  20. if x0 == x1:
  21. a = -1
  22. b = 0
  23. c = x1
  24. elif y0 == y1:
  25. a = 0
  26. b = 1
  27. c = -y1
  28. else:
  29. a = (y0-y1)
  30. b = (x0-x1)
  31. c = (x0*y1 - x1*y0)
  32. return a, b, c
  33. def line2d_dist(l, p):
  34. """
  35. Distance from line to point
  36. line is a tuple of coefficients a,b,c
  37. """
  38. a, b, c = l
  39. x0, y0 = p
  40. return abs((a*x0 + b*y0 + c)/np.sqrt(a**2+b**2))
  41. def line2d_seg_dist(p1, p2, p0):
  42. """distance(s) from line defined by p1 - p2 to point(s) p0
  43. p0[0] = x(s)
  44. p0[1] = y(s)
  45. intersection point p = p1 + u*(p2-p1)
  46. and intersection point lies within segment if u is between 0 and 1
  47. """
  48. x21 = p2[0] - p1[0]
  49. y21 = p2[1] - p1[1]
  50. x01 = np.asarray(p0[0]) - p1[0]
  51. y01 = np.asarray(p0[1]) - p1[1]
  52. u = (x01*x21 + y01*y21) / (x21**2 + y21**2)
  53. u = np.clip(u, 0, 1)
  54. d = np.sqrt((x01 - u*x21)**2 + (y01 - u*y21)**2)
  55. return d
  56. def mod(v):
  57. """3d vector length"""
  58. return np.sqrt(v[0]**2+v[1]**2+v[2]**2)
  59. def world_transformation(xmin, xmax,
  60. ymin, ymax,
  61. zmin, zmax):
  62. dx, dy, dz = (xmax-xmin), (ymax-ymin), (zmax-zmin)
  63. return np.array([
  64. [1.0/dx,0,0,-xmin/dx],
  65. [0,1.0/dy,0,-ymin/dy],
  66. [0,0,1.0/dz,-zmin/dz],
  67. [0,0,0,1.0]])
  68. def view_transformation(E, R, V):
  69. n = (E - R)
  70. ## new
  71. # n /= mod(n)
  72. # u = np.cross(V,n)
  73. # u /= mod(u)
  74. # v = np.cross(n,u)
  75. # Mr = np.diag([1.]*4)
  76. # Mt = np.diag([1.]*4)
  77. # Mr[:3,:3] = u,v,n
  78. # Mt[:3,-1] = -E
  79. ## end new
  80. ## old
  81. n = n / mod(n)
  82. u = np.cross(V, n)
  83. u = u / mod(u)
  84. v = np.cross(n, u)
  85. Mr = [[u[0],u[1],u[2],0],
  86. [v[0],v[1],v[2],0],
  87. [n[0],n[1],n[2],0],
  88. [0, 0, 0, 1],
  89. ]
  90. #
  91. Mt = [[1, 0, 0, -E[0]],
  92. [0, 1, 0, -E[1]],
  93. [0, 0, 1, -E[2]],
  94. [0, 0, 0, 1]]
  95. ## end old
  96. return np.dot(Mr, Mt)
  97. def persp_transformation(zfront, zback):
  98. a = (zfront+zback)/(zfront-zback)
  99. b = -2*(zfront*zback)/(zfront-zback)
  100. return np.array([[1,0,0,0],
  101. [0,1,0,0],
  102. [0,0,a,b],
  103. [0,0,-1,0]
  104. ])
  105. def ortho_transformation(zfront, zback):
  106. # note: w component in the resulting vector will be (zback-zfront), not 1
  107. a = -(zfront + zback)
  108. b = -(zfront - zback)
  109. return np.array([[2,0,0,0],
  110. [0,2,0,0],
  111. [0,0,-2,0],
  112. [0,0,a,b]
  113. ])
  114. def proj_transform_vec(vec, M):
  115. vecw = np.dot(M, vec)
  116. w = vecw[3]
  117. # clip here..
  118. txs, tys, tzs = vecw[0]/w, vecw[1]/w, vecw[2]/w
  119. return txs, tys, tzs
  120. def proj_transform_vec_clip(vec, M):
  121. vecw = np.dot(M, vec)
  122. w = vecw[3]
  123. # clip here.
  124. txs, tys, tzs = vecw[0] / w, vecw[1] / w, vecw[2] / w
  125. tis = (0 <= vecw[0]) & (vecw[0] <= 1) & (0 <= vecw[1]) & (vecw[1] <= 1)
  126. if np.any(tis):
  127. tis = vecw[1] < 1
  128. return txs, tys, tzs, tis
  129. def inv_transform(xs, ys, zs, M):
  130. iM = linalg.inv(M)
  131. vec = vec_pad_ones(xs, ys, zs)
  132. vecr = np.dot(iM, vec)
  133. try:
  134. vecr = vecr/vecr[3]
  135. except OverflowError:
  136. pass
  137. return vecr[0], vecr[1], vecr[2]
  138. def vec_pad_ones(xs, ys, zs):
  139. return np.array([xs, ys, zs, np.ones_like(xs)])
  140. def proj_transform(xs, ys, zs, M):
  141. """
  142. Transform the points by the projection matrix
  143. """
  144. vec = vec_pad_ones(xs, ys, zs)
  145. return proj_transform_vec(vec, M)
  146. def proj_transform_clip(xs, ys, zs, M):
  147. """
  148. Transform the points by the projection matrix
  149. and return the clipping result
  150. returns txs,tys,tzs,tis
  151. """
  152. vec = vec_pad_ones(xs, ys, zs)
  153. return proj_transform_vec_clip(vec, M)
  154. transform = proj_transform
  155. def proj_points(points, M):
  156. return np.column_stack(proj_trans_points(points, M))
  157. def proj_trans_points(points, M):
  158. xs, ys, zs = zip(*points)
  159. return proj_transform(xs, ys, zs, M)
  160. def proj_trans_clip_points(points, M):
  161. xs, ys, zs = zip(*points)
  162. return proj_transform_clip(xs, ys, zs, M)
  163. def rot_x(V, alpha):
  164. cosa, sina = np.cos(alpha), np.sin(alpha)
  165. M1 = np.array([[1,0,0,0],
  166. [0,cosa,-sina,0],
  167. [0,sina,cosa,0],
  168. [0,0,0,1]])
  169. return np.dot(M1, V)