Propósito: Recientemente escribí código C++ y encontré algunos algoritmos básicos. Como calcular la distancia de un vértice a un plano.
El artículo anterior ha introducido el plano y su expresión matemática:
expresión geométrica Plano PlanoPAGS l a n mi : plano normal vectorN = ( n 0 n 1 n 2 ) N=\begin{pmatrix} n_0 \\ n_1 \\ n_2 \end{pmatrix}norte=⎝⎛norte0norte1norte2⎠⎞, y un vértice P 0 = ( x 0 y 0 z 0 ) P_0=\begin{pmatrix} x_0 \\ y_0 \\ z_0 \end{pmatrix}PAG0=⎝⎛X0y0z0⎠⎞
Expresión de vértice: P 1 = ( x 1 y 1 z 1 ) P_1=\begin{pmatrix} x_1 \\ y_1 \\ z_1 \end{pmatrix}PAG1=⎝⎛X1y1z1⎠⎞
Calcular P 1 P_1PAG1Al plano Plano plano PlanoDistancia del plano . _ _ _
Avión AviónLa expresión de P l a n e se puede transformar enn 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)=0norte0( X−X0)+norte1( y−y0)+norte2( z−z0)=0 , calcularn 0 x + n 1 y + n 2 z + d = 0 n_0x+n_1y+n_2z+d=0norte0X+norte1y+norte2z+d=0其中d = − norte 0 x 0 − norte 1 y 0 − norte 2 z 0 = − NT ⋅ PAGS 0 d=-n_0x_0-n_1y_0-n_2z_0=-N^T \cdot P_0d=- norte0X0−norte1y0−norte2z0=- norteT⋅PAG0
La fórmula de derivación es la siguiente:
Calcular el vértice rojo P 1 P_1PAG1Al avión azul P lane AviónDistancia del plano . _ _ _ El vértice amarillo es un punto P 0 P_0en el planoPAG0. Definición de distancia de vértice a plano: la distancia de su punto perpendicular al plano (indicado por el segmento de línea gris en la figura).
Según el algoritmo del triángulo vertical, la longitud del segmento de la línea gris es dist distre yo s t
dist = ∥ PAGS 0 − PAGS 1 ∥ ⋅ cos ( θ ) ( 1 ) dist=\lVert P_0-P_1 \rVert \cdot cos(\theta) \space \space (1)distancia _ _ _=∥ P0−PAG1∥⋅c o s ( θ ) ( 1 )
aumentarθ \thetaθ son dos vectoresP 0 P 1 P_0P_1PAG0PAG1y plano vector normal NNPor ejemplo ,
cos ( θ ) = ∣ NT ⋅ ( PAGS 0 − PAGS 1 ) ∣ ∥ PAGS 0 − PAGS 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)c o s ( yo )=∥ P0−PAG1∥⋅∥ norte ∥∣ norteT⋅( PAG0−PAG1) ∣ ( 2 )
Sustituyendo la fórmula (2) en la fórmula (1) se puede obtener de la siguiente manera:
dist = ∣ NT ⋅ ( P 0 − P 1 ) ∣ ∥ N ∥ ( 3 ) dist=\cfrac {|N^T \cdot ( P_0 -P_1)|}{ \lVert N \rVert} \espacio \espacio (3)distancia _ _ _=∥ norte ∥∣ norteT⋅( PAG0−PAG1) ∣ ( 3 )
Pon el vectorP 0 = ( x 0 y 0 z 0 ) P_0=\begin{pmatrix} x_0 \\ y_0 \\ z_0 \end{pmatrix}PAG0=⎝⎛X0y0z0⎠⎞, PAGS 1 = ( x 1 y 1 z 1 ) P_1=\begin{pmatrix} x_1 \\ y_1 \\ z_1 \end{pmatrix}PAG1=⎝⎛X1y1z1⎠⎞, norte = ( norte 0 norte 1 norte 2 ) norte = \begin{pmatrix} n_0 \\ n_1 \\ n_2 \end{pmatrix}norte=⎝⎛norte0norte1norte2⎠⎞La ecuación
dist = ∣ NT ⋅ PAGS 0 − NT ⋅ PAGS 1 ∣ ∥ N ∥ = ∣ − re − NT ⋅ PAGS 1 ∣ ∥ N ∥ = ∣ NT ⋅ PAGS 1 + re ∣ ∥ 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)distancia _ _ _=∥ norte ∥∣ norteT⋅PAG0−norteT⋅PAG1∣=∥ norte ∥∣−d−norteT⋅PAG1∣=∥ norte ∥∣ norteT⋅PAG1+d∣ _ ( 4 )
La forma vectorial anterior se puede reescribir en forma algebraica:
dist = ∣ 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}} \espacio \espacio (5)distancia _ _ _=norte02+norte12+norte22∣ norte0X1+norte1y1+norte2z1+d∣ _ ( 5 )
El código fuente es el siguiente, está escrito en modo vectorial. Requiere que configure la biblioteca Eigen:
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;
}
Consulte el sitio web para obtener más detalles: https://mathinsight.org/distance_point_plane