Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Various transforms used for by the 3D code 

3""" 

4 

5import numpy as np 

6import numpy.linalg as linalg 

7 

8from matplotlib import cbook 

9 

10 

11@cbook.deprecated("3.1") 

12def 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 

34 

35@cbook.deprecated("3.1") 

36def line2d_dist(l, p): 

37 """ 

38 Distance from line to point 

39 line is a tuple of coefficients a, b, c 

40 """ 

41 a, b, c = l 

42 x0, y0 = p 

43 return abs((a*x0 + b*y0 + c) / np.hypot(a, b)) 

44 

45 

46def _line2d_seg_dist(p1, p2, p0): 

47 """distance(s) from line defined by p1 - p2 to point(s) p0 

48 

49 p0[0] = x(s) 

50 p0[1] = y(s) 

51 

52 intersection point p = p1 + u*(p2-p1) 

53 and intersection point lies within segment if u is between 0 and 1 

54 """ 

55 

56 x21 = p2[0] - p1[0] 

57 y21 = p2[1] - p1[1] 

58 x01 = np.asarray(p0[0]) - p1[0] 

59 y01 = np.asarray(p0[1]) - p1[1] 

60 

61 u = (x01*x21 + y01*y21) / (x21**2 + y21**2) 

62 u = np.clip(u, 0, 1) 

63 d = np.hypot(x01 - u*x21, y01 - u*y21) 

64 

65 return d 

66 

67 

68@cbook.deprecated("3.1") 

69def line2d_seg_dist(p1, p2, p0): 

70 """distance(s) from line defined by p1 - p2 to point(s) p0 

71 

72 p0[0] = x(s) 

73 p0[1] = y(s) 

74 

75 intersection point p = p1 + u*(p2-p1) 

76 and intersection point lies within segment if u is between 0 and 1 

77 """ 

78 return _line2d_seg_dist(p1, p2, p0) 

79 

80 

81@cbook.deprecated("3.1", alternative="np.linalg.norm") 

82def mod(v): 

83 """3d vector length""" 

84 return np.sqrt(v[0]**2+v[1]**2+v[2]**2) 

85 

86 

87def world_transformation(xmin, xmax, 

88 ymin, ymax, 

89 zmin, zmax): 

90 dx, dy, dz = (xmax-xmin), (ymax-ymin), (zmax-zmin) 

91 return np.array([[1/dx, 0, 0, -xmin/dx], 

92 [0, 1/dy, 0, -ymin/dy], 

93 [0, 0, 1/dz, -zmin/dz], 

94 [0, 0, 0, 1]]) 

95 

96 

97def view_transformation(E, R, V): 

98 n = (E - R) 

99 ## new 

100# n /= np.linalg.norm(n) 

101# u = np.cross(V, n) 

102# u /= np.linalg.norm(u) 

103# v = np.cross(n, u) 

104# Mr = np.diag([1.] * 4) 

105# Mt = np.diag([1.] * 4) 

106# Mr[:3,:3] = u, v, n 

107# Mt[:3,-1] = -E 

108 ## end new 

109 

110 ## old 

111 n = n / np.linalg.norm(n) 

112 u = np.cross(V, n) 

113 u = u / np.linalg.norm(u) 

114 v = np.cross(n, u) 

115 Mr = [[u[0], u[1], u[2], 0], 

116 [v[0], v[1], v[2], 0], 

117 [n[0], n[1], n[2], 0], 

118 [0, 0, 0, 1]] 

119 # 

120 Mt = [[1, 0, 0, -E[0]], 

121 [0, 1, 0, -E[1]], 

122 [0, 0, 1, -E[2]], 

123 [0, 0, 0, 1]] 

124 ## end old 

125 

126 return np.dot(Mr, Mt) 

127 

128 

129def persp_transformation(zfront, zback): 

130 a = (zfront+zback)/(zfront-zback) 

131 b = -2*(zfront*zback)/(zfront-zback) 

132 return np.array([[1, 0, 0, 0], 

133 [0, 1, 0, 0], 

134 [0, 0, a, b], 

135 [0, 0, -1, 0]]) 

136 

137 

138def ortho_transformation(zfront, zback): 

139 # note: w component in the resulting vector will be (zback-zfront), not 1 

140 a = -(zfront + zback) 

141 b = -(zfront - zback) 

142 return np.array([[2, 0, 0, 0], 

143 [0, 2, 0, 0], 

144 [0, 0, -2, 0], 

145 [0, 0, a, b]]) 

146 

147 

148def _proj_transform_vec(vec, M): 

149 vecw = np.dot(M, vec) 

150 w = vecw[3] 

151 # clip here.. 

152 txs, tys, tzs = vecw[0]/w, vecw[1]/w, vecw[2]/w 

153 return txs, tys, tzs 

154 

155 

156@cbook.deprecated("3.1") 

157def proj_transform_vec(vec, M): 

158 return _proj_transform_vec(vec, M) 

159 

160 

161def _proj_transform_vec_clip(vec, M): 

162 vecw = np.dot(M, vec) 

163 w = vecw[3] 

164 # clip here. 

165 txs, tys, tzs = vecw[0] / w, vecw[1] / w, vecw[2] / w 

166 tis = (0 <= vecw[0]) & (vecw[0] <= 1) & (0 <= vecw[1]) & (vecw[1] <= 1) 

167 if np.any(tis): 

168 tis = vecw[1] < 1 

169 return txs, tys, tzs, tis 

170 

171 

172@cbook.deprecated("3.1") 

173def proj_transform_vec_clip(vec, M): 

174 return _proj_transform_vec_clip(vec, M) 

175 

176 

177def inv_transform(xs, ys, zs, M): 

178 iM = linalg.inv(M) 

179 vec = _vec_pad_ones(xs, ys, zs) 

180 vecr = np.dot(iM, vec) 

181 try: 

182 vecr = vecr / vecr[3] 

183 except OverflowError: 

184 pass 

185 return vecr[0], vecr[1], vecr[2] 

186 

187 

188def _vec_pad_ones(xs, ys, zs): 

189 return np.array([xs, ys, zs, np.ones_like(xs)]) 

190 

191 

192@cbook.deprecated("3.1") 

193def vec_pad_ones(xs, ys, zs): 

194 return _vec_pad_ones(xs, ys, zs) 

195 

196 

197def proj_transform(xs, ys, zs, M): 

198 """ 

199 Transform the points by the projection matrix 

200 """ 

201 vec = _vec_pad_ones(xs, ys, zs) 

202 return _proj_transform_vec(vec, M) 

203 

204 

205transform = proj_transform 

206 

207 

208def proj_transform_clip(xs, ys, zs, M): 

209 """ 

210 Transform the points by the projection matrix 

211 and return the clipping result 

212 returns txs, tys, tzs, tis 

213 """ 

214 vec = _vec_pad_ones(xs, ys, zs) 

215 return _proj_transform_vec_clip(vec, M) 

216 

217 

218def proj_points(points, M): 

219 return np.column_stack(proj_trans_points(points, M)) 

220 

221 

222def proj_trans_points(points, M): 

223 xs, ys, zs = zip(*points) 

224 return proj_transform(xs, ys, zs, M) 

225 

226 

227@cbook.deprecated("3.1") 

228def proj_trans_clip_points(points, M): 

229 xs, ys, zs = zip(*points) 

230 return proj_transform_clip(xs, ys, zs, M) 

231 

232 

233def rot_x(V, alpha): 

234 cosa, sina = np.cos(alpha), np.sin(alpha) 

235 M1 = np.array([[1, 0, 0, 0], 

236 [0, cosa, -sina, 0], 

237 [0, sina, cosa, 0], 

238 [0, 0, 0, 1]]) 

239 return np.dot(M1, V)