Class: Mittsu::Matrix4

Inherits:
Object
  • Object
show all
Defined in:
lib/mittsu/math/matrix4.rb

Constant Summary collapse

DIMENSIONS =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMatrix4

Returns a new instance of Matrix4.



9
10
11
12
13
14
15
16
# File 'lib/mittsu/math/matrix4.rb', line 9

def initialize()
  @elements = [
    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
  ]
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



5
6
7
# File 'lib/mittsu/math/matrix4.rb', line 5

def elements
  @elements
end

Instance Method Details

#==(other) ⇒ Object



568
569
570
# File 'lib/mittsu/math/matrix4.rb', line 568

def ==(other)
  other.elements == @elements
end

#apply_to_vector3_array(array, offset = 0, length = array.length) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/mittsu/math/matrix4.rb', line 277

def apply_to_vector3_array(array, offset = 0, length = array.length)
  v1 = Mittsu::Vector3.new
  i = 0
  j = offset
  while i < length
    v1.x = array[j].to_f
    v1.y = array[j + 1].to_f
    v1.z = array[j + 2].to_f
    v1.apply_matrix4(self)
    array[j]     = v1.x
    array[j + 1] = v1.y
    array[j + 2] = v1.z
    i += 3
    j += 3
  end
  array
end

#cloneObject



582
583
584
# File 'lib/mittsu/math/matrix4.rb', line 582

def clone
  Mittsu::Matrix4.new.from_array(self.elements)
end

#compose(position, quaternion, scale) ⇒ Object



476
477
478
479
480
481
# File 'lib/mittsu/math/matrix4.rb', line 476

def compose(position, quaternion, scale)
  self.make_rotation_from_quaternion(quaternion)
  self.scale(scale)
  self.set_position(position)
  self
end

#copy(m) ⇒ Object



37
38
39
40
# File 'lib/mittsu/math/matrix4.rb', line 37

def copy(m)
  self.from_array(m.elements)
  self
end

#copy_position(m) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/mittsu/math/matrix4.rb', line 42

def copy_position(m)
  te = self.elements
  me = m.elements
  te[12] = me[12]
  te[13] = me[13]
  te[14] = me[14]
  self
end

#decompose(position, quaternion, scale) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/mittsu/math/matrix4.rb', line 483

def decompose(position, quaternion, scale)
  vector = Mittsu::Vector3.new
  matrix = Mittsu::Matrix4.new
  te = self.elements
  sx = vector.set(te[0], te[1],  te[2]).length
  sy = vector.set(te[4], te[5],  te[6]).length
  sz = vector.set(te[8], te[9], te[10]).length
  # if determine is negative, we need to invert one scale
  det = self.determinant
  if det < 0.0
    sx = -sx
  end
  position.x = te[12]
  position.y = te[13]
  position.z = te[14]
  # scale the rotation part
  matrix.elements[0...15] = self.elements # at this point matrix is incomplete so we can't use .copy
  inv_sx = 1.0 / sx
  inv_sy = 1.0 / sy
  inv_sz = 1.0 / sz
  matrix.elements[0]  *= inv_sx
  matrix.elements[1]  *= inv_sx
  matrix.elements[2]  *= inv_sx
  matrix.elements[4]  *= inv_sy
  matrix.elements[5]  *= inv_sy
  matrix.elements[6]  *= inv_sy
  matrix.elements[8]  *= inv_sz
  matrix.elements[9]  *= inv_sz
  matrix.elements[10] *= inv_sz
  quaternion.set_from_rotation_matrix(matrix)
  scale.x = sx
  scale.y = sy
  scale.z = sz
  self
end

#determinantObject



295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/mittsu/math/matrix4.rb', line 295

def determinant
  te = self.elements
  n11 = te[0]; n12 = te[4]; n13 = te[8]; n14 = te[12]
  n21 = te[1]; n22 = te[5]; n23 = te[9]; n24 = te[13]
  n31 = te[2]; n32 = te[6]; n33 = te[10]; n34 = te[14]
  n41 = te[3]; n42 = te[7]; n43 = te[11]; n44 = te[15]
  #TODO: make this more efficient
  #(based on http:#www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm)
  n41 * (n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34) +
  n42 * (n11 * n23 * n34 - n11 * n24 * n33 + n14 * n21 * n33 - n13 * n21 * n34 + n13 * n24 * n31 - n14 * n23 * n31) +
  n43 * (n11 * n24 * n32 - n11 * n22 * n34 - n14 * n21 * n32 + n12 * n21 * n34 + n14 * n22 * n31 - n12 * n24 * n31) +
  n44 * (-n13 * n22 * n31 - n11 * n23 * n32 + n11 * n22 * n33 + n13 * n21 * n32 - n12 * n21 * n33 + n12 * n23 * n31)
end

#extract_basis(x_axis, y_axis, z_axis) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/mittsu/math/matrix4.rb', line 51

def extract_basis(x_axis, y_axis, z_axis)
  te = self.elements
  x_axis.set(te[0], te[1], te[2])
  y_axis.set(te[4], te[5], te[6])
  z_axis.set(te[8], te[9], te[10])
  self
end

#extract_rotation(m) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mittsu/math/matrix4.rb', line 69

def extract_rotation(m)
  v1 = Mittsu::Vector3.new
  te = self.elements
  me = m.elements
  scale_x = 1.0 / v1.set(me[0], me[1], me[2]).length
  scale_y = 1.0 / v1.set(me[4], me[5], me[6]).length
  scale_z = 1.0 / v1.set(me[8], me[9], me[10]).length
  te[0] = me[0] * scale_x
  te[1] = me[1] * scale_x
  te[2] = me[2] * scale_x
  te[4] = me[4] * scale_y
  te[5] = me[5] * scale_y
  te[6] = me[6] * scale_y
  te[8] = me[8] * scale_z
  te[9] = me[9] * scale_z
  te[10] = me[10] * scale_z
  self
end

#flatten_to_array_offset(array, offset) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/mittsu/math/matrix4.rb', line 320

def flatten_to_array_offset(array, offset)
  te = self.elements
  array[offset    ] = te[0]
  array[offset + 1] = te[1]
  array[offset + 2] = te[2]
  array[offset + 3] = te[3]
  array[offset + 4] = te[4]
  array[offset + 5] = te[5]
  array[offset + 6] = te[6]
  array[offset + 7] = te[7]
  array[offset + 8]  = te[8]
  array[offset + 9]  = te[9]
  array[offset + 10] = te[10]
  array[offset + 11] = te[11]
  array[offset + 12] = te[12]
  array[offset + 13] = te[13]
  array[offset + 14] = te[14]
  array[offset + 15] = te[15]
  array
end

#from_array(array) ⇒ Object



563
564
565
566
# File 'lib/mittsu/math/matrix4.rb', line 563

def from_array(array)
  self.elements[0..array.length] = array
  self
end

#identityObject



27
28
29
30
31
32
33
34
35
# File 'lib/mittsu/math/matrix4.rb', line 27

def identity
  self.set(
    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
  )
  self
end

#inverse(m, throw_on_invertable = false) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/mittsu/math/matrix4.rb', line 349

def inverse(m, throw_on_invertable = false)
  # based on http:#www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  te = @elements
  me = m.elements
  n11 = me[0]; n12 = me[4]; n13 = me[8];  n14 = me[12]
  n21 = me[1]; n22 = me[5]; n23 = me[9];  n24 = me[13]
  n31 = me[2]; n32 = me[6]; n33 = me[10]; n34 = me[14]
  n41 = me[3]; n42 = me[7]; n43 = me[11]; n44 = me[15]
  te[0] = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44
  te[4] = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44
  te[8] = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44
  te[12] = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34
  te[1] = n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44
  te[5] = n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44
  te[9] = n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44
  te[13] = n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34
  te[2] = n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44
  te[6] = n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44
  te[10] = n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44
  te[14] = n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34
  te[3] = n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43
  te[7] = n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43
  te[11] = n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43
  te[15] = n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33
  det = n11 * te[0] + n21 * te[4] + n31 * te[8] + n41 * te[12]
  if det.zero?
    msg = "Mittsu::Matrix4#inverse: can't invert matrix, determinant is 0"
    if throw_on_invertable
      raise Error.new(msg)
    else
      # THREE.warn(msg)
      puts "WARNING: #{msg}"
    end
    self.identity
    return self
  end
  self.multiply_scalar(1.0 / det)
  self
end

#look_at(eye, target, up) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/mittsu/math/matrix4.rb', line 201

def look_at(eye, target, up)
  x = Mittsu::Vector3.new
  y = Mittsu::Vector3.new
  z = Mittsu::Vector3.new
  te = self.elements
  z.sub_vectors(eye, target).normalize
  if z.length.zero?
    z.z = 1.0
  end
  x.cross_vectors(up, z).normalize
  if x.length.zero?
    z.x += 0.0001
    x.cross_vectors(up, z).normalize
  end
  y.cross_vectors(z, x)
  te[0] = x.x; te[4] = y.x; te[8] = z.x
  te[1] = x.y; te[5] = y.y; te[9] = z.y
  te[2] = x.z; te[6] = y.z; te[10] = z.z
  self
end

#make_basis(x_axis, y_axis, z_axis) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/mittsu/math/matrix4.rb', line 59

def make_basis(x_axis, y_axis, z_axis)
  self.set(
    x_axis.x, y_axis.x, z_axis.x, 0.0,
    x_axis.y, y_axis.y, z_axis.y, 0.0,
    x_axis.z, y_axis.z, z_axis.z, 0.0,
        0.0,     0.0,     0.0, 1.0
  )
  self
end

#make_frustum(left, right, bottom, top, near, far) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/mittsu/math/matrix4.rb', line 519

def make_frustum(left, right, bottom, top, near, far)
  left, right, bottom, top, near, far =
    left.to_f, right.to_f, bottom.to_f, top.to_f, near.to_f, far.to_f
  te = self.elements
  x = 2.0 * near / (right - left)
  y = 2.0 * near / (top - bottom)
  a = (right + left) / (right - left)
  b = (top + bottom) / (top - bottom)
  c =     -(far + near) / (far - near)
  d = -2.0 * far * near / (far - near)
  te[0] =   x; te[4] = 0.0;  te[8]  =    a;  te[12] = 0.0
  te[1] = 0.0; te[5] =   y;  te[9]  =    b;  te[13] = 0.0
  te[2] = 0.0; te[6] = 0.0;  te[10] =    c;  te[14] =   d
  te[3] = 0.0; te[7] = 0.0;  te[11] = -1.0;  te[15] = 0.0
  self
end

#make_orthographic(left, right, top, bottom, near, far) ⇒ Object



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/mittsu/math/matrix4.rb', line 546

def make_orthographic(left, right, top, bottom, near, far)
  left, right, top, bottom, near, far =
    left.to_f, right.to_f, top.to_f, bottom.to_f, near.to_f, far.to_f
  te = self.elements
  w = right - left
  h = top - bottom
  p = far - near
  x = (right + left) / w
  y = (top + bottom) / h
  z = (far + near) / p
  te[0] = 2.0 / w; te[4] =     0.0; te[8]  =      0.0; te[12] =  -x
  te[1] =     0.0; te[5] = 2.0 / h; te[9]  =      0.0; te[13] =  -y
  te[2] =     0.0; te[6] =     0.0; te[10] = -2.0 / p; te[14] =  -z
  te[3] =     0.0; te[7] =     0.0; te[11] =      0.0; te[15] = 1.0
  self
end

#make_perspective(fov, aspect, near, far) ⇒ Object



536
537
538
539
540
541
542
543
544
# File 'lib/mittsu/math/matrix4.rb', line 536

def make_perspective(fov, aspect, near, far)
  fov, aspect, near, far =
    fov.to_f, aspect.to_f, near.to_f, far.to_f
  ymax = near * Math.tan(Math.deg_to_rad(fov * 0.5))
  ymin = -ymax
  xmin = ymin * aspect
  xmax = ymax * aspect
  self.make_frustum(xmin, xmax, ymin, ymax, near, far)
end

#make_rotation_axis(axis, angle) ⇒ Object



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/mittsu/math/matrix4.rb', line 450

def make_rotation_axis(axis, angle)
  # Based on http:#www.gamedev.net/reference/articles/article1199.asp
  c = Math.cos(angle)
  s = Math.sin(angle)
  t = 1.0 - c
  x, y, z = axis.x, axis.y, axis.z
  tx, ty = t * x, t * y
  self.set(
        tx * x + c, tx * y - s * z, tx * z + s * y, 0.0,
    tx * y + s * z,     ty * y + c, ty * z - s * x, 0.0,
    tx * z - s * y, ty * z + s * x,  t * z * z + c, 0.0,
               0.0,            0.0,            0.0, 1.0
  )
  self
end

#make_rotation_from_euler(euler) ⇒ Object



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
# File 'lib/mittsu/math/matrix4.rb', line 88

def make_rotation_from_euler(euler)
  te = self.elements
  x, y, z = euler.x, euler.y, euler.z
  a, b = Math.cos(x), Math.sin(x)
  c, d = Math.cos(y), Math.sin(y)
  e, f = Math.cos(z), Math.sin(z)
  if euler.order == 'XYZ'
    ae = a * e; af = a * f; be = b * e; bf = b * f
    te[0] = c * e
    te[4] = - c * f
    te[8] = d
    te[1] = af + be * d
    te[5] = ae - bf * d
    te[9] = - b * c
    te[2] = bf - ae * d
    te[6] = be + af * d
    te[10] = a * c
  elsif euler.order == 'YXZ'
    ce = c * e; cf = c * f; de = d * e; df = d * f
    te[0] = ce + df * b
    te[4] = de * b - cf
    te[8] = a * d
    te[1] = a * f
    te[5] = a * e
    te[9] = - b
    te[2] = cf * b - de
    te[6] = df + ce * b
    te[10] = a * c
  elsif euler.order == 'ZXY'
    ce = c * e; cf = c * f; de = d * e; df = d * f
    te[0] = ce - df * b
    te[4] = - a * f
    te[8] = de + cf * b
    te[1] = cf + de * b
    te[5] = a * e
    te[9] = df - ce * b
    te[2] = - a * d
    te[6] = b
    te[10] = a * c
  elsif euler.order == 'ZYX'
    ae = a * e; af = a * f; be = b * e; bf = b * f
    te[0] = c * e
    te[4] = be * d - af
    te[8] = ae * d + bf
    te[1] = c * f
    te[5] = bf * d + ae
    te[9] = af * d - be
    te[2] = - d
    te[6] = b * c
    te[10] = a * c
  elsif euler.order == 'YZX'
    ac = a * c; ad = a * d; bc = b * c; bd = b * d
    te[0] = c * e
    te[4] = bd - ac * f
    te[8] = bc * f + ad
    te[1] = f
    te[5] = a * e
    te[9] = - b * e
    te[2] = - d * e
    te[6] = ad * f + bc
    te[10] = ac - bd * f
  elsif euler.order == 'XZY'
    ac = a * c; ad = a * d; bc = b * c; bd = b * d
    te[0] = c * e
    te[4] = - f
    te[8] = d * e
    te[1] = ac * f + bd
    te[5] = a * e
    te[9] = ad * f - bc
    te[2] = bc * f - ad
    te[6] = b * e
    te[10] = bd * f + ac
  end
  # last column
  te[3] = 0.0
  te[7] = 0.0
  te[11] = 0.0
  # bottom row
  te[12] = 0.0
  te[13] = 0.0
  te[14] = 0.0
  te[15] = 1.0
  self
end

#make_rotation_from_quaternion(q) ⇒ Object



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
# File 'lib/mittsu/math/matrix4.rb', line 173

def make_rotation_from_quaternion(q)
  te = self.elements
  x, y, z, w = q.x, q.y, q.z, q.w
  x2, y2, z2 = x + x, y + y, z + z
  xx, xy, xz = x * x2, x * y2, x * z2
  yy, yz, zz = y * y2, y * z2, z * z2
  wx, wy, wz = w * x2, w * y2, w * z2
  te[0] = 1.0 - (yy + zz)
  te[4] = xy - wz
  te[8] = xz + wy
  te[1] = xy + wz
  te[5] = 1.0 - (xx + zz)
  te[9] = yz - wx
  te[2] = xz - wy
  te[6] = yz + wx
  te[10] = 1.0 - (xx + yy)
  # last column
  te[3] = 0.0
  te[7] = 0.0
  te[11] = 0.0
  # bottom row
  te[12] = 0.0
  te[13] = 0.0
  te[14] = 0.0
  te[15] = 1.0
  self
end

#make_rotation_x(theta) ⇒ Object



417
418
419
420
421
422
423
424
425
426
# File 'lib/mittsu/math/matrix4.rb', line 417

def make_rotation_x(theta)
  c, s = Math.cos(theta), Math.sin(theta)
  self.set(
    1.0, 0.0,  0.0, 0.0,
    0.0,   c,   -s, 0.0,
    0.0,   s,    c, 0.0,
    0.0, 0.0,  0.0, 1.0
  )
  self
end

#make_rotation_y(theta) ⇒ Object



428
429
430
431
432
433
434
435
436
437
# File 'lib/mittsu/math/matrix4.rb', line 428

def make_rotation_y(theta)
  c, s = Math.cos(theta), Math.sin(theta)
  self.set(
       c, 0.0,   s, 0.0,
     0.0, 1.0, 0.0, 0.0,
      -s, 0.0,   c, 0.0,
     0.0, 0.0, 0.0, 1.0
  )
  self
end

#make_rotation_z(theta) ⇒ Object



439
440
441
442
443
444
445
446
447
448
# File 'lib/mittsu/math/matrix4.rb', line 439

def make_rotation_z(theta)
  c, s = Math.cos(theta), Math.sin(theta)
  self.set(
      c,   -s, 0.0, 0.0,
      s,    c, 0.0, 0.0,
    0.0,  0.0, 1.0, 0.0,
    0.0,  0.0, 0.0, 1.0
  )
  self
end

#make_scale(x, y, z) ⇒ Object



466
467
468
469
470
471
472
473
474
# File 'lib/mittsu/math/matrix4.rb', line 466

def make_scale(x, y, z)
  self.set(
    x.to_f, 0.0,    0.0,    0.0,
    0.0,    y.to_f, 0.0,    0.0,
    0.0,    0.0,    z.to_f, 0.0,
    0.0,    0.0,    0.0,    1.0
  )
  self
end

#make_translation(x, y, z) ⇒ Object



407
408
409
410
411
412
413
414
415
# File 'lib/mittsu/math/matrix4.rb', line 407

def make_translation(x, y, z)
  self.set(
    1.0, 0.0, 0.0, x.to_f,
    0.0, 1.0, 0.0, y.to_f,
    0.0, 0.0, 1.0, z.to_f,
    0.0, 0.0, 0.0, 1.0
  )
  self
end

#max_scale_on_axisObject



399
400
401
402
403
404
405
# File 'lib/mittsu/math/matrix4.rb', line 399

def max_scale_on_axis
  te = self.elements
  scale_x_sq = te[0] * te[0] + te[1] * te[1] + te[2] * te[2]
  scale_y_sq = te[4] * te[4] + te[5] * te[5] + te[6] * te[6]
  scale_z_sq = te[8] * te[8] + te[9] * te[9] + te[10] * te[10]
  Math.sqrt([scale_x_sq, scale_y_sq, scale_z_sq].max)
end

#multiply(m) ⇒ Object



222
223
224
# File 'lib/mittsu/math/matrix4.rb', line 222

def multiply(m)
  self.multiply_matrices(self, m)
end

#multiply_matrices(a, b) ⇒ Object



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
# File 'lib/mittsu/math/matrix4.rb', line 226

def multiply_matrices(a, b)
  ae = a.elements
  be = b.elements
  te = self.elements
  a11 = ae[0]; a12 = ae[4]; a13 = ae[8];  a14 = ae[12]
  a21 = ae[1]; a22 = ae[5]; a23 = ae[9];  a24 = ae[13]
  a31 = ae[2]; a32 = ae[6]; a33 = ae[10]; a34 = ae[14]
  a41 = ae[3]; a42 = ae[7]; a43 = ae[11]; a44 = ae[15]
  b11 = be[0]; b12 = be[4]; b13 = be[8];  b14 = be[12]
  b21 = be[1]; b22 = be[5]; b23 = be[9];  b24 = be[13]
  b31 = be[2]; b32 = be[6]; b33 = be[10]; b34 = be[14]
  b41 = be[3]; b42 = be[7]; b43 = be[11]; b44 = be[15]
  te[0]  = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41
  te[4]  = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42
  te[8]  = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43
  te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44
  te[1]  = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41
  te[5]  = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42
  te[9]  = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43
  te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44
  te[2]  = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41
  te[6]  = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42
  te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43
  te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44
  te[3]  = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41
  te[7]  = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42
  te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43
  te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44
  self
end

#multiply_scalar(s) ⇒ Object



267
268
269
270
271
272
273
274
275
# File 'lib/mittsu/math/matrix4.rb', line 267

def multiply_scalar(s)
  te = self.elements
  s = s.to_f
  te[0] *= s; te[4] *= s; te[8]  *= s; te[12] *= s
  te[1] *= s; te[5] *= s; te[9]  *= s; te[13] *= s
  te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s
  te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s
  self
end

#multiply_to_array(a, b, r) ⇒ Object



257
258
259
260
261
262
263
264
265
# File 'lib/mittsu/math/matrix4.rb', line 257

def multiply_to_array(a, b, r)
  te = self.elements
  self.multiply_matrices(a, b)
  r[0]  = te[0];  r[1]  = te[1];  r[2]  = te[2];  r[3]  = te[3]
  r[4]  = te[4];  r[5]  = te[5];  r[6]  = te[6];  r[7]  = te[7]
  r[8]  = te[8];  r[9]  = te[9];  r[10] = te[10]; r[11] = te[11]
  r[12] = te[12]; r[13] = te[13]; r[14] = te[14]; r[15] = te[15]
  self
end

#scale(v) ⇒ Object



389
390
391
392
393
394
395
396
397
# File 'lib/mittsu/math/matrix4.rb', line 389

def scale(v)
  te = self.elements
  x = v.x; y = v.y; z = v.z
  te[0] *= x; te[4] *= y; te[8]  *= z
  te[1] *= x; te[5] *= y; te[9]  *= z
  te[2] *= x; te[6] *= y; te[10] *= z
  te[3] *= x; te[7] *= y; te[11] *= z
  self
end

#set(n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/mittsu/math/matrix4.rb', line 18

def set(n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44)
  te = self.elements
  te[0] = n11.to_f; te[4] = n12.to_f; te[8] = n13.to_f; te[12] = n14.to_f
  te[1] = n21.to_f; te[5] = n22.to_f; te[9] = n23.to_f; te[13] = n24.to_f
  te[2] = n31.to_f; te[6] = n32.to_f; te[10] = n33.to_f; te[14] = n34.to_f
  te[3] = n41.to_f; te[7] = n42.to_f; te[11] = n43.to_f; te[15] = n44.to_f
  self
end

#set_position(v) ⇒ Object



341
342
343
344
345
346
347
# File 'lib/mittsu/math/matrix4.rb', line 341

def set_position(v)
  te = self.elements
  te[12] = v.x
  te[13] = v.y
  te[14] = v.z
  self
end

#to_aObject



572
573
574
575
576
577
578
579
580
# File 'lib/mittsu/math/matrix4.rb', line 572

def to_a
  te = self.elements
  [
    te[0], te[1], te[2], te[3],
    te[4], te[5], te[6], te[7],
    te[8], te[9], te[10], te[11],
    te[12], te[13], te[14], te[15]
  ]
end

#transposeObject



309
310
311
312
313
314
315
316
317
318
# File 'lib/mittsu/math/matrix4.rb', line 309

def transpose
  te = self.elements
  tmp = te[1];  te[1]  = te[4];  te[4]  = tmp
  tmp = te[2];  te[2]  = te[8];  te[8]  = tmp
  tmp = te[6];  te[6]  = te[9];  te[9]  = tmp
  tmp = te[3];  te[3]  = te[12]; te[12] = tmp
  tmp = te[7];  te[7]  = te[13]; te[13] = tmp
  tmp = te[11]; te[11] = te[14]; te[14] = tmp
  self
end