Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a single arbitrary unit vector, what is the best method to compute an arbitrary orthogonal unit vector?

Essentially the same question was asked here, but in a non-programming context. A suggested solution is take { y, -x, 0 }. This would work for all vectors that have an x or y component, but fails if the vector is equal to + or - { 0, 0, 1 }. In this case we would get { 0, 0, 0 }.

My current solution (in c++):

// floating point comparison utilizing epsilon
bool is_equal(float, float);

// ...

vec3 v = /* some unit length vector */

// ...

// Set as a non-parallel vector which we will use to find the 
//   orthogonal vector. Here we choose either the x or y axis.
vec3 orthog;
if( is_equal(v.x, 1.0f) )
  orthog.set(1.0f, 0.0f, 0.0f);
else
  orthog.set(0.0f, 1.0f, 0.0f);

// Find orthogonal vector
orthog = cross(v, orthog);
orthog.normalize(); 

This method works, but I feel that there may be a better method and my searches turn up nothing more.


[EDIT]

Just for fun I did a quick code up of naive implementations of each of the suggested answers in c++ and verified they all worked (though some don't always return a unit vector naturally, I added a noramlize() call where needed).

My original idea:

vec3 orthog_peter(vec3 const& v)
{
  vec3 arbitrary_non_parallel_vec = v.x != 1.0f ? vec3(1.0, 0.0f, 0.0f) : vec3(0.0f, 1.0f, 0.0f);
  vec3 orthog = cross(v, arbitrary_non_parallel_vec);

  return normalize( orthog );
}

https://stackoverflow.com/a/19650362/2507444

vec3 orthog_robert(vec3 const& v)
{
  vec3 orthog;
  if(v.x == 0.0f && v.y == 0.0f)
    orthog = vec3(1.0f, 1.0f, 0.0f);
  else if(v.x == 0.0f)
    orthog = vec3(1.0f, v.z / v.y, 1.0f);
  else if(v.y == 0.0f)
    orthog = vec3(-v.z / v.x, 1.0f, 1.0f);
  else
    orthog = vec3(-(v.z + v.y) / v.x, 1.0f, 1.0f);

  return normalize(orthog);
}

https://stackoverflow.com/a/19651668/2507444

// NOTE: u and v variable names are swapped from author's example
vec3 orthog_abhishek(vec3 const& v)
{
  vec3 u(1.0f, 0.0f, 0.0f);
  float u_dot_v = dot(u, v);

  if(abs(u_dot_v) != 1.0f)
    return normalize(u + (v * -u_dot_v));
  else
    return vec3(0.0f, 1.0f, 0.0f);
}

https://stackoverflow.com/a/19658055/2507444

vec3 orthog_dmuir(vec3 const& v)
{
  float length = hypotf( v.x, hypotf(v.y, v.z));
  float dir_scalar = (v.x > 0.0) ? length : -length;
  float xt = v.x + dir_scalar;
  float dot = -v.y / (dir_scalar * xt);

  return vec3(
    dot * xt, 
    1.0f + dot * v.y, 
    dot * v.z);
};
like image 614
Peter Clark Avatar asked Nov 01 '25 01:11

Peter Clark


1 Answers

Another way is to use Householder reflectors.

We can find a reflector Q that maps our vector to a multiple of (1,0,0). Applying Q to (0,1,0) will give a vector perpendicular to our vector. One advantage of this method is that it applies to any number of dimensions; another is that we can get the other vector(s) perpendicular to the original and the new: apply Q to (0,0,1). It might sound complicated, but here's the C code for 3d (xp,yp,zp is the required vector, and has length 1; as written everything is a double, but you could use float instead and use hypotf instead of hypot)

l = hypot( x, hypot(y,z));
s = (x > 0.0) ? l : -l;
xt = x + s;
dot = -y/(s*xt);
xp = dot*xt;
yp = 1.0 + dot*y;
zp = dot*z;
like image 143
dmuir Avatar answered Nov 03 '25 04:11

dmuir