Class: OpenC3::Quaternion
Overview
A quaternion where q is the scalar component
Instance Attribute Summary collapse
-
#data ⇒ Array<Float, Float, Float, Float>
the last element is the scalar.
Class Method Summary collapse
- .arc(f, t) ⇒ Object
-
.qfromc(rotation_matrix) ⇒ Quaternion
Create a quaternion from a direction-cosine matrix (rotation matrix).
-
.signnz(value) ⇒ Float
The sign of a number as 1.0 = positive, -1.0 = negative.
Instance Method Summary collapse
-
#*(other) ⇒ Quaternion
(also: #qmult)
New quaternion resulting from the multiplication.
-
#[](index) ⇒ Float
The quaternion component.
- #[]=(index, value) ⇒ Object
-
#initialize(array = [0.0, 0.0, 0.0, 0.0], angle = nil) ⇒ Quaternion
constructor
Create a Quaternion given the initial components.
-
#inverse ⇒ Quaternion
(also: #inv)
The inverse of the current quaternion.
-
#normalize ⇒ Quaternion
The normalized version of the current quaternion.
-
#q0 ⇒ Float
(also: #x)
The first element.
- #q0=(value) ⇒ Object
-
#q1 ⇒ Float
(also: #y)
The second element.
- #q1=(value) ⇒ Object
-
#q2 ⇒ Float
(also: #z)
The third element.
- #q2=(value) ⇒ Object
-
#q3 ⇒ Float
(also: #w)
The scalar element.
- #q3=(value) ⇒ Object
-
#to_s ⇒ String
The name of the class and the object_id followed by the data.
-
#vecrot(vector) ⇒ Array<Float, Float, Float>
Rotate a vector using this quaternion.
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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/openc3/utilities/quaternion.rb', line 31 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
Class Method Details
.arc(f, t) ⇒ Object
159 160 161 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 |
# File 'lib/openc3/utilities/quaternion.rb', line 159 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
212 213 214 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 |
# File 'lib/openc3/utilities/quaternion.rb', line 212 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.
199 200 201 202 203 204 205 |
# File 'lib/openc3/utilities/quaternion.rb', line 199 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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/openc3/utilities/quaternion.rb', line 115 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.
55 56 57 |
# File 'lib/openc3/utilities/quaternion.rb', line 55 def [](index) return data[index] end |
#[]=(index, value) ⇒ Object
61 62 63 |
# File 'lib/openc3/utilities/quaternion.rb', line 61 def []=(index, value) @data[index] = value end |
#inverse ⇒ Quaternion Also known as: inv
Returns The inverse of the current quaternion.
132 133 134 |
# File 'lib/openc3/utilities/quaternion.rb', line 132 def inverse Quaternion.new([-@data[0], -@data[1], -@data[2], @data[3]]) end |
#normalize ⇒ Quaternion
Returns The normalized version of the current quaternion.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/openc3/utilities/quaternion.rb', line 138 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 |
#q0 ⇒ Float Also known as: x
Returns The first element.
70 71 72 |
# File 'lib/openc3/utilities/quaternion.rb', line 70 def q0 return @data[0] end |
#q0=(value) ⇒ Object
94 95 96 |
# File 'lib/openc3/utilities/quaternion.rb', line 94 def q0=(value) @data[0] = value end |
#q1 ⇒ Float Also known as: y
Returns The second element.
76 77 78 |
# File 'lib/openc3/utilities/quaternion.rb', line 76 def q1 return @data[1] end |
#q1=(value) ⇒ Object
99 100 101 |
# File 'lib/openc3/utilities/quaternion.rb', line 99 def q1=(value) @data[1] = value end |
#q2 ⇒ Float Also known as: z
Returns The third element.
82 83 84 |
# File 'lib/openc3/utilities/quaternion.rb', line 82 def q2 return @data[2] end |
#q2=(value) ⇒ Object
104 105 106 |
# File 'lib/openc3/utilities/quaternion.rb', line 104 def q2=(value) @data[2] = value end |
#q3 ⇒ Float Also known as: w
Returns The scalar element.
88 89 90 |
# File 'lib/openc3/utilities/quaternion.rb', line 88 def q3 return @data[3] end |
#q3=(value) ⇒ Object
109 110 111 |
# File 'lib/openc3/utilities/quaternion.rb', line 109 def q3=(value) @data[3] = value end |
#to_s ⇒ String
Returns The name of the class and the object_id followed by the data.
49 50 51 |
# File 'lib/openc3/utilities/quaternion.rb', line 49 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
154 155 156 157 |
# File 'lib/openc3/utilities/quaternion.rb', line 154 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 |