(* :Title: Rotations *) (* :Author: Stephen Wolfram, modified by Jan Mangaldan *) (* :Summary: This package provides functions for rotating points in 2 and 3 dimensions. *) (* :Context: Geometry`Rotations` *) (* :Package Version: 2.0 *) (* :Copyright: Copyright 1987-2003, Wolfram Research,Inc. *) (* :History: *) (* :Keywords: *) (* :Source: *) (* :Warning: *) (* :Mathematica Version: 2.0 *) (* :Limitation: *) (* :Discussion: *) BeginPackage["Geometry`Rotations`"] (** Two dimensions **) RotationMatrix2D::usage = "RotationMatrix2D[theta] gives the matrix for rotation by angle theta in two dimensions." Rotate2D::usage = "Rotate2D[vec, theta, (pt:{0,0})] rotates the vector vec by angle theta about point pt." (** Three dimensions **) RotationMatrix3D::usage = "RotationMatrix3D[psi, theta, phi, opts] gives the matrix for rotation by the specified Euler angles in three dimensions. The convention used for the Euler angles depends upon the setting for AngleConvention." Rotate3D::usage = "Rotate3D[vec, psi, theta, phi, (pt:{0, 0, 0}), opts] rotates the vector vec by specified Euler angles about point pt. The convention used for the Euler angles depends upon the setting for AngleConvention." AngleConvention::usage = "AngleConvention is an option to RotationMatrix3D and Rotate3D that specifies which Euler angle convention to use. Possible values are XConvention, YConvention, and XYZConvention." XConvention::usage = "XConvention is a choice for the option AngleConvention of RotationMatrix3D and Rotate3D. In the x-convention, psi is a rotation about the z-axis, theta is a rotation about the x-axis, and phi is a rotation about the z-axis (again). theta should be between 0 and Pi inclusive." YConvention::usage = "YConvention is a choice for the option AngleConvention of RotationMatrix3D and Rotate3D. In the y-convention, psi is a rotation about the z-axis, theta is a rotation about the y-axis, and phi is a rotation about the z-axis (again). theta should be between 0 and Pi inclusive." XYZConvention::usage = "XYZConvention is a choice for the option AngleConvention of RotationMatrix3D and Rotate3D. In the xyz-convention or pitch-roll-yaw convention, psi is a rotation about the y-axis (roll), theta is a rotation about the x-axis (pitch), and phi is a rotation about the z-axis (yaw)." Begin["`Private`"] (** Two dimensions **) RotationMatrix2D[theta_] := {{Cos[theta], Sin[theta]}, {-Sin[theta], Cos[theta]}} Rotate2D[vec:{_,_}, theta_] := RotationMatrix2D[theta] . vec Rotate2D[vec:{_,_}, theta_, pt:{_,_}] := pt + RotationMatrix2D[theta] . (vec - pt) (** Three dimensions **) RotationMatrix3D::badopt = "Value of AngleConvention should either be XConvention, YConvention, or XYZConvention." Options[RotationMatrix3D]= Options[Rotate3D]={AngleConvention\[Rule]XConvention}; RotationMatrix3D[psi_, theta_, phi_, opts___] := Module[{conv}, conv = AngleConvention /. {opts} /. Options[RotationMatrix3D]; Which[conv === XConvention, {{Cos[psi], Sin[psi], 0}, {-Sin[psi], Cos[psi], 0}, {0, 0, 1}} . {{1, 0, 0}, {0, Cos[theta], Sin[theta]}, {0, -Sin[theta], Cos[theta]}} . {{Cos[phi], Sin[phi], 0}, {-Sin[phi], Cos[phi], 0}, {0, 0, 1}}, conv === YConvention, {{Sin[psi], -Cos[psi], 0}, {Cos[psi], Sin[psi], 0}, {0, 0, 1}} . {{1, 0, 0}, {0, Cos[theta], -Sin[theta]}, {0, Sin[theta], Cos[theta]}} . {{-Sin[phi], Cos[phi], 0}, {-Cos[phi], -Sin[phi], 0}, {0, 0, 1}}, conv === XYZConvention, {{1, 0, 0}, {0, Cos[theta], Sin[theta]}, {0, -Sin[theta], Cos[theta]}} . {{Cos[psi], 0, Sin[psi]}, {0, 1, 0}, {-Sin[psi], 0, Cos[psi]}} . {{Cos[phi], Sin[phi], 0}, {-Sin[phi], Cos[phi], 0}, {0, 0, 1}}] /; If[ !MemberQ[{XConvention, YConvention, XYZConvention}, conv], Message[RotationMatrix3D::badopt]; False, True]] Rotate3D[vec : {_, _, _}, psi_, theta_, phi_, opts___] := Module[{conv}, conv = AngleConvention /. {opts} /. Options[Rotate3D]; RotationMatrix3D[psi, theta, phi, AngleConvention -> conv].vec] Rotate3D[vec : {_, _, _}, psi_, theta_, phi_, pt : {_, _, _}, opts___] := Module[{conv}, conv = AngleConvention /. {opts} /. Options[Rotate3D]; pt + RotationMatrix3D[psi, theta, phi, AngleConvention -> conv].(vec - pt)] End[] EndPackage[ ] Null