Class: RMath3D::RVec3

Inherits:
Object
  • Object
show all
Defined in:
lib/rmath3d/rmath3d_plain.rb

Overview

Document-class: RMath3D::RVec3 provies 3 element vector arithmetic.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a) ⇒ RVec3

call-seq:

RVec3.new -> (0,0,0)
RVec3.new(e) -> (e,e,e)
RVec3.new( other ) : Copy Constructor
RVec3.new( e0, e1, e2 ) -> (e0,e1,e2)

Creates a new 3 element vector.



2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
# File 'lib/rmath3d/rmath3d_plain.rb', line 2293

def initialize( *a )
  @e = []
  case a.length
  when 0
    @e = [0.0, 0.0, 0.0]
  when 1
    case a[0]
    when Fixnum, Float
      @e = [ a[0], a[0], a[0] ]
    when RVec3
      @e = [ a[0].x, a[0].y, a[0].z ]
    else
      raise TypeError, "RVec3#initialize : Unknown type #{a[0].class}."
      return nil
    end
  when 3
    a.each_with_index do |elem, index|
      case elem
      when Fixnum, Float
        @e[index] = elem
      else
        raise TypeError, "RVec3#initialize : Unknown type #{elem.class}."
        return nil
      end
    end
  else
    raise RuntimeError, "RVec3#initialize : wrong # of arguments (#{a.length})"
    return nil
  end
  return self
end

Class Method Details

.cross(v1, v2) ⇒ Object

call-seq: RVec3.cross(v_a,v_b) -> RVec3(v_a x v_b)

Calculates the cross product of v_a and v_b.



2461
2462
2463
2464
2465
# File 'lib/rmath3d/rmath3d_plain.rb', line 2461

def RVec3.cross( v1, v2 )
  return RVec3.new(v1.y*v2.z - v1.z*v2.y,
                   v1.z*v2.x - v1.x*v2.z,
                   v1.x*v2.y - v1.y*v2.x)
end

.dot(v1, v2) ⇒ Object

call-seq: RVec3.dot(v_a,v_b) -> value

Calculates the dot product of v_a and v_b.



2452
2453
2454
# File 'lib/rmath3d/rmath3d_plain.rb', line 2452

def RVec3.dot( v1, v2 )
  return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z
end

Instance Method Details

#*(arg) ⇒ Object

call-seq: *

vec1 * vec2 : Binary multiply operator.



2751
2752
2753
2754
2755
2756
2757
2758
2759
# File 'lib/rmath3d/rmath3d_plain.rb', line 2751

def *( arg )
  case arg
  when Fixnum, Float
    return RVec3.new( @e[0]*arg, @e[1]*arg, @e[2]*arg )
  else
    raise TypeError, "RVec3#* : Unknown type #{arg}."
    return nil
  end
end

#+(arg) ⇒ Object

call-seq: +

vec1 + vec2 : Binary plus operator.



2725
2726
2727
2728
2729
2730
2731
# File 'lib/rmath3d/rmath3d_plain.rb', line 2725

def +( arg )
  if arg.class != RVec3
    raise TypeError, "RVec3#+ : Unknown type #{arg.class}."
    return nil
  end
  RVec3.new( x+arg.x, y+arg.y, z+arg.z )
end

#+@Object

call-seq: +

+vec : Unary plus operator.



2707
2708
2709
# File 'lib/rmath3d/rmath3d_plain.rb', line 2707

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

vec1 - vec2 : Binary minus operator.



2738
2739
2740
2741
2742
2743
2744
# File 'lib/rmath3d/rmath3d_plain.rb', line 2738

def -( arg )
  if arg.class != RVec3
    raise TypeError, "RVec3#- : Unknown type #{arg.class}."
    return nil
  end
  RVec3.new( x-arg.x, y-arg.y, z-arg.z )
end

#-@Object

call-seq: -

-vec : Unary minus operator.



2716
2717
2718
# File 'lib/rmath3d/rmath3d_plain.rb', line 2716

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

#==(other) ⇒ Object

call-seq: ==

vec1 == vec2 : evaluates equality.



2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
# File 'lib/rmath3d/rmath3d_plain.rb', line 2766

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

#[](i) ⇒ Object

call-seq: vec3 -> value

Returns the element at i.



2404
2405
2406
# File 'lib/rmath3d/rmath3d_plain.rb', line 2404

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

#[]=(i, value) ⇒ Object

call-seq: vec3= value

Stores value at i.



2374
2375
2376
# File 'lib/rmath3d/rmath3d_plain.rb', line 2374

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.



2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
# File 'lib/rmath3d/rmath3d_plain.rb', line 2786

def add!( other )
  if other.class != RVec3
    raise TypeError, "RVec3#add! : Unknown type #{other.class}."
    return nil
  end

  self.x += other.x
  self.y += other.y
  self.z += other.z

  return self
end

#coerce(arg) ⇒ Object

call-seq: coerse(other)

Resolves type mismatch.



2348
2349
2350
2351
2352
2353
2354
2355
2356
# File 'lib/rmath3d/rmath3d_plain.rb', line 2348

def coerce( arg )
  case arg
  when Fixnum, Float, Bignum
    return [ self, arg ]
  else
    raise TypeError, "RVec3#coerce : #{arg.self} can't be coerced into  #{self.class}."
    return nil
  end
end

#getLengthObject

call-seq: getLength

Returns the Euclidean length.



2434
2435
2436
# File 'lib/rmath3d/rmath3d_plain.rb', line 2434

def getLength
  return Math.sqrt( @e[0]*@e[0] + @e[1]*@e[1] + @e[2]*@e[2] )
end

#getLengthSqObject

call-seq: getLengthSq

Returns the squared Euclidean length.



2443
2444
2445
# File 'lib/rmath3d/rmath3d_plain.rb', line 2443

def getLengthSq
  return (@e[0]*@e[0] + @e[1]*@e[1] + @e[2]*@e[2]).to_f
end

#getNormalizedObject

call-seq: getNormalized -> RVec3

Returns normalized vector.



2682
2683
2684
2685
2686
# File 'lib/rmath3d/rmath3d_plain.rb', line 2682

def getNormalized
  l = getLength()
  l = 1.0/l
  return RVec3.new( @e[0]*l, @e[1]*l, @e[2]*l )
end

#mul!(arg) ⇒ Object

call-seq: vec1.mul!( vec2 )

vec1 *= vec2



2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
# File 'lib/rmath3d/rmath3d_plain.rb', line 2822

def mul!( arg )
  if arg.class != Fixnum && arg.class != Float
    raise TypeError, "RVec3#mul! : Unknown type #{arg.class}."
    return nil
  end

  self.x *= arg
  self.y *= arg
  self.z *= arg

  return self
end

#normalize!Object

call-seq: normalize! -> self

Normalizes itself.



2693
2694
2695
2696
2697
2698
2699
2700
# File 'lib/rmath3d/rmath3d_plain.rb', line 2693

def normalize!
  l = getLength()
  l = 1.0/l
  @e[0] *= l
  @e[1] *= l
  @e[2] *= l
  return self
end

#setElements(x, y, z) ⇒ Object

call-seq: setElements( e0, e1, e2 )

Stores given 3 new values.



2363
2364
2365
2366
2367
# File 'lib/rmath3d/rmath3d_plain.rb', line 2363

def setElements( x, y, z )
  self.x = x
  self.y = y
  self.z = z
end

#sub!(other) ⇒ Object

call-seq: vec1.sub!( vec2 )

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



2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
# File 'lib/rmath3d/rmath3d_plain.rb', line 2804

def sub!( other )
  if other.class != RVec3
    raise TypeError, "RVec3#sub! : Unknown type #{other.class}."
    return nil
  end

  self.x -= other.x
  self.y -= other.y
  self.z -= other.z

  return self
end

#to_aObject

call-seq: to_a

Returns its elements as a new Array.



2339
2340
2341
# File 'lib/rmath3d/rmath3d_plain.rb', line 2339

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



2330
2331
2332
# File 'lib/rmath3d/rmath3d_plain.rb', line 2330

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

#transform(mtx4) ⇒ Object

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

Returns new RVec4 containing the result of the transformation of

RVec4(self.x,self.y,self.z,1.0) by +mtx4+ (RMtx4).


2473
2474
2475
2476
2477
2478
2479
2480
2481
# File 'lib/rmath3d/rmath3d_plain.rb', line 2473

def transform( mtx4 )
  result = RVec4.new
  result.x = mtx4.e00 * self[0] + mtx4.e01 * self[1] + mtx4.e02 * self[2] + mtx4.e03
  result.y = mtx4.e10 * self[0] + mtx4.e11 * self[1] + mtx4.e12 * self[2] + mtx4.e13
  result.z = mtx4.e20 * self[0] + mtx4.e21 * self[1] + mtx4.e22 * self[2] + mtx4.e23
  result.w = mtx4.e30 * self[0] + mtx4.e31 * self[1] + mtx4.e32 * self[2] + mtx4.e33

  return result
end

#transformByQuaternion(q) ⇒ Object

call-seq: transformByQuaternion(q) -> transformed RVec3



2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
# File 'lib/rmath3d/rmath3d_plain.rb', line 2647

def transformByQuaternion( q )
  result = RVec3.new
  t_x = q.w*self[0]               + q.y*self[2] - q.z*self[1]
  t_y = q.w*self[1] - q.x*self[2]               + q.z*self[0]
  t_z = q.w*self[2] + q.x*self[1] - q.y*self[0]
  t_w =             - q.x*self[0] - q.y*self[1] - q.z*self[2]

  result.x = -t_w*q.x + t_x*q.w - t_y*q.z + t_z*q.y;
  result.y = -t_w*q.y + t_x*q.z + t_y*q.w - t_z*q.x;
  result.z = -t_w*q.z - t_x*q.y + t_y*q.x + t_z*q.w;

  return result
end

#transformByQuaternion!(q) ⇒ Object

call-seq: transformByQuaternion!(q) -> self



2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
# File 'lib/rmath3d/rmath3d_plain.rb', line 2664

def transformByQuaternion!( q )
  t_x = q.w*self[0]               + q.y*self[2] - q.z*self[1]
  t_y = q.w*self[1] - q.x*self[2]               + q.z*self[0]
  t_z = q.w*self[2] + q.x*self[1] - q.y*self[0]
  t_w =             - q.x*self[0] - q.y*self[1] - q.z*self[2]

  self.x = -t_w*q.x + t_x*q.w - t_y*q.z + t_z*q.y;
  self.y = -t_w*q.y + t_x*q.z + t_y*q.w - t_z*q.x;
  self.z = -t_w*q.z - t_x*q.y + t_y*q.x + t_z*q.w;

  return self
end

#transformCoord(mtx4) ⇒ Object

call-seq: transformCoord(mtx) -> transformed RVec3

Returns RVec3(x/w, y/w, z/w), where x,y,z and w are the elements of the transformation result:

RVec4(self.x,self.y,self.z,1.0).transform(+mtx+) -> RVec4(x,y,z,w). (mtx : RMtx4)


2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
# File 'lib/rmath3d/rmath3d_plain.rb', line 2490

def transformCoord( mtx4 )
  result = RVec3.new
  result.x = mtx4.e00 * self[0] + mtx4.e01 * self[1] + mtx4.e02 * self[2] + mtx4.e03
  result.y = mtx4.e10 * self[0] + mtx4.e11 * self[1] + mtx4.e12 * self[2] + mtx4.e13
  result.z = mtx4.e20 * self[0] + mtx4.e21 * self[1] + mtx4.e22 * self[2] + mtx4.e23
  w = mtx4.e30 * self[0] + mtx4.e31 * self[1] + mtx4.e32 * self[2] + mtx4.e33
  w = 1.0 / w
  result *= w

  return result
end

#transformCoord!(mtx4) ⇒ Object

call-seq: transformCoord!(mtx) -> self

Make itself as RVec3(x/w, y/w, z/w), where x,y,z and w are the elements of the transformation result:

RVec4(self.x,self.y,self.z,1.0).transform(+mtx+) -> RVec4(x,y,z,w). (mtx : RMtx4)


2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
# File 'lib/rmath3d/rmath3d_plain.rb', line 2509

def transformCoord!( mtx4 )
  x = self[0]
  y = self[1]
  z = self[2]
  w = mtx4.e30 * x + mtx4.e31 * y + mtx4.e32 * z + mtx4.e33
  w = 1.0 / w
  self.x = w * (mtx4.e00 * x + mtx4.e01 * y + mtx4.e02 * z + mtx4.e03)
  self.y = w * (mtx4.e10 * x + mtx4.e11 * y + mtx4.e12 * z + mtx4.e13)
  self.z = w * (mtx4.e20 * x + mtx4.e21 * y + mtx4.e22 * z + mtx4.e23)

  return self
end

#transformNormal(mtx) ⇒ Object

call-seq: transformNormal(mtx) -> transformed RVec3

Returns the transformation result of

RVec4(self.x,self.y,self.z,0.0).transform(mtx).xyz

Notice

  • mtx : RMtx4



2531
2532
2533
2534
2535
2536
2537
2538
# File 'lib/rmath3d/rmath3d_plain.rb', line 2531

def transformNormal( mtx )
  result = RVec3.new
  result.x = mtx.e00 * self[0] + mtx.e01 * self[1] + mtx.e02 * self[2]
  result.y = mtx.e10 * self[0] + mtx.e11 * self[1] + mtx.e12 * self[2]
  result.z = mtx.e20 * self[0] + mtx.e21 * self[1] + mtx.e22 * self[2]

  return result
end

#transformNormal!(mtx) ⇒ Object

call-seq: transformNormal!(mtx) -> self

Make itself as the transformation result of

RVec4(self.x,self.y,self.z,0.0).transform(mtx).xyz

Notice

  • mtx : RMtx4



2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
# File 'lib/rmath3d/rmath3d_plain.rb', line 2549

def transformNormal!( mtx )
  x = self[0]
  y = self[1]
  z = self[2]
  self.x = mtx.e00 * x + mtx.e01 * y + mtx.e02 * z
  self.y = mtx.e10 * x + mtx.e11 * y + mtx.e12 * z
  self.z = mtx.e20 * x + mtx.e21 * y + mtx.e22 * z

  return self
end

#transformRS(mtx) ⇒ Object

call-seq: transformRS(mtx) -> transformed RVec3

Returns the transformation result of

RVec3(self.x,self.y,self.z).transform(mtx)

Notice

  • mtx : RMtx3

  • the suffix “RS” means “matrix representing Rotational and Scaling transformation”.



2571
2572
2573
2574
2575
2576
2577
2578
# File 'lib/rmath3d/rmath3d_plain.rb', line 2571

def transformRS( mtx )
  result = RVec3.new
  result.x = mtx.e00 * self[0] + mtx.e01 * self[1] + mtx.e02 * self[2]
  result.y = mtx.e10 * self[0] + mtx.e11 * self[1] + mtx.e12 * self[2]
  result.z = mtx.e20 * self[0] + mtx.e21 * self[1] + mtx.e22 * self[2]

  return result
end

#transformRS!(mtx) ⇒ Object

call-seq: transformRS!(mtx) -> self

Makes itself as the transformation result of

RVec3(self.x,self.y,self.z).transform(mtx)

Notice

  • mtx : RMtx3

  • the suffix “RS” means “matrix representing Rotational and Scaling transformation”.



2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
# File 'lib/rmath3d/rmath3d_plain.rb', line 2591

def transformRS!( mtx )
  x = self[0]
  y = self[1]
  z = self[2]
  self.x = mtx.e00 * x + mtx.e01 * y + mtx.e02 * z
  self.y = mtx.e10 * x + mtx.e11 * y + mtx.e12 * z
  self.z = mtx.e20 * x + mtx.e21 * y + mtx.e22 * z

  return self
end

#transformRSTransposed(mtx) ⇒ Object

call-seq: transformRSTransposed(mtx) -> RVec3 transformed by mtx^T

Returns the transformation result of

RVec3(self.x,self.y,self.z).transform(mtx^T)

Notice

  • mtx : RMtx3

  • the suffix “RS” means “matrix representing Rotational and Scaling transformation”.



2613
2614
2615
2616
2617
2618
2619
2620
# File 'lib/rmath3d/rmath3d_plain.rb', line 2613

def transformRSTransposed( mtx )
  result = RVec3.new
  result.x = mtx.e00 * self[0] + mtx.e10 * self[1] + mtx.e20 * self[2]
  result.y = mtx.e01 * self[0] + mtx.e11 * self[1] + mtx.e21 * self[2]
  result.z = mtx.e02 * self[0] + mtx.e12 * self[1] + mtx.e22 * self[2]

  return result
end

#transformRSTransposed!(mtx) ⇒ Object

call-seq: transformRSTransposed!(mtx) -> self

Makes itself as the transformation result of

RVec3(self.x,self.y,self.z).transform(mtx^T)

Notice

  • mtx : RMtx3

  • the suffix “RS” means “matrix representing Rotational and Scaling transformation”.



2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
# File 'lib/rmath3d/rmath3d_plain.rb', line 2633

def transformRSTransposed!( mtx )
  x = self[0]
  y = self[1]
  z = self[2]
  self.x = mtx.e00 * x + mtx.e10 * y + mtx.e20 * z
  self.y = mtx.e01 * x + mtx.e11 * y + mtx.e21 * z
  self.z = mtx.e02 * x + mtx.e12 * y + mtx.e22 * z

  return self
end

#xObject

call-seq: x -> value

Returns the value of x.



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

def x() return @e[0] end

#x=(value) ⇒ Object

call-seq: x= value

Stores value as x.



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

def x=(value) @e[0] = value end

#yObject

call-seq: y -> value

Returns the value of y.



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

def y() return @e[1] end

#y=(value) ⇒ Object

call-seq: y= value

Stores value as y.



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

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

#zObject

call-seq: z -> value

Returns the value of z.



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

def z() return @e[2] end

#z=(value) ⇒ Object

call-seq: z= value

Stores value as z.



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

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