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

Returns a new instance of Vector.



9
10
11
12
13
14
15
# 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

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



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

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

.point(*args) ⇒ Object



17
18
19
# File 'lib/geo3d/vector.rb', line 17

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

.reflect(normal, incident) ⇒ Object



130
131
132
133
# 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) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# 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

Instance Method Details

#!=(vec) ⇒ Object



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

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

#*(scalar) ⇒ Object



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

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

#+(vec) ⇒ Object



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

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

#+@Object



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

def +@
  self * 1
end

#-(vec) ⇒ Object



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

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

#-@Object



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

def -@
  self * -1
end

#/(scalar) ⇒ Object



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

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

#==(vec) ⇒ Object



69
70
71
72
73
74
# 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) ⇒ 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



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

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



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

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

#to_sObject



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

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



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

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

#zero_wObject



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

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