Class: Geo3d::Quaternion
- Inherits:
-
Object
- Object
- Geo3d::Quaternion
- Defined in:
- lib/quaternion.rb
Instance Attribute Summary collapse
-
#w ⇒ Object
Returns the value of attribute w.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Class Method Summary collapse
Instance Method Summary collapse
- #*(quat) ⇒ Object
- #angle ⇒ Object
- #axis ⇒ Object
-
#initialize ⇒ Quaternion
constructor
A new instance of Quaternion.
- #to_matrix ⇒ Object
Constructor Details
#initialize ⇒ Quaternion
Returns a new instance of Quaternion.
5 6 7 |
# File 'lib/quaternion.rb', line 5 def initialize @x = @y = @z = @w = 0.0 end |
Instance Attribute Details
#w ⇒ Object
Returns the value of attribute w.
3 4 5 |
# File 'lib/quaternion.rb', line 3 def w @w end |
#x ⇒ Object
Returns the value of attribute x.
3 4 5 |
# File 'lib/quaternion.rb', line 3 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
3 4 5 |
# File 'lib/quaternion.rb', line 3 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
3 4 5 |
# File 'lib/quaternion.rb', line 3 def z @z end |
Class Method Details
.from_axis(rotation_axis, radians = 0) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/quaternion.rb', line 25 def self.from_axis rotation_axis, radians = 0 normalized_rotation_axis = rotation_axis.normalize #const float radians = GeoConvertToRadians( degrees ); q = self.new q.x = Math.sin(radians / 2.0) * normalized_rotation_axis.x q.y = Math.sin(radians / 2.0) * normalized_rotation_axis.y q.z = Math.sin(radians / 2.0) * normalized_rotation_axis.z q.w = Math.cos(radians / 2.0) q end |
.from_matrix(pm) ⇒ Object
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 |
# File 'lib/quaternion.rb', line 37 def self.from_matrix pm pout = self.new trace = pm._11 + pm._22 + pm._33 + 1.0 if trace > 0 pout.x = (pm._23 - pm._32) / (2.0 * Math.sqrt(trace)) pout.y = (pm._31 - pm._13) / (2.0 * Math.sqrt(trace)) pout.z = (pm._12- pm._21) / (2.0 * Math.sqrt(trace)) pout.w = Math.sqrt(trace) / 2.0 return pout end maxi = 0 maxdiag = pm._11 for i in 1..2 if pm[i, i] > maxdiag #todo: indexing might need to be fixed > maxdiag maxi = i maxdiag = pm[i, i] #todo: indexing might need to be fixed end end case maxi when 0 s = 2.0 * Math.sqrt(1.0 + pm._11 - pm._22 - pm._33) pout.x = 0.25 * s pout.y = (pm._12 + pm._21) / s pout.z = (pm._13 + pm._31) / s pout.w = (pm._23 - pm._32) / s when 1 s = 2.0 * Math.sqrt(1.0 + pm._22 - pm._11 - pm._33) pout.x = (pm._12 + pm._21) / s pout.y = 0.25 * s pout.z = (pm._23 + pm._32) / s pout.w = (pm._31 - pm._13) / s when 2 s = 2.0 * Math.sqrt(1.0 + pm._33 - pm._11 - pm._22) pout.x = (pm._13 + pm._31) / s pout.y = (pm._23 + pm._32) / s pout.z = 0.25 * s pout.w = (pm._12 - pm._21) / s end pout end |
Instance Method Details
#*(quat) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/quaternion.rb', line 83 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
115 116 117 |
# File 'lib/quaternion.rb', line 115 def angle Math.acos(w) * 2.0 end |
#axis ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/quaternion.rb', line 107 def axis v = Vector.new v.x = x / Math.sqrt(1-w*w) v.y = y / Math.sqrt(1-w*w) v.z = z / Math.sqrt(1-w*w) v end |
#to_matrix ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/quaternion.rb', line 92 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 |