Class: Mittsu::Vector4
- Inherits:
-
Object
- Object
- Mittsu::Vector4
- Defined in:
- lib/mittsu/math/vector4.rb
Instance Attribute Summary collapse
-
#w ⇒ Object
Returns the value of attribute w.
-
#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
- #==(v) ⇒ Object
- #add(v) ⇒ Object
- #add_scalar(s) ⇒ Object
- #add_vectors(a, b) ⇒ Object
- #apply_matrix4(m) ⇒ Object
- #ceil ⇒ Object
- #clamp(min, max) ⇒ Object
- #clone ⇒ Object
- #copy(v) ⇒ Object
- #divide_scalar(scalar) ⇒ Object
- #dot(v) ⇒ Object
- #floor ⇒ Object
- #from_array(array, offset = 0) ⇒ Object
- #from_attribute(attribute, index, offset = 0) ⇒ Object
- #get_component(index) ⇒ Object
-
#initialize(x = 0.0, y = 0.0, z = 0.0, w = 1.0) ⇒ Vector4
constructor
A new instance of Vector4.
- #length ⇒ Object
- #length_manhattan ⇒ Object
- #length_sq ⇒ Object
- #lerp(v, alpha) ⇒ Object
- #lerp_vectors(v1, v2, alpha) ⇒ Object
- #max(v) ⇒ Object
- #min(v) ⇒ Object
- #multiply_scalar(scalar) ⇒ Object
- #negate ⇒ Object
- #normalize ⇒ Object
- #round ⇒ Object
- #round_to_zero ⇒ Object
- #set(x, y, z, w) ⇒ Object
- #set_axis_angle_from_quaternion(q) ⇒ Object
- #set_axis_angle_from_rotation_matrix(m) ⇒ Object
- #set_component(index, value) ⇒ Object
- #set_length(l) ⇒ Object
- #set_w(w) ⇒ Object
- #set_x(x) ⇒ Object
- #set_y(y) ⇒ Object
- #set_z(z) ⇒ Object
- #sub(v) ⇒ Object
- #sub_scalar(s) ⇒ Object
- #sub_vectors(a, b) ⇒ Object
- #to_array(array = [], offset = 0) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(x = 0.0, y = 0.0, z = 0.0, w = 1.0) ⇒ Vector4
Returns a new instance of Vector4.
6 7 8 |
# File 'lib/mittsu/math/vector4.rb', line 6 def initialize(x = 0.0, y = 0.0, z = 0.0, w = 1.0) self.set(x, y, z, w) end |
Instance Attribute Details
#w ⇒ Object
Returns the value of attribute w.
5 6 7 |
# File 'lib/mittsu/math/vector4.rb', line 5 def w @w end |
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/mittsu/math/vector4.rb', line 5 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
5 6 7 |
# File 'lib/mittsu/math/vector4.rb', line 5 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
5 6 7 |
# File 'lib/mittsu/math/vector4.rb', line 5 def z @z end |
Instance Method Details
#==(v) ⇒ Object
377 378 379 |
# File 'lib/mittsu/math/vector4.rb', line 377 def ==(v) ((v.x == @x) && (v.y == @y) && (v.z == @z) && (v.w == @w)) end |
#add(v) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/mittsu/math/vector4.rb', line 63 def add(v) @x += v.x @y += v.y @z += v.z @w += v.w self end |
#add_scalar(s) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/mittsu/math/vector4.rb', line 71 def add_scalar(s) @x += s @y += s @z += s @w += s self end |
#add_vectors(a, b) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/mittsu/math/vector4.rb', line 79 def add_vectors(a, b) @x = a.x + b.x @y = a.y + b.y @z = a.z + b.z @w = a.w + b.w self end |
#apply_matrix4(m) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/mittsu/math/vector4.rb', line 119 def apply_matrix4(m) x1, y1, z1, w1 = @x, @y, @z, @w e = m.elements @x = e[0] * x1 + e[4] * y1 + e[8] * z1 + e[12] * w1 @y = e[1] * x1 + e[5] * y1 + e[9] * z1 + e[13] * w1 @z = e[2] * x1 + e[6] * y1 + e[10] * z1 + e[14] * w1 @w = e[3] * x1 + e[7] * y1 + e[11] * z1 + e[15] * w1 self end |
#ceil ⇒ Object
304 305 306 307 308 309 310 |
# File 'lib/mittsu/math/vector4.rb', line 304 def ceil @x = (@x).ceil @y = (@y).ceil @z = (@z).ceil @w = (@w).ceil self end |
#clamp(min, max) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/mittsu/math/vector4.rb', line 271 def clamp(min, max) # This function assumes min < max, if self assumption isn't true it will not operate correctly if @x < min.x @x = min.x elsif @x > max.x @x = max.x end if @y < min.y @y = min.y elsif @y > max.y @y = max.y end if @z < min.z @z = min.z elsif @z > max.z @z = max.z end if @w < min.w @w = min.w elsif @w > max.w @w = max.w end self end |
#clone ⇒ Object
406 407 408 |
# File 'lib/mittsu/math/vector4.rb', line 406 def clone Mittsu::Vector4.new @x, @y, @z, @w end |
#copy(v) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/mittsu/math/vector4.rb', line 55 def copy(v) @x = v.x @y = v.y @z = v.z @w = v.w || 1.0 self end |
#divide_scalar(scalar) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mittsu/math/vector4.rb', line 129 def divide_scalar(scalar) if scalar != 0.0 inv_scalar = 1.0 / scalar @x *= inv_scalar @y *= inv_scalar @z *= inv_scalar @w *= inv_scalar else @x, @y, @z, @w = 0.0, 0.0, 0.0, 1.0 end self end |
#dot(v) ⇒ Object
336 337 338 |
# File 'lib/mittsu/math/vector4.rb', line 336 def dot(v) @x * v.x + @y * v.y + @z * v.z + @w * v.w end |
#floor ⇒ Object
296 297 298 299 300 301 302 |
# File 'lib/mittsu/math/vector4.rb', line 296 def floor @x = (@x).floor @y = (@y).floor @z = (@z).floor @w = (@w).floor self end |
#from_array(array, offset = 0) ⇒ Object
381 382 383 384 385 386 387 |
# File 'lib/mittsu/math/vector4.rb', line 381 def from_array(array, offset = 0) @x = array[offset] @y = array[offset + 1] @z = array[offset + 2] @w = array[offset + 3] self end |
#from_attribute(attribute, index, offset = 0) ⇒ Object
397 398 399 400 401 402 403 404 |
# File 'lib/mittsu/math/vector4.rb', line 397 def from_attribute(attribute, index, offset = 0) index = index * attribute.itemSize + offset @x = attribute.array[index] @y = attribute.array[index + 1] @z = attribute.array[index + 2] @w = attribute.array[index + 3] self end |
#get_component(index) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/mittsu/math/vector4.rb', line 45 def get_component(index) case index when 0 then return @x when 1 then return @y when 2 then return @z when 3 then return @w else raise IndexError.new end end |
#length ⇒ Object
344 345 346 |
# File 'lib/mittsu/math/vector4.rb', line 344 def length Math.sqrt(@x * @x + @y * @y + @z * @z + @w * @w) end |
#length_manhattan ⇒ Object
348 349 350 |
# File 'lib/mittsu/math/vector4.rb', line 348 def length_manhattan (@x).abs + (@y).abs + (@z).abs + (@w).abs end |
#length_sq ⇒ Object
340 341 342 |
# File 'lib/mittsu/math/vector4.rb', line 340 def length_sq @x * @x + @y * @y + @z * @z + @w * @w end |
#lerp(v, alpha) ⇒ Object
364 365 366 367 368 369 370 |
# File 'lib/mittsu/math/vector4.rb', line 364 def lerp(v, alpha) @x += (v.x - @x) * alpha @y += (v.y - @y) * alpha @z += (v.z - @z) * alpha @w += (v.w - @w) * alpha self end |
#lerp_vectors(v1, v2, alpha) ⇒ Object
372 373 374 375 |
# File 'lib/mittsu/math/vector4.rb', line 372 def lerp_vectors(v1, v2, alpha) self.sub_vectors(v2, v1).multiply_scalar(alpha).add(v1) self end |
#max(v) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/mittsu/math/vector4.rb', line 255 def max(v) if @x < v.x @x = v.x end if @y < v.y @y = v.y end if @z < v.z @z = v.z end if @w < v.w @w = v.w end self end |
#min(v) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/mittsu/math/vector4.rb', line 239 def min(v) if @x > v.x @x = v.x end if @y > v.y @y = v.y end if @z > v.z @z = v.z end if @w > v.w @w = v.w end self end |
#multiply_scalar(scalar) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/mittsu/math/vector4.rb', line 111 def multiply_scalar(scalar) @x *= scalar @y *= scalar @z *= scalar @w *= scalar self end |
#negate ⇒ Object
328 329 330 331 332 333 334 |
# File 'lib/mittsu/math/vector4.rb', line 328 def negate @x = - @x @y = - @y @z = - @z @w = - @w self end |
#normalize ⇒ Object
352 353 354 |
# File 'lib/mittsu/math/vector4.rb', line 352 def normalize self.divide_scalar(self.length) end |
#round ⇒ Object
312 313 314 315 316 317 318 |
# File 'lib/mittsu/math/vector4.rb', line 312 def round @x = (@x).round @y = (@y).round @z = (@z).round @w = (@w).round self end |
#round_to_zero ⇒ Object
320 321 322 323 324 325 326 |
# File 'lib/mittsu/math/vector4.rb', line 320 def round_to_zero @x = (@x < 0) ? (@x).ceil : (@x).floor @y = (@y < 0) ? (@y).ceil : (@y).floor @z = (@z < 0) ? (@z).ceil : (@z).floor @w = (@w < 0) ? (@w).ceil : (@w).floor self end |
#set(x, y, z, w) ⇒ Object
10 11 12 13 |
# File 'lib/mittsu/math/vector4.rb', line 10 def set(x, y, z, w) @x, @y, @z, @w = x.to_f, y.to_f, z.to_f, w.to_f self end |
#set_axis_angle_from_quaternion(q) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/mittsu/math/vector4.rb', line 142 def set_axis_angle_from_quaternion(q) # http:#www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm # q is assumed to be normalized @w = 2.0 * Math.acos(q.w) s = Math.sqrt(1.0 - q.w * q.w) if s < 0.0001 @x = 1.0 @y = 0.0 @z = 0.0 else @x = q.x / s @y = q.y / s @z = q.z / s end self end |
#set_axis_angle_from_rotation_matrix(m) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/mittsu/math/vector4.rb', line 159 def set_axis_angle_from_rotation_matrix(m) # http:#www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm # assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) angle, x1, y1, z1 = nil # variables for result epsilon = 0.01 # margin to allow for rounding errors epsilon2 = 0.1 # margin to distinguish between 0 and 180 degrees te = m.elements m11, m12, m13 = te[0], te[4], te[8] m21, m22, m23 = te[1], te[5], te[9] m31, m32, m33 = te[2], te[6], te[10] if (((m12 - m21).abs < epsilon) && ((m13 - m31).abs < epsilon) && ((m23 - m32).abs < epsilon)) # singularity found # first check for identity matrix which must have +1 for all terms # in leading diagonal and zero in other terms if (((m12 + m21).abs < epsilon2) && ((m13 + m31).abs < epsilon2) && ((m23 + m32).abs < epsilon2) && ((m11 + m22 + m33 - 3).abs < epsilon2)) # self singularity is identity matrix so angle = 0 self.set(1, 0, 0, 0) return self # zero angle, arbitrary axis end # otherwise self singularity is angle = 180 angle = Math::PI xx = (m11 + 1.0) / 2.0 yy = (m22 + 1.0) / 2.0 zz = (m33 + 1.0) / 2.0 xy = (m12 + m21) / 4.0 xz = (m13 + m31) / 4.0 yz = (m23 + m32) / 4.0 if (xx > yy) && (xx > zz) # m11 is the largest diagonal term if xx < epsilon x1 = 0.0 y1 = 0.707106781 z1 = 0.707106781 else x1 = Math.sqrt(xx) y1 = xy / x1 z1 = xz / x1 end elsif yy > zz # m22 is the largest diagonal term if yy < epsilon x1 = 0.707106781 y1 = 0.0 z1 = 0.707106781 else y1 = Math.sqrt(yy) x1 = xy / y1 z1 = yz / y1 end else # m33 is the largest diagonal term so base result on self if zz < epsilon x1 = 0.707106781 y1 = 0.707106781 z1 = 0.0 else z1 = Math.sqrt(zz) x1 = xz / z1 y1 = yz / z1 end end self.set(x1, y1, z1, angle) return self # return 180 deg rotation end # as we have reached here there are no singularities so we can handle normally s = Math.sqrt((m32 - m23) * (m32 - m23) + (m13 - m31) * (m13 - m31) + (m21 - m12) * (m21 - m12)) # used to normalize s = 1.0 if (s.abs < 0.001) # prevent divide by zero, should not happen if matrix is orthogonal and should be # caught by singularity test above, but I've left it in just in case @x = (m32 - m23) / s @y = (m13 - m31) / s @z = (m21 - m12) / s @w = Math.acos((m11 + m22 + m33 - 1.0) / 2.0) self end |
#set_component(index, value) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/mittsu/math/vector4.rb', line 35 def set_component(index, value) case index when 0 then @x = value.to_f when 1 then @y = value.to_f when 2 then @z = value.to_f when 3 then @w = value.to_f else raise IndexError.new end end |
#set_length(l) ⇒ Object
356 357 358 359 360 361 362 |
# File 'lib/mittsu/math/vector4.rb', line 356 def set_length(l) old_length = self.length if old_length != 0 && l != old_length self.multiply_scalar(l / old_length) end self end |
#set_w(w) ⇒ Object
30 31 32 33 |
# File 'lib/mittsu/math/vector4.rb', line 30 def set_w(w) @w = w.to_f self end |
#set_x(x) ⇒ Object
15 16 17 18 |
# File 'lib/mittsu/math/vector4.rb', line 15 def set_x(x) @x = x.to_f self end |
#set_y(y) ⇒ Object
20 21 22 23 |
# File 'lib/mittsu/math/vector4.rb', line 20 def set_y(y) @y = y.to_f self end |
#set_z(z) ⇒ Object
25 26 27 28 |
# File 'lib/mittsu/math/vector4.rb', line 25 def set_z(z) @z = z.to_f self end |
#sub(v) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/mittsu/math/vector4.rb', line 87 def sub(v) @x -= v.x @y -= v.y @z -= v.z @w -= v.w self end |
#sub_scalar(s) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/mittsu/math/vector4.rb', line 95 def sub_scalar(s) @x -= s @y -= s @z -= s @w -= s self end |
#sub_vectors(a, b) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/mittsu/math/vector4.rb', line 103 def sub_vectors(a, b) @x = a.x - b.x @y = a.y - b.y @z = a.z - b.z @w = a.w - b.w self end |
#to_array(array = [], offset = 0) ⇒ Object
389 390 391 392 393 394 395 |
# File 'lib/mittsu/math/vector4.rb', line 389 def to_array(array = [], offset = 0) array[offset] = @x array[offset + 1] = @y array[offset + 2] = @z array[offset + 3] = @w array end |
#to_s ⇒ Object
410 411 412 |
# File 'lib/mittsu/math/vector4.rb', line 410 def to_s "[#{x}, #{y}, #{z}, #{w}]" end |