Class: Mittsu::Quaternion

Inherits:
Object
  • Object
show all
Defined in:
lib/mittsu/math/quaternion.rb

Constant Summary collapse

EPS =
0.000001

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0.0, y = 0.0, z = 0.0, w = 1.0) ⇒ Quaternion

Returns a new instance of Quaternion.



7
8
9
10
# File 'lib/mittsu/math/quaternion.rb', line 7

def initialize(x = 0.0, y = 0.0, z = 0.0, w = 1.0)
  @x, @y, @z, @w = x, y, z, w
  @on_change_callback = nil
end

Instance Attribute Details

#wObject

Returns the value of attribute w.



5
6
7
# File 'lib/mittsu/math/quaternion.rb', line 5

def w
  @w
end

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/mittsu/math/quaternion.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/mittsu/math/quaternion.rb', line 5

def y
  @y
end

#zObject

Returns the value of attribute z.



5
6
7
# File 'lib/mittsu/math/quaternion.rb', line 5

def z
  @z
end

Class Method Details

.slerp(qa, qb, qm, t) ⇒ Object



303
304
305
# File 'lib/mittsu/math/quaternion.rb', line 303

def self.slerp(qa, qb, qm, t)
  qm.copy(qa).slerp(qb, t)
end

Instance Method Details

#==(quaternion) ⇒ Object



268
269
270
# File 'lib/mittsu/math/quaternion.rb', line 268

def ==(quaternion)
  (quaternion.x == @x) && (quaternion.y == @y) && (quaternion.z == @z) && (quaternion.w == @w)
end

#cloneObject



299
300
301
# File 'lib/mittsu/math/quaternion.rb', line 299

def clone
  Mittsu::Quaternion.new(@x, @y, @z, @w)
end

#conjugateObject



173
174
175
176
177
178
179
# File 'lib/mittsu/math/quaternion.rb', line 173

def conjugate
  @x *= -1.0
  @y *= -1.0
  @z *= -1.0
  self.on_change_callback
  self
end

#copy(quaternion) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/mittsu/math/quaternion.rb', line 41

def copy(quaternion)
  @x = quaternion.x
  @y = quaternion.y
  @z = quaternion.z
  @w = quaternion.w
  self.on_change_callback
  self
end

#dot(v) ⇒ Object



181
182
183
# File 'lib/mittsu/math/quaternion.rb', line 181

def dot(v)
  @x * v._x + @y * v._y + @z * v._z + @w * v._w
end

#from_array(array, offset = 0) ⇒ Object



272
273
274
275
276
277
278
279
# File 'lib/mittsu/math/quaternion.rb', line 272

def from_array(array, offset = 0)
  @x = array[offset]
  @y = array[offset + 1]
  @z = array[offset + 2]
  @w = array[offset + 3]
  self.on_change_callback
  self
end

#inverseObject



168
169
170
171
# File 'lib/mittsu/math/quaternion.rb', line 168

def inverse
  self.conjugate.normalize
  self
end

#lengthObject



189
190
191
# File 'lib/mittsu/math/quaternion.rb', line 189

def length
  ::Math.sqrt(@x * @x + @y * @y + @z * @z + @w * @w)
end

#length_sqObject



185
186
187
# File 'lib/mittsu/math/quaternion.rb', line 185

def length_sq
  @x * @x + @y * @y + @z * @z + @w * @w
end

#multiply(q) ⇒ Object



211
212
213
# File 'lib/mittsu/math/quaternion.rb', line 211

def multiply(q)
  self.multiply_quaternions(self, q)
end

#multiply_quaternions(a, b) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/mittsu/math/quaternion.rb', line 215

def multiply_quaternions(a, b)
  # from http:#www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  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
  self.on_change_callback
  self
end

#normalizeObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/mittsu/math/quaternion.rb', line 193

def normalize
  l = self.length
  if l == 0.0
    @x = 0.0
    @y = 0.0
    @z = 0.0
    @w = 1.0
  else
    l = 1.0 / l
    @x = @x * l
    @y = @y * l
    @z = @z * l
    @w = @w * l
  end
  self.on_change_callback
  self
end

#on_change(&callback) ⇒ Object



289
290
291
292
# File 'lib/mittsu/math/quaternion.rb', line 289

def on_change(&callback)
  @on_change_callback = callback
  self
end

#on_change_callbackObject



294
295
296
297
# File 'lib/mittsu/math/quaternion.rb', line 294

def on_change_callback
  return unless @on_change_callback
  @on_change_callback.call
end

#set(x, y, z, w) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/mittsu/math/quaternion.rb', line 12

def set(x, y, z, w)
  @x = x.to_f
  @y = y.to_f
  @z = z.to_f
  @w = w.to_f
  self.on_change_callback
  self
end

#set_from_axis_angle(axis, angle) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mittsu/math/quaternion.rb', line 95

def set_from_axis_angle(axis, angle)
  # http:#www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  # assumes axis is normalized
  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)
  self.on_change_callback
  self
end

#set_from_euler(euler, update = true) ⇒ Object



50
51
52
53
54
55
56
57
58
59
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
# File 'lib/mittsu/math/quaternion.rb', line 50

def set_from_euler(euler, update = true)
  # http:#www.mathworks.com/matlabcentral/fileexchange/
  #   20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  #  content/SpinCalc.m
  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)
  if euler.order == '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
  elsif euler.order == '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
  elsif euler.order == '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
  elsif euler.order == '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
  elsif euler.order == '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
  elsif euler.order == '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
  end
  self.on_change_callback if update
  self
end

#set_from_rotation_matrix(m) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
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
# File 'lib/mittsu/math/quaternion.rb', line 108

def set_from_rotation_matrix(m)
  # http:#www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  # assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  te = m.elements
  m11 = te[0]; m12 = te[4]; m13 = te[8]
  m21 = te[1]; m22 = te[5]; m23 = te[9]
  m31 = te[2]; m32 = te[6]; m33 = te[10]
  trace = m11 + m22 + m33
  if trace > 0
    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
  self.on_change_callback
  self
end

#set_from_unit_vectors(v_from, v_to) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/mittsu/math/quaternion.rb', line 145

def set_from_unit_vectors(v_from, v_to)
  # http:#lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
  # assumes direction vectors v_from and v_to are normalized
  v1 = Mittsu::Vector3.new
  r = v_from.dot(v_to) + 1.0
  if r < EPS
    r = 0.0
    if v_from.x.abs > v_from.z.abs
      v1.set(-v_from.y, v_from.x, 0.0)
    else
      v1.set(0.0, -v_from.z, v_from.y)
    end
  else
    v1.cross_vectors(v_from, v_to)
  end
  @x = v1.x
  @y = v1.y
  @z = v1.z
  @w = r
  self.normalize
  self
end

#slerp(qb, t) ⇒ Object



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
259
260
261
262
263
264
265
266
# File 'lib/mittsu/math/quaternion.rb', line 227

def slerp(qb, t)
  return self if t.zero?
  return self.copy(qb) if t == 1.0
  _x, _y, _z, _w = @x, @y, @z, @w
  # http:#www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  cos_half_theta = _w * qb.w + _x * qb.x + _y * qb.y + _z * qb.z
  if cos_half_theta < 0.0
    @w = -qb.w
    @x = -qb.x
    @y = -qb.y
    @z = -qb.z
    cos_half_theta = - cos_half_theta
  else
    self.copy(qb)
  end
  if cos_half_theta >= 1.0
    @w = _w
    @x = _x
    @y = _y
    @z = _z
    return self
  end
  half_theta = ::Math.acos(cos_half_theta)
  sin_half_theta = ::Math.sqrt(1.0 - cos_half_theta * cos_half_theta)
  if sin_half_theta.abs < 0.001
    @w = 0.5 * (_w + @w)
    @x = 0.5 * (_x + @x)
    @y = 0.5 * (_y + @y)
    @z = 0.5 * (_z + @z)
    return self
  end
  ratio_a = ::Math.sin((1.0. - t) * half_theta) / sin_half_theta,
  ratio_b = ::Math.sin(t * half_theta) / sin_half_theta
  @w = (_w * ratio_a + @w * ratio_b)
  @x = (_x * ratio_a + @x * ratio_b)
  @y = (_y * ratio_a + @y * ratio_b)
  @z = (_z * ratio_a + @z * ratio_b)
  self.on_change_callback
  self
end

#to_array(array = [], offset = 0) ⇒ Object



281
282
283
284
285
286
287
# File 'lib/mittsu/math/quaternion.rb', line 281

def to_array(array = [], offset = 0)
  array[offset] = @x
  array[offset + 1] = @y
  array[offset + 2] = @z
  array[offset + 3] = @w
  array
end