如何在3d中获得点到平面的距离? [英] How to get distance from point to plane in 3d?

查看:53
本文介绍了如何在3d中获得点到平面的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个三角形,其中包含点 A、B、C 和空间中的点 (P).如何获得点到平面的距离?我需要计算从 P 到平面的距离,即使我的三角形离得很远(或不在点上方,如图所示).

I have a triangle with points A, B, C and Point in space (P). How can I get distance from point to plane? I need to calc distance from P to plane, even when my triangle lie far away(or not above to the point, like on picture).

点和三角形:

推荐答案

我假设你想计算点和平面之间的垂直距离,给定 3 个点形成一个三角形.这里是矢量数学方法:

I assume you want to compute perpendicular distance between point and plane given 3 points on it forming a triangle. Here vector math approach:

  1. 定义

设三角形点为p0,p1,p2和测试点p.

let the triangle points be p0,p1,p2 and tested point p.

平面法线

首先我们需要获得平面法线,即平面内任意两个非平行非零向量的简单向量乘法:

first we need to obtain plane normal, that is simple vector multiplication of any two non parallel and non zero vectors inside the plane:

n = cross( p1-p0 , p2-p0 )

并将其归一化为单位向量(以简化内容):

and normalize it to unit vector (to simplify stuff):

n = n/|n|

  • 垂直距离

    我们可以为此利用点积,因此只需创建一个从平面上的任何点到您的测试点的向量,并使用单位法线...

    we can exploit dot product for this so just crate a vector going from any point on the plane into your tested point and dot with unit normal ...

    dist = |dot ( p-p0 , n )|
    

    最后一个绝对值(在标量距离上)只会去掉结果的符号,它告诉你点 p 是否在正常的方向 n 或相反,有时需要此类信息,因此在这种情况下,删除最外面的 abs 值并使用多边形缠绕和叉积操作数顺序来保持所需的法线方向.

    the last absolute value (on scalar distance) will just get rid of the sign of the result which tells you if the point p is in direction of normal n or in opposite one sometimes such info is wanted so in such case remove the outermost abs value and use polygon winding and cross product operands order to maintain wanted normal direction.

    在这里(寻找[edit2])你会发现crossdot||方程需要时使用:

    Here (look for [edit2]) you will find the cross , dot and || equations used if needed:

    所以如果我把所有的代码放在一起,就像表单一样:

    so if I put all together in code like form:

    U.x=p1.x-p0.x; V.x=p2.x-p0.x; // basis vectors on the plane
    U.y=p1.y-p0.y; V.y=p2.y-p0.y;
    U.z=p1.z-p0.z; V.z=p2.z-p0.z;
    n.x=(U.y*V.z)-(U.z*V.y);      // plane normal
    n.y=(U.z*V.x)-(U.x*V.z);
    n.z=(U.x*V.y)-(U.y*V.x);
    dist = sqrt( (n.x*n.x) + (n.y*n.y) + (n.z*n.z) ); // normalized
    n.x /= dist;
    n.y /= dist;
    n.z /= dist;
    dist = abs( (p.x-p0.x)*n.x + (p.y-p0.y)*n.y + (p.z-p0.z)*n.z ); // your perpendicular distance
    

    这篇关于如何在3d中获得点到平面的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆