[go: up one dir, main page]

Menu

[485645]: / doc / matrixfaq.htm  Maximize  Restore  History

Download this file

2194 lines (1480 with data), 68.4 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 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
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 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
 850
 851
 852
 853
 854
 855
 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
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 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
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
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
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
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
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
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
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
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
1780
1781
1782
1783
1784
1785
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
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Matrix and Quaternion FAQ</title>
<meta name="GENERATOR" content="Microsoft FrontPage 1.1">
</head>
<body>
<pre>The Matrix and Quaternions FAQ
==============================
Version 1.2 2nd September 1997
-------------------------------
This FAQ is maintained by &quot;<a href="mailto:hexapod@netcom.com">hexapod@netcom.com</a>&quot;. Any additional suggestions
or related questions are welcome. Just send E-mail to the above address.
Feel free to distribute or copy this FAQ as you please.
Contributions
-------------
<a href="#I1">Introduction I1</a>: <a href="mailto:steve@mred.bgm.link.com">steve@mred.bgm.link.com</a>
Introduction
============
I1. Important note relating to OpenGL and this document
Questions
---------
BASICS
======
<a href="#Q1">Q1. What is a matrix?</a>
<a href="#Q2">Q2. What is the order of a matrix?</a>
<a href="#Q3">Q3. How do I represent a matrix using the C/C++ programming languages?</a>
<a href="#Q4">Q4. What are the advantages of using matrices?</a>
<a href="#Q5">Q5. How do matrices relate to coordinate systems?</a>
ARITHMETIC
==========
<a href="#Q6">Q6. What is the identity matrix?</a>
<a href="#Q7">Q7. What is the major diagonal matrix of a matrix?</a>
<a href="#Q8">Q8. What is the transpose of a matrix?</a>
<a href="#Q9">Q9. How do I add two matrices together?</a>
<a href="#Q10">Q10. How do I subtract two matrices?</a>
<a href="#Q11">Q11. How do I multiply two matrices together?</a>
<a href="#Q12">Q12. How do I square or raise a matrix to a power?</a>
<a href="#Q13">Q13. How do I multiply one or more vectors by a matrix?</a>
DETERMINANTS AND INVERSES
=========================
<a href="#Q14">Q14. What is the determinant of a matrix?</a>
<a href="#Q15">Q15. How do I calculate the determinant of a matrix?</a>
<a href="#Q16">Q16. What are Isotropic and Anisotropic matrices?</a>
<a href="#Q17">Q17. What is the inverse of a matrix?</a>
<a href="#Q18">Q18. How do I calculate the inverse of an arbitary matrix?</a>
<a href="#Q19">Q19. How do I calculate the inverse of an identity matrix?</a>
<a href="#Q20">Q20. How do I calculate the inverse of a rotation matrix?</a>
<a href="#Q21">Q21. How do I calculate the inverse of a matrix using Kramer's rule?</a>
<a href="#Q22">Q22. How do I calculate the inverse of a 2x2 matrix?</a>
<a href="#Q23">Q23. How do I calculate the inverse of a 3x3 matrix?</a>
<a href="#Q24">Q24. How do I calculate the inverse of a 4x4 matrix?</a>
<a href="#Q25">Q25. How do I calculate the inverse of a matrix using linear equations?</a>
TRANSFORMS
==========
<a href="#Q26">Q26. What is a rotation matrix?</a>
<a href="#Q27">Q27. How do I generate a rotation matrix in the X-axis?</a>
<a href="#Q28">Q28. How do I generate a rotation matrix in the Y-axis?</a>
<a href="#Q29">Q29. How do I generate a rotation matrix in the Z-axis?</a>
<a href="#Q30">Q30. What are Euler angles?</a>
<a href="#Q31">Q31. What are yaw, roll and pitch?</a>
<a href="#Q32">Q32. How do I combine rotation matrices?</a>
<a href="#Q33">Q33. What is Gimbal Lock?</a>
<a href="#Q34">Q34. What is the correct way to combine rotation matrices?</a>
<a href="#Q35">Q35. How do I generate a rotation matrix from Euler angles?</a>
<a href="#Q36">Q36. How do I generate Euler angles from a rotation matrix?</a>
<a href="#Q37">Q37. How do I generate a rotation matrix for a selected axis and angle?</a>
<a href="#Q38">Q38. How do I generate a rotation matrix to map one vector onto another?</a>
<a href="#Q39">Q39. What is a translation matrix?</a>
<a href="#Q40">Q40. What is a scaling matrix?</a>
<a href="#Q41">Q41. What is a shearing matrix?</a>
<a href="#Q42">Q42. How do I perform linear interpolation between two matrices?</a>
<a href="#Q43">Q43. How do I perform cubic interpolation between four matrices?</a>
<a href="#Q44">Q44. How can I render a matrix?</a>
QUATERIONS
==========
<a href="#Q45">Q45. What are quaternions?</a>
<a href="#Q46">Q46. How do quaternions relate to 3D animation?</a>
<a href="#Q47">Q47. How do I convert a quaternion to a rotation matrix?</a>
<a href="#Q48">Q48. How do I convert a rotation matrix to a quaternion?</a>
<a href="#Q49">Q49. How do I convert a rotation axis and angle to a quaternion?</a>
<a href="#Q50">Q50. How do I convert a quaternion to a rotation axis and angle?</a>
<a href="#Q51">Q51. How do I convert a spherical rotation angles to a quaternion?</a>
<a href="#Q52">Q52. How do I convert a quaternion to a spherical rotation angles?</a>
<a href="#Q53">Q53. How do I use quaternions to perform linear interpolation between matrices?</a>
<a href="#Q54">Q54. How do I use quaternions to perform cubic interpolation between matrices?</a>
Introduction
============
<a name="I1">I1</a>. Important note relating to OpenGl and this document
-------------------------------------------------------
In this document (as in most math textbooks), all matrices are drawn
in the standard mathematical manner. Unfortunately graphics libraries
like IrisGL, OpenGL and SGI's Performer all represent them with the
rows and columns swapped.
Hence, in this document you will see (for example) a 4x4 Translation
matrix represented as follows:
| 1 0 0 X |
| |
| 0 1 0 Y |
M = | |
| 0 0 1 Z |
| |
| 0 0 0 1 |
In Performer (for example) this would be populated as follows:
M[0][1] = M[0][2] = M[0][3] =
M[1][0] = M[1][2] = M[1][3] =
M[2][0] = M[2][1] = M[2][3] = 0 ;
M[0][0] = M[1][1] = M[2][2] = m[3][3] = 1 ;
M[3][0] = X ;
M[3][1] = Y ;
M[3][2] = Z ;
ie, the matrix is stored like this:
| M[0][0] M[1][0] M[2][0] M[3][0] |
| |
| M[0][1] M[1][1] M[2][1] M[3][1] |
M = | |
| M[0][2] M[1][2] M[2][2] M[3][2] |
| |
| M[0][3] M[1][3] M[2][3] M[3][3] |
OpenGL uses a one-dimensional array to store matrices - but fortunately,
the packing order results in the same layout of bytes in memory - so
taking the address of a pfMatrix and casting it to a float* will allow
you to pass it directly into routines like glLoadMatrixf.
In the code snippets scattered throughout this document, a one-dimensional
array is used to store a matrix. The ordering of the array elements is
transposed with respect to OpenGL.
This Document OpenGL
| 0 1 2 3 | | 0 4 8 12 |
| | | |
| 4 5 6 7 | | 1 5 9 13 |
M = | | M = | |
| 8 9 10 11 | | 2 6 10 14 |
| | | |
| 12 13 14 15 | | 3 7 11 15 |
Basics
======
<a name="Q1">Q1</a>. What is a matrix?
----------------------
A matrix is a two dimensional array of numeric data, where each
row or column consists of one or more numeric values.
Arithmetic operations which can be performed with matrices include
addition, subtraction, multiplication and division.
The size of a matrix is defined in terms of the number of rows
and columns.
A matrix with M rows and N columns is defined as a MxN matrix.
Individual elements of the matrix are referenced using two index
values. Using mathematical notation these are usually assigned the
variables 'i' and 'j'. The order is row first, column second
For example, if a matrix M with order 4x4 exists, then the elements
of the matrix are indexed by the following row:column pairs:
| 00 10 20 30 |
M = | 01 11 21 31 |
| 02 12 22 32 |
| 03 13 23 33 |
The element at the top right of the matrix has i=0 and j=3
This is referenced as follows:
M = M
i,j 0,3
In computer animation, the most commonly used matrices have either
2, 3 or 4 rows and columns. These are referred to as 2x2, 3x3 and 4x4
matrices respectively.
2x2 matrices are used to perform rotations, shears and other types
of image processing. General purpose NxN matrices can be used to
perform image processing functions such as convolution.
3x3 matrices are used to perform low-budget 3D animation. Operations
such as rotation and multiplication can be performed using matrix
operations, but perspective depth projection is performed using
standard optimised into pure divide operations.
4x4 matrices are used to perform high-end 3D animation. Operations
such as multiplication and perspective depth projection can be
performed using matrix mathematics.
<a name="Q2">Q2</a>. What is the &quot;order&quot; of a matrix?
-------------------------------------
The &quot;order&quot; of a matrix is another name for the size of the matrix.
A matrix with M rows and N columns is said to have order MxN.
<a name="Q3">Q3</a>. How do I represent a matrix using the C/C++ programming languages?
-----------------------------------------------------------------------
The simplest way of defining a matrix using the C/C++ programming
languages is to make use of the &quot;typedef&quot; keyword. Both 3x3 and 4x4
matrices may be defined in this way ie:
typedef float MATRIX3[9];
typedef float MATRIX4[16];
Since each type of matrix has dimensions 3x3 and 4x4, this requires
9 and 16 data elements respectively.
At first glance, the use of a single linear array of data values may
seem counter-intuitive. The use of two dimensional arrays may seem
more convenient ie.
typedef float MATRIX3[3][3];
typedef float MATRIX4[4][4];
However, the use of two reference systems for each matrix element
very often leads to confusion. With mathemetics, the order is row
first (i), column second (j) ie.
Mij
Using C/C++, this becomes
matrix[j][i]
Using two dimensional arrays also incurs a CPU performance penalty in
that C compilers will often make use of multiplication operations to
resolve array index operations.
So, it is more efficient to stick with linear arrays. However, one issue
still remains to be resolved. How is an two dimensional matrix mapped
onto a linear array? Since there are only two methods (row first/column
second or column first/row column).
The performance differences between the two are subtle. If all for-next
loops are unravelled, then there is very little difference in the
performance for operations such as matrix-matrix multiplication.
Using the C/C++ programming languages the linear ordering of each
matrix is as follows:
mat[0] = M mat[3] = M
00 03
mat[12] = M mat[15] = M
30 33
| 0 1 2 3 |
| | | 0 1 2 |
| 4 5 6 7 | | |
M = | | M = | 3 4 5 |
| 8 9 10 11 | | |
| | | 6 7 8 |
| 12 13 14 15 |
<a name="Q4">Q4</a>. What are the advantages of using matrices?
-----------------------------------------------
One of the first questions asked about the use of matrices in computer
animation is why they should be used at all in the first place.
Intuitively, it would appear that the overhead of for-next loops and
matrix multiplication would slow down an application.
Arguments that resolve these objections can be pointed out. These include
the use of CPU registers to handle loop counters on-board data caches
to optimise memory accesses.
Advantages can also be pointed out. By following a mathematical approach
to defining 3D algorithms, it is possible to predict and plan the
design of a 3D animation system. Such mathematical approaches allow
for the implementation of character animation, spline curves and inverse
kinematics.
However, one objection that frequently comes up is that it would be
quicker to just multiply each pair of coordinates by the rotation
coefficients for that axis, rather than perform a full vector-matrix
multiplication.
ie. Rotation in X transforms Y and Z
Rotation in Y transforms X and Z
Rotation in Z transforms X and Y
The argument to this goes as follows:
Given a vertex V = (x,y,z), rotation angles (A,B and C) and translation
(D,E,F). A the algorithm
is defined as follows:
---------------------------
sx = sin(A) // Setup - only done once
cx = cos(A)
sy = sin(B)
cy = cos(B)
sz = sin(C)
cz = cos(C)
x1 = x * cz + y * sz // Rotation of each vertex
y1 = y * cz - x * sz
z1 = z
x2 = x1 * cy + z1 * sy
y2 = z1
z2 = z1 * cy - x1 * sy
x3 = x2
y3 = y2 * cx + z1 * sx
z3 = z2 * cx - x1 * sx
xr = x3 + D // Translation of each vertex
yr = y3 + E
zr = z3 + F
---------------------------
Altogether, this algorithm will use the following amounts of processing
time:
Set-up Per-vertex
------------------------- ------------------------
6 trigonometric functions
6 assignment operations. 12 assignment
12 multiplication
9 addition
------------------------- ------------------------
Assume that the same operations is being performed using matrix
multiplication.
With a 4x4 matrix, the procesing time is used as follows:
Set-up Change Per-vertex Change
-------------------------- ------ ------------------------ ------
6 trigonometric functions 0 0
18 assignment operation -12 3 assignment -9
12 multiplication +12 9 multiplication -3
6 subtraction +6 6 addition -3
-------------------------- ------ ------------------------ ------
Comparing the two tables, it can be seen that setting up a rotation
matrix costs at least 12 multiplication calculations and an extra
18 assignment calls.
However, while this may seem extravagant, the savings come from
processing each vertex. Using matrix multiplication, the savings made
from processing just 4 vertices, will outweigh the additional set-up
cost.
<a name="Q5">Q5</a>. How do matrices relate to coordinate systems?
--------------------------------------------------
With either 3x3 or 4x4 rotation, translation or shearing matrices, there
is a simple relationship between each matrix and the resulting coordinate
system.
The first three columns of the matrix define the direction vector of the
X, Y and Z axii respectively.
If a 4x4 matrix is defined as:
| A B C D |
M = | E F G H |
| I J K L |
| M N O P |
Then the direction vector for each axis is as follows:
X-axis = [ A E I ]
Y-axis = [ B F J ]
Z-axis = [ C G K ]
Arithmetic
==========
<a name="Q6">Q6</a>. What is the identity matrix?
---------------------------------
The identity matrix is matrix in which has an identical number of rows
and columns. Also, all the elements in which i=j are set one. All others
are set to zero. For example a 4x4 identity matrix is as follows:
| 1 0 0 0 |
M = | 0 1 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
<a name="Q7">Q7</a>. What is the major diagonal of a matrix?
--------------------------------------------
The major diagonal of a matrix is the set of elements where the
row number is equal to the column number ie.
M where i=j
ij
In the case of the identity matrix, only the elements on the major
diagonal are set to 1, while all others are set to 0.
<a name="Q8">Q8</a>. What is the transpose of a matrix?
---------------------------------------
The transpose of matrix is the matrix generated when every element in
the matrix is swapped with the opposite relative to the major diagonal
This can be expressed as the mathematical operation:
M' = M
ij ji
However, this can only be performed if a matrix has an equal number
of rows and columns.
If the matrix M is defined as:
| 0.707 -0.866 |
M = | |
| 0.866 0.707 |
Then the transpose is equal to:
| 0.707 0.866 |
T = | |
| -0.866 0.707 |
If the matrix is a rotation matrix, then the transpose is guaranteed
to be the inverse of the matrix.
<a name="Q9">Q9</a>. How do I add two matrices together?
----------------------------------------
The rule of thumb with adding two matrices together is:
&quot;add row and column to row and column&quot;
This can be expressed mathematically as:
R = M + L
ij ij ij
However, both matrices must be identical in size.
For example, if the 2x2 matrix M is added with the 2x2 matrix L then
the result is as follow:
R = M + L
| A B C | | J K L |
| | | |
= | D E F | + | M N O |
| | | |
| G H I | | P Q R |
| A+J B+K C+L |
| |
= | D+M E+N F+O |
| |
| G+P H+Q I+R |
<a name="Q10">Q10</a>. How do I subtract two matrices?
-------------------------------------
The rule of thumb with subtracting two matrices is:
&quot;subtract row and column from row and column&quot;
This can be expressed mathematically as:
R = M - L
ij ij ij
However, both matrices must be identical in size.
For example, if the 2x2 matrix L is subtracted from the 2x2 matrix M then
the result is as follows:
R = M - L
| A B C | | J K L |
| | | |
= | D E F | - | M N O |
| | | |
| G H I | | P Q R |
| A-J B-K C-L |
| |
= | D-M E-N F-O |
| |
| G-P H-Q I-R |
<a name="Q11">Q11</a>. How do I multiply two matrices together?
---------------------------------------------
The rule of thumb with multiplying two matrices together is:
&quot;multiply row into column and sum the result&quot;.
This can be expressed mathematically as:
n
--
R = \ M x L
ij / ij ji
--
i=1
If the two matrices to be multiplied together have orders:
M = AxB and L = CxD
then the two values B and C must be identical.
Also, the resulting matrix has an order of AxD
Thus, it is possible to multiply a 4xN matrix with a 4x4 matrix
but not the other way around.
For example, if the 4x4 matrix M is defined as:
| A B C D |
M = | E F G H |
| I J K L |
| M N O P |
and a 4x2 matrix L is defined as:
L = | Q R |
| S T |
| U V |
| W X |
then the size of the resulting matrix is 2x4. The resulting matrix
is defined as:
R = M x L
| A B C D | | Q R |
= | E F G H | x | S T |
| I J K L | | U V |
| M N O P | | W X |
| AQ+BS+CU+DW AR+BT+CV+DX |
= | EQ+FS+GU+HW ER+FT+GV+HX |
| IQ+JS+KU+LW IR+JT+KV+LX |
| MQ+NS+OU+PW MR+NT+OV+PX |
<a name="Q12">Q12</a>. How do I square or raise a matrix to a power?
--------------------------------------------------
A matrix may be squared or even raised to an integer power. However
there are several restrictions. For all powers, the matrix must be
orthogonal ie. have the same width and height
For example,
-1
M is the inverse of the matrix
0
M generates the identity matrix
1
M leaves the matrix undamaged.
2
M squares the matrix and
3
M generates the cube of the matrix
Raising a matrix to a power greater than one involves multiplying a matrix
by itself a specific number of times.
For example,
2
M = M . M
3
M = M . M . M
and so on.
Raising the identity matrix to any power always generates the identity
matrix ie.
n
I = I
<a name="Q13">Q13</a>. How do I multiply one or more vectors by a matrix?
-------------------------------------------------------
The best way to perform this task is to treat the list of vectors as
a single matrix, with each vector represented as a column vector.
If N vectors are to be multiplied by a 4x4 matrix, then they can be
treated as a single 4xN matrix:
If the matrix is defined as:
| A B C D |
M = | E F G H |
| I J K L |
| M N O P |
and the list of vectors is defined as:
| x1 x2 x3 x4 x5|
V = | y1 y2 y3 y4 y5|
| z1 z2 z3 z4 z5|
| 1 1 1 1 |
Note that an additional row of constant terms is added to the vector
list, all of which are set to 1.0. In real life, this row does not
exist. It is simply used to make the orders of the matrix M and the
vector list V match.
Then the multiplication is performed as follows:
M . V = V'
| A B C D | | x1 x2 x3 x4 x5 | | A.x1+B.y1+C.z1+D A.x2+B.y2+C.z2+D ... |
| E F G H | . | y1 y2 y3 y4 y5 | = | E.x1+F.y1+G.z1+H E.x2+F.y2+G.z2+H ... |
| I J K L | | z1 z2 y3 y4 z5 | | I.x1+J.y1+K.z1+L I.x2+J.y2+K.z2+L ... |
| M N O P | | 1 1 1 1 1 | | M.x1+N.y1+O.z1+P M.x2+N.y2+O.z2+P ... |
For each vector in the list there will be a total of 12 multiplication
16 addition and 1 division operation (for perspective).
If the matrix is known not to be a rotation or translation matrix then the
division operation can be skipped.
Determinants and Inverses
=========================
<a name="Q14">Q14</a>. What is the determinant of a matrix?
-----------------------------------------
The determinant of a matrix is a floating point value which is used to
indicate whether the matrix has an inverse or not. If negative, then
no inverse exists. If the determinant is positive, then an inverse
exists.
For an identity matrix, the determinant is always equal to one.
Any matrix with a determinant of 1.0 is said to be isotropic.
Thus all rotation matrices are said to be isotropic, since the
determinant is always equal to 1.0.
This can be proved as follows:
| A B | | cos X -sin X |
M = | | = | |
| C D | | sin X cos X |
D = AD - BC
D = (cos X . cos X) - (-sin X . sin X)
2 2
D = (cos X ) + (sin X)
2 2
But, cos X + sin X = 1
Therefore,
D = 1
<a name="Q15">Q15</a>. How do I calculate the determinant of a matrix?
----------------------------------------------------
The determinant of a matrix is calculated using Kramer's rule, where
the value can be calculated by breaking the matrix into smaller
matrices.
For a 2x2 matrix M, the determinant D is calculated as follows:
| A B |
M = | |
| C D |
D = AD - BC
For 3x3 and 4x4 matrices, this is more complicated, but can be solved
by methods such as Kramer's Rule.
<a name="Q16">Q16</a>. What are Isotropic and Anisotropic matrices?
-------------------------------------------------
An Isotropic matrix is one in which the sum of the squares of all
three rows or columns add up to one.
A matrix in which this is not the case, is said to be Anisotropic.
When 3x3 or 4x4 matrices are used to rotate and scale an object, it
is sometimes necessary to enlarge or shrink one axis more than the
others.
For example, with seismic surveys, it is convenient to enlarge the
Z-axis by a factor or 50 or more, while letting the X and Y axii
remain the same.
Another example is the implementation of &quot;squash&quot; and &quot;stretch&quot;
with character animation. When a character is hit by a heavy object
eg. an anvil, the desired effect is to character stretched out
sideways and squashed vertically:
A suitable matrix would be as follows:
| 2 0 0 0 |
M = | 0 2 0 0 |
| 0 0 0.5 0 |
| 0 0 0 1 |
However, there is problem looming ahead. While this matrix will cause
no problems with the transformation of vertex data, it will cause
problems with gouraud shading using outward normals.
Because the transformation stage is implemented using matrix
multiplication, both vertex data and outward normal data will be
multiplied with this matrix.
While this is not a problem with vertex data (it is the desired effect)
it causes a major headache with the outward normal data.
After raw multiplication, each outward normal will no longer be
normalised and consequently will affect other calculations such as
shading and back-face culling.
<a name="Q17">Q17</a>. What is the inverse of a matrix?
-------------------------------------
-1
Given a matrix M, then the inverse of that matrix, denoted as M , is
the matrix which satisfies the following expression:
-1
M . M = I
where I is the identity matrix.
Thus, multiplying a matrix with its inverse will generate the identity
matrix. However, several requirements must be satisfied before the
inverse of a matrix can be calculated.
These include that the width and height of the matrix are identical and
that the determinant of the matrix is non-zero.
Calculating the inverse of a matrix is a task often performed in order
to implement inverse kinematics using spline curves.
<a name="Q18">Q18</a>. How do I calculate the inverse of an arbitary matrix?
----------------------------------------------------------
Depending upon the size of the matrix, the calculation of the inverse
can be trivial or extremely complicated.
For example, the inverse of a 1x1 matrix is simply the reciprical of
the single element:
ie. M = | x |
Then the inverse is defined as:
-1 | 1 |
M = | - |
| x |
Solving 2x2 matrices and larger can be achieved by using Kramer's Rule
or by solving as a set of simultaneous equations.
However, in certain cases, such as identity or rotation matrices, the
inverse is already known or can be determined from taking the transpose
of the matrix.
<a name="Q19">Q19</a>. How do I calculate the inverse of an identity matrix?
----------------------------------------------------------
Don't even bother. The inverse of an identity matrix is the identity
matrix. ie.
-1
I . I = I
Any identity matrix will always have a determinant of +1.
<a name="Q20">Q20</a>. How do I calculate the inverse of a rotation matrix?
---------------------------------------------------------
Since a rotation matrix always generates a determinant of +1,
calculating the inverse is equivalent of calculating the transpose.
Alternatively, if the rotation angle is known, then the rotation
angle can be negated and used to calculate a new rotation matrix.
<a name="Q21">Q21</a>. How do I calculate the inverse of a matrix using Kramer's rule?
--------------------------------------------------------------------
Given a 3x3 matrix M:
| A B C |
| |
M = | D E F |
| |
| G H I |
Then the determinant is calculated as follows:
n
---
\ i
det M = / M * submat M * -1
--- 0,i 0,i
i=1
where
submat M defines the matrix composed of all rows and columns of M
ij
excluding row i and column j. submat may be called recursively.
ij
If the determinant is non-zero then the inverse of the matrix exists.
In this case, the value of each matrix element is defined by:
-1 1 i+j
M = ----- * det submat M * -1
j,i det M i,j
<a name="Q22">Q22</a>. How do I calculate the inverse of a 2x2 matrix?
----------------------------------------------------
For a 2x2 matrix, the calculation is slightly harder. If the matrix is
defined as follows:
| A B |
M = | |
| C D |
Then the determinant is defined as:
det = AD - BC
And the inverse is defined as:
-1 1 | D -B |
M = --- | |
det | -C A |
This can be proved using Kramer's rule. Given the matrix M:
| A B |
M = | |
| C D |
Then the determinant is:
0 1
det = M * submat M * -1 + M * submat M * -1
0,0 0,0 0,1 0,1
&lt;=&gt; M * M * 1 + M * M * -1
0,0 1,1 0,1 1,0
&lt;=&gt; A * D + B * C * -1
&lt;=&gt; AD + BC . -1
&lt;=&gt; AD - BC
==============
And the inverse is derived from:
-1 0+0 -1
M = det submat * -1 &lt;=&gt; M = M * 1 &lt;=&gt; D
0,0 0,0 0,0 1,1
-1 1+0 -1
M = det submat * -1 &lt;=&gt; M = M * -1 &lt;=&gt; C * -1
0,1 1,0 0,1 1,0
-1 0+1 -1
M = det submat * -1 &lt;=&gt; M = M * -1 &lt;=&gt; B * -1
1,0 0,1 1,0 0,1
-1 1+1 -1
M = det submat * -1 &lt;=&gt; M = M * 1 &lt;=&gt; A
1,1 1,1 1,1 0,0
Then the inverse matrix is equal to:
-1 1 | D -C |
M = --- | |
det | -B A |
Providing that the determinant is not zero.
<a name="Q23">Q23</a>. How do I calculate the inverse of a 3x3 matrix?
----------------------------------------------------
For 3x3 matrices and larger, the inverse can be calculated by
either applying Kramer's rule or by solving as a set of linear
equations.
If Kramer's rule is applied to a matrix M:
| A B C |
M = | D E F |
| G H I |
then the determinant is calculated as follows:
det M = A * (EI - HF) - B * (DI - GF) + C * (DH - GE)
Providing that the determinant is non-zero, then the inverse is
calculated as:
-1 1 | EI-FH -(BI-HC) BF-EC |
M = ----- . | -(DI-FG) AI-GC -(AF-DC) |
det M | DH-GE -(AH-GB) AE-BD |
This can be implemented using a pair of 'C' functions:
---------------------------------
VFLOAT m3_det( MATRIX3 mat )
{
VFLOAT det;
det = mat[0] * ( mat[4]*mat[8] - mat[7]*mat[5] )
- mat[1] * ( mat[3]*mat[8] - mat[6]*mat[5] )
+ mat[2] * ( mat[3]*mat[7] - mat[6]*mat[4] );
return( det );
}
----------------------------------
void m3_inverse( MATRIX3 mr, MATRIX3 ma )
{
VFLOAT det = m3_det( ma );
if ( fabs( det ) &lt; 0.0005 )
{
m3_identity( ma );
return;
}
mr[0] = ma[4]*ma[8] - ma[5]*ma[7] / det;
mr[1] = -( ma[1]*ma[8] - ma[7]*ma[2] ) / det;
mr[2] = ma[1]*ma[5] - ma[4]*ma[2] / det;
mr[3] = -( ma[3]*ma[8] - ma[5]*ma[6] ) / det;
mr[4] = ma[0]*ma[8] - ma[6]*ma[2] / det;
mr[5] = -( ma[0]*ma[5] - ma[3]*ma[2] ) / det;
mr[6] = ma[3]*ma[7] - ma[6]*ma[4] / det;
mr[7] = -( ma[0]*ma[7] - ma[6]*ma[1] ) / det;
mr[8] = ma[0]*ma[4] - ma[1]*ma[3] / det;
}
---------------------------------
<a name="Q24">Q24</a>. How do I calculate the inverse of a 4x4 matrix?
----------------------------------------------------
As with 3x3 matrices, either Kramer's rule can be applied or the
matrix can be solved as a set of linear equations.
An efficient way is to make use of the existing 'C' functions defined
to calculate the determinant and inverse of a 3x3 matrix.
In order to implement Kramer's rule with 4x4 matrices, it is necessary
to determine individual sub-matrices. This is achieved by the following
routine:
--------------------------
void m4_submat( MATRIX4 mr, MATRIX3 mb, int i, int j )
{
int ti, tj, idst, jdst;
for ( ti = 0; ti &lt; 4; ti++ )
{
if ( ti &lt; i )
idst = ti;
else
if ( ti &gt; i )
idst = ti-1;
for ( tj = 0; tj &lt; 4; tj++ )
{
if ( tj &lt; j )
jdst = tj;
else
if ( tj &gt; j )
jdst = tj-1;
if ( ti != i &amp;&amp; tj != j )
mb[idst*3 + jdst] = mr[ti*4 + tj ];
}
}
}
--------------------------
The determinant of a 4x4 matrix can be calculated as follows:
--------------------------
VFLOAT m4_det( MATRIX4 mr )
{
VFLOAT det, result = 0, i = 1;
MATRIX3 msub3;
int n;
for ( n = 0; n &lt; 4; n++, i *= -1 )
{
m4_submat( mr, msub3, 0, n );
det = m3_det( msub3 );
result += mr[n] * det * i;
}
return( result );
}
--------------------------
And the inverse can be calculated as follows:
--------------------------
int m4_inverse( MATRIX4 mr, MATRIX4 ma )
{
VFLOAT mdet = m4_det( ma );
MATRIX3 mtemp;
int i, j, sign;
if ( fabs( mdet ) &lt; 0.0005 )
return( 0 );
for ( i = 0; i &lt; 4; i++ )
for ( j = 0; j &lt; 4; j++ )
{
sign = 1 - ( (i +j) % 2 ) * 2;
m4_submat( ma, mtemp, i, j );
mr[i+j*4] = ( m3_det( mtemp ) * sign ) / mdet;
}
return( 1 );
}
--------------------------
Having a function that can calculate the inverse of any 4x4 matrix is
an incredibly useful tool. Application include being able to calculate
the base matrix for splines, inverse rotations and rearranging matrix
equations.
<a name="Q25">Q25</a>. How do I calculate the inverse of a matrix using linear equations?
-----------------------------------------------------------------------
If a matrix M exists, such that:
| A B C |
M = | D E F |
| G H I |
then the inverse exists:
| P Q R |
M' = | S T U |
| V W X |
and the following expression is valid:
-1
M . M = I
| A B C | | P Q R | | 1 0 0 |
| D E F | . | S T U | = | 0 1 0 |
| G H I | | V W X | | 0 0 1 |
The inverse can then be calculated through the solution as a set of
linear equations ie.:
| AP + BS + CV | | 1 | Column 0 (X)
| DP + ES + FV | = | 0 |
| GP + HS + IV | | 0 |
| AQ + BT + CW | | 0 | Column 1 (Y)
| DQ + ET + FW | = | 1 |
| GQ + HT + IW | | 0 |
| AR + BU + CX | | 0 | Column 2 (Z)
| DR + EU + FX | = | 0 |
| GR + HU + IX | | 1 |
Transforms
==========
<a name="Q26">Q26</a>. What is a rotation matrix?
-------------------------------
A rotation matrix is used to rotate a set of points within a
coordinate system. While the individual points are assigned new
coordinates, their relative distances do not change.
All rotations are defined using the trigonometric &quot;sine&quot; and &quot;cosine&quot;
functions.
For a two-dimensional coordinate system, the rotation matrix is as
follows:
| cos(A) -sin(A) |
| |
| sin(A) cos(A) |
With the rotation angle A set to zero, this generates the identity
matrix:
| 1 0 |
I = | |
| 0 1 |
If the rotation is set to +90 degrees, then the matrix is as follows:
| 0 -1 |
M = | |
| 1 0 |
If the rotation is set to -90 degrees, then the matrix is as follows:
| 0 1 |
M = | |
| -1 0 |
Negating the rotation angle is equivalent to generating the transpose
of the matrix.
If a rotation matrix is multiplied with its transpose, the result is
the identity matrix.
<a name="Q27">Q27</a>. How do I generate a rotation matrix in the X-axis?
-------------------------------------------------------
Use the 4x4 matrix:
| 1 0 0 0 |
M = | 0 cos(A) -sin(A) 0 |
| 0 sin(A) cos(A) 0 |
| 0 0 0 1 |
<a name="Q28">Q28</a>. How do I generate a rotation matrix in the Y-axis?
-------------------------------------------------------
Use the 4x4 matrix:
| cos(A) 0 -sin(A) 0 |
M = | 0 1 0 0 |
| sin(A) 0 cos(A) 0 |
| 0 0 0 1 |
<a name="Q29">Q29</a>. How do I generate a rotation matrix in the Z-axis?
-------------------------------------------------------
Use the 4x4 matrix:
| cos(A) -sin(A) 0 0 |
M = | sin(A) cos(A) 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
<a name="Q30">Q30</a>. What are Euler angles?
---------------------------
Euler angles are the name given to the set of rotation angles which
specify the rotation in each of the X, Y and Z rotation axii.
These are specfied in vector format eg. |x y z| and can be stored
as a VECTOR data structure.
For example, the set
| 0 0 0 | will always generate the identity matrix.
Other angles are represented as follows:
| 90 0 0 | is a rotation of +90 degrees in the X-axis.
| 0 90 0 | is a rotation of +90 degrees in the Y-axis and
| 0 0 90 | is a rotation of +90 degrees in the Z-axis.
Euler angles can be represented using a single vector data structure.
<a name="Q31">Q31</a>. What are Yaw, Roll and Pitch?
----------------------------------
Yaw, Roll and Pitch are aeronautical terms for rotation using the
Euclidean coordinate system (Euler angles), relative to the local
coordinate system of an aeroplane.
Imagine you are viewing an aeroplane from above and from directly behind.
The Z-axis is lined up with the tail and nose of the aeroplane.
The X-axis runs from the tip of the left wing to the tip of the right
wing.
The Y axis points straight up from the ground.
Pitch then becomes rotation in the X-axis, Yaw becomes rotation in the
Y-axis and Roll becomes rotation in the Z-axis.
<a name="Q32">Q32</a>. How do I combine rotation matrices?
----------------------------------------
Rotation matrices are combined together using matrix multiplication.
As a result, the order of multiplication is very important.
<a name="Q33">Q33</a>. What is Gimbal lock?
-------------------------
Gimbal lock is the name given to a problem that occurs with the use of
Euler angles. Because the final rotation matrix depends on the order
of multiplication, it is sometimes the case that the rotation in one
axis will be mapped onto another rotation axis.
Even worse, it may become impossible to rotate an object in a desired
axis. This is called Gimbal lock.
For example, assume that an object is being rotated in the order Z,Y,X
and that the rotation in the Y-axis is 90 degrees.
In this case, rotation in the Z-axis is performed first and therefore
correctly. The Y-axis is also rotated correctly. However, after
rotation in the Y axis, the X-axis is rotated onto the Z-axis.
Thus, any rotation in the X-axis actually rotates the object in the
Z-axis. Even worse, it becomes to rotate the object in the X-axis.
The only solution to this problem is to make use of Quaternions.
<a name="Q34">Q34</a>. What is the correct way to combine rotation matrices?
----------------------------------------------------------
Really, there is no &quot;correct way&quot; of combining rotation matrices.
However, in order to be able to predict the result of combining
matrices together, some organisation is required. This is also
necessary if a full 3D matrix library is to be built.
The simplest way to rotate an object is to multiply the matrices
using the order:
M = X.Y.Z
where M is the final rotation matrix, and X,Y,Z are the individual
rotation matrices. This defines a rotation in the X-axis (pitch) first,
followed by the Y-axis (yaw) and a final rotation in the Z-axis (roll).
However, whenever the view from the camera viewpoint is being
evaluated, then the order and signs of the rotation is reversed.
For example, if you are standing up, and turn to your left, everything
in your field of view appears to move towards the right.
However, someone else facing you will say that you turned towards their
right.
Thus the view from the camera is modelled using the order:
M = -Z.-Y.-X
This is the inverse (or transpose) of the rotation matrix generated
if the camera were being rendered as another object.
<a name="Q35">Q35</a>. How do I generate a rotation matrix from Euler angles?
-----------------------------------------------------------
At first glance, the most obvious method to generate a rotation matrix
from a set of Euler angles is to generate each matrix individually and
multiply all three together ie.
m3_rotx( mat_x, vec -&gt; angle_x );
m3_roty( mat_y, vec -&gt; angle_y );
m3_rotz( mat_z, vec -&gt; angle_z );
m3_mult( mat_tmp, mat_z, mat_y );
m3_mult( mat_final, mat_tmp, mat_x );
This set of calls could be placed in a separate routine eg.
m3_fromeuler( MATRIX *mat_final, VECTOR3 *euler )
However, to perform this sequence of calls is very wasteful in terms
of processing time. Given that each 4x4 rotation matrix is guaranteed
to have 10 elements with value zero (0), 2 elements with value one (1)
and four others of arbitary value, over 75% of every matrix operation
is wasted. This does not include the set up and initialisation of each
matrix.
Altogether, over 75% of all matrix operations are spent processing
arithmetic expressions which lead to either zero or one.
A more efficient way must be found. Fortunately, there is another way
of determining the final resulting matrix.
If all three matrices are combined in algebraic format, the following
expression is defined:
M = X.Y.Z
where M is the final matrix,
X is the rotation matrix for the X-axis,
Y is the rotation matrix for the Y-axis,
Z is the rotation matrix for the Z-axis.
Expanding into rotation matrices in algebraic format gives:
| 1 0 0 |
X = | 0 A -B |
| 0 B A |
| C 0 -D |
Y = | 0 1 0 |
| D 0 C |
| E -F 0 |
Z = | F E 0 |
| 0 0 1 |
where A,B are the cosine and sine of the X-axis rotation axis,
C,D are the cosine and sine of the Y-axis rotation axis,
E,F are the cosine and sine of the Z-axis rotation axis.
Then the expression:
M = X.Y.Z
can be split into two matrix multiplications:
M' = X.Y
M = M'.Z
Evaluating M' first:
M' = X.Y
| 1 0 0 | | C 0 -D |
M' = | 0 A -B | . | 0 1 0 |
| 0 B A | | D 0 C |
| 1.C + 0.0 + 0.D 1.0 + 0.1 + 0.0 1.-D + 0.0 + 0.C |
M' = | 0.C + A.0 + -B.D 0.0 + A.1 + -B.0 0.-D + A.0 + -B.C |
| 0.C + B.0 + A.D 0.0 + B.1 + A.0 0.-D + B.0 + A.C |
Simplifying M' gives:
| C 0 -D |
M' = | -B.D A -B.C |
| A.D B A.C |
Evaluating M gives:
M = M'.Z
| C 0 -D | | E -F 0 |
M = | -BD A -BC | . | F E 0 |
| AD B AC | | 0 0 1 |
| C.E + 0.F + -D.0 C.-F + 0.E + -D.0 C.0 + 0.0 + -D.1 |
M = | -BD.E + A.F + -BC.0 -BD.-F + A.E + -BC.0 -BD.0 + A.0 + -BC.1 |
| AD.E + B.F + AC.0 AD.-F + B.E + AC.0 AD.0 + 0.0 + AC.1 |
Simplifying M gives a 3x3 matrix:
| CE -CF -D |
M = | -BDE+AF -BDF+AE -BC |
| ADE+BF -ADF+BE AC |
This is the final rotation matrix. As a 4x4 matrix this is:
| CE -CF -D 0 |
M = | -BDE+AF BDF+AE -BC 0 |
| ADE+BF -ADF+BE AC 0 |
| 0 0 0 1 |
The individual values of A,B,C,D,E and F are evaluated first. Also, the
values of BD and AD are also evaluated since they occur more than once.
Thus, the final algorithm is as follows:
-----------------------
A = cos(angle_x);
B = sin(angle_x);
C = cos(angle_y);
D = sin(angle_y);
E = cos(angle_z);
F = sin(angle_z);
AD = A * D;
BD = B * D;
mat[0] = C * E;
mat[1] = -C * F;
mat[2] = -D;
mat[4] = -BD * E + A * F;
mat[5] = BD * F + A * E;
mat[6] = -B * C;
mat[8] = AD * E + B * F;
mat[9] = -AD * F + B * E;
mat[10] = A * C;
mat[3] = mat[7] = mat[11] = mat[12] = mat[13] = mat[14] = 0;
mat[15] = 1;
-----------------------
Using basic matrix calculations, the operation count would reach
128 multiplications, 96 additions and 80 assignments operations.
Using the optimised algorithm, only 12 multiplications, 6 subtractions
and 18 assignment operations are required.
So, it is obvious that by using the optimised algorithm, a performance
achievement of 1000% is achieved!
<a name="Q36">Q36</a>. How do I convert a rotation matrix to Euler angles?
--------------------------------------------------------
This operation is the exact opposite to the one answered in the question
above. Given that the rotation matrix is:
| CE -CF -D 0 |
M = | -BDE+AF BDF+AE -BC 0 |
| ADE+BF -ADF+BE AC 0 |
| 0 0 0 1 |
where A,B are the cosine and sine of the X-axis rotation axis,
C,D are the cosine and sine of the Y-axis rotation axis,
E,F are the cosine and sine of the Z-axis rotation axis.
-----------------------
Then the entire process can be reversed:
-----------------------------------
angle_y = D = -asin( mat[2]);
C = cos( angle_y );
angle_y *= RADIANS;
if ( fabs( angle_y ) &gt; 0.0005 )
{
trx = mat[10] / C;
try = -mat[6] / C;
angle_x = atan2( try, trx ) * RADIANS;
trx = mat[0] / C;
try = -mat[1] / C;
angle_z = atan2( try, trx ) * RADIANS;
}
else
{
angle_x = 0;
trx = mat[5];
try = mat[4];
angle_z = atan2( try, trx ) * RADIANS;
}
angle_x = clamp( angle_x, 0, 360 );
angle_y = clamp( angle_y, 0, 360 );
angle_z = clamp( angle_z, 0, 360 );
-----------------------------------
<a name="Q37">Q37</a>. How do I generate a rotation matrix for a selected axis and angle?
-----------------------------------------------------------------------
The only way to generate this type of rotation matrix is through the
use of quaternion mathematics.
See question [Q47. How do I convert a quaternion to a rotation matrix?]
for further details.
<a name="Q38">Q38</a>. How do I generate a rotation matrix to map one vector onto another?
------------------------------------------------------------------------
When developing animation software, a common requirement is to find
a rotation matrix that will map one direction vector onto another.
This problem may be visualised by considering the two direction
vectors to be attached at their starting points. Then the entire
rotation space forms a unit sphere.
In theory, there are an infinite number of rotation axii and angles
that will map one vector onto the other. All of these axii lie on the
plane where all of the points are the exact same distance from both
vectors.
However, only one solution is of practical interest. This is the path
which covers the shortest angular distance between the two vectors.
The rotation axis to this path is calculated by taking the cross
product between the two vectors:
Vaxis = Vs x Vf
The rotation angle is calculated by taking the dot product between the
two vectors:
-1
Vangle = cos ( Vs . Vf )
One practical application of the solution to this problem is finding
the shortest flight path between two cities. In this case, each city
is represented as a direction vector generated from spherical
coordinates. Since planet Earth is spherical, the desired flight path
is the shortest angular rotation between the two cities.
<a name="Q39">Q39</a>. What is a translation matrix?
----------------------------------
A translation matrix is used to position an object within 3D space
without rotating in any way. Translation operations using matrix
multiplication can only be performed using 4x4 matrices.
If the translation is defined by the vector [X Y Z ], then the 4x4
matrix to implement translation is as follows:
| 1 0 0 X |
| |
| 0 1 0 Y |
M = | |
| 0 0 1 Z |
| |
| 0 0 0 1 |
If the vector is [0 0 0] then the vertex list will remain as before.
<a name="Q40">Q40</a>. What is a scaling matrix?
------------------------------
A scaling matrix is used to enlarge or shrink the size of a 3D
model.
If the scaling vector is [X Y Z] then the matrix to perform this is
as follows:
| X 0 0 0 |
| |
| 0 Y 0 0 |
M = | |
| 0 0 Z 0 |
| |
| 0 0 0 1 |
If the scaling vector is [1 1 1], then this generates the identity
matrix and vertex geometry will remain unchanged.
<a name="Q41">Q41</a>. What is a shearing matrix?
-------------------------------
A shearing matrix is used to make a 3D model appear to slant sideways.
For example, &quot;italic&quot; text requires each character to slant towards the
right.
In three dimensions six possible shearing directions exist:
o shear X by Y
o shear X by Z
o shear Y by X
o shear Y by Z
o shear Z by X
o shear Z by Y
All six shearing directions may be combined into a single matrix:
| 1 Syx Szx 0 |
| |
| Sxy 1 Szy 0 |
M = | |
| Sxz Syz 1 0 |
| |
| 0 0 0 1 |
| |
Where Sij implements a shear of I by J
Thus, Sxy shears X by Y
In theory, rotation in three dimensions may be considered a combination
of six shearing directions.
<a name="Q42">Q42</a>. How do I perform linear interpolation between two matrices?
----------------------------------------------------------------
Given two rotation matrices, the problem is to find a way of
determining intermediate positions specified by a parametric
variable t, where t ranges from 0.0 to 1.0
This can be achieved by converting the two matrices into either
Euler angles or Spherical rotation angles (via quaternions) and
a translation vector.
In either case, each matrix is converted into a pair of 3D vectors.
Interpolation between these two vectors can then be performed
through the use of the standard linear interpolation equation:
Vr = Va + t .(Vb - Va )
where Vr is the resulting vector
Va is the start position vector
Vb is the final position vector
This equation may be applied to both translation and rotation
vectors.
Once determined, the resulting translation and rotation are then
converted back into the desired intermediate matrix.
<a name="Q43">Q43</a>. How do I perform cubic interpolation between four matrices?
----------------------------------------------------------------
Given four rotation or translation matrices, the problem is to
find a way of determining intermediate positions specified by a
parametric variable t.
This can be achieved by making use of cubic interpolation. As with
linear interpolation, the four matrices are converted into their
corresponding translation and rotation vectors (Again, either Euler
angles or spherical rotation angles).
Each set of four vectors is then converted into a single geometry
vector G. Through the use of spline mathematics, this geometry vector
is converted into an interpolation matrix M.
If the geometry vector is defined as:
| x1 x2 x3 x4 |
G = | y1 y2 y3 y4 |
| z1 z2 z3 z4 |
Then multiplication by the base matrix:
| -4.5 9.0 -5.5 1.0 |
Mb = | 13.5 -22.5 9.0 0.0 |
| -13.5 18.0 -4.5 0.0 |
| 4.5 -4.5 1.0 0.0 |
will generate the 3x4 interpolation matrix Mi:
Mi = G .Mb
This can be implemented through a standard matrix-vector multiplication.
Interpolation can then be performed by the use of the parametric
variable t:
R = Mi . t
|t^3|
| xr | | A B C D | |t^2|
| yr | = | E F G H | . |t |
| zr | | I J K L | |1 |
The result vector can then be converted back into a rotation or
translation matrix.
It should be noted that the rotation paths that are generated may
occasionally become rather loopy. This is normal, as the algorithm
is trying to find the path with the least amount of rotation between
all four vectors.
Of the two methods, spherical rotation angles will usually be seen to
provide the cleanest interpolation paths for rotation.
<a name="Q44">Q44</a>. How can I render a matrix?
-------------------------------
When using a graphics window for 3D animation, it is convenient to be
able to view a rotation matrix concurrently with the animation.
However, displaying a rotation matrix as an array of numeric values
does not provide a very meaningful context.
An alternative to rendering numeric data is to make use of graphical
display methods such as bar-graphs.
Much like a graphic equalizer on a stereo, a rotation matrix may be
displayed in a bar graph format. Each element of the rotation matrix
is rendered as an individual bar-graph in the range -1 to +1.
A 3x3 matrix would look like the following:
+--+ +--+ +--+
|##| | | | |
+--+ +--+ +--+
| | | | | |
+--+ +--+ +--+
+--+ +--+ +--+
| | |##| | |
+--+ +--+ +--+
| | | | | |
+--+ +--+ +--+
+--+ +--+ +--+
| | | | |##|
+--+ +--+ +--+
| | | | | |
+--+ +--+ +--+
In this case, the rotation matrix is the identity matrix, since each
element in the major diagonal is +1, and all others are zero.
For added visual clarity, parameters which are negative may shaded
in a different colour than those which are positive.
QUATERNIONS
===========
<a name="Q45">Q45</a>. What are quaternions?
--------------------------
Quaternions extend the concept of rotation in three dimensions to
rotation in four dimensions. This avoids the problem of &quot;gimbal-lock&quot;
and allows for the implementation of smooth and continuous rotation.
In effect, they may be considered to add a additional rotation angle
to spherical coordinates ie. Longitude, Latitude and Rotation angles
A Quaternion is defined using four floating point values |x y z w|.
These are calculated from the combination of the three coordinates
of the rotation axis and the rotation angle.
<a name="Q46">Q46</a>. How do quaternions relate to 3D animation?
-----------------------------------------------
As mentioned before, Euler angles have the disadvantage of being
susceptible to &quot;Gimbal lock&quot; where attempts to rotate an
object fail due to the order in which the rotations are performed.
Quaternions are a solution to this problem. Instead of rotating an
object through a series of successive rotations, a quaternion allows
the programmer to rotate an object through a single arbitary rotation
axis.
Because the rotation axis is specifed as a unit direction vector,
it may be calculated through vector mathematics or from spherical
coordinates ie (longitude/latitude).
Quaternions offer another advantage in that they be interpolated.
This allows for smooth and predictable rotation effects.
<a name="Q47">Q47</a>. How do I convert a quaternion to a rotation matrix?
--------------------------------------------------------
Assuming that a quaternion has been created in the form:
Q = |X Y Z W|
Then the quaternion can then be converted into a 4x4 rotation
matrix using the following expression:
| 2 2 |
| 1 - 2Y - 2Z 2XY - 2ZW 2XZ + 2YW |
| |
| 2 2 |
M = | 2XY + 2ZW 1 - 2X - 2Z 2YZ - 2XW |
| |
| 2 2 |
| 2XZ - 2YW 2YZ + 2XW 1 - 2X - 2Y |
| |
If a 4x4 matrix is required, then the bottom row and right-most column
may be added.
The matrix may be generated using the following expression:
----------------
xx = X * X;
xy = X * Y;
xz = X * Z;
xw = X * W;
yy = Y * Y;
yz = Y * Z;
yw = Y * W;
zz = Z * Z;
zw = Z * W;
mat[0] = 1 - 2 * ( yy + zz );
mat[1] = 2 * ( xy - zw );
mat[2] = 2 * ( xz + yw );
mat[4] = 2 * ( xy + zw );
mat[5] = 1 - 2 * ( xx + zz );
mat[6] = 2 * ( yz - xw );
mat[8] = 2 * ( xz - yw );
mat[9] = 2 * ( yz + xw );
mat[10] = 1 - 2 * ( xx + yy );
mat[3] = mat[7] = mat[11 = mat[12] = mat[13] = mat[14] = 0;
mat[15] = 1;
----------------
<a name="Q48">Q48</a>. How do I convert a rotation matrix to a quaternion?
--------------------------------------------------------
A rotation may be converted back to a quaternion through the use of
the following algorithm:
The process is performed in the following stages, which are as follows:
Calculate the trace of the matrix T from the equation:
2 2 2
T = 4 - 4x - 4y - 4z
2 2 2
= 4( 1 -x - y - z )
= mat[0] + mat[5] + mat[10] + 1
If the trace of the matrix is greater than zero, then
perform an &quot;instant&quot; calculation.
S = 0.5 / sqrt(T)
W = 0.25 / S
X = ( mat[9] - mat[6] ) * S
Y = ( mat[2] - mat[8] ) * S
Z = ( mat[4] - mat[1] ) * S
If the trace of the matrix is less than or equal to zero
then identify which major diagonal element has the greatest
value.
Depending on this value, calculate the following:
Column 0:
S = sqrt( 1.0 + mr[0] - mr[5] - mr[10] ) * 2;
Qx = 0.5 / S;
Qy = (mr[1] + mr[4] ) / S;
Qz = (mr[2] + mr[8] ) / S;
Qw = (mr[6] + mr[9] ) / S;
Column 1:
S = sqrt( 1.0 + mr[5] - mr[0] - mr[10] ) * 2;
Qx = (mr[1] + mr[4] ) / S;
Qy = 0.5 / S;
Qz = (mr[6] + mr[9] ) / S;
Qw = (mr[2] + mr[8] ) / S;
Column 2:
S = sqrt( 1.0 + mr[10] - mr[0] - mr[5] ) * 2;
Qx = (mr[2] + mr[8] ) / S;
Qy = (mr[6] + mr[9] ) / S;
Qz = 0.5 / S;
Qw = (mr[1] + mr[4] ) / S;
The quaternion is then defined as:
Q = | Qx Qy Qz Qw |
<a name="Q49">Q49</a>. How do I convert a rotation axis and angle to a quaternion?
----------------------------------------------------------------
Given the rotation axis and angle, the following algorithm may be
used to generate a quaternion:
sin_a = sin( angle / 2 )
cos_a = cos( angle / 2 )
qx = axis -&gt; x / sin_a
qy = axis -&gt; y / sin_a
qz = axis -&gt; z / sin_a
qw = cos_a
<a name="Q50">Q50</a>. How do I convert a quaternion to a rotation axis and angle?
----------------------------------------------------------------
A quaternion can be converted back to a rotation axis and angle
using the following algorithm:
cos_angle = qr -&gt; qw;
angle = acos( cos_angle ) * 2 * RADIANS;
sin_angle = sqrt( 1.0 - cos_angle * cos_angle );
if ( fabs( sin_angle ) &lt; 0.0005 )
sa = 1;
axis -&gt; vx = qr -&gt; qx / sa;
axis -&gt; vy = qr -&gt; qy / sa;
axis -&gt; vz = qr -&gt; qz / sa;
<a name="Q51">Q51</a>. How do I convert spherical rotation angles to a quaternion?
----------------------------------------------------------------
A rotation axis itself may be defined using spherical coordinates
(latitude and longitude) and a rotation angle
In this case, the quaternion can be calculated as follows:
-----------------------
sin_a = sin( angle / 2 )
cos_a = cos( angle / 2 )
sin_lat = sin( latitude )
cos_lat = cos( latitude )
sin_long = sin( longitude )
cos_long = cos( longitude )
qx = sin_a * cos_lat * sin_long
qy = sin_a * sin_lat
qz = sin_a * sin_lat * cos_long
qw = cos_a
-----------------------
<a name="Q52">Q52</a>. How do I convert a quaternion to spherical rotation angles?
----------------------------------------------------------------
A quaternion can be converted to spherical coordinates by extending
the conversion process:
-----------------------
cos_angle = q -&gt; qw;
sin_angle = sqrt( 1.0 - cos_angle * cos_angle );
angle = acos( cos_angle ) * 2 * RADIANS;
if ( fabs( sin_angle ) &lt; 0.0005 )
sa = 1;
tx = q -&gt; qx / sa;
ty = q -&gt; qy / sa;
tz = q -&gt; qz / sa;
latitude = -asin( ty );
if ( tx * tx + tz * tz &lt; 0.0005 )
longitude = 0;
else
longitude = atan2( tx, tz ) * RADIANS;
if ( longitude &lt; 0 )
longitude += 360.0;
-----------------------
<a name="Q53">Q53</a>. How do I use quaternions to perform linear interpolation between matrices?
-------------------------------------------------------------------------------
For many animation applications, it is necessary to interpolate
between two rotation positions of a given object. These positions may
have been specified using keyframe animation or inverse kinematics.
Using either method, at least two rotation matrices must be known, and
the desired goal is to interpolate between them. The two matrices are
referred to as the starting and finish matrices( MS and MF).
Using linear interpolation, the interpolated rotation matrix is
generated using a blending equation with the parameter T, which
ranges from 0.0 to 1.0.
At T=0, the interpolated matrix is equal to the starting matrix.
At T=1, the interpolated matrix is equal to the finishing matrix.
Then the interpolated rotation matrix (MI) is specified as:
MI = F( MS, MI, T )
where F is a blending function.
The first stage in interpolating between the two matrices is to
determine the rotation matrix that will convert MS to MF.
This is achieved using the following expression:
-1
T = Ms . Mf
where Ms is the start matrix,
Mf is the finish matrix,
and T is the intermediate matrix.
The next stage is to convert this matrix into a rotation axis and
angle. This is achieved by converting the matrix into a quaternion
and finally into the required rotation axis and angle.
In order to generate the interpolated rotation matrix, it is only
necessary to scale the rotation angle and convert this angle and
the rotation axis back into a rotation matrix.
Using a 4x4 matrix library, this is as follows:
m4_transpose( mt, ms ); /* Inverse */
m4_mult( ms, mt, mb ); /* Rotation matrix */
m4_to_axisangle( ms, axis, angle ); /* Rotation axis/angle */
for ( t = 0; t &lt; 1.0; t += 0.05 )
{
m4_from_axisangle( mi, axis, angle * t ); /* Final interpolation */
... whatever ...
}
where t is the interpolation factor ranging from 0.0 to 1.0
<a name="Q54">Q54</a>. How do I use quaternions to perform cubic interpolation between matrices?
------------------------------------------------------------------------------
For some applications, it may not be convenient or possible to use linear
interpolation for animation purposes. In this case, cubic interpolation
is another alternative.
In order to use cubic interpolation, at least four rotation matrices must
be known.
Each of these is then converted into a set of spherical rotations
via quaternions and spherical rotation angles (ie. longitude, latitude
and rotation angle).
These are then multiplied with the base matrix for a Cardinal spline
curve. This interpolation matrix can then be used to determine the
intermediate spherical rotation angles.
Once the interpolated coordinates are known (latitude, longitude and
rotation angle), the interpolated rotation matrix can then be generated
through the conversion to quaternions.
Using a 4x4 matrix library, the algorithm is as follows:
----------------------------------------------------------------------
for ( n = 0; n &lt; 4; n++ )
m4_to_spherical( mat[n], &amp;v_sph[n] ); /* Spherical coordinates */
m4_multspline( m_cardinal, v_sph, v_interp ); /* Interpolation vector */
...
v3_cubic( v_pos, v_interp, t ); /* Interpolation */
m4_from_spherical( m_rot, v_pos ); /* Back to a matrix */
----------------------------------------------------------------------
</pre>
</body>
</html>