旋转矩阵给定角度和 X、Y、Z 中的点
我正在进行图像处理,我想根据角度、原点以及 x、y 和 z 坐标旋转 xyz 空间中的所有像素。
我只需要设置正确的矩阵(4x4),然后我就可以从那里开始了。角度以度为单位,而不是弧度,x、y、z 都将从 -1 到 1(浮点)
编辑:
好的,这是我编写的代码,用于围绕由 定义的给定线进行旋转原点和 X、Y、Z 坐标。
float ang = angD * (float)(Math.PI / 180); // from degrees to radians, if needed
//U = n*n(t) + cos(a)*(I-n*n(t)) + sin(a)*N(x).
var u = MatrixDouble.Identity(4); // 4x4 Identity Matrix
u = u.Multiply(Math.Cos(ang));
var n = new MatrixDouble(1, 4, new List<double> { x, y, z, 0 });
var nt = n.Transpose();
// This next part is the N(x) matrix. The data is inputted in Column
// first order and fills in the 4x4 matrix with the given 16 Doubles
var nx = new MatrixDouble(4, 4, new List<double> { 0, z, -y, 0, -z, 0, x, 0, y, -x, 0, 0, 0, 0, 0, 1 });
nx = nx.Multiply(Math.Sin(ang));
var ret = nt.Multiply(n);
ret[3, 3] = 1;
u = u.Subtract(ret);
u = ret.Add(u.Add(nx));
它有点复杂,我使用的是自定义 Matrix 库,但使用任何功能正常的 Matrix 库都应该不会太难实现。
唷,很多数学!
I am doing image manipulation and I want to rotate all of the pixels in xyz space based on an angle, the origin, and an x,y, and z coordinate.
I just need to setup the proper matrix (4x4) and then I will be good from there. The Angle is in degrees, not radians and the x,y,z are all going to be from -1 to 1 (floats)
EDIT:
Ok, here is the code that I whipped up to do the rotation about a given line defined by the origin and an X, Y, Z coorinate.
float ang = angD * (float)(Math.PI / 180); // from degrees to radians, if needed
//U = n*n(t) + cos(a)*(I-n*n(t)) + sin(a)*N(x).
var u = MatrixDouble.Identity(4); // 4x4 Identity Matrix
u = u.Multiply(Math.Cos(ang));
var n = new MatrixDouble(1, 4, new List<double> { x, y, z, 0 });
var nt = n.Transpose();
// This next part is the N(x) matrix. The data is inputted in Column
// first order and fills in the 4x4 matrix with the given 16 Doubles
var nx = new MatrixDouble(4, 4, new List<double> { 0, z, -y, 0, -z, 0, x, 0, y, -x, 0, 0, 0, 0, 0, 1 });
nx = nx.Multiply(Math.Sin(ang));
var ret = nt.Multiply(n);
ret[3, 3] = 1;
u = u.Subtract(ret);
u = ret.Add(u.Add(nx));
It's a little complicated and I'm using a custom Matrix library, but nothing up there should be too hard to implement with any functioning Matrix lib.
Phew, lots of math!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
完整的旋转矩阵是在 https://sites.google.com/site/glennmurray/glenn-murray-ph-d/rotation-matrices-and-formulas/rotation-about -三维任意轴。
摘自论文:
5.2 绕原点旋转的简化矩阵
注意,这里假设 (u, v, w) 是旋转轴的方向向量,并且 u^2 + v^2 + w^2 = 1。
< img src="https://i.sstatic.net/rFtsT.png" alt="绕原点旋转的简化 3D 矩阵。">
如果您想要旋转一个点 (x, y, z),然后我们可以获得七个变量的函数,该函数产生旋转点:
f(x, y, z, u, v, w, theta) =
本文还包括绕任意轴(不一定通过原点)旋转的矩阵和公式、Apache 许可证下可用的 Java 代码以及链接到说明旋转的 Web 应用程序。
The complete rotation matrices are derived and given at https://sites.google.com/site/glennmurray/glenn-murray-ph-d/rotation-matrices-and-formulas/rotation-about-an-arbitrary-axis-in-3-dimensions.
From the paper:
5.2 The simplified matrix for rotations about the origin
Note this assumes that (u, v, w) is a direction vector for the axis of rotation and that u^2 + v^2 + w^2 = 1.
If you have a point (x, y, z) that you want to rotate, then we can obtain a function of of seven variables that yields the rotated point:
f(x, y, z, u, v, w, theta) =
The paper also includes matrices and formulas for rotations about an arbitrary axis (not necessarily through the origin), Java code available under the Apache license, and a link to a web app that illustrates rotations.
使用 Matrix3D 结构 (MSDN) - 表示用于 3D 空间中转换的 4 x 4 矩阵
请查看此处的教程:构建 3D 引擎
本质上,矩阵是为 X、Y 和 Z 旋转构建的,然后您可以按任何顺序乘以旋转。
Use the Matrix3D Structure (MSDN) - Represents a 4 x 4 matrix used for transformations in 3-D space
Take a look here for a tutorial: Building a 3D Engine
Essentially, matrices are built for X, Y, and Z rotations and then you can multiply the rotations in any order.
函数
rotateAroundAxis()
围绕 3D 中的任何轴旋转点。这是我使用解析几何和编程对过程进行建模的 3D 旋转解决方案。该代码是 JavaScript 代码。Function
rotateAroundAxis()
rotates point around any axis in 3D. It is my solution to the rotation in 3D using analytic geometry and programming to model the process. The code is in JavaScript.