Class: Mittsu::Matrix3

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMatrix3

Returns a new instance of Matrix3.



6
7
8
9
10
11
12
# File 'lib/mittsu/math/matrix3.rb', line 6

def initialize
  @elements = [
    1.0, 0.0, 0.0,
    0.0, 1.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/matrix3.rb', line 5

def elements
  @elements
end

Instance Method Details

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mittsu/math/matrix3.rb', line 41

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

#cloneObject



158
159
160
# File 'lib/mittsu/math/matrix3.rb', line 158

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

#copy(m) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/mittsu/math/matrix3.rb', line 31

def copy(m)
  me = m.elements
  self.set(
    me[0], me[3], me[6],
    me[1], me[4], me[7],
    me[2], me[5], me[8]
  )
  self
end

#determinantObject



66
67
68
69
# File 'lib/mittsu/math/matrix3.rb', line 66

def determinant
  a, b, c, d, e, f, g, h, i = *self.elements
  a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g
end

#flatten_to_array_offset(array, offset) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mittsu/math/matrix3.rb', line 110

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
end

#from_array(array) ⇒ Object



144
145
146
147
# File 'lib/mittsu/math/matrix3.rb', line 144

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

#identityObject



22
23
24
25
26
27
28
29
# File 'lib/mittsu/math/matrix3.rb', line 22

def identity
  self.set(
    1.0, 0.0, 0.0,
    0.0, 1.0, 0.0,
    0.0, 0.0, 1.0
  )
  self
end

#inverse(matrix, throw_on_invertible = false) ⇒ Object



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

def inverse(matrix, throw_on_invertible = false)
  # input: Mittsu::Matrix4
  # (based on http:#code.google.com/p/webgl-mjs/)
  me = matrix.elements
  te = self.elements
  te[0] =   me[10] * me[5] - me[6] * me[9]
  te[1] = - me[10] * me[1] + me[2] * me[9]
  te[2] =   me[6] * me[1] - me[2] * me[5]
  te[3] = - me[10] * me[4] + me[6] * me[8]
  te[4] =   me[10] * me[0] - me[2] * me[8]
  te[5] = - me[6] * me[0] + me[2] * me[4]
  te[6] =   me[9] * me[4] - me[5] * me[8]
  te[7] = - me[9] * me[0] + me[1] * me[8]
  te[8] =   me[5] * me[0] - me[1] * me[4]
  det = me[0] * te[0] + me[1] * te[3] + me[2] * te[6]
  # no inverse
  if det.zero?
    msg = "Mittsu::Matrix3#inverse: can't invert matrix, determinant is 0"
    if throw_on_invertible
      raise Error.new(msg)
    else
      puts "WARNING: #{msg}"
      # THREE.warn(msg)
    end
    self.identity
    return self
  end
  self.multiply_scalar(1.0 / det)
  self
end

#multiply_scalar(s) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/mittsu/math/matrix3.rb', line 58

def multiply_scalar(s)
  te = self.elements
  te[0] *= s; te[3] *= s; te[6] *= s
  te[1] *= s; te[4] *= s; te[7] *= s
  te[2] *= s; te[5] *= s; te[8] *= s
  self
end

#normal_matrix(m) ⇒ Object



124
125
126
127
128
# File 'lib/mittsu/math/matrix3.rb', line 124

def normal_matrix(m)
  # input: THREE.Matrix4
  self.inverse(m).transpose
  self
end

#set(n11, n12, n13, n21, n22, n23, n31, n32, n33) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/mittsu/math/matrix3.rb', line 14

def set(n11, n12, n13, n21, n22, n23, n31, n32, n33)
  te = self.elements
  te[0] = n11.to_f; te[3] = n12.to_f; te[6] = n13.to_f
  te[1] = n21.to_f; te[4] = n22.to_f; te[7] = n23.to_f
  te[2] = n31.to_f; te[5] = n32.to_f; te[8] = n33.to_f
  self
end

#to_aObject



149
150
151
152
153
154
155
156
# File 'lib/mittsu/math/matrix3.rb', line 149

def to_a
  te = self.elements
  [
    te[0], te[1], te[2],
    te[3], te[4], te[5],
    te[6], te[7], te[8]
  ]
end

#transposeObject



102
103
104
105
106
107
108
# File 'lib/mittsu/math/matrix3.rb', line 102

def transpose
  m = self.elements
  tmp = m[1]; m[1] = m[3]; m[3] = tmp
  tmp = m[2]; m[2] = m[6]; m[6] = tmp
  tmp = m[5]; m[5] = m[7]; m[7] = tmp
  self
end

#transpose_into_array(r) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/mittsu/math/matrix3.rb', line 130

def transpose_into_array(r)
  m = self.elements
  r[0] = m[0]
  r[1] = m[3]
  r[2] = m[6]
  r[3] = m[1]
  r[4] = m[4]
  r[5] = m[7]
  r[6] = m[2]
  r[7] = m[5]
  r[8] = m[8]
  self
end