Class: Three::Vector3
- Inherits:
-
Object
- Object
- Three::Vector3
- Includes:
- Enumerable
- Defined in:
- lib/three/math/vector3.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(scalar) ⇒ Object
- #==(other) ⇒ Object
- #add(vector) ⇒ Object
- #add_scalar(scalar) ⇒ Object
- #add_scaled_vector(vector, scale) ⇒ Object
- #add_vectors(a, b) ⇒ Object
- #apply_matrix3(matrix) ⇒ Object
- #apply_matrix4(matrix) ⇒ Object
- #apply_quaternion(quaternion) ⇒ Object
- #clone ⇒ Object
- #copy(vector) ⇒ Object
- #cross(vector) ⇒ Object
- #cross_vectors(a, b) ⇒ Object
- #deconstruct ⇒ Object
- #distance_to(vector) ⇒ Object
- #distance_to_squared(vector) ⇒ Object
- #divide(vector) ⇒ Object
- #divide_scalar(scalar) ⇒ Object
- #dot(vector) ⇒ Object
- #each {|@x| ... } ⇒ Object
- #equals?(vector, epsilon: 0.0) ⇒ Boolean
- #from_array(array, offset = 0) ⇒ Object
- #get_component(index) ⇒ Object (also: #[])
-
#initialize(x = 0, y = 0, z = 0) ⇒ Vector3
constructor
A new instance of Vector3.
- #inspect ⇒ Object
- #length ⇒ Object
- #length_sq ⇒ Object
- #manhattan_length ⇒ Object
- #multiply(vector) ⇒ Object
- #multiply_scalar(scalar) ⇒ Object
- #negate ⇒ Object
- #normalize ⇒ Object
- #on_change(&callback) ⇒ Object (also: #_on_change)
- #set(x, y, z = @z) ⇒ Object
- #set_component(index, value) ⇒ Object (also: #[]=)
- #set_from_matrix3_column(matrix, index) ⇒ Object
- #set_from_matrix_column(matrix, index) ⇒ Object
- #set_from_matrix_position(matrix) ⇒ Object
- #set_from_matrix_scale(matrix) ⇒ Object
- #set_length(value) ⇒ Object
- #set_scalar(scalar) ⇒ Object
- #set_x(x) ⇒ Object
- #set_y(y) ⇒ Object
- #set_z(z) ⇒ Object
- #sub(vector) ⇒ Object
- #sub_scalar(scalar) ⇒ Object
- #sub_vectors(a, b) ⇒ Object
- #to_a ⇒ Object
- #to_array(array = [], offset = 0) ⇒ Object
Constructor Details
#initialize(x = 0, y = 0, z = 0) ⇒ Vector3
Returns a new instance of Vector3.
9 10 11 12 13 14 |
# File 'lib/three/math/vector3.rb', line 9 def initialize(x = 0, y = 0, z = 0) @x = x @y = y @z = z @on_change_callback = proc {} end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
7 8 9 |
# File 'lib/three/math/vector3.rb', line 7 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
7 8 9 |
# File 'lib/three/math/vector3.rb', line 7 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
7 8 9 |
# File 'lib/three/math/vector3.rb', line 7 def z @z end |
Instance Method Details
#*(scalar) ⇒ Object
358 359 360 |
# File 'lib/three/math/vector3.rb', line 358 def *(scalar) clone.multiply_scalar(scalar) end |
#+(other) ⇒ Object
350 351 352 |
# File 'lib/three/math/vector3.rb', line 350 def +(other) clone.add(other) end |
#-(other) ⇒ Object
354 355 356 |
# File 'lib/three/math/vector3.rb', line 354 def -(other) clone.sub(other) end |
#-@ ⇒ Object
366 367 368 |
# File 'lib/three/math/vector3.rb', line 366 def -@ clone.negate end |
#/(scalar) ⇒ Object
362 363 364 |
# File 'lib/three/math/vector3.rb', line 362 def /(scalar) clone.divide_scalar(scalar) end |
#==(other) ⇒ Object
346 347 348 |
# File 'lib/three/math/vector3.rb', line 346 def ==(other) other.is_a?(self.class) && equals?(other) end |
#add(vector) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/three/math/vector3.rb', line 101 def add(vector) @x += vector.x @y += vector.y @z += vector.z changed! self end |
#add_scalar(scalar) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/three/math/vector3.rb', line 109 def add_scalar(scalar) @x += scalar @y += scalar @z += scalar changed! self end |
#add_scaled_vector(vector, scale) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/three/math/vector3.rb', line 125 def add_scaled_vector(vector, scale) @x += vector.x * scale @y += vector.y * scale @z += vector.z * scale changed! self end |
#add_vectors(a, b) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/three/math/vector3.rb', line 117 def add_vectors(a, b) @x = a.x + b.x @y = a.y + b.y @z = a.z + b.z changed! self end |
#apply_matrix3(matrix) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/three/math/vector3.rb', line 261 def apply_matrix3(matrix) elements = matrix.elements x = @x y = @y z = @z @x = elements[0] * x + elements[3] * y + elements[6] * z @y = elements[1] * x + elements[4] * y + elements[7] * z @z = elements[2] * x + elements[5] * y + elements[8] * z changed! self end |
#apply_matrix4(matrix) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/three/math/vector3.rb', line 247 def apply_matrix4(matrix) elements = matrix.elements x = @x y = @y z = @z w = 1.0 / (elements[3] * x + elements[7] * y + elements[11] * z + elements[15]) @x = (elements[0] * x + elements[4] * y + elements[8] * z + elements[12]) * w @y = (elements[1] * x + elements[5] * y + elements[9] * z + elements[13]) * w @z = (elements[2] * x + elements[6] * y + elements[10] * z + elements[14]) * w changed! self end |
#apply_quaternion(quaternion) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/three/math/vector3.rb', line 274 def apply_quaternion(quaternion) vx = @x vy = @y vz = @z qx = quaternion.x qy = quaternion.y qz = quaternion.z qw = quaternion.w tx = 2 * (qy * vz - qz * vy) ty = 2 * (qz * vx - qx * vz) tz = 2 * (qx * vy - qy * vx) @x = vx + qw * tx + qy * tz - qz * ty @y = vy + qw * ty + qz * tx - qx * tz @z = vz + qw * tz + qx * ty - qy * tx changed! self end |
#clone ⇒ Object
89 90 91 |
# File 'lib/three/math/vector3.rb', line 89 def clone self.class.new(@x, @y, @z) end |
#copy(vector) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/three/math/vector3.rb', line 93 def copy(vector) @x = vector.x @y = vector.y @z = vector.z changed! self end |
#cross(vector) ⇒ Object
193 194 195 |
# File 'lib/three/math/vector3.rb', line 193 def cross(vector) cross_vectors(self, vector) end |
#cross_vectors(a, b) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/three/math/vector3.rb', line 197 def cross_vectors(a, b) ax = a.x ay = a.y az = a.z bx = b.x by = b.y bz = b.z @x = ay * bz - az * by @y = az * bx - ax * bz @z = ax * by - ay * bx changed! self end |
#deconstruct ⇒ Object
382 383 384 |
# File 'lib/three/math/vector3.rb', line 382 def deconstruct to_a end |
#distance_to(vector) ⇒ Object
236 237 238 |
# File 'lib/three/math/vector3.rb', line 236 def distance_to(vector) Math.sqrt(distance_to_squared(vector)) end |
#distance_to_squared(vector) ⇒ Object
240 241 242 243 244 245 |
# File 'lib/three/math/vector3.rb', line 240 def distance_to_squared(vector) dx = @x - vector.x dy = @y - vector.y dz = @z - vector.z dx * dx + dy * dy + dz * dz end |
#divide(vector) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/three/math/vector3.rb', line 173 def divide(vector) @x = @x.to_f / vector.x @y = @y.to_f / vector.y @z = @z.to_f / vector.z changed! self end |
#divide_scalar(scalar) ⇒ Object
181 182 183 |
# File 'lib/three/math/vector3.rb', line 181 def divide_scalar(scalar) multiply_scalar(1.0 / scalar) end |
#dot(vector) ⇒ Object
189 190 191 |
# File 'lib/three/math/vector3.rb', line 189 def dot(vector) @x * vector.x + @y * vector.y + @z * vector.z end |
#each {|@x| ... } ⇒ Object
370 371 372 373 374 375 376 |
# File 'lib/three/math/vector3.rb', line 370 def each return enum_for(:each) unless block_given? yield @x yield @y yield @z end |
#equals?(vector, epsilon: 0.0) ⇒ Boolean
340 341 342 343 344 |
# File 'lib/three/math/vector3.rb', line 340 def equals?(vector, epsilon: 0.0) (@x - vector.x).abs <= epsilon && (@y - vector.y).abs <= epsilon && (@z - vector.z).abs <= epsilon end |
#from_array(array, offset = 0) ⇒ Object
318 319 320 321 322 323 324 |
# File 'lib/three/math/vector3.rb', line 318 def from_array(array, offset = 0) @x = array[offset] @y = array[offset + 1] @z = array[offset + 2] changed! self end |
#get_component(index) ⇒ Object Also known as: []
77 78 79 80 81 82 83 84 |
# File 'lib/three/math/vector3.rb', line 77 def get_component(index) case index when 0 then @x when 1 then @y when 2 then @z else raise IndexError, "index is out of range: #{index}" end end |
#inspect ⇒ Object
386 387 388 |
# File 'lib/three/math/vector3.rb', line 386 def inspect "#<#{self.class} x=#{@x.inspect} y=#{@y.inspect} z=#{@z.inspect}>" end |
#length ⇒ Object
216 217 218 |
# File 'lib/three/math/vector3.rb', line 216 def length Math.sqrt(length_sq) end |
#length_sq ⇒ Object
212 213 214 |
# File 'lib/three/math/vector3.rb', line 212 def length_sq @x * @x + @y * @y + @z * @z end |
#manhattan_length ⇒ Object
220 221 222 |
# File 'lib/three/math/vector3.rb', line 220 def manhattan_length @x.abs + @y.abs + @z.abs end |
#multiply(vector) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/three/math/vector3.rb', line 157 def multiply(vector) @x *= vector.x @y *= vector.y @z *= vector.z changed! self end |
#multiply_scalar(scalar) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/three/math/vector3.rb', line 165 def multiply_scalar(scalar) @x *= scalar @y *= scalar @z *= scalar changed! self end |
#negate ⇒ Object
185 186 187 |
# File 'lib/three/math/vector3.rb', line 185 def negate multiply_scalar(-1) end |
#normalize ⇒ Object
224 225 226 227 |
# File 'lib/three/math/vector3.rb', line 224 def normalize current_length = length divide_scalar(current_length.zero? ? 1 : current_length) end |
#on_change(&callback) ⇒ Object Also known as: _on_change
326 327 328 329 |
# File 'lib/three/math/vector3.rb', line 326 def on_change(&callback) @on_change_callback = callback || proc {} self end |
#set(x, y, z = @z) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/three/math/vector3.rb', line 31 def set(x, y, z = @z) @x = x @y = y @z = z changed! self end |
#set_component(index, value) ⇒ Object Also known as: []=
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/three/math/vector3.rb', line 65 def set_component(index, value) case index when 0 then @x = value when 1 then @y = value when 2 then @z = value else raise IndexError, "index is out of range: #{index}" end changed! self end |
#set_from_matrix3_column(matrix, index) ⇒ Object
314 315 316 |
# File 'lib/three/math/vector3.rb', line 314 def set_from_matrix3_column(matrix, index) from_array(matrix.elements, index * 3) end |
#set_from_matrix_column(matrix, index) ⇒ Object
310 311 312 |
# File 'lib/three/math/vector3.rb', line 310 def set_from_matrix_column(matrix, index) from_array(matrix.elements, index * 4) end |
#set_from_matrix_position(matrix) ⇒ Object
294 295 296 297 298 299 300 301 |
# File 'lib/three/math/vector3.rb', line 294 def set_from_matrix_position(matrix) elements = matrix.elements @x = elements[12] @y = elements[13] @z = elements[14] changed! self end |
#set_from_matrix_scale(matrix) ⇒ Object
303 304 305 306 307 308 |
# File 'lib/three/math/vector3.rb', line 303 def set_from_matrix_scale(matrix) sx = set_from_matrix_column(matrix, 0).length sy = set_from_matrix_column(matrix, 1).length sz = set_from_matrix_column(matrix, 2).length set(sx, sy, sz) end |
#set_length(value) ⇒ Object
229 230 231 232 233 234 |
# File 'lib/three/math/vector3.rb', line 229 def set_length(value) old_length = length return self if old_length.zero? || old_length == value multiply_scalar(value / old_length) end |
#set_scalar(scalar) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/three/math/vector3.rb', line 39 def set_scalar(scalar) @x = scalar @y = scalar @z = scalar changed! self end |
#set_x(x) ⇒ Object
47 48 49 50 51 |
# File 'lib/three/math/vector3.rb', line 47 def set_x(x) @x = x changed! self end |
#set_y(y) ⇒ Object
53 54 55 56 57 |
# File 'lib/three/math/vector3.rb', line 53 def set_y(y) @y = y changed! self end |
#set_z(z) ⇒ Object
59 60 61 62 63 |
# File 'lib/three/math/vector3.rb', line 59 def set_z(z) @z = z changed! self end |
#sub(vector) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/three/math/vector3.rb', line 133 def sub(vector) @x -= vector.x @y -= vector.y @z -= vector.z changed! self end |
#sub_scalar(scalar) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/three/math/vector3.rb', line 141 def sub_scalar(scalar) @x -= scalar @y -= scalar @z -= scalar changed! self end |
#sub_vectors(a, b) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/three/math/vector3.rb', line 149 def sub_vectors(a, b) @x = a.x - b.x @y = a.y - b.y @z = a.z - b.z changed! self end |
#to_a ⇒ Object
378 379 380 |
# File 'lib/three/math/vector3.rb', line 378 def to_a [@x, @y, @z] end |
#to_array(array = [], offset = 0) ⇒ Object
333 334 335 336 337 338 |
# File 'lib/three/math/vector3.rb', line 333 def to_array(array = [], offset = 0) array[offset] = @x array[offset + 1] = @y array[offset + 2] = @z array end |