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.



3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
# File 'lib/rmath3d/rmath3d_plain.rb', line 3854

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.



4054
4055
4056
# File 'lib/rmath3d/rmath3d_plain.rb', line 4054

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.



4201
4202
4203
4204
4205
4206
4207
4208
4209
# File 'lib/rmath3d/rmath3d_plain.rb', line 4201

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.



4175
4176
4177
4178
4179
4180
4181
# File 'lib/rmath3d/rmath3d_plain.rb', line 4175

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.



4157
4158
4159
# File 'lib/rmath3d/rmath3d_plain.rb', line 4157

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

vec1 - vec2 : Binary minus operator.



4188
4189
4190
4191
4192
4193
4194
# File 'lib/rmath3d/rmath3d_plain.rb', line 4188

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.



4166
4167
4168
# File 'lib/rmath3d/rmath3d_plain.rb', line 4166

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

#==(other) ⇒ Object

call-seq: ==

vec1 == vec2 : evaluates equality.



4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
# File 'lib/rmath3d/rmath3d_plain.rb', line 4216

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.



3990
3991
3992
# File 'lib/rmath3d/rmath3d_plain.rb', line 3990

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

#[]=(i, value) ⇒ Object

call-seq: vec4= value

Stores value at i.



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

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.



4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
# File 'lib/rmath3d/rmath3d_plain.rb', line 4236

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.



3911
3912
3913
3914
3915
3916
3917
3918
3919
# File 'lib/rmath3d/rmath3d_plain.rb', line 3911

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.



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

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.



4045
4046
4047
# File 'lib/rmath3d/rmath3d_plain.rb', line 4045

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.



4131
4132
4133
4134
4135
# File 'lib/rmath3d/rmath3d_plain.rb', line 4131

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



4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
# File 'lib/rmath3d/rmath3d_plain.rb', line 4274

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.



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

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.



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

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.



4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
# File 'lib/rmath3d/rmath3d_plain.rb', line 4255

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.



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

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



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

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



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

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



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

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



4097
4098
4099
4100
4101
4102
4103
4104
4105
# File 'lib/rmath3d/rmath3d_plain.rb', line 4097

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



4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
# File 'lib/rmath3d/rmath3d_plain.rb', line 4112

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.



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

def w() return @e[3] end

#w=(value) ⇒ Object

call-seq: w= value

Stores value as w.



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

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

#xObject

call-seq: x -> value

Returns the value of x.



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

def x() return @e[0] end

#x=(value) ⇒ Object

call-seq: x= value

Stores value as x.



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

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



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

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.



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

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.



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

def y() return @e[1] end

#y=(value) ⇒ Object

call-seq: y= value

Stores value as y.



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

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

#zObject

call-seq: z -> value

Returns the value of z.



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

def z() return @e[2] end

#z=(value) ⇒ Object

call-seq: z= value

Stores value as z.



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

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