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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

""" 

""" 

import numpy as np 

from navipy.maths import constants 

from navipy.maths import euler 

from navipy.maths import quaternion 

from navipy.maths.tools import vector_norm 

from navipy.maths.tools import unit_vector 

 

 

def translation_matrix(direction): 

""" 

Return matrix to translate by direction vector 

""" 

# get 4x4 identity matrix 

tmatrix = np.identity(4) 

# set the first three rows of the last colum to be the direction vector 

tmatrix[:3, 3] = direction[:3] 

return tmatrix 

 

 

def translation_from_matrix(matrix): 

""" 

Return translation vector from translation matrix 

""" 

# returns the first three rows of the last colum in the rotation matrix 

return np.array(matrix, copy=False)[:3, 3].copy() 

 

 

def reflection_matrix(point, normal): 

""" 

Return matrix to mirror at plane defined by point and normal vector 

""" 

# use only the first three columns of the normal vector 

normal = unit_vector(normal[:3]) 

# get 4x4 identity matrix 

M = np.identity(4) 

# substract two times the outer product of the normal vector from the \ 

# upper left 3x3 matrix of the unit matrix to 

M[:3, :3] -= 2.0 * np.outer(normal, normal) 

# set the fourth column of the resutlting matrix to 2* the dot product of \ 

# the point and the normal vector times the noraml vector 

M[:3, 3] = (2.0 * np.dot(point[:3], normal)) * normal 

return M 

 

 

def reflection_from_matrix(matrix): 

"""Return mirror plane point and normal vector from reflection matrix. 

""" 

# pointer to matrix (same as input) 

M = np.array(matrix, dtype=np.float64, copy=False) 

# normal: unit eigenvector corresponding to eigenvalue -1 

w, V = np.linalg.eig(M[:3, :3]) 

# get eigenvectors with eigenvalue close to length 1 -> unit eigenvector; \ 

# only for the upper left 3x3 part 

i = np.where(abs(np.real(w) + 1.0) < 1e-8)[0] 

if not len(i): 

raise ValueError("no unit eigenvector corresponding to eigenvalue -1") 

# choose only first of unit length eigenvectors 

normal = np.real(V[:, i[0]]).squeeze() 

# point: any unit eigenvector corresponding to eigenvalue 1 

# find eigenvector with unit length for whole matrix 

w, V = np.linalg.eig(M) 

i = np.where(abs(np.real(w) - 1.0) < 1e-8)[0] 

if not len(i): 

raise ValueError("no unit eigenvector corresponding to eigenvalue 1") 

# choose last of the unit length eigenvectors 

point = np.real(V[:, i[-1]]).squeeze() 

point /= point[3] 

return point, normal 

 

 

def scale_matrix(factor, origin=None, direction=None): 

"""Return matrix to scale by factor around origin in direction. 

Use factor -1 for point symmetry. 

""" 

if direction is None: 

# uniform scaling 

M = np.diag([factor, factor, factor, 1.0]) 

if origin is not None: 

M[:3, 3] = origin[:3] 

M[:3, 3] *= 1.0 - factor 

else: 

# nonuniform scaling 

direction = unit_vector(direction[:3]) 

factor = 1.0 - factor 

M = np.identity(4) 

M[:3, :3] -= factor * np.outer(direction, direction) 

if origin is not None: 

M[:3, 3] = (factor * np.dot(origin[:3], direction)) * direction 

return M 

 

 

def scale_from_matrix(matrix): 

"""Return scaling factor, origin and direction from scaling matrix. """ 

M = np.array(matrix, dtype=np.float64, copy=False) 

M33 = M[:3, :3] 

factor = np.trace(M33) - 2.0 

try: 

# direction: unit eigenvector corresponding to eigenvalue factor 

w, V = np.linalg.eig(M33) 

i = np.where(abs(np.real(w) - factor) < 1e-8)[0][0] 

direction = np.real(V[:, i]).squeeze() 

direction /= vector_norm(direction) 

except IndexError: 

# uniform scaling 

factor = (factor + 2.0) / 3.0 

direction = None 

# origin: any eigenvector corresponding to eigenvalue 1 

w, V = np.linalg.eig(M) 

i = np.where(abs(np.real(w) - 1.0) < 1e-8)[0] 

if not len(i): 

raise ValueError("no eigenvector corresponding to eigenvalue 1") 

origin = np.real(V[:, i[-1]]).squeeze() 

origin /= origin[3] 

return factor, origin, direction 

 

 

def projection_matrix(point, normal, direction=None, 

perspective=None, pseudo=False): 

"""Return matrix to project onto plane defined by point and normal. 

 

Using either perspective point, projection direction, or none of both. 

 

If pseudo is True, perspective projections will preserve relative depth 

such that Perspective = dot(Orthogonal, PseudoPerspective). 

""" 

M = np.identity(4) 

point = np.array(point[:3], dtype=np.float64, copy=False) 

normal = unit_vector(normal[:3]) 

if perspective is not None: 

# perspective projection 

perspective = np.array(perspective[:3], dtype=np.float64, 

copy=False) 

M[0, 0] = M[1, 1] = M[2, 2] = np.dot(perspective - point, normal) 

M[:3, :3] -= np.outer(perspective, normal) 

if pseudo: 

# preserve relative depth 

M[:3, :3] -= np.outer(normal, normal) 

M[:3, 3] = np.dot(point, normal) * (perspective + normal) 

else: 

M[:3, 3] = np.dot(point, normal) * perspective 

M[3, :3] = -normal 

M[3, 3] = np.dot(perspective, normal) 

elif direction is not None: 

# parallel projection 

direction = np.array(direction[:3], dtype=np.float64, copy=False) 

scale = np.dot(direction, normal) 

M[:3, :3] -= np.outer(direction, normal) / scale 

M[:3, 3] = direction * (np.dot(point, normal) / scale) 

else: 

# orthogonal projection 

M[:3, :3] -= np.outer(normal, normal) 

M[:3, 3] = np.dot(point, normal) * normal 

return M 

 

 

def projection_from_matrix(matrix, pseudo=False): 

"""Return projection plane and perspective point from projection matrix. 

 

Return values are same as arguments for projection_matrix function: 

point, normal, direction, perspective, and pseudo. 

""" 

M = np.array(matrix, dtype=np.float64, copy=False) 

M33 = M[:3, :3] 

w, V = np.linalg.eig(M) 

i = np.where(abs(np.real(w) - 1.0) < 1e-8)[0] 

if not pseudo and len(i): 

# point: any eigenvector corresponding to eigenvalue 1 

point = np.real(V[:, i[-1]]).squeeze() 

point /= point[3] 

# direction: unit eigenvector corresponding to eigenvalue 0 

w, V = np.linalg.eig(M33) 

i = np.where(abs(np.real(w)) < 1e-8)[0] 

if not len(i): 

raise ValueError("no eigenvector corresponding to eigenvalue 0") 

direction = np.real(V[:, i[0]]).squeeze() 

direction /= vector_norm(direction) 

# normal: unit eigenvector of M33.T corresponding to eigenvalue 0 

w, V = np.linalg.eig(M33.T) 

i = np.where(abs(np.real(w)) < 1e-8)[0] 

if len(i): 

# parallel projection 

normal = np.real(V[:, i[0]]).squeeze() 

normal /= vector_norm(normal) 

return point, normal, direction, None, False 

else: 

# orthogonal projection, where normal equals direction vector 

return point, direction, None, None, False 

else: 

# perspective projection 

i = np.where(abs(np.real(w)) > 1e-8)[0] 

if not len(i): 

raise ValueError( 

"no eigenvector not corresponding to eigenvalue 0") 

point = np.real(V[:, i[-1]]).squeeze() 

point /= point[3] 

normal = - M[3, :3] 

perspective = M[:3, 3] / np.dot(point[:3], normal) 

if pseudo: 

perspective -= normal 

return point, normal, None, perspective, pseudo 

 

 

def shear_matrix(angle, direction, point, normal): 

"""Return matrix to shear by angle along direction vector on shear plane. 

 

The shear plane is defined by a point and normal vector. The direction 

vector must be orthogonal to the plane's normal vector. 

 

A point P is transformed by the shear matrix into P" such that 

the vector P-P" is parallel to the direction vector and its extent is 

given by the angle of P-P'-P", where P' is the orthogonal projection 

of P onto the shear plane. 

""" 

normal = unit_vector(normal[:3]) 

direction = unit_vector(direction[:3]) 

if abs(np.dot(normal, direction)) > constants._EPS: 

raise ValueError("direction and normal vectors are not orthogonal") 

angle = np.tan(angle) 

M = np.identity(4) 

M[:3, :3] += angle * np.outer(direction, normal) 

M[:3, 3] = -angle * np.dot(point[:3], normal) * direction 

return M 

 

 

def shear_from_matrix(matrix): 

"""Return shear angle, direction and plane from shear matrix. 

""" 

M = np.array(matrix, dtype=np.float64, copy=False) 

M33 = M[:3, :3] 

# normal: cross independent eigenvectors corresponding to the eigenvalue 1 

w, V = np.linalg.eig(M33) 

i = np.where(abs(np.real(w) - 1.0) < 1e-4)[0] 

if len(i) < 2: 

raise ValueError("no two linear independent eigenvectors found %s" % w) 

V = np.real(V[:, i]).squeeze().T 

lenorm = -1.0 

for i0, i1 in ((0, 1), (0, 2), (1, 2)): 

n = np.cross(V[i0], V[i1]) 

w = vector_norm(n) 

if w > lenorm: 

lenorm = w 

normal = n 

normal /= lenorm 

# direction and angle 

direction = np.dot(M33 - np.identity(3), normal) 

angle = vector_norm(direction) 

direction /= angle 

angle = np.arctan(angle) 

# point: eigenvector corresponding to eigenvalue 1 

w, V = np.linalg.eig(M) 

i = np.where(abs(np.real(w) - 1.0) < 1e-8)[0] 

if not len(i): 

raise ValueError("no eigenvector corresponding to eigenvalue 1") 

point = np.real(V[:, i[-1]]).squeeze() 

point /= point[3] 

return angle, direction, point, normal 

 

 

def decompose_matrix(matrix, axes='xyz'): 

"""Return sequence oftransformations from transformation matrix. 

 

matrix : array_like 

Non-degenerative homogeneous transformation matrix 

 

Return tuple of: 

scale : vector of 3 scaling factors 

shear : list of shear factors for x-y, x-z, y-z axes 

angles : list of Euler angles about static x, y, z axes 

translate : translation vector along x, y, z axes 

perspective : perspective partition of matrix 

 

Raise ValueError if matrix is of wrong type or degenerative. 

""" 

M = np.array(matrix, dtype=np.float64, copy=True).T 

if abs(M[3, 3]) < constants._EPS: 

raise ValueError("M[3, 3] is zero") 

M /= M[3, 3] 

P = M.copy() 

P[:, 3] = 0.0, 0.0, 0.0, 1.0 

if not np.linalg.det(P): 

raise ValueError("matrix is singular") 

 

scale = np.zeros((3, )) 

shear = [0.0, 0.0, 0.0] 

angles = [0.0, 0.0, 0.0] 

 

if any(abs(M[:3, 3]) > constants._EPS): 

perspective = np.dot(M[:, 3], np.linalg.inv(P.T)) 

M[:, 3] = 0.0, 0.0, 0.0, 1.0 

else: 

perspective = np.array([0.0, 0.0, 0.0, 1.0]) 

 

translate = M[3, :3].copy() 

M[3, :3] = 0.0 

 

row = M[:3, :3].copy() 

scale[0] = vector_norm(row[0]) 

row[0] /= scale[0] 

shear[0] = np.dot(row[0], row[1]) 

row[1] -= row[0] * shear[0] 

scale[1] = vector_norm(row[1]) 

row[1] /= scale[1] 

shear[0] /= scale[1] 

shear[1] = np.dot(row[0], row[2]) 

row[2] -= row[0] * shear[1] 

shear[2] = np.dot(row[1], row[2]) 

row[2] -= row[1] * shear[2] 

scale[2] = vector_norm(row[2]) 

row[2] /= scale[2] 

shear[1:] /= scale[2] 

 

if np.dot(row[0], np.cross(row[1], row[2])) < 0: 

np.negative(scale, scale) 

np.negative(row, row) 

 

mat = np.linalg.inv(row[:3, :3]) 

angles = euler.from_matrix(mat, axes) 

return scale, shear, angles, translate, perspective 

 

 

def compose_matrix(scale=None, shear=None, angles=None, translate=None, 

perspective=None, axes='xyz'): 

"""Return transformation matrix from sequence oftransformations. 

 

This is the inverse of the decompose_matrix function. 

 

Sequence of transformations: 

scale : vector of 3 scaling factors 

shear : list of shear factors for x-y, x-z, y-z axes 

angles : list of Euler angles about static x, y, z axes 

translate : translation vector along x, y, z axes 

perspective : perspective partition of matrix 

""" 

M = np.identity(4) 

if perspective is not None: 

P = np.identity(4) 

P[3, :] = perspective[:4] 

M = np.dot(M, P) 

if translate is not None: 

T = np.identity(4) 

T[:3, 3] = translate[:3] 

M = np.dot(M, T) 

if angles is not None: 

if axes == 'quaternion': 

R = quaternion.matrix(angles) 

else: 

R = euler.matrix(angles[0], angles[1], angles[2], axes) 

M = np.dot(M, R) 

if shear is not None: 

Z = np.identity(4) 

Z[1, 2] = shear[2] 

Z[0, 2] = shear[1] 

Z[0, 1] = shear[0] 

M = np.dot(M, Z) 

if scale is not None: 

S = np.identity(4) 

S[0, 0] = scale[0] 

S[1, 1] = scale[1] 

S[2, 2] = scale[2] 

M = np.dot(M, S) 

M /= M[3, 3] 

return M 

 

 

def is_same_transform(matrix0, matrix1): 

"""Return True if two matrices perform same transformation. 

""" 

matrix0 = np.array(matrix0, dtype=np.float64, copy=True) 

matrix0 /= matrix0[3, 3] 

matrix1 = np.array(matrix1, dtype=np.float64, copy=True) 

matrix1 /= matrix1[3, 3] 

return np.allclose(matrix0, matrix1) 

 

 

def testing_is_same_transform(matrix0, matrix1): 

"""Return True if two matrices perform same transformation. 

""" 

matrix0 = np.array(matrix0, dtype=np.float64, copy=True) 

matrix0 /= matrix0[3, 3] 

matrix1 = np.array(matrix1, dtype=np.float64, copy=True) 

matrix1 /= matrix1[3, 3] 

np.testing.assert_allclose(matrix0, matrix1)