围绕矢量3d旋转点 [英] Rotate point around vector 3d
问题描述
我想围绕矢量(与P1和P2相交)将P3(附近)旋转x度.
I want to rotate a P3 (that is somewhere near) around a vector (that intersects P1 and P2) by x degrees.
P1和P2是2个点,它们与图像中的矢量(线)e相交.我进行了很多研究和搜索,发现了一些好的材料,但是我的三角学技能太差了.我需要PAWN(小),这里有一些代码,但实际上并没有按预期工作.如果有人可以帮助我,我将非常感激:)
P1 and P2 are 2 points that are intersected by vector(line) e from image. I've researched and searched a lot and found some good materials, but my trigonometry skills are so poor. I need this for PAWN (small), here i have some code, but doesn't really work as intended. If anyone can help me, i'll be very thankful :)
P1/P2/P3 =(x,y,z)
P1 / P2 / P3 = (x,y,z)
float vec[3];
SubtractVectors(P1, P2, vec);
float newp[3];
float rotation[4][4];
SetupMatrix(90.0, vec, rotation);
MultiplyMatrix(P3, rotation, newp);
//---------------------------------
//---------------------------------
stock void MultiplyMatrix(float input[3], float rotation[4][4], float output[3])
{
float input2[4];
input2[0] = input[0];
input2[1] = input[1];
input2[2] = input[2];
input2[3] = 1.0;
float output2[4];
for(int i = 0 ; i < 4 ; i++)
{
for(int j = 0 ; j < 4 ; j++)
{
output2[i] += rotation[i][j] * input2[j];
}
}
output[0] = output2[0];
output[1] = output2[1];
output[2] = output2[2];
}
stock void SetupMatrix(float angle, float vector[3], float rotation[4][4])
{
float L = (vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
angle = angle * M_PI / 180.0;
float u2 = vector[0] * vector[0];
float v2 = vector[1] * vector[1];
float w2 = vector[2] * vector[2];
rotation[0][0] = (u2 + (v2 + w2) * Cosine(angle)) / L;
rotation[0][1] = (vector[0] * vector[1] * (1 - Cosine(angle)) - vector[2] * SquareRoot(L) * Sine(angle)) / L;
rotation[0][2] = (vector[0] * vector[2] * (1 - Cosine(angle)) + vector[1] * SquareRoot(L) * Sine(angle)) / L;
rotation[0][3] = 0.0;
rotation[1][0] = (vector[0] * vector[1] * (1 - Cosine(angle)) + vector[2] * SquareRoot(L) * Sine(angle)) / L;
rotation[1][1] = (v2 + (u2 + w2) * Cosine(angle)) / L;
rotation[1][2] = (vector[1] * vector[2] * (1 - Cosine(angle)) - vector[0] * SquareRoot(L) * Sine(angle)) / L;
rotation[1][3] = 0.0;
rotation[2][0] = (vector[0] * vector[2] * (1 - Cosine(angle)) - vector[1] * SquareRoot(L) * Sine(angle)) / L;
rotation[2][1] = (vector[1] * vector[2] * (1 - Cosine(angle)) + vector[0] * SquareRoot(L) * Sine(angle)) / L;
rotation[2][2] = (w2 + (u2 + v2) * Cosine(angle)) / L;
rotation[2][3] = 0.0;
rotation[3][0] = 0.0;
rotation[3][1] = 0.0;
rotation[3][2] = 0.0;
rotation[3][3] = 1.0;
}
推荐答案
查看四元数,它们完全可以满足您的需要.如果您不像我那样使用它们,则:
look at quaternions they do exactly what you need. if you do not use them like me then:
-
构造表示旋转轴坐标系的变换矩阵
M
construct transform matrix
M
representing your rotational axis coordinate system
首先在这里查看:转换矩阵解剖.例如,原点是点P1
,因此X
轴是P2-P1
(旋转轴).让Q = (1.0,0.0,0.0)
或(0.0,1.0,0.0)
选择一个与X
轴向量不平行的轴,这样
first look here: transform matrix anatomy. For example origin is the point P1
So X
axis is P2-P1
(rotation axis). Let Q = (1.0,0.0,0.0)
or (0.0,1.0,0.0)
choose one that is not parallel to X
axis vector so
X = P2-P1
Q = (1.0,0.0,0.0) or (0.0,1.0,0.0)
Y = cros(X,Q)
Z = cros(X,Y)
现在,您可以否定轴或更改其叉积操作数顺序以匹配所需的轴方向(如果需要).同样不要忘记使所有轴单位矢量
now you can negate axises or change their cross product operands order to match the axises direction you need (if desired). Also do not forget to make all the axises unit vectors
现在也可以进行局部坐标系(LCS)旋转
在这里 LCS转换,在这里
look here LCS transforms and here LCS rotation around X axis lrotx C++ implementation search for lrotx
at the end of answer
现在
获取要旋转的 GCS 点P
以获得其 LCS 坐标(对于未旋转的变换矩阵M
)
take GCS point P
you want to rotate get its LCS coordinates (for unrotated transform matrix M
)
Q = inverse(M)*P // if `M` before rotation was one then you can do Q=P instead
旋转:
M = inverse(inverse(M)*rotation)
将Q
转换回 GCS
Q = M*Q
这就是全部...
[注释]
如果旋转方向错误,则只需取反其中一个轴...或使用取反的角度
if you have wrong rotation direction then just negate one of the axises ... or use negated angle
这篇关于围绕矢量3d旋转点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!