I have a unit vector $v = (x_v, y_v, z_v)$ in $\mathbb{R}^3$ and I want to construct another vector $u$ which is orthogonal to $v$. The construction process should be a straight-forward formula without singularities (divisions by zero, roots of negative number, etc.) and special cases.
For example, we can take the plane equation
$$ A(x-x_0) + B(y-y_0) + C(z-z_0) = 0 $$
for a plane which contains $(x_0, y_0, z_0)$ point and $(A,B,C)$ is a normal vector for it. In our case it boils down to
$$ x_vx + y_vy + z_vz = 0 $$
and finally
$$ x = \frac{- y_vy - z_vz}{x_v} $$
so we can chose any $y$ and $z$ and we'll get $x$ such that $(x,y,z)$ is orthogonal to $v$. But what if $x_v$ is zero? In this case the plane contains $Ox$ axis, thus the choice of $x$ does not matter for any $y$ and $z$ from the plane. But I want to implement the construction of such vector in a GPU shader program, so processing special cases (branching) is unwanted because:
- It breaks performance of the program
- $x_v$ may be arbitrary close but not equal to zero
- $x_v$ may smoothly vary with time, so special case may introduce some animation artifacts.
Another approach is to take another predefined (i.e. hard-coded) vector $a$ and make $u = v \times a$, but the only restriction on $v$ is it's unitness, so this approach faces same problems if $a$ and $v$ are collinear or close to that.
I also tried to represent $v$ as some rotation of $(1,0,0)$, but I ended up with
$$ \beta = \arcsin(-z), $$ $$ \cos{\gamma} = \frac{x}{\cos{\beta}}. $$
For $z = \pm{1}$ it faces the same problem as above and it corresponds to a case when we rotate $(0,0,1)$ about $Oz$ axis, which does not make sense and always equals to identity rotation.
Could you give me a clue how to construct an orthogonal vector without singularities or an outline of a proof if it's impossible?
This is a consequence of the hairy ball theorem.