我找不到一种找到点和平面之间有符号距离的一致方法。给定一个定义为点和法线的平面,如何计算?
struct Plane
{
Vec3 point;
Vec3 normal;
}
答案 0 :(得分:21)
你让事情变得太复杂了。如果您的法线正常化,您可以这样做:
float dist = dotProduct(p.normal, (vectorSubtract(point, p.point)));
答案 1 :(得分:2)
别担心,我完全理解你的感受。我假设你想要一些代码片段。所以你可以用自己的方式实现它。你需要做更多的工作,而不仅仅是找到点积。
由您来理解此算法并将其实施到您自己的程序中 我要做的是给你一个这个算法的实现
点与平面之间的签名距离
以下是这些算法的一些示例“C ++”实现。
// Assume that classes are already given for the objects:
// Point and Vector with
// coordinates {float x, y, z;}
// operators for:
// Point = Point ± Vector
// Vector = Point - Point
// Vector = Scalar * Vector (scalar product)
// Plane with a point and a normal {Point V0; Vector n;}
//===================================================================
// dot product (3D) which allows vector operations in arguments
#define dot(u,v) ((u).x * (v).x + (u).y * (v).y + (u).z * (v).z)
#define norm(v) sqrt(dot(v,v)) // norm = length of vector
#define d(u,v) norm(u-v) // distance = norm of difference
// pbase_Plane(): get base of perpendicular from point to a plane
// Input: P = a 3D point
// PL = a plane with point V0 and normal n
// Output: *B = base point on PL of perpendicular from P
// Return: the distance from P to the plane PL
float
pbase_Plane( Point P, Plane PL, Point* B)
{
float sb, sn, sd;
sn = -dot( PL.n, (P - PL.V0));
sd = dot(PL.n, PL.n);
sb = sn / sd;
*B = P + sb * PL.n;
return d(P, *B);
}
从这里采取: http://www.softsurfer.com/Archive/algorithm_0104/algorithm_0104.htm
<强> PK