-
Notifications
You must be signed in to change notification settings - Fork 4
/
matrix.cr
340 lines (290 loc) · 8.05 KB
/
matrix.cr
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
module CrystalEdge
# Column major matrix
class Matrix(T, H, W)
property matrix = Slice(T).new(W*H, T.new 0)
# Initializes an empty matrix
def initialize
end
# Initializes a matrix with a Slice of elements
def initialize(@matrix)
end
# Initializes a matrix like a static array
def initialize(&block : Int32 -> T)
@matrix = typeof(@matrix).new W*H, &block
end
# Initializes matrix filled with `value`
def initialize(value : T)
@matrix = typeof(@matrix).new W*H, value
end
# Initializes matrix using a block called with row and column number
def initialize(&block : Int32, Int32 -> T)
mat = Slice(T).new(W*H, T.new 0)
W.times do |col|
H.times do |row|
mat[index(row, col)] = block.call row, col
end
end
@matrix = mat
end
def self.identity
new do |i, j|
i == j ? T.new 1 : T.new 0
end
end
include Enumerable(T)
include Indexable(T)
delegate each, to_unsafe, to: matrix
def unsafe_fetch(idx : Int)
matrix.unsafe_fetch idx
end
# Returns width (number of columns)
def width
W
end
# Returns height (number of rows)
def height
H
end
# Returns size of square matrix
#
# Raises if matrix is not square
def size
raise "Cannot take size of non-square matrix" if width != height
width
end
# Tests matrices for equality
def ==(other)
self.matrix == other.matrix
end
# Tests matrices for inequality
def !=(other)
self.matrix != other.matrix
end
# Clones matrix
def clone
typeof(self).new @matrix
end
# Gets an element by index
def [](i : Int)
matrix[i]
end
# ditto
def []?(i : Int)
matrix[i]?
end
# Sets an element by index
def []=(i, v : T)
matrix[i] = v
end
# Calculates index from row and column numbers
def index(r, c)
c * height + r
end
# Gets an element by row and column numbers
def [](r, c)
self[index(r, c)]
end
# ditto
def []?(r, c)
self[index(r, c)]?
end
# Sets an element by row and column numbers
def []=(r, c, v : T)
self[index(r, c)] = v
end
# Transposes a matrix
def transpose : Matrix(T, W, H)
mat = Matrix(T, W, H).new
width.times do |row|
height.times do |col|
mat[row, col] = self[col, row]
end
end
mat
end
# Transposes a matrix.
#
# This method changes the object
#
# Raises if current matrix is not square
def transpose! : self
raise "Cannot transpose non-square matrix into itself" unless width == height
initialize transpose.matrix
end
# Performs addition of two matrices
def +(other : self)
typeof(self).new { |i| self[i] + other[i] }
end
# Performs subtraction of two matrices
def -(other : self)
typeof(self).new { |i| self[i] - other[i] }
end
# Performs multiplication by number
def *(other : Number)
typeof(self).new { |i| self[i] * other }
end
# Performs division by number
def /(other : Number)
inverse = 1.0 / other
typeof(self).new { |i| self[i] * inverse }
end
# Returns submatrix of type `m` (size `M`x`N`) with offset `x`, `y`
def submatrix(x, y, m : Matrix(T, M, N).class) forall M, N
raise "Out of bounds" if x + m.width >= width || y + m.height >= height
mat = m.new
mat.width.times do |col|
mat.height.times do |row|
mat[row, col] = self[row + y, col + x]
end
end
mat
end
# Calculates the product of two matrices
def *(other : Matrix(T, W, U)) forall U
result = Matrix(T, H, U).new
result.height.times do |i|
result.width.times do |j|
result[i, j] = (0...width).reduce(T.new 0) do |memo, r|
memo + self[i, r] * other[r, j]
end
end
end
result
end
# Make translation matrix from a zero-filled one.
#
# This method changes current object
def make_translation!(*values : T)
values.each_with_index do |el, i|
break unless height > i
self[i, width - 1] = el
end
Math.min(width, height).times do |i|
self[i, i] = T.new 1
end
self
end
# ditto
def make_translation!(vec)
make_translation! *vec.values
end
# Make scaling matrix from a zero-filled one.
#
# This method changes current object
def make_scaling!(*values : T)
values.each_with_index do |e, i|
break unless Math.min(width, height) > i
self[i, i] = values[i]
end
self
end
# ditto
def make_scaling!(vec)
make_scaling! *vec.values
end
# Make rotation matrix from a zero-filled one.
#
# This method changes current object.
#
# Can be called with one of four values:
# ```
# # 2D rotation
# matrix.make_rotation! theta
#
# # 3D rotation by angle theta around axis represented by unit vector {l, m, n}
# matrix.make_rotation! theta, l, m, n
# ```
#
# Raises if called with other number of arguments
def make_rotation!(*values : T)
case values.size
when 1
theta = values.first
sin, cos = T.new(Math.sin(theta)), T.new(Math.cos(theta))
self[0, 0] = self[1, 1] = cos
self[0, 1] = -sin
self[1, 0] = sin
when 4
theta, l, m, n = values
raise "Invalid size : #{height}x#{width}, not 3x3" if width < 3 || height < 3
sin, cos = T.new(Math.sin(theta)), T.new(Math.cos(theta))
icos = T.new(1 - cos)
self[0, 0] = l * l * icos + cos
self[0, 1] = m * l * icos - n * sin
self[0, 2] = n * l * icos + m * sin
self[1, 0] = l * m * icos + n * sin
self[1, 1] = m * m * icos + cos
self[1, 2] = n * m * icos - l * sin
self[2, 0] = l * n * icos - m * sin
self[2, 1] = m * n * icos + l * sin
self[2, 2] = n * n * icos + cos
else
raise "Invalid count of values : #{values.size}"
end
self
end
# Copies contents of this matrix into `m`
def copy_to(m : self)
m.matrix = matrix.clone
end
# Copies contents of `m` into this matrix
def copy_from(m : self)
@matrix = m.matrix.clone
end
{% for k, m in {:scale => :scaling, :rotate => :rotation, :translate => :translation} %}
{% key = k.id %}
{% method = m.id %}
# Cloning version of `#make_{{method}}!`
def make_{{method}}(*values)
clone.make_{{method}}! *values
end
# Makes a new {{method}} matrix
#
# Can be called with one Vector2 or Vector3, like `#make_{{method}}!`
def self.{{method}}(*values)
new.make_{{method}}! *values
end
# {{k.capitalize.id}}s matrix
def {{key}}(*values)
self * make_{{method}}(*values)
end
# {{k.capitalize.id}}s this matrix
#
# Changes the value of current matrix.
def {{key}}!(*values)
@matrix = {{key}}(*values).matrix
raise "Too short: #{@matrix.size}" if @matrix.size < width * height
self
end
# Return string representation of the matrix
def to_s(io)
# Format matrix by taking longest number
longer = 0
W.times do |row|
H.times do |col|
if longer < "#{@matrix[index(row, col)]} ".size
longer = "#{@matrix[index(row, col)]} ".size
end
end
end
# Printing matrix
H.times do |row|
io << "| "
W.times do |col|
if col != H-1
io << "#{@matrix[index(row, col)]} "
else
io << "#{@matrix[index(row, col)]}"
end
(longer - "#{@matrix[index(row, col)]} ".size).times do
io << " "
end
end
io << " |\n"
end
end
{% end %}
end
alias Matrix3 = Matrix(Float64, 3, 3)
alias Matrix4 = Matrix(Float64, 4, 4)
end