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.
3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3200 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 |
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
.
3368 3369 3370 3371 3372 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3368 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
.
3359 3360 3361 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3359 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.
3658 3659 3660 3661 3662 3663 3664 3665 3666 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3658 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 |
#+(arg) ⇒ Object
call-seq: +
vec1 + vec2 : Binary plus operator.
3632 3633 3634 3635 3636 3637 3638 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3632 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.
3614 3615 3616 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3614 def +@ return self end |
#-(arg) ⇒ Object
call-seq: -
vec1 - vec2 : Binary minus operator.
3645 3646 3647 3648 3649 3650 3651 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3645 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.
3623 3624 3625 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3623 def -@ return RVec3.new( -@e[0], -@e[1], -@e[2] ) end |
#==(other) ⇒ Object
call-seq: ==
vec1 == vec2 : evaluates equality.
3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3673 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 |
#[](i) ⇒ Object
call-seq: vec3 -> value
Returns the element at i
.
3311 3312 3313 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3311 def [](i) @e[i] end |
#[]=(i, value) ⇒ Object
call-seq: vec3= value
Stores value
at i
.
3281 3282 3283 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3281 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.
3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3692 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.
3255 3256 3257 3258 3259 3260 3261 3262 3263 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3255 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 |
#getLength ⇒ Object
call-seq: getLength
Returns the Euclidean length.
3341 3342 3343 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3341 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.
3350 3351 3352 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3350 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.
3589 3590 3591 3592 3593 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3589 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
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3728 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 |
#normalize! ⇒ Object
call-seq: normalize! -> self
Normalizes itself.
3600 3601 3602 3603 3604 3605 3606 3607 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3600 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.
3270 3271 3272 3273 3274 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3270 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.
3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3710 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.
3246 3247 3248 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3246 def to_a return @e end |
#to_s ⇒ Object
call-seq: to_s
Returns human-readable string.
3237 3238 3239 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3237 def to_s return "( #{@e[0]}, #{@e[1]}, #{@e[2]} )" 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).
3380 3381 3382 3383 3384 3385 3386 3387 3388 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3380 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
3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3554 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
3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3571 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)
3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3397 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)
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3416 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
3438 3439 3440 3441 3442 3443 3444 3445 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3438 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
3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3456 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”.
3478 3479 3480 3481 3482 3483 3484 3485 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3478 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”.
3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3498 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”.
3520 3521 3522 3523 3524 3525 3526 3527 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3520 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”.
3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3540 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
.
3320 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3320 def x() return @e[0] end |
#x=(value) ⇒ Object
call-seq: x= value
Stores value
as x
.
3290 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3290 def x=(value) @e[0] = value end |
#y ⇒ Object
call-seq: y -> value
Returns the value of y
.
3327 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3327 def y() return @e[1] end |
#y=(value) ⇒ Object
call-seq: y= value
Stores value
as y
.
3297 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3297 def y=(value) @e[1] = value end |
#z ⇒ Object
call-seq: z -> value
Returns the value of z
.
3334 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3334 def z() return @e[2] end |
#z=(value) ⇒ Object
call-seq: z= value
Stores value
as z
.
3304 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 3304 def z=(value) @e[2] = value end |