Class: RMath3D::RMtx4

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

Overview

Document-class: RMath3D::RMtx4 provies 4x4 matrix arithmetic.

Notice

  • elements are stored in column-major order.

Instance Method Summary collapse

Constructor Details

#initialize(*a) ⇒ RMtx4

call-seq:

RMtx4.new -> ((1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1))
RMtx4.new(e) -> ((e,e,e,e),(e,e,e,e),(e,e,e,e),(e,e,e,e))
RMtx4.new( other ) : Copy Constructor
RMtx4.new( e0, e1, ..., e15 ) -> ((e0,e1,e2,e3),(e4,e5,e6,e7),(e8,e9,e10,e11),(e12,e13,e14,e15))

Creates a new 4x4 matrix.



722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/rmath3d/rmath3d_plain.rb', line 722

def initialize( *a )
  # [NOTE] elemetns are stored in column-major order.
  @e = []
  case a.length
  when 0
    @e = [ 0.0, 0.0, 0.0, 0.0,
           0.0, 0.0, 0.0, 0.0,
           0.0, 0.0, 0.0, 0.0,
           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],
             a[0], a[0], a[0], a[0],
             a[0], a[0], a[0], a[0],
             a[0], a[0], a[0], a[0] ]
    when RMtx4
      # Copy Constructor
      @e = [ a[0].e00, a[0].e10, a[0].e20, a[0].e30,
             a[0].e01, a[0].e11, a[0].e21, a[0].e31,
             a[0].e02, a[0].e12, a[0].e22, a[0].e32,
             a[0].e03, a[0].e13, a[0].e23, a[0].e33 ]
    else
      raise TypeError, "RMtx4#initialize : Unknown type #{a[0].class}."
      return nil
    end
  when 16
    # Element-wise setter
    for row in 0...4 do
      for col in 0...4 do
        index = 4*row + col
        case a[index]
        when Fixnum, Float
          setElement( row, col, a[index] )
        else
          raise TypeError, "RMtx4#initialize : Unknown type #{a[0].class}."
          return nil
        end
      end
    end
  else
    raise RuntimeError, "RMtx4#initialize : wrong # of arguments (#{a.length})"
    return nil
  end

  return self
end

Instance Method Details

#*(arg) ⇒ Object

call-seq: *

mtx1 * mtx2 : Binary multiply operator.



1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
# File 'lib/rmath3d/rmath3d_plain.rb', line 1520

def *( arg )
  case arg
  when Fixnum, Float, Bignum
    return RMtx4.new( arg*self.e00, arg*self.e01, arg*self.e02, arg*self.e03,
                      arg*self.e10, arg*self.e11, arg*self.e12, arg*self.e13,
                      arg*self.e20, arg*self.e21, arg*self.e22, arg*self.e23,
                      arg*self.e30, arg*self.e31, arg*self.e32, arg*self.e33 )

  when RMtx4
    result = RMtx4.new
    for row in 0...4 do
      for col in 0...4 do
        sum = 0.0
        for i in 0...4 do
          sum += getElement( row, i ) * arg.getElement( i, col )
        end
        result.setElement( row, col, sum )
      end
    end
    return result

  else
    raise TypeError, "RMtx4#*(arg) : Unknown type #{arg.class} given."
    return nil
  end
end

#+(arg) ⇒ Object

call-seq: +

mtx1 + mtx2 : Binary plus operator.



1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
# File 'lib/rmath3d/rmath3d_plain.rb', line 1478

def +( arg )
  if ( arg.class != RMtx4 )
    raise TypeError, "RMtx4#+(arg) : Unknown type #{arg.class} given as RMtx4."
    return nil
  end

  result = RMtx4.new
  for row in 0...4 do
    for col in 0...4 do
      result.setElement( row, col, getElement(row,col) + arg.getElement(row,col) )
    end
  end

  return result
end

#+@Object

call-seq: +

+mtx : Unary plus operator.



1460
1461
1462
# File 'lib/rmath3d/rmath3d_plain.rb', line 1460

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

mtx1 - mtx2 : Binary minus operator.



1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
# File 'lib/rmath3d/rmath3d_plain.rb', line 1499

def -( arg )
  if ( arg.class != RMtx4 )
    raise TypeError, "RMtx4#-(arg) : Unknown type #{arg.class} given as RMtx4."
    return nil
  end

  result = RMtx4.new
  for row in 0...4 do
    for col in 0...4 do
      result.setElement( row, col, getElement(row,col) - arg.getElement(row,col) )
    end
  end

  return result
end

#-@Object

call-seq: -

-mtx : Unary minus operator.



1469
1470
1471
# File 'lib/rmath3d/rmath3d_plain.rb', line 1469

def -@
  return RMtx4.new( self * -1.0 )
end

#==(other) ⇒ Object

call-seq: ==

mtx1 == mtx2 : evaluates equality.



1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
# File 'lib/rmath3d/rmath3d_plain.rb', line 1552

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

  for row in 0...4 do
    for col in 0...4 do
      if ( (getElement(row,col) - other.getElement(row,col)).abs > TOLERANCE )
        return false
      end
    end
  end
  return true
end

#[](row, col) ⇒ Object Also known as: getElement

call-seq: [row,col] -> value

Returns the element at (row,col).



841
842
843
844
# File 'lib/rmath3d/rmath3d_plain.rb', line 841

def [](row,col)
  # [NOTE] elemetns are stored in column-major order.
  return @e[col*4+row]
end

#[]=(row, col, value) ⇒ Object Also known as: setElement

call-seq: [row,col]= value

Stores value at (row,col).



830
831
832
833
# File 'lib/rmath3d/rmath3d_plain.rb', line 830

def []=(row,col,value)
  # [NOTE] elemetns are stored in column-major order.
  @e[col*4+row] = value
end

#add!(other) ⇒ Object

call-seq: mtx1.add!( mtx2 )

mtx1 += mtx2 : appends the elements of mtx2 into corresponding mtx1 elements.



1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
# File 'lib/rmath3d/rmath3d_plain.rb', line 1573

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

  result = RMtx4.new
  for row in 0...4 do
    for col in 0...4 do
      self.setElement( row, col, getElement(row,col) + other.getElement(row,col) )
    end
  end

  return self
end

#coerceObject

call-seq: coerse(other)

Resolves type mismatch.



796
797
798
799
800
801
802
803
804
# File 'lib/rmath3d/rmath3d_plain.rb', line 796

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

#e00Object

Returns the element at row 0 and column 0.



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

def e00() getElement(0,0) end

#e00=(value) ⇒ Object

Replaces the element at row 0 and column 0 by value.



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

def e00=(value) setElement(0,0,value) end

#e01Object

Returns the element at row 0 and column 1.



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

def e01() getElement(0,1) end

#e01=(value) ⇒ Object

Replaces the element at row 0 and column 1 by value.



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

def e01=(value) setElement(0,1,value) end

#e02Object

Returns the element at row 0 and column 2.



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

def e02() getElement(0,2) end

#e02=(value) ⇒ Object

Replaces the element at row 0 and column 2 by value.



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

def e02=(value) setElement(0,2,value) end

#e03Object

Returns the element at row 0 and column 3.



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

def e03() getElement(0,3) end

#e03=(value) ⇒ Object

Replaces the element at row 0 and column 3 by value.



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

def e03=(value) setElement(0,3,value) end

#e10Object

Returns the element at row 1 and column 0.



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

def e10() getElement(1,0) end

#e10=(value) ⇒ Object

Replaces the element at row 1 and column 0 by value.



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

def e10=(value) setElement(1,0,value) end

#e11Object

Returns the element at row 1 and column 1.



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

def e11() getElement(1,1) end

#e11=(value) ⇒ Object

Replaces the element at row 1 and column 1 by value.



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

def e11=(value) setElement(1,1,value) end

#e12Object

Returns the element at row 1 and column 2.



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

def e12() getElement(1,2) end

#e12=(value) ⇒ Object

Replaces the element at row 1 and column 2 by value.



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

def e12=(value) setElement(1,2,value) end

#e13Object

Returns the element at row 1 and column 3.



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

def e13() getElement(1,3) end

#e13=(value) ⇒ Object

Replaces the element at row 1 and column 3 by value.



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

def e13=(value) setElement(1,3,value) end

#e20Object

Returns the element at row 2 and column 0.



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

def e20() getElement(2,0) end

#e20=(value) ⇒ Object

Replaces the element at row 2 and column 0 by value.



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

def e20=(value) setElement(2,0,value) end

#e21Object

Returns the element at row 2 and column 1.



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

def e21() getElement(2,1) end

#e21=(value) ⇒ Object

Replaces the element at row 2 and column 1 by value.



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

def e21=(value) setElement(2,1,value) end

#e22Object

Returns the element at row 2 and column 2.



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

def e22() getElement(2,2) end

#e22=(value) ⇒ Object

Replaces the element at row 2 and column 2 by value.



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

def e22=(value) setElement(2,2,value) end

#e23Object

Returns the element at row 2 and column 3.



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

def e23() getElement(2,3) end

#e23=(value) ⇒ Object

Replaces the element at row 2 and column 3 by value.



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

def e23=(value) setElement(2,3,value) end

#e30Object

Returns the element at row 3 and column 0.



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

def e30() getElement(3,0) end

#e30=(value) ⇒ Object

Replaces the element at row 3 and column 0 by value.



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

def e30=(value) setElement(3,0,value) end

#e31Object

Returns the element at row 3 and column 1.



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

def e31() getElement(3,1) end

#e31=(value) ⇒ Object

Replaces the element at row 3 and column 1 by value.



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

def e31=(value) setElement(3,1,value) end

#e32Object

Returns the element at row 3 and column 2.



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

def e32() getElement(3,2) end

#e32=(value) ⇒ Object

Replaces the element at row 3 and column 2 by value.



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

def e32=(value) setElement(3,2,value) end

#e33Object

Returns the element at row 3 and column 3.



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

def e33() getElement(3,3) end

#e33=(value) ⇒ Object

Replaces the element at row 3 and column 3 by value.



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

def e33=(value) setElement(3,3,value) end

#getColumn(column) ⇒ Object

call-seq: mtx4.getColumn© -> RVec4

Returns c-th column vector.



927
928
929
# File 'lib/rmath3d/rmath3d_plain.rb', line 927

def getColumn( column )
  return RVec4.new( self[0,column], self[1,column], self[2,column], self[3,column] )
end

#getDeterminantObject

call-seq: getDeterminant -> determinant

Calculates determinant.



1017
1018
1019
1020
1021
1022
# File 'lib/rmath3d/rmath3d_plain.rb', line 1017

def getDeterminant
  e00 * det3( e11,e12,e13, e21,e22,e23, e31,e32,e33 ) -
  e01 * det3( e10,e12,e13, e20,e22,e23, e30,e32,e33 ) +
  e02 * det3( e10,e11,e13, e20,e21,e23, e30,e31,e33 ) -
  e03 * det3( e10,e11,e12, e20,e21,e22, e30,e31,e32 )
end

#getInverseObject

call-seq: getInverse -> inverse

Returns the inverse.



1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
# File 'lib/rmath3d/rmath3d_plain.rb', line 1055

def getInverse
  result = RMtx4.new

  result.e00 =  det3( e11,e12,e13, e21,e22,e23, e31,e32,e33 )
  result.e01 = -det3( e01,e02,e03, e21,e22,e23, e31,e32,e33 )
  result.e02 =  det3( e01,e02,e03, e11,e12,e13, e31,e32,e33 )
  result.e03 = -det3( e01,e02,e03, e11,e12,e13, e21,e22,e23 )

  result.e10 = -det3( e10,e12,e13, e20,e22,e23, e30,e32,e33 )
  result.e11 =  det3( e00,e02,e03, e20,e22,e23, e30,e32,e33 )
  result.e12 = -det3( e00,e02,e03, e10,e12,e13, e30,e32,e33 )
  result.e13 =  det3( e00,e02,e03, e10,e12,e13, e20,e22,e23 )

  result.e20 =  det3( e10,e11,e13, e20,e21,e23, e30,e31,e33 )
  result.e21 = -det3( e00,e01,e03, e20,e21,e23, e30,e31,e33 )
  result.e22 =  det3( e00,e01,e03, e10,e11,e13, e30,e31,e33 )
  result.e23 = -det3( e00,e01,e03, e10,e11,e13, e20,e21,e23 )

  result.e30 = -det3( e10,e11,e12, e20,e21,e22, e30,e31,e32 )
  result.e31 =  det3( e00,e01,e02, e20,e21,e22, e30,e31,e32 )
  result.e32 = -det3( e00,e01,e02, e10,e11,e12, e30,e31,e32 )
  result.e33 =  det3( e00,e01,e02, e10,e11,e12, e20,e21,e22 )

  det = e00 * result.e00 + e01 * result.e10 + e02 * result.e20 + e03 * result.e30

  if ( det.abs < TOLERANCE )
    raise RuntimeError, "RMtx4#getInverse : det.abs < TOLERANCE"
    return nil
  end

  d = 1.0 / det

  result.mul!( d )

  return result
end

#getRow(row) ⇒ Object

call-seq: mtx4.getRow® -> RVec4

Returns r-th row vector.



918
919
920
# File 'lib/rmath3d/rmath3d_plain.rb', line 918

def getRow( row )
  return RVec4.new( self[row,0], self[row,1], self[row,2], self[row,3] )
end

#getTransposedObject

call-seq: getTransposed

Returns transposed matrix.



1029
1030
1031
1032
1033
1034
# File 'lib/rmath3d/rmath3d_plain.rb', line 1029

def getTransposed
  return RMtx4.new( @e[ 0], @e[ 1], @e[ 2], @e[ 3],
                    @e[ 4], @e[ 5], @e[ 6], @e[ 7],
                    @e[ 8], @e[ 9], @e[10], @e[11],
                    @e[12], @e[13], @e[14], @e[15] )
end

#getUpper3x3Object



955
956
957
958
959
# File 'lib/rmath3d/rmath3d_plain.rb', line 955

def getUpper3x3
  return RMtx3.new( self.e00, self.e01, self.e02,
                    self.e10, self.e11, self.e12,
                    self.e20, self.e21, self.e22 )
end

#invert!Object

call-seq: invert! -> self

Makes itself as the inverse of the original matrix.



1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
# File 'lib/rmath3d/rmath3d_plain.rb', line 1097

def invert!
  elements = Array.new( 16 )

  elements[4*0+0] =  det3( self.e11,self.e12,self.e13, self.e21,self.e22,self.e23, self.e31,self.e32,self.e33 )
  elements[4*0+1] = -det3( self.e01,self.e02,self.e03, self.e21,self.e22,self.e23, self.e31,self.e32,self.e33 )
  elements[4*0+2] =  det3( self.e01,self.e02,self.e03, self.e11,self.e12,self.e13, self.e31,self.e32,self.e33 )
  elements[4*0+3] = -det3( self.e01,self.e02,self.e03, self.e11,self.e12,self.e13, self.e21,self.e22,self.e23 )

  elements[4*1+0] = -det3( self.e10,self.e12,self.e13, self.e20,self.e22,self.e23, self.e30,self.e32,self.e33 )
  elements[4*1+1] =  det3( self.e00,self.e02,self.e03, self.e20,self.e22,self.e23, self.e30,self.e32,self.e33 )
  elements[4*1+2] = -det3( self.e00,self.e02,self.e03, self.e10,self.e12,self.e13, self.e30,self.e32,self.e33 )
  elements[4*1+3] =  det3( self.e00,self.e02,self.e03, self.e10,self.e12,self.e13, self.e20,self.e22,self.e23 )

  elements[4*2+0] =  det3( self.e10,self.e11,self.e13, self.e20,self.e21,self.e23, self.e30,self.e31,self.e33 )
  elements[4*2+1] = -det3( self.e00,self.e01,self.e03, self.e20,self.e21,self.e23, self.e30,self.e31,self.e33 )
  elements[4*2+2] =  det3( self.e00,self.e01,self.e03, self.e10,self.e11,self.e13, self.e30,self.e31,self.e33 )
  elements[4*2+3] = -det3( self.e00,self.e01,self.e03, self.e10,self.e11,self.e13, self.e20,self.e21,self.e23 )

  elements[4*3+0] = -det3( self.e10,self.e11,self.e12, self.e20,self.e21,self.e22, self.e30,self.e31,self.e32 )
  elements[4*3+1] =  det3( self.e00,self.e01,self.e02, self.e20,self.e21,self.e22, self.e30,self.e31,self.e32 )
  elements[4*3+2] = -det3( self.e00,self.e01,self.e02, self.e10,self.e11,self.e12, self.e30,self.e31,self.e32 )
  elements[4*3+3] =  det3( self.e00,self.e01,self.e02, self.e10,self.e11,self.e12, self.e20,self.e21,self.e22 )

  det = e00 * elements[4*0+0] + e01 * elements[4*1+0] + e02 * elements[4*2+0] + e03 * elements[4*3+0]

  if ( det.abs< TOLERANCE )
    raise RuntimeError, "RMtx4invert! : det.abs < TOLERANCE"
    return nil
  end

  d = 1.0 / det

  setElement( 0, 0, d * elements[4*0+0] )
  setElement( 0, 1, d * elements[4*0+1] )
  setElement( 0, 2, d * elements[4*0+2] )
  setElement( 0, 3, d * elements[4*0+3] )

  setElement( 1, 0, d * elements[4*1+0] )
  setElement( 1, 1, d * elements[4*1+1] )
  setElement( 1, 2, d * elements[4*1+2] )
  setElement( 1, 3, d * elements[4*1+3] )

  setElement( 2, 0, d * elements[4*2+0] )
  setElement( 2, 1, d * elements[4*2+1] )
  setElement( 2, 2, d * elements[4*2+2] )
  setElement( 2, 3, d * elements[4*2+3] )

  setElement( 3, 0, d * elements[4*3+0] )
  setElement( 3, 1, d * elements[4*3+1] )
  setElement( 3, 2, d * elements[4*3+2] )
  setElement( 3, 3, d * elements[4*3+3] )

  return self
end

#lookAtRH(eye, at, up) ⇒ Object

call-seq: lookAtRH(eye,at,up) -> self

Builds a viewing matrix for a right-handed coordinate system from:

  • eye position (eye: RVec3)

  • a point looking at (at: RVec3)

  • up vector (up: RVec3)



1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
# File 'lib/rmath3d/rmath3d_plain.rb', line 1319

def lookAtRH( eye, at, up )
  setIdentity()

  axis_z = (eye - at).normalize!
  axis_x = RVec3.cross( up, axis_z ).normalize!
  axis_y = RVec3.cross( axis_z, axis_x )

  self.e00 = axis_x[0]
  self.e01 = axis_x[1]
  self.e02 = axis_x[2]
  self.e03 = -RVec3.dot( axis_x, eye )

  self.e10 = axis_y[0]
  self.e11 = axis_y[1]
  self.e12 = axis_y[2]
  self.e13 = -RVec3.dot( axis_y, eye )

  self.e20 = axis_z[0]
  self.e21 = axis_z[1]
  self.e22 = axis_z[2]
  self.e23 = -RVec3.dot( axis_z, eye )

  return self
end

#mul!(other) ⇒ Object

call-seq: mtx1.mul!( mtx2 )

mtx1 *= mtx2



1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
# File 'lib/rmath3d/rmath3d_plain.rb', line 1615

def mul!( other )
  case other
  when Fixnum, Float, Bignum
    self.e00 = other*self.e00
    self.e01 = other*self.e01
    self.e02 = other*self.e02
    self.e03 = other*self.e03

    self.e10 = other*self.e10
    self.e11 = other*self.e11
    self.e12 = other*self.e12
    self.e13 = other*self.e13

    self.e20 = other*self.e20
    self.e21 = other*self.e21
    self.e22 = other*self.e22
    self.e23 = other*self.e23

    self.e30 = other*self.e30
    self.e31 = other*self.e31
    self.e32 = other*self.e32
    self.e33 = other*self.e33

    return self

  when RMtx4
    result = RMtx4.new
    for row in 0...4 do
      for col in 0...4 do
        sum = 0.0
        for i in 0...4 do
          sum += getElement( row, i ) * other.getElement( i, col )
        end
        result.setElement( row, col, sum )
      end
    end

    self.e00 = result.e00
    self.e01 = result.e01
    self.e02 = result.e02
    self.e03 = result.e03

    self.e10 = result.e10
    self.e11 = result.e11
    self.e12 = result.e12
    self.e13 = result.e13

    self.e20 = result.e20
    self.e21 = result.e21
    self.e22 = result.e22
    self.e23 = result.e23

    self.e30 = result.e30
    self.e31 = result.e31
    self.e32 = result.e32
    self.e33 = result.e33

    return self
  end
end

#orthoOffCenterRH(left, right, bottom, top, znear, zfar) ⇒ Object

call-seq: orthoOffCenterRH(left,right,bottom,top,znear,zfar) -> self

Builds a orthogonal projection matrix for a right-handed coordinate system from:

  • Minimum value of the view volume width (left)

  • Maximum value of the view volume width (right)

  • Minimum value of the view volume height (bottom)

  • Maximum value of the view volume height (top)

  • Near clip plane distance (znear)

  • Far clip plane distance (zfar)



1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
# File 'lib/rmath3d/rmath3d_plain.rb', line 1438

def orthoOffCenterRH( left, right, bottom, top, znear, zfar )
  tx = (right+left) / (right-left)
  ty = (top+bottom) / (top-bottom)
  tz = (zfar+znear) / (zfar-znear)

  setIdentity()

  setElement( 0, 0, 2.0/(right-left) )
  setElement( 0, 3, tx )
  setElement( 1, 1, 2.0/(top-bottom) )
  setElement( 1, 3, ty )
  setElement( 2, 2, -2.0/(zfar-znear) )
  setElement( 2, 3, tz )

  return self
end

#orthoRH(width, height, znear, zfar) ⇒ Object

call-seq: orthoRH(width,height,znear,zfar) -> self

Builds a orthogonal projection matrix for a right-handed coordinate system from:

  • View volume width (width)

  • View volume height (height)

  • Near clip plane distance (znear)

  • Far clip plane distance (zfar)



1422
1423
1424
1425
# File 'lib/rmath3d/rmath3d_plain.rb', line 1422

def orthoRH( width, height, znear, zfar )
  orthoOffCenterRH( -width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar )
  return self
end

#perspectiveFovRH(fovy_radian, aspect, znear, zfar) ⇒ Object

call-seq: perspectiveFovRH(fovy,aspect,znear,zfar) -> self

Builds a perspective projection matrix for a right-handed coordinate system from:

  • Field of view in y direction (fovy radian)

  • Aspect ratio (aspect)

  • Near clip plane distance (znear)

  • Far clip plane distance (zfar)



1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
# File 'lib/rmath3d/rmath3d_plain.rb', line 1367

def perspectiveFovRH( fovy_radian, aspect, znear, zfar )
  f = Math::tan( fovy_radian / 2.0 )
  f = 1.0 / f

  setIdentity()
  setElement( 0, 0, f / aspect )
  setElement( 1, 1, f )
  setElement( 2, 2, (zfar+znear)/(znear-zfar) )
  setElement( 2, 3, 2*zfar*znear/(znear-zfar) )
  setElement( 3, 2, -1.0 )
  setElement( 3, 3, 0.0 )

  return self
end

#perspectiveOffCenterRH(left, right, bottom, top, znear, zfar) ⇒ Object

call-seq: perspectiveOffCenterRH(left,right,bottom,top,znear,zfar) -> self

Builds a perspective projection matrix for a right-handed coordinate system from:

  • Minimum value of the view volume width (left)

  • Maximum value of the view volume width (right)

  • Minimum value of the view volume height (bottom)

  • Maximum value of the view volume height (top)

  • Near clip plane distance (znear)

  • Far clip plane distance (zfar)



1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
# File 'lib/rmath3d/rmath3d_plain.rb', line 1393

def perspectiveOffCenterRH( left, right, bottom, top, znear, zfar )
  a = (right+left) / (right-left)
  b = (top+bottom) / (top-bottom)
  c = -(zfar+znear) / (zfar-znear)
  d = -(2*znear*zfar) / (zfar-znear)

  setIdentity()

  setElement( 0, 0, 2*znear/(right-left) )
  setElement( 0, 2, a )
  setElement( 1, 1, 2*znear/(top-bottom) )
  setElement( 1, 2, b )
  setElement( 2, 2, c )
  setElement( 2, 3, d )
  setElement( 3, 2, -1.0 )
  setElement( 3, 3,  0.0 )

  return self
end

#perspectiveRH(width, height, znear, zfar) ⇒ Object

call-seq: perspectiveRH(width,height,znear,zfar) -> self

Builds a perspective projection matrix for a right-handed coordinate system from:

  • View volume width (width)

  • View volume height (height)

  • Near clip plane distance (znear)

  • Far clip plane distance (zfar)



1353
1354
1355
1356
# File 'lib/rmath3d/rmath3d_plain.rb', line 1353

def perspectiveRH( width, height, znear, zfar )
  perspectiveOffCenterRH(-width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar )
  return self
end

#rotationAxis(axis, radian) ⇒ Object

call-seq: rotationAxis(axis,radian) -> self

Makes a matrix that rotates around the axis.



1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
# File 'lib/rmath3d/rmath3d_plain.rb', line 1225

def rotationAxis( axis, radian )
  if ( axis.class != RVec3 )
    raise TypeError, "RMtx4#rotationAxis : Unknown type #{axis.class} given as axis."
    return nil
  end
  s   = Math.sin( radian )
  c   = Math.cos( radian )
  omc = 1.0 - c
  x   = axis.x.to_f
  y   = axis.y.to_f
  z   = axis.z.to_f

  setIdentity()

  self.e00 = x*x*omc + c
  self.e01 = x*y*omc - z*s
  self.e02 = z*x*omc + y*s
  self.e10 = x*y*omc + z*s
  self.e11 = y*y*omc + c
  self.e12 = y*z*omc - x*s
  self.e20 = z*x*omc - y*s
  self.e21 = y*z*omc + x*s
  self.e22 = z*z*omc + c

  return self
end

#rotationQuaternion(q) ⇒ Object

call-seq: rotationQuaternion(q) -> self

Makes a rotation matrix from a normalized quaternion q.



1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
# File 'lib/rmath3d/rmath3d_plain.rb', line 1257

def rotationQuaternion( q )
  if ( q.class != RQuat )
    raise TypeError, "RMtx4#rotationQuaternion : Unknown type #{q.class} given as RQuat."
    return nil
  end
  x  = q.x
  y  = q.y
  z  = q.z
  w  = q.w

  x2 = 2.0 * x
  y2 = 2.0 * y
  z2 = 2.0 * z

  xx2 = x * x2
  yy2 = y * y2
  zz2 = z * z2

  yz2 = y * z2
  wx2 = w * x2
  xy2 = x * y2
  wz2 = w * z2
  xz2 = x * z2
  wy2 = w * y2

  setIdentity()

  self.e00 = 1.0 - yy2 - zz2
  self.e10 = xy2 + wz2
  self.e20 = xz2 - wy2
  self.e01 = xy2 - wz2
  self.e11 = 1.0 - xx2 - zz2
  self.e21 = yz2 + wx2
  self.e02 = xz2 + wy2
  self.e12 = yz2 - wx2
  self.e22 = 1.0 - xx2 - yy2

  return self
end

#rotationX(radian) ⇒ Object

call-seq: rotationX(radian) -> self

Makes a matrix that rotates around the x-axis.



1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
# File 'lib/rmath3d/rmath3d_plain.rb', line 1171

def rotationX( radian )
  s = Math.sin( radian )
  c = Math.cos( radian )

  setIdentity()
  self.e11 =  c
  self.e12 = -s
  self.e21 =  s
  self.e22 =  c

  return self
end

#rotationY(radian) ⇒ Object

call-seq: rotationY(radian) -> self

Makes a matrix that rotates around the y-axis.



1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
# File 'lib/rmath3d/rmath3d_plain.rb', line 1189

def rotationY( radian )
  s = Math.sin( radian )
  c = Math.cos( radian )

  setIdentity()
  self.e00 =  c
  self.e02 =  s
  self.e20 = -s
  self.e22 =  c

  return self
end

#rotationZ(radian) ⇒ Object

call-seq: rotationZ(radian) -> self

Makes a matrix that rotates around the z-axis.



1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
# File 'lib/rmath3d/rmath3d_plain.rb', line 1207

def rotationZ( radian )
  s = Math.sin( radian )
  c = Math.cos( radian )

  setIdentity()
  self.e00 =  c
  self.e01 = -s
  self.e10 =  s
  self.e11 =  c

  return self
end

#scaling(sx, sy, sz) ⇒ Object

call-seq: scaling(sx,sy,sz) -> self

Makes itself as a scaling matrix.



1302
1303
1304
1305
1306
1307
1308
1309
# File 'lib/rmath3d/rmath3d_plain.rb', line 1302

def scaling( sx, sy, sz )
  setIdentity()
  setElement( 0, 0, sx )
  setElement( 1, 1, sy )
  setElement( 2, 2, sz )

  return self
end

#setColumn(v, column) ⇒ Object

call-seq: mtx4.setColumn(v,c)

Returns sets c-th column by vector v.



948
949
950
951
952
953
# File 'lib/rmath3d/rmath3d_plain.rb', line 948

def setColumn( v, column )
  self[0,column] = v.x
  self[1,column] = v.y
  self[2,column] = v.z
  self[3,column] = v.w
end

#setElements(*a) ⇒ Object

call-seq: setElements( e0, e1, …, e15 )

Stores given 16 new values.



811
812
813
814
815
816
817
818
819
820
821
822
823
# File 'lib/rmath3d/rmath3d_plain.rb', line 811

def setElements( *a )
  if a.length != 16
    raise RuntimeError, "RMtx4#setElements : wrong # of arguments (#{a.length})"
    return nil
  end

  for row in 0...4 do
    for col in 0...4 do
      index = 4*row + col
      setElement( row, col, a[index] )
    end
  end
end

#setIdentityObject

call-seq: setIdentity

Sets as identity matrix.



991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/rmath3d/rmath3d_plain.rb', line 991

def setIdentity
  for row in 0...4 do
    for col in 0...4 do
      index = 4*row + col
      if ( row == col )
        setElement( row, col, 1.0 )
      else
        setElement( row, col, 0.0 )
      end
    end
  end
  return self
end

#setRow(v, row) ⇒ Object

call-seq: mtx4.setRow(v,r)

Returns sets r-th row by vector v.



936
937
938
939
940
941
# File 'lib/rmath3d/rmath3d_plain.rb', line 936

def setRow( v, row )
  self[row,0] = v.x
  self[row,1] = v.y
  self[row,2] = v.z
  self[row,3] = v.w
end

#setUpper3x3(mtx3x3) ⇒ Object



961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'lib/rmath3d/rmath3d_plain.rb', line 961

def setUpper3x3( mtx3x3 )
  self.e00 = mtx3x3.e00
  self.e01 = mtx3x3.e01
  self.e02 = mtx3x3.e02
  self.e10 = mtx3x3.e10
  self.e11 = mtx3x3.e11
  self.e12 = mtx3x3.e12
  self.e20 = mtx3x3.e20
  self.e21 = mtx3x3.e21
  self.e22 = mtx3x3.e22

  return self
end

#setZeroObject

call-seq: setZero

Clears all elements by 0.0



979
980
981
982
983
984
# File 'lib/rmath3d/rmath3d_plain.rb', line 979

def setZero
  16.times do |i|
    @e[i] = 0.0
  end
  return self
end

#sub!(other) ⇒ Object

call-seq: mtx1.sub!( mtx2 )

mtx1 -= mtx2 : subtracts the elements of mtx2 from corresponding mtx1 elements.



1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
# File 'lib/rmath3d/rmath3d_plain.rb', line 1594

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

  result = RMtx4.new
  for row in 0...4 do
    for col in 0...4 do
      self.setElement( row, col, getElement(row,col) - other.getElement(row,col) )
    end
  end

  return self
end

#to_aObject

call-seq: to_a

Returns its elements as a new Array.



787
788
789
# File 'lib/rmath3d/rmath3d_plain.rb', line 787

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



775
776
777
778
779
780
# File 'lib/rmath3d/rmath3d_plain.rb', line 775

def to_s
  "( #{@e[0]}, #{@e[4]}, #{@e[8]}, #{@e[12]} )\n"  +
  "( #{@e[1]}, #{@e[5]}, #{@e[9]}, #{@e[13]} )\n"  +
  "( #{@e[2]}, #{@e[6]}, #{@e[10]}, #{@e[14]} )\n" +
  "( #{@e[3]}, #{@e[7]}, #{@e[11]}, #{@e[15]} )\n"
end

#translation(tx, ty, tz) ⇒ Object

call-seq: translation(tx,ty,tz) -> self

Makes itself as a translation matrix.



1157
1158
1159
1160
1161
1162
1163
1164
# File 'lib/rmath3d/rmath3d_plain.rb', line 1157

def translation( tx, ty, tz )
  setIdentity()
  self.e03 =  tx
  self.e13 =  ty
  self.e23 =  tz

  return self
end

#transpose!Object

call-seq: transpose!

Transposeas its elements.



1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/rmath3d/rmath3d_plain.rb', line 1041

def transpose!
  @e[ 1], @e[ 4] = @e[ 4], @e[ 1]
  @e[ 2], @e[ 8] = @e[ 8], @e[ 2]
  @e[ 3], @e[12] = @e[12], @e[ 3]
  @e[ 6], @e[ 9] = @e[ 9], @e[ 6]
  @e[ 7], @e[13] = @e[13], @e[ 7]
  @e[11], @e[14] = @e[14], @e[11]
end