当前位置:网站首页>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
边栏推荐
- mpich安装
- 阿里二面:列出 Api 接口优化的几个技巧
- how to use xilinx's FFT ip
- Bull: remove common characters
- Test Development Engineer Growth Diary 003 - Interface Automation Framework Construction
- Test Development Engineer Growth Diary 001 - Some Introduction to Agile Testing, CI/CD/CT, DecOps
- prometheus监控nacos
- 黑盒测试的概念及测试方法
- Rodrigues:旋转矩阵的向量表达
- STL source code analysis: conceptual understanding of iterators, and code testing.
猜你喜欢

Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)

计算矩阵的逆源码(使用伴随矩阵,3×3的矩阵)

Event Delivery and Responder Chains

(GGG)JWT

05-Theos

STL source code analysis: conceptual understanding of iterators, and code testing.

使用 Grafana 的 Redis Data Source 插件监控 Redis

MongoDB-CUD without R

How to import matlab data into modelsim simulation

不会吧,Log4j 漏洞还没有完全修复?
随机推荐
I can't hide it, I want to expose the bad things about cloud native
Proftpd配置文件
npm安装nodejs环境配置
Network Protocol 03 - Routing and NAT
Graph analysis like NetworkX with GraphScope
测开基础知识01
新人误删数据,组长巧用MySQL主从复制延迟挽回损失
Test and Development Engineer Growth Diary 009 - Environment Pai Pai Station: Development Environment, Test Environment, Production Environment, UAT Environment, Simulation Environment
As a test leader, examine several aspects of job candidates
【Untitled】
B站崩了,如果是你是那晚负责的开发人员你会怎么做?
MongoDB - query
使用Apifox测试套件自动化测试接口
从安装到编译: 10分钟教你在本地使用和开发GraphScope
LVM和磁盘配额
Multithreading basics (multithreaded memory, security, communication, thread pools and blocking queues)
idea内置翻译插件
MongoDB-CUD without R
The calculation proof of the intersection of the space line and the plane and its source code
debian vsftpd + ssl