3D变换矩阵的分解公式
3D變換矩陣:平移、縮放、旋轉(zhuǎn)
3D變換矩陣是一個(gè)4x4的矩陣,即由16個(gè)實(shí)數(shù)組成的二維數(shù)組,在三維空間中,任何的線性變換都可以用一個(gè)變換矩陣來(lái)表示。本文介紹從變換矩陣中提取出平移、縮放、旋轉(zhuǎn)向量的方法,提取公式的復(fù)雜程度為“平移 < 縮放 < 旋轉(zhuǎn)”,文章同時(shí)給出數(shù)學(xué)公式和JavaScript代碼(使用了瀏覽器的數(shù)學(xué)庫(kù)),首先給定一個(gè)行主序的4x4的變換矩陣:
// 變換矩陣(a~l為任意實(shí)數(shù))
const transform = [
??? [a,?b, c, d],
??? [e,?f, g, h],
??? [i,?j, k, l],
??? [0,?0, 0, 1],
];
最后一列就是平移向量:
// 平移向量
const translate = [
??? transform[0][3],?
??? transform[1][3],?
??? transform[2][3]
];
前三列向量的長(zhǎng)度就是縮放向量:
// 縮放向量
const scale = [
??? Math.hypot(transform[0][0],?transform[1][0], transform[2][0]),
??? Math.hypot(transform[0][1],?transform[1][1], transform[2][1]),
??? Math.hypot(transform[0][2],?transform[1][2], transform[2][2]),
]
旋轉(zhuǎn)向量有若干種不同的表現(xiàn)形式,包括Euler角、四元數(shù)、軸-角,但旋轉(zhuǎn)矩陣是統(tǒng)一的,將前三列分別除以縮放向量,就得到3x3的旋轉(zhuǎn)矩陣:
// 旋轉(zhuǎn)矩陣
const scale = [
??? [
??????? transform[0][0]?/ scale[0],
??????? transform[0][1]?/ scale[1],
??????? transform[0][2]?/ scale[2]
??? ],?[
??????? transform[1][0]?/ scale[0],
??????? transform[1][1]?/ scale[1],
??????? transform[1][2]?/ scale[2]
??? ],?[
??????? transform[2][0]?/ scale[0],
??????? transform[2][1]?/ scale[1],
??????? transform[2][2]?/ scale[2]
??? ],
]
下面這張圖可以直觀地看到,平移、縮放、旋轉(zhuǎn)在變換矩陣中的位置關(guān)系:
總結(jié)
以上是生活随笔為你收集整理的3D变换矩阵的分解公式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: matlab prn文件,PRN文件扩展
- 下一篇: python学习的一个定位_python