简体   繁体   中英

Rotating a dynamic 3D array of positions

So I have a system that holds a cluster of items in positions. The cluster is stored in an array as follows:

int[,,] = int[length, width, height];

Length, width, and height can all be different depending on the cluster. If I wanted to rotate the entire cluster by a set of degrees (ranging 0 to 360):

double rX, double rZ, double rY

How can I determine the new positions of each item and export in a new array?


My busted attempts all start like this:

int iX = Math.Abs(rX / 90), iZ = Math.Abs(rZ / 90), iY = Math.Abs(rY / 90);
if (iY == 1) // 90 or -90 degrees
{
    group.Length = (rY / 90) * back.Width;
    group.Width  = (rY / 90) * back.Length;
}
else if (iY == 2) // 180 degrees
{
    group.Length *= -1;
    group.Width  *= -1;
}
if (iZ == 1) // 90 or -90 degrees
{
    group.Length = (rZ / 90) * back.Height;
    group.Height = (rZ / 90) * back.Length;
}
else if (iZ == 2) // 180 degrees
{
    group.Length *= -1;
    group.Height *= -1;
}
if (iX == 1) // 90 or -90 degrees
{
    group.Width = (rX / 90) * back.Height;
    group.Height = (rX / 90) * back.Width;
}
else if (iX == 2) // 180 degrees
{
    group.Width *= -1;
    group.Height *= -1;
}
 for(int gX = 0; gX < group.Length; gX++)
{
    for (int gZ = 0; gZ < group.Width; gZ++)
    {
        for (int gY = 0; gY < group.Height; gY++)
        {
            //I lose track here.
        }
    }
}

From there I don't know where to go. group is the cluster I'm trying to rotate, and back is a copy of group before these operations. The array in this cluster is like this:

Cluster.Items[,,]

And it's sizes are set to the dimensions of the group . The array is based on a X (Length) Z (Width) Y (Height) axis.

I'm guessing the answer has something to do with matrices and flipping certain axis.

You will need a rotation matrix.

A rotation matrix is a matrix which when multiplied with a vector, will result is a rotation of that vector.

there are three types of rotation matrices

Rotation around x-Axis

Rx(a) =  [ 1   0      0     0,
           0 cos(a) -sin(a) 0,
           0 sin(a)  cos(a) 0,
           0   0      0     1]

around y-Axis

Ry(a) =  [ cos(a)  0 sin(a) 0,
             0     1  0     0,
           -sin(a) 0 cos(a) 0,
             0     0  0     1]

ans rotation around z-Axis

Rz(a) =  [ cos(a) -sin(a) 0 0,
           sin(a) cos(y)  0 0,
             0      0     1 0,
             0      0     0 0]

More maths about rotation matrices you will find here

I'm still not convinced of your data structure. But let me just answer your question.

First, specify an order of rotations. In the following, I assume the order x, z, y. Then, find the according rotation matrix (eg from here ). Then, multiply the position vector with the matrix to get the new vector.

If the old vector has coordinates x, y, z , then the new vector's x-coordinate would be (first row of the matrix):

newX = x * cos(rZ) * cos(rY) - y * sin(rZ) + z * cos(rZ) * sin(rY)

So the first entry in the row is multiplied with x , the second with y and so on. Insert the correct angles any you're done.

Since cosine and sine are always -1, 0, or 1 for degrees that are multiples of 90°, the according calculation can be improved to not use the actual sine and cosine functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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