Class: RMath3D::RVec4

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

Overview

Document-class: RMath3D::RVec4 provies 4 element vector arithmetic.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a) ⇒ RVec4

call-seq:

RVec4.new -> (0,0,0,0)
RVec4.new(e) -> (e,e,e,e)
RVec4.new( other ) : Copy Constructor
RVec4.new( e0, e1, e2, e3 ) -> (e0,e1,e2,e3)

Creates a new 4 element vector.



3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
# File 'lib/rmath3d/rmath3d_plain.rb', line 3716

def initialize( *a )
  @e = []
  case a.length
  when 0
    @e = [0.0, 0.0, 0.0, 0.0]
  when 1
    case a[0]
    when Fixnum, Float
      @e = [ a[0], a[0], a[0], a[0] ]
    when RVec3
      @e = [ a[0].x, a[0].y, a[0].z, 0.0 ]
    when RVec4
      @e = [ a[0].x, a[0].y, a[0].z, a[0].w ]
    else
      raise TypeError, "RVec4#initialize : Unknown type #{a[0].class}."
      return nil
    end
  when 4
    a.each_with_index do |elem, index|
      case elem
      when Fixnum, Float
        @e[index] = elem
      else
        raise TypeError, "RVec4#initialize : Unknown type #{elem.class}."
        return nil
      end
    end
  else
    raise RuntimeError, "RVec4#initialize : wrong # of arguments (#{a.length})"
    return nil
  end
  return self
end

Class Method Details

.dot(v1, v2) ⇒ Object

call-seq: RVec4.dot(v1,v2) -> value

Calculates the dot product of v1 and v2.



3916
3917
3918
# File 'lib/rmath3d/rmath3d_plain.rb', line 3916

def RVec4.dot( v1, v2 )
  return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w
end

Instance Method Details

#*(arg) ⇒ Object

call-seq: *

vec1 * vec2 : Binary multiply operator.



4063
4064
4065
4066
4067
4068
4069
4070
4071
# File 'lib/rmath3d/rmath3d_plain.rb', line 4063

def *( arg )
  case arg
  when Fixnum, Float
    return RVec4.new( @e[0]*arg, @e[1]*arg, @e[2]*arg, @e[3]*arg )
  else
    raise TypeError, "RVec4#* : Unknown type #{arg}."
    return nil
  end
end

#+(arg) ⇒ Object

call-seq: +

vec1 + vec2 : Binary plus operator.



4037
4038
4039
4040
4041
4042
4043
# File 'lib/rmath3d/rmath3d_plain.rb', line 4037

def +( arg )
  if arg.class != RVec4
    raise TypeError, "RVec4#+ : Unknown type #{arg.class}."
    return nil
  end
  RVec4.new( x+arg.x, y+arg.y, z+arg.z, w+arg.w )
end

#+@Object

call-seq: +

+vec : Unary plus operator.



4019
4020
4021
# File 'lib/rmath3d/rmath3d_plain.rb', line 4019

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

vec1 - vec2 : Binary minus operator.



4050
4051
4052
4053
4054
4055
4056
# File 'lib/rmath3d/rmath3d_plain.rb', line 4050

def -( arg )
  if arg.class != RVec4
    raise TypeError, "RVec4#+ : Unknown type #{arg.class}."
    return nil
  end
  RVec4.new( x-arg.x, y-arg.y, z-arg.z, w-arg.w )
end

#-@Object

call-seq: -

-vec : Unary minus operator.



4028
4029
4030
# File 'lib/rmath3d/rmath3d_plain.rb', line 4028

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

#==(other) ⇒ Object

call-seq: ==

vec1 == vec2 : evaluates equality.



4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
# File 'lib/rmath3d/rmath3d_plain.rb', line 4078

def ==( other )
  if other.class == RVec4
    if  (x-other.x).abs<=Float::EPSILON &&
        (y-other.y).abs<=Float::EPSILON &&
        (z-other.z).abs<=Float::EPSILON &&
        (w-other.w).abs<=Float::EPSILON
      return true
    else
      return false
    end
  else
    return false
  end
end

#[](i) ⇒ Object

call-seq: vec4 -> value

Returns the element at i.



3852
3853
3854
# File 'lib/rmath3d/rmath3d_plain.rb', line 3852

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

#[]=(i, value) ⇒ Object

call-seq: vec4= value

Stores value at i.



3800
3801
3802
# File 'lib/rmath3d/rmath3d_plain.rb', line 3800

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.



4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
# File 'lib/rmath3d/rmath3d_plain.rb', line 4098

def add!( other )
  if other.class != RVec4
    raise TypeError, "RVec4#add! : Unknown type #{other.class}."
    return nil
  end

  self.x += other.x
  self.y += other.y
  self.z += other.z
  self.w += other.w

  return self
end

#coerce(arg) ⇒ Object

call-seq: coerse(other)

Resolves type mismatch.



3773
3774
3775
3776
3777
3778
3779
3780
3781
# File 'lib/rmath3d/rmath3d_plain.rb', line 3773

def coerce( arg )
  case arg
  when Fixnum, Float, Bignum
    return [ self, arg ]
  else
    raise TypeError, "RVec4#coerce : #{arg.self} can't be coerced into  #{self.class}."
    return nil
  end
end

#getLengthObject

call-seq: getLength

Returns the Euclidean length.



3898
3899
3900
# File 'lib/rmath3d/rmath3d_plain.rb', line 3898

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

#getLengthSqObject

call-seq: getLengthSq

Returns the squared Euclidean length.



3907
3908
3909
# File 'lib/rmath3d/rmath3d_plain.rb', line 3907

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

#getNormalizedObject

call-seq: getNormalized -> RVec4

Returns normalized vector.



3993
3994
3995
3996
3997
# File 'lib/rmath3d/rmath3d_plain.rb', line 3993

def getNormalized
  l = getLength()
  l = 1.0/l
  return RVec4.new( @e[0]*l, @e[1]*l, @e[2]*l, @e[3]*l )
end

#mul!(other) ⇒ Object

call-seq: vec1.mul!( vec2 )

vec1 *= vec2



4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
# File 'lib/rmath3d/rmath3d_plain.rb', line 4136

def mul!( other )
  if other.class != Fixnum && other.class != Float
    raise TypeError, "RVec4#mul! : Unknown type #{other.class}."
    return nil
  end

  self.x *= other
  self.y *= other
  self.z *= other
  self.w *= other

  return self
end

#normalize!Object

call-seq: normalize! -> self

Normalizes itself.



4004
4005
4006
4007
4008
4009
4010
4011
4012
# File 'lib/rmath3d/rmath3d_plain.rb', line 4004

def normalize!
  l = getLength()
  l = 1.0/l
  @e[0] *= l
  @e[1] *= l
  @e[2] *= l
  @e[3] *= l
  return self
end

#setElements(x, y, z, w) ⇒ Object

call-seq: setElements( e0, e1, e2, e3 )

Stores given 4 new values.



3788
3789
3790
3791
3792
3793
# File 'lib/rmath3d/rmath3d_plain.rb', line 3788

def setElements( x, y, z, w )
  self.x = x
  self.y = y
  self.z = z
  self.w = w
end

#sub!(other) ⇒ Object

call-seq: vec1.sub!( vec2 )

vec1 -= vec2 : subtracts the elements of vec2 from corresponding vec1 elements.



4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
# File 'lib/rmath3d/rmath3d_plain.rb', line 4117

def sub!( other )
  if other.class != RVec4
    raise TypeError, "RVec4#sub! : Unknown type #{other.class}."
    return nil
  end

  self.x -= other.x
  self.y -= other.y
  self.z -= other.z
  self.w -= other.w

  return self
end

#to_aObject

call-seq: to_a

Returns its elements as a new Array.



3764
3765
3766
# File 'lib/rmath3d/rmath3d_plain.rb', line 3764

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



3755
3756
3757
# File 'lib/rmath3d/rmath3d_plain.rb', line 3755

def to_s
  return "( #{@e[0]}, #{@e[1]}, #{@e[2]}, #{@e[3]} )"
end

#transform(mtx) ⇒ Object

call-seq: transform(mtx4) -> transformed RVec4

Returns new RVec4 containing the result of the transformation by mtx4 (RMtx4).



3925
3926
3927
3928
3929
3930
3931
3932
3933
# File 'lib/rmath3d/rmath3d_plain.rb', line 3925

def transform( mtx )
  result = RVec4.new
  result.x = mtx.e00 * self[0] + mtx.e01 * self[1] + mtx.e02 * self[2] + mtx.e03 * self[3]
  result.y = mtx.e10 * self[0] + mtx.e11 * self[1] + mtx.e12 * self[2] + mtx.e13 * self[3]
  result.z = mtx.e20 * self[0] + mtx.e21 * self[1] + mtx.e22 * self[2] + mtx.e23 * self[3]
  result.w = mtx.e30 * self[0] + mtx.e31 * self[1] + mtx.e32 * self[2] + mtx.e33 * self[3]

  return result
end

#transform!(mtx) ⇒ Object

call-seq: transform(mtx4) -> self

Applies the transform matrix mtx4 (RMtx4).



3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
# File 'lib/rmath3d/rmath3d_plain.rb', line 3940

def transform!( mtx )
  x = self[0]
  y = self[1]
  z = self[2]
  w = self[3]

  self.x = mtx.e00 * x + mtx.e01 * y + mtx.e02 * z + mtx.e03 * w
  self.y = mtx.e10 * x + mtx.e11 * y + mtx.e12 * z + mtx.e13 * w
  self.z = mtx.e20 * x + mtx.e21 * y + mtx.e22 * z + mtx.e23 * w
  self.w = mtx.e30 * x + mtx.e31 * y + mtx.e32 * z + mtx.e33 * w

  return self
end

#transformTransposed(mtx) ⇒ Object

call-seq: transformTransposed(mtx4) -> RVec4 transformed by mtx4^T

Returns new RVec4 containing the result of the transformation by mtx4^T (RMtx4).



3959
3960
3961
3962
3963
3964
3965
3966
3967
# File 'lib/rmath3d/rmath3d_plain.rb', line 3959

def transformTransposed( mtx )
  result = RVec4.new
  result.x = mtx.e00 * self[0] + mtx.e10 * self[1] + mtx.e20 * self[2] + mtx.e30 * self[3]
  result.y = mtx.e01 * self[0] + mtx.e11 * self[1] + mtx.e21 * self[2] + mtx.e31 * self[3]
  result.z = mtx.e02 * self[0] + mtx.e12 * self[1] + mtx.e22 * self[2] + mtx.e32 * self[3]
  result.w = mtx.e03 * self[0] + mtx.e13 * self[1] + mtx.e23 * self[2] + mtx.e33 * self[3]

  return result
end

#transformTransposed!(mtx) ⇒ Object

call-seq: transformTransposed!(mtx4) -> self

Applies the transform matrix mtx4^T (RMtx4).



3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
# File 'lib/rmath3d/rmath3d_plain.rb', line 3974

def transformTransposed!( mtx )
  x = self[0]
  y = self[1]
  z = self[2]
  w = self[3]

  self.x = mtx.e00 * x + mtx.e10 * y + mtx.e20 * z + mtx.e30 * w
  self.y = mtx.e01 * x + mtx.e11 * y + mtx.e21 * z + mtx.e31 * w
  self.z = mtx.e02 * x + mtx.e12 * y + mtx.e22 * z + mtx.e32 * w
  self.w = mtx.e03 * x + mtx.e13 * y + mtx.e23 * z + mtx.e33 * w

  return self
end

#wObject

call-seq: w -> value

Returns the value of w.



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

def w() return @e[3] end

#w=(value) ⇒ Object

call-seq: w= value

Stores value as w.



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

def w=(value) @e[3] = value end

#xObject

call-seq: x -> value

Returns the value of x.



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

def x() return @e[0] end

#x=(value) ⇒ Object

call-seq: x= value

Stores value as x.



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

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

#xyzObject

call-seq: xyz -> RVec3

Returns the values of x, y and z with new RVec3(x,y,z).



3889
3890
3891
# File 'lib/rmath3d/rmath3d_plain.rb', line 3889

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

#xyz=(arg) ⇒ Object

call-seq: xyz= vXYZ

Copies the values of vXYZ(RVec3) into x, y and z.



3837
3838
3839
3840
3841
3842
3843
3844
3845
# File 'lib/rmath3d/rmath3d_plain.rb', line 3837

def xyz=( arg )
  if arg.class != RVec3
    raise TypeError, "RVec4#xyz= : Unknown type #{arg.class}."
    return nil
  end
  @e[0] = arg.x
  @e[1] = arg.y
  @e[2] = arg.z
end

#yObject

call-seq: y -> value

Returns the value of y.



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

def y() return @e[1] end

#y=(value) ⇒ Object

call-seq: y= value

Stores value as y.



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

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

#zObject

call-seq: z -> value

Returns the value of z.



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

def z() return @e[2] end

#z=(value) ⇒ Object

call-seq: z= value

Stores value as z.



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

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