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.



3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
# File 'lib/rmath3d/rmath3d_plain.rb', line 3757

def initialize( *a )
  @e = []
  case a.length
  when 0
    @e = [0.0, 0.0, 0.0, 0.0]
  when 1
    case a[0]
    when Float, Integer
      @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 Float, Integer
        @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.



3957
3958
3959
# File 'lib/rmath3d/rmath3d_plain.rb', line 3957

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.



4104
4105
4106
4107
4108
4109
4110
4111
4112
# File 'lib/rmath3d/rmath3d_plain.rb', line 4104

def *( arg )
  case arg
  when Float, Integer
    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.



4078
4079
4080
4081
4082
4083
4084
# File 'lib/rmath3d/rmath3d_plain.rb', line 4078

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.



4060
4061
4062
# File 'lib/rmath3d/rmath3d_plain.rb', line 4060

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

vec1 - vec2 : Binary minus operator.



4091
4092
4093
4094
4095
4096
4097
# File 'lib/rmath3d/rmath3d_plain.rb', line 4091

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.



4069
4070
4071
# File 'lib/rmath3d/rmath3d_plain.rb', line 4069

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

#==(other) ⇒ Object

call-seq: ==

vec1 == vec2 : evaluates equality.



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

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.



3893
3894
3895
# File 'lib/rmath3d/rmath3d_plain.rb', line 3893

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

#[]=(i, value) ⇒ Object

call-seq: vec4= value

Stores value at i.



3841
3842
3843
# File 'lib/rmath3d/rmath3d_plain.rb', line 3841

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.



4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
# File 'lib/rmath3d/rmath3d_plain.rb', line 4139

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.



3814
3815
3816
3817
3818
3819
3820
3821
3822
# File 'lib/rmath3d/rmath3d_plain.rb', line 3814

def coerce( arg )
  case arg
  when Float, Integer
    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.



3939
3940
3941
# File 'lib/rmath3d/rmath3d_plain.rb', line 3939

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.



3948
3949
3950
# File 'lib/rmath3d/rmath3d_plain.rb', line 3948

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.



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

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



4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
# File 'lib/rmath3d/rmath3d_plain.rb', line 4177

def mul!( other )
  if !(other.class == Float || other.class == Integer)
    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.



4045
4046
4047
4048
4049
4050
4051
4052
4053
# File 'lib/rmath3d/rmath3d_plain.rb', line 4045

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.



3829
3830
3831
3832
3833
3834
# File 'lib/rmath3d/rmath3d_plain.rb', line 3829

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.



4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
# File 'lib/rmath3d/rmath3d_plain.rb', line 4158

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.



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

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



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

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



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

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



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

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



4000
4001
4002
4003
4004
4005
4006
4007
4008
# File 'lib/rmath3d/rmath3d_plain.rb', line 4000

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



4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
# File 'lib/rmath3d/rmath3d_plain.rb', line 4015

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.



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

def w() return @e[3] end

#w=(value) ⇒ Object

call-seq: w= value

Stores value as w.



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

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

#xObject

call-seq: x -> value

Returns the value of x.



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

def x() return @e[0] end

#x=(value) ⇒ Object

call-seq: x= value

Stores value as x.



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

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



3930
3931
3932
# File 'lib/rmath3d/rmath3d_plain.rb', line 3930

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.



3878
3879
3880
3881
3882
3883
3884
3885
3886
# File 'lib/rmath3d/rmath3d_plain.rb', line 3878

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.



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

def y() return @e[1] end

#y=(value) ⇒ Object

call-seq: y= value

Stores value as y.



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

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

#zObject

call-seq: z -> value

Returns the value of z.



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

def z() return @e[2] end

#z=(value) ⇒ Object

call-seq: z= value

Stores value as z.



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

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