2016-10-06 13 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) 

처음인가 회전한다.

  • 는 3 회전
  • 가 해결되지 순서의 다양한 세트를 구성하여 회전을 얻을 수 있다는 것을 알고 있어야합니다, 그것은 X-Y-Z 수 또는 Z-Y-X 또는 Z-X-Z 또는 어떤 조합 할 수있다. 시퀀스는 그것이
  • 변경으로 각의 값이 변경 될 "위험한"사용이 임계 값의 회전 (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)로 변환하는 회전입니다.