function [vecRot] = rotVecToLoc(vecToRot,rotQuat)
%rotVecToLoc Rotate vector from Global to Local frames
% This function rotates a vector from the global coordinate frame to
% the local coordinate frame, using the rotation quaternion. It
% implements Equations 41 & 42 in Aircraft Quaternions, but from local to
% global frames. rotQuat is assumed to be a column 4-vector. vecToRot
% and vecRot are assumed to be column 3-vectors.

T(1) = -vecToRot(1)*rotQuat(2) - vecToRot(2)*rotQuat(3) - vecToRot(3)*rotQuat(4);
T(2) = vecToRot(1)*rotQuat(1) + vecToRot(2)*rotQuat(4) - vecToRot(3)*rotQuat(3);
T(3) = -vecToRot(1)*rotQuat(4) + vecToRot(2)*rotQuat(1) + vecToRot(3)*rotQuat(2);
T(4) = vecToRot(1)*rotQuat(3) - vecToRot(2)*rotQuat(2) + vecToRot(3)*rotQuat(1);
vecRot(1) = rotQuat(1)*T(2) - rotQuat(2)*T(1) - rotQuat(3)*T(4) + rotQuat(4)*T(3);
vecRot(2) = rotQuat(1)*T(3) + rotQuat(2)*T(4) - rotQuat(3)*T(1) - rotQuat(4)*T(2);
vecRot(3) = rotQuat(1)*T(4) - rotQuat(2)*T(3) + rotQuat(3)*T(2) - rotQuat(4)*T(1);
vecRot = transpose(vecRot);
end