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.



2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
# File 'lib/rmath3d/rmath3d_plain.rb', line 2851

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.



3051
3052
3053
# File 'lib/rmath3d/rmath3d_plain.rb', line 3051

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.



3198
3199
3200
3201
3202
3203
3204
3205
3206
# File 'lib/rmath3d/rmath3d_plain.rb', line 3198

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.



3172
3173
3174
3175
3176
3177
3178
# File 'lib/rmath3d/rmath3d_plain.rb', line 3172

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.



3154
3155
3156
# File 'lib/rmath3d/rmath3d_plain.rb', line 3154

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

vec1 - vec2 : Binary minus operator.



3185
3186
3187
3188
3189
3190
3191
# File 'lib/rmath3d/rmath3d_plain.rb', line 3185

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.



3163
3164
3165
# File 'lib/rmath3d/rmath3d_plain.rb', line 3163

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

#==(other) ⇒ Object

call-seq: ==

vec1 == vec2 : evaluates equality.



3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
# File 'lib/rmath3d/rmath3d_plain.rb', line 3213

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.



2987
2988
2989
# File 'lib/rmath3d/rmath3d_plain.rb', line 2987

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

#[]=(i, value) ⇒ Object

call-seq: vec4= value

Stores value at i.



2935
2936
2937
# File 'lib/rmath3d/rmath3d_plain.rb', line 2935

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.



3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
# File 'lib/rmath3d/rmath3d_plain.rb', line 3234

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.



2908
2909
2910
2911
2912
2913
2914
2915
2916
# File 'lib/rmath3d/rmath3d_plain.rb', line 2908

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.



3033
3034
3035
# File 'lib/rmath3d/rmath3d_plain.rb', line 3033

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.



3042
3043
3044
# File 'lib/rmath3d/rmath3d_plain.rb', line 3042

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.



3128
3129
3130
3131
3132
# File 'lib/rmath3d/rmath3d_plain.rb', line 3128

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



3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
# File 'lib/rmath3d/rmath3d_plain.rb', line 3272

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.



3139
3140
3141
3142
3143
3144
3145
3146
3147
# File 'lib/rmath3d/rmath3d_plain.rb', line 3139

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.



2923
2924
2925
2926
2927
2928
# File 'lib/rmath3d/rmath3d_plain.rb', line 2923

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.



3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
# File 'lib/rmath3d/rmath3d_plain.rb', line 3253

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.



2899
2900
2901
# File 'lib/rmath3d/rmath3d_plain.rb', line 2899

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



2890
2891
2892
# File 'lib/rmath3d/rmath3d_plain.rb', line 2890

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



3060
3061
3062
3063
3064
3065
3066
3067
3068
# File 'lib/rmath3d/rmath3d_plain.rb', line 3060

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



3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
# File 'lib/rmath3d/rmath3d_plain.rb', line 3075

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



3094
3095
3096
3097
3098
3099
3100
3101
3102
# File 'lib/rmath3d/rmath3d_plain.rb', line 3094

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



3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
# File 'lib/rmath3d/rmath3d_plain.rb', line 3109

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.



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

def w() return @e[3] end

#w=(value) ⇒ Object

call-seq: w= value

Stores value as w.



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

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

#xObject

call-seq: x -> value

Returns the value of x.



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

def x() return @e[0] end

#x=(value) ⇒ Object

call-seq: x= value

Stores value as x.



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

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



3024
3025
3026
# File 'lib/rmath3d/rmath3d_plain.rb', line 3024

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.



2972
2973
2974
2975
2976
2977
2978
2979
2980
# File 'lib/rmath3d/rmath3d_plain.rb', line 2972

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.



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

def y() return @e[1] end

#y=(value) ⇒ Object

call-seq: y= value

Stores value as y.



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

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

#zObject

call-seq: z -> value

Returns the value of z.



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

def z() return @e[2] end

#z=(value) ⇒ Object

call-seq: z= value

Stores value as z.



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

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