WPF Matrix3D.Rotate() 函数不准确吗?
我目前正在尝试了解 WPF,并且正处于使用 Matrix3D 结构在坐标空间之间进行转换的阶段。
在意识到 WPF 在 Point3D 和 Vector3D 之间存在差异(那是两个小时我永远不会回来......)之后,我可以设置一个平移矩阵。我现在尝试引入旋转矩阵,但它似乎给出了不准确的结果。这是我的世界坐标转换代码...
private Point3D toWorldCoords(int x, int y)
{
Point3D inM = new Point3D(x, y, 0);
//setup matrix for screen to world
Screen2World = Matrix3D.Identity;
Screen2World.Translate(new Vector3D(-200, -200, 0));
Screen2World.Rotate(new Quaternion(new Vector3D(0, 0, 1), 90));
//do the multiplication
Point3D outM = Point3D.Multiply(inM, Screen2World);
//return the transformed point
return new Point3D(outM.X, outM.Y, m_ZVal);
}
平移似乎工作正常,但旋转 90 度似乎返回浮点不准确。矩阵的 Offset 行似乎偏离了一个微小的因素(无论哪种方式都是 0.000001),这会在渲染上产生锯齿。我在这里遗漏了什么,还是我只需要手动对矩阵进行舍入?
干杯
I'm currently trying to wrap my head around WPF, and I'm at the stage of converting between coordinate spaces with Matrix3D structures.
After realising WPF has differences between Point3D and Vector3D (thats two hours I'll never get back...) I can get a translation matrix set up. I'm now trying to introduce a rotation matrix, but it seems to be giving innacurate results. Here is my code for my world coordinate transformation...
private Point3D toWorldCoords(int x, int y)
{
Point3D inM = new Point3D(x, y, 0);
//setup matrix for screen to world
Screen2World = Matrix3D.Identity;
Screen2World.Translate(new Vector3D(-200, -200, 0));
Screen2World.Rotate(new Quaternion(new Vector3D(0, 0, 1), 90));
//do the multiplication
Point3D outM = Point3D.Multiply(inM, Screen2World);
//return the transformed point
return new Point3D(outM.X, outM.Y, m_ZVal);
}
The translation appears to be working fine, the rotation by 90 degrees however seems to return floating point inaacuracies. The Offset row of the matrix seems to be off by a slight factor (0.000001 either way) which is producing aliasing on the renders. Is there something I'm missing here, or do I just need to manually round the matrix up?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使使用双精度数学,矩阵乘法也会出现舍入误差。
您将执行 4 次乘法,然后对新矩阵中每个元素的结果求和。
最好将 Screen2World 矩阵设置为平移矩阵,而不是将单位矩阵乘以平移,然后乘以旋转。这是两次矩阵乘法,而不是一次和(超过)两倍的舍入误差。
Even with double precision mathematics there will be rounding errors with matrix multiplication.
You are performing 4 multiplications and then summing the results for each element in the new matrix.
It might be better to set your
Screen2World
matrix up as the translation matrix to start with rather than multiplying an identity matrix by your translation and then by the rotation. That's two matrix multiplications rather than one and (more than) twice the rounding error.