Define an plane
An 3D plane can be define in many ways, however all of them can be derived from an simple case where we have three points.
One of the common ways we define an plane is with the following equation:
A
x
+
B
y
+
C
z
+
D
=
0
Ax + By + Cz + D = 0
Ax+By+Cz+D=0
如果已知平面内3个点p0,p1,p2,那么公式的4个参数可以这样计算:
-
计算Vector u = p1 - p0,v = p2 - p0
-
计算两向量cross product :n = u×v
-
将n归一化
-
假设归一化后的n=(nx, ny, nz),那么可以计算出:
- A = nx
- B = ny
- C = nz
-
D的计算可以将一个平面上的已知点(如p0)代入平面公式得到:
D = -(Ap0x + Bp0y + C*p0z) = - n *(dot product) p0
Compute the signed distance to a plane
计算三维空间中平面到一个点的带符号距离,可以将点的坐标值代入平面定义公式的左边,如平面到空间一个点r(rx, ry, rz)的距离dist为:
dist = A * rx + B * ry + C * rz + D = n * r + D
dist的绝对值即距离值,dist的正负号表示点在平面的哪一边:
- 正号表示点在平面法向量指向的同侧
- 负号表示点在平面法向量指向的反侧
Project an point to plane
假设点q是三维空间一个点,dist为点q到平面的singed distance,计算q到平面的投影点p方法如下:
p = q - dist * n