rmath3d 1.1.0 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,7 +29,7 @@ module RMath3D
29
29
  0.0, 0.0 ]
30
30
  when 1
31
31
  case a[0]
32
- when Fixnum, Float
32
+ when Float, Integer
33
33
  @e = [ a[0], a[0],
34
34
  a[0], a[0] ]
35
35
  when RMtx2
@@ -46,7 +46,7 @@ module RMath3D
46
46
  for col in 0...2 do
47
47
  index = 2*row + col
48
48
  case a[index]
49
- when Fixnum, Float
49
+ when Float, Integer
50
50
  setElement( row, col, a[index] )
51
51
  else
52
52
  raise TypeError, "RMtx2#initialize : Unknown type #{a[0].class}."
@@ -88,7 +88,7 @@ module RMath3D
88
88
  #
89
89
  def coerce
90
90
  case arg
91
- when Fixnum, Float, Bignum
91
+ when Float, Integer
92
92
  return [ self, arg ]
93
93
  else
94
94
  raise TypeError, "RMtx2#coerce : #{arg.self} can't be coerced into #{self.class}."
@@ -411,7 +411,7 @@ module RMath3D
411
411
  #
412
412
  def *( arg )
413
413
  case arg
414
- when Fixnum, Float, Bignum
414
+ when Float, Integer
415
415
  return RMtx2.new( arg*self.e00, arg*self.e01,
416
416
  arg*self.e10, arg*self.e11 )
417
417
 
@@ -440,19 +440,18 @@ module RMath3D
440
440
  # mtx1 == mtx2 : evaluates equality.
441
441
  #
442
442
  def ==( other )
443
- if ( other.class != RMtx2 )
444
- raise TypeError, "RMtx2#==(other) : Unknown type #{other.class} given as RMtx2."
445
- return nil
446
- end
447
-
448
- for row in 0...2 do
449
- for col in 0...2 do
450
- if ( (getElement(row,col) - other.getElement(row,col)).abs > TOLERANCE )
451
- return false
443
+ if other.class == RMtx2
444
+ for row in 0...2 do
445
+ for col in 0...2 do
446
+ if ( (getElement(row,col) - other.getElement(row,col)).abs > TOLERANCE )
447
+ return false
448
+ end
452
449
  end
453
450
  end
451
+ return true
452
+ else
453
+ return false
454
454
  end
455
- return true
456
455
  end
457
456
 
458
457
  #
@@ -504,7 +503,7 @@ module RMath3D
504
503
  #
505
504
  def mul!( other )
506
505
  case other
507
- when Fixnum, Float, Bignum
506
+ when Float, Integer
508
507
  self.e00 = other*self.e00
509
508
  self.e01 = other*self.e01
510
509
  self.e10 = other*self.e10
@@ -561,7 +560,7 @@ module RMath3D
561
560
  0.0, 0.0, 0.0 ]
562
561
  when 1
563
562
  case a[0]
564
- when Fixnum, Float
563
+ when Float, Integer
565
564
  @e = [ a[0], a[0], a[0],
566
565
  a[0], a[0], a[0],
567
566
  a[0], a[0], a[0] ]
@@ -580,7 +579,7 @@ module RMath3D
580
579
  for col in 0...3 do
581
580
  index = 3*row + col
582
581
  case a[index]
583
- when Fixnum, Float
582
+ when Float, Integer
584
583
  setElement( row, col, a[index] )
585
584
  else
586
585
  raise TypeError, "RMtx3#initialize : Unknown type #{a[0].class}."
@@ -623,7 +622,7 @@ module RMath3D
623
622
  #
624
623
  def coerce
625
624
  case arg
626
- when Fixnum, Float, Bignum
625
+ when Float, Integer
627
626
  return [ self, arg ]
628
627
  else
629
628
  raise TypeError, "RMtx3#coerce : #{arg.self} can't be coerced into #{self.class}."
@@ -1099,7 +1098,7 @@ module RMath3D
1099
1098
  #
1100
1099
  def *( arg )
1101
1100
  case arg
1102
- when Fixnum, Float, Bignum
1101
+ when Float, Integer
1103
1102
  return RMtx3.new( arg*self.e00, arg*self.e01, arg*self.e02,
1104
1103
  arg*self.e10, arg*self.e11, arg*self.e12,
1105
1104
  arg*self.e20, arg*self.e21, arg*self.e22 )
@@ -1129,19 +1128,18 @@ module RMath3D
1129
1128
  # mtx1 == mtx2 : evaluates equality.
1130
1129
  #
1131
1130
  def ==( other )
1132
- if ( other.class != RMtx3 )
1133
- raise TypeError, "RMtx3#==(other) : Unknown type #{other.class} given as RMtx3."
1134
- return nil
1135
- end
1136
-
1137
- for row in 0...3 do
1138
- for col in 0...3 do
1139
- if ( (getElement(row,col) - other.getElement(row,col)).abs > TOLERANCE )
1140
- return false
1131
+ if other.class == RMtx3
1132
+ for row in 0...3 do
1133
+ for col in 0...3 do
1134
+ if ( (getElement(row,col) - other.getElement(row,col)).abs > TOLERANCE )
1135
+ return false
1136
+ end
1141
1137
  end
1142
1138
  end
1139
+ return true
1140
+ else
1141
+ return false
1143
1142
  end
1144
- return true
1145
1143
  end
1146
1144
 
1147
1145
  #
@@ -1193,7 +1191,7 @@ module RMath3D
1193
1191
  #
1194
1192
  def mul!( other )
1195
1193
  case other
1196
- when Fixnum, Float, Bignum
1194
+ when Float, Integer
1197
1195
  self.e00 = other*self.e00
1198
1196
  self.e01 = other*self.e01
1199
1197
  self.e02 = other*self.e02
@@ -1261,7 +1259,7 @@ module RMath3D
1261
1259
  0.0, 0.0, 0.0, 0.0 ]
1262
1260
  when 1
1263
1261
  case a[0]
1264
- when Fixnum, Float
1262
+ when Float, Integer
1265
1263
  @e = [ a[0], a[0], a[0], a[0],
1266
1264
  a[0], a[0], a[0], a[0],
1267
1265
  a[0], a[0], a[0], a[0],
@@ -1282,7 +1280,7 @@ module RMath3D
1282
1280
  for col in 0...4 do
1283
1281
  index = 4*row + col
1284
1282
  case a[index]
1285
- when Fixnum, Float
1283
+ when Float, Integer
1286
1284
  setElement( row, col, a[index] )
1287
1285
  else
1288
1286
  raise TypeError, "RMtx4#initialize : Unknown type #{a[0].class}."
@@ -1326,7 +1324,7 @@ module RMath3D
1326
1324
  #
1327
1325
  def coerce
1328
1326
  case arg
1329
- when Fixnum, Float, Bignum
1327
+ when Float, Integer
1330
1328
  return [ self, arg ]
1331
1329
  else
1332
1330
  raise TypeError, "RMtx4#coerce : #{arg.self} can't be coerced into #{self.class}."
@@ -1839,6 +1837,39 @@ module RMath3D
1839
1837
  return self
1840
1838
  end
1841
1839
 
1840
+ #
1841
+ # call-seq: lookAtLH(eye,at,up) -> self
1842
+ #
1843
+ # Builds a viewing matrix for a left-handed coordinate system from:
1844
+ # * eye position (+eye+: RVec3)
1845
+ # * a point looking at (+at+: RVec3)
1846
+ # * up vector (+up+: RVec3)
1847
+ #
1848
+ def lookAtLH( eye, at, up )
1849
+ setIdentity()
1850
+
1851
+ axis_z = (at - eye).normalize!
1852
+ axis_x = RVec3.cross( up, axis_z ).normalize!
1853
+ axis_y = RVec3.cross( axis_z, axis_x )
1854
+
1855
+ self.e00 = axis_x[0]
1856
+ self.e01 = axis_x[1]
1857
+ self.e02 = axis_x[2]
1858
+ self.e03 = -RVec3.dot( axis_x, eye )
1859
+
1860
+ self.e10 = axis_y[0]
1861
+ self.e11 = axis_y[1]
1862
+ self.e12 = axis_y[2]
1863
+ self.e13 = -RVec3.dot( axis_y, eye )
1864
+
1865
+ self.e20 = axis_z[0]
1866
+ self.e21 = axis_z[1]
1867
+ self.e22 = axis_z[2]
1868
+ self.e23 = -RVec3.dot( axis_z, eye )
1869
+
1870
+ return self
1871
+ end
1872
+
1842
1873
  #
1843
1874
  # call-seq: lookAtRH(eye,at,up) -> self
1844
1875
  #
@@ -1873,43 +1904,146 @@ module RMath3D
1873
1904
  end
1874
1905
 
1875
1906
  #
1876
- # call-seq: perspectiveRH(width,height,znear,zfar) -> self
1907
+ # call-seq: perspectiveLH(width,height,znear,zfar,ndc_convention) -> self
1877
1908
  #
1878
1909
  # Builds a perspective projection matrix for a right-handed coordinate system from:
1879
1910
  # * View volume width (+width+)
1880
1911
  # * View volume height (+height+)
1881
1912
  # * Near clip plane distance (+znear+)
1882
1913
  # * Far clip plane distance (+zfar+)
1914
+ # * Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (+ndc_homogeneous+)
1883
1915
  #
1884
- def perspectiveRH( width, height, znear, zfar )
1885
- perspectiveOffCenterRH(-width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar )
1886
- return self
1916
+ def perspectiveLH( width, height, znear, zfar, ndc_homogeneous = true)
1917
+ return perspectiveOffCenterLH(-width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar, ndc_homogeneous )
1887
1918
  end
1888
1919
 
1889
1920
  #
1890
- # call-seq: perspectiveFovRH(fovy,aspect,znear,zfar) -> self
1921
+ # call-seq: perspectiveFovLH(fovy,aspect,znear,zfar,ndc_homogeneous) -> self
1891
1922
  #
1892
1923
  # Builds a perspective projection matrix for a right-handed coordinate system from:
1893
1924
  # * Field of view in y direction (+fovy+ radian)
1894
1925
  # * Aspect ratio (+aspect+)
1895
1926
  # * Near clip plane distance (+znear+)
1896
1927
  # * Far clip plane distance (+zfar+)
1928
+ # * Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (+ndc_homogeneous+)
1929
+ #
1930
+ def perspectiveFovLH( fovy_radian, aspect, znear, zfar, ndc_homogeneous = true)
1931
+ # Ref.: https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix
1932
+ top = Math::tan(fovy_radian / 2.0) * znear
1933
+ bottom = -top
1934
+ right = top * aspect
1935
+ left = -right
1936
+ return perspectiveOffCenterLH(left, right, bottom, top, znear, zfar, ndc_homogeneous)
1937
+ end
1938
+
1939
+ #
1940
+ # call-seq: perspectiveOffCenterLH(left,right,bottom,top,znear,zfar) -> self
1941
+ #
1942
+ # Builds a perspective projection matrix for a right-handed coordinate system from:
1943
+ # * Minimum value of the view volume width (+left+)
1944
+ # * Maximum value of the view volume width (+right+)
1945
+ # * Minimum value of the view volume height (+bottom+)
1946
+ # * Maximum value of the view volume height (+top+)
1947
+ # * Near clip plane distance (+znear+)
1948
+ # * Far clip plane distance (+zfar+)
1949
+ # * Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (+ndc_homogeneous+)
1950
+ #
1951
+ def perspectiveOffCenterLH( left, right, bottom, top, znear, zfar, ndc_homogeneous = true)
1952
+ a = (right+left) / (right-left)
1953
+ b = (top+bottom) / (top-bottom)
1954
+ c = ndc_homogeneous ? -(zfar+znear) / (zfar-znear) : -zfar / (zfar-znear)
1955
+ d = ndc_homogeneous ? -(2*znear*zfar) / (zfar-znear) : -(znear*zfar) / (zfar-znear)
1956
+
1957
+ setZero()
1958
+
1959
+ setElement( 0, 0, 2*znear/(right-left) )
1960
+ setElement( 0, 2, -a )
1961
+ setElement( 1, 1, 2*znear/(top-bottom) )
1962
+ setElement( 1, 2, -b )
1963
+ setElement( 2, 2, -c )
1964
+ setElement( 2, 3, d )
1965
+ setElement( 3, 2, 1.0 )
1966
+
1967
+ return self
1968
+ end
1969
+
1970
+ #
1971
+ # call-seq: orthoLH(width,height,znear,zfar) -> self
1972
+ #
1973
+ # Builds a orthogonal projection matrix for a right-handed coordinate system from:
1974
+ # * View volume width (+width+)
1975
+ # * View volume height (+height+)
1976
+ # * Near clip plane distance (+znear+)
1977
+ # * Far clip plane distance (+zfar+)
1978
+ # * Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (+ndc_homogeneous+)
1897
1979
  #
1898
- def perspectiveFovRH( fovy_radian, aspect, znear, zfar )
1899
- f = Math::tan( fovy_radian / 2.0 )
1900
- f = 1.0 / f
1980
+ def orthoLH( width, height, znear, zfar, ndc_homogeneous = true)
1981
+ orthoOffCenterLH( -width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar, ndc_homogeneous )
1982
+ return self
1983
+ end
1984
+
1985
+ #
1986
+ # call-seq: orthoOffCenterLH(left,right,bottom,top,znear,zfar) -> self
1987
+ #
1988
+ # Builds a orthogonal projection matrix for a right-handed coordinate system from:
1989
+ # * Minimum value of the view volume width (+left+)
1990
+ # * Maximum value of the view volume width (+right+)
1991
+ # * Minimum value of the view volume height (+bottom+)
1992
+ # * Maximum value of the view volume height (+top+)
1993
+ # * Near clip plane distance (+znear+)
1994
+ # * Far clip plane distance (+zfar+)
1995
+ # * Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (+ndc_homogeneous+)
1996
+ #
1997
+ def orthoOffCenterLH( left, right, bottom, top, znear, zfar, ndc_homogeneous = true)
1998
+ tx = -(right+left) / (right-left)
1999
+ ty = -(top+bottom) / (top-bottom)
2000
+ tz = ndc_homogeneous ? -(zfar+znear) / (zfar-znear) : -znear / (zfar-znear)
1901
2001
 
1902
2002
  setIdentity()
1903
- setElement( 0, 0, f / aspect )
1904
- setElement( 1, 1, f )
1905
- setElement( 2, 2, (zfar+znear)/(znear-zfar) )
1906
- setElement( 2, 3, 2*zfar*znear/(znear-zfar) )
1907
- setElement( 3, 2, -1.0 )
1908
- setElement( 3, 3, 0.0 )
2003
+
2004
+ setElement( 0, 0, 2.0/(right-left) )
2005
+ setElement( 0, 3, tx )
2006
+ setElement( 1, 1, 2.0/(top-bottom) )
2007
+ setElement( 1, 3, ty )
2008
+ setElement( 2, 2, (ndc_homogeneous ? 2.0 : 1.0)/(zfar-znear) )
2009
+ setElement( 2, 3, tz )
1909
2010
 
1910
2011
  return self
1911
2012
  end
1912
2013
 
2014
+ #
2015
+ # call-seq: perspectiveRH(width,height,znear,zfar,ndc_convention) -> self
2016
+ #
2017
+ # Builds a perspective projection matrix for a right-handed coordinate system from:
2018
+ # * View volume width (+width+)
2019
+ # * View volume height (+height+)
2020
+ # * Near clip plane distance (+znear+)
2021
+ # * Far clip plane distance (+zfar+)
2022
+ # * Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (+ndc_homogeneous+)
2023
+ #
2024
+ def perspectiveRH( width, height, znear, zfar, ndc_homogeneous = true)
2025
+ return perspectiveOffCenterRH(-width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar, ndc_homogeneous )
2026
+ end
2027
+
2028
+ #
2029
+ # call-seq: perspectiveFovRH(fovy,aspect,znear,zfar,ndc_homogeneous) -> self
2030
+ #
2031
+ # Builds a perspective projection matrix for a right-handed coordinate system from:
2032
+ # * Field of view in y direction (+fovy+ radian)
2033
+ # * Aspect ratio (+aspect+)
2034
+ # * Near clip plane distance (+znear+)
2035
+ # * Far clip plane distance (+zfar+)
2036
+ # * Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (+ndc_homogeneous+)
2037
+ #
2038
+ def perspectiveFovRH( fovy_radian, aspect, znear, zfar, ndc_homogeneous = true)
2039
+ # Ref.: https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix
2040
+ top = Math::tan(fovy_radian / 2.0) * znear
2041
+ bottom = -top
2042
+ right = top * aspect
2043
+ left = -right
2044
+ return perspectiveOffCenterRH(left, right, bottom, top, znear, zfar, ndc_homogeneous)
2045
+ end
2046
+
1913
2047
  #
1914
2048
  # call-seq: perspectiveOffCenterRH(left,right,bottom,top,znear,zfar) -> self
1915
2049
  #
@@ -1920,14 +2054,15 @@ module RMath3D
1920
2054
  # * Maximum value of the view volume height (+top+)
1921
2055
  # * Near clip plane distance (+znear+)
1922
2056
  # * Far clip plane distance (+zfar+)
2057
+ # * Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (+ndc_homogeneous+)
1923
2058
  #
1924
- def perspectiveOffCenterRH( left, right, bottom, top, znear, zfar )
2059
+ def perspectiveOffCenterRH( left, right, bottom, top, znear, zfar, ndc_homogeneous = true)
1925
2060
  a = (right+left) / (right-left)
1926
2061
  b = (top+bottom) / (top-bottom)
1927
- c = -(zfar+znear) / (zfar-znear)
1928
- d = -(2*znear*zfar) / (zfar-znear)
2062
+ c = ndc_homogeneous ? -(zfar+znear) / (zfar-znear) : -zfar / (zfar-znear)
2063
+ d = ndc_homogeneous ? -(2*znear*zfar) / (zfar-znear) : -(znear*zfar) / (zfar-znear)
1929
2064
 
1930
- setIdentity()
2065
+ setZero()
1931
2066
 
1932
2067
  setElement( 0, 0, 2*znear/(right-left) )
1933
2068
  setElement( 0, 2, a )
@@ -1936,7 +2071,6 @@ module RMath3D
1936
2071
  setElement( 2, 2, c )
1937
2072
  setElement( 2, 3, d )
1938
2073
  setElement( 3, 2, -1.0 )
1939
- setElement( 3, 3, 0.0 )
1940
2074
 
1941
2075
  return self
1942
2076
  end
@@ -1949,9 +2083,10 @@ module RMath3D
1949
2083
  # * View volume height (+height+)
1950
2084
  # * Near clip plane distance (+znear+)
1951
2085
  # * Far clip plane distance (+zfar+)
2086
+ # * Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (+ndc_homogeneous+)
1952
2087
  #
1953
- def orthoRH( width, height, znear, zfar )
1954
- orthoOffCenterRH( -width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar )
2088
+ def orthoRH( width, height, znear, zfar, ndc_homogeneous = true)
2089
+ orthoOffCenterRH( -width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar, ndc_homogeneous )
1955
2090
  return self
1956
2091
  end
1957
2092
 
@@ -1965,11 +2100,12 @@ module RMath3D
1965
2100
  # * Maximum value of the view volume height (+top+)
1966
2101
  # * Near clip plane distance (+znear+)
1967
2102
  # * Far clip plane distance (+zfar+)
2103
+ # * Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (+ndc_homogeneous+)
1968
2104
  #
1969
- def orthoOffCenterRH( left, right, bottom, top, znear, zfar )
1970
- tx = (right+left) / (right-left)
1971
- ty = (top+bottom) / (top-bottom)
1972
- tz = (zfar+znear) / (zfar-znear)
2105
+ def orthoOffCenterRH( left, right, bottom, top, znear, zfar, ndc_homogeneous = true)
2106
+ tx = -(right+left) / (right-left)
2107
+ ty = -(top+bottom) / (top-bottom)
2108
+ tz = ndc_homogeneous ? -(zfar+znear) / (zfar-znear) : -znear / (zfar-znear)
1973
2109
 
1974
2110
  setIdentity()
1975
2111
 
@@ -1977,7 +2113,7 @@ module RMath3D
1977
2113
  setElement( 0, 3, tx )
1978
2114
  setElement( 1, 1, 2.0/(top-bottom) )
1979
2115
  setElement( 1, 3, ty )
1980
- setElement( 2, 2, -2.0/(zfar-znear) )
2116
+ setElement( 2, 2, -(ndc_homogeneous ? 2.0 : 1.0)/(zfar-znear) )
1981
2117
  setElement( 2, 3, tz )
1982
2118
 
1983
2119
  return self
@@ -2050,7 +2186,7 @@ module RMath3D
2050
2186
  #
2051
2187
  def *( arg )
2052
2188
  case arg
2053
- when Fixnum, Float, Bignum
2189
+ when Float, Integer
2054
2190
  return RMtx4.new( arg*self.e00, arg*self.e01, arg*self.e02, arg*self.e03,
2055
2191
  arg*self.e10, arg*self.e11, arg*self.e12, arg*self.e13,
2056
2192
  arg*self.e20, arg*self.e21, arg*self.e22, arg*self.e23,
@@ -2081,19 +2217,18 @@ module RMath3D
2081
2217
  # mtx1 == mtx2 : evaluates equality.
2082
2218
  #
2083
2219
  def ==( other )
2084
- if ( other.class != RMtx4 )
2085
- raise TypeError, "RMtx4#==(other) : Unknown type #{other.class} given as RMtx4."
2086
- return nil
2087
- end
2088
-
2089
- for row in 0...4 do
2090
- for col in 0...4 do
2091
- if ( (getElement(row,col) - other.getElement(row,col)).abs > TOLERANCE )
2092
- return false
2220
+ if other.class == RMtx4
2221
+ for row in 0...4 do
2222
+ for col in 0...4 do
2223
+ if ( (getElement(row,col) - other.getElement(row,col)).abs > TOLERANCE )
2224
+ return false
2225
+ end
2093
2226
  end
2094
2227
  end
2228
+ return true
2229
+ else
2230
+ return false
2095
2231
  end
2096
- return true
2097
2232
  end
2098
2233
 
2099
2234
  #
@@ -2145,7 +2280,7 @@ module RMath3D
2145
2280
  #
2146
2281
  def mul!( other )
2147
2282
  case other
2148
- when Fixnum, Float, Bignum
2283
+ when Float, Integer
2149
2284
  self.e00 = other*self.e00
2150
2285
  self.e01 = other*self.e01
2151
2286
  self.e02 = other*self.e02
@@ -2227,7 +2362,7 @@ module RMath3D
2227
2362
  @e = [0.0, 0.0, 0.0, 0.0]
2228
2363
  when 1
2229
2364
  case a[0]
2230
- when Fixnum, Float
2365
+ when Float, Integer
2231
2366
  @e = [ a[0], a[0], a[0], a[0] ]
2232
2367
  when RQuat
2233
2368
  @e = [ a[0].x, a[0].y, a[0].z, a[0].w ]
@@ -2238,7 +2373,7 @@ module RMath3D
2238
2373
  when 4
2239
2374
  a.each_with_index do |elem, index|
2240
2375
  case elem
2241
- when Fixnum, Float
2376
+ when Float, Integer
2242
2377
  @e[index] = elem
2243
2378
  else
2244
2379
  raise TypeError, "RQuat#initialize : Unknown type #{elem.class}."
@@ -2258,7 +2393,7 @@ module RMath3D
2258
2393
  # Returns human-readable string.
2259
2394
  #
2260
2395
  def to_s
2261
- return "( #{@e[0]}, #{@e[1]}, #{@e[2]}, #{@e[3]} )\n"
2396
+ return "( #{@e[0]}, #{@e[1]}, #{@e[2]}, #{@e[3]} )"
2262
2397
  end
2263
2398
 
2264
2399
  #
@@ -2277,7 +2412,7 @@ module RMath3D
2277
2412
  #
2278
2413
  def coerce( arg )
2279
2414
  case arg
2280
- when Fixnum, Float, Bignum
2415
+ when Float, Integer
2281
2416
  return [ self, arg ]
2282
2417
  else
2283
2418
  raise TypeError, "RQuat#coerce : #{arg.self} can't be coerced into #{self.class}."
@@ -2616,7 +2751,7 @@ module RMath3D
2616
2751
  z = q1w*q2z + q1x*q2y - q1y*q2x + q1z*q2w
2617
2752
  w = q1w*q2w - q1x*q2x - q1y*q2y - q1z*q2z
2618
2753
  return RQuat.new( x, y, z, w )
2619
- when Fixnum, Float
2754
+ when Float, Integer
2620
2755
  return RQuat.new( @e[0]*arg, @e[1]*arg, @e[2]*arg, @e[3]*arg )
2621
2756
  else
2622
2757
  raise TypeError, "RQuat#* : Unknown type #{arg}."
@@ -2630,16 +2765,15 @@ module RMath3D
2630
2765
  # quat1 == quat2 : evaluates equality.
2631
2766
  #
2632
2767
  def ==( other )
2633
- if other.class != RQuat
2634
- raise TypeError, "RQuat#== : Unknown type #{other.class}."
2635
- return nil
2636
- end
2637
-
2638
- if (x-other.x).abs<=Float::EPSILON &&
2639
- (y-other.y).abs<=Float::EPSILON &&
2640
- (z-other.z).abs<=Float::EPSILON &&
2641
- (w-other.w).abs<=Float::EPSILON
2642
- return true
2768
+ if other.class == RQuat
2769
+ if (x-other.x).abs<=Float::EPSILON &&
2770
+ (y-other.y).abs<=Float::EPSILON &&
2771
+ (z-other.z).abs<=Float::EPSILON &&
2772
+ (w-other.w).abs<=Float::EPSILON
2773
+ return true
2774
+ else
2775
+ return false
2776
+ end
2643
2777
  else
2644
2778
  return false
2645
2779
  end
@@ -2712,7 +2846,7 @@ module RMath3D
2712
2846
 
2713
2847
  return self
2714
2848
 
2715
- when Fixnum, Float
2849
+ when Float, Integer
2716
2850
  self.x *= other
2717
2851
  self.y *= other
2718
2852
  self.z *= other
@@ -2828,7 +2962,7 @@ module RMath3D
2828
2962
  @e = [0.0, 0.0]
2829
2963
  when 1
2830
2964
  case a[0]
2831
- when Fixnum, Float
2965
+ when Float, Integer
2832
2966
  @e = [ a[0], a[0] ]
2833
2967
  when RVec2
2834
2968
  @e = [ a[0].x, a[0].y ]
@@ -2839,7 +2973,7 @@ module RMath3D
2839
2973
  when 2
2840
2974
  a.each_with_index do |elem, index|
2841
2975
  case elem
2842
- when Fixnum, Float
2976
+ when Float, Integer
2843
2977
  @e[index] = elem
2844
2978
  else
2845
2979
  raise TypeError, "RVec2#initialize : Unknown type #{elem.class}."
@@ -2859,7 +2993,7 @@ module RMath3D
2859
2993
  # Returns human-readable string.
2860
2994
  #
2861
2995
  def to_s
2862
- return "( #{@e[0]}, #{@e[1]} )\n"
2996
+ return "( #{@e[0]}, #{@e[1]} )"
2863
2997
  end
2864
2998
 
2865
2999
  #
@@ -2878,7 +3012,7 @@ module RMath3D
2878
3012
  #
2879
3013
  def coerce( arg )
2880
3014
  case arg
2881
- when Fixnum, Float, Bignum
3015
+ when Float, Integer
2882
3016
  return [ self, arg ]
2883
3017
  else
2884
3018
  raise TypeError, "RVec2#coerce : #{arg.self} can't be coerced into #{self.class}."
@@ -3067,7 +3201,7 @@ module RMath3D
3067
3201
  #
3068
3202
  def *( arg )
3069
3203
  case arg
3070
- when Fixnum, Float
3204
+ when Float, Integer
3071
3205
  return RVec2.new( @e[0]*arg, @e[1]*arg )
3072
3206
  else
3073
3207
  raise TypeError, "RVec2#* : Unknown type #{arg}."
@@ -3081,14 +3215,13 @@ module RMath3D
3081
3215
  # vec1 == vec2 : evaluates equality.
3082
3216
  #
3083
3217
  def ==( other )
3084
- if other.class != RVec2
3085
- raise TypeError, "RVec2#== : Unknown type #{other.class}."
3086
- return nil
3087
- end
3088
-
3089
- if (x-other.x).abs<=Float::EPSILON &&
3090
- (y-other.y).abs<=Float::EPSILON
3091
- return true
3218
+ if other.class == RVec2
3219
+ if (x-other.x).abs<=Float::EPSILON &&
3220
+ (y-other.y).abs<=Float::EPSILON
3221
+ return true
3222
+ else
3223
+ return false
3224
+ end
3092
3225
  else
3093
3226
  return false
3094
3227
  end
@@ -3134,7 +3267,7 @@ module RMath3D
3134
3267
  # vec1 *= vec2
3135
3268
  #
3136
3269
  def mul!( arg )
3137
- if arg.class != Fixnum && arg.class != Float
3270
+ if !(arg.class == Float || arg.class == Integer)
3138
3271
  raise TypeError, "RVec2#mul! : Unknown type #{arg.class}."
3139
3272
  return nil
3140
3273
  end
@@ -3168,7 +3301,7 @@ module RMath3D
3168
3301
  @e = [0.0, 0.0, 0.0]
3169
3302
  when 1
3170
3303
  case a[0]
3171
- when Fixnum, Float
3304
+ when Float, Integer
3172
3305
  @e = [ a[0], a[0], a[0] ]
3173
3306
  when RVec3
3174
3307
  @e = [ a[0].x, a[0].y, a[0].z ]
@@ -3179,7 +3312,7 @@ module RMath3D
3179
3312
  when 3
3180
3313
  a.each_with_index do |elem, index|
3181
3314
  case elem
3182
- when Fixnum, Float
3315
+ when Float, Integer
3183
3316
  @e[index] = elem
3184
3317
  else
3185
3318
  raise TypeError, "RVec3#initialize : Unknown type #{elem.class}."
@@ -3199,7 +3332,7 @@ module RMath3D
3199
3332
  # Returns human-readable string.
3200
3333
  #
3201
3334
  def to_s
3202
- return "( #{@e[0]}, #{@e[1]}, #{@e[2]} )\n"
3335
+ return "( #{@e[0]}, #{@e[1]}, #{@e[2]} )"
3203
3336
  end
3204
3337
 
3205
3338
  #
@@ -3218,7 +3351,7 @@ module RMath3D
3218
3351
  #
3219
3352
  def coerce( arg )
3220
3353
  case arg
3221
- when Fixnum, Float, Bignum
3354
+ when Float, Integer
3222
3355
  return [ self, arg ]
3223
3356
  else
3224
3357
  raise TypeError, "RVec3#coerce : #{arg.self} can't be coerced into #{self.class}."
@@ -3522,9 +3655,9 @@ module RMath3D
3522
3655
  t_z = q.w*self[2] + q.x*self[1] - q.y*self[0]
3523
3656
  t_w = - q.x*self[0] - q.y*self[1] - q.z*self[2]
3524
3657
 
3525
- result.x = -t_w*q.x + t_x*q.w - t_y*q.z + t_z*q.y;
3526
- result.y = -t_w*q.y + t_x*q.z + t_y*q.w - t_z*q.x;
3527
- result.z = -t_w*q.z - t_x*q.y + t_y*q.x + t_z*q.w;
3658
+ result.x = -t_w*q.x + t_x*q.w - t_y*q.z + t_z*q.y
3659
+ result.y = -t_w*q.y + t_x*q.z + t_y*q.w - t_z*q.x
3660
+ result.z = -t_w*q.z - t_x*q.y + t_y*q.x + t_z*q.w
3528
3661
 
3529
3662
  return result
3530
3663
  end
@@ -3538,9 +3671,9 @@ module RMath3D
3538
3671
  t_z = q.w*self[2] + q.x*self[1] - q.y*self[0]
3539
3672
  t_w = - q.x*self[0] - q.y*self[1] - q.z*self[2]
3540
3673
 
3541
- self.x = -t_w*q.x + t_x*q.w - t_y*q.z + t_z*q.y;
3542
- self.y = -t_w*q.y + t_x*q.z + t_y*q.w - t_z*q.x;
3543
- self.z = -t_w*q.z - t_x*q.y + t_y*q.x + t_z*q.w;
3674
+ self.x = -t_w*q.x + t_x*q.w - t_y*q.z + t_z*q.y
3675
+ self.y = -t_w*q.y + t_x*q.z + t_y*q.w - t_z*q.x
3676
+ self.z = -t_w*q.z - t_x*q.y + t_y*q.x + t_z*q.w
3544
3677
 
3545
3678
  return self
3546
3679
  end
@@ -3621,7 +3754,7 @@ module RMath3D
3621
3754
  #
3622
3755
  def *( arg )
3623
3756
  case arg
3624
- when Fixnum, Float
3757
+ when Float, Integer
3625
3758
  return RVec3.new( @e[0]*arg, @e[1]*arg, @e[2]*arg )
3626
3759
  else
3627
3760
  raise TypeError, "RVec3#* : Unknown type #{arg}."
@@ -3635,15 +3768,14 @@ module RMath3D
3635
3768
  # vec1 == vec2 : evaluates equality.
3636
3769
  #
3637
3770
  def ==( other )
3638
- if other.class != RVec3
3639
- raise TypeError, "RVec3#== : Unknown type #{other.class}."
3640
- return nil
3641
- end
3642
-
3643
- if (x-other.x).abs<=Float::EPSILON &&
3644
- (y-other.y).abs<=Float::EPSILON &&
3645
- (z-other.z).abs<=Float::EPSILON
3646
- return true
3771
+ if other.class == RVec3
3772
+ if (x-other.x).abs<=Float::EPSILON &&
3773
+ (y-other.y).abs<=Float::EPSILON &&
3774
+ (z-other.z).abs<=Float::EPSILON
3775
+ return true
3776
+ else
3777
+ return false
3778
+ end
3647
3779
  else
3648
3780
  return false
3649
3781
  end
@@ -3691,7 +3823,7 @@ module RMath3D
3691
3823
  # vec1 *= vec2
3692
3824
  #
3693
3825
  def mul!( arg )
3694
- if arg.class != Fixnum && arg.class != Float
3826
+ if !(arg.class == Float || arg.class == Integer)
3695
3827
  raise TypeError, "RVec3#mul! : Unknown type #{arg.class}."
3696
3828
  return nil
3697
3829
  end
@@ -3726,7 +3858,7 @@ module RMath3D
3726
3858
  @e = [0.0, 0.0, 0.0, 0.0]
3727
3859
  when 1
3728
3860
  case a[0]
3729
- when Fixnum, Float
3861
+ when Float, Integer
3730
3862
  @e = [ a[0], a[0], a[0], a[0] ]
3731
3863
  when RVec3
3732
3864
  @e = [ a[0].x, a[0].y, a[0].z, 0.0 ]
@@ -3739,7 +3871,7 @@ module RMath3D
3739
3871
  when 4
3740
3872
  a.each_with_index do |elem, index|
3741
3873
  case elem
3742
- when Fixnum, Float
3874
+ when Float, Integer
3743
3875
  @e[index] = elem
3744
3876
  else
3745
3877
  raise TypeError, "RVec4#initialize : Unknown type #{elem.class}."
@@ -3759,7 +3891,7 @@ module RMath3D
3759
3891
  # Returns human-readable string.
3760
3892
  #
3761
3893
  def to_s
3762
- return "( #{@e[0]}, #{@e[1]}, #{@e[2]}, #{@e[3]} )\n"
3894
+ return "( #{@e[0]}, #{@e[1]}, #{@e[2]}, #{@e[3]} )"
3763
3895
  end
3764
3896
 
3765
3897
  #
@@ -3778,7 +3910,7 @@ module RMath3D
3778
3910
  #
3779
3911
  def coerce( arg )
3780
3912
  case arg
3781
- when Fixnum, Float, Bignum
3913
+ when Float, Integer
3782
3914
  return [ self, arg ]
3783
3915
  else
3784
3916
  raise TypeError, "RVec4#coerce : #{arg.self} can't be coerced into #{self.class}."
@@ -4068,7 +4200,7 @@ module RMath3D
4068
4200
  #
4069
4201
  def *( arg )
4070
4202
  case arg
4071
- when Fixnum, Float
4203
+ when Float, Integer
4072
4204
  return RVec4.new( @e[0]*arg, @e[1]*arg, @e[2]*arg, @e[3]*arg )
4073
4205
  else
4074
4206
  raise TypeError, "RVec4#* : Unknown type #{arg}."
@@ -4082,16 +4214,15 @@ module RMath3D
4082
4214
  # vec1 == vec2 : evaluates equality.
4083
4215
  #
4084
4216
  def ==( other )
4085
- if other.class != RVec4
4086
- raise TypeError, "RVec4#== : Unknown type #{other.class}."
4087
- return nil
4088
- end
4089
-
4090
- if (x-other.x).abs<=Float::EPSILON &&
4091
- (y-other.y).abs<=Float::EPSILON &&
4092
- (z-other.z).abs<=Float::EPSILON &&
4093
- (w-other.w).abs<=Float::EPSILON
4094
- return true
4217
+ if other.class == RVec4
4218
+ if (x-other.x).abs<=Float::EPSILON &&
4219
+ (y-other.y).abs<=Float::EPSILON &&
4220
+ (z-other.z).abs<=Float::EPSILON &&
4221
+ (w-other.w).abs<=Float::EPSILON
4222
+ return true
4223
+ else
4224
+ return false
4225
+ end
4095
4226
  else
4096
4227
  return false
4097
4228
  end
@@ -4141,7 +4272,7 @@ module RMath3D
4141
4272
  # vec1 *= vec2
4142
4273
  #
4143
4274
  def mul!( other )
4144
- if other.class != Fixnum && other.class != Float
4275
+ if !(other.class == Float || other.class == Integer)
4145
4276
  raise TypeError, "RVec4#mul! : Unknown type #{other.class}."
4146
4277
  return nil
4147
4278
  end
@@ -4159,7 +4290,7 @@ end
4159
4290
 
4160
4291
  =begin
4161
4292
  RMath : Ruby math module for 3D Applications
4162
- Copyright (c) 2008- vaiorabbit <http://twitter.com/vaiorabbit>
4293
+ Copyright (c) 2008-2020 vaiorabbit <http://twitter.com/vaiorabbit>
4163
4294
 
4164
4295
  This software is provided 'as-is', without any express or implied
4165
4296
  warranty. In no event will the authors be held liable for any damages