给定一个任意单位向量,什么是计算任意正交单位向量的最佳方法是什么? [英] Given a single arbitrary unit vector, what is the best method to compute an arbitrary orthogonal unit vector?

查看:313
本文介绍了给定一个任意单位向量,什么是计算任意正交单位向量的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从本质上讲同一个问题被问<一href="http://stackoverflow.com/questions/3049509/given-vector-of-one-axis-how-do-i-find-vectors-of-other-two-axes/3049709#3049709"标题=这里>此处,但在非编程环境。一个建议的解决方案是采取{Y,-x 0 }。这将工作针对具有x或y分量的所有矢量,但失败,如果该载体是等于+或 - {0,0,<强> 1 }。在这种情况下,我们会得到{0,0, 0 }

我目前的解决方案(在C ++中):

  //浮点比较利​​用小量
布尔is_equal(浮动,浮动);

// ...

VEC3 V = / *一些单位长度向量* /

// ...

//设置作为非平行矢量我们将使用查找
//正交向量。这里我们选择其中的x或y轴。
VEC3 orthog;
如果(is_equal(V.X,1.0F))
  orthog.set(1.0F,0.0,0.0);
其他
  orthog.set(0.0,1.0F,0.0);

//查找正交向量
orthog =交(V,orthog);
orthog.normalize();
 

这个方法可行,但我觉得有可能是一个更好的方法,我的搜索​​露面罢了。


只是为了好玩我做了一个快速code可高达每一个在C ++中的参考答案的幼稚的实现并验证它们都工作(尽管有些并不总是自然地返回单位向量,我添加了一个noramlize()调用需要的地方)。

我最初的想法:

  VEC3 orthog_peter(VEC 3常量和放大器; 5)
{
  VEC3 arbitrary_non_parallel_vec = V.X!= 1.0F? VEC3(1.0,0.0,0.0):VEC3(0.0,1.0F,0.0);
  VEC3 orthog =交(V,arbitrary_non_parallel_vec);

  回归正常化(orthog);
}
 

<一个href="http://stackoverflow.com/a/19650362/2507444">http://stackoverflow.com/a/19650362/2507444

  VEC3 orthog_robert(VEC 3常量和放大器; 5)
{
  VEC3 orthog;
  如果(V.X == 0.0&功放;&安培; V.Y == 0.0)
    orthog = VEC3(1.0F,1.0F,0.0);
  否则,如果(V.X == 0.0)
    orthog = VEC3(1.0F,V.Z / V.Y,1.0F);
  否则,如果(V.Y == 0.0)
    orthog = VEC3(-v.z / V.X,1.0F,1.0F);
  其他
    orthog = VEC3( - (V.Z + V.Y)/ V.X,1.0F,1.0F);

  回归正常化(orthog);
}
 

<一个href="http://stackoverflow.com/a/19651668/2507444">http://stackoverflow.com/a/19651668/2507444

  //注:u和v变量名是从作者的例子交换
VEC3 orthog_abhishek(VEC 3常量和放大器; 5)
{
  VEC3 U(1.0F,0.0,0.0);
  浮u_dot_v =点(U,V);

  如果(绝对(u_dot_v)!= 1.0F)
    回归正常化(U +(V * -u_dot_v));
  其他
    返回VEC3(0.0,1.0F,0.0);
}
 

<一个href="http://stackoverflow.com/a/19658055/2507444">http://stackoverflow.com/a/19658055/2507444

  VEC3 orthog_dmuir(VEC 3常量和放大器; 5)
{
  浮长度= hypotf(V.X,hypotf(V.Y,V.Z));
  浮动dir_scalar =(V.X&GT; 0.0)?长度: - 长度;
  浮XT = V.X + dir_scalar;
  浮点= -v.y /(dir_scalar * XT);

  返回VEC3(
    点* XT,
    1.0F +点* V.Y,
    点* V.Z);
};
 

解决方案

好了,这里有一个去了解它的方式。让矢量(A,B,C)进行说明。解方程(A,B,C)的点(AA,BB,CC)= 0为AA,BB,和CC(并确保AA,BB,而cc不都是零),所以(AA,BB,CC )正交于(A,B,C)。我用千里马( http://maxima.sf.net )来解决这个问题。

 (%I42)解决([A,B,C]。[AA,BB,CC] = 0,[AA,BB,CC]),A = 0,B = 0;
(%O42)[AA =%R19,BB =%R20,CC = 0]
(%I43)解决([A,B,C] [AA,BB,CC] = 0,[AA,BB,CC])中,a = 0;
                                        %R21Ç
(%o43)[AA =%R22,BB =  -  ------,CC =%R21]
                                          b
(%I44)解决([A,B,C] [AA,BB,CC] = 0,[AA,BB,CC]。)时,b = 0;
                             %R23Ç
(%O44)[AA =  -  ------,BB =%R24,CC =%R23]
                               一个
(%I45)解决([A,B,C] [AA,BB,CC] = 0,[AA,BB,CC。);
                        R25%C +%R26 b
(%O45)[AA =  -  ---------------,BB =%R26,CC =%R25]
                               一个
 

请注意,我已经首次解决了特殊的情况(α= 0和b = 0,或a = 0时,或b = 0),因为发现了解决方案并不都是有效的某些组件等于零。这显示在%R变量是任意常数。我将它们设置为1,以得到一些具体的解决方案。

 (%I52)SUBST([%R19 = 1%R20 = 1]%O42);
(%O52)[[AA = 1,BB = 1,CC = 0]]
(%I53)实名词([%R21 = 1,%R22 = 1],%o43);
                                          C
(%O53)[AA = 1,BB =  -   - ,CC = 1]
                                          b
(%I54)实名词([%R23 = 1,%R 24 = 1],%O44);
                                  C
(%o54)[AA =  -   - ,BB = 1,CC = 1]
                                  一个
(%I55)实名词([%R25 = 1,%R 26 = 1],%O45);
                                C + B
(%O55)[AA =  -  -----,BB = 1,CC = 1]
                                  一个
 

希望这有助于。祝你好运和放大器;保持良好的工作。

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

http://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);
}

http://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);
}

http://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);
};

解决方案

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.

这篇关于给定一个任意单位向量,什么是计算任意正交单位向量的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆