class Rabbit::TrackBall::Vector

Public Instance Methods

axis_to_quat(phi) click to toggle source

Given an axis and angle, compute quaternion.

# 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_rotmatrix() click to toggle source

Build a rotation matrix, given a quaternion rotation.

# 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) click to toggle source
Calls superclass method
# File lib/rabbit/trackball.rb, line 141
def collect(&block)
  Vector.new(super)
end
normalize_quat() click to toggle source

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.

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
vlength() click to toggle source
# File lib/rabbit/trackball.rb, line 86
def vlength
  Math.sqrt(inject(0){|ret, i| ret + i ** 2})
end
vnormal() click to toggle source
# File lib/rabbit/trackball.rb, line 91
def vnormal; vscale(1.0 / vlength); end
vscale(div) click to toggle source
# File lib/rabbit/trackball.rb, line 90
def vscale(div); collect{|v| v *= div}; end
vsub(other, range = nil) click to toggle source
# 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