Class: Cosmos::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 muliplication.
-
#[](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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cosmos/utilities/quaternion.rb', line 33 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
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 196 197 |
# File 'lib/cosmos/utilities/quaternion.rb', line 161 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
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 254 255 |
# File 'lib/cosmos/utilities/quaternion.rb', line 214 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.
201 202 203 204 205 206 207 |
# File 'lib/cosmos/utilities/quaternion.rb', line 201 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/cosmos/utilities/quaternion.rb', line 117 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.
57 58 59 |
# File 'lib/cosmos/utilities/quaternion.rb', line 57 def [](index) return data[index] end |
#[]=(index, value) ⇒ Object
63 64 65 |
# File 'lib/cosmos/utilities/quaternion.rb', line 63 def []=(index, value) @data[index] = value end |
#inverse ⇒ Quaternion Also known as: inv
Returns The inverse of the current quaternion.
134 135 136 |
# File 'lib/cosmos/utilities/quaternion.rb', line 134 def inverse Quaternion.new([-@data[0], -@data[1], -@data[2], @data[3]]) end |
#normalize ⇒ Quaternion
Returns The normalized version of the current quaternion.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/cosmos/utilities/quaternion.rb', line 140 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.
72 73 74 |
# File 'lib/cosmos/utilities/quaternion.rb', line 72 def q0 return @data[0] end |
#q0=(value) ⇒ Object
96 97 98 |
# File 'lib/cosmos/utilities/quaternion.rb', line 96 def q0=(value) @data[0] = value end |
#q1 ⇒ Float Also known as: y
Returns The second element.
78 79 80 |
# File 'lib/cosmos/utilities/quaternion.rb', line 78 def q1 return @data[1] end |
#q1=(value) ⇒ Object
101 102 103 |
# File 'lib/cosmos/utilities/quaternion.rb', line 101 def q1=(value) @data[1] = value end |
#q2 ⇒ Float Also known as: z
Returns The third element.
84 85 86 |
# File 'lib/cosmos/utilities/quaternion.rb', line 84 def q2 return @data[2] end |
#q2=(value) ⇒ Object
106 107 108 |
# File 'lib/cosmos/utilities/quaternion.rb', line 106 def q2=(value) @data[2] = value end |
#q3 ⇒ Float Also known as: w
Returns The scalar element.
90 91 92 |
# File 'lib/cosmos/utilities/quaternion.rb', line 90 def q3 return @data[3] end |
#q3=(value) ⇒ Object
111 112 113 |
# File 'lib/cosmos/utilities/quaternion.rb', line 111 def q3=(value) @data[3] = value end |
#to_s ⇒ String
Returns The name of the class and the object_id followed by the data.
51 52 53 |
# File 'lib/cosmos/utilities/quaternion.rb', line 51 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
156 157 158 159 |
# File 'lib/cosmos/utilities/quaternion.rb', line 156 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 |