Class: RMath3D::RMtx3

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

Overview

Document-class: RMath3D::RMtx3 provies 3x3 matrix arithmetic.

Notice

  • elements are stored in column-major order.

Instance Method Summary collapse

Constructor Details

#initialize(*a) ⇒ RMtx3

call-seq:

RMtx3.new -> ((1,0,0),(0,1,0),(0,0,1))
RMtx3.new(e) -> ((e,e,e), (e,e,e), (e,e,e))
RMtx3.new( other ) : Copy Constructor
RMtx3.new( e0, e1, ..., e8 ) -> ((e0,e1,e2), (e3,e4,e5), (e6,e7,e8))

Creates a new 3x3 matrix.



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/rmath3d/rmath3d_plain.rb', line 553

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 ]
  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] ]
    when RMtx3
      # Copy Constructor
      @e = [ a[0].e00, a[0].e10, a[0].e20,
             a[0].e01, a[0].e11, a[0].e21,
             a[0].e02, a[0].e12, a[0].e22 ]
    else
      raise TypeError, "RMtx3#initialize : Unknown type #{a[0].class}."
      return nil
    end
  when 9
    # Element-wise setter
    for row in 0...3 do
      for col in 0...3 do
        index = 3*row + col
        case a[index]
        when Fixnum, Float
          setElement( row, col, a[index] )
        else
          raise TypeError, "RMtx3#initialize : Unknown type #{a[0].class}."
          return nil
        end
      end
    end
  else
    raise RuntimeError, "RMtx3#initialize : wrong # of arguments (#{a.length})"
    return nil
  end

  return self
end

Instance Method Details

#*(arg) ⇒ Object

call-seq: *

mtx1 * mtx2 : Binary multiply operator.



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
# File 'lib/rmath3d/rmath3d_plain.rb', line 1099

def *( arg )
  case arg
  when Fixnum, Float, Bignum
    return RMtx3.new( arg*self.e00, arg*self.e01, arg*self.e02,
                      arg*self.e10, arg*self.e11, arg*self.e12,
                      arg*self.e20, arg*self.e21, arg*self.e22 )

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

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

#+(arg) ⇒ Object

call-seq: +

mtx1 + mtx2 : Binary plus operator.



1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
# File 'lib/rmath3d/rmath3d_plain.rb', line 1057

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

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

  return result
end

#+@Object

call-seq: +

+mtx : Unary plus operator.



1039
1040
1041
# File 'lib/rmath3d/rmath3d_plain.rb', line 1039

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

mtx1 - mtx2 : Binary minus operator.



1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
# File 'lib/rmath3d/rmath3d_plain.rb', line 1078

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

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

  return result
end

#-@Object

call-seq: -

-mtx : Unary minus operator.



1048
1049
1050
# File 'lib/rmath3d/rmath3d_plain.rb', line 1048

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

#==(other) ⇒ Object

call-seq: ==

mtx1 == mtx2 : evaluates equality.



1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
# File 'lib/rmath3d/rmath3d_plain.rb', line 1130

def ==( other )
  if other.class == RMtx3
    for row in 0...3 do
      for col in 0...3 do
        if ( (getElement(row,col) - other.getElement(row,col)).abs > TOLERANCE )
          return false
        end
      end
    end
    return true
  else
    return false
  end
end

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

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

Returns the element at (row,col).



668
669
670
671
# File 'lib/rmath3d/rmath3d_plain.rb', line 668

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

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

call-seq: [row,col]= value

Stores value at (row,col).



657
658
659
660
# File 'lib/rmath3d/rmath3d_plain.rb', line 657

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

#add!(other) ⇒ Object

call-seq: mtx1.add!( mtx2 )

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



1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
# File 'lib/rmath3d/rmath3d_plain.rb', line 1150

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

  result = RMtx3.new
  for row in 0...3 do
    for col in 0...3 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.



623
624
625
626
627
628
629
630
631
# File 'lib/rmath3d/rmath3d_plain.rb', line 623

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

#e00Object

Returns the element at row 0 and column 0.



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

def e00() getElement(0,0) end

#e00=(value) ⇒ Object

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



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

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

#e01Object

Returns the element at row 0 and column 1.



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

def e01() getElement(0,1) end

#e01=(value) ⇒ Object

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



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

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

#e02Object

Returns the element at row 0 and column 2.



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

def e02() getElement(0,2) end

#e02=(value) ⇒ Object

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



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

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

#e10Object

Returns the element at row 1 and column 0.



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

def e10() getElement(1,0) end

#e10=(value) ⇒ Object

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



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

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

#e11Object

Returns the element at row 1 and column 1.



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

def e11() getElement(1,1) end

#e11=(value) ⇒ Object

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



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

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

#e12Object

Returns the element at row 1 and column 2.



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

def e12() getElement(1,2) end

#e12=(value) ⇒ Object

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



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

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

#e20Object

Returns the element at row 2 and column 0.



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

def e20() getElement(2,0) end

#e20=(value) ⇒ Object

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



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

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

#e21Object

Returns the element at row 2 and column 1.



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

def e21() getElement(2,1) end

#e21=(value) ⇒ Object

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



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

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

#e22Object

Returns the element at row 2 and column 2.



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

def e22() getElement(2,2) end

#e22=(value) ⇒ Object

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



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

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

#getColumn(column) ⇒ Object

call-seq: mtx3.getColumn© -> RVec3

Returns c-th column vector.



727
728
729
# File 'lib/rmath3d/rmath3d_plain.rb', line 727

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

#getDeterminantObject

call-seq: getDeterminant -> determinant

Calculates determinant.



789
790
791
792
793
# File 'lib/rmath3d/rmath3d_plain.rb', line 789

def getDeterminant
  e00 * (e11*e22 - e12*e21) -
  e01 * (e10*e22 - e12*e20) +
  e02 * (e10*e21 - e11*e20)
end

#getInverseObject

call-seq: getInverse -> inverse

Returns the inverse.



822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
# File 'lib/rmath3d/rmath3d_plain.rb', line 822

def getInverse
  result = RMtx3.new

  result.e00 =  (self.e11*self.e22 - self.e12*self.e21)
  result.e01 = -(self.e01*self.e22 - self.e02*self.e21)
  result.e02 =  (self.e01*self.e12 - self.e02*self.e11)

  result.e10 = -(self.e10*self.e22 - self.e12*self.e20)
  result.e11 =  (self.e00*self.e22 - self.e02*self.e20)
  result.e12 = -(self.e00*self.e12 - self.e02*self.e10)

  result.e20 =  (self.e10*self.e21 - self.e11*self.e20)
  result.e21 = -(self.e00*self.e21 - self.e01*self.e20)
  result.e22 =  (self.e00*self.e11 - self.e01*self.e10)

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

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

  d = 1.0 / det

  result.mul!( d )

  return result
end

#getRow(row) ⇒ Object

call-seq: mtx3.getRow® -> RVec3

Returns r-th row vector.



718
719
720
# File 'lib/rmath3d/rmath3d_plain.rb', line 718

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

#getTransposedObject

call-seq: getTransposed

Returns transposed matrix.



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

def getTransposed
  return RMtx3.new( @e[0], @e[1], @e[2],
                    @e[3], @e[4], @e[5],
                    @e[6], @e[7], @e[8] )
end

#invert!Object

call-seq: invert! -> self

Makes itself as the inverse of the original matrix.



856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
# File 'lib/rmath3d/rmath3d_plain.rb', line 856

def invert!
  elements = Array.new( 9 )

  elements[3*0+0] =  (self.e11*self.e22 - self.e12*self.e21)
  elements[3*0+1] = -(self.e01*self.e22 - self.e02*self.e21)
  elements[3*0+2] =  (self.e01*self.e12 - self.e02*self.e11)

  elements[3*1+0] = -(self.e10*self.e22 - self.e12*self.e20)
  elements[3*1+1] =  (self.e00*self.e22 - self.e02*self.e20)
  elements[3*1+2] = -(self.e00*self.e12 - self.e02*self.e10)

  elements[3*2+0] =  (self.e10*self.e21 - self.e11*self.e20)
  elements[3*2+1] = -(self.e00*self.e21 - self.e01*self.e20)
  elements[3*2+2] =  (self.e00*self.e11 - self.e01*self.e10)

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

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

  d = 1.0 / det

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

  return self
end

#mul!(other) ⇒ Object

call-seq: mtx1.mul!( mtx2 )

mtx1 *= mtx2



1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
# File 'lib/rmath3d/rmath3d_plain.rb', line 1192

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.e10 = other*self.e10
    self.e11 = other*self.e11
    self.e12 = other*self.e12
    self.e20 = other*self.e20
    self.e21 = other*self.e21
    self.e22 = other*self.e22

    return self
  when RMtx3
    result = RMtx3.new
    for row in 0...3 do
      for col in 0...3 do
        sum = 0.0
        for i in 0...3 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.e10 = result.e10
    self.e11 = result.e11
    self.e12 = result.e12
    self.e20 = result.e20
    self.e21 = result.e21
    self.e22 = result.e22

    return self
  end
end

#rotationAxis(axis, radian) ⇒ Object

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

Makes a matrix that rotates around the axis.



952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# File 'lib/rmath3d/rmath3d_plain.rb', line 952

def rotationAxis( axis, radian )
  if ( axis.class != RVec3 )
    raise TypeError, "RMtx3#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

  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.



982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/rmath3d/rmath3d_plain.rb', line 982

def rotationQuaternion( q )
  if ( q.class != RQuat )
    raise TypeError, "RMtx3#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

  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.



898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/rmath3d/rmath3d_plain.rb', line 898

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.



916
917
918
919
920
921
922
923
924
925
926
927
# File 'lib/rmath3d/rmath3d_plain.rb', line 916

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.



934
935
936
937
938
939
940
941
942
943
944
945
# File 'lib/rmath3d/rmath3d_plain.rb', line 934

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.



1025
1026
1027
1028
1029
1030
1031
1032
# File 'lib/rmath3d/rmath3d_plain.rb', line 1025

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: mtx3.setColumn(v,c)

Returns sets c-th column by vector v.



747
748
749
750
751
# File 'lib/rmath3d/rmath3d_plain.rb', line 747

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

#setElements(*a) ⇒ Object

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

Stores given 9 new values.



638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/rmath3d/rmath3d_plain.rb', line 638

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

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

#setIdentityObject

call-seq: setIdentity

Sets as identity matrix.



770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/rmath3d/rmath3d_plain.rb', line 770

def setIdentity
  for row in 0...3 do
    for col in 0...3 do
      index = 3*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: mtx3.setRow(v,r)

Returns sets r-th row by vector v.



736
737
738
739
740
# File 'lib/rmath3d/rmath3d_plain.rb', line 736

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

#setZeroObject

call-seq: setZero

Clears all elements by 0.0



758
759
760
761
762
763
# File 'lib/rmath3d/rmath3d_plain.rb', line 758

def setZero
  9.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.



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

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

  result = RMtx3.new
  for row in 0...3 do
    for col in 0...3 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.



614
615
616
# File 'lib/rmath3d/rmath3d_plain.rb', line 614

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



603
604
605
606
607
# File 'lib/rmath3d/rmath3d_plain.rb', line 603

def to_s
  "( #{@e[0]}, #{@e[3]}, #{@e[6]} )\n" +
  "( #{@e[1]}, #{@e[4]}, #{@e[7]} )\n" +
  "( #{@e[2]}, #{@e[5]}, #{@e[8]} )\n"
end

#transpose!Object

call-seq: transpose!

Transposeas its elements.



811
812
813
814
815
# File 'lib/rmath3d/rmath3d_plain.rb', line 811

def transpose!
  @e[1], @e[3] = @e[3], @e[1]
  @e[2], @e[6] = @e[6], @e[2]
  @e[5], @e[7] = @e[7], @e[5]
end