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.



1251
1252
1253
1254
1255
1256
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
1296
1297
# File 'lib/rmath3d/rmath3d_plain.rb', line 1251

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 Float, Integer
      @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 Float, Integer
          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.



2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
# File 'lib/rmath3d/rmath3d_plain.rb', line 2187

def *( arg )
  case arg
  when Float, Integer
    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.



2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
# File 'lib/rmath3d/rmath3d_plain.rb', line 2145

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.



2127
2128
2129
# File 'lib/rmath3d/rmath3d_plain.rb', line 2127

def +@
  return self
end

#-(arg) ⇒ Object

call-seq: -

mtx1 - mtx2 : Binary minus operator.



2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
# File 'lib/rmath3d/rmath3d_plain.rb', line 2166

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.



2136
2137
2138
# File 'lib/rmath3d/rmath3d_plain.rb', line 2136

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

#==(other) ⇒ Object

call-seq: ==

mtx1 == mtx2 : evaluates equality.



2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
# File 'lib/rmath3d/rmath3d_plain.rb', line 2219

def ==( other )
  if other.class == RMtx4
    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
  else
    return false
  end
end

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

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

Returns the element at (row,col).



1370
1371
1372
1373
# File 'lib/rmath3d/rmath3d_plain.rb', line 1370

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



1359
1360
1361
1362
# File 'lib/rmath3d/rmath3d_plain.rb', line 1359

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.



2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
# File 'lib/rmath3d/rmath3d_plain.rb', line 2239

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.



1325
1326
1327
1328
1329
1330
1331
1332
1333
# File 'lib/rmath3d/rmath3d_plain.rb', line 1325

def coerce
  case arg
  when Float, Integer
    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.



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

def e00() getElement(0,0) end

#e00=(value) ⇒ Object

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



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

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

#e01Object

Returns the element at row 0 and column 1.



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

def e01() getElement(0,1) end

#e01=(value) ⇒ Object

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



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

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

#e02Object

Returns the element at row 0 and column 2.



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

def e02() getElement(0,2) end

#e02=(value) ⇒ Object

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



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

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

#e03Object

Returns the element at row 0 and column 3.



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

def e03() getElement(0,3) end

#e03=(value) ⇒ Object

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



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

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

#e10Object

Returns the element at row 1 and column 0.



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

def e10() getElement(1,0) end

#e10=(value) ⇒ Object

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



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

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

#e11Object

Returns the element at row 1 and column 1.



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

def e11() getElement(1,1) end

#e11=(value) ⇒ Object

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



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

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

#e12Object

Returns the element at row 1 and column 2.



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

def e12() getElement(1,2) end

#e12=(value) ⇒ Object

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



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

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

#e13Object

Returns the element at row 1 and column 3.



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

def e13() getElement(1,3) end

#e13=(value) ⇒ Object

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



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

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

#e20Object

Returns the element at row 2 and column 0.



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

def e20() getElement(2,0) end

#e20=(value) ⇒ Object

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



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

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

#e21Object

Returns the element at row 2 and column 1.



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

def e21() getElement(2,1) end

#e21=(value) ⇒ Object

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



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

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

#e22Object

Returns the element at row 2 and column 2.



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

def e22() getElement(2,2) end

#e22=(value) ⇒ Object

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



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

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

#e23Object

Returns the element at row 2 and column 3.



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

def e23() getElement(2,3) end

#e23=(value) ⇒ Object

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



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

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

#e30Object

Returns the element at row 3 and column 0.



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

def e30() getElement(3,0) end

#e30=(value) ⇒ Object

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



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

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

#e31Object

Returns the element at row 3 and column 1.



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

def e31() getElement(3,1) end

#e31=(value) ⇒ Object

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



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

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

#e32Object

Returns the element at row 3 and column 2.



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

def e32() getElement(3,2) end

#e32=(value) ⇒ Object

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



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

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

#e33Object

Returns the element at row 3 and column 3.



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

def e33() getElement(3,3) end

#e33=(value) ⇒ Object

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



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

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

#getColumn(column) ⇒ Object

call-seq: mtx4.getColumn© -> RVec4

Returns c-th column vector.



1456
1457
1458
# File 'lib/rmath3d/rmath3d_plain.rb', line 1456

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.



1546
1547
1548
1549
1550
1551
# File 'lib/rmath3d/rmath3d_plain.rb', line 1546

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.



1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
# File 'lib/rmath3d/rmath3d_plain.rb', line 1584

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.



1447
1448
1449
# File 'lib/rmath3d/rmath3d_plain.rb', line 1447

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.



1558
1559
1560
1561
1562
1563
# File 'lib/rmath3d/rmath3d_plain.rb', line 1558

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



1484
1485
1486
1487
1488
# File 'lib/rmath3d/rmath3d_plain.rb', line 1484

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.



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
1675
1676
1677
1678
1679
# File 'lib/rmath3d/rmath3d_plain.rb', line 1626

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

#lookAtLH(eye, at, up) ⇒ Object

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

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

  • eye position (eye: RVec3)

  • a point looking at (at: RVec3)

  • up vector (up: RVec3)



1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
# File 'lib/rmath3d/rmath3d_plain.rb', line 1848

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

  axis_z = (at - eye).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

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



1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
# File 'lib/rmath3d/rmath3d_plain.rb', line 1881

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



2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
# File 'lib/rmath3d/rmath3d_plain.rb', line 2281

def mul!( other )
  case other
  when Float, Integer
    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

#orthoLH(width, height, znear, zfar, ndc_homogeneous = true) ⇒ Object

call-seq: orthoLH(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)

  • Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (ndc_homogeneous)



1980
1981
1982
1983
# File 'lib/rmath3d/rmath3d_plain.rb', line 1980

def orthoLH( width, height, znear, zfar, ndc_homogeneous = true)
  orthoOffCenterLH( -width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar, ndc_homogeneous )
  return self
end

#orthoOffCenterLH(left, right, bottom, top, znear, zfar, ndc_homogeneous = true) ⇒ Object

call-seq: orthoOffCenterLH(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)

  • Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (ndc_homogeneous)



1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
# File 'lib/rmath3d/rmath3d_plain.rb', line 1997

def orthoOffCenterLH( left, right, bottom, top, znear, zfar, ndc_homogeneous = true)
  tx = -(right+left) / (right-left)
  ty = -(top+bottom) / (top-bottom)
  tz = ndc_homogeneous ? -(zfar+znear) / (zfar-znear) : -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, (ndc_homogeneous ? 2.0 : 1.0)/(zfar-znear) )
  setElement( 2, 3, tz )

  return self
end

#orthoOffCenterRH(left, right, bottom, top, znear, zfar, ndc_homogeneous = true) ⇒ 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)

  • Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (ndc_homogeneous)



2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
# File 'lib/rmath3d/rmath3d_plain.rb', line 2105

def orthoOffCenterRH( left, right, bottom, top, znear, zfar, ndc_homogeneous = true)
  tx = -(right+left) / (right-left)
  ty = -(top+bottom) / (top-bottom)
  tz = ndc_homogeneous ? -(zfar+znear) / (zfar-znear) : -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, -(ndc_homogeneous ? 2.0 : 1.0)/(zfar-znear) )
  setElement( 2, 3, tz )

  return self
end

#orthoRH(width, height, znear, zfar, ndc_homogeneous = true) ⇒ 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)

  • Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (ndc_homogeneous)



2088
2089
2090
2091
# File 'lib/rmath3d/rmath3d_plain.rb', line 2088

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

#perspectiveFovLH(fovy_radian, aspect, znear, zfar, ndc_homogeneous = true) ⇒ Object

call-seq: perspectiveFovLH(fovy,aspect,znear,zfar,ndc_homogeneous) -> 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)

  • Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (ndc_homogeneous)



1930
1931
1932
1933
1934
1935
1936
1937
# File 'lib/rmath3d/rmath3d_plain.rb', line 1930

def perspectiveFovLH( fovy_radian, aspect, znear, zfar, ndc_homogeneous = true)
  # Ref.: https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix
  top = Math::tan(fovy_radian / 2.0) * znear
  bottom = -top
  right = top * aspect
  left = -right
  return perspectiveOffCenterLH(left, right, bottom, top, znear, zfar, ndc_homogeneous)
end

#perspectiveFovRH(fovy_radian, aspect, znear, zfar, ndc_homogeneous = true) ⇒ Object

call-seq: perspectiveFovRH(fovy,aspect,znear,zfar,ndc_homogeneous) -> 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)

  • Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (ndc_homogeneous)



2038
2039
2040
2041
2042
2043
2044
2045
# File 'lib/rmath3d/rmath3d_plain.rb', line 2038

def perspectiveFovRH( fovy_radian, aspect, znear, zfar, ndc_homogeneous = true)
  # Ref.: https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix
  top = Math::tan(fovy_radian / 2.0) * znear
  bottom = -top
  right = top * aspect
  left = -right
  return perspectiveOffCenterRH(left, right, bottom, top, znear, zfar, ndc_homogeneous)
end

#perspectiveLH(width, height, znear, zfar, ndc_homogeneous = true) ⇒ Object

call-seq: perspectiveLH(width,height,znear,zfar,ndc_convention) -> 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)

  • Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (ndc_homogeneous)



1916
1917
1918
# File 'lib/rmath3d/rmath3d_plain.rb', line 1916

def perspectiveLH( width, height, znear, zfar, ndc_homogeneous = true)
  return perspectiveOffCenterLH(-width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar, ndc_homogeneous )
end

#perspectiveOffCenterLH(left, right, bottom, top, znear, zfar, ndc_homogeneous = true) ⇒ Object

call-seq: perspectiveOffCenterLH(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)

  • Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (ndc_homogeneous)



1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
# File 'lib/rmath3d/rmath3d_plain.rb', line 1951

def perspectiveOffCenterLH( left, right, bottom, top, znear, zfar, ndc_homogeneous = true)
  a = (right+left) / (right-left)
  b = (top+bottom) / (top-bottom)
  c = ndc_homogeneous ? -(zfar+znear) / (zfar-znear) : -zfar / (zfar-znear)
  d = ndc_homogeneous ? -(2*znear*zfar) / (zfar-znear) : -(znear*zfar) / (zfar-znear)

  setZero()

  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 )

  return self
end

#perspectiveOffCenterRH(left, right, bottom, top, znear, zfar, ndc_homogeneous = true) ⇒ 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)

  • Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (ndc_homogeneous)



2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
# File 'lib/rmath3d/rmath3d_plain.rb', line 2059

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

  setZero()

  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 )

  return self
end

#perspectiveRH(width, height, znear, zfar, ndc_homogeneous = true) ⇒ Object

call-seq: perspectiveRH(width,height,znear,zfar,ndc_convention) -> 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)

  • Set true for the environment with Z coordinate ranges from -1 to +1 (OpenGL), and false otherwise (Direct3D, Metal) (ndc_homogeneous)



2024
2025
2026
# File 'lib/rmath3d/rmath3d_plain.rb', line 2024

def perspectiveRH( width, height, znear, zfar, ndc_homogeneous = true)
  return perspectiveOffCenterRH(-width/2.0, width/2.0, -height/2.0, height/2.0, znear, zfar, ndc_homogeneous )
end

#rotationAxis(axis, radian) ⇒ Object

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

Makes a matrix that rotates around the axis.



1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
# File 'lib/rmath3d/rmath3d_plain.rb', line 1754

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.



1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
# File 'lib/rmath3d/rmath3d_plain.rb', line 1786

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.



1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
# File 'lib/rmath3d/rmath3d_plain.rb', line 1700

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.



1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
# File 'lib/rmath3d/rmath3d_plain.rb', line 1718

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.



1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
# File 'lib/rmath3d/rmath3d_plain.rb', line 1736

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.



1831
1832
1833
1834
1835
1836
1837
1838
# File 'lib/rmath3d/rmath3d_plain.rb', line 1831

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.



1477
1478
1479
1480
1481
1482
# File 'lib/rmath3d/rmath3d_plain.rb', line 1477

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.



1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
# File 'lib/rmath3d/rmath3d_plain.rb', line 1340

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.



1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
# File 'lib/rmath3d/rmath3d_plain.rb', line 1520

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.



1465
1466
1467
1468
1469
1470
# File 'lib/rmath3d/rmath3d_plain.rb', line 1465

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



1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
# File 'lib/rmath3d/rmath3d_plain.rb', line 1490

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



1508
1509
1510
1511
1512
1513
# File 'lib/rmath3d/rmath3d_plain.rb', line 1508

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.



2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
# File 'lib/rmath3d/rmath3d_plain.rb', line 2260

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.



1316
1317
1318
# File 'lib/rmath3d/rmath3d_plain.rb', line 1316

def to_a
  return @e
end

#to_sObject

call-seq: to_s

Returns human-readable string.



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

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.



1686
1687
1688
1689
1690
1691
1692
1693
# File 'lib/rmath3d/rmath3d_plain.rb', line 1686

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.



1570
1571
1572
1573
1574
1575
1576
1577
# File 'lib/rmath3d/rmath3d_plain.rb', line 1570

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