如何使用 3x3 旋转矩阵和平移向量应用变换? [英] How to apply transformation using 3x3 rotation matrix and a translation vector?
问题描述
我正在使用 ARCore 进行增强现实项目.每次启动应用程序时,ARCore 的坐标系都会发生变化,以初始位置为原点.我在另一个坐标系中有 5 个点,我可以使用 ARCore 增强图像在 Unity 世界空间中找到其中 4 个位置.当然,这些点在我的其他坐标系中具有不同的值.我必须使用其在其他坐标系中的位置在 Unity 世界空间中找到第 5 个点的位置.
I am working on an Augmented Reality project using ARCore. Coordinate system of ARCore changes every time you launch the application making the initial position as origin. I have 5 points in another coordinate system and i can find 4 of these positions in Unity world space using ARCore Augmented Image. These points have different values in my other coordinate system of course. I have to find position of a 5th point in Unity world space using its position in other coordinate system.
我已按照此教程来实现此目的.但由于 Unity 不支持 3x3 矩阵,我使用了 Accord.NET 框架.使用教程和 Accord 矩阵,我可以计算一个 3x3 Rotation
矩阵和一个 Translation
向量.
I have followed this tutorial to achieve this. But since Unity does not support 3x3 matrices i used Accord.NET framework. Using the tutorial and Accord matrices i can calculate a 3x3 Rotation
matrix and a Translation
vector.
但是,当我尝试使用 TestObject.transform.Translate(AccordtoUnity(Translation),Space.World)
将此应用于我的第 5 点时,我遇到了问题.当最初的 4 个对象和参考对象处于同一方向时,我的翻译工作完美.但是,当我的参考对象旋转时,此转换不起作用.这当然是有道理的,因为我只做过翻译.我的问题是如何将旋转和平移应用于我的第 5 点.或者有没有办法将我的 3x3 旋转矩阵和平移转换为 Unity 4x4Matrix
,从那时起我就可以使用 Matrix4x4.MultiplyPoint3x4
.或者是否可以将我的 3x3 旋转矩阵转换为 Quaternion
,让我使用 4x4Matrix.SetTRS
.我对这种转换有点困惑,因为 4x4Matrix
也包括缩放,但我没有做任何缩放.
However, when i tried to apply this to my 5th point using TestObject.transform.Translate(AccordtoUnity(Translation),Space.World)
i am having trouble. When the initial 4 objects and reference objects are at same orientation my translation works perfect. However, when my reference objects are rotated this translation does not work. This makes sense of course since i have only done a translation. My question is how can i apply rotation and translation to my 5th point. Or is there a way to convert my 3x3 rotation matrix and translation to Unity 4x4Matrix
since then i can use Matrix4x4.MultiplyPoint3x4
. Or is it possible to convert my 3x3 rotation matrix to a Quaternion
which let me use4x4Matrix.SetTRS
. I am a bit confused about this conversion because 4x4Matrix
includes scaling as well but i am not doing any scaling.
如果有人能给我一些提示或提供更好的方法来找到找到第 5 点的方法,我会很高兴.谢谢!
I would be happy if someone can give me some hint or offer a better approach to find a way to find 5th point. Thanks!
我实际上根据 Daveloper 的回答解决了这个问题.我构建了一个 Unity 4x4 矩阵,如下所示:
I actually solved the problem based on Daveloper's answer. I constructed a Unity 4x4 matrix like this:
[ R00 R01 R02 T.x ]
[ R10 R11 R12 T.y ]
[ R20 R21 R22 T.z ]
[ 0 0 0 1 ]
我测试了在 Unity 中创建原始对象并使用上面的矩阵应用平移和旋转,如下所示:
I tested this creating primitive objects in Unity and apply translation and rotation using the matrix above like this:
TestObject.transform.position = TransformationMatrix.MultiplyPoint3x4(TestObject.transform.position);
TestObject.transform.rotation *= Quaternion.LookRotation(TransformationMatrix.GetColumn(2), TransformationMatrix.GetColumn(1));
推荐答案
要使用 3x3 旋转矩阵和平移向量来设置变换,请使用
to use a 3x3 rotation matrix and a translation vector to set a transform, use
// rotationMatrixCV = your 3x3 rotation matrix; translation = your translation vector
var rotationMatrix = new Matrix4x4();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
rotationMatrix[i, j] = rotationMatrixCV[i, j];
}
}
rotationMatrix[3, 3] = 1f;
var localToWorldMatrix = Matrix4x4.Translate(translation) * rotationMatrix);
Vector3 scale;
scale.x = new Vector4(localToWorldMatrix.m00, localToWorldMatrix.m10, matrix.m20, localToWorldMatrix.m30).magnitude;
scale.y = new Vector4(localToWorldMatrix.m01, localToWorldMatrix.m11, matrix.m21, localToWorldMatrix.m31).magnitude;
scale.z = new Vector4(localToWorldMatrix.m02, localToWorldMatrix.m12, matrix.m22, localToWorldMatrix.m32).magnitude;
transform.localScale = scale;
Vector3 position;
position.x = localToWorldMatrix.m03;
position.y = localToWorldMatrix.m13;
position.z = localToWorldMatrix.m23;
transform.position = position;
Vector3 forward;
forward.x = localToWorldMatrix.m02;
forward.y = localToWorldMatrix.m12;
forward.z = localToWorldMatrix.m22;
Vector3 upwards;
upwards.x = localToWorldMatrix.m01;
upwards.y = localToWorldMatrix.m11;
upwards.z = localToWorldMatrix.m21;
transform.rotation = Quaternion.LookRotation(forward, upwards);
注意:
仅当此旋转和平移定义了您在世界中正在使用的坐标系中的第 5 个点的位置时,这才有用...
This is only useful if this rotation and translation define your 5th point's location in the world in the coordinate system that is actively being used...
如果您的旋转和平移还有其他意义,您将不得不做更多事情.如果您能准确定义此旋转和平移的含义,很高兴为您提供进一步帮助.
If your rotation and translation mean anything else, you'll have to do more. Glad to help further if you can define what this rotation and translation mean exactly.
这篇关于如何使用 3x3 旋转矩阵和平移向量应用变换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!