Class: Rabbit::TrackBall::Vector

Inherits:
Array
  • Object
show all
Defined in:
lib/rabbit/trackball.rb

Instance Method Summary collapse

Instance Method Details

#axis_to_quat(phi) ⇒ Object

Given an axis and angle, compute quaternion.


136
137
138
139
# File 'lib/rabbit/trackball.rb', line 136

def axis_to_quat(phi)
  b, c, d = self.vnormal.vscale(Math.sin(phi/2.0))
  Vector.new([b, c, d, Math.cos(phi/2.0)])
end

#build_rotmatrixObject

Build a rotation matrix, given a quaternion rotation.


120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rabbit/trackball.rb', line 120

def build_rotmatrix
  m = []
  m << [1.0 - 2.0 * (self[1] * self[1] + self[2] * self[2]),
        2.0 * (self[0] * self[1] - self[2] * self[3]),
        2.0 * (self[2] * self[0] + self[1] * self[3]), 0.0]
  m << [2.0 * (self[0] * self[1] + self[2] * self[3]),
        1.0 - 2.0 * (self[2] * self[2] + self[0] * self[0]),
        2.0 * (self[1] * self[2] - self[0] * self[3]), 0.0]
  m << [2.0 * (self[2] * self[0] - self[1] * self[3]),
        2.0 * (self[1] * self[2] + self[0] * self[3]),
        1.0 - 2.0 * (self[1] * self[1] + self[0] * self[0]), 0.0]
  m << [0.0, 0.0, 0.0, 1.0]
  m
end

#collect(&block) ⇒ Object


141
142
143
# File 'lib/rabbit/trackball.rb', line 141

def collect(&block)
  Vector.new(super)
end

#normalize_quatObject

Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0 If they don't add up to 1.0, dividing by their magnitued will renormalize them.

Note: See the following for more information on quaternions:

  • Shoemake, K., Animating rotation with quaternion curves, Computer Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.

  • Pletinckx, D., Quaternion calculus as a basic tool in computer graphics, The Visual Computer 5, 2-13, 1989.


115
116
117
# File 'lib/rabbit/trackball.rb', line 115

def normalize_quat
  collect{|q| q = q / inject(0){|ret, i| ret + i ** 2}}
end

#vadd(other, range = nil) ⇒ Object


98
99
100
101
# File 'lib/rabbit/trackball.rb', line 98

def vadd(other, range = nil)
  range = [size, other.size].min unless range
  (0...range).inject(Vector.new){|ret, i| ret << self[i] + other[i]}
end

#vcross(other) ⇒ Object


79
80
81
82
83
84
# File 'lib/rabbit/trackball.rb', line 79

def vcross(other)
  dst = Vector.new
  dst << (self[1] * other[2]) - (self[2] * other[1])
  dst << (self[2] * other[0]) - (self[0] * other[2])
  dst << (self[0] * other[1]) - (self[1] * other[0])
end

#vdot(other, range = nil) ⇒ Object


93
94
95
96
# File 'lib/rabbit/trackball.rb', line 93

def vdot(other, range = nil)
  range = [size, other.size].min unless range
  (0...range).inject(0.0){|ret, i| ret + self[i] * other[i]}
end

#vlengthObject


86
87
88
# File 'lib/rabbit/trackball.rb', line 86

def vlength
  Math.sqrt(inject(0){|ret, i| ret + i ** 2})
end

#vnormalObject


91
# File 'lib/rabbit/trackball.rb', line 91

def vnormal; vscale(1.0 / vlength); end

#vscale(div) ⇒ Object


90
# File 'lib/rabbit/trackball.rb', line 90

def vscale(div); collect{|v| v *= div}; end

#vsub(other, range = nil) ⇒ Object


74
75
76
77
# File 'lib/rabbit/trackball.rb', line 74

def vsub(other, range = nil)
  range = [size, other.size].min unless range
  (0...range).inject(Vector.new){|ret, i| ret << self[i] - other[i]}
end