Class: RMath3D::RVec3

Inherits:
Object
  • Object
show all
Defined in:
lib/rmath3d/rmath3d_plain.rb

Overview

Document-class: RMath3D::RVec3 provies 3 element vector arithmetic.

Class Method Summary collapse

Instance Method Summary collapse

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.



3159
3160
3161
3162
3163
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
# File 'lib/rmath3d/rmath3d_plain.rb', line 3159

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.



3327
3328
3329
3330
3331
# File 'lib/rmath3d/rmath3d_plain.rb', line 3327

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.



3318
3319
3320
# File 'lib/rmath3d/rmath3d_plain.rb', line 3318

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.



3617
3618
3619
3620
3621
3622
3623
3624
3625
# File 'lib/rmath3d/rmath3d_plain.rb', line 3617

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.



3591
3592
3593
3594
3595
3596
3597
# File 'lib/rmath3d/rmath3d_plain.rb', line 3591

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.



3573
3574
3575
# File 'lib/rmath3d/rmath3d_plain.rb', line 3573

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

vec1 - vec2 : Binary minus operator.



3604
3605
3606
3607
3608
3609
3610
# File 'lib/rmath3d/rmath3d_plain.rb', line 3604

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.



3582
3583
3584
# File 'lib/rmath3d/rmath3d_plain.rb', line 3582

def -@
  return RVec3.new( -@e[0], -@e[1], -@e[2] )
end

#==(other) ⇒ Object

call-seq: ==

vec1 == vec2 : evaluates equality.



3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
# File 'lib/rmath3d/rmath3d_plain.rb', line 3632

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.



3270
3271
3272
# File 'lib/rmath3d/rmath3d_plain.rb', line 3270

def [](i)
  @e[i]
end

#[]=(i, value) ⇒ Object

call-seq: vec3= value

Stores value at i.



3240
3241
3242
# File 'lib/rmath3d/rmath3d_plain.rb', line 3240

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.



3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
# File 'lib/rmath3d/rmath3d_plain.rb', line 3651

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.



3214
3215
3216
3217
3218
3219
3220
3221
3222
# File 'lib/rmath3d/rmath3d_plain.rb', line 3214

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

#getLengthObject

call-seq: getLength

Returns the Euclidean length.



3300
3301
3302
# File 'lib/rmath3d/rmath3d_plain.rb', line 3300

def getLength
  return Math.sqrt( @e[0]*@e[0] + @e[1]*@e[1] + @e[2]*@e[2] )
end

#getLengthSqObject

call-seq: getLengthSq

Returns the squared Euclidean length.



3309
3310
3311
# File 'lib/rmath3d/rmath3d_plain.rb', line 3309

def getLengthSq
  return (@e[0]*@e[0] + @e[1]*@e[1] + @e[2]*@e[2]).to_f
end

#getNormalizedObject

call-seq: getNormalized -> RVec3

Returns normalized vector.



3548
3549
3550
3551
3552
# File 'lib/rmath3d/rmath3d_plain.rb', line 3548

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



3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
# File 'lib/rmath3d/rmath3d_plain.rb', line 3687

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.



3559
3560
3561
3562
3563
3564
3565
3566
# File 'lib/rmath3d/rmath3d_plain.rb', line 3559

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.



3229
3230
3231
3232
3233
# File 'lib/rmath3d/rmath3d_plain.rb', line 3229

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.



3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
# File 'lib/rmath3d/rmath3d_plain.rb', line 3669

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_aObject

call-seq: to_a

Returns its elements as a new Array.



3205
3206
3207
# File 'lib/rmath3d/rmath3d_plain.rb', line 3205

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



3196
3197
3198
# File 'lib/rmath3d/rmath3d_plain.rb', line 3196

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).


3339
3340
3341
3342
3343
3344
3345
3346
3347
# File 'lib/rmath3d/rmath3d_plain.rb', line 3339

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



3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
# File 'lib/rmath3d/rmath3d_plain.rb', line 3513

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



3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
# File 'lib/rmath3d/rmath3d_plain.rb', line 3530

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)


3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
# File 'lib/rmath3d/rmath3d_plain.rb', line 3356

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)


3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
# File 'lib/rmath3d/rmath3d_plain.rb', line 3375

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



3397
3398
3399
3400
3401
3402
3403
3404
# File 'lib/rmath3d/rmath3d_plain.rb', line 3397

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



3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
# File 'lib/rmath3d/rmath3d_plain.rb', line 3415

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”.



3437
3438
3439
3440
3441
3442
3443
3444
# File 'lib/rmath3d/rmath3d_plain.rb', line 3437

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”.



3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
# File 'lib/rmath3d/rmath3d_plain.rb', line 3457

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”.



3479
3480
3481
3482
3483
3484
3485
3486
# File 'lib/rmath3d/rmath3d_plain.rb', line 3479

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”.



3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
# File 'lib/rmath3d/rmath3d_plain.rb', line 3499

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

#xObject

call-seq: x -> value

Returns the value of x.



3279
# File 'lib/rmath3d/rmath3d_plain.rb', line 3279

def x() return @e[0] end

#x=(value) ⇒ Object

call-seq: x= value

Stores value as x.



3249
# File 'lib/rmath3d/rmath3d_plain.rb', line 3249

def x=(value) @e[0] = value end

#yObject

call-seq: y -> value

Returns the value of y.



3286
# File 'lib/rmath3d/rmath3d_plain.rb', line 3286

def y() return @e[1] end

#y=(value) ⇒ Object

call-seq: y= value

Stores value as y.



3256
# File 'lib/rmath3d/rmath3d_plain.rb', line 3256

def y=(value) @e[1] = value end

#zObject

call-seq: z -> value

Returns the value of z.



3293
# File 'lib/rmath3d/rmath3d_plain.rb', line 3293

def z() return @e[2] end

#z=(value) ⇒ Object

call-seq: z= value

Stores value as z.



3263
# File 'lib/rmath3d/rmath3d_plain.rb', line 3263

def z=(value) @e[2] = value end