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.



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
3749
3750
3751
3752
3753
3754
# File 'lib/rmath3d/rmath3d_plain.rb', line 3722

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.



3922
3923
3924
# File 'lib/rmath3d/rmath3d_plain.rb', line 3922

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.



4069
4070
4071
4072
4073
4074
4075
4076
4077
# File 'lib/rmath3d/rmath3d_plain.rb', line 4069

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.



4043
4044
4045
4046
4047
4048
4049
# File 'lib/rmath3d/rmath3d_plain.rb', line 4043

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.



4025
4026
4027
# File 'lib/rmath3d/rmath3d_plain.rb', line 4025

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

vec1 - vec2 : Binary minus operator.



4056
4057
4058
4059
4060
4061
4062
# File 'lib/rmath3d/rmath3d_plain.rb', line 4056

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.



4034
4035
4036
# File 'lib/rmath3d/rmath3d_plain.rb', line 4034

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

#==(other) ⇒ Object

call-seq: ==

vec1 == vec2 : evaluates equality.



4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
# File 'lib/rmath3d/rmath3d_plain.rb', line 4084

def ==( other )
  if other.class != RVec4
    raise TypeError, "RVec4#== : 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 &&
      (w-other.w).abs<=Float::EPSILON
    return true
  else
    return false
  end
end

#[](i) ⇒ Object

call-seq: vec4 -> value

Returns the element at i.



3858
3859
3860
# File 'lib/rmath3d/rmath3d_plain.rb', line 3858

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

#[]=(i, value) ⇒ Object

call-seq: vec4= value

Stores value at i.



3806
3807
3808
# File 'lib/rmath3d/rmath3d_plain.rb', line 3806

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.



4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
# File 'lib/rmath3d/rmath3d_plain.rb', line 4105

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.



3779
3780
3781
3782
3783
3784
3785
3786
3787
# File 'lib/rmath3d/rmath3d_plain.rb', line 3779

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.



3904
3905
3906
# File 'lib/rmath3d/rmath3d_plain.rb', line 3904

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.



3913
3914
3915
# File 'lib/rmath3d/rmath3d_plain.rb', line 3913

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.



3999
4000
4001
4002
4003
# File 'lib/rmath3d/rmath3d_plain.rb', line 3999

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



4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
# File 'lib/rmath3d/rmath3d_plain.rb', line 4143

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.



4010
4011
4012
4013
4014
4015
4016
4017
4018
# File 'lib/rmath3d/rmath3d_plain.rb', line 4010

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.



3794
3795
3796
3797
3798
3799
# File 'lib/rmath3d/rmath3d_plain.rb', line 3794

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.



4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
# File 'lib/rmath3d/rmath3d_plain.rb', line 4124

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.



3770
3771
3772
# File 'lib/rmath3d/rmath3d_plain.rb', line 3770

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



3761
3762
3763
# File 'lib/rmath3d/rmath3d_plain.rb', line 3761

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

#transform(mtx) ⇒ Object

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

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



3931
3932
3933
3934
3935
3936
3937
3938
3939
# File 'lib/rmath3d/rmath3d_plain.rb', line 3931

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



3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
# File 'lib/rmath3d/rmath3d_plain.rb', line 3946

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



3965
3966
3967
3968
3969
3970
3971
3972
3973
# File 'lib/rmath3d/rmath3d_plain.rb', line 3965

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



3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
# File 'lib/rmath3d/rmath3d_plain.rb', line 3980

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.



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

def w() return @e[3] end

#w=(value) ⇒ Object

call-seq: w= value

Stores value as w.



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

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

#xObject

call-seq: x -> value

Returns the value of x.



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

def x() return @e[0] end

#x=(value) ⇒ Object

call-seq: x= value

Stores value as x.



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

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



3895
3896
3897
# File 'lib/rmath3d/rmath3d_plain.rb', line 3895

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.



3843
3844
3845
3846
3847
3848
3849
3850
3851
# File 'lib/rmath3d/rmath3d_plain.rb', line 3843

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.



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

def y() return @e[1] end

#y=(value) ⇒ Object

call-seq: y= value

Stores value as y.



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

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

#zObject

call-seq: z -> value

Returns the value of z.



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

def z() return @e[2] end

#z=(value) ⇒ Object

call-seq: z= value

Stores value as z.



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

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