How can I rotate a 3D vector using a norm direction and angle in C++?

In summary, the individual is asking for suggestions on how to rotate a vector in 3D using a C++ class for a math vector. They would like the user to be able to input the direction of rotation as a norm vector and the angle, and have the function automatically rotate the initial vector and return the new components. They are considering using a matrix or quaternion class to implement this rotation. They share their current understanding of how to do the rotation using a quaternion and ask for confirmation. Another user suggests using a matrix and explains the steps for implementing the rotation, including accounting for an arbitrary point. The individual confirms that their rotation is for a homogeneous magnetic field and thanks the other user for their help.
  • #1
TheDestroyer
402
1
I have a C++ class for a math vector. I would like to add a function that rotates this vector in 3D, so that the user could input the direction of rotation as a norm vector, and the angle with which he would like to rotate, and have the function automatically rotate the initial vector for him and return the new 3 components.

What's the way to do it? could you guys please provide me with this transformation as

v'(vx',vy',vz') = f(θ,a,b,c,v(vx,vy,vz))

Where θ is the angle, a,b,c, is the norm to rotation direction, and v is the initial vector, and v' is the rotated vector.

Thank you for any efforts :-)
 
Mathematics news on Phys.org
  • #2
TheDestroyer said:
I have a C++ class for a math vector. I would like to add a function that rotates this vector in 3D, so that the user could input the direction of rotation as a norm vector, and the angle with which he would like to rotate, and have the function automatically rotate the initial vector for him and return the new 3 components.

What's the way to do it? could you guys please provide me with this transformation as

v'(vx',vy',vz') = f(θ,a,b,c,v(vx,vy,vz))

Where θ is the angle, a,b,c, is the norm to rotation direction, and v is the initial vector, and v' is the rotated vector.

Thank you for any efforts :-)

Hey TheDestroyer.

In terms of rotating vectors, you can do it using a matrix or you can use what is known as quaternions.

If you want to do rotations where you 'interpolate' between rotations then my advice is to implement a quaternion class and then convert the representation to a matrix. If you don't need this much flexibility then just use a matrix class. In terms of the actual implementation you can use existing code (search and you'll find dozens of implementations) or if you want to code up your own use this formula:

http://en.wikipedia.org/wiki/Axis-angle_representation#Rotating_a_vector

For quaternions, again search the internet for quaterion classes. You should get tonnes of hits. If you don't look up any decent open source game engine and there should be a quaternion and matrix class: Also Intel have optimized routines for various matrix routines using SSE and SSE2 instructions. I promise you there are tonnes of implementations already shared and use google to find them.

In terms of the way to do this:

1) Create rotation matrix M
2) Transform vector by calculating v' = Mv
3) v' is rotated vector.
 
  • #3
Thanks a lot for your answer, chiro.

I would not like to complicate stuff by creating a Quaternion class, but I could use the quaternion and substitute it in the rotation matrix. It's apparently just simpler.

So what I got is the following: To rotate a vector v(vx,vy,vz) around a vector w(wx,wy,wz) with the angle θ I do the following:

1. Normalize w (from now on w is normalised)
2. Create the quaternion:

q0 = Cos[θ/2]
q1 = Sin[θ/2]*wx
q2 = Sin[θ/2]*wy
q3 = Sin[θ/2]*wz

3. Create the rotation matrix from that:


\begin{array}{ccc}
\text{q0}^2+\text{q1}^2-\text{q2}^2-\text{q3}^2 & 2 \text{q1} \text{q2}-2 \text{q0} \text{q3} & 2 \text{q0} \text{q2}+2 \text{q1} \text{q3} \\
2 \text{q1} \text{q2}+2 \text{q0} \text{q3} & \text{q0}^2-\text{q1}^2+\text{q2}^2-\text{q3}^2 & -2 \text{q0} \text{q1}+2 \text{q2} \text{q3} \\
-2 \text{q0} \text{q2}+2 \text{q1} \text{q3} & 2 \text{q0} \text{q1}+2 \text{q2} \text{q3} & \text{q0}^2-\text{q1}^2-\text{q2}^2+\text{q3}^2
\end{array}

4. Multiply it with the vector.

Hope it's right :-) Please confirm that!
 
  • #4
TheDestroyer said:
Thanks a lot for your answer, chiro.

I would not like to complicate stuff by creating a Quaternion class, but I could use the quaternion and substitute it in the rotation matrix. It's apparently just simpler.

So what I got is the following: To rotate a vector v(vx,vy,vz) around a vector w(wx,wy,wz) with the angle θ I do the following:

1. Normalize w (from now on w is normalised)
2. Create the quaternion:

q0 = Cos[θ/2]
q1 = Sin[θ/2]*wx
q2 = Sin[θ/2]*wy
q3 = Sin[θ/2]*wz

3. Create the rotation matrix from that:


\begin{array}{ccc}
\text{q0}^2+\text{q1}^2-\text{q2}^2-\text{q3}^2 & 2 \text{q1} \text{q2}-2 \text{q0} \text{q3} & 2 \text{q0} \text{q2}+2 \text{q1} \text{q3} \\
2 \text{q1} \text{q2}+2 \text{q0} \text{q3} & \text{q0}^2-\text{q1}^2+\text{q2}^2-\text{q3}^2 & -2 \text{q0} \text{q1}+2 \text{q2} \text{q3} \\
-2 \text{q0} \text{q2}+2 \text{q1} \text{q3} & 2 \text{q0} \text{q1}+2 \text{q2} \text{q3} & \text{q0}^2-\text{q1}^2-\text{q2}^2+\text{q3}^2
\end{array}

4. Multiply it with the vector.

Hope it's right :-) Please confirm that!

I haven't confirmed the details of the matrix but yeah that should that work.

Also just one thing I need to add: if it is around the origin then the above should work. If you need to rotate it about an arbitrary point, then you need to translate the vector first by the negative of this point, rotate it, and then translate it back. You can use matrices to do transformations and then you can just compose the matrices to get your final transformation matrix.

This will be useful to know if you end up doing transformations that are hierarchical like for example modelling things like human movement (hands connected to arms connected to torso kinda thing) or doing some kind of modelling like say a solar system (circular orbits). If you want to create a general dynamics system of some sort, then you will have to do this (think about when you have check a chain through the air and each link is effect the motion of the entire chain).
 
  • #5
Actually what I want to rotate is simply a homogeneous magnetic field for a simulation. I think that thing with the point isn't necessary, since it's a homogeneous vector field.

Thanks a lot, buddy :-)

Have a nice day!
 
  • #6
If all you want to do is one rotation it's faster and easier to use the rotation axis as a vector. Suppose [itex]\vec v[/itex] is the vector you want to rotate, [itex]\hat u[/itex] is the unit vector along the rotation axis, and [itex]\theta[/itex] is the rotation angle.

Define
[tex]\begin{aligned}
\vec v_1 &= (\hat u \cdot \vec v)\hat u \\
\vec v_3 &= \hat u \times \vec v \\
\vec v_2 &= \vec v - \vec v_1
\end{aligned}[/tex]

(Note that [itex]\vec v_2 = \vec v_3 \times \hat u[/itex], but computing via subtraction is a lot faster.)

The rotated vector is simply
[tex]\vec v_{u,\theta} = \vec v_1 + \cos\theta\,\vec v_2 + \sin\theta\,\vec v_3[/tex]

The total cost is 18 multiplies, 14 adds. Building the rotation matrix alone costs that much. Once you have the rotation matrix, using it only costs 9 multiplies, 6 adds. If you are going to reuse the same rotation more than twice it pays off to create and use the matrix.
 
  • #7
Thanks a lot, D H. I'm going to try that :)
 

Related to How can I rotate a 3D vector using a norm direction and angle in C++?

1. What is rotation around the origin?

Rotation around the origin is a transformation in which a shape is rotated around a fixed point, also known as the origin (0,0) on a coordinate plane.

2. How is rotation around the origin measured?

Rotation around the origin is measured in degrees or radians. In a 2D coordinate plane, a positive angle is measured counterclockwise and a negative angle is measured clockwise.

3. What is the difference between positive and negative rotation around the origin?

Positive rotation around the origin is in the counterclockwise direction, while negative rotation is in the clockwise direction. This is important when determining the final position of a shape after rotation.

4. Can any shape be rotated around the origin?

Yes, any shape can be rotated around the origin as long as it is on a 2D coordinate plane. The shape may change in size and orientation, but it will still maintain its original properties.

5. How does rotation around the origin affect the coordinates of a shape?

Rotation around the origin changes the coordinates of a shape by applying a specific transformation to each point. For a counterclockwise rotation, the x and y coordinates of each point are multiplied by a cosine and sine value, respectively. For a clockwise rotation, the x and y coordinates are multiplied by a negative cosine and sine value, respectively.

Similar threads

Replies
2
Views
1K
Replies
1
Views
762
Replies
20
Views
3K
Replies
4
Views
11K
Replies
7
Views
316
  • Calculus
Replies
15
Views
2K
  • General Math
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Astronomy and Astrophysics
Replies
3
Views
3K
Back
Top