# What's a numerically stable, non-branching algorithm for choosing two vectors perpendicular to a given normal?

I have a unit vector $$N$$ which is the normal to some flat surface. I want to generate two other unit vectors $$U$$ and $$V$$ which are mutually perpendicular and lie in this surface, so $$(N,U,V)$$ will be a set of basis vectors. It does not matter how $$U$$ and $$V$$ are rotationally oriented.

In principle I can choose an arbitrary unit vector $$M \neq N$$ and take cross-products, but this isn't numerically stable in all cases.

Example of something which will not work, choosing $$M=N+(1,0,0)^T$$, because in the special case that $$M=(1,0,0)^T$$, the cross products will degenerate.

I can tweak this algorithm with suitable checks, obviously, but this will run on embedded hardware which doesn't handle conditional branches very well and performance is an issue.

Is there a numerically-stable, non-branching algorithm for picking $$U$$ and $$V$$ given $$N$$?

• Take a basis $v_k$ and apply Gram Schmidt to $N,v_1,...$ and remove the element that gets reduced to zero. (Hmm, I guess that latter step is branching?) – copper.hat Sep 15 '19 at 22:34