Class: Matrix
Overview
OpenC3 specific additions to the Ruby Matrix class
Class Method Summary collapse
- .cfromq(quaternion) ⇒ Object
-
.rot(axis, rotation_angle_in_radians) ⇒ Object
Creates a rotation matrix around one axis as defined by: mathworld.wolfram.com/RotationMatrix.html.
Instance Method Summary collapse
-
#[](i, j = nil) ⇒ Object
Allow [i] to return an entire row instead of being forced to pass both the row and column (i.e [i,j]) to return an individual element.
-
#[]=(i, j, value) ⇒ Object
Allow [i,j] = x to set the element at row i, column j to value x.
- #rot4(quaternion) ⇒ Object
- #scale4(x, y, z) ⇒ Object
-
#trace ⇒ Object
Sums the diagonal values of the matrix.
- #trans4(x, y, z) ⇒ Object
Class Method Details
.cfromq(quaternion) ⇒ Object
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 |
# File 'lib/openc3/core_ext/matrix.rb', line 99 def self.cfromq(quaternion) result = Matrix.zero(3) tx = 2.0 * quaternion.x ty = 2.0 * quaternion.y tz = 2.0 * quaternion.z twx = tx * quaternion.w twy = ty * quaternion.w twz = tz * quaternion.w txx = tx * quaternion.x txy = ty * quaternion.x txz = tz * quaternion.x tyy = ty * quaternion.y tyz = tz * quaternion.y tzz = tz * quaternion.z result[0][0] = 1.0 - tyy - tzz result[0][1] = txy + twz result[0][2] = txz - twy result[1][0] = txy - twz result[1][1] = 1.0 - txx - tzz result[1][2] = tyz + twx result[2][0] = txz + twy result[2][1] = tyz - twx result[2][2] = 1.0 - txx - tyy return result end |
.rot(axis, rotation_angle_in_radians) ⇒ Object
Creates a rotation matrix around one axis as defined by: mathworld.wolfram.com/RotationMatrix.html
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/openc3/core_ext/matrix.rb', line 61 def self.rot(axis, rotation_angle_in_radians) rotation_matrix = Matrix.identity(3).to_a case axis when :X, :x, 1 rotation_matrix[1][1] = cos(rotation_angle_in_radians) rotation_matrix[1][2] = sin(rotation_angle_in_radians) rotation_matrix[2][2] = rotation_matrix[1][1] rotation_matrix[2][1] = -(rotation_matrix[1][2]) when :Y, :y, 2 rotation_matrix[0][0] = cos(rotation_angle_in_radians) rotation_matrix[2][0] = sin(rotation_angle_in_radians) rotation_matrix[2][2] = rotation_matrix[0][0] rotation_matrix[0][2] = -(rotation_matrix[2][0]) when :Z, :z, 3 rotation_matrix[0][0] = cos(rotation_angle_in_radians) rotation_matrix[0][1] = sin(rotation_angle_in_radians) rotation_matrix[1][1] = rotation_matrix[0][0] rotation_matrix[1][0] = -(rotation_matrix[0][1]) end return Matrix[*rotation_matrix] end |
Instance Method Details
#[](i, j = nil) ⇒ Object
Allow [i] to return an entire row instead of being forced to pass both the row and column (i.e [i,j]) to return an individual element.
36 37 38 39 40 41 42 |
# File 'lib/openc3/core_ext/matrix.rb', line 36 def [](i, j = nil) if j @rows[i][j] else @rows[i] end end |
#[]=(i, j, value) ⇒ Object
Allow [i,j] = x to set the element at row i, column j to value x
50 51 52 |
# File 'lib/openc3/core_ext/matrix.rb', line 50 def []=(i, j, value) @rows[i][j] = value end |
#rot4(quaternion) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/openc3/core_ext/matrix.rb', line 143 def rot4(quaternion) # Get rotation matrix r = Matrix.cfromq(quaternion) 4.times do |row| x = @rows[0][row] y = @rows[1][row] z = @rows[2][row] 3.times do |i| @rows[i][row] = x * r[i][0] + y * r[i][1] + z * r[i][2] end end # Ensure the final row is floating point for consistency @rows[3].map! { |x| x.to_f } return self end |
#scale4(x, y, z) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/openc3/core_ext/matrix.rb', line 136 def scale4(x, y, z) @rows[0][0] *= x; @rows[0][1] *= x; @rows[0][2] *= x; @rows[0][3] *= x @rows[1][0] *= y; @rows[1][1] *= y; @rows[1][2] *= y; @rows[1][3] *= y @rows[2][0] *= z; @rows[2][1] *= z; @rows[2][2] *= z; @rows[2][3] *= z return self end |
#trace ⇒ Object
Sums the diagonal values of the matrix
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/openc3/core_ext/matrix.rb', line 86 def trace sum = 0.0 @rows.length.times do |index| value = @rows[index][index] if not value.nil? sum += value else break end end return sum end |
#trans4(x, y, z) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/openc3/core_ext/matrix.rb', line 128 def trans4(x, y, z) @rows[3][0] += x * @rows[0][0] + y * @rows[1][0] + z * @rows[2][0] @rows[3][1] += x * @rows[0][1] + y * @rows[1][1] + z * @rows[2][1] @rows[3][2] += x * @rows[0][2] + y * @rows[1][2] + z * @rows[2][2] @rows[3][3] += x * @rows[0][3] + y * @rows[1][3] + z * @rows[2][3] return self end |