-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixUtils.rb
166 lines (148 loc) · 3.68 KB
/
MatrixUtils.rb
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
include Math
module MatrixUtils
# Create an identity matrix of side-length size
def self.identity(size)
ret = Matrix.new(size, size)
for i in (0...size)
ret.set(i, i, 1)
end
return ret
end
# Multiplies 2 matricies (dot product) and modifies second input
def self.multiply(m1, m2, modify_second: true)
raise "Cannot multiply these matricies due to their dimensions" if m1.cols != m2.rows
ret = Matrix.new(m1.rows, m2.cols)
for r in (0...ret.rows)
for c in (0...ret.cols)
sum = 0
for i in (0...m1.rows) # Get the dot product and sum it
sum += m1.get_row(r)[i] * m2.get_col(c)[i] end
ret.set(r, c, sum)
end
end
if modify_second
for r in (0...m2.rows)
for c in (0...m2.cols)
m2.set(r, c, ret.get(r, c))
end
end
end
return ret
end
def self.multiply_constant(mat, k, modify_input: true)
ret = Matrix.new(mat.rows, mat.cols)
for r in (0...ret.rows)
for c in (0...ret.cols)
ret.set(r, c, k*mat.get(r, c))
end
end
if modify_input
for r in (0...mat.rows)
for c in (0...mat.cols)
mat.set(r, c, ret.get(r, c))
end
end
end
return ret
end
# Adds 2 matricies and modifies first input
def self.add(m1, m2, modify_second: true)
raise "Cannot add these matricies due to their dimensions" if m1.rows != m2.rows || m1.cols != m2.cols
ret = Matrix.new(m1.rows, m1.cols)
for r in (0...m1.rows)
for c in (0...m1.cols)
ret.set(r, c, m1.get(r, c) + m2.get(r, c))
end
end
if modify_second
for r in (0...m2.rows)
for c in (0...m2.cols)
m1.set(r, c, ret.get(r, c))
end
end
end
return ret
end
# Subtracts 2 matricies and modifies first input
def self.subtract(m1, m2, modify_second: true)
raise "Cannot subtract these matricies due to their dimensions" if m1.rows != m2.rows || m1.cols != m2.cols
ret = Matrix.new(m1.rows, m1.cols)
for r in (0...m1.rows)
for c in (0...m1.cols)
ret.set(r, c, m1.get(r, c) - m2.get(r, c))
end
end
if modify_second
for r in (0...m2.rows)
for c in (0...m2.cols)
m1.set(r, c, ret.get(r, c))
end
end
end
return ret
end
def self.dilation(sx, sy, sz)
ret = identity(4);
ret.set(0, 0, sx)
ret.set(1, 1, sy)
ret.set(2, 2, sz)
return ret
end
def self.translation(a, b, c)
ret = identity(4);
ret.set(0, 3, a)
ret.set(1, 3, b)
ret.set(2, 3, c)
return ret
end
def self.rotation(axis, theta)
theta = theta * $TAU / 360
ret = identity(4);
case(axis)
when 'x'
ret.set(1, 1, cos(theta))
ret.set(1, 2, -1 * sin(theta))
ret.set(2, 1, sin(theta))
ret.set(2, 2, cos(theta))
when 'y'
ret.set(0, 0, cos(theta))
ret.set(0, 2, sin(theta))
ret.set(2, 0, -1 * sin(theta))
ret.set(2, 2, cos(theta))
when 'z'
ret.set(0, 0, cos(theta))
ret.set(0, 1, -1 * sin(theta))
ret.set(1, 0, sin(theta))
ret.set(1, 1, cos(theta))
end
return ret
end
def self.hermite()
ret = Matrix.new(4, 4)
ret.set(0, 0, 2)
ret.set(0, 1, -2)
ret.set(0, 2, 1)
ret.set(0, 3, 1)
ret.set(1, 0, -3)
ret.set(1, 1, 3)
ret.set(1, 2, -2)
ret.set(1, 3, -1)
ret.set(2, 2, 1)
ret.set(3, 0, 1)
return ret
end
def self.bezier()
ret = Matrix.new(4, 4)
ret.set(0, 0, -1)
ret.set(0, 1, 3)
ret.set(0, 2, -3)
ret.set(0, 3, 1)
ret.set(1, 0, 3)
ret.set(1, 1, -6)
ret.set(1, 2, 3)
ret.set(2, 0, -3)
ret.set(2, 1, 3)
ret.set(3, 0, 1)
return ret
end
end