Class: Geo3d::Quaternion

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rotation_axis = nil, radians = 0) ⇒ Quaternion

Returns a new instance of Quaternion.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/quaternion.rb', line 5

def initialize rotation_axis = nil, radians = 0
  @x = @y = @z = @w = 0

  if rotation_axis

    normalized_rotation_axis = rotation_axis.normalize
    #const float radians = GeoConvertToRadians( degrees );
    @x = Math.sin(radians / 2.0) * normalized_rotation_axis.x
    @y = Math.sin(radians / 2.0) * normalized_rotation_axis.y
    @z = Math.sin(radians / 2.0) * normalized_rotation_axis.z
    @w = Math.cos(radians / 2.0);
  end
end

Instance Attribute Details

#wObject (readonly)

Returns the value of attribute w.



3
4
5
# File 'lib/quaternion.rb', line 3

def w
  @w
end

#xObject (readonly)

Returns the value of attribute x.



3
4
5
# File 'lib/quaternion.rb', line 3

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



3
4
5
# File 'lib/quaternion.rb', line 3

def y
  @y
end

#zObject (readonly)

Returns the value of attribute z.



3
4
5
# File 'lib/quaternion.rb', line 3

def z
  @z
end

Instance Method Details

#*(quat) ⇒ Object



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

def * quat
  out = Quat.new
  out.w = w * quat.w - x * quat.x - y * quat.y - z * quat.z
  out.x = w * quat.x + x * quat.w + y * quat.z - z * quat.y
  out.y = w * quat.y - x * quat.z + y * quat.w + z * quat.x
  out.z = w * quat.z + x * quat.y - y * quat.x + z * quat.w
  out
end

#angleObject



47
48
49
# File 'lib/quaternion.rb', line 47

def angle
  Math.acos(w) * 2.0
end

#axisObject



43
44
45
# File 'lib/quaternion.rb', line 43

def axis
  Vector.new(x, y, z).normalize
end

#to_matrixObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/quaternion.rb', line 28

def to_matrix
  v = Vector.new(x, y, z, w); ## Normalize();
  matrix = Matrix.identity
  matrix._11 = 1.0 - 2.0 * (v.y * v.y + v.z * v.z)
  matrix._12 = 2.0 * (v.x * v.y + v.z * v.w)
  matrix._13 = 2.0 * (v.x * v.z - v.y * v.w)
  matrix._21 = 2.0 * (v.x * v.y - v.z * v.w)
  matrix._22 = 1.0 - 2.0 * (v.x * v.x + v.z * v.z)
  matrix._23 = 2.0 * (v.y * v.z + v.x * v.w)
  matrix._31 = 2.0 * (v.x * v.z + v.y * v.w)
  matrix._32 = 2.0 * (v.y * v.z - v.x * v.w)
  matrix._33 = 1.0 - 2.0 * (v.x * v.x + v.y * v.y)
  matrix
end