class Geo3d::Vector

Attributes

a[RW]
b[RW]
c[RW]
d[RW]
w[RW]
x[RW]
y[RW]
z[RW]

Public Class Methods

direction(*args) click to toggle source
# File lib/geo3d/vector.rb, line 21
def self.direction *args
  self.new(*args).zero_w
end
new(*args) click to toggle source
# File lib/geo3d/vector.rb, line 9
def initialize *args
  @x, @y, @z, @w = 0.0, 0.0, 0.0, 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
point(*args) click to toggle source
# File lib/geo3d/vector.rb, line 17
def self.point *args
  self.new(*args).one_w
end
reflect(normal, incident) click to toggle source
# File lib/geo3d/vector.rb, line 130
def self.reflect normal, incident
  s = 2.0 * normal.xyz.dot(incident.xyz)
  (incident - normal * s).xyz
end
refract(normal, incident, index_of_refraction) click to toggle source
# File lib/geo3d/vector.rb, line 135
def self.refract normal, incident, index_of_refraction
  t = incident.xyz.dot normal.xyz
  r = 1.0 - index_of_refraction * index_of_refraction * (1.0 - t*t)

  if r < 0.0 # Total internal reflection
    self.new 0, 0, 0, 0
  else
    s = index_of_refraction * t + Math.sqrt(r)
    (incident * index_of_refraction - normal * s).xyz
  end
end

Public Instance Methods

!=(vec) click to toggle source
# File lib/geo3d/vector.rb, line 76
def != vec
  !(self == vec)
end
*(scalar) click to toggle source
# File lib/geo3d/vector.rb, line 61
def * scalar
  self.class.new x * scalar, y * scalar, z * scalar, w
end
+(vec) click to toggle source
# File lib/geo3d/vector.rb, line 53
def + vec
  self.class.new x + vec.x, y + vec.y, z + vec.z, w
end
+@() click to toggle source
# File lib/geo3d/vector.rb, line 45
def +@
  self * 1
end
-(vec) click to toggle source
# File lib/geo3d/vector.rb, line 57
def - vec
  self.class.new x - vec.x, y - vec.y, z - vec.z, w
end
-@() click to toggle source
# File lib/geo3d/vector.rb, line 49
def -@
  self * -1
end
/(scalar) click to toggle source
# File lib/geo3d/vector.rb, line 65
def / scalar
  self.class.new x / scalar, y / scalar, z / scalar, w
end
==(vec) click to toggle source
# File lib/geo3d/vector.rb, line 69
def == vec
  Geo3d::Utils.float_cmp(x, vec.x) &&
      Geo3d::Utils.float_cmp(y, vec.y) &&
      Geo3d::Utils.float_cmp(z, vec.z) &&
      Geo3d::Utils.float_cmp(w, vec.w)
end
cross(vec) click to toggle source
# File lib/geo3d/vector.rb, line 80
def cross vec
  self.class.new y * vec.z - z * vec.y, z * vec.x - x * vec.z, x * vec.y - y * vec.x
end
dot(vec) click to toggle source
# File lib/geo3d/vector.rb, line 84
def dot vec
  x * vec.x + y * vec.y + z * vec.z + w * vec.w
end
length() click to toggle source
# File lib/geo3d/vector.rb, line 104
def length
  Math.sqrt length_squared
end
length_squared() click to toggle source
# File lib/geo3d/vector.rb, line 108
def length_squared
  dot self
end
lerp(vec, s) click to toggle source
# File lib/geo3d/vector.rb, line 112
def lerp vec, s
  l = self + (vec - self)*s
  l.w = w + (vec.w - w)*s
  l
end
normalize() click to toggle source
# File lib/geo3d/vector.rb, line 98
def normalize
  v = self.class.new x, y, z, w
  v.normalize!
  v
end
normalize!() click to toggle source
# File lib/geo3d/vector.rb, line 88
def normalize!
  len = length
  if length > 0
    @x /= len
    @y /= len
    @z /= len
    @w /= len
  end
end
one_w() click to toggle source
# File lib/geo3d/vector.rb, line 29
def one_w
  self.class.new x, y, z, 1
end
project(viewport, projection, view, world) click to toggle source
# File lib/geo3d/vector.rb, line 118
def project viewport, projection, view, world
  clipspace_vector = projection * view * world * one_w
  normalized_clipspace_vector = (clipspace_vector / clipspace_vector.w.to_f).one_w
  viewport * normalized_clipspace_vector
end
to_a() click to toggle source
# File lib/geo3d/vector.rb, line 41
def to_a
  [x, y, z, w]
end
to_s() click to toggle source
# File lib/geo3d/vector.rb, line 37
def to_s
  to_a.compact.join ' '
end
unproject(viewport, projection, view, world) click to toggle source
# File lib/geo3d/vector.rb, line 124
def unproject viewport, projection, view, world
  normalized_clipspace_vector = viewport.inverse * one_w
  almost_objectspace_vector = (projection * view * world).inverse * normalized_clipspace_vector.one_w
  (almost_objectspace_vector / almost_objectspace_vector.w).one_w
end
xyz() click to toggle source
# File lib/geo3d/vector.rb, line 33
def xyz
  self.class.new x, y, z, 0
end
zero_w() click to toggle source
# File lib/geo3d/vector.rb, line 25
def zero_w
  self.class.new x, y, z, 0
end