使用 4x4 矩阵变换 3D 平面

发布于 2024-12-09 04:14:57 字数 327 浏览 0 评论 0原文

我有一个由几个三角形组成的形状,它位于世界空间中的某个位置,具有缩放、旋转、平移功能。我还有一个平面,我想在其上投影(正交)形状。

我可以将形状中每个三角形的每个顶点与对象变换矩阵相乘,以找出它在世界坐标中的位置,然后将该点投影到平面上。

但我不需要绘制投影,而是想用形状的逆变换矩阵来变换平面,然后将所有顶点投影到(逆变换)平面上。因为它只需要我变换平面一次而不是每个顶点。

我的平面有法线 (xyz) 和距离 (d)。如何将它与 4x4 变换矩阵相乘才能得到正确的结果?

你能创建一个 vec4 作为 xyzd 并将其相乘吗?或者创建一个向量 xyz1 然后如何处理 d?

I have a shape made out of several triangles which is positioned somewhere in world space with scale, rotate, translate. I also have a plane on which I would like to project (orthogonal) the shape.

I could multiply every vertex of every triangle in the shape with the objects transformation matrix to find out where it is located in world coordinates, and then project this point onto the plane.

But I don't need to draw the projection, and instead I would like to transform the plane with the inverse transformation matrix of the shape, and then project all the vertices onto the (inverse transformed) plane. Since it only requires me to transform the plane once and not every vertex.

My plane has a normal (xyz) and a distance (d). How do I multiply it with a 4x4 transformation matrix so that it turns out ok?

Can you create a vec4 as xyzd and multiply that? Or maybe create a vector xyz1 and then what to do with d?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木槿暧夏七纪年 2024-12-16 04:14:57

您需要将您的平面转换为不同的表示形式。其中 N 是法线,O 是平面上的任意点。你已经知道的常态是你的 (xyz)。平面上的点也很简单,它是你的正常N乘以你的距离d

以正常方式将 O 变换为 4x4 矩阵,这将成为您的新 O。您将需要一个 Vector4 来与 4x4 矩阵相乘,将 W 分量设置为 1 (x, y, z, 1)。

同样通过 4x4 矩阵变换 N,但将 W 分量设置为 0 (x, y, z, 0)。将 W 分量设置为 0 意味着法线不会被转换。如果您的矩阵不仅仅由平移和旋转组成,那么这一步就不是那么简单了。您不必乘以变换矩阵,而是必须乘以矩阵的转置,即Matrix4.Transpose(Matrix4.Invert(Transform))这里有一个很好的解释。

您现在有了一个新的法线向量N 和一个新的位置向量O。不过我想你又想要 xyzd 形式吗?没问题。和以前一样,xyz 是正常的 N ,剩下的就是计算 d 了。 d 是平面沿法向量距原点的距离。因此,它只是ON 的点积。

给你了!如果你告诉我你用什么语言来做这件事,我也很乐意用代码输入它。

编辑,伪代码:

平面是vector3 xyz数字d,矩阵是matrix4x4 M

vector4 O = (xyz * d, 1)
vector4 N = (xyz, 0)
O = M * O
N = transpose(invert(M)) * N
xyz = N.xyz
d = dot(O.xyz, N.xyz)

xyzd 代表新平面

You need to convert your plane to a different representation. One where N is the normal, and O is any point on the plane. The normal you already know, it's your (xyz). A point on the plane is also easy, it's your normal N times your distance d.

Transform O by the 4x4 matrix in the normal way, this becomes your new O. You will need a Vector4 to multiply with a 4x4 matrix, set the W component to 1 (x, y, z, 1).

Also transform N by the 4x4 matrix, but set the W component to 0 (x, y, z, 0). Setting the W component to 0 means that your normals won't get translated. If your matrix is composed of more that just translating and rotating, then this step isn't so simple. Instead of multiplying by your transformation matrix, you have to multiply by the transpose of the inverse of the matrix i.e. Matrix4.Transpose(Matrix4.Invert(Transform)), there's a good explanation on why here.

You now have a new normal vector N and a new position vector O. However I suppose you want it in xyzd form again? No problem. As before, xyz is your normal N all that's left is to calculate d. d is the distance of the plane from the origin, along the normal vector. Hence, it is simply the dot product of O and N.

There you have it! If you tell me what language you're doing this in, I'd happily type it up in code as well.

EDIT, In pseudocode:

The plane is vector3 xyz and number d, the matrix is a matrix4x4 M

vector4 O = (xyz * d, 1)
vector4 N = (xyz, 0)
O = M * O
N = transpose(invert(M)) * N
xyz = N.xyz
d = dot(O.xyz, N.xyz)

xyz and d represent the new plane

爱殇璃 2024-12-16 04:14:57

这个问题有点老了,但我想纠正已接受的答案。
您不需要转换您的平面表示。

任意点 v=(x,y,z ,1) 位于平面上 p=(a,b,c,d) 如果 ax+by+cz+d
它可以写成点积: pt v=0

您正在寻找由 4x4 矩阵转换的平面 p' M
出于同样的原因,您必须拥有 p't Mv=0

所以 p't Mv=pt v 并进行一些安排 p'=M^T p


TLDR :如果 p=(a,b,c,d), p' = 转置(逆(M))*p

This question is a bit old but I would like to correct the accepted answer.
You do not need to convert your plane representation.

Any point v=(x,y,z,1) lies on the plane p=(a,b,c,d) if ax+by+cz+d
It can be written as dot product : pt v=0

You are looking for the plane p' transformed by your 4x4 matrix M.
For the same reason, you must have p't Mv=0

So p't Mv=pt v and with some arrangements p'=M^T p


TLDR : if p=(a,b,c,d), p' = transpose(inverse(M))*p

素衣风尘叹 2024-12-16 04:14:57

符号

  • n 是表示为 (1x3) 行向量的法线
  • n' 是根据变换矩阵 n 的变换法线>T
  • (n|d) 是表示为 (1x4) 行向量的平面(其中 n 为平面法线,d代码> 平面到原点的距离)
  • (n'|d')(n|d) 根据变换矩阵 T
  • T 变换后的平面> 是一个(4x4)(仿射)列主变换矩阵(即变换列向量t 定义为t' = T t)。

变换法线n

n' = n adj(T)

变换平面(n|d)

(n'|d') = (n|d) adj(T)

这里,adj是矩阵的伴随,其定义如下:矩阵的逆矩阵和行列式项:

T^-1 = adj(T)/det(T)

注意

  • 佐数通常不等于变换矩阵 T 的逆矩阵。如果 T 包含反射,则 det(T) = -1 ,反转绕组顺序!

  • 在数学上不需要重新归一化 n'(但可能在数值上取决于实现),因为缩放由行列式负责。 感谢 Adrian Leonhard。

  • 您可以直接变换平面,而无需首先分解和重组平面(法线和点)。

Notation:

  • n is a normal represented as a (1x3) row-vector
  • n' is the transformed normal of n according to transform matrix T
  • (n|d) is a plane represented as a (1x4) row-vector (with n the plane's normal and d the plane's distance to the origin)
  • (n'|d') is the transformed plane of (n|d) according to transform matrix T
  • T is a (4x4) (affine) column-major transformation matrix (i.e. transforming a column-vector t is defined as t' = T t).

Transforming a normal n:

n' = n adj(T)

Transforming a plane (n|d):

(n'|d') = (n|d) adj(T)

Here, adj is the adjugate of a matrix which is defined as follows in terms of the inverse and determinant of a matrix:

T^-1 = adj(T)/det(T)

Note:

  • The adjugate is generally not equal to the inverse of a transformation matrix T. If T includes a reflection, det(T) = -1, reversing the winding order!

  • Re-normalizing n' is mathematically not required (but maybe numerically depending on the implementation) since scaling is taken care off by the determinant. Thanks to Adrian Leonhard.

  • You can directly transform the plane without first decomposing and recomposing a plane (normal and point).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文