Class: Mittsu::Matrix3
- Inherits:
-
Object
- Object
- Mittsu::Matrix3
- Defined in:
- lib/mittsu/math/matrix3.rb
Constant Summary collapse
- DIMENSIONS =
3
Instance Attribute Summary collapse
-
#elements ⇒ Object
Returns the value of attribute elements.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #apply_to_vector3_array(array, offset = 0, length = array.length) ⇒ Object
- #clone ⇒ Object
- #copy(m) ⇒ Object
- #determinant ⇒ Object
- #flatten_to_array_offset(array, offset) ⇒ Object
- #from_array(array) ⇒ Object
- #identity ⇒ Object
-
#initialize ⇒ Matrix3
constructor
A new instance of Matrix3.
- #inverse(matrix, throw_on_invertible = false) ⇒ Object
- #multiply_scalar(s) ⇒ Object
- #normal_matrix(m) ⇒ Object
- #set(n11, n12, n13, n21, n22, n23, n31, n32, n33) ⇒ Object
- #to_a ⇒ Object
- #transpose ⇒ Object
- #transpose_into_array(r) ⇒ Object
Constructor Details
#initialize ⇒ Matrix3
Returns a new instance of Matrix3.
9 10 11 12 13 14 15 |
# File 'lib/mittsu/math/matrix3.rb', line 9 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
#elements ⇒ Object
Returns the value of attribute elements.
5 6 7 |
# File 'lib/mittsu/math/matrix3.rb', line 5 def elements @elements end |
Instance Method Details
#==(other) ⇒ Object
147 148 149 |
# File 'lib/mittsu/math/matrix3.rb', line 147 def ==(other) other.elements == @elements end |
#apply_to_vector3_array(array, offset = 0, length = array.length) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mittsu/math/matrix3.rb', line 44 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 |
#clone ⇒ Object
165 166 167 |
# File 'lib/mittsu/math/matrix3.rb', line 165 def clone Mittsu::Matrix3.new.from_array(self.elements) end |
#copy(m) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/mittsu/math/matrix3.rb', line 34 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 |
#determinant ⇒ Object
69 70 71 72 |
# File 'lib/mittsu/math/matrix3.rb', line 69 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
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/mittsu/math/matrix3.rb', line 113 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
151 152 153 154 |
# File 'lib/mittsu/math/matrix3.rb', line 151 def from_array(array) self.elements[0..array.length] = (array) self end |
#identity ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/mittsu/math/matrix3.rb', line 25 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
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 |
# File 'lib/mittsu/math/matrix3.rb', line 74 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
61 62 63 64 65 66 67 |
# File 'lib/mittsu/math/matrix3.rb', line 61 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
127 128 129 130 131 |
# File 'lib/mittsu/math/matrix3.rb', line 127 def normal_matrix(m) # input: THREE.Matrix4 self.inverse(m).transpose self end |
#set(n11, n12, n13, n21, n22, n23, n31, n32, n33) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/mittsu/math/matrix3.rb', line 17 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_a ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/mittsu/math/matrix3.rb', line 156 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 |
#transpose ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/mittsu/math/matrix3.rb', line 105 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
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mittsu/math/matrix3.rb', line 133 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 |