当前位置:网站首页>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
边栏推荐
- 如何理解普吕克坐标(几何理解)
- Advanced multi-threading (CountDownLatch, deadlock, thread-safe collection class)
- Camera coordinate system, world coordinate system, pixel coordinate system conversion, and Fov conversion of OPENGLDEFocal Length and Opengl
- STL源码剖析:bound friend template friend代码测试和理解
- (GGG)JWT
- 空间平面相交的直线的计算及其源码
- STL源码剖析:class template explicit specialization代码测试和理解
- MongoDB-CUD没有R
- 测试开发工程师成长日记002 - 从0开始做接口自动化
- MySQL主从复制配置搭建,一步到位
猜你喜欢

Mastering JESD204B (1) – Debugging of AD6676

Linx common directory & file management commands & VI editor usage introduction

大厂年薪50w+招聘具有测试平台开发能力的测试工程师

Required request body is missing 问题解决

DNS域名解析服务

Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)

How to import matlab data into modelsim simulation

空间顶点到直线的距离计算及其源码

STL源码剖析:临时对象的代码测试和理解

Redis6的数据类型
随机推荐
使用 Grafana 的 Redis Data Source 插件监控 Redis
空间顶点到直线的距离计算及其源码
MySQL common commands and mysqldump backup
Shortcut keys commonly used in the use of Word
Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)
02-Use of Cycript
Software Testing Terminology - Scenario Testing
Event Delivery and Responder Chains
debian problem
软件测试_01
Test Development Engineer Growth Diary 001 - Some Introduction to Agile Testing, CI/CD/CT, DecOps
Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)
Deploy GraphScope with Helm
A New Paradigm for Distributed Deep Learning Programming: Global Tensor
MongoDB-CUD没有R
(GGG)JWT
openstack删除计算节点
测试开发工程师成长日记003 - 接口自动化框架搭建
Local Implicit Grid Representations for 3D Scenes详解
proftpd 配置文件说明