• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1383
  • Last Modified:

Rotating Vectors C++

Hi all. I have a vector defined by:
vector < vector < vector < bool > > >

This vector defines an object in 3 dimensions. In other words, each true value represents a point and each false value represents a space.

I would basically like to "rotate" this vector on the x y and z axis separately. How would I do this?
0
momonja
Asked:
momonja
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
Infinity08Commented:
Start by getting a 2D matrix rotated ... The 3D situation is equivalent, in that it comes down to rotating several 2D matrices (at the same time) instead of just one.

To rotate a 2D matrix, I assume we're talking about right-angled rotations, like :

        0 1 2 3
        4 5 6 7
        8 9 A B
        C D E F

becomes the following after a rotation over 90 degrees clockwise :

        C 8 4 0
        D 9 5 1
        E A 6 2
        F B 7 3

By looking at that example, it should be clear which positions move where when rotating, and implementing a matrix rotation shouldn't be too difficult.
0
 
t0t0Commented:

Hiya momonia

You can rotate vector coordinates using matrix transformation.
For linear displacement you should look at translation.
Oh, and you can use inverse transformations to remove hidden surfaces.
Apart from that and a bit of trig the the rest is fairly simple.
Have fun.

 

// code to multiply matrices (concatenation) 
 
void multiply_matrices(float a[4][4], float b[4][4])
{
  float tmp[4][4];
  int i, j; 
  for (j = 0; j < 4; j++)
    for (i = 0; i < 4; i++)
      tmp[i][j] = a[i][0] * b[0][j]
                 +a[i][1] * b[1][j]
                 +a[i][2] * b[2][j]
                 +a[i][3] * b[3][j]; 
  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++)
      a[i][j] = tmp[i][j];
} 
 
 
 
// multiply with: 
 
// x-rotation 
 
//    1        0        0        0
//    0        cos(x)   sin(x)   0
//    0        -sin(x)  cos(x)   0
//    0        0        0        1 
 
 
  
// y-rotation 
 
//    cos(y)   0        -sin(y)  0
//    0        1        0        0
//    sin(y)   0        cos(y)   0
//    0        0        0        1 
 
 
  
// z-rotation 
 
//    cos(z)   sin(z)   0        0
//    -sin(z)  cos(z)   0        0
//    0        0        1        0
//    0        0        0        1 
 
 
 
// Maths required to transform a vector using a matrix
// x0 etc are the original object space coords for the vector 
 
x = x0 * a[0][0] + y0 * a[1][0] + z0 * a[2][0] + a[3][0];
y = x0 * a[0][1] + y0 * a[1][1] + z0 * a[2][1] + a[3][1];
z = x0 * a[0][2] + y0 * a[1][2] + z0 * a[2][2] + a[3][2]; 
 
// You may need to look into homogenous coordinate systems... 

Open in new window

0
 
t0t0Commented:
Sorry to repeat for the sake of clarity...
void multiply_matrices(float a[4][4], float b[4][4])
{
  float tmp[4][4];
 
  int i, j; 
 
  for (j = 0; j < 4; j++)
    for (i = 0; i < 4; i++)
      tmp[i][j] = a[i][0] * b[0][j]
                + a[i][1] * b[1][j]
                + a[i][2] * b[2][j]
                + a[i][3] * b[3][j]; 
 
  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++)
      a[i][j] = tmp[i][j];
} 

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
momonjaAuthor Commented:
t0t0: vectors, not arrays

is there any code that anyone can show me? I'm having trouble doing it myself.
0
 
Infinity08Commented:
In order to help you better, we'll need to know what this will be used for ... Can you give a bit more background information ?

Can you also confirm that my interpretation (in my first post) is correct ?
0
 
t0t0Commented:
momonia

I've just reread your post - I thought maybe i'd misinterpreted your question.

Let me get this right... you want to rotate the position of a single point in space whose vector coordinates are (x, y, z) around either the x, y or z axis. Is that about the size of it?

Because if that's the case then you'll have to get to grips with matrix transformation.... unless you want to skip the formal stuff and invent a lazy way to do something that's been done day in and day out for the past century using the tools already suggested by myself and infinty08.

Let's say you've got a point somewhere in space. The point's position is denoted by it's x, y and z coordinates. Would you agree? In that case we can say p = (x, y, z).

There are three things you might want to do with a point in space...

Translation - moving a point in any plain leaving the other two coordinates of the point unchanged.

Rotation - rotating the point around either the x, y or z axis, where the coordinate around the axis of rotation remains unchaged (think about it) and the other two coordinates change in relation to their original positions.

The other operation you might want to perform on an (x, y, z) vector coordinate is scaling, but I'm not going to discuss that.

Simple translation
Let's say you want to translate a point (x, y, z). You'll be altering it's relative position without introducing other changes (such as scaling or rotation). We can denote transformed qhantities by a prime ( ' ) and if the displacement vector is t, then the translation can be expressed as:

        r' = r + t

where r is the position vector of the original point and r' is the position vector of the same point after translation. Applying this transformation on every vertex of the original point results i a new set of vertices that define the transformed point.

When expressed in the component form, the transformed coorinates are evaluated from:

                                                  /  1   0   0   0  \
                                                  |  0   1   0   0  |
        (x', y', z', 1) = (x, y, z, 1)   |  0   0   1   0  |
                                                  \  tx  ty  tz  1  /

where tx, ty and tz are the components of the translation vector t. And that's just simple displacement!

ROTATION
Now, suppose a point rotates about the z axis through a an angle g. The axis of rotation is fixed, only the x and y coordinates change. The angle of rotation is measured in a couterclockwise direction when the point is seen along the rotation axis, towards the origin. This is represented by:

                                                  /  cos(g)   sin(g)    0   0  \
                                                  |  -sin(g)   cos(g)   0   0  |
        (x', y', z', 1) = (x, y, z, 1)   |      0           0        1   0  |
                                                  \      0           0        0   1  /

Graphically, the rotation about the y axis is given by:

                                                  /  cos(g)   0    -sin(g)   0  \
                                                  |      0        1        0        0  |
        (x', y', z', 1) = (x, y, z, 1)   |  sin(g)    0    cos(g)    0  |
                                                  \      0       0         0        1  /

And rotation about the x axis is given by:

                                                  /  1         0            0        0  \
                                                  |   0     cos(g)   sin(g)     0  |
        (x', y', z', 1) = (x, y, z, 1)   |  0     -sin(g)    cos(g)   0  |
                                                  \  0         0            0        1  /


To display a point in 3D onto a 2D plane, say a computer screen, it has to be projected using parallel or perspective projection but that's another matter and one which i'll need to look up - something perhaps you might like to do....
0
 
Jose ParrotGraphics ExpertCommented:
To transform these vectors,  you need coordinates.
In your case, a point coordinates are its indexes.
This is done in a four steps procedure:
  1. Convert point indexes to coordinates
  2. Set such point to false (it will be in another place...)
  3. Do the transformation by rotating, scaling or translating.
  4. Convert transformed coordinate back to index
As a matter of ease understanding, follows an example of single displacement (translation)
be  [v1][v2][v3]  the vector to translate
step 1:     x = v1; y = v2; z = v3;
step 2:     vector[v1][v2][v3] = false // it will be in another place...
step 3:     x = v1+dx;  y = v2+dy;  z=v3+dz;
step 4:    vector[x][y][z];
The same procedure applies to rotation and scaling.
Jose
0
 
Jose ParrotGraphics ExpertCommented:
oooops, fix pack 1:
step 4:    vector[x][y][z] = true;
Jose
0
 
t0t0Commented:
momonia

Thank you for accepting my answer. Good luck with your vectors!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now