如何使用3x3旋转矩阵和平移矢量应用变换? [英] How to apply transformation using 3x3 rotation matrix and a translation vector?
问题描述
我正在使用ARCore进行增强现实项目。每次启动应用程序时,ARCore的坐标系都会更改,并将初始位置作为原点。我在另一个坐标系中有5个点,我可以使用ARCore增强图像在Unity世界空间中找到这些位置中的4个。当然,这些点在我的其他坐标系中具有不同的值。我必须使用其在其他坐标系中的位置来找到第5个点在Unity世界空间中的位置。
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)
将其应用于我的第五点时我有麻烦了。当最初的4个对象和参考对象处于相同方向时,我的翻译效果很好。但是,当旋转参考对象时,此平移不起作用。这当然很有意义,因为我只做了翻译。我的问题是我该如何对第五点进行旋转和平移。或者有没有办法将我的3x3旋转矩阵转换为Unity 4x4Matrix
,因为那样我就可以使用 Matrix4x4.MultiplyPoint3x4
。或者可以将我的3x3旋转矩阵转换为四元数
,让我使用 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.
如果有人可以给我一些提示或提供一种更好的方法来找到第五点的方法,我将感到非常高兴。谢谢!
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屋!