Class: Three::Quaternion
- Inherits:
-
Object
- Object
- Three::Quaternion
- Includes:
- Enumerable
- Defined in:
- lib/three/math/quaternion.rb
Instance Attribute Summary collapse
-
#w ⇒ Object
Returns the value of attribute w.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #clone ⇒ Object
- #conjugate ⇒ Object
- #copy(quaternion, update: true) ⇒ Object
- #deconstruct ⇒ Object
- #dot(quaternion) ⇒ Object
- #each {|@x| ... } ⇒ Object
- #equals?(quaternion, epsilon: 0.0) ⇒ Boolean
- #identity(update: true) ⇒ Object
-
#initialize(x = 0, y = 0, z = 0, w = 1) ⇒ Quaternion
constructor
A new instance of Quaternion.
- #inspect ⇒ Object
- #invert ⇒ Object
- #length ⇒ Object
- #length_sq ⇒ Object
- #multiply(quaternion) ⇒ Object
- #multiply_quaternions(a, b) ⇒ Object
- #normalize ⇒ Object
- #on_change(&callback) ⇒ Object (also: #_on_change)
- #premultiply(quaternion) ⇒ Object
- #set(x, y, z, w, update: true) ⇒ Object
- #set_from_axis_angle(axis, angle, update: true) ⇒ Object
- #set_from_euler(euler, update: true) ⇒ Object
- #set_from_rotation_matrix(matrix, update: true) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(x = 0, y = 0, z = 0, w = 1) ⇒ Quaternion
Returns a new instance of Quaternion.
11 12 13 14 15 16 17 |
# File 'lib/three/math/quaternion.rb', line 11 def initialize(x = 0, y = 0, z = 0, w = 1) @x = x @y = y @z = z @w = w @on_change_callback = proc {} end |
Instance Attribute Details
#w ⇒ Object
Returns the value of attribute w.
9 10 11 |
# File 'lib/three/math/quaternion.rb', line 9 def w @w end |
#x ⇒ Object
Returns the value of attribute x.
9 10 11 |
# File 'lib/three/math/quaternion.rb', line 9 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
9 10 11 |
# File 'lib/three/math/quaternion.rb', line 9 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
9 10 11 |
# File 'lib/three/math/quaternion.rb', line 9 def z @z end |
Instance Method Details
#==(other) ⇒ Object
239 240 241 |
# File 'lib/three/math/quaternion.rb', line 239 def ==(other) other.is_a?(self.class) && equals?(other) end |
#clone ⇒ Object
52 53 54 |
# File 'lib/three/math/quaternion.rb', line 52 def clone self.class.new(@x, @y, @z, @w) end |
#conjugate ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/three/math/quaternion.rb', line 188 def conjugate @x *= -1 @y *= -1 @z *= -1 changed! self end |
#copy(quaternion, update: true) ⇒ Object
56 57 58 |
# File 'lib/three/math/quaternion.rb', line 56 def copy(quaternion, update: true) set(quaternion.x, quaternion.y, quaternion.z, quaternion.w, update: update) end |
#deconstruct ⇒ Object
263 264 265 |
# File 'lib/three/math/quaternion.rb', line 263 def deconstruct to_a end |
#dot(quaternion) ⇒ Object
200 201 202 |
# File 'lib/three/math/quaternion.rb', line 200 def dot(quaternion) @x * quaternion.x + @y * quaternion.y + @z * quaternion.z + @w * quaternion.w end |
#each {|@x| ... } ⇒ Object
250 251 252 253 254 255 256 257 |
# File 'lib/three/math/quaternion.rb', line 250 def each return enum_for(:each) unless block_given? yield @x yield @y yield @z yield @w end |
#equals?(quaternion, epsilon: 0.0) ⇒ Boolean
232 233 234 235 236 237 |
# File 'lib/three/math/quaternion.rb', line 232 def equals?(quaternion, epsilon: 0.0) (@x - quaternion.x).abs <= epsilon && (@y - quaternion.y).abs <= epsilon && (@z - quaternion.z).abs <= epsilon && (@w - quaternion.w).abs <= epsilon end |
#identity(update: true) ⇒ Object
48 49 50 |
# File 'lib/three/math/quaternion.rb', line 48 def identity(update: true) set(0, 0, 0, 1, update: update) end |
#inspect ⇒ Object
267 268 269 |
# File 'lib/three/math/quaternion.rb', line 267 def inspect "#<#{self.class} x=#{@x.inspect} y=#{@y.inspect} z=#{@z.inspect} w=#{@w.inspect}>" end |
#invert ⇒ Object
196 197 198 |
# File 'lib/three/math/quaternion.rb', line 196 def invert conjugate end |
#length ⇒ Object
208 209 210 |
# File 'lib/three/math/quaternion.rb', line 208 def length Math.sqrt(length_sq) end |
#length_sq ⇒ Object
204 205 206 |
# File 'lib/three/math/quaternion.rb', line 204 def length_sq dot(self) end |
#multiply(quaternion) ⇒ Object
162 163 164 |
# File 'lib/three/math/quaternion.rb', line 162 def multiply(quaternion) multiply_quaternions(self, quaternion) end |
#multiply_quaternions(a, b) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/three/math/quaternion.rb', line 170 def multiply_quaternions(a, b) qax = a.x qay = a.y qaz = a.z qaw = a.w qbx = b.x qby = b.y qbz = b.z qbw = b.w @x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby @y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz @z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx @w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz changed! self end |
#normalize ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/three/math/quaternion.rb', line 212 def normalize current_length = length if current_length.zero? @x = 0 @y = 0 @z = 0 @w = 1 else scale = 1.0 / current_length @x *= scale @y *= scale @z *= scale @w *= scale end changed! self end |
#on_change(&callback) ⇒ Object Also known as: _on_change
243 244 245 246 |
# File 'lib/three/math/quaternion.rb', line 243 def on_change(&callback) @on_change_callback = callback || proc {} self end |
#premultiply(quaternion) ⇒ Object
166 167 168 |
# File 'lib/three/math/quaternion.rb', line 166 def premultiply(quaternion) multiply_quaternions(quaternion, self) end |
#set(x, y, z, w, update: true) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/three/math/quaternion.rb', line 39 def set(x, y, z, w, update: true) @x = x @y = y @z = z @w = w changed! if update self end |
#set_from_axis_angle(axis, angle, update: true) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/three/math/quaternion.rb', line 107 def set_from_axis_angle(axis, angle, update: true) half_angle = angle / 2.0 s = Math.sin(half_angle) @x = axis.x * s @y = axis.y * s @z = axis.z * s @w = Math.cos(half_angle) changed! if update self end |
#set_from_euler(euler, update: true) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/three/math/quaternion.rb', line 60 def set_from_euler(euler, update: true) c1 = Math.cos(euler.x / 2.0) c2 = Math.cos(euler.y / 2.0) c3 = Math.cos(euler.z / 2.0) s1 = Math.sin(euler.x / 2.0) s2 = Math.sin(euler.y / 2.0) s3 = Math.sin(euler.z / 2.0) case euler.order when "XYZ" @x = s1 * c2 * c3 + c1 * s2 * s3 @y = c1 * s2 * c3 - s1 * c2 * s3 @z = c1 * c2 * s3 + s1 * s2 * c3 @w = c1 * c2 * c3 - s1 * s2 * s3 when "YXZ" @x = s1 * c2 * c3 + c1 * s2 * s3 @y = c1 * s2 * c3 - s1 * c2 * s3 @z = c1 * c2 * s3 - s1 * s2 * c3 @w = c1 * c2 * c3 + s1 * s2 * s3 when "ZXY" @x = s1 * c2 * c3 - c1 * s2 * s3 @y = c1 * s2 * c3 + s1 * c2 * s3 @z = c1 * c2 * s3 + s1 * s2 * c3 @w = c1 * c2 * c3 - s1 * s2 * s3 when "ZYX" @x = s1 * c2 * c3 - c1 * s2 * s3 @y = c1 * s2 * c3 + s1 * c2 * s3 @z = c1 * c2 * s3 - s1 * s2 * c3 @w = c1 * c2 * c3 + s1 * s2 * s3 when "YZX" @x = s1 * c2 * c3 + c1 * s2 * s3 @y = c1 * s2 * c3 + s1 * c2 * s3 @z = c1 * c2 * s3 - s1 * s2 * c3 @w = c1 * c2 * c3 - s1 * s2 * s3 when "XZY" @x = s1 * c2 * c3 - c1 * s2 * s3 @y = c1 * s2 * c3 - s1 * c2 * s3 @z = c1 * c2 * s3 + s1 * s2 * c3 @w = c1 * c2 * c3 + s1 * s2 * s3 else raise ArgumentError, "unknown Euler order: #{euler.order}" end changed! if update self end |
#set_from_rotation_matrix(matrix, update: true) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/three/math/quaternion.rb', line 119 def set_from_rotation_matrix(matrix, update: true) elements = matrix.elements m11 = elements[0] m12 = elements[4] m13 = elements[8] m21 = elements[1] m22 = elements[5] m23 = elements[9] m31 = elements[2] m32 = elements[6] m33 = elements[10] trace = m11 + m22 + m33 if trace.positive? s = 0.5 / Math.sqrt(trace + 1.0) @w = 0.25 / s @x = (m32 - m23) * s @y = (m13 - m31) * s @z = (m21 - m12) * s elsif m11 > m22 && m11 > m33 s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33) @w = (m32 - m23) / s @x = 0.25 * s @y = (m12 + m21) / s @z = (m13 + m31) / s elsif m22 > m33 s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33) @w = (m13 - m31) / s @x = (m12 + m21) / s @y = 0.25 * s @z = (m23 + m32) / s else s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22) @w = (m21 - m12) / s @x = (m13 + m31) / s @y = (m23 + m32) / s @z = 0.25 * s end changed! if update self end |
#to_a ⇒ Object
259 260 261 |
# File 'lib/three/math/quaternion.rb', line 259 def to_a [@x, @y, @z, @w] end |