Class: Geo3d::Quaternion

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Quaternion

Returns a new instance of Quaternion.



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

def initialize *args
  @x = 0.0
  @y = 0.0
  @z = 0.0
  @w = 0.0
  @x = args[0].to_f if args.size > 0
  @y = args[1].to_f if args.size > 1
  @z = args[2].to_f if args.size > 2
  @w = args[3].to_f if args.size > 3
end

Instance Attribute Details

#wObject

Returns the value of attribute w.



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

def w
  @w
end

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

#zObject

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/quaternion.rb', line 48

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_axis_degrees(rotation_axis, degrees = 0) ⇒ Object



60
61
62
# File 'lib/quaternion.rb', line 60

def self.from_axis_degrees rotation_axis, degrees = 0
  from_axis rotation_axis, Geo3d::Utils.to_radians(degrees)
end

.from_matrix(pm) ⇒ Object



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/quaternion.rb', line 64

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

.identityObject



214
215
216
# File 'lib/quaternion.rb', line 214

def self.identity
  self.new 0, 0, 0, 1
end

Instance Method Details

#!=(vec) ⇒ Object



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

def != vec
  !(self == vec)
end

#*(v) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/quaternion.rb', line 118

def * v
  if Quaternion == v.class
    quat = v
    out = self.class.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
  else
    self.class.new x*v, y*v, z*v, w*v
  end
end

#+(quat) ⇒ Object



110
111
112
# File 'lib/quaternion.rb', line 110

def + quat
  self.class.new x + quat.x, y + quat.y, z + quat.z, w + quat.w
end

#+@Object



32
33
34
# File 'lib/quaternion.rb', line 32

def +@
  self.class.new x, y, z, w
end

#-(quat) ⇒ Object



114
115
116
# File 'lib/quaternion.rb', line 114

def - quat
  self.class.new x - quat.x, y - quat.y, z - quat.z, w - quat.w
end

#-@Object



36
37
38
# File 'lib/quaternion.rb', line 36

def -@
  self.class.new -x, -y, -z, -w
end

#/(v) ⇒ Object



132
133
134
# File 'lib/quaternion.rb', line 132

def / v
  self.class.new x/v, y/v, z/v, w/v
end

#==(q) ⇒ Object



40
41
42
# File 'lib/quaternion.rb', line 40

def == q
  Geo3d::Utils.float_cmp(x, q.x) && Geo3d::Utils.float_cmp(y, q.y) && Geo3d::Utils.float_cmp(z, q.z) && Geo3d::Utils.float_cmp(w, q.w)
end

#angleObject



160
161
162
163
# File 'lib/quaternion.rb', line 160

def angle
  q = normalize
  Math.acos(q.w) * 2.0
end

#angle_degreesObject



165
166
167
# File 'lib/quaternion.rb', line 165

def angle_degrees
  Geo3d::Utils.to_degrees angle
end

#axisObject



151
152
153
154
155
156
157
158
# File 'lib/quaternion.rb', line 151

def axis
  q = normalize
  v = Vector.new
  v.x = q.x / Math.sqrt(1-q.w*q.w)
  v.y = q.y / Math.sqrt(1-q.w*q.w)
  v.z = q.z / Math.sqrt(1-q.w*q.w)
  v
end

#conjugateObject



197
198
199
# File 'lib/quaternion.rb', line 197

def conjugate
  self.class.new -x, -y, -z, w
end

#dot(quat) ⇒ Object



177
178
179
# File 'lib/quaternion.rb', line 177

def dot quat
  x * quat.x + y * quat.y + z * quat.z + w * quat.w
end

#identity?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/quaternion.rb', line 210

def identity?
  self == self.class.identity
end

#inverseObject



201
202
203
204
205
206
207
208
# File 'lib/quaternion.rb', line 201

def inverse
  norm = length_squared
  if norm.zero?
    self.class.new 0, 0, 0, 0
  else
    conjugate / norm
  end
end

#lengthObject



173
174
175
# File 'lib/quaternion.rb', line 173

def length
  Math.sqrt length_squared
end

#length_squaredObject



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

def length_squared
  dot self
end

#normalizeObject



191
192
193
194
195
# File 'lib/quaternion.rb', line 191

def normalize
  q = self.class.new x, y, z, w
  q.normalize!
  q
end

#normalize!Object



181
182
183
184
185
186
187
188
189
# File 'lib/quaternion.rb', line 181

def normalize!
  len = length
  if length > 0
    @x /= len
    @y /= len
    @z /= len
    @w /= len
  end
end

#to_matrixObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/quaternion.rb', line 136

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