Perpendicular Possibilities


Hi. I would like to humbly point to this article, featuring both branching and branchless implementations that I believe improve on both Hughes—Möller and Stark.


Yes, I'd seen that – nice work Sam!

I've been meaning to do a followup post to cover the paper Mikkel mentioned and the point David raised about stability. I'll include your method there too. Thanks for giving me the extra motivation!


Sorry I’m a bit late to the party, but is there any reason why crossing a vector with a swizzled version of itself with one component negated wouldn’t work? Eg:

cross(v, float3(v.y, -v.z, v.x))


Sorry for the delay in replying! In any formulation like this, there will be points of degeneracy (see Sam’s post that he links to in an earlier comment, which covers the Hairy Ball Theorem). For instance, in this case, if v is (-1, 1, 1) or (1, -1, -1), the cross product will give (0, 0, 0).


No need to apologise - I assumed you may not be paying much attention to a 6 year old post!

I completely missed that case; thanks for pointing out my error.

[edited to add]

Although I do wonder if there is any merit to the idea of trying to detect the singularity case after it has happened, rather than trying to predict it. Since the singularity is such a rare exception, it should be quite friendly to the GPU branch predictor.


Singularities are not the only concern, since there can be precision issues as the length of the vector goes to zero. This recent paper goes into depth on the topic and provides a numerically robust implementation, plus a branchless variant.