Class: Geo3d::Vector

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Vector



9
10
11
12
13
14
15
16
17
18
# File 'lib/geo3d/vector.rb', line 9

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 Also known as: d

Returns the value of attribute w.



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

def w
  @w
end

#xObject Also known as: a

Returns the value of attribute x.



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

def x
  @x
end

#yObject Also known as: b

Returns the value of attribute y.



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

def y
  @y
end

#zObject Also known as: c

Returns the value of attribute z.



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

def z
  @z
end

Class Method Details

.direction(*args) ⇒ Object



24
25
26
# File 'lib/geo3d/vector.rb', line 24

def self.direction *args
  self.new(*args).zero_w
end

.point(*args) ⇒ Object



20
21
22
# File 'lib/geo3d/vector.rb', line 20

def self.point *args
  self.new(*args).one_w
end

Instance Method Details

#!=(vec) ⇒ Object



76
77
78
# File 'lib/geo3d/vector.rb', line 76

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

#*(scalar) ⇒ Object



64
65
66
# File 'lib/geo3d/vector.rb', line 64

def * scalar
  self.class.new x * scalar, y * scalar, z * scalar, w
end

#+(vec) ⇒ Object



56
57
58
# File 'lib/geo3d/vector.rb', line 56

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

#+@Object



48
49
50
# File 'lib/geo3d/vector.rb', line 48

def +@
  self * 1
end

#-(vec) ⇒ Object



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

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

#-@Object



52
53
54
# File 'lib/geo3d/vector.rb', line 52

def -@
  self * -1
end

#/(scalar) ⇒ Object



68
69
70
# File 'lib/geo3d/vector.rb', line 68

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

#==(vec) ⇒ Object



72
73
74
# File 'lib/geo3d/vector.rb', line 72

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) ⇒ Object



80
81
82
# 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) ⇒ Object



84
85
86
# File 'lib/geo3d/vector.rb', line 84

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

#lengthObject



104
105
106
# File 'lib/geo3d/vector.rb', line 104

def length
  Math.sqrt length_squared
end

#length_squaredObject



108
109
110
# File 'lib/geo3d/vector.rb', line 108

def length_squared
  dot self
end

#lerp(vec, s) ⇒ Object



112
113
114
115
116
# 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

#normalizeObject



98
99
100
101
102
# File 'lib/geo3d/vector.rb', line 98

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

#normalize!Object



88
89
90
91
92
93
94
95
96
# 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_wObject



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

def one_w
  self.class.new x, y, z, 1
end

#project(viewport, projection, view, world) ⇒ Object



118
119
120
121
122
# 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_aObject



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

def to_a
  [x, y, z, w]
end

#to_sObject



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

def to_s
  to_a.compact.join ' '
end

#unproject(viewport, projection, view, world) ⇒ Object



124
125
126
127
128
# 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

#xyzObject



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

def xyz
  self.class.new x, y, z
end

#zero_wObject



28
29
30
# File 'lib/geo3d/vector.rb', line 28

def zero_w
  self.class.new x, y, z, 0
end