仅给定一个 3d 向量构建正交基
我正在寻找一种简单有效的方法来解决以下问题:
我有一个 3d 向量,我想获得一个正交基 (x, y, < strong>z),其中基向量之一(假设x)是给定向量。所以我正在寻找两个彼此垂直的向量,它们也垂直于我给定的向量。
我知道这有无穷多个解,但我不在乎得到哪一个,只要它满足上述要求并且得到它是简单而高效的。
I'm looking for a simple and efficient way to solve the following problem:
I have one vector in 3d and I want to get an orthonormal base (x, y, z) where one of the base vectors (let's say x) is the given vector. So I'm looking for two vectors, perpendicular to each other, that are also perpendicular to my given vector.
I know that this has infinite many solutions, but I don't care which one I get, as long as it satisfies the above requirements and getting it is simple and efficient.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们将
x
称为单位向量。调用u = (1,0,0)
。如果dot(u,x) ~= 0
,则取u = (0,1,0)
。然后,y = x ^ u
和z = x ^ y
。Let's call
x
your unit vector. Callu = (1,0,0)
. Ifdot(u,x) ~= 0
, then takeu = (0,1,0)
. Then,y = x ^ u
andz = x ^ y
.要摆脱 Tibur 的 if,您可以通过使用便宜的 mul+float 转换来获得轻微的改进,
在叉积之后进行向量比较可以为您提供比检查是否 u == x 更稳定的解决方案,float 转换取决于您的体系结构,但适用于大多数编译器/平台。
基本上,当 x 与 u 共线时,这种基函数总是具有奇点,因此请尝试从上下文中明智地选择 u,记住 u 不必是常数。在大多数情况下,您可以选择 u 来与一个简单的情况一致,这样您就可以消除奇点并保持整体变换的稳定。
To get rid of Tibur's if, you can get a slight improvement by using a cheap(er) mul+float cast
Doing a vector-compare after the cross product gives you a more stable solution than checking if u == x, the float cast depends on your architecture, but works in most compilers/platforms.
Basically this kind of base function will always have a singularity when x is co-linear with u, so try to pick u wisely from the context, remember that u does not have to be a constant. In most cases you can choose u to coincide with a trivial case so you smooth out the singularity and keep your overall transform stable.