Class: Cosmos::Quaternion

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

Overview

A quaternion where q is the scalar component

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cosmos/utilities/quaternion.rb', line 26

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

Class Method Details

.arc(f, t) ⇒ Object



162
163
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
# File 'lib/cosmos/utilities/quaternion.rb', line 162

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



215
216
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
# File 'lib/cosmos/utilities/quaternion.rb', line 215

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



202
203
204
205
206
207
208
# File 'lib/cosmos/utilities/quaternion.rb', line 202

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 muliplication.

Parameters:

  • other (Quaternion)

    Quaternion to multiply with

Returns:

  • (Quaternion)

    New quaternion resulting from the muliplication



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cosmos/utilities/quaternion.rb', line 118

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



50
51
52
# File 'lib/cosmos/utilities/quaternion.rb', line 50

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

#[]=(index, value) ⇒ Object

Parameters:

  • index (Integer)

    The component to set

  • value (Float)

    The quaternion component



56
57
58
# File 'lib/cosmos/utilities/quaternion.rb', line 56

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

#dataArray<Float, Float, Float, Float>

the last element is the scalar

Returns:

  • (Array<Float, Float, Float, Float>)

    The entire quaternion where the



62
63
64
# File 'lib/cosmos/utilities/quaternion.rb', line 62

def data
  return @data
end

#data=(array) ⇒ Object

where the the last element is the scalar

Parameters:

  • array (Array<Float, Float, Float, Float>)

    The entire quaternion



68
69
70
# File 'lib/cosmos/utilities/quaternion.rb', line 68

def data=(array)
  @data = array
end

#inverseQuaternion Also known as: inv

Returns The inverse of the current quaternion.

Returns:

  • (Quaternion)

    The inverse of the current quaternion



135
136
137
# File 'lib/cosmos/utilities/quaternion.rb', line 135

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



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cosmos/utilities/quaternion.rb', line 141

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



73
74
75
# File 'lib/cosmos/utilities/quaternion.rb', line 73

def q0
  return @data[0]
end

#q0=(value) ⇒ Object

Parameters:

  • value (Float)

    Set the first element



97
98
99
# File 'lib/cosmos/utilities/quaternion.rb', line 97

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

#q1Float Also known as: y

Returns The second element.

Returns:

  • (Float)

    The second element



79
80
81
# File 'lib/cosmos/utilities/quaternion.rb', line 79

def q1
  return @data[1]
end

#q1=(value) ⇒ Object

Parameters:

  • value (Float)

    Set the second element



102
103
104
# File 'lib/cosmos/utilities/quaternion.rb', line 102

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

#q2Float Also known as: z

Returns The third element.

Returns:

  • (Float)

    The third element



85
86
87
# File 'lib/cosmos/utilities/quaternion.rb', line 85

def q2
  return @data[2]
end

#q2=(value) ⇒ Object

Parameters:

  • value (Float)

    Set the third element



107
108
109
# File 'lib/cosmos/utilities/quaternion.rb', line 107

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

#q3Float Also known as: w

Returns The scalar element.

Returns:

  • (Float)

    The scalar element



91
92
93
# File 'lib/cosmos/utilities/quaternion.rb', line 91

def q3
  return @data[3]
end

#q3=(value) ⇒ Object

Parameters:

  • value (Float)

    Set the scalar element



112
113
114
# File 'lib/cosmos/utilities/quaternion.rb', line 112

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



44
45
46
# File 'lib/cosmos/utilities/quaternion.rb', line 44

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

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

Rotate a vector using this quaternion

Parameters:

  • vector (Array<Float, Float Float>)

    Vector to rotate

Returns:

  • (Array<Float, Float, Float>)

    New rotated vector



157
158
159
160
# File 'lib/cosmos/utilities/quaternion.rb', line 157

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