简体   繁体   English

相对于z轴旋转球体集合

[英]rotating collection of spheres with respect to the z-axis

I have coordinates (x,y,z) of the center of spheres stored in a numpy array. 我有一个存储在numpy数组中的球心坐标(x,y,z)。 I want be able to rotate the spheres with respect to the z-axis but am getting weird results. 我希望能够相对于z轴旋转球体,但结果却很奇怪。 My code does the rotation but it seems to also be moving it up and right. 我的代码进行旋转,但它似乎也在向上和向右移动。 Maybe this is an intended result from the rotation, but I don't think it is. 也许这是轮换的预期结果,但我认为不是。 Here is my code: 这是我的代码:

theta = math.pi/6
ct = math.cos(theta)
st = math.sin(theta)
z = np.array([[ct, -st, 0], [st, ct, 0], [0, 0, 1]])
self.atoms = np.array([[90,100, 1], [140,100, 1]])
self.atoms = self.atoms.dot(z)

Here is what the image looked like before the rotation: 这是旋转之前图像的外观:

在此处输入图片说明

and here is what it looks like after: 如下所示:

在此处输入图片说明

You have to translate the entire system so that the center of the rotation is the center of the system. 您必须平移整个系统,以使旋转中心成为系统的中心。

The equations used for rotation that you use work only if rotating around the origin. 仅当绕原点旋转时,用于旋转的方程式才起作用。

For the translation you can also multiply with a matrix having the direction of translation as the last row. 对于平移,您还可以将平移方向作为最后一行的矩阵相乘。

Anyway, the entire transformation is P' = inv(T) * R * T * P (where P is each point of the figure and P' is where it will be in the final result, see example) For the inverse of the translation matrix simply negate the sign of the translation components. 无论如何,整个变换为P' = inv(T) * R * T * P (其中P是图形的每个点, P'是最终结果中的位置,请参见示例)。矩阵简单地抵消了翻译成分的符号。

Edit (worked out example -- you might have to transpose everything -- switch rows with columns): 编辑 (解决的示例-您可能必须转置所有内容-用列切换行):

You start with the points placed at: 您从以下几点开始:

atoms =
    90   140
   100   100
     1     1

which is presented in 呈现在

两个水平点

Then you apply a rotation with matrix 然后用矩阵旋转

R =
   0.86603  -0.50000   0.00000
   0.50000   0.86603   0.00000
   0.00000   0.00000   1.00000

and get a result of 并得到的结果

R * atoms =
    27.9423    71.2436
   131.6025   156.6025
     1.0000     1.0000

which translates into (as you observed, the red points are the new ones) 转换为(如您所观察到的,红点是新的)

旋转但不行

The problem is that by doing R * atoms we rotate around the origin. 问题是通过做R * atoms我们绕原点旋转。 In the following figure, the angle between the two blue lines is exactly pi/6 在下图中,两条蓝线之间的角度正好是pi/6

旋转的

Now, we'd like to obtain the blue circles in: 现在,我们要获得蓝色圆圈:

最后结果

To do this we need several steps: 为此,我们需要几个步骤:

  • Build a translation matrix to convert the points such that the center of the rotation is the center of the axes: 建立一个平移矩阵以转换点,使旋转的中心为轴的中心:

     T = 1 0 -115 0 1 -100 0 0 1 

    (the -115 and -100 are the negatives of the center of the atoms) -115-100是原子中心的负数)

  • Translate the points such that the two centers overlap (and we get the red atoms) 平移点,使两个中心重叠(我们得到红色原子)

     T * atoms = -25 25 0 0 1 1 

    (observe that our new two points are symmetrical around the origin) (观察到我们新的两个点围绕原点对称)

  • Rotate the new points (and we get the green circles) 旋转新点(我们得到绿色圆圈)

     R * T * atoms -21.6506 21.6506 -12.5000 12.5000 1.0000 1.0000 
  • Finally, translate everything back 最后,将所有内容翻译回去

     inv(T) * R * T * atoms = 93.3494 136.6506 87.5000 112.5000 1.0000 1.0000 

Final remarks : 最后说明

  1. The output that you've obtained is explicable due to the fact that my origin is in the lower corner/middle of the figure while yours is in the upper corner. 您获得的输出是可解释的,因为我的原点在图形的下角/中,而您的原点在上角。

  2. Because of this you might have to also reverse the order of the multiplications: point * translation * rotation * translation . 因此,您可能还必须颠倒乘法的顺序: point * translation * rotation * translation Check what works properly. 检查什么工作正常。

  3. I cheated a little and did the transformation on both points at the same time. 我有点作弊,并同时在两点上进行了转换。 Luckily, the results are the same as if doing the transformation on each point in turn. 幸运的是,结果与在每个点上依次进行转换一样。

  4. Every 2D/3D transform can be written in term of matrices. 每个2D / 3D变换都可以用矩阵表示。 We use 3x3 matrices for 2D and 4x4 for 3D for this. 为此,我们将3x3矩阵用于2D,将4x4矩阵用于3D。 Basically, we work with Homogenous coordinates 基本上,我们使用同质坐标

  5. Finally, see Transformation matrix for more examples and details. 最后,有关更多示例和详细信息,请参见转换矩阵

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM