// sv-6.cc // A ZOOM PhysicsVectors tutorial example // // Tutorial example 6 for the SpaceVector and UnitVector class: // // Rotating SpaceVectors and UnitVectors // // * Rotating around a given axis // * Rotating around a coordinate axis // * Rotations by Euler Angles // // This example shows how to do "direct" rotations. // The PhysicsVectors package also supports Rotation objects which // may be manipulated and applied to vectors. // To see how the sames things can be done with the Rotation class, look // at tutorial example ro-1 for construction and access, and ro-3 for // application to vectors. Bear in mind that you need not use Rotation // if all that is needed is to rotate vectors by some given amount. // Key lines are marked by // <------------- // Get the UnitVector and SpaceVector classes #include "ZMutility/ZMenvironment.h" #include "ZMutility/FixedTypes.h" #include "PhysicsVectors/SpaceVector.h" #include "PhysicsVectors/UnitVector.h" ZM_USING_NAMESPACE( zmpv ) USING( std::cout ) USING( std::endl ) // These classes themselves pull in FixedTypes, but just to // remind ourselves that we are using Float8, not double, for // 64-bit floats... #include "ZMutility/FixedTypes.h" ZM_USING_NAMESPACE( zmfxt ) // This example will use iostream output, so get that -- // in a portable way. #include "ZMutility/iostream" using std::cout; using std::endl; const Float8 PI = 3.1415926535897931; int main() { // ***** Rotating around a given axis // Here is how to rotate a vector about a UnitVector axis //------------------------------------------------------- Float8 theta = 45; Float8 phi = 30; UnitVector u1 ( theta, DEGREES, phi, DEGREES ); cout << "axis =" << u1 << endl; SpaceVector v1 ( 12, -16, 48 ); cout << "Before rotation vector is " << v1 << endl; cout << "Magnitude is " << v1.mag() << endl; v1.rotate ( u1, PI/3 ); // <------------- cout << "After PI/3 rotation vector is " << v1 << endl; cout << "Magnitude is " << v1.mag() << endl; UnitVector u2 = Y_HAT; cout << "Before rotation unit vector is " << u2 << endl; u2.rotate ( u1, PI/2 ); // <------------- cout << "After PI/2 rotation unit vector is " << u2 << endl; // Here is how to supply a non-zero SpaceVector axis //-------------------------------------------------- SpaceVector v2 = 20 * u1; cout << "axis =" << v2 << endl; v1.rotate ( v2, -PI/3 ); // <------------- cout << "After -PI/3 rotation vector is " << v1 << endl; // Here is how to form a new vector by rotating an existing vector //---------------------------------------------------------------- SpaceVector v3 = rotationOf (v1, u1, PI/4); // <------------- cout << "After PI/ rotation new vector is " << v3 << endl; cout << "Old vector remains " << v1 << endl; // ***** Rotating around a coordinate axis // Here is how to rotate around Z with a minimal amount of computation //-------------------------------------------------------------------- v3.rotateZ ( PI/2 ); // <------------- cout << "After rotating by PI/2 about Z axis this is " << v3 << endl; // This is equivalent to, but much faster than: v3.rotate ( Z_HAT, PI/2 ); cout << "After another PI/2 about Z axis this is " << v3 << endl; // Similarly, v1.rotateX (PI/3); // <------------- v2.rotateY (PI); // <------------- // And the analog to rotationOf is also there: v1 = rotationZOf (v3, PI/2); // <------------- cout << "After yet another PI/2 about Z axis, " << v1 << endl; // ***** Rotations by Euler Angles // Here is how to rotate a vector using Euler Angles //-------------------------------------------------- phi = PI/6; theta = PI/4; Float8 psi = -PI/3; cout << "Vector is " << v3 << endl; v3.rotate ( phi, theta, psi ); // <------------- cout << "Now vector is " << v3 << endl; // The Euler angles definition is that of // Goldstein Classical Mechanics, page 109 v2 = rotationOf ( v3, PI/3, PI/4, -PI/6 ); // <------------- cout << "New vector is " << v2 << endl; return 0; } /* end of main */