-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_transformations.py
155 lines (135 loc) · 4.47 KB
/
matrix_transformations.py
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
from bases import point, vector
from matrices import matrix
from math import cos, sin, pi
def translation(x, y, z):
"""
returns a transformation matrix
"""
return matrix(4, 4, ([[1, 0, 0, x], [0, 1, 0, y], [0, 0, 1, z], [0, 0, 0, 1]]))
def scaling(x, y, z):
"""
returns a scaling matrix
"""
return matrix(4, 4, ([[x, 0, 0, 0], [0, y, 0, 0], [0, 0, z, 0], [0, 0, 0, 1]]))
def x_rotation(rad):
"""
returns a rotation matrix in teh x direction
"""
return matrix(4, 4, ([[1, 0, 0, 0], [0, cos(rad), -sin(rad), 0], [0, sin(rad), cos(rad), 0], [0, 0, 0, 1]]))
def y_rotation(rad):
"""
returns a rotation matrix in teh y direction
"""
return matrix(4, 4, ([[cos(rad), 0, sin(rad), 0], [0, 1, 0, 0], [-sin(rad), 0, cos(rad), 0], [0, 0, 0, 1]]))
def z_rotation(rad):
"""
returns a transformation matrix in the z direction
"""
return matrix(4, 4, ([[cos(rad), -sin(rad), 0, 0], [sin(rad), cos(rad), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]))
def shearing(xy, xz, yx, yz, zx, zy):
"""
returns a shearing matrix
"""
return matrix(4, 4, ([[1, xy, xz, 0], [yx, 1, yz, 0], [zx, zy, 1, 0], [0, 0, 0, 1]]))
def check_tuple(x, y, z, w):
"""
checks if the tuple is a point or a vector
"""
if w == 0:
return vector(x, y, z)
return point(x, y, z)
def to_matrix(Tuple):
"""
converts a Tuple(point or a vector) into a matrix
"""
return matrix(4, 4, ([[Tuple.val[0], 0, 0, 0], [0, Tuple.val[1], 0, 0], [0, 0, Tuple.val[2], 0], [0, 0, 0, Tuple.val[3]]]))
def view_transform(FROM, TO, UP):
forward = TO - FROM
forward = forward.normalize()
upn = UP.normalize()
left = forward.cross(upn)
true_up = left.cross(forward)
orientation = matrix(4, 4, ([[left.val[0], left.val[1], left.val[2], 0],
[true_up.val[0], true_up.val[1], true_up.val[2], 0],
[-forward.val[0], -forward.val[1], -forward.val[2], 0],
[0, 0, 0, 1]]))
return orientation * translation(-FROM.val[0], -FROM.val[1], -FROM.val[2])
# if __name__ == '__main__':
# FROM = point(1, 3, 2)
# TO = point(4, -2, 8)
# UP = vector(1, 1, 0)
# print(view_transform(FROM, TO, UP))
# """
# tests used to check if the above function are working or not
# """
# x = point(-3, 4, 5)
# y = vector(-3, 4, 5)
# z = translation(5, -3, 2)
# inv = z.inverse()
# print(inv.multiply_tuple(x))
# print(x)
# print(x, z)
# print(z.multiply_tuple(x))
# print(z.multiply_tuple(y))
# print()
# scale = scaling(2, 3, 4)
# p = point(-4, 6, 8)
# v = vector(-4, 6, 8)
# print(scale.multiply_tuple(p))
# print()
# print(scale.multiply_tuple(v))
# print()
# inv = scale.inverse()
# print(inv.multiply_tuple(v))
# print()
# new = point(2, 3, 4)
# scale1 = scaling(-1, 1, 1)
# print(scale1.multiply_tuple(new))
# p = point(0, 1, 0)
# half = x_rotation(pi/4)
# full = x_rotation(pi/2)
# print(half.multiply_tuple(p))
# print()
# print(full.multiply_tuple(p))
# inv = half.inverse()
# print()
# print(inv.multiply_tuple(p))
# p = point(0, 0, 1)
# half = y_rotation(pi/4)
# full = y_rotation(pi/2)
# print(half.multiply_tuple(p))
# print()
# print(full.multiply_tuple(p))
# inv = half.inverse()
# print()
# print(inv.multiply_tuple(p))
# p = point(0, 1, 0)
# half = z_rotation(pi/4)
# full = z_rotation(pi/2)
# print(half.multiply_tuple(p))
# print(full.multiply_tuple(p))
# p = point(2, 3, 4)
# xy = shearing(1, 0, 0, 0, 0, 0)
# xz = shearing(0, 1, 0, 0, 0, 0)
# yx = shearing(0, 0, 1, 0, 0, 0)
# yz = shearing(0, 0, 0, 1, 0, 0)
# zx = shearing(0, 0, 0, 0, 1, 0)
# zy = shearing(0, 0, 0, 0, 0, 1)
# print(xy.multiply_tuple(p))
# print(xz.multiply_tuple(p))
# print(yx.multiply_tuple(p))
# print(yz.multiply_tuple(p))
# print(zx.multiply_tuple(p))
# print(zy.multiply_tuple(p))
# p = point(1, 0, 1)
# rot = x_rotation(pi/2)
# scale = scaling(5, 5, 5)
# trans = translation(10, 5, 7)
# p2 = rot.multiply_tuple(p)
# print(p2)
# print()
# p3 = scale.multiply_tuple(p2)
# print(p3)
# print()
# p4 = trans.multiply_tuple(p3)
# print(p4)