Given a single arbitrary unit vector, what is the

2019-05-17 14:10发布

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);
};

4条回答
Anthone
2楼-- · 2019-05-17 14:33

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;
查看更多
淡お忘
3楼-- · 2019-05-17 14:47

Heres a C version which uses the dominant axis to give a more deterministic result.

The caller needs to normalize the result of ortho_v3_v3.

inline int axis_dominant_v3_single(const float vec[3])
{
    const float x = fabsf(vec[0]);
    const float y = fabsf(vec[1]);
    const float z = fabsf(vec[2]);
    return ((x > y) ?
           ((x > z) ? 0 : 2) :
           ((y > z) ? 1 : 2));
}

/**
 * Calculates \a p - a perpendicular vector to \a v
 *
 * \note return vector won't maintain same length.
 */
void ortho_v3_v3(float p[3], const float v[3])
{
    const int axis = axis_dominant_v3_single(v);

    assert(p != v);

    switch (axis) {
        case 0:
            p[0] = -v[1] - v[2];
            p[1] =  v[0];
            p[2] =  v[0];
            break;
        case 1:
            p[0] =  v[1];
            p[1] = -v[0] - v[2];
            p[2] =  v[1];
            break;
        case 2:
            p[0] =  v[2];
            p[1] =  v[2];
            p[2] = -v[0] - v[1];
            break;
    }
}
查看更多
相关推荐>>
4楼-- · 2019-05-17 14:49

Well, here's one way to go about it. Let a vector (a, b, c) be given. Solve the equation (a, b, c) dot (aa, bb, cc) = 0 for aa, bb, and cc (and ensuring that aa, bb, and cc are not all zero), so (aa, bb, cc) is orthogonal to (a, b, c). I've used Maxima (http://maxima.sf.net) to solve it.

(%i42) solve ([a, b, c] . [aa, bb, cc] = 0, [aa, bb, cc]), a=0, b=0;
(%o42)                 [[aa = %r19, bb = %r20, cc = 0]]
(%i43) solve ([a, b, c] . [aa, bb, cc] = 0, [aa, bb, cc]), a=0;
                                        %r21 c
(%o43)              [[aa = %r22, bb = - ------, cc = %r21]]
                                          b
(%i44) solve ([a, b, c] . [aa, bb, cc] = 0, [aa, bb, cc]), b=0;
                             %r23 c
(%o44)              [[aa = - ------, bb = %r24, cc = %r23]]
                               a
(%i45) solve ([a, b, c] . [aa, bb, cc] = 0, [aa, bb, cc]);
                        %r25 c + %r26 b
(%o45)         [[aa = - ---------------, bb = %r26, cc = %r25]]
                               a

Note that I've solved special cases first (a = 0 and b = 0, or a = 0, or b = 0) since the solutions found aren't all valid for some components equal to zero. The %r variables which appear are arbitrary constants. I'll set them equal to 1 to get some specific solutions.

(%i52) subst ([%r19 = 1, %r20 = 1], %o42);
(%o52)                    [[aa = 1, bb = 1, cc = 0]]
(%i53) subst ([%r21 = 1, %r22 = 1], %o43);
                                          c
(%o53)                   [[aa = 1, bb = - -, cc = 1]]
                                          b
(%i54) subst ([%r23 = 1, %r24 = 1], %o44);
                                  c
(%o54)                   [[aa = - -, bb = 1, cc = 1]]
                                  a
(%i55) subst ([%r25 = 1, %r26 = 1], %o45);
                                c + b
(%o55)                 [[aa = - -----, bb = 1, cc = 1]]
                                  a

Hope this helps. Good luck & keep up the good work.

查看更多
相关推荐>>
5楼-- · 2019-05-17 14:49

You need to choose a point v that is not equal to zero and not on the line joining origin with the given unit vector u.

As already suggested, you can choose a unit vector on any axis so long as that point satisfies the above condition. If the point u already lies on an axis, then just choose any other axis for point v.

Then you need to solve the equation (v + tu).u = 0. (just solve for t)

Ofcourse you will need to normalize it to get the orthogonal unit vector.

enter image description here

查看更多
登录 后发表回答