MTIMES Multiply two quaternion objects Invoked by the * operator, handle two cases: q1*q2 standard quaternion multiplication q1*v rotate vector v by quaternion q1*s multiply vector v by scalar
0001 %MTIMES Multiply two quaternion objects 0002 % 0003 % Invoked by the * operator, handle two cases: 0004 % 0005 % q1*q2 standard quaternion multiplication 0006 % q1*v rotate vector v by quaternion 0007 % q1*s multiply vector v by scalar 0008 0009 % Copyright (C) 1999-2008, by Peter I. Corke 0010 % 0011 % This file is part of The Robotics Toolbox for Matlab (RTB). 0012 % 0013 % RTB is free software: you can redistribute it and/or modify 0014 % it under the terms of the GNU Lesser General Public License as published by 0015 % the Free Software Foundation, either version 3 of the License, or 0016 % (at your option) any later version. 0017 % 0018 % RTB is distributed in the hope that it will be useful, 0019 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0020 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0021 % GNU Lesser General Public License for more details. 0022 % 0023 % You should have received a copy of the GNU Leser General Public License 0024 % along with RTB. If not, see <http://www.gnu.org/licenses/>. 0025 0026 function qp = mtimes(q1, q2) 0027 0028 if isa(q1, 'quaternion') & isa(q2, 'quaternion') 0029 %QQMUL Multiply unit-quaternion by unit-quaternion 0030 % 0031 % QQ = qqmul(Q1, Q2) 0032 % 0033 % Return a product of unit-quaternions. 0034 % 0035 % See also: TR2Q 0036 0037 % Copyright (C) 1993 Peter Corke 0038 0039 % decompose into scalar and vector components 0040 s1 = q1.s; v1 = q1.v; 0041 s2 = q2.s; v2 = q2.v; 0042 0043 % form the product 0044 qp = quaternion([s1*s2-v1*v2' s1*v2+s2*v1+cross(v1,v2)]); 0045 0046 elseif isa(q1, 'quaternion') & isa(q2, 'double'), 0047 0048 %QVMUL Multiply vector by unit-quaternion 0049 % 0050 % VT = qvmul(Q, V) 0051 % 0052 % Rotate the vector V by the unit-quaternion Q. 0053 % 0054 % See also: QQMUL, QINV 0055 0056 % Copyright (C) 1993 Peter Corke 0057 0058 % MOD HISTORY 0059 % fixed error in q-v product, added inv(q1) on RHS 0060 0061 if length(q2) == 3, 0062 qp = q1 * quaternion([0 q2(:)']) * inv(q1); 0063 qp = qp.v; 0064 elseif length(q2) == 1, 0065 qp = quaternion( double(q1)*q2); 0066 else 0067 error('quaternion-vector product: must be a 3-vector or scalar'); 0068 end 0069 0070 elseif isa(q2, 'quaternion') & isa(q1, 'double'), 0071 if length(q1) == 3, 0072 qp = q2 * quaternion([0 q1(:)']) * inv(q2); 0073 qp = qp.v; 0074 elseif length(q1) == 1, 0075 qp = quaternion( double(q2)*q1); 0076 else 0077 error('quaternion-vector product: must be a 3-vector or scalar'); 0078 end 0079 end