Class: Engine::Quaternion

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

Constant Summary collapse

EULER_ORDER =
"XYZ"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(w, x, y, z) ⇒ Quaternion

Returns a new instance of Quaternion.



8
9
10
11
12
13
# File 'lib/engine/quaternion.rb', line 8

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

Instance Attribute Details

#euler_orderObject

Returns the value of attribute euler_order.



6
7
8
# File 'lib/engine/quaternion.rb', line 6

def euler_order
  @euler_order
end

#wObject

Returns the value of attribute w.



6
7
8
# File 'lib/engine/quaternion.rb', line 6

def w
  @w
end

#xObject

Returns the value of attribute x.



6
7
8
# File 'lib/engine/quaternion.rb', line 6

def x
  @x
end

#yObject

Returns the value of attribute y.



6
7
8
# File 'lib/engine/quaternion.rb', line 6

def y
  @y
end

#zObject

Returns the value of attribute z.



6
7
8
# File 'lib/engine/quaternion.rb', line 6

def z
  @z
end

Class Method Details

.from_angle_axis(angle, axis) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/engine/quaternion.rb', line 204

def self.from_angle_axis(angle, axis)
  axis = axis.normalize
  angle = angle * Math::PI / 180

  half_angle = angle / 2
  sin_half_angle = Math.sin(half_angle)
  cos_half_angle = Math.cos(half_angle)

  Quaternion.new(
    cos_half_angle,
    axis[0] * sin_half_angle,
    axis[1] * sin_half_angle,
    axis[2] * sin_half_angle
  )
end

.from_euler(euler) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/engine/quaternion.rb', line 29

def self.from_euler(euler)
  e_1 = euler[0] * Math::PI / 180
  e_2 = euler[1] * Math::PI / 180
  e_3 = euler[2] * Math::PI / 180

  c_1 = Math.cos(e_1 / 2)
  c_2 = Math.cos(e_2 / 2)
  c_3 = Math.cos(e_3 / 2)

  s_1 = Math.sin(e_1 / 2)
  s_2 = Math.sin(e_2 / 2)
  s_3 = Math.sin(e_3 / 2)

  case EULER_ORDER
  when "XYZ"
    Quaternion.new(
      c_1 * c_2 * c_3 - s_1 * s_2 * s_3,
      s_1 * c_2 * c_3 + c_1 * s_2 * s_3,
      c_1 * s_2 * c_3 - s_1 * c_2 * s_3,
      c_1 * c_2 * s_3 + s_1 * s_2 * c_3
    )
  when "YXZ"
    Quaternion.new(
      c_1 * c_2 * c_3 + s_1 * s_2 * s_3,
      s_1 * c_2 * c_3 + c_1 * s_2 * s_3,
      c_1 * s_2 * c_3 - s_1 * c_2 * s_3,
      c_1 * c_2 * s_3 - s_1 * s_2 * c_3
    )
  when "ZXY"
    Quaternion.new(
      c_1 * c_2 * c_3 - s_1 * s_2 * s_3,
      s_1 * c_2 * c_3 - c_1 * s_2 * s_3,
      c_1 * s_2 * c_3 + s_1 * c_2 * s_3,
      c_1 * c_2 * s_3 + s_1 * s_2 * c_3
    )
  when "ZYX"
    Quaternion.new(
      c_1 * c_2 * c_3 + s_1 * s_2 * s_3,
      s_1 * c_2 * c_3 - c_1 * s_2 * s_3,
      c_1 * s_2 * c_3 + s_1 * c_2 * s_3,
      c_1 * c_2 * s_3 - s_1 * s_2 * c_3
    )
  when "YZX"
    Quaternion.new(
      s_1 * c_2 * c_3 + c_1 * s_2 * s_3,
      c_1 * s_2 * c_3 + s_1 * c_2 * s_3,
      c_1 * c_2 * s_3 - s_1 * s_2 * c_3,
      c_1 * c_2 * c_3 - s_1 * s_2 * s_3
    )
  when "XZY"
    Quaternion.new(
      c_1 * c_2 * c_3 + s_1 * s_2 * s_3,
      s_1 * c_2 * c_3 - c_1 * s_2 * s_3,
      c_1 * s_2 * c_3 - s_1 * c_2 * s_3,
      c_1 * c_2 * s_3 + s_1 * s_2 * c_3
    )
  end
end

Instance Method Details

#*(other) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/engine/quaternion.rb', line 19

def *(other)
  Quaternion.new(
    @w * other.w - @x * other.x - @y * other.y - @z * other.z,
    @w * other.x + @x * other.w + @y * other.z - @z * other.y,
    @w * other.y - @x * other.z + @y * other.w + @z * other.x,
    @w * other.z + @x * other.y - @y * other.x + @z * other.w
  )
end

#teObject



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
198
199
200
201
202
# File 'lib/engine/quaternion.rb', line 166

def te
  x2 = x + x
  y2 = y + y
  z2 = z + z

  xx = x * x2
  xy = x * y2
  xz = x * z2
  yy = y * y2
  yz = y * z2
  zz = z * z2
  wx = w * x2
  wy = w * y2
  wz = w * z2

  [
    1 - (yy + zz),
    xy + wz,
    xz - wy,
    0,

    xy - wz,
    1 - (xx + zz),
    yz + wx,
    0,

    xz + wy,
    yz - wx,
    1 - (xx + yy),
    0,

    0,
    0,
    0,
    1,
  ]
end

#to_angle_axisObject



220
221
222
223
224
225
226
# File 'lib/engine/quaternion.rb', line 220

def to_angle_axis
  angle = 2 * Math.acos(@w)
  s = Math.sqrt(1 - @w * @w)
  s = 1 if s < 0.0001
  axis = Vector[@x / s, @y / s, @z / s]
  [angle * 180 / Math::PI, axis]
end

#to_eulerObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/engine/quaternion.rb', line 89

def to_euler
  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]

  case EULER_ORDER
  when "XYZ"
    e_y = Math.asin(m13.clamp(-1, 1))

    if m13.abs < 0.9999999
      e_x = Math.atan2(-m23, m33)
      e_z = Math.atan2(-m12, m11)
    else
      e_x = Math.atan2(m32, m22)
      e_z = 0
    end
  when "YXZ"
    e_x = Math.asin(-m23.clamp(-1, 1))

    if m23.abs < 0.9999999
      e_y = Math.atan2(m13, m33)
      e_z = Math.atan2(m21, m22)
    else
      e_y = Math.atan2(-m31, m11)
      e_z = 0
    end
  when "ZXY"
    e_x = Math.asin(m32.clamp(-1, 1))

    if m32.abs < 0.9999999
      e_y = Math.atan2(-m31, m33)
      e_z = Math.atan2(-m12, m22)
    else
      e_y = 0
      e_z = Math.atan2(m21, m11)
    end
  when "ZYX"
    e_y = Math.asin(-m31.clamp(-1, 1))

    if m31.abs < 0.9999999
      e_x = Math.atan2(m32, m33)
      e_z = Math.atan2(m21, m11)
    else
      e_x = 0
      e_z = Math.atan2(-m12, m22)
    end
  when "YZX"
    e_z = Math.asin(m21.clamp(-1, 1))

    if m21.abs < 0.9999999
      e_x = Math.atan2(-m23, m22)
      e_y = Math.atan2(-m31, m11)
    else
      e_x = 0
      e_y = Math.atan2(m13, m33)
    end
  when "XZY"
    e_z = Math.asin(-m12.clamp(-1, 1))

    if m12.abs < 0.9999999
      e_x = Math.atan2(m32, m22)
      e_y = Math.atan2(m13, m11)
    else
      e_x = Math.atan2(-m23, m33)
      e_y = 0
    end
  end

  Vector[e_x, e_y, e_z] * 180 / Math::PI
end

#to_sObject



15
16
17
# File 'lib/engine/quaternion.rb', line 15

def to_s
  "Quaternion(w: #{@w}, x: #{@x}, y: #{@y}, z: #{@z})"
end