给定一个任意单位向量,计算任意正交单位向量的最佳方法是什么? [英] Given a single arbitrary unit vector, what is the best method to compute an arbitrary orthogonal unit vector?
问题描述
基本上问了同样的问题here,但在非编程环境中.建议的解决方案是采用 { y, -x, 0 }.这适用于所有具有 x 或 y 分量的向量,但如果向量等于 + 或 - { 0, 0, 1 } 则失败.在这种情况下,我们将得到 { 0, 0, 0 }.
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 }.
我目前的解决方案(在 C++ 中):
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.
只是为了好玩,我在 C++ 中快速编写了每个建议答案的幼稚实现代码,并验证它们都有效(尽管有些并不总是自然地返回单位向量,但我在需要的地方添加了一个 noramlize() 调用).
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).
我最初的想法:
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);
};
推荐答案
好吧,这是解决问题的一种方法.给定一个向量 (a, b, c).解方程 (a, b, c) dot (aa, bb, cc) = 0 for aa, bb, and cc(并确保 aa, bb, and cc 不全为零),所以 (aa, bb, cc) 与 (a, b, c) 正交.我使用了 Maxima (http://maxima.sf.net) 来解决它.
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
请注意,我首先解决了特殊情况(a = 0 和 b = 0,或 a = 0,或 b = 0),因为找到的解决方案对于某些等于 0 的分量并不都是有效的.出现的 %r 变量是任意常数.我会将它们设置为 1 以获得一些特定的解决方案.
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屋!