现代计算机图形学02---3D变换
闫令琪老师的网站:
https://sites.cs.ucsb.edu/~lingqi/teaching/games101.html
有了二维空间的变换基础以后,三维的就容易理解了。
对于三维空间的变换,同样需要采用齐次坐标(homogeneous coordinates)的方式来处理。
- 3D point = (x, y, z, 1)T
- 3D vector = (x, y, z, 0)T
行优先和列优先
一个点或者向量其实有两种写法:
- point = (x, y, z),行优先,即1*3矩阵
- point = (x, y, z)T,列优先,即3*1矩阵
为什么先提出这种差别,因为这个涉及到后面的矩阵相乘运算次序问题。
对于行优先的点,即1*3
矩阵,想要进行矩阵变换之后得到新的点,则只能点右边乘一个3*3
的矩阵;
对于列优先的点,即3*1
矩阵,想要进行矩阵变换之后得到新的点,则只能点左边乘一个3*3
的矩阵;
如果期望对一个点先进行缩放S,再进行旋转R,最后进行偏移转换T,则:
已知的Revit和Rhino中的点和向量,都是列优先,所以对点/向量进行坐标变换的写法是:
对于列优先:P新 = T*R*S*P
,即从右往左开始逐个应用
对于行优先:P新 = P*S*R*T
,即从左往右开始逐个应用
Revit、Rhino中的矩阵都是使用的列优先,glTF格式中也是采用的列优先。
了解了列优先或者行优先,才能明确不同的开发软件如今进行复合矩阵的计算。
Tips:
列优先或者行优先完全不影响矩阵的书写方式,比如glTF中,即使定义了是列优先,但是一个偏移矩阵的数组表达仍然是行的方式:
"matrix" :
[
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
Tx,Ty,Tz,1
]
一般形式
采用4*4的矩阵:
[
x
′
y
′
z
′
1
]
=
[
a
b
c
t
x
d
e
f
t
y
g
h
i
t
z
0
0
0
01
]
[
x
y
z
1
]
\left[ \begin{matrix} x' \\y' \\z'\\1 \end{matrix} \right] = \left[ \begin{matrix} a & b & c& tx\\ d & e & f& ty\\ g & h & i& tz\\ 0 & 0 & 0 & 01 \end{matrix} \right] \left[ \begin{matrix} x \\y \\z\\1 \end{matrix} \right]
x′y′z′1
=
adg0beh0cfi0txtytz01
xyz1
缩放
[
s
1
0
0
0
0
s
2
0
0
0
0
s
3
0
0
0
0
1
]
\left[ \begin{matrix} s1 & 0 & 0& 0\\ 0 & s2 & 0& 0\\ 0 & 0 & s3& 0\\ 0 & 0 & 0 & 1 \end{matrix} \right]
s10000s20000s300001
在Revit中,缩放比较少见,即使有,3个坐标轴的缩放比例也是相等的,即s1=s2=s3等比例缩放。
平移
[ 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ] \left[ \begin{matrix} 1 & 0 & 0& tx\\ 0 & 1 & 0& ty\\ 0 & 0 & 1& tz\\ 0 & 0 & 0 & 1 \end{matrix} \right] 100001000010txtytz1
旋转
1.绕x轴旋转
x轴保持不变:
[
1
0
0
0
0
c
o
s
θ
−
s
i
n
θ
0
0
s
i
n
θ
c
o
s
θ
0
0
0
0
1
]
\left[ \begin{matrix} 1 & 0 & 0& 0\\ 0 & cos\theta &-sin\theta& 0\\ 0 & sin\theta &cos\theta& 0\\ 0 & 0 & 0 & 1 \end{matrix} \right]
10000cosθsinθ00−sinθcosθ00001
2.绕y轴旋转
y轴保持不变:(注意和x轴旋转与y轴旋转的规律不一样)
[
c
o
s
θ
0
s
i
n
θ
0
0
1
0
0
−
s
i
n
θ
0
c
o
s
θ
0
0
0
0
1
]
\left[ \begin{matrix} cos\theta & 0 &sin\theta& 0\\ 0 & 1 &0& 0\\ -sin\theta & 0 &cos\theta& 0\\ 0 & 0 & 0 & 1 \end{matrix} \right]
cosθ0−sinθ00100sinθ0cosθ00001
3.绕z轴旋转
z轴保持不变:
[
c
o
s
θ
−
s
i
n
θ
0
0
s
i
n
θ
c
o
s
θ
0
0
0
0
1
0
0
0
0
1
]
\left[ \begin{matrix} cos\theta & -sin\theta & 0& 0\\ sin\theta & cos\theta &0& 0\\ 0 & 0 &1& 0\\ 0 & 0 & 0 & 1 \end{matrix} \right]
cosθsinθ00−sinθcosθ0000100001
Revit中的坐标系
第1列:BasisX;第2列:BasisY;第3列:BasisZ;第4列:Origin。
其实不仅仅是在Revit中,其他如gltf、opengl等三维数据格式都是以相同的方式来表示。(不过gltf中对应的matrix书写上是行和列交换了一下)。
Revit到glTF坐标系转换
上图左边是gltf坐标系,右边是revit坐标系。
要实现revit坐标系到gltf坐标系的转换,只需要绕着X轴逆时针旋转90度即可:
[
1
0
0
0
0
c
o
s
9
0
∘
−
s
i
n
9
0
∘
0
0
s
i
n
9
0
∘
c
o
s
9
0
∘
0
0
0
0
1
]
=
[
1
0
0
0
0
0
−
1
0
0
1
0
0
0
0
0
1
]
\left[ \begin{matrix} 1 & 0 & 0& 0\\ 0 & cos90^{\circ} &-sin90^{\circ}& 0\\ 0 & sin90^{\circ} &cos90^{\circ}& 0\\ 0 & 0 & 0 & 1 \end{matrix} \right] =\left[ \begin{matrix} 1 & 0 & 0& 0\\ 0 & 0 &-1& 0\\ 0 & 1 &0& 0\\ 0 & 0 & 0 & 1 \end{matrix} \right]
10000cos90∘sin90∘00−sin90∘cos90∘00001
=
100000100−1000001
// 坐标系(基)变换矩阵
var transform = Transform.CreateRotation(XYZ.BasisX, Math.PI / 2);
那么将revit坐标系中的点转换为gltf坐标系中的点坐标,对应的转换矩阵为:
// 坐标变换矩阵 = 基变换矩阵的逆矩阵
var totalTr = transform.Inverse;