我从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
也是法线(平面上2个向量的交叉乘积,之前已经从4个选定点中的3个计算得到)到平面并且它被标准化了:
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个点指向同一平面的最佳方法是什么?
答案 0 :(得分:5)
获得平面的法线向量后,可以计算平面公式:
normal vector components : [A, B, C]
Plane equation : A·x + B·y + C·z + D = 0;
使用三个点之一(P1
,P2
或P3
)来获取法线向量来评估D
然后只需检查第四点( P4
)满足等式:
D = - (A·x1 + B·y1 + C·z1)
A·x4 + B·y4 + C·z4 - (A·x1 + B·y1 + C·z1) = 0
请注意,您正在使用浮点算术,因此无法测试严格的相等性。您需要定义一个可接受的错误,并根据这样的容差检查第四个点是否符合等式:
|A·x4 + B·y4 + C·z4 - (A·x1 + B·y1 + C·z1)| < TOLERANCE
更新:以下是我如何为您的问题编写解决方案的代码:
public struct Point3D
{
public double X { get; }
public double Y { get; }
public double Z { get; }
public Point3D(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
}
public struct Vector3D
{
public double X { get; }
public double Y { get; }
public double Z { get; }
public double Magnitude => Math.Sqrt(X * X + Y * Y + Z * Z);
public Vector3D(Point3D p1, Point3D p2)
: this(p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z)
{
}
public Vector3D(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
public static Vector3D CrossProduct(Vector3D left, Vector3D right)
{
double tmpX = 0, tmpY = 0, tmpZ = 0;
tmpX = left.Y * right.Z - left.Z * right.Y;
tmpY = left.Z * right.X - left.X * right.Z;
tmpZ = left.X * right.Y - left.Y * right.X;
return new Vector3D(tmpX, tmpY, tmpZ);
}
public static double DotProduct(Vector3D left, Vector3D right)
{
return left.X * right.X + left.Y * right.Y + left.Z * right.Z;
}
}
public struct Plane3D
{
private const double TOLERANCE = 0.001;
private readonly double independentTerm;
public Vector3D Normal { get; }
public Plane3D(Point3D p1, Point3D p2, Point3D p3)
{
Normal = Vector3D.crossProduct(new Vector3D(p1, p2), new Vector3D(p1, p3));
if (Normal.Magnitude < TOLERANCE)
throw new ArgumentException("Specified points do not define a valid plane.");
independentTerm = -(Normal.X * p1.X + Normal.Y * p1.Y + Normal.Z * p1.Z);
}
public bool Contains(Point3D p) => Math.Abs(Normal.X * p.X + Normal.Y * p.Y + Normal.Z * p.Z + independentTerm) < TOLERANCE;
}
注意事项:
Point3D
和Vector3D
更改为结构。这在很大程度上取决于您将如何使用这些对象,但乍一看,值类型似乎更合适。static
方法。这可能取决于个人品味。TOLERANCE
内实施了Plane
const。可能有更好的地方来定义它,它只是为了方便起见。