Sign in to follow this  
knarkowicz

Generate random perpendicular unit vector

Recommended Posts

Hi,

 

I have a unit vector v1 and would like to generate a random perpendicular unit vector v2 with uniform or close to uniform distribution.

 

Potential solution is to generate a random unit vector and cross it with v1 to get v2. Now this has two issues - non uniform distribution and for some inputs it generates degenerate vectors, so it requires running in a loop until a valid vector is returned.

 

My current solution is to generate a random angle. Build a rotation matrix M using v1 and two perpendicular vectors. Those vectors are generated by crossing v1 with axis of it's smallest component and by crossing result with v1. Finally transform vector [ cos(randomAngle), sin(randomAngle), 0 ] using rotation matrix M.

 

It works, but I feel that I'm doing excessive amount of calculations and there is a much simpler way to solve it.

Share this post


Link to post
Share on other sites

My current solution is to generate a random angle. Build a rotation matrix M using v1 and two perpendicular vectors. Those vectors are generated by crossing v1 with axis of it's smallest component and by crossing result with v1. Finally transform vector [ cos(randomAngle), sin(randomAngle), 0 ] using rotation matrix M.

If you call your two perpendicular vectors v2 and v3, isn't it equivalent to just do: (v2 * cos(randomAngle)) + (v3 * sin(randomAngle)); ?

Not much less work I suppose, but a little quicker. Edited by C0lumbo

Share this post


Link to post
Share on other sites
You can avoid sin and cos by generating two random numbers x and y in the range -1 to +1, rejecting if the two are outside the unit circle or both zero, then normalising and multiplying by the perpendicular vectors.

Share this post


Link to post
Share on other sites

You can avoid sin and cos by generating two random numbers x and y in the range -1 to +1, rejecting if the two are outside the unit circle or both zero, then normalising and multiplying by the perpendicular vectors.

 

Rejection sampling will cause you to reject (4 - pi) / 4 = 21% of samples taken, and will thus introduce branching in the computation (as well as an extra normalization). It is probably more efficient to just use sin and cos, depending on your platform.

Share this post


Link to post
Share on other sites
This is what I would do:
http://pastebin.com/fr4aU0cw (needs C++11)

1. Generate a random unit vector
2. Make it orthogonal to your given vector by using the Gram-Schmidt Process (http://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process)

Edit: The snippet does not check, whether v and random u are linearly independent. Edited by Kepakiano

Share this post


Link to post
Share on other sites

You can avoid sin and cos by generating two random numbers x and y in the range -1 to +1, rejecting if the two are outside the unit circle or both zero, then normalising and multiplying by the perpendicular vectors.

 
Rejection sampling will cause you to reject (4 - pi) / 4 = 21% of samples taken, and will thus introduce branching in the computation (as well as an extra normalization). It is probably more efficient to just use sin and cos, depending on your platform.
No doubt it could be made branchless, if running in a loop. If not in a loop, it's unlikely to be a hotspot.

Really, I was just pointing out that it can be done without trig functions. The OP never mentioned a performance issue.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this