class RMath3D::RVec3
Document-class: RMath3D::RVec3
provies 3 element vector arithmetic.
Public Class Methods
Calculates the cross product of v_a
and v_b
.
# File lib/rmath3d/rmath3d_plain.rb, line 3465 def RVec3.cross( v1, v2 ) return RVec3.new(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x) end
Calculates the dot product of v_a
and v_b
.
# File lib/rmath3d/rmath3d_plain.rb, line 3456 def RVec3.dot( v1, v2 ) return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z end
Creates a new 3 element vector.
# File lib/rmath3d/rmath3d_plain.rb, line 3297 def initialize( *a ) @e = [] case a.length when 0 @e = [0.0, 0.0, 0.0] when 1 case a[0] when Float, Integer @e = [ a[0], a[0], a[0] ] when RVec3 @e = [ a[0].x, a[0].y, a[0].z ] else raise TypeError, "RVec3#initialize : Unknown type #{a[0].class}." return nil end when 3 a.each_with_index do |elem, index| case elem when Float, Integer @e[index] = elem else raise TypeError, "RVec3#initialize : Unknown type #{elem.class}." return nil end end else raise RuntimeError, "RVec3#initialize : wrong # of arguments (#{a.length})" return nil end return self end
Public Instance Methods
vec1 * vec2 : Binary multiply operator.
# File lib/rmath3d/rmath3d_plain.rb, line 3755 def *( arg ) case arg when Float, Integer return RVec3.new( @e[0]*arg, @e[1]*arg, @e[2]*arg ) else raise TypeError, "RVec3#* : Unknown type #{arg}." return nil end end
vec1 + vec2 : Binary plus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 3729 def +( arg ) if arg.class != RVec3 raise TypeError, "RVec3#+ : Unknown type #{arg.class}." return nil end RVec3.new( x+arg.x, y+arg.y, z+arg.z ) end
+vec : Unary plus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 3711 def +@ return self end
vec1 - vec2 : Binary minus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 3742 def -( arg ) if arg.class != RVec3 raise TypeError, "RVec3#- : Unknown type #{arg.class}." return nil end RVec3.new( x-arg.x, y-arg.y, z-arg.z ) end
-vec : Unary minus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 3720 def -@ return RVec3.new( -@e[0], -@e[1], -@e[2] ) end
vec1 == vec2 : evaluates equality.
# File lib/rmath3d/rmath3d_plain.rb, line 3770 def ==( other ) if other.class == RVec3 if (x-other.x).abs<=Float::EPSILON && (y-other.y).abs<=Float::EPSILON && (z-other.z).abs<=Float::EPSILON return true else return false end else return false end end
Returns the element at i
.
# File lib/rmath3d/rmath3d_plain.rb, line 3408 def [](i) @e[i] end
Stores value
at i
.
# File lib/rmath3d/rmath3d_plain.rb, line 3378 def []=(i,value) @e[i] = value end
vec1 += vec2 : appends the elements of vec2
into corresponding vec1
elements.
# File lib/rmath3d/rmath3d_plain.rb, line 3789 def add!( other ) if other.class != RVec3 raise TypeError, "RVec3#add! : Unknown type #{other.class}." return nil end self.x += other.x self.y += other.y self.z += other.z return self end
Resolves type mismatch.
# File lib/rmath3d/rmath3d_plain.rb, line 3352 def coerce( arg ) case arg when Float, Integer return [ self, arg ] else raise TypeError, "RVec3#coerce : #{arg.self} can't be coerced into #{self.class}." return nil end end
Returns the Euclidean length.
# File lib/rmath3d/rmath3d_plain.rb, line 3438 def getLength return Math.sqrt( @e[0]*@e[0] + @e[1]*@e[1] + @e[2]*@e[2] ) end
Returns the squared Euclidean length.
# File lib/rmath3d/rmath3d_plain.rb, line 3447 def getLengthSq return (@e[0]*@e[0] + @e[1]*@e[1] + @e[2]*@e[2]).to_f end
Returns normalized vector.
# File lib/rmath3d/rmath3d_plain.rb, line 3686 def getNormalized l = getLength() l = 1.0/l return RVec3.new( @e[0]*l, @e[1]*l, @e[2]*l ) end
vec1 *= vec2
# File lib/rmath3d/rmath3d_plain.rb, line 3825 def mul!( arg ) if !(arg.class == Float || arg.class == Integer) raise TypeError, "RVec3#mul! : Unknown type #{arg.class}." return nil end self.x *= arg self.y *= arg self.z *= arg return self end
Normalizes itself.
# File lib/rmath3d/rmath3d_plain.rb, line 3697 def normalize! l = getLength() l = 1.0/l @e[0] *= l @e[1] *= l @e[2] *= l return self end
Stores given 3 new values.
# File lib/rmath3d/rmath3d_plain.rb, line 3367 def setElements( x, y, z ) self.x = x self.y = y self.z = z end
vec1 -= vec2 : subtracts the elements of vec2
from corresponding vec1
elements.
# File lib/rmath3d/rmath3d_plain.rb, line 3807 def sub!( other ) if other.class != RVec3 raise TypeError, "RVec3#sub! : Unknown type #{other.class}." return nil end self.x -= other.x self.y -= other.y self.z -= other.z return self end
Returns its elements as a new Array.
# File lib/rmath3d/rmath3d_plain.rb, line 3343 def to_a return @e end
Returns human-readable string.
# File lib/rmath3d/rmath3d_plain.rb, line 3334 def to_s return "( #{@e[0]}, #{@e[1]}, #{@e[2]} )" end
Returns new RVec4
containing the result of the transformation of
RVec4(self.x,self.y,self.z,1.0) by +mtx4+ (RMtx4).
# File lib/rmath3d/rmath3d_plain.rb, line 3477 def transform( mtx4 ) result = RVec4.new result.x = mtx4.e00 * self[0] + mtx4.e01 * self[1] + mtx4.e02 * self[2] + mtx4.e03 result.y = mtx4.e10 * self[0] + mtx4.e11 * self[1] + mtx4.e12 * self[2] + mtx4.e13 result.z = mtx4.e20 * self[0] + mtx4.e21 * self[1] + mtx4.e22 * self[2] + mtx4.e23 result.w = mtx4.e30 * self[0] + mtx4.e31 * self[1] + mtx4.e32 * self[2] + mtx4.e33 return result end
# File lib/rmath3d/rmath3d_plain.rb, line 3651 def transformByQuaternion( q ) result = RVec3.new t_x = q.w*self[0] + q.y*self[2] - q.z*self[1] t_y = q.w*self[1] - q.x*self[2] + q.z*self[0] t_z = q.w*self[2] + q.x*self[1] - q.y*self[0] t_w = - q.x*self[0] - q.y*self[1] - q.z*self[2] result.x = -t_w*q.x + t_x*q.w - t_y*q.z + t_z*q.y result.y = -t_w*q.y + t_x*q.z + t_y*q.w - t_z*q.x result.z = -t_w*q.z - t_x*q.y + t_y*q.x + t_z*q.w return result end
# File lib/rmath3d/rmath3d_plain.rb, line 3668 def transformByQuaternion!( q ) t_x = q.w*self[0] + q.y*self[2] - q.z*self[1] t_y = q.w*self[1] - q.x*self[2] + q.z*self[0] t_z = q.w*self[2] + q.x*self[1] - q.y*self[0] t_w = - q.x*self[0] - q.y*self[1] - q.z*self[2] self.x = -t_w*q.x + t_x*q.w - t_y*q.z + t_z*q.y self.y = -t_w*q.y + t_x*q.z + t_y*q.w - t_z*q.x self.z = -t_w*q.z - t_x*q.y + t_y*q.x + t_z*q.w return self end
Returns RVec3(x/w, y/w, z/w), where x,y,z and w are the elements of the transformation result:
RVec4(self.x,self.y,self.z,1.0).transform(+mtx+) -> RVec4(x,y,z,w). (mtx : RMtx4)
# File lib/rmath3d/rmath3d_plain.rb, line 3494 def transformCoord( mtx4 ) result = RVec3.new result.x = mtx4.e00 * self[0] + mtx4.e01 * self[1] + mtx4.e02 * self[2] + mtx4.e03 result.y = mtx4.e10 * self[0] + mtx4.e11 * self[1] + mtx4.e12 * self[2] + mtx4.e13 result.z = mtx4.e20 * self[0] + mtx4.e21 * self[1] + mtx4.e22 * self[2] + mtx4.e23 w = mtx4.e30 * self[0] + mtx4.e31 * self[1] + mtx4.e32 * self[2] + mtx4.e33 w = 1.0 / w result *= w return result end
Make itself as RVec3(x/w, y/w, z/w), where x,y,z and w are the elements of the transformation result:
RVec4(self.x,self.y,self.z,1.0).transform(+mtx+) -> RVec4(x,y,z,w). (mtx : RMtx4)
# File lib/rmath3d/rmath3d_plain.rb, line 3513 def transformCoord!( mtx4 ) x = self[0] y = self[1] z = self[2] w = mtx4.e30 * x + mtx4.e31 * y + mtx4.e32 * z + mtx4.e33 w = 1.0 / w self.x = w * (mtx4.e00 * x + mtx4.e01 * y + mtx4.e02 * z + mtx4.e03) self.y = w * (mtx4.e10 * x + mtx4.e11 * y + mtx4.e12 * z + mtx4.e13) self.z = w * (mtx4.e20 * x + mtx4.e21 * y + mtx4.e22 * z + mtx4.e23) return self end
Returns the transformation result of
RVec4(self.x,self.y,self.z,0.0).transform(mtx).xyz
Notice¶ ↑
-
mtx :
RMtx4
# File lib/rmath3d/rmath3d_plain.rb, line 3535 def transformNormal( mtx ) result = RVec3.new result.x = mtx.e00 * self[0] + mtx.e01 * self[1] + mtx.e02 * self[2] result.y = mtx.e10 * self[0] + mtx.e11 * self[1] + mtx.e12 * self[2] result.z = mtx.e20 * self[0] + mtx.e21 * self[1] + mtx.e22 * self[2] return result end
Make itself as the transformation result of
RVec4(self.x,self.y,self.z,0.0).transform(mtx).xyz
Notice¶ ↑
-
mtx :
RMtx4
# File lib/rmath3d/rmath3d_plain.rb, line 3553 def transformNormal!( mtx ) x = self[0] y = self[1] z = self[2] self.x = mtx.e00 * x + mtx.e01 * y + mtx.e02 * z self.y = mtx.e10 * x + mtx.e11 * y + mtx.e12 * z self.z = mtx.e20 * x + mtx.e21 * y + mtx.e22 * z return self end
Returns the transformation result of
RVec3(self.x,self.y,self.z).transform(mtx)
Notice¶ ↑
-
mtx :
RMtx3
-
the suffix “RS” means “matrix representing Rotational and Scaling transformation”.
# File lib/rmath3d/rmath3d_plain.rb, line 3575 def transformRS( mtx ) result = RVec3.new result.x = mtx.e00 * self[0] + mtx.e01 * self[1] + mtx.e02 * self[2] result.y = mtx.e10 * self[0] + mtx.e11 * self[1] + mtx.e12 * self[2] result.z = mtx.e20 * self[0] + mtx.e21 * self[1] + mtx.e22 * self[2] return result end
Makes itself as the transformation result of
RVec3(self.x,self.y,self.z).transform(mtx)
Notice¶ ↑
-
mtx :
RMtx3
-
the suffix “RS” means “matrix representing Rotational and Scaling transformation”.
# File lib/rmath3d/rmath3d_plain.rb, line 3595 def transformRS!( mtx ) x = self[0] y = self[1] z = self[2] self.x = mtx.e00 * x + mtx.e01 * y + mtx.e02 * z self.y = mtx.e10 * x + mtx.e11 * y + mtx.e12 * z self.z = mtx.e20 * x + mtx.e21 * y + mtx.e22 * z return self end
Returns the transformation result of
RVec3(self.x,self.y,self.z).transform(mtx^T)
Notice¶ ↑
-
mtx :
RMtx3
-
the suffix “RS” means “matrix representing Rotational and Scaling transformation”.
# File lib/rmath3d/rmath3d_plain.rb, line 3617 def transformRSTransposed( mtx ) result = RVec3.new result.x = mtx.e00 * self[0] + mtx.e10 * self[1] + mtx.e20 * self[2] result.y = mtx.e01 * self[0] + mtx.e11 * self[1] + mtx.e21 * self[2] result.z = mtx.e02 * self[0] + mtx.e12 * self[1] + mtx.e22 * self[2] return result end
Makes itself as the transformation result of
RVec3(self.x,self.y,self.z).transform(mtx^T)
Notice¶ ↑
-
mtx :
RMtx3
-
the suffix “RS” means “matrix representing Rotational and Scaling transformation”.
# File lib/rmath3d/rmath3d_plain.rb, line 3637 def transformRSTransposed!( mtx ) x = self[0] y = self[1] z = self[2] self.x = mtx.e00 * x + mtx.e10 * y + mtx.e20 * z self.y = mtx.e01 * x + mtx.e11 * y + mtx.e21 * z self.z = mtx.e02 * x + mtx.e12 * y + mtx.e22 * z return self end
Returns the value of x
.
# File lib/rmath3d/rmath3d_plain.rb, line 3417 def x() return @e[0] end
Stores value
as x
.
# File lib/rmath3d/rmath3d_plain.rb, line 3387 def x=(value) @e[0] = value end
Returns the value of y
.
# File lib/rmath3d/rmath3d_plain.rb, line 3424 def y() return @e[1] end
Stores value
as y
.
# File lib/rmath3d/rmath3d_plain.rb, line 3394 def y=(value) @e[1] = value end
Returns the value of z
.
# File lib/rmath3d/rmath3d_plain.rb, line 3431 def z() return @e[2] end
Stores value
as z
.
# File lib/rmath3d/rmath3d_plain.rb, line 3401 def z=(value) @e[2] = value end