我从Kinect输出的图像中选择了4个点,每个点都有它的 (x, y, z)
坐标。
我的目标是确定这4个点是否在同一个平面上。
这是我的函数:
public bool isValidPlane()
{
for (int i = 0; i < edgesPoints.Length; i++)
{
double absPlaneEquation = Math.Abs(distance -
(normal.X * edgesPoints[i].X + normal.Y * edgesPoints[i].Y + normal.Z * edgesPoints[i].Z));
if (absPlaneEquation > 1500) /* 1500 is a tolerance error*/
{
return false;
}
}
return true;
}
normal
也是平面的法向量(通过平面上两个向量的叉积计算得出,这两个向量是由所选的四个点中任意选三个点计算得出的),并已被归一化:
private void calcPlaneNormalVector()
{
if (lastEdgeNumber < 3)
{
return;
}
Vector3D vec1 = new Vector3D(edgesPoints[0], edgesPoints[1]);
Vector3D vec2 = new Vector3D(edgesPoints[0], edgesPoints[2]);
vec2 = vec1.crossProduct(vec2);
double lengthNormal = Math.Sqrt(Math.Pow(vec2.X, 2) + Math.Pow(vec2.Y, 2) + Math.Pow(vec2.Z, 2));
//normalizing:
normal = new Vector3D((vec2.X / lengthNormal), (vec2.Y / lengthNormal), (vec2.Z / lengthNormal));
distance = (-1) * (edgesPoints[0].X * normal.X + edgesPoints[0].Y * normal.Y + edgesPoints[0].Z + normal.Z);
}
Vector3D
是一个表示向量的类:
public class Vector3D
{
private double x, y, z;
public Vector3D(Point3D p1, Point3D p2)
{
x = p2.X - p1.X;
y = p2.Y - p1.Y;
z = p2.Z - p1.Z;
}
public Vector3D(double a = 0, double b = 0, double c = 0)
{
x = a;
y = b;
z = c;
}
<get properties for x, y, z >
public Vector3D crossProduct(Vector3D u)
{
double tmpX = 0, tmpY = 0, tmpZ = 0;
tmpX = y * u.Z - z * u.Y;
tmpY = z * u.X - x * u.Z;
tmpZ = x * u.Y - y * u.X;
return new Vector3D(tmpX, tmpY, tmpZ);
}
public double dotProduct(Vector3D u)
{
return x * u.X + y * u.Y + z * u.Z;
}
}
即使选择的4个点不在同一个平面上,我总是得到 1300 <= absPlaneEquation <= 1400
。如何最好地检测这4个点是否在同一个平面上?