Class: OpenC3::Quaternion

Inherits:
Object show all
Defined in:
lib/openc3/utilities/quaternion.rb

Overview

A quaternion where q is the scalar component

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array = [0.0, 0.0, 0.0, 0.0], angle = nil) ⇒ Quaternion

Create a Quaternion given the initial components

the forth value is the scalar or [Array<Float, Float, Float>] which as an axis of rotation

Parameters:

  • array (Array<Float, Float, Float, Float>) (defaults to: [0.0, 0.0, 0.0, 0.0])

    Initial values where

  • angle (Float) (defaults to: nil)

    if axis given for array parameter



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/openc3/utilities/quaternion.rb', line 36

def initialize(array = [0.0, 0.0, 0.0, 0.0], angle = nil)
  if array.length == 4
    @data = array.clone
  elsif array.length == 3 and angle
    a = 0.5 * angle
    s = sin(a) / sqrt((array[0] * array[0]) + (array[1] * array[1]) + (array[2] * array[2]))
    @data = []
    @data[0] = array[0] * s
    @data[1] = array[1] * s
    @data[2] = array[2] * s
    @data[3] = cos(a)
  else
    raise "Invalid arguments given to Quaternion.new"
  end
end

Instance Attribute Details

#dataArray<Float, Float, Float, Float>

the last element is the scalar

Returns:



72
73
74
# File 'lib/openc3/utilities/quaternion.rb', line 72

def data
  @data
end

Class Method Details

.arc(f, t) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/openc3/utilities/quaternion.rb', line 164

def self.arc(f, t)
  dot = (f[0] * t[0]) + (f[1] * t[1]) + (f[2] * t[2])
  if dot > 0.999999
    x = 0.0
    y = 0.0
    z = 0.0
    w = 1.0
  elsif dot < -0.999999
    if (f.z.abs < f.x.abs) && (f.z.abs < f.y.abs)
      x = (f[0] * f[2]) - (f[2] * f[1])
      y = (f[2] * f[0]) + (f[1] * f[2])
      z = (-f[1] * f[1]) - (f[0] * f[0])
    elsif f.y.abs < f.x.abs
      x = (f[1] * f[2]) - (f[0] * f[1])
      y = (f[0] * f[0]) + (f[2] * f[2])
      z = (-f[2] * f[1]) - (f[1] * f[0])
    else
      x = (-f[2] * f[2]) - (f[1] * f[1])
      y = (f[1] * f[0]) - (f[0] * f[2])
      z = (f[0] * f[1]) + (f[2] * f[0])
    end

    dot = (x * x) + (y * y) + (z * z)
    div = sqrt(dot)
    x /= div
    y /= div
    z /= div
    w = 0.0
  else
    div = sqrt((dot + 1.0) * 2.0)
    x = ((f[1] * t[2]) - (f[2] * t[1])) / div
    y = ((f[2] * t[0]) - (f[0] * t[2])) / div
    z = ((f[0] * t[1]) - (f[1] * t[0])) / div
    w = div * 0.5
  end
  return Quaternion.new([x, y, z, w])
end

.qfromc(rotation_matrix) ⇒ Quaternion

Create a quaternion from a direction-cosine matrix (rotation matrix). Reference Article: J. Spacecraft Vol.13, No.12 Dec.1976 p754

Parameters:

  • rotation_matrix (Matrix)

    The rotation matrix

Returns:

  • (Quaternion)

    New quaternion resulting from the matrix



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/openc3/utilities/quaternion.rb', line 217

def self.qfromc(rotation_matrix)
  tracec = rotation_matrix.trace()
  p = 1.0 + tracec
  if p < 0.0
    p = 0.0
  end
  q = Quaternion.new([0.0, 0.0, 0.0, sqrt(p) / 2.0])
  if q[3] >= 0.1
    factor = 1.0 / (4.0 * q[3])
    q[0] = (rotation_matrix[1][2] - rotation_matrix[2][1]) * factor
    q[1] = (rotation_matrix[2][0] - rotation_matrix[0][2]) * factor
    q[2] = (rotation_matrix[0][1] - rotation_matrix[1][0]) * factor
  else # For rotations near 180 degrees
    q[0] = sqrt(((2.0 * rotation_matrix[0][0]) + 1.0 - tracec) / 4.0)
    q[1] = sqrt(((2.0 * rotation_matrix[1][1]) + 1.0 - tracec) / 4.0)
    q[2] = sqrt(((2.0 * rotation_matrix[2][2]) + 1.0 - tracec) / 4.0)

    i = 0
    if q[1] >= q[i]
      i = 1
    end
    if q[2] >= q[i]
      i = 2
    end
    case i
    when 0
      q[0] = q[0].abs * Quaternion.signnz(rotation_matrix[1][2] - rotation_matrix[2][1])
      q[1] = q[1].abs * Quaternion.signnz((rotation_matrix[1][0] + rotation_matrix[0][1]) * q[0])
      q[2] = q[2].abs * Quaternion.signnz((rotation_matrix[2][0] + rotation_matrix[0][2]) * q[0])
    when 1
      q[1] = q[1].abs * Quaternion.signnz(rotation_matrix[2][0] - rotation_matrix[0][2])
      q[0] = q[0].abs * Quaternion.signnz((rotation_matrix[1][0] + rotation_matrix[0][1]) * q[1])
      q[2] = q[2].abs * Quaternion.signnz((rotation_matrix[2][1] + rotation_matrix[1][2]) * q[1])
    else
      q[2] = q[2].abs * Quaternion.signnz(rotation_matrix[0][1] - rotation_matrix[1][0])
      q[0] = q[0].abs * Quaternion.signnz((rotation_matrix[0][2] + rotation_matrix[2][0]) * q[2])
      q[1] = q[1].abs * Quaternion.signnz((rotation_matrix[1][2] + rotation_matrix[2][1]) * q[2])
    end
  end

  return q
end

.signnz(value) ⇒ Float

Returns The sign of a number as 1.0 = positive, -1.0 = negative.

Parameters:

Returns:

  • (Float)

    The sign of a number as 1.0 = positive, -1.0 = negative



204
205
206
207
208
209
210
# File 'lib/openc3/utilities/quaternion.rb', line 204

def self.signnz(value)
  if value >= 0.0
    return 1.0
  else
    return -1.0
  end
end

Instance Method Details

#*(other) ⇒ Quaternion Also known as: qmult

Returns New quaternion resulting from the multiplication.

Parameters:

  • other (Quaternion)

    Quaternion to multiply with

Returns:

  • (Quaternion)

    New quaternion resulting from the multiplication



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/openc3/utilities/quaternion.rb', line 120

def *(other)
  q = Quaternion.new()

  q[0] =  (@data[3] * other[0]) - (@data[2] * other[1]) +
          (@data[1] * other[2]) + (@data[0] * other[3])
  q[1] = (@data[2] * other[0]) + (@data[3] * other[1]) -
         (@data[0] * other[2]) + (@data[1] * other[3])
  q[2] = -(@data[1] * other[0]) + (@data[0] * other[1]) +
         (@data[3] * other[2]) + (@data[2] * other[3])
  q[3] = -(@data[0] * other[0]) - (@data[1] * other[1]) -
         (@data[2] * other[2]) + (@data[3] * other[3])

  return q
end

#[](index) ⇒ Float

Returns The quaternion component.

Parameters:

  • index (Integer)

    Which component to access

Returns:

  • (Float)

    The quaternion component



60
61
62
# File 'lib/openc3/utilities/quaternion.rb', line 60

def [](index)
  return data[index]
end

#[]=(index, value) ⇒ Object

Parameters:

  • index (Integer)

    The component to set

  • value (Float)

    The quaternion component



66
67
68
# File 'lib/openc3/utilities/quaternion.rb', line 66

def []=(index, value)
  @data[index] = value
end

#inverseQuaternion Also known as: inv

Returns The inverse of the current quaternion.

Returns:

  • (Quaternion)

    The inverse of the current quaternion



137
138
139
# File 'lib/openc3/utilities/quaternion.rb', line 137

def inverse
  Quaternion.new([-@data[0], -@data[1], -@data[2], @data[3]])
end

#normalizeQuaternion

Returns The normalized version of the current quaternion.

Returns:

  • (Quaternion)

    The normalized version of the current quaternion



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/openc3/utilities/quaternion.rb', line 143

def normalize
  t = (@data[0] * @data[0]) + (@data[1] * @data[1]) + (@data[2] * @data[2]) + (@data[3] * @data[3])
  if t > 0.0
    f = 1.0 / sqrt(t)
    @data[0] *= f
    @data[1] *= f
    @data[2] *= f
    @data[3] *= f
  end
  return self
end

#q0Float Also known as: x

Returns The first element.

Returns:

  • (Float)

    The first element



75
76
77
# File 'lib/openc3/utilities/quaternion.rb', line 75

def q0
  return @data[0]
end

#q0=(value) ⇒ Object

Parameters:

  • value (Float)

    Set the first element



99
100
101
# File 'lib/openc3/utilities/quaternion.rb', line 99

def q0=(value)
  @data[0] = value
end

#q1Float Also known as: y

Returns The second element.

Returns:

  • (Float)

    The second element



81
82
83
# File 'lib/openc3/utilities/quaternion.rb', line 81

def q1
  return @data[1]
end

#q1=(value) ⇒ Object

Parameters:

  • value (Float)

    Set the second element



104
105
106
# File 'lib/openc3/utilities/quaternion.rb', line 104

def q1=(value)
  @data[1] = value
end

#q2Float Also known as: z

Returns The third element.

Returns:

  • (Float)

    The third element



87
88
89
# File 'lib/openc3/utilities/quaternion.rb', line 87

def q2
  return @data[2]
end

#q2=(value) ⇒ Object

Parameters:

  • value (Float)

    Set the third element



109
110
111
# File 'lib/openc3/utilities/quaternion.rb', line 109

def q2=(value)
  @data[2] = value
end

#q3Float Also known as: w

Returns The scalar element.

Returns:

  • (Float)

    The scalar element



93
94
95
# File 'lib/openc3/utilities/quaternion.rb', line 93

def q3
  return @data[3]
end

#q3=(value) ⇒ Object

Parameters:

  • value (Float)

    Set the scalar element



114
115
116
# File 'lib/openc3/utilities/quaternion.rb', line 114

def q3=(value)
  @data[3] = value
end

#to_sString

Returns The name of the class and the object_id followed by the data.

Returns:

  • (String)

    The name of the class and the object_id followed by the data



54
55
56
# File 'lib/openc3/utilities/quaternion.rb', line 54

def to_s
  "#<OpenC3::Quaternion:0x#{self.object_id.to_s(16)}> #{@data}"
end

#vecrot(vector) ⇒ Array<Float, Float, Float>

Rotate a vector using this quaternion

Parameters:

Returns:



159
160
161
162
# File 'lib/openc3/utilities/quaternion.rb', line 159

def vecrot(vector)
  temp_q = self.inverse * (Quaternion.new([vector[0], vector[1], vector[2], 0]) * self)
  return [temp_q[0], temp_q[1], temp_q[2]]
end