Class: Geo3d::Quaternion
- Inherits:
-
Object
- Object
- Geo3d::Quaternion
- Defined in:
- lib/quaternion.rb
Instance Attribute Summary collapse
-
#w ⇒ Object
readonly
Returns the value of attribute w.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
-
#z ⇒ Object
readonly
Returns the value of attribute z.
Instance Method Summary collapse
- #*(quat) ⇒ Object
- #angle ⇒ Object
- #axis ⇒ Object
-
#initialize(rotation_axis = nil, radians = 0) ⇒ Quaternion
constructor
A new instance of Quaternion.
- #to_matrix ⇒ Object
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
#w ⇒ Object (readonly)
Returns the value of attribute w.
3 4 5 |
# File 'lib/quaternion.rb', line 3 def w @w end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
3 4 5 |
# File 'lib/quaternion.rb', line 3 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
3 4 5 |
# File 'lib/quaternion.rb', line 3 def y @y end |
#z ⇒ Object (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 |
#angle ⇒ Object
47 48 49 |
# File 'lib/quaternion.rb', line 47 def angle Math.acos(w) * 2.0 end |
#axis ⇒ Object
43 44 45 |
# File 'lib/quaternion.rb', line 43 def axis Vector.new(x, y, z).normalize end |
#to_matrix ⇒ Object
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 |