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.



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

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.



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

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.



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

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.



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

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

mtx1 - mtx2 : Binary minus operator.



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

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.



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

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

#==(other) ⇒ Object

call-seq: ==

mtx1 == mtx2 : evaluates equality.



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

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

  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
end

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

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

Returns the element at (row,col).



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

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



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

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.



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

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.



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

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.



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

def e00() getElement(0,0) end

#e00=(value) ⇒ Object

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



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

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

#e01Object

Returns the element at row 0 and column 1.



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

def e01() getElement(0,1) end

#e01=(value) ⇒ Object

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



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

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

#e02Object

Returns the element at row 0 and column 2.



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

def e02() getElement(0,2) end

#e02=(value) ⇒ Object

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



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

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

#e10Object

Returns the element at row 1 and column 0.



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

def e10() getElement(1,0) end

#e10=(value) ⇒ Object

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



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

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

#e11Object

Returns the element at row 1 and column 1.



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

def e11() getElement(1,1) end

#e11=(value) ⇒ Object

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



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

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

#e12Object

Returns the element at row 1 and column 2.



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

def e12() getElement(1,2) end

#e12=(value) ⇒ Object

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



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

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

#e20Object

Returns the element at row 2 and column 0.



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

def e20() getElement(2,0) end

#e20=(value) ⇒ Object

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



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

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

#e21Object

Returns the element at row 2 and column 1.



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

def e21() getElement(2,1) end

#e21=(value) ⇒ Object

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



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

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

#e22Object

Returns the element at row 2 and column 2.



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

def e22() getElement(2,2) end

#e22=(value) ⇒ Object

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



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

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

#getColumn(column) ⇒ Object

call-seq: mtx3.getColumn© -> RVec3

Returns c-th column vector.



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

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

#getDeterminantObject

call-seq: getDeterminant -> determinant

Calculates determinant.



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

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.



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

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.



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

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

#getTransposedObject

call-seq: getTransposed

Returns transposed matrix.



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

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.



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

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



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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



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

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.



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

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.



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

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



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

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.



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

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