开发者

Construct an orthonormal base given only one vector in 3d

开发者 https://www.devze.com 2023-04-12 14:13 出处:网络
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

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.


Let's call x your unit vector. Call u = (1,0,0). If dot(u,x) ~= 0, then take u = (0,1,0). Then, y = x ^ u and z = x ^ y.


To get rid of Tibur's if, you can get a slight improvement by using a cheap(er) mul+float cast

y = x ^ u
y.z += float(y==0); // this changes a zero-vector into (0,0,1)
z = x ^ y

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消