当前位置:网站首页>Proof of distance calculation from space vertex to plane and its source code
Proof of distance calculation from space vertex to plane and its source code
2022-07-30 07:50:00 【sunset stained ramp】
目的:最近写C++代码,遇到一些基础的算法.Such as calculating the distance from the vertex to the plane.
The previous article has introduced the plane and its mathematical expression:
geometric expression P l a n e Plane Plane:平面法向量 N = ( n 0 n 1 n 2 ) N=\begin{pmatrix} n_0 \\ n_1 \\ n_2 \end{pmatrix} N=⎝⎛n0n1n2⎠⎞,and a vertex on the plane P 0 = ( x 0 y 0 z 0 ) P_0=\begin{pmatrix} x_0 \\ y_0 \\ z_0 \end{pmatrix} P0=⎝⎛x0y0z0⎠⎞
The representation of the vertex: P 1 = ( x 1 y 1 z 1 ) P_1=\begin{pmatrix} x_1 \\ y_1 \\ z_1 \end{pmatrix} P1=⎝⎛x1y1z1⎠⎞
计算 P 1 P_1 P1到平面 P l a n e Plane Plane的距离.
P l a n e Plane Planeexpression can be converted to n 0 ( x − x 0 ) + n 1 ( y − y 0 ) + n 2 ( z − z 0 ) = 0 n_0(x-x_0)+n_1(y-y_0)+n_2(z-z_0)=0 n0(x−x0)+n1(y−y0)+n2(z−z0)=0,计算得到 n 0 x + n 1 y + n 2 z + d = 0 n_0x+n_1y+n_2z+d=0 n0x+n1y+n2z+d=0其中 d = − n 0 x 0 − n 1 y 0 − n 2 z 0 = − N T ⋅ P 0 d=-n_0x_0-n_1y_0-n_2z_0=-N^T \cdot P_0 d=−n0x0−n1y0−n2z0=−NT⋅P0
推导公式如下:
Calculate the red vertices P 1 P_1 P1to the blue plane P l a n e Plane Plane的距离.The yellow vertex is a point on the plane P 0 P_0 P0.Vertex-to-plane distance definition:The distance from the point where it is perpendicular to the plane(Represented by grey line segments in the figure).
The algorithm based on the vertical triangle obtains the length of the gray line segment as d i s t dist dist
d i s t = ∥ P 0 − P 1 ∥ ⋅ c o s ( θ ) ( 1 ) dist=\lVert P_0-P_1 \rVert \cdot cos(\theta) \space \space (1) dist=∥P0−P1∥⋅cos(θ) (1)
其中 θ \theta θ是两个向量 P 0 P 1 P_0P_1 P0P1and the plane normal vector N N N的夹角,
c o s ( θ ) = ∣ N T ⋅ ( P 0 − P 1 ) ∣ ∥ P 0 − P 1 ∥ ⋅ ∥ N ∥ ( 2 ) cos(\theta) = \cfrac {|N^T \cdot (P_0-P_1)|}{\lVert P_0-P_1 \rVert \cdot \lVert N \rVert} \space \space (2) cos(θ)=∥P0−P1∥⋅∥N∥∣NT⋅(P0−P1)∣ (2)
将公式(2)带入公式(1)可以得到如下:
d i s t = ∣ N T ⋅ ( P 0 − P 1 ) ∣ ∥ N ∥ ( 3 ) dist=\cfrac {|N^T \cdot (P_0-P_1)|}{ \lVert N \rVert} \space \space (3) dist=∥N∥∣NT⋅(P0−P1)∣ (3)
将向量 P 0 = ( x 0 y 0 z 0 ) P_0=\begin{pmatrix} x_0 \\ y_0 \\ z_0 \end{pmatrix} P0=⎝⎛x0y0z0⎠⎞, P 1 = ( x 1 y 1 z 1 ) P_1=\begin{pmatrix} x_1 \\ y_1 \\ z_1 \end{pmatrix} P1=⎝⎛x1y1z1⎠⎞, N = ( n 0 n 1 n 2 ) N=\begin{pmatrix} n_0 \\ n_1 \\ n_2 \end{pmatrix} N=⎝⎛n0n1n2⎠⎞带入公式(3)the resulting equation
d i s t = ∣ N T ⋅ P 0 − N T ⋅ P 1 ∣ ∥ N ∥ = ∣ − d − N T ⋅ P 1 ∣ ∥ N ∥ = ∣ N T ⋅ P 1 + d ∣ ∥ N ∥ ( 4 ) dist=\cfrac {|N^T \cdot P_0 - N^T \cdot P_1|}{ \lVert N \rVert}=\cfrac {|-d - N^T \cdot P_1|}{ \lVert N \rVert} =\cfrac {|N^T \cdot P_1 + d|}{ \lVert N \rVert} \space \space (4) dist=∥N∥∣NT⋅P0−NT⋅P1∣=∥N∥∣−d−NT⋅P1∣=∥N∥∣NT⋅P1+d∣ (4)
The above vector form can be rewritten in algebraic form as :
d i s t = ∣ n 0 x 1 + n 1 y 1 + n 2 z 1 + d ∣ n 0 2 + n 1 2 + n 2 2 ( 5 ) dist = \cfrac{|n_0x_1 + n_1y_1+n_2z_1+d|}{\sqrt{n_0^2+n_1^2+n_2^2}} \space \space (5) dist=n02+n12+n22∣n0x1+n1y1+n2z1+d∣ (5)
The source code is as follows,It is written in vector based schema.It needs to be configured by youEigen库:
float Pnt3d2PlaneDist(Eigen::Vector4f& fn, Eigen::Vector3f& pnt)
{
Eigen::Vector3f n = Eigen::Vector3f(fn[0], fn[1], fn[2]);
float sd = n.norm();
if (sd < 1e-8)
return std::numeric_limits<float>::max();
float sb = std::abs(n.dot(pnt) + fn[3]);
float d = sb / sd;
return d;
}
See the website for details:https://mathinsight.org/distance_point_plane
边栏推荐
- Bull: remove common characters
- GAIA-IR: Parallelized Graph Query Engine on GraphScope
- Test the basics 01
- LVM和磁盘配额
- Test Development Engineer Growth Diary 017 - The Life Cycle of a Bug
- Data types of Redis6
- Redis download and installation
- How to create a shortcut without the "shortcut" suffix?
- 测试开发工程师成长日记010 - Jenkins中的CI/CD/CT(持续集成构建/持续交付/持续测试)
- Selenium02
猜你喜欢
As a test leader, examine several aspects of job candidates
02-Use of Cycript
flask项目快速搭建部署gunicorn+supervisor
相机坐标系,世界坐标系,像素坐标系三者转换,以及OPENGLDEFocal Length和Opengl 的 Fov转换
远程连接服务器的MySql
阿里二面:列出 Api 接口优化的几个技巧
Test development engineer growth diary 016 - those things about the test
Bull: remove common characters
空间顶点到平面的距离计算的证明及其源码
LVM和磁盘配额
随机推荐
CTO说不建议我使用SELECT * ,这是为什么?
进程和计划任务管理
Distance calculation from space vertex to straight line and its source code
MongoDB - query
libgrape-lite on GPUs: GPU helps accelerate graph analysis tasks
引导过程与服务控制
Detailed explanation of numpy multidimensional array ndarray
Advanced multi-threading (CountDownLatch, deadlock, thread-safe collection class)
RAID磁盘阵列
GNNLab: A Novel GNN System Based on Spatial Sharing Ideas
Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)
prometheus-basic_auth加密配置
From installation to compilation: 10 minutes to teach you to use and develop GraphScope locally
Rapidly develop GraphScope graph analysis applications
Swagger使用方式,告别postman
(GGG)JWT
空间顶点到平面的距离计算的证明及其源码
新人误删数据,组长巧用MySQL主从复制延迟挽回损失
Event Delivery and Responder Chains
向量三重积的等式推导证明