Class: Mittsu::Matrix3

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

Constant Summary collapse

DIMENSIONS =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMatrix3

Returns a new instance of Matrix3.



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

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.



3
4
5
# File 'lib/mittsu/math/matrix3.rb', line 3

def elements
  @elements
end

Instance Method Details

#==(other) ⇒ Object



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

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

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



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

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



163
164
165
# File 'lib/mittsu/math/matrix3.rb', line 163

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

#copy(m) ⇒ Object



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

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



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

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



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

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



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

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

#identityObject



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

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



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

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



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

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



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

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

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



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

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



154
155
156
157
158
159
160
161
# File 'lib/mittsu/math/matrix3.rb', line 154

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



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

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



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

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