class RMath3D::RVec4

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

Public Class Methods

dot(v1,v2) → value click to toggle source

Calculates the dot product of v1 and v2.

# 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
new → (0,0,0,0) click to toggle source
new(e) → (e,e,e,e)
new( other ) : Copy Constructor
new( e0, e1, e2, e3 ) → (e0,e1,e2,e3)

Creates a new 4 element vector.

# 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

Public Instance Methods

* click to toggle source

vec1 * vec2 : Binary multiply operator.

# 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
+ click to toggle source

vec1 + vec2 : Binary plus operator.

# 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
+ click to toggle source

+vec : Unary plus operator.

# File lib/rmath3d/rmath3d_plain.rb, line 4157
def +@
  return self
end
- click to toggle source

vec1 - vec2 : Binary minus operator.

# 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
- click to toggle source

-vec : Unary minus operator.

# File lib/rmath3d/rmath3d_plain.rb, line 4166
def -@
  return RVec4.new( -@e[0], -@e[1], -@e[2], -@e[3] )
end
== click to toggle source

vec1 == vec2 : evaluates equality.

# 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
vec4[i] → value click to toggle source

Returns the element at i.

# File lib/rmath3d/rmath3d_plain.rb, line 3990
def [](i)
  @e[i]
end
vec4[i]= value click to toggle source

Stores value at i.

# File lib/rmath3d/rmath3d_plain.rb, line 3938
def []=(i,value)
  @e[i] = value
end
add!( vec2 ) click to toggle source

vec1 += vec2 : appends the elements of vec2 into corresponding vec1 elements.

# 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
coerse(other) click to toggle source

Resolves type mismatch.

# 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
getLength click to toggle source

Returns the Euclidean length.

# 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
getLengthSq click to toggle source

Returns the squared Euclidean length.

# 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
getNormalized → RVec4 click to toggle source

Returns normalized vector.

# 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!( vec2 ) click to toggle source

vec1 *= vec2

# 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! → self click to toggle source

Normalizes itself.

# 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( e0, e1, e2, e3 ) click to toggle source

Stores given 4 new values.

# 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!( vec2 ) click to toggle source

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

# 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_a click to toggle source

Returns its elements as a new Array.

# File lib/rmath3d/rmath3d_plain.rb, line 3902
def to_a
  return @e
end
to_s click to toggle source

Returns human-readable string.

# File lib/rmath3d/rmath3d_plain.rb, line 3893
def to_s
  return "( #{@e[0]}, #{@e[1]}, #{@e[2]}, #{@e[3]} )"
end
transform(mtx4) → transformed RVec4 click to toggle source

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

# 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(mtx4) → self click to toggle source

Applies the transform matrix mtx4 (RMtx4).

# 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(mtx4) → RVec4 transformed by mtx4^T click to toggle source

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

# 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!(mtx4) → self click to toggle source

Applies the transform matrix +mtx4^T+ (RMtx4).

# 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
w → value click to toggle source

Returns the value of w.

# File lib/rmath3d/rmath3d_plain.rb, line 4020
def w() return @e[3] end
w= value click to toggle source

Stores value as w.

# File lib/rmath3d/rmath3d_plain.rb, line 3968
def w=(value) @e[3] = value end
x → value click to toggle source

Returns the value of x.

# File lib/rmath3d/rmath3d_plain.rb, line 3999
def x() return @e[0] end
x= value click to toggle source

Stores value as x.

# File lib/rmath3d/rmath3d_plain.rb, line 3947
def x=(value) @e[0] = value end
xyz → RVec3 click to toggle source

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

# File lib/rmath3d/rmath3d_plain.rb, line 4027
def xyz()
  return RVec3.new( @e[0], @e[1], @e[2] )
end
xyz= vXYZ click to toggle source

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

# 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
y → value click to toggle source

Returns the value of y.

# File lib/rmath3d/rmath3d_plain.rb, line 4006
def y() return @e[1] end
y= value click to toggle source

Stores value as y.

# File lib/rmath3d/rmath3d_plain.rb, line 3954
def y=(value) @e[1] = value end
z → value click to toggle source

Returns the value of z.

# File lib/rmath3d/rmath3d_plain.rb, line 4013
def z() return @e[2] end
z= value click to toggle source

Stores value as z.

# File lib/rmath3d/rmath3d_plain.rb, line 3961
def z=(value) @e[2] = value end