Class: Geo3d::Vector
- Inherits:
-
Object
- Object
- Geo3d::Vector
- Defined in:
- lib/geo3d/vector.rb
Instance Attribute Summary collapse
-
#w ⇒ Object
(also: #d)
Returns the value of attribute w.
-
#x ⇒ Object
(also: #a)
Returns the value of attribute x.
-
#y ⇒ Object
(also: #b)
Returns the value of attribute y.
-
#z ⇒ Object
(also: #c)
Returns the value of attribute z.
Class Method Summary collapse
- .direction(*args) ⇒ Object
- .point(*args) ⇒ Object
- .reflect(normal, incident) ⇒ Object
- .refract(normal, incident, index_of_refraction) ⇒ Object
Instance Method Summary collapse
- #!=(vec) ⇒ Object
- #*(scalar) ⇒ Object
- #+(vec) ⇒ Object
- #+@ ⇒ Object
- #-(vec) ⇒ Object
- #-@ ⇒ Object
- #/(scalar) ⇒ Object
- #==(vec) ⇒ Object
- #cross(vec) ⇒ Object
- #dot(vec) ⇒ Object
-
#initialize(*args) ⇒ Vector
constructor
A new instance of Vector.
- #length ⇒ Object
- #length_squared ⇒ Object
- #lerp(vec, s) ⇒ Object
- #normalize ⇒ Object
- #normalize! ⇒ Object
- #one_w ⇒ Object
- #project(viewport, projection, view, world) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #unproject(viewport, projection, view, world) ⇒ Object
- #xyz ⇒ Object
- #zero_w ⇒ Object
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
#w ⇒ Object Also known as: d
Returns the value of attribute w.
3 4 5 |
# File 'lib/geo3d/vector.rb', line 3 def w @w end |
#x ⇒ Object Also known as: a
Returns the value of attribute x.
3 4 5 |
# File 'lib/geo3d/vector.rb', line 3 def x @x end |
#y ⇒ Object Also known as: b
Returns the value of attribute y.
3 4 5 |
# File 'lib/geo3d/vector.rb', line 3 def y @y end |
#z ⇒ Object 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 |
#length ⇒ Object
104 105 106 |
# File 'lib/geo3d/vector.rb', line 104 def length Math.sqrt length_squared end |
#length_squared ⇒ Object
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 |
#normalize ⇒ Object
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_w ⇒ Object
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 , projection, view, world clipspace_vector = projection * view * world * one_w normalized_clipspace_vector = (clipspace_vector / clipspace_vector.w.to_f).one_w * normalized_clipspace_vector end |
#to_a ⇒ Object
41 42 43 |
# File 'lib/geo3d/vector.rb', line 41 def to_a [x, y, z, w] end |
#to_s ⇒ Object
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 , projection, view, world normalized_clipspace_vector = .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 ⇒ Object
33 34 35 |
# File 'lib/geo3d/vector.rb', line 33 def xyz self.class.new x, y, z, 0 end |
#zero_w ⇒ Object
25 26 27 |
# File 'lib/geo3d/vector.rb', line 25 def zero_w self.class.new x, y, z, 0 end |