Class: RMath3D::RVec3
- Inherits:
-
Object
- Object
- RMath3D::RVec3
- Defined in:
- lib/rmath3d/rmath3d_plain.rb
Overview
Document-class: RMath3D::RVec3 provies 3 element vector arithmetic.
Class Method Summary collapse
-
.cross(v1, v2) ⇒ Object
call-seq: RVec3.cross(v_a,v_b) -> RVec3(v_a x v_b).
-
.dot(v1, v2) ⇒ Object
call-seq: RVec3.dot(v_a,v_b) -> value.
Instance Method Summary collapse
-
#*(arg) ⇒ Object
call-seq: *.
-
#+(arg) ⇒ Object
call-seq: +.
-
#+@ ⇒ Object
call-seq: +.
-
#-(arg) ⇒ Object
call-seq: -.
-
#-@ ⇒ Object
call-seq: -.
-
#==(other) ⇒ Object
call-seq: ==.
-
#[](i) ⇒ Object
call-seq: vec3 -> value.
-
#[]=(i, value) ⇒ Object
call-seq: vec3= value.
-
#add!(other) ⇒ Object
call-seq: vec1.add!( vec2 ).
-
#coerce(arg) ⇒ Object
call-seq: coerse(other).
-
#getLength ⇒ Object
call-seq: getLength.
-
#getLengthSq ⇒ Object
call-seq: getLengthSq.
-
#getNormalized ⇒ Object
call-seq: getNormalized -> RVec3.
-
#initialize(*a) ⇒ RVec3
constructor
call-seq: RVec3.new -> (0,0,0) RVec3.new(e) -> (e,e,e) RVec3.new( other ) : Copy Constructor RVec3.new( e0, e1, e2 ) -> (e0,e1,e2).
-
#mul!(arg) ⇒ Object
call-seq: vec1.mul!( vec2 ).
-
#normalize! ⇒ Object
call-seq: normalize! -> self.
-
#setElements(x, y, z) ⇒ Object
call-seq: setElements( e0, e1, e2 ).
-
#sub!(other) ⇒ Object
call-seq: vec1.sub!( vec2 ).
-
#to_a ⇒ Object
call-seq: to_a.
-
#to_s ⇒ Object
call-seq: to_s.
-
#transform(mtx4) ⇒ Object
call-seq: transform(mtx4) -> transformed RVec4.
-
#transformByQuaternion(q) ⇒ Object
call-seq: transformByQuaternion(q) -> transformed RVec3.
-
#transformByQuaternion!(q) ⇒ Object
call-seq: transformByQuaternion!(q) -> self.
-
#transformCoord(mtx4) ⇒ Object
call-seq: transformCoord(mtx) -> transformed RVec3.
-
#transformCoord!(mtx4) ⇒ Object
call-seq: transformCoord!(mtx) -> self.
-
#transformNormal(mtx) ⇒ Object
call-seq: transformNormal(mtx) -> transformed RVec3.
-
#transformNormal!(mtx) ⇒ Object
call-seq: transformNormal!(mtx) -> self.
-
#transformRS(mtx) ⇒ Object
call-seq: transformRS(mtx) -> transformed RVec3.
-
#transformRS!(mtx) ⇒ Object
call-seq: transformRS!(mtx) -> self.
-
#transformRSTransposed(mtx) ⇒ Object
call-seq: transformRSTransposed(mtx) -> RVec3 transformed by mtx^T.
-
#transformRSTransposed!(mtx) ⇒ Object
call-seq: transformRSTransposed!(mtx) -> self.
-
#x ⇒ Object
call-seq: x -> value.
-
#x=(value) ⇒ Object
call-seq: x= value.
-
#y ⇒ Object
call-seq: y -> value.
-
#y=(value) ⇒ Object
call-seq: y= value.
-
#z ⇒ Object
call-seq: z -> value.
-
#z=(value) ⇒ Object
call-seq: z= value.
Constructor Details
#initialize(*a) ⇒ RVec3
call-seq:
RVec3.new -> (0,0,0)
RVec3.new(e) -> (e,e,e)
RVec3.new( other ) : Copy Constructor
RVec3.new( e0, e1, e2 ) -> (e0,e1,e2)
Creates a new 3 element vector.
3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3164 def initialize( *a ) @e = [] case a.length when 0 @e = [0.0, 0.0, 0.0] when 1 case a[0] when Fixnum, Float @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 Fixnum, Float @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 |
Class Method Details
.cross(v1, v2) ⇒ Object
call-seq: RVec3.cross(v_a,v_b) -> RVec3(v_a x v_b)
Calculates the cross product of v_a and v_b.
3332 3333 3334 3335 3336 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3332 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 |
.dot(v1, v2) ⇒ Object
call-seq: RVec3.dot(v_a,v_b) -> value
Calculates the dot product of v_a and v_b.
3323 3324 3325 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3323 def RVec3.dot( v1, v2 ) return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z end |
Instance Method Details
#*(arg) ⇒ Object
call-seq: *
vec1 * vec2 : Binary multiply operator.
3622 3623 3624 3625 3626 3627 3628 3629 3630 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3622 def *( arg ) case arg when Fixnum, Float return RVec3.new( @e[0]*arg, @e[1]*arg, @e[2]*arg ) else raise TypeError, "RVec3#* : Unknown type #{arg}." return nil end end |
#+(arg) ⇒ Object
call-seq: +
vec1 + vec2 : Binary plus operator.
3596 3597 3598 3599 3600 3601 3602 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3596 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 |
#+@ ⇒ Object
call-seq: +
+vec : Unary plus operator.
3578 3579 3580 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3578 def +@ return self end |
#-(arg) ⇒ Object
call-seq: -
vec1 - vec2 : Binary minus operator.
3609 3610 3611 3612 3613 3614 3615 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3609 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 |
#-@ ⇒ Object
call-seq: -
-vec : Unary minus operator.
3587 3588 3589 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3587 def -@ return RVec3.new( -@e[0], -@e[1], -@e[2] ) end |
#==(other) ⇒ Object
call-seq: ==
vec1 == vec2 : evaluates equality.
3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3637 def ==( other ) if other.class != RVec3 raise TypeError, "RVec3#== : Unknown type #{other.class}." return nil end 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 end |
#[](i) ⇒ Object
call-seq: vec3 -> value
Returns the element at i.
3275 3276 3277 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3275 def [](i) @e[i] end |
#[]=(i, value) ⇒ Object
call-seq: vec3= value
Stores value at i.
3245 3246 3247 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3245 def []=(i,value) @e[i] = value end |
#add!(other) ⇒ Object
call-seq: vec1.add!( vec2 )
vec1 += vec2 : appends the elements of vec2 into corresponding vec1 elements.
3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3657 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 |
#coerce(arg) ⇒ Object
call-seq: coerse(other)
Resolves type mismatch.
3219 3220 3221 3222 3223 3224 3225 3226 3227 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3219 def coerce( arg ) case arg when Fixnum, Float, Bignum return [ self, arg ] else raise TypeError, "RVec3#coerce : #{arg.self} can't be coerced into #{self.class}." return nil end end |
#getLength ⇒ Object
call-seq: getLength
Returns the Euclidean length.
3305 3306 3307 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3305 def getLength return Math.sqrt( @e[0]*@e[0] + @e[1]*@e[1] + @e[2]*@e[2] ) end |
#getLengthSq ⇒ Object
call-seq: getLengthSq
Returns the squared Euclidean length.
3314 3315 3316 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3314 def getLengthSq return (@e[0]*@e[0] + @e[1]*@e[1] + @e[2]*@e[2]).to_f end |
#getNormalized ⇒ Object
call-seq: getNormalized -> RVec3
Returns normalized vector.
3553 3554 3555 3556 3557 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3553 def getNormalized l = getLength() l = 1.0/l return RVec3.new( @e[0]*l, @e[1]*l, @e[2]*l ) end |
#mul!(arg) ⇒ Object
call-seq: vec1.mul!( vec2 )
vec1 *= vec2
3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3693 def mul!( arg ) if arg.class != Fixnum && arg.class != Float raise TypeError, "RVec3#mul! : Unknown type #{arg.class}." return nil end self.x *= arg self.y *= arg self.z *= arg return self end |
#normalize! ⇒ Object
call-seq: normalize! -> self
Normalizes itself.
3564 3565 3566 3567 3568 3569 3570 3571 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3564 def normalize! l = getLength() l = 1.0/l @e[0] *= l @e[1] *= l @e[2] *= l return self end |
#setElements(x, y, z) ⇒ Object
call-seq: setElements( e0, e1, e2 )
Stores given 3 new values.
3234 3235 3236 3237 3238 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3234 def setElements( x, y, z ) self.x = x self.y = y self.z = z end |
#sub!(other) ⇒ Object
call-seq: vec1.sub!( vec2 )
vec1 -= vec2 : subtracts the elements of vec2 from corresponding vec1 elements.
3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3675 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 |
#to_a ⇒ Object
call-seq: to_a
Returns its elements as a new Array.
3210 3211 3212 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3210 def to_a return @e end |
#to_s ⇒ Object
call-seq: to_s
Returns human-readable string.
3201 3202 3203 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3201 def to_s return "( #{@e[0]}, #{@e[1]}, #{@e[2]} )\n" end |
#transform(mtx4) ⇒ Object
call-seq: transform(mtx4) -> transformed RVec4
Returns new RVec4 containing the result of the transformation of
RVec4(self.x,self.y,self.z,1.0) by +mtx4+ (RMtx4).
3344 3345 3346 3347 3348 3349 3350 3351 3352 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3344 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 |
#transformByQuaternion(q) ⇒ Object
call-seq: transformByQuaternion(q) -> transformed RVec3
3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3518 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 |
#transformByQuaternion!(q) ⇒ Object
call-seq: transformByQuaternion!(q) -> self
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3535 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 |
#transformCoord(mtx4) ⇒ Object
call-seq: transformCoord(mtx) -> transformed RVec3
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)
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3361 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 |
#transformCoord!(mtx4) ⇒ Object
call-seq: transformCoord!(mtx) -> self
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)
3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3380 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 |
#transformNormal(mtx) ⇒ Object
call-seq: transformNormal(mtx) -> transformed RVec3
Returns the transformation result of
RVec4(self.x,self.y,self.z,0.0).transform(mtx).xyz
Notice
-
mtx : RMtx4
3402 3403 3404 3405 3406 3407 3408 3409 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3402 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 |
#transformNormal!(mtx) ⇒ Object
call-seq: transformNormal!(mtx) -> self
Make itself as the transformation result of
RVec4(self.x,self.y,self.z,0.0).transform(mtx).xyz
Notice
-
mtx : RMtx4
3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3420 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 |
#transformRS(mtx) ⇒ Object
call-seq: transformRS(mtx) -> transformed RVec3
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”.
3442 3443 3444 3445 3446 3447 3448 3449 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3442 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 |
#transformRS!(mtx) ⇒ Object
call-seq: transformRS!(mtx) -> self
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”.
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3462 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 |
#transformRSTransposed(mtx) ⇒ Object
call-seq: transformRSTransposed(mtx) -> RVec3 transformed by mtx^T
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”.
3484 3485 3486 3487 3488 3489 3490 3491 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3484 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 |
#transformRSTransposed!(mtx) ⇒ Object
call-seq: transformRSTransposed!(mtx) -> self
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”.
3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3504 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 |
#x ⇒ Object
call-seq: x -> value
Returns the value of x.
3284 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3284 def x() return @e[0] end |
#x=(value) ⇒ Object
call-seq: x= value
Stores value as x.
3254 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3254 def x=(value) @e[0] = value end |
#y ⇒ Object
call-seq: y -> value
Returns the value of y.
3291 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3291 def y() return @e[1] end |
#y=(value) ⇒ Object
call-seq: y= value
Stores value as y.
3261 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3261 def y=(value) @e[1] = value end |
#z ⇒ Object
call-seq: z -> value
Returns the value of z.
3298 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3298 def z() return @e[2] end |
#z=(value) ⇒ Object
call-seq: z= value
Stores value as z.
3268 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3268 def z=(value) @e[2] = value end |