proj3d.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. """
  2. Various transforms used for by the 3D code
  3. """
  4. import numpy as np
  5. from matplotlib import _api
  6. def world_transformation(xmin, xmax,
  7. ymin, ymax,
  8. zmin, zmax, pb_aspect=None):
  9. """
  10. Produce a matrix that scales homogeneous coords in the specified ranges
  11. to [0, 1], or [0, pb_aspect[i]] if the plotbox aspect ratio is specified.
  12. """
  13. dx = xmax - xmin
  14. dy = ymax - ymin
  15. dz = zmax - zmin
  16. if pb_aspect is not None:
  17. ax, ay, az = pb_aspect
  18. dx /= ax
  19. dy /= ay
  20. dz /= az
  21. return np.array([[1/dx, 0, 0, -xmin/dx],
  22. [ 0, 1/dy, 0, -ymin/dy],
  23. [ 0, 0, 1/dz, -zmin/dz],
  24. [ 0, 0, 0, 1]])
  25. def _rotation_about_vector(v, angle):
  26. """
  27. Produce a rotation matrix for an angle in radians about a vector.
  28. """
  29. vx, vy, vz = v / np.linalg.norm(v)
  30. s = np.sin(angle)
  31. c = np.cos(angle)
  32. t = 2*np.sin(angle/2)**2 # more numerically stable than t = 1-c
  33. R = np.array([
  34. [t*vx*vx + c, t*vx*vy - vz*s, t*vx*vz + vy*s],
  35. [t*vy*vx + vz*s, t*vy*vy + c, t*vy*vz - vx*s],
  36. [t*vz*vx - vy*s, t*vz*vy + vx*s, t*vz*vz + c]])
  37. return R
  38. def _view_axes(E, R, V, roll):
  39. """
  40. Get the unit viewing axes in data coordinates.
  41. Parameters
  42. ----------
  43. E : 3-element numpy array
  44. The coordinates of the eye/camera.
  45. R : 3-element numpy array
  46. The coordinates of the center of the view box.
  47. V : 3-element numpy array
  48. Unit vector in the direction of the vertical axis.
  49. roll : float
  50. The roll angle in radians.
  51. Returns
  52. -------
  53. u : 3-element numpy array
  54. Unit vector pointing towards the right of the screen.
  55. v : 3-element numpy array
  56. Unit vector pointing towards the top of the screen.
  57. w : 3-element numpy array
  58. Unit vector pointing out of the screen.
  59. """
  60. w = (E - R)
  61. w = w/np.linalg.norm(w)
  62. u = np.cross(V, w)
  63. u = u/np.linalg.norm(u)
  64. v = np.cross(w, u) # Will be a unit vector
  65. # Save some computation for the default roll=0
  66. if roll != 0:
  67. # A positive rotation of the camera is a negative rotation of the world
  68. Rroll = _rotation_about_vector(w, -roll)
  69. u = np.dot(Rroll, u)
  70. v = np.dot(Rroll, v)
  71. return u, v, w
  72. def _view_transformation_uvw(u, v, w, E):
  73. """
  74. Return the view transformation matrix.
  75. Parameters
  76. ----------
  77. u : 3-element numpy array
  78. Unit vector pointing towards the right of the screen.
  79. v : 3-element numpy array
  80. Unit vector pointing towards the top of the screen.
  81. w : 3-element numpy array
  82. Unit vector pointing out of the screen.
  83. E : 3-element numpy array
  84. The coordinates of the eye/camera.
  85. """
  86. Mr = np.eye(4)
  87. Mt = np.eye(4)
  88. Mr[:3, :3] = [u, v, w]
  89. Mt[:3, -1] = -E
  90. M = np.dot(Mr, Mt)
  91. return M
  92. def _persp_transformation(zfront, zback, focal_length):
  93. e = focal_length
  94. a = 1 # aspect ratio
  95. b = (zfront+zback)/(zfront-zback)
  96. c = -2*(zfront*zback)/(zfront-zback)
  97. proj_matrix = np.array([[e, 0, 0, 0],
  98. [0, e/a, 0, 0],
  99. [0, 0, b, c],
  100. [0, 0, -1, 0]])
  101. return proj_matrix
  102. def _ortho_transformation(zfront, zback):
  103. # note: w component in the resulting vector will be (zback-zfront), not 1
  104. a = -(zfront + zback)
  105. b = -(zfront - zback)
  106. proj_matrix = np.array([[2, 0, 0, 0],
  107. [0, 2, 0, 0],
  108. [0, 0, -2, 0],
  109. [0, 0, a, b]])
  110. return proj_matrix
  111. def _proj_transform_vec(vec, M):
  112. vecw = np.dot(M, vec.data)
  113. w = vecw[3]
  114. txs, tys, tzs = vecw[0]/w, vecw[1]/w, vecw[2]/w
  115. if np.ma.isMA(vec[0]): # we check each to protect for scalars
  116. txs = np.ma.array(txs, mask=vec[0].mask)
  117. if np.ma.isMA(vec[1]):
  118. tys = np.ma.array(tys, mask=vec[1].mask)
  119. if np.ma.isMA(vec[2]):
  120. tzs = np.ma.array(tzs, mask=vec[2].mask)
  121. return txs, tys, tzs
  122. def _proj_transform_vec_clip(vec, M, focal_length):
  123. vecw = np.dot(M, vec.data)
  124. w = vecw[3]
  125. txs, tys, tzs = vecw[0] / w, vecw[1] / w, vecw[2] / w
  126. if np.isinf(focal_length): # don't clip orthographic projection
  127. tis = np.ones(txs.shape, dtype=bool)
  128. else:
  129. tis = (-1 <= txs) & (txs <= 1) & (-1 <= tys) & (tys <= 1) & (tzs <= 0)
  130. if np.ma.isMA(vec[0]):
  131. tis = tis & ~vec[0].mask
  132. if np.ma.isMA(vec[1]):
  133. tis = tis & ~vec[1].mask
  134. if np.ma.isMA(vec[2]):
  135. tis = tis & ~vec[2].mask
  136. txs = np.ma.masked_array(txs, ~tis)
  137. tys = np.ma.masked_array(tys, ~tis)
  138. tzs = np.ma.masked_array(tzs, ~tis)
  139. return txs, tys, tzs, tis
  140. def inv_transform(xs, ys, zs, invM):
  141. """
  142. Transform the points by the inverse of the projection matrix, *invM*.
  143. """
  144. vec = _vec_pad_ones(xs, ys, zs)
  145. vecr = np.dot(invM, vec)
  146. if vecr.shape == (4,):
  147. vecr = vecr.reshape((4, 1))
  148. for i in range(vecr.shape[1]):
  149. if vecr[3][i] != 0:
  150. vecr[:, i] = vecr[:, i] / vecr[3][i]
  151. return vecr[0], vecr[1], vecr[2]
  152. def _vec_pad_ones(xs, ys, zs):
  153. if np.ma.isMA(xs) or np.ma.isMA(ys) or np.ma.isMA(zs):
  154. return np.ma.array([xs, ys, zs, np.ones_like(xs)])
  155. else:
  156. return np.array([xs, ys, zs, np.ones_like(xs)])
  157. def proj_transform(xs, ys, zs, M):
  158. """
  159. Transform the points by the projection matrix *M*.
  160. """
  161. vec = _vec_pad_ones(xs, ys, zs)
  162. return _proj_transform_vec(vec, M)
  163. @_api.deprecated("3.10")
  164. def proj_transform_clip(xs, ys, zs, M):
  165. return _proj_transform_clip(xs, ys, zs, M, focal_length=np.inf)
  166. def _proj_transform_clip(xs, ys, zs, M, focal_length):
  167. """
  168. Transform the points by the projection matrix
  169. and return the clipping result
  170. returns txs, tys, tzs, tis
  171. """
  172. vec = _vec_pad_ones(xs, ys, zs)
  173. return _proj_transform_vec_clip(vec, M, focal_length)
  174. def _proj_points(points, M):
  175. return np.column_stack(_proj_trans_points(points, M))
  176. def _proj_trans_points(points, M):
  177. points = np.asanyarray(points)
  178. xs, ys, zs = points[:, 0], points[:, 1], points[:, 2]
  179. return proj_transform(xs, ys, zs, M)