-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathTools.py
More file actions
92 lines (70 loc) · 2.76 KB
/
MathTools.py
File metadata and controls
92 lines (70 loc) · 2.76 KB
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
'''
Created on May 16, 2013
@author: ivano.ras@gmail.com
MIT license
'''
import math
from PyQt4.QtGui import QMatrix4x4
class Tools:
'''
MathTools class
'''
def __init__(self, mtx = QMatrix4x4 (1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 0.0,0.0,0.0,1.0)):
'''
Constructor
'''
self.__normal_mtx = mtx
self.__pi_over_180 = math.pi / 180
self.__he_over_pi = 1 / self.__pi_over_180
self.__h = 0.5
def cameraToWorldTransform (self, vx, vy, vz):
'''
this method right multiplies the camera "normal transform" matrix by a given vector (vx, vy, vz,1)
and returns a 3-list, the last bottom value=1 is not returned.
'''
m_ref = self.__normal_mtx.data () # retrieves the 16 items in this matrix and copies them to values in row-major order.
return [m_ref[0]*vx + m_ref[4]*vy + m_ref[8]*vz + m_ref[12],
m_ref[1]*vx + m_ref[5]*vy + m_ref[9]*vz + m_ref[13],
m_ref[2]*vx + m_ref[6]*vy + m_ref[10]*vz + m_ref[14]]
def normalise (self, vect):
'''
this method returns a normalised vector as a 3-list.
'''
vx = vect[0]; vy = vect[1]; vz = vect[2]
euclidean_norm = math.sqrt (vx*vx + vy*vy +vz*vz)
inv_norm = 1 / euclidean_norm
return [vx*inv_norm, vy*inv_norm, vz*inv_norm]
def setNormalMatrix (self, normal_mtx):
'''
setter
'''
self.__normal_mtx = normal_mtx
def getAngle (self, fovy):
'''
this method calculates the angle the camera opens to based on the value of the "field of view, y".
'''
return math.tan (fovy / 2 * self.__pi_over_180)
def alignZAxisToVector (self, v_x, v_y, v_z):
'''
this method calculates the angles the y-axis needs to rotate around to overlap to the given vector.
'''
v_x2=v_x*v_x;
v_y2=v_y*v_y;
v_z2=v_z*v_z
xz_dist = math.sqrt (v_x2 + v_z2)
v_dist = math.sqrt (v_x2 + v_y2 + v_z2)
if xz_dist == 0:
if v_x > 0:
y_angle = math.pi * self.__h # 90 degrees in radians
else:
y_angle = -math.pi * self.__h # -90 degrees in radians
else:
y_angle = math.acos (v_z / xz_dist) # y_angle in radians
x_angle = math.acos (xz_dist / v_dist) # x_angle in radians
if v_y > 0:
pass
else:
x_angle = - x_angle
if v_x > 0:
y_angle = - y_angle
return [x_angle * self.__he_over_pi, y_angle * self.__he_over_pi, 0] # return angles (degrees) around main axes.