如何将两个向量之间的相同旋转应用于另一个向量

问题描述

比方说,我在一个单位球体周围有两个矢量,就像图中的黑色矢量一样。我想将相同旋转应用于始终为[1、0、0](图中蓝色为一个)的另一个向量。

enter image description here

我已经有很多代码可以做到这一点,但这并不完美。加工。 例如,当两个向量之间的距离小于45度时,它似乎可以工作:

enter image description here

看起来还可以 但是,通过另一对向量,我得到:

enter image description here

enter image description here

这是错误的。 为什么我认为这是错误的?我认为将两个黑色矢量之间的旋转应用于蓝色矢量将导致在相同纬度上的新旋转。但是,事实并非如此

这是我正在使用的代码。很抱歉,长度,但大部分用于绘图: (您需要pyquaternion库:http://kieranwynn.github.io/pyquaternion/

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from pyquaternion import Quaternion
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

plt.close('all')
fig = plt.figure(figsize=(10,10))
ax = fig.gca(projection='3d')
# draw sphere
u,v = np.mgrid[0:2*np.pi:20j,0:np.pi:10j]
x = np.cos(u)*np.sin(v)
y = np.sin(u)*np.sin(v)
z = np.cos(v)
ax.plot_wireframe(x,y,z,color=[1,0.2])

# draw a point
ax.scatter([0],[0],color="g",s=100)

# draw a vector
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d


class Arrow3D(FancyArrowPatch):
    def __init__(self,xs,ys,zs,*args,**kwargs):
        FancyArrowPatch.__init__(self,(0,0),**kwargs)
        self._verts3d = xs,zs

    def draw(self,renderer):
        xs3d,ys3d,zs3d = self._verts3d
        xs,zs = proj3d.proj_transform(xs3d,zs3d,renderer.M)
        self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))
        FancyArrowPatch.draw(self,renderer)

def norm_v(v):
    return v / np.linalg.norm(v)

def add_norm_vector(u,col="k",norm=True):
    if norm:
        u = norm_v(u)
    vh = Arrow3D([0,u[0]],[0,u[1]],u[2]],mutation_scale=20,lw=1,arrowstyle="-|>",color=col)
    ax.add_artist(vh)
    return vh

def rotation_v_respect_to_u(v,u):
    rot = Quaternion(axis=np.cross(v,u),angle=np.arccos(np.dot(u,v))).normalised
    vv = rot.rotate([1,0])
    return vv

## THIS ONE IS CORRECT
a=[0,1,0]
b = [-0.671868,2.399499,-0.04294401]

distance = rotation_v_respect_to_u(a,b)
add_norm_vector([1,0],'b')

add_norm_vector(a)
add_norm_vector(b)
add_norm_vector(distance,'r')

## THIS ONE IS NOT CORRECT!
a=[0,0]
b = [-3.7956853,-3.90564173,-1.713661]

distance = rotation_v_respect_to_u(a,'r')

重要位在函数rotation_v_respect_to_u中。该公式摘自这篇文章:Finding quaternion representing the rotation from one vector to another

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)