定位与网格平行的对象

问题描述

我正在尝试根据网格中三角形的方向沿人体圆周对齐多个线对象。我想将线平行于网格。我正确地分配了沿圆周线的位置,但我还需要添加线的旋转,以便与身体平行。 身体是由多个三角形组成的网格,每条线都与一个三角形“相连”。

我只有:

  • 每条线距网格最近的三角形的 3 个点

  • 三角形的法线

  • 实例化线的位置(2 点,起点和终点)

我需要计算线的每个 X、Y、Z 轴的角度,使得三角形的法线与线网格垂直。我不知道如何获得所需的角度。如果有人愿意帮助我,我真的很感激。

输入:

Fvector TrianglePoints[3];

FVector Triangle_normal; //计算为(B-A)^(C-A),其中A、B、C为三角形的点

Fvector linePosition; //如果有帮助,我还有起始线和结束线位置

输出

//Frotator旋转(x,y,z),使三角形法线与线对象垂直。

圆周线构造概述。现在使用每条线的起始位置和结束位置计算旋转。当我们穿过网格的一些不规则部分时,我们希望正确旋转线条。现在旋转是固定的,只取决于线的开始和结束位置。

解决方法

如果我正确理解了您的目标,这里是一些相关的向量几何:

A,B,C are the vertices of the triangle:
A = [xA,yA,zA],B = [xB,yB,zB]
C = [xC,yC,zC]

K,L are the endpoints of the line-segment:
K = [xK,yK,zK]
L = [xL,yL,zL]

vectors are interpreted as row-vectors
by . I denote matrix multiplication
by x I denote cross product of 3D vectors
by t() I denote the transpose of a matrix
by | | I denote the norm (magnitude) of a vector

Goal: find the rotation matrix and rotation transformation of segment KL 
      around its midpoint,so that after rotation KL is parallel to the plane ABC
      also,the rotation is the "minimal" angle rotation by witch we need to 
      rotate KL in order to make it parallel to ABC

AB = B - A
AC = C - A
KL = L - K

n = AB x AC
n = n / |n|

u = KL x n
u = u / |u|

v = n x u

cos = ( KL . t(v) ) / |KL|
sin = ( KL . t(n) ) / |KL|   

U = [[ u[0],u[1],u[2] ],[ v[0],v[1],v[2] ],[ n[0],n[1],n[2] ],R = [[1,0],[0,cos,sin],-sin,cos]]

ROT = t(U).R.U

then,one can rotate the segment KL around its midpoint 
M = (K + L)/2

Y = M + ROT (X - M)

这是一个python脚本版本

A = np.array([0,0])
B = np.array([3,0])
C = np.array([2,3,0])

K = np.array([ -1,1])
L = np.array([  2,2,2])
KL = L-K

U = np.empty((3,3),dtype=float)

U[2,:] = np.cross(B-A,C-A)
U[2,:] = U[2,:] / np.linalg.norm(U[2,:])

U[0,:] = np.cross(KL,U[2,:])
U[0,:] = U[0,:] / np.linalg.norm(U[0,:])

U[1,:] = np.cross(U[2,:],U[0,:])

norm_KL = np.linalg.norm(KL)
cos_ = KL.dot(U[1,:]) / norm_KL 
sin_ = KL.dot(U[2,:]) / norm_KL 

R = np.array([[1,cos_,sin_],-sin_,cos_]])

ROT = (U.T).dot(R.dot(U))

M = (K+L) / 2

K_rot = M + ROT.dot( K - M )
L_rot = M + ROT.dot( L - M )

print(L_rot)
print(K_rot)
print(L_rot-K_rot)
print((L_rot-K_rot).dot(U[2,:]))