root / ase / gui / rot_tools.py
Historique | Voir | Annoter | Télécharger (1,58 ko)
1 |
# Gives the rotation matrix which rotates theta degrees about
|
---|---|
2 |
# vecU
|
3 |
|
4 |
# Generates the rotation matrix that rotate theta degrees about the vecU
|
5 |
def rotate_about_vec(vecU, theta): |
6 |
import numpy as np |
7 |
vecU = np.array(vecU) |
8 |
vecU = vecU / (sum(vecU ** 2) ** 0.5) |
9 |
ux, uy, uz = vecU |
10 |
st = np.sin(theta) |
11 |
ct = np.cos(theta) |
12 |
mat = np.array([[ux ** 2 + ct * (1 - ux ** 2), |
13 |
ux * uy * (1 - ct) - uz * st,
|
14 |
uz * ux * (1 - ct) + uy * st],
|
15 |
[ux * uy * (1 - ct) + uz * st,
|
16 |
uy ** 2 + ct * (1 - uy ** 2), |
17 |
uy * uz * (1 - ct) - ux * st],
|
18 |
[uz * ux * (1 - ct) - uy * st,
|
19 |
uy * uz * (1 - ct) + ux * st,
|
20 |
uz ** 2 + ct * (1 - uz **2)]]) |
21 |
return (mat)
|
22 |
|
23 |
# Generates the rotation matrix which rotates aVec into intoVec
|
24 |
def rotate_vec_into_newvec(aVec, intoVec): |
25 |
def length(v): |
26 |
return((sum(v ** 2)) ** 0.5) |
27 |
|
28 |
import numpy as np |
29 |
from math import acos |
30 |
fac = 1.0
|
31 |
aVec = np.array(aVec) |
32 |
intoVec = np.array(intoVec) |
33 |
nor = np.cross(aVec, intoVec) |
34 |
if length(nor) == 0: |
35 |
nor = np.array([1, 0, 0]) |
36 |
nor = nor / length(nor) |
37 |
theta = acos(np.dot(aVec, intoVec) / (length(aVec) * length(intoVec))) |
38 |
if np.dot(aVec, intoVec) < 0: |
39 |
theta = theta + np.pi |
40 |
fac = -1
|
41 |
return(fac * rotate_about_vec(nor, theta))
|
42 |
|
43 |
# Applies the rotation matrix to the vector and returns the rotated vector
|
44 |
def rotate_vec (rot_mat, vec): |
45 |
import numpy as np |
46 |
rot_vec = np.dot(rot_mat, vec) |
47 |
|
48 |
return (rot_vec)
|