Perpendicular 3D vector

Started by
6 comments, last by steven katic 15 years, 5 months ago
Is there any way to obtain a 3D unit vector that's perfectly perpendicular to another 3D unit vector without randomness and conditional statements? In other words, I only have one 3D unit vector. I have nothing else to go on. I need to find another arbitrary 3D unit vector that is perpendicular to this one. It seems that the standard is to test two hard-coded vectors (such as up=0,1,0 and right=1,0,0) to see which best fits the current vector to perform a cross product. I'm working in a shader where conditional branches will be overkill, so I can't afford to test this way. Are there any other methods to do this? Any help is appreciated.
Advertisement
I don't know if there are better methods but I think it's a correct one.

Let u be your unit vector and i, j, k the canonical bases of R3. Using the Gram-Schmidt process you can orthogonalize the vectors {u, i, j} obtaining {u, e1, e2} where only one of the two vectors e1 or e2 can be zero. e1 and e2 are perpendicular and span a subspace of dimension 1 or 2 perpendicular to u. Every vector obtained by normalize(αe1 + βe2) with α != 0 and β != 0 is a non-zero vector perpendicular to u.
That doesn't seem to work if u is aligned with (1,0,0). For instance, if u=(1,0,0), e1=(0,0,0) and computing e2 requires a division by 0.

I don't see how you can get away without a conditional at some point...

Edit: Actually, there is no continuous function that maps unit-vectors into vector that are perpendicular to the input (see the hairy-ball theorem). This means that you have to introduce a discontinuity somehow. If you can't use a conditional, you need some other means of making the mapping discontinuous.
Quote:Original post by alvaro
That doesn't seem to work if u is aligned with (1,0,0). For instance, if u=(1,0,0), e1=(0,0,0) and computing e2 requires a division by 0.

I don't see how you can get away without a conditional at some point...

Edit: Actually, there is no continuous function that maps unit-vectors into vector that are perpendicular to the input (see the hairy-ball theorem). This means that you have to introduce a discontinuity somehow. If you can't use a conditional, you need some other means of making the mapping discontinuous.

[embarrass] You are right... Maybe using a texture?
Yeah, you can compute two perpendicular vectors in such a way that they won't both be 0 at the same time, and then return a weighted average of them, where the weights are controlled by a tiny texture. A cubic texture with no interpolation would do.

I have access to min(a,b) and max(a,b) in the shader. Those can sometimes be used in a clever way to compensate for no conditions. Would that be possible here? Some way to cross the existing vector with two others (like 0,1,0 and 1,0,0) and use min/max with the float results to secure something to work with?
`floor' or `sign' are better candidates.

this might help:

http://www.akiti.ca/RotateTrans.html

This topic is closed to new replies.

Advertisement