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.



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

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

Instance Attribute Details

#wObject

Returns the value of attribute w.



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

def w
  @w
end

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

#zObject

Returns the value of attribute z.



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

def z
  @z
end

Class Method Details

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



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

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

Instance Method Details

#==(quaternion) ⇒ Object



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

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

#cloneObject



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

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

#conjugateObject



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

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

#copy(quaternion) ⇒ Object



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

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

#dot(v) ⇒ Object



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

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

#from_array(array, offset = 0) ⇒ Object



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

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



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

def inverse
  self.conjugate.normalize
  self
end

#lengthObject



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

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

#length_sqObject



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

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

#multiply(q) ⇒ Object



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

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

#multiply_quaternions(a, b) ⇒ Object



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

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



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

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



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

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

#on_change_callbackObject



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

def on_change_callback
  return unless @on_change_callback
  @on_change_callback.call
end

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



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

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



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

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



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
94
# File 'lib/mittsu/math/quaternion.rb', line 51

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



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
144
# File 'lib/mittsu/math/quaternion.rb', line 109

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



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

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



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
267
# File 'lib/mittsu/math/quaternion.rb', line 228

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



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

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