2016-10-06 14 views
0

私はpythonで書いているフィッティングルーチンの座標変換をランダムに生成しようとしています。原点についてのデータ(一組の[x、y、z]座標)を回転させたい、理想的にはランダムに生成された一連の法線ベクトルを使用して、私は既に平面を定義しています - それはz = 0の平面にあるように定義されています。ランダムに生成された法線ベクトルの座標変換

ここで私の変換行列があると、世話をするべきコードのスニペットがあります。私は私の法線ベクトルから変換行列を得る方法と、これに対してnumpyよりも複雑なものが必要なのかどうかは分かりません。

import matplotlib as plt 
import numpy as np 
import math 

origin = np.array([35,35,35]) 
normal = np.array([np.random.uniform(-1,1),np.random.uniform(-1,1),np.random.uniform(0,1)]) 
mag = np.sum(np.multiply(normal,normal)) 
normal = normal/mag 

a = normal[0] 
b = normal[1] 
c = normal[2] 

#I know this is not the right transformation matrix but I'm not sure what is... 
#Looking for the steps that will take me from the normal vector to this transformation matrix 
rotation = np.array([[a, 0, 0], [0, b, 0], [0, 0, c]]) 

#Here v would be a datapoint I'm trying to shift? 
v=(test_x,test_y,test_z) 
s = np.subtract(v,origin) #shift points in the plane so that the center of rotation is at the origin 
so = np.multiply(rotation,s) #apply the rotation about the origin 
vo = np.add(so,origin) #shift again so the origin goes back to the desired center of rotation 

x_new = vo[0] 
y_new = vo[1] 
z_new = vo[2] 

fig = plt.figure(figsize=(9,9)) 
plt3d = fig.gca(projection='3d') 
plt3d.scatter(x_new, y_new, z_new, s=50, c='g', edgecolor='none') 
+0

あなたの質問は何ですか?現在のコードは有効かどうか?ありがとう。 –

+0

私の質問は、どのように法線ベクトルから正しい変換行列を得るのですか? – Arnold

+0

あなたの質問が分かりました。Z軸の周りを回転させることができるので、いくつかの変換行列が正しい答えを与えることができます。代わりにhttp://math.stackexchange.com/を試してみてください。 –

答えて

0

数学スタック交換の上の人のおかげで、私は答えがあります。しかし、翻訳を実行する必要がある場合は、正常なベクトルとポイントで平面を定義しているため、通常のベクトルは変化しますが、ポイントはそうではないため、実行しないと機能しません。ここに私のために働いたものです。

import matplotlib as plt 
import numpy as np 
import math 

def unit_vector(vector): 
    """ Returns the unit vector of the vector. """ 
    return vector/np.linalg.norm(vector) 

cen_x, cen_y, cen_z = 35.112, 35.112, 35.112 
origin = np.array([[cen_x,cen_y,cen_z]]) 

z_plane_norm = np.array([1,1,0]) 
z_plane_norm = unit_vector(z_plane_norm) 

normal = np.array([np.random.uniform(-1,1),np.random.uniform(-1,1),np.random.uniform(0,1)]) 
normal = unit_vector(normal) 

a1 = normal[0] 
b1 = normal[1] 
c1 = normal[2] 

rot = np.matrix([[b1/math.sqrt(a1**2+b1**2), -1*a1/math.sqrt(a1**2+b1**2), 0], [a1*c1/math.sqrt(a1**2+b1**2), b1*c1/math.sqrt(a1**2+b1**2), -1*math.sqrt(a1**2+b1**2)], [a1, b1, c1]]) 

init = np.matrix(normal) 

fin = rot*init.T 
fin = np.array(fin) 

# equation for a plane is a*x+b*y+c*z+d=0 where [a,b,c] is the normal 
# so calculate d from the normal 
d1 = -origin.dot(normal) 

# create x,y 
xx, yy = np.meshgrid(np.arange(cen_x-0.5,cen_x+0.5,0.05),np.arange(cen_y-0.5,cen_y+0.5,0.05)) 

# calculate corresponding z 
z1 = (-a1 * xx - b1 * yy - d1) * 1./c1 

#------------- 

a2 = fin[0][0] 
b2 = fin[1][0] 
c2 = fin[2][0] 

d2 = -origin.dot(fin) 
d2 = np.array(d2) 
d2 = d2[0][0] 

z2 = (-a2 * xx - b2 * yy - d2) * 1./c2 

#------------- 

# plot the surface 
fig = plt.figure(figsize=(9,9)) 
plt3d = fig.gca(projection='3d') 

plt3d.plot_surface(xx, yy, z1, color='r', alpha=0.5, label = "original") 
plt3d.plot_surface(xx, yy, z2, color='b', alpha=0.5, label = "rotated") 


plt3d.set_xlabel('X (Mpc)') 
plt3d.set_ylabel('Y (Mpc)') 
plt3d.set_zlabel('Z (Mpc)') 

plt.show() 

あなたにも翻訳を実行する必要がある場合、私はhereのオフに働いていた完全な答えを参照してください。

2

あなたは回転行列の概念が間違っていると思います。回転行列はある角度の回転を定義し、対角構造を持つことはできません。

あなたは、X軸周りの回転の組成物としてあらゆる回転を想像次いで、Y軸回り、次にZ軸周りに、あなたは各マトリックスを構築することができ、行列の積として最終的な回転を構成する場合

R = Rz*Ry*Rx 
Rotated_item = R*original_item 

又は

Rotated_item = np.multiply(R,original_item) 

式中Rxは第1の印加回転です。
は、あなたがそれが固定されていない、それはX-Y-ZまたはZ-Y-XまたはZ-X-Zまたは何でも組み合わせ可能性がありシーケンス3回転

  • の多くの異なるセットを構成することで、あなたの回転を得ることができ

    • ことに注意してください。シーケンスはそれが
    • を変更するような角度の値が変更される可能性があり、「危険」の使用は、これは重要な値の回転(90-180-270-360度)のための行列の周りの各一回転行列を構成する方法

    1軸ですか? this image from wikipediaを参照してください。ナンシーはあなたが必要とするすべてのものを持っています。

    これで3つの角度値を定義するだけです。 疑問に書いているようにランダム正規化ベクトル(a、b、c)から3つの角度値を導出できますが、回転は別のベクトルのベクトルを変換するプロセスです。多分、 のようなものを指定しなければならないかもしれません。 "(0,0,1)を(a、b、c)に変換する原点を中心に回転Rを見つけたいと思います。完全に異なる回転R 'は、(1,0,0)を(a、b、c)に変換する回転である。

  • 関連する問題