Class: Matrix

Inherits:
Object show all
Defined in:
lib/openc3/core_ext/matrix.rb

Overview

OpenC3 specific additions to the Ruby Matrix class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cfromq(quaternion) ⇒ Object



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
# File 'lib/openc3/core_ext/matrix.rb', line 94

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

Parameters:

  • axis (Symbol)

    Must be :X,:x,1, or :Y,:y,2, or :Z,:z,3

  • rotation_angle_in_radians (Float)

    The rotation angle in radians to rotate the maxtrix about the given axis



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/openc3/core_ext/matrix.rb', line 56

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.

Parameters:

  • i (Integer)

    Row index

  • j (Integer) (defaults to: nil)

    Optional column index. Pass nil to return the entire row given by i.

Returns:

  • Either the row as an Array or the element



31
32
33
34
35
36
37
# File 'lib/openc3/core_ext/matrix.rb', line 31

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

Parameters:

  • i (Integer)

    Row index

  • j (Integer)

    Column index

  • value (Object)

    The value to set



45
46
47
# File 'lib/openc3/core_ext/matrix.rb', line 45

def []=(i, j, value)
  @rows[i][j] = value
end

#rot4(quaternion) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/openc3/core_ext/matrix.rb', line 138

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



131
132
133
134
135
136
# File 'lib/openc3/core_ext/matrix.rb', line 131

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

#traceObject

Sums the diagonal values of the matrix



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/openc3/core_ext/matrix.rb', line 81

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



123
124
125
126
127
128
129
# File 'lib/openc3/core_ext/matrix.rb', line 123

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